Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 14 | #include <limits.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 15 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 16 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 17 | #include <lauxlib.h> |
| 18 | #include <lua.h> |
| 19 | #include <lualib.h> |
| 20 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 21 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 22 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 23 | #endif |
| 24 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 25 | #include <ebpttree.h> |
| 26 | |
| 27 | #include <common/cfgparse.h> |
Willy Tarreau | b059b89 | 2018-10-16 17:57:36 +0200 | [diff] [blame] | 28 | #include <common/compiler.h> |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 29 | #include <common/hathreads.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 30 | #include <common/initcall.h> |
| 31 | #include <common/xref.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 32 | #include <common/h1.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 33 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 34 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 35 | #include <types/hlua.h> |
| 36 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 37 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 38 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 39 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 40 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 41 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 42 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 43 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 44 | #include <proto/stats.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 45 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 46 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 47 | #include <proto/hlua_fcn.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 48 | #include <proto/http_fetch.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 49 | #include <proto/http_htx.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 50 | #include <proto/http_rules.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 51 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 52 | #include <proto/obj_type.h> |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 53 | #include <proto/queue.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 54 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 55 | #include <proto/payload.h> |
| 56 | #include <proto/proto_http.h> |
| 57 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 58 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 59 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 60 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 61 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 62 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 63 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 64 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 65 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 66 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 67 | * macro is used only for identifying the function that can |
| 68 | * not return because a longjmp is executed. |
| 69 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 70 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 71 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 72 | */ |
| 73 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 74 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 75 | #define MAY_LJMP(func) func |
| 76 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 77 | /* This couple of function executes securely some Lua calls outside of |
| 78 | * the lua runtime environment. Each Lua call can return a longjmp |
| 79 | * if it encounter a memory error. |
| 80 | * |
| 81 | * Lua documentation extract: |
| 82 | * |
| 83 | * If an error happens outside any protected environment, Lua calls |
| 84 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 85 | * exiting the host application. Your panic function can avoid this |
| 86 | * exit by never returning (e.g., doing a long jump to your own |
| 87 | * recovery point outside Lua). |
| 88 | * |
| 89 | * The panic function runs as if it were a message handler (see |
| 90 | * §2.3); in particular, the error message is at the top of the |
| 91 | * stack. However, there is no guarantee about stack space. To push |
| 92 | * anything on the stack, the panic function must first check the |
| 93 | * available space (see §4.2). |
| 94 | * |
| 95 | * We must check all the Lua entry point. This includes: |
| 96 | * - The include/proto/hlua.h exported functions |
| 97 | * - the task wrapper function |
| 98 | * - The action wrapper function |
| 99 | * - The converters wrapper function |
| 100 | * - The sample-fetch wrapper functions |
| 101 | * |
| 102 | * It is tolerated that the initilisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 103 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 104 | * |
| 105 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 106 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 107 | * because they must be exists in the program stack when the longjmp |
| 108 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 109 | * |
| 110 | * Note that the Lua processing is not really thread safe. It provides |
| 111 | * heavy system which consists to add our own lock function in the Lua |
| 112 | * code and recompile the library. This system will probably not accepted |
| 113 | * by maintainers of various distribs. |
| 114 | * |
| 115 | * Our main excution point of the Lua is the function lua_resume(). A |
| 116 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 117 | * of function and a lua_unlock() at the end of the function. So I |
| 118 | * conclude that the Lua thread safe mode just perform a mutex around |
| 119 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 120 | * easier for distro maintainers. |
| 121 | * |
| 122 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 123 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 124 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 125 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 126 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 127 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 128 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 129 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 130 | |
| 131 | #define SET_SAFE_LJMP(__L) \ |
| 132 | ({ \ |
| 133 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 134 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 135 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 136 | lua_atpanic(__L, hlua_panic_safe); \ |
| 137 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 138 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 139 | } else { \ |
| 140 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 141 | ret = 1; \ |
| 142 | } \ |
| 143 | ret; \ |
| 144 | }) |
| 145 | |
| 146 | /* If we are the last function catching Lua errors, we |
| 147 | * must reset the panic function. |
| 148 | */ |
| 149 | #define RESET_SAFE_LJMP(__L) \ |
| 150 | do { \ |
| 151 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 152 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 153 | } while(0) |
| 154 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 155 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 156 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 157 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 158 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 159 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 160 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 161 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 162 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 163 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 164 | /* The main Lua execution context. */ |
| 165 | struct hlua gL; |
| 166 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 167 | /* This is the memory pool containing struct lua for applets |
| 168 | * (including cli). |
| 169 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 170 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 171 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 172 | /* Used for Socket connection. */ |
| 173 | static struct proxy socket_proxy; |
| 174 | static struct server socket_tcp; |
| 175 | #ifdef USE_OPENSSL |
| 176 | static struct server socket_ssl; |
| 177 | #endif |
| 178 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 179 | /* List head of the function called at the initialisation time. */ |
| 180 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 181 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 182 | /* The following variables contains the reference of the different |
| 183 | * Lua classes. These references are useful for identify metadata |
| 184 | * associated with an object. |
| 185 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 186 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 187 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 188 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 189 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 190 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 191 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 192 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 193 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 194 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 195 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 196 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 197 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 198 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 199 | * because a task may remain alive during all the haproxy execution. |
| 200 | */ |
| 201 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 202 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 203 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 204 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 205 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 206 | * it is used for preventing infinite loops. |
| 207 | * |
| 208 | * I test the scheer with an infinite loop containing one incrementation |
| 209 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 210 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 211 | * to one interrupt each 10 000 instructions. |
| 212 | * |
| 213 | * configured | Number of |
| 214 | * instructions | loops executed |
| 215 | * between two | in milions |
| 216 | * forced yields | |
| 217 | * ---------------+--------------- |
| 218 | * 10 | 160 |
| 219 | * 500 | 670 |
| 220 | * 1000 | 680 |
| 221 | * 5000 | 700 |
| 222 | * 7000 | 700 |
| 223 | * 8000 | 700 |
| 224 | * 9000 | 710 <- ceil |
| 225 | * 10000 | 710 |
| 226 | * 100000 | 710 |
| 227 | * 1000000 | 710 |
| 228 | * |
| 229 | */ |
| 230 | static unsigned int hlua_nb_instruction = 10000; |
| 231 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 232 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 233 | * be enforced on any memory allocation. |
| 234 | */ |
| 235 | struct hlua_mem_allocator { |
| 236 | size_t allocated; |
| 237 | size_t limit; |
| 238 | }; |
| 239 | |
| 240 | static struct hlua_mem_allocator hlua_global_allocator; |
| 241 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 242 | static const char error_500[] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 243 | "HTTP/1.0 500 Internal Server Error\r\n" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 244 | "Cache-Control: no-cache\r\n" |
| 245 | "Connection: close\r\n" |
| 246 | "Content-Type: text/html\r\n" |
| 247 | "\r\n" |
Joseph Herlant | 7fe1577 | 2018-11-15 10:07:32 -0800 | [diff] [blame] | 248 | "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n"; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 249 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 250 | /* These functions converts types between HAProxy internal args or |
| 251 | * sample and LUA types. Another function permits to check if the |
| 252 | * LUA stack contains arguments according with an required ARG_T |
| 253 | * format. |
| 254 | */ |
| 255 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 256 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 257 | __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] | 258 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 259 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 260 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 261 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 262 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 263 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 264 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 265 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 266 | do { \ |
| 267 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 268 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 269 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 270 | } while (0) |
| 271 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 272 | /* Used to check an Lua function type in the stack. It creates and |
| 273 | * returns a reference of the function. This function throws an |
| 274 | * error if the rgument is not a "function". |
| 275 | */ |
| 276 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 277 | { |
| 278 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 279 | 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] | 280 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 281 | } |
| 282 | lua_pushvalue(L, argno); |
| 283 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 284 | } |
| 285 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 286 | /* Return the string that is of the top of the stack. */ |
| 287 | const char *hlua_get_top_error_string(lua_State *L) |
| 288 | { |
| 289 | if (lua_gettop(L) < 1) |
| 290 | return "unknown error"; |
| 291 | if (lua_type(L, -1) != LUA_TSTRING) |
| 292 | return "unknown error"; |
| 293 | return lua_tostring(L, -1); |
| 294 | } |
| 295 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 296 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 297 | { |
| 298 | lua_Debug ar; |
| 299 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 300 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 301 | int filled = 0; |
| 302 | |
| 303 | while (lua_getstack(L, level++, &ar)) { |
| 304 | |
| 305 | /* Add separator */ |
| 306 | if (filled) |
| 307 | chunk_appendf(msg, ", "); |
| 308 | filled = 1; |
| 309 | |
| 310 | /* Fill fields: |
| 311 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 312 | * 'l': fills in the field currentline; |
| 313 | * 'n': fills in the field name and namewhat; |
| 314 | * 't': fills in the field istailcall; |
| 315 | */ |
| 316 | lua_getinfo(L, "Slnt", &ar); |
| 317 | |
| 318 | /* Append code localisation */ |
| 319 | if (ar.currentline > 0) |
| 320 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 321 | else |
| 322 | chunk_appendf(msg, "%s ", ar.short_src); |
| 323 | |
| 324 | /* |
| 325 | * Get function name |
| 326 | * |
| 327 | * if namewhat is no empty, name is defined. |
| 328 | * what contains "Lua" for Lua function, "C" for C function, |
| 329 | * or "main" for main code. |
| 330 | */ |
| 331 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 332 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 333 | |
| 334 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 335 | chunk_appendf(msg, "main chunk"); |
| 336 | |
| 337 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 338 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 339 | |
| 340 | else /* nothing left... */ |
| 341 | chunk_appendf(msg, "?"); |
| 342 | |
| 343 | |
| 344 | /* Display tailed call */ |
| 345 | if (ar.istailcall) |
| 346 | chunk_appendf(msg, " ..."); |
| 347 | } |
| 348 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 349 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 353 | /* This function check the number of arguments available in the |
| 354 | * stack. If the number of arguments available is not the same |
| 355 | * then <nb> an error is throwed. |
| 356 | */ |
| 357 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 358 | { |
| 359 | if (lua_gettop(L) == nb) |
| 360 | return; |
| 361 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 362 | } |
| 363 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 364 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 365 | * and the line number where the error is encountered. |
| 366 | */ |
| 367 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 368 | { |
| 369 | va_list argp; |
| 370 | va_start(argp, fmt); |
| 371 | luaL_where(L, 1); |
| 372 | lua_pushvfstring(L, fmt, argp); |
| 373 | va_end(argp); |
| 374 | lua_concat(L, 2); |
| 375 | return 1; |
| 376 | } |
| 377 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 378 | /* This functions is used with sample fetch and converters. It |
| 379 | * converts the HAProxy configuration argument in a lua stack |
| 380 | * values. |
| 381 | * |
| 382 | * It takes an array of "arg", and each entry of the array is |
| 383 | * converted and pushed in the LUA stack. |
| 384 | */ |
| 385 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 386 | { |
| 387 | switch (arg->type) { |
| 388 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 389 | case ARGT_TIME: |
| 390 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 391 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 392 | break; |
| 393 | |
| 394 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 395 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 396 | break; |
| 397 | |
| 398 | case ARGT_IPV4: |
| 399 | case ARGT_IPV6: |
| 400 | case ARGT_MSK4: |
| 401 | case ARGT_MSK6: |
| 402 | case ARGT_FE: |
| 403 | case ARGT_BE: |
| 404 | case ARGT_TAB: |
| 405 | case ARGT_SRV: |
| 406 | case ARGT_USR: |
| 407 | case ARGT_MAP: |
| 408 | default: |
| 409 | lua_pushnil(L); |
| 410 | break; |
| 411 | } |
| 412 | return 1; |
| 413 | } |
| 414 | |
| 415 | /* This function take one entrie in an LUA stack at the index "ud", |
| 416 | * and try to convert it in an HAProxy argument entry. This is useful |
| 417 | * with sample fetch wrappers. The input arguments are gived to the |
| 418 | * lua wrapper and converted as arg list by thi function. |
| 419 | */ |
| 420 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 421 | { |
| 422 | switch (lua_type(L, ud)) { |
| 423 | |
| 424 | case LUA_TNUMBER: |
| 425 | case LUA_TBOOLEAN: |
| 426 | arg->type = ARGT_SINT; |
| 427 | arg->data.sint = lua_tointeger(L, ud); |
| 428 | break; |
| 429 | |
| 430 | case LUA_TSTRING: |
| 431 | arg->type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 432 | arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data); |
Tim Duesterhus | f371b83 | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 433 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 434 | arg->data.str.size = arg->data.str.data; |
| 435 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 436 | break; |
| 437 | |
| 438 | case LUA_TUSERDATA: |
| 439 | case LUA_TNIL: |
| 440 | case LUA_TTABLE: |
| 441 | case LUA_TFUNCTION: |
| 442 | case LUA_TTHREAD: |
| 443 | case LUA_TLIGHTUSERDATA: |
| 444 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 445 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | return 1; |
| 449 | } |
| 450 | |
| 451 | /* the following functions are used to convert a struct sample |
| 452 | * in Lua type. This useful to convert the return of the |
| 453 | * fetchs or converters. |
| 454 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 455 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 456 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 457 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 458 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 459 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 460 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 461 | break; |
| 462 | |
| 463 | case SMP_T_BIN: |
| 464 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 465 | 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] | 466 | break; |
| 467 | |
| 468 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 469 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 470 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 471 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 472 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 473 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 474 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 475 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 476 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 477 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 478 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 479 | 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] | 480 | break; |
| 481 | default: |
| 482 | lua_pushnil(L); |
| 483 | break; |
| 484 | } |
| 485 | break; |
| 486 | |
| 487 | case SMP_T_IPV4: |
| 488 | case SMP_T_IPV6: |
| 489 | 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] | 490 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 491 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 492 | 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] | 493 | else |
| 494 | lua_pushnil(L); |
| 495 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 496 | default: |
| 497 | lua_pushnil(L); |
| 498 | break; |
| 499 | } |
| 500 | return 1; |
| 501 | } |
| 502 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 503 | /* the following functions are used to convert a struct sample |
| 504 | * in Lua strings. This is useful to convert the return of the |
| 505 | * fetchs or converters. |
| 506 | */ |
| 507 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 508 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 509 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 510 | |
| 511 | case SMP_T_BIN: |
| 512 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 513 | 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] | 514 | break; |
| 515 | |
| 516 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 517 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 518 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 519 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 520 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 521 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 522 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 523 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 524 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 525 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 526 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 527 | 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] | 528 | break; |
| 529 | default: |
| 530 | lua_pushstring(L, ""); |
| 531 | break; |
| 532 | } |
| 533 | break; |
| 534 | |
| 535 | case SMP_T_SINT: |
| 536 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 537 | case SMP_T_IPV4: |
| 538 | case SMP_T_IPV6: |
| 539 | 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] | 540 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 541 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 542 | 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] | 543 | else |
| 544 | lua_pushstring(L, ""); |
| 545 | break; |
| 546 | default: |
| 547 | lua_pushstring(L, ""); |
| 548 | break; |
| 549 | } |
| 550 | return 1; |
| 551 | } |
| 552 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 553 | /* the following functions are used to convert an Lua type in a |
| 554 | * struct sample. This is useful to provide data from a converter |
| 555 | * to the LUA code. |
| 556 | */ |
| 557 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 558 | { |
| 559 | switch (lua_type(L, ud)) { |
| 560 | |
| 561 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 562 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 563 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 564 | break; |
| 565 | |
| 566 | |
| 567 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 568 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 569 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 570 | break; |
| 571 | |
| 572 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 573 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 574 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 575 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data); |
Tim Duesterhus | f371b83 | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 576 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 577 | smp->data.u.str.size = smp->data.u.str.data; |
| 578 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 579 | break; |
| 580 | |
| 581 | case LUA_TUSERDATA: |
| 582 | case LUA_TNIL: |
| 583 | case LUA_TTABLE: |
| 584 | case LUA_TFUNCTION: |
| 585 | case LUA_TTHREAD: |
| 586 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 587 | case LUA_TNONE: |
| 588 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 589 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 590 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | return 1; |
| 594 | } |
| 595 | |
| 596 | /* This function check the "argp" builded by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 597 | * 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] | 598 | * 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] | 599 | * |
| 600 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 601 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 602 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 603 | __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] | 604 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 605 | { |
| 606 | int min_arg; |
| 607 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 608 | struct proxy *px; |
| 609 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 610 | |
| 611 | idx = 0; |
| 612 | min_arg = ARGM(mask); |
| 613 | mask >>= ARGM_BITS; |
| 614 | |
| 615 | while (1) { |
| 616 | |
| 617 | /* Check oversize. */ |
| 618 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 619 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /* Check for mandatory arguments. */ |
| 623 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 624 | if (idx < min_arg) { |
| 625 | |
| 626 | /* If miss other argument than the first one, we return an error. */ |
| 627 | if (idx > 0) |
| 628 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 629 | |
| 630 | /* If first argument have a certain type, some default values |
| 631 | * may be used. See the function smp_resolve_args(). |
| 632 | */ |
| 633 | switch (mask & ARGT_MASK) { |
| 634 | |
| 635 | case ARGT_FE: |
| 636 | if (!(p->cap & PR_CAP_FE)) |
| 637 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 638 | argp[idx].data.prx = p; |
| 639 | argp[idx].type = ARGT_FE; |
| 640 | argp[idx+1].type = ARGT_STOP; |
| 641 | break; |
| 642 | |
| 643 | case ARGT_BE: |
| 644 | if (!(p->cap & PR_CAP_BE)) |
| 645 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 646 | argp[idx].data.prx = p; |
| 647 | argp[idx].type = ARGT_BE; |
| 648 | argp[idx+1].type = ARGT_STOP; |
| 649 | break; |
| 650 | |
| 651 | case ARGT_TAB: |
| 652 | argp[idx].data.prx = p; |
| 653 | argp[idx].type = ARGT_TAB; |
| 654 | argp[idx+1].type = ARGT_STOP; |
| 655 | break; |
| 656 | |
| 657 | default: |
| 658 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 659 | break; |
| 660 | } |
| 661 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | /* Check for exceed the number of requiered argument. */ |
| 666 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 667 | argp[idx].type != ARGT_STOP) { |
| 668 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 669 | } |
| 670 | |
| 671 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 672 | argp[idx].type == ARGT_STOP) { |
| 673 | return 0; |
| 674 | } |
| 675 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 676 | /* Convert some argument types. */ |
| 677 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 678 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 679 | if (argp[idx].type != ARGT_SINT) |
| 680 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 681 | argp[idx].type = ARGT_SINT; |
| 682 | break; |
| 683 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 684 | case ARGT_TIME: |
| 685 | if (argp[idx].type != ARGT_SINT) |
| 686 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 687 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 688 | break; |
| 689 | |
| 690 | case ARGT_SIZE: |
| 691 | if (argp[idx].type != ARGT_SINT) |
| 692 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 693 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 694 | break; |
| 695 | |
| 696 | case ARGT_FE: |
| 697 | if (argp[idx].type != ARGT_STR) |
| 698 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 699 | memcpy(trash.area, argp[idx].data.str.area, |
| 700 | argp[idx].data.str.data); |
| 701 | trash.area[argp[idx].data.str.data] = 0; |
| 702 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 703 | if (!argp[idx].data.prx) |
| 704 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 705 | argp[idx].type = ARGT_FE; |
| 706 | break; |
| 707 | |
| 708 | case ARGT_BE: |
| 709 | if (argp[idx].type != ARGT_STR) |
| 710 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 711 | memcpy(trash.area, argp[idx].data.str.area, |
| 712 | argp[idx].data.str.data); |
| 713 | trash.area[argp[idx].data.str.data] = 0; |
| 714 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 715 | if (!argp[idx].data.prx) |
| 716 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 717 | argp[idx].type = ARGT_BE; |
| 718 | break; |
| 719 | |
| 720 | case ARGT_TAB: |
| 721 | if (argp[idx].type != ARGT_STR) |
| 722 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 723 | memcpy(trash.area, argp[idx].data.str.area, |
| 724 | argp[idx].data.str.data); |
| 725 | trash.area[argp[idx].data.str.data] = 0; |
Tim Duesterhus | e351e76 | 2019-09-29 23:03:09 +0200 | [diff] [blame] | 726 | argp[idx].data.t = stktable_find_by_name(trash.area); |
| 727 | if (!argp[idx].data.t) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 728 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 729 | argp[idx].type = ARGT_TAB; |
| 730 | break; |
| 731 | |
| 732 | case ARGT_SRV: |
| 733 | if (argp[idx].type != ARGT_STR) |
| 734 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 735 | memcpy(trash.area, argp[idx].data.str.area, |
| 736 | argp[idx].data.str.data); |
| 737 | trash.area[argp[idx].data.str.data] = 0; |
| 738 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 739 | if (sname) { |
| 740 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 741 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 742 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 743 | if (!px) |
| 744 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 745 | } |
| 746 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 747 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 748 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 749 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 750 | argp[idx].data.srv = findserver(px, sname); |
| 751 | if (!argp[idx].data.srv) |
| 752 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 753 | argp[idx].type = ARGT_SRV; |
| 754 | break; |
| 755 | |
| 756 | case ARGT_IPV4: |
Christopher Faulet | e7f1fcf | 2020-08-07 09:07:26 +0200 | [diff] [blame^] | 757 | if (argp[idx].type != ARGT_STR) |
| 758 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 759 | memcpy(trash.area, argp[idx].data.str.area, |
| 760 | argp[idx].data.str.data); |
| 761 | trash.area[argp[idx].data.str.data] = 0; |
| 762 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 763 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 764 | argp[idx].type = ARGT_IPV4; |
| 765 | break; |
| 766 | |
| 767 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 768 | memcpy(trash.area, argp[idx].data.str.area, |
| 769 | argp[idx].data.str.data); |
| 770 | trash.area[argp[idx].data.str.data] = 0; |
| 771 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 772 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 773 | argp[idx].type = ARGT_MSK4; |
| 774 | break; |
| 775 | |
| 776 | case ARGT_IPV6: |
Christopher Faulet | e7f1fcf | 2020-08-07 09:07:26 +0200 | [diff] [blame^] | 777 | if (argp[idx].type != ARGT_STR) |
| 778 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 779 | memcpy(trash.area, argp[idx].data.str.area, |
| 780 | argp[idx].data.str.data); |
| 781 | trash.area[argp[idx].data.str.data] = 0; |
| 782 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 783 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 784 | argp[idx].type = ARGT_IPV6; |
| 785 | break; |
| 786 | |
| 787 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 788 | memcpy(trash.area, argp[idx].data.str.area, |
| 789 | argp[idx].data.str.data); |
| 790 | trash.area[argp[idx].data.str.data] = 0; |
| 791 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 792 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 793 | argp[idx].type = ARGT_MSK6; |
| 794 | break; |
| 795 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 796 | case ARGT_MAP: |
| 797 | case ARGT_REG: |
| 798 | case ARGT_USR: |
| 799 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 800 | break; |
| 801 | } |
| 802 | |
| 803 | /* Check for type of argument. */ |
| 804 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 805 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 806 | arg_type_names[(mask & ARGT_MASK)], |
| 807 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 808 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 809 | } |
| 810 | |
| 811 | /* Next argument. */ |
| 812 | mask >>= ARGT_BITS; |
| 813 | idx++; |
| 814 | } |
| 815 | } |
| 816 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 817 | /* |
| 818 | * The following functions are used to make correspondance between the the |
| 819 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 820 | * |
| 821 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 822 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 823 | */ |
| 824 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 825 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 826 | struct hlua **hlua = lua_getextraspace(L); |
| 827 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 828 | } |
| 829 | static inline void hlua_sethlua(struct hlua *hlua) |
| 830 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 831 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 832 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 833 | } |
| 834 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 835 | /* This function is used to send logs. It try to send on screen (stderr) |
| 836 | * and on the default syslog server. |
| 837 | */ |
| 838 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 839 | { |
| 840 | struct tm tm; |
| 841 | char *p; |
| 842 | |
| 843 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 844 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 845 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 846 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 847 | /* Break the message if exceed the buffer size. */ |
| 848 | *(p-4) = ' '; |
| 849 | *(p-3) = '.'; |
| 850 | *(p-2) = '.'; |
| 851 | *(p-1) = '.'; |
| 852 | break; |
| 853 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 854 | if (isprint(*msg)) |
| 855 | *p = *msg; |
| 856 | else |
| 857 | *p = '.'; |
| 858 | } |
| 859 | *p = '\0'; |
| 860 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 861 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 862 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 863 | get_localtime(date.tv_sec, &tm); |
| 864 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 865 | 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] | 866 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 867 | fflush(stderr); |
| 868 | } |
| 869 | } |
| 870 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 871 | /* This function just ensure that the yield will be always |
| 872 | * returned with a timeout and permit to set some flags |
| 873 | */ |
| 874 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 875 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 876 | { |
| 877 | struct hlua *hlua = hlua_gethlua(L); |
| 878 | |
| 879 | /* Set the wake timeout. If timeout is required, we set |
| 880 | * the expiration time. |
| 881 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 882 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 883 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 884 | hlua->flags |= flags; |
| 885 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 886 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 887 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 888 | } |
| 889 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 890 | /* This function initialises the Lua environment stored in the stream. |
| 891 | * 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] | 892 | * 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] | 893 | * |
| 894 | * This function is particular. it initialises a new Lua thread. If the |
| 895 | * initialisation fails (example: out of memory error), the lua function |
| 896 | * throws an error (longjmp). |
| 897 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 898 | * In some case (at least one), this function can be called from safe |
| 899 | * environement, so we must not initialise it. While the support of |
| 900 | * threads appear, the safe environment set a lock to ensure only one |
| 901 | * Lua execution at a time. If we initialize safe environment in another |
| 902 | * safe environmenet, we have a dead lock. |
| 903 | * |
| 904 | * set "already_safe" true if the context is initialized form safe |
| 905 | * Lua fonction. |
| 906 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 907 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 908 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 909 | * MUST NOT manipulate the created thread stack state, because it is not |
| 910 | * proctected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 911 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 912 | 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] | 913 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 914 | if (!already_safe) { |
| 915 | if (!SET_SAFE_LJMP(gL.T)) { |
| 916 | lua->Tref = LUA_REFNIL; |
| 917 | return 0; |
| 918 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 919 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 920 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 921 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 922 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 923 | lua->T = lua_newthread(gL.T); |
| 924 | if (!lua->T) { |
| 925 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 926 | if (!already_safe) |
| 927 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 928 | return 0; |
| 929 | } |
| 930 | hlua_sethlua(lua); |
| 931 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 932 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 933 | if (!already_safe) |
| 934 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 935 | return 1; |
| 936 | } |
| 937 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 938 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 939 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 940 | * is not freed. |
| 941 | */ |
| 942 | void hlua_ctx_destroy(struct hlua *lua) |
| 943 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 944 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 945 | return; |
| 946 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 947 | if (!lua->T) |
| 948 | goto end; |
| 949 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 950 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 951 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 952 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 953 | if (!SET_SAFE_LJMP(lua->T)) |
| 954 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 955 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 956 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 957 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 958 | if (!SET_SAFE_LJMP(gL.T)) |
| 959 | return; |
| 960 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 961 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 962 | /* Forces a garbage collecting process. If the Lua program is finished |
| 963 | * without error, we run the GC on the thread pointer. Its freed all |
| 964 | * the unused memory. |
| 965 | * If the thread is finnish with an error or is currently yielded, |
| 966 | * it seems that the GC applied on the thread doesn't clean anything, |
| 967 | * so e run the GC on the main thread. |
| 968 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 969 | * the garbage collection. |
| 970 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 971 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 972 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 973 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 974 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 975 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 976 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 977 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 978 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 979 | |
| 980 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 981 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | /* This function is used to restore the Lua context when a coroutine |
| 985 | * fails. This function copy the common memory between old coroutine |
| 986 | * and the new coroutine. The old coroutine is destroyed, and its |
| 987 | * replaced by the new coroutine. |
| 988 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 989 | * as string error message and it is copied in the new stack. |
| 990 | */ |
| 991 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 992 | { |
| 993 | lua_State *T; |
| 994 | int new_ref; |
| 995 | |
| 996 | /* Renew the main LUA stack doesn't have sense. */ |
| 997 | if (lua == &gL) |
| 998 | return 0; |
| 999 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1000 | /* New Lua coroutine. */ |
| 1001 | T = lua_newthread(gL.T); |
| 1002 | if (!T) |
| 1003 | return 0; |
| 1004 | |
| 1005 | /* Copy last error message. */ |
| 1006 | if (keep_msg) |
| 1007 | lua_xmove(lua->T, T, 1); |
| 1008 | |
| 1009 | /* Copy data between the coroutines. */ |
| 1010 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1011 | lua_xmove(lua->T, T, 1); |
| 1012 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 1013 | |
| 1014 | /* Destroy old data. */ |
| 1015 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1016 | |
| 1017 | /* The thread is garbage collected by Lua. */ |
| 1018 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1019 | |
| 1020 | /* Fill the struct with the new coroutine values. */ |
| 1021 | lua->Mref = new_ref; |
| 1022 | lua->T = T; |
| 1023 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1024 | |
| 1025 | /* Set context. */ |
| 1026 | hlua_sethlua(lua); |
| 1027 | |
| 1028 | return 1; |
| 1029 | } |
| 1030 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1031 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1032 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1033 | struct hlua *hlua = hlua_gethlua(L); |
| 1034 | |
| 1035 | /* Lua cannot yield when its returning from a function, |
| 1036 | * so, we can fix the interrupt hook to 1 instruction, |
| 1037 | * expecting that the function is finnished. |
| 1038 | */ |
| 1039 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1040 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1041 | return; |
| 1042 | } |
| 1043 | |
| 1044 | /* restore the interrupt condition. */ |
| 1045 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1046 | |
| 1047 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1048 | * If the state is not yieldable, trying yield causes an error. |
| 1049 | */ |
| 1050 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1051 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1052 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1053 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1054 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1055 | hlua->run_time += now_ms - hlua->start_time; |
| 1056 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1057 | lua_pushfstring(L, "execution timeout"); |
| 1058 | WILL_LJMP(lua_error(L)); |
| 1059 | } |
| 1060 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1061 | /* Update the start time. */ |
| 1062 | hlua->start_time = now_ms; |
| 1063 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1064 | /* Try to interrupt the process at the end of the current |
| 1065 | * unyieldable function. |
| 1066 | */ |
| 1067 | 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] | 1068 | } |
| 1069 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1070 | /* This function start or resumes the Lua stack execution. If the flag |
| 1071 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1072 | * The function return an error. |
| 1073 | * |
| 1074 | * The function can returns 4 values: |
| 1075 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1076 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1077 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1078 | * - 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] | 1079 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1080 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1081 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1082 | * 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] | 1083 | * LUA code. |
| 1084 | */ |
| 1085 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1086 | { |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1087 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1088 | int nres; |
| 1089 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1090 | int ret; |
| 1091 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1092 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1093 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1094 | /* Initialise run time counter. */ |
| 1095 | if (!HLUA_IS_RUNNING(lua)) |
| 1096 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1097 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1098 | /* Lock the whole Lua execution. This lock must be before the |
| 1099 | * label "resume_execution". |
| 1100 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1101 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1102 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1103 | resume_execution: |
| 1104 | |
| 1105 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1106 | * instructions. it is used for preventing infinite loops. |
| 1107 | */ |
| 1108 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1109 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1110 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1111 | HLUA_SET_RUN(lua); |
| 1112 | HLUA_CLR_CTRLYIELD(lua); |
| 1113 | HLUA_CLR_WAKERESWR(lua); |
| 1114 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1115 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1116 | /* Update the start time. */ |
| 1117 | lua->start_time = now_ms; |
| 1118 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1119 | /* Call the function. */ |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1120 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1121 | ret = lua_resume(lua->T, gL.T, lua->nargs, &nres); |
| 1122 | #else |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1123 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1124 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1125 | switch (ret) { |
| 1126 | |
| 1127 | case LUA_OK: |
| 1128 | ret = HLUA_E_OK; |
| 1129 | break; |
| 1130 | |
| 1131 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1132 | /* Check if the execution timeout is expired. It it is the case, we |
| 1133 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1134 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1135 | tv_update_date(0, 1); |
| 1136 | lua->run_time += now_ms - lua->start_time; |
| 1137 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1138 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1139 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1140 | break; |
| 1141 | } |
| 1142 | /* Process the forced yield. if the general yield is not allowed or |
| 1143 | * if no task were associated this the current Lua execution |
| 1144 | * coroutine, we resume the execution. Else we want to return in the |
| 1145 | * scheduler and we want to be waked up again, to continue the |
| 1146 | * current Lua execution. So we schedule our own task. |
| 1147 | */ |
| 1148 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1149 | if (!yield_allowed || !lua->task) |
| 1150 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1151 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1152 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1153 | if (!yield_allowed) { |
| 1154 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1155 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1156 | break; |
| 1157 | } |
| 1158 | ret = HLUA_E_AGAIN; |
| 1159 | break; |
| 1160 | |
| 1161 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1162 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1163 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1164 | * because the errors ares the only one mean to return immediately |
| 1165 | * from and lua execution. |
| 1166 | */ |
| 1167 | if (lua->flags & HLUA_EXIT) { |
| 1168 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1169 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1170 | break; |
| 1171 | } |
| 1172 | |
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 | if (!lua_checkstack(lua->T, 1)) { |
| 1175 | ret = HLUA_E_ERR; |
| 1176 | break; |
| 1177 | } |
| 1178 | msg = lua_tostring(lua->T, -1); |
| 1179 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1180 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1181 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1182 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1183 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1184 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1185 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1186 | ret = HLUA_E_ERRMSG; |
| 1187 | break; |
| 1188 | |
| 1189 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1190 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1191 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1192 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1193 | break; |
| 1194 | |
| 1195 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1196 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1197 | if (!lua_checkstack(lua->T, 1)) { |
| 1198 | ret = HLUA_E_ERR; |
| 1199 | break; |
| 1200 | } |
| 1201 | msg = lua_tostring(lua->T, -1); |
| 1202 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1203 | lua_pop(lua->T, 1); |
| 1204 | if (msg) |
| 1205 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1206 | else |
| 1207 | lua_pushfstring(lua->T, "message handler error"); |
| 1208 | ret = HLUA_E_ERRMSG; |
| 1209 | break; |
| 1210 | |
| 1211 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1212 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1213 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1214 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1215 | break; |
| 1216 | } |
| 1217 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1218 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1219 | if (lua->flags & HLUA_MUST_GC && |
| 1220 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1221 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1222 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1223 | switch (ret) { |
| 1224 | case HLUA_E_AGAIN: |
| 1225 | break; |
| 1226 | |
| 1227 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1228 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1229 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1230 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1231 | break; |
| 1232 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1233 | case HLUA_E_ETMOUT: |
| 1234 | case HLUA_E_NOMEM: |
| 1235 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1236 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1237 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1238 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1239 | hlua_ctx_renew(lua, 0); |
| 1240 | break; |
| 1241 | |
| 1242 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1243 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1244 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1245 | break; |
| 1246 | } |
| 1247 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1248 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1249 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1250 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1251 | return ret; |
| 1252 | } |
| 1253 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1254 | /* This function exit the current code. */ |
| 1255 | __LJMP static int hlua_done(lua_State *L) |
| 1256 | { |
| 1257 | struct hlua *hlua = hlua_gethlua(L); |
| 1258 | |
| 1259 | hlua->flags |= HLUA_EXIT; |
| 1260 | WILL_LJMP(lua_error(L)); |
| 1261 | |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1265 | /* This function is an LUA binding. It provides a function |
| 1266 | * for deleting ACL from a referenced ACL file. |
| 1267 | */ |
| 1268 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1269 | { |
| 1270 | const char *name; |
| 1271 | const char *key; |
| 1272 | struct pat_ref *ref; |
| 1273 | |
| 1274 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1275 | |
| 1276 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1277 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1278 | |
| 1279 | ref = pat_ref_lookup(name); |
| 1280 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1281 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1282 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1283 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1284 | pat_ref_delete(ref, key); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1285 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1286 | return 0; |
| 1287 | } |
| 1288 | |
| 1289 | /* This function is an LUA binding. It provides a function |
| 1290 | * for deleting map entry from a referenced map file. |
| 1291 | */ |
| 1292 | static int hlua_del_map(lua_State *L) |
| 1293 | { |
| 1294 | const char *name; |
| 1295 | const char *key; |
| 1296 | struct pat_ref *ref; |
| 1297 | |
| 1298 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1299 | |
| 1300 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1301 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1302 | |
| 1303 | ref = pat_ref_lookup(name); |
| 1304 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1305 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1306 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1307 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1308 | pat_ref_delete(ref, key); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1309 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1310 | return 0; |
| 1311 | } |
| 1312 | |
| 1313 | /* This function is an LUA binding. It provides a function |
| 1314 | * for adding ACL pattern from a referenced ACL file. |
| 1315 | */ |
| 1316 | static int hlua_add_acl(lua_State *L) |
| 1317 | { |
| 1318 | const char *name; |
| 1319 | const char *key; |
| 1320 | struct pat_ref *ref; |
| 1321 | |
| 1322 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1323 | |
| 1324 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1325 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1326 | |
| 1327 | ref = pat_ref_lookup(name); |
| 1328 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1329 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1330 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1331 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1332 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1333 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1334 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1335 | return 0; |
| 1336 | } |
| 1337 | |
| 1338 | /* This function is an LUA binding. It provides a function |
| 1339 | * for setting map pattern and sample from a referenced map |
| 1340 | * file. |
| 1341 | */ |
| 1342 | static int hlua_set_map(lua_State *L) |
| 1343 | { |
| 1344 | const char *name; |
| 1345 | const char *key; |
| 1346 | const char *value; |
| 1347 | struct pat_ref *ref; |
| 1348 | |
| 1349 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1350 | |
| 1351 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1352 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1353 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1354 | |
| 1355 | ref = pat_ref_lookup(name); |
| 1356 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1357 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1358 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1359 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1360 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1361 | pat_ref_set(ref, key, value, NULL); |
| 1362 | else |
| 1363 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1364 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1365 | return 0; |
| 1366 | } |
| 1367 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1368 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1369 | * an integer or user data. This data is associated with a metatable. This |
| 1370 | * metatable have an original version registred in the global context with |
| 1371 | * the name of the object (_G[<name>] = <metable> ). |
| 1372 | * |
| 1373 | * A metable is a table that modify the standard behavior of a standard |
| 1374 | * access to the associated data. The entries of this new metatable are |
| 1375 | * defined as is: |
| 1376 | * |
| 1377 | * http://lua-users.org/wiki/MetatableEvents |
| 1378 | * |
| 1379 | * __index |
| 1380 | * |
| 1381 | * we access an absent field in a table, the result is nil. This is |
| 1382 | * true, but it is not the whole truth. Actually, such access triggers |
| 1383 | * the interpreter to look for an __index metamethod: If there is no |
| 1384 | * such method, as usually happens, then the access results in nil; |
| 1385 | * otherwise, the metamethod will provide the result. |
| 1386 | * |
| 1387 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1388 | * the key does not appear in the table, but the metatable has an __index |
| 1389 | * property: |
| 1390 | * |
| 1391 | * - if the value is a function, the function is called, passing in the |
| 1392 | * table and the key; the return value of that function is returned as |
| 1393 | * the result. |
| 1394 | * |
| 1395 | * - if the value is another table, the value of the key in that table is |
| 1396 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1397 | * table's metatable has an __index property, then it continues on up) |
| 1398 | * |
| 1399 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1400 | * |
| 1401 | * http://www.lua.org/pil/13.4.1.html |
| 1402 | * |
| 1403 | * __newindex |
| 1404 | * |
| 1405 | * Like __index, but control property assignment. |
| 1406 | * |
| 1407 | * __mode - Control weak references. A string value with one or both |
| 1408 | * of the characters 'k' and 'v' which specifies that the the |
| 1409 | * keys and/or values in the table are weak references. |
| 1410 | * |
| 1411 | * __call - Treat a table like a function. When a table is followed by |
| 1412 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1413 | * a __call key pointing to a function, that function is invoked |
| 1414 | * (passing any specified arguments) and the return value is |
| 1415 | * returned. |
| 1416 | * |
| 1417 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1418 | * called, if the metatable for myTable has a __metatable |
| 1419 | * key, the value of that key is returned instead of the |
| 1420 | * actual metatable. |
| 1421 | * |
| 1422 | * __tostring - Control string representation. When the builtin |
| 1423 | * "tostring( myTable )" function is called, if the metatable |
| 1424 | * for myTable has a __tostring property set to a function, |
| 1425 | * that function is invoked (passing myTable to it) and the |
| 1426 | * return value is used as the string representation. |
| 1427 | * |
| 1428 | * __len - Control table length. When the table length is requested using |
| 1429 | * the length operator ( '#' ), if the metatable for myTable has |
| 1430 | * a __len key pointing to a function, that function is invoked |
| 1431 | * (passing myTable to it) and the return value used as the value |
| 1432 | * of "#myTable". |
| 1433 | * |
| 1434 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1435 | * collected, if the metatable has a __gc field pointing to a |
| 1436 | * function, that function is first invoked, passing the userdata |
| 1437 | * to it. The __gc metamethod is not called for tables. |
| 1438 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1439 | * |
| 1440 | * Special metamethods for redefining standard operators: |
| 1441 | * http://www.lua.org/pil/13.1.html |
| 1442 | * |
| 1443 | * __add "+" |
| 1444 | * __sub "-" |
| 1445 | * __mul "*" |
| 1446 | * __div "/" |
| 1447 | * __unm "!" |
| 1448 | * __pow "^" |
| 1449 | * __concat ".." |
| 1450 | * |
| 1451 | * Special methods for redfining standar relations |
| 1452 | * http://www.lua.org/pil/13.2.html |
| 1453 | * |
| 1454 | * __eq "==" |
| 1455 | * __lt "<" |
| 1456 | * __le "<=" |
| 1457 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1458 | |
| 1459 | /* |
| 1460 | * |
| 1461 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1462 | * Class Map |
| 1463 | * |
| 1464 | * |
| 1465 | */ |
| 1466 | |
| 1467 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1468 | * a class session, otherwise it throws an error. |
| 1469 | */ |
| 1470 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1471 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1472 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1473 | } |
| 1474 | |
| 1475 | /* This function is the map constructor. It don't need |
| 1476 | * the class Map object. It creates and return a new Map |
| 1477 | * object. It must be called only during "body" or "init" |
| 1478 | * context because it process some filesystem accesses. |
| 1479 | */ |
| 1480 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1481 | { |
| 1482 | const char *fn; |
| 1483 | int match = PAT_MATCH_STR; |
| 1484 | struct sample_conv conv; |
| 1485 | const char *file = ""; |
| 1486 | int line = 0; |
| 1487 | lua_Debug ar; |
| 1488 | char *err = NULL; |
| 1489 | struct arg args[2]; |
| 1490 | |
| 1491 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1492 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1493 | |
| 1494 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1495 | |
| 1496 | if (lua_gettop(L) >= 2) { |
| 1497 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1498 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1499 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1500 | } |
| 1501 | |
| 1502 | /* Get Lua filename and line number. */ |
| 1503 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1504 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1505 | if (ar.currentline > 0) { /* is there info? */ |
| 1506 | file = ar.short_src; |
| 1507 | line = ar.currentline; |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | /* fill fake sample_conv struct. */ |
| 1512 | conv.kw = ""; /* unused. */ |
| 1513 | conv.process = NULL; /* unused. */ |
| 1514 | conv.arg_mask = 0; /* unused. */ |
| 1515 | conv.val_args = NULL; /* unused. */ |
| 1516 | conv.out_type = SMP_T_STR; |
| 1517 | conv.private = (void *)(long)match; |
| 1518 | switch (match) { |
| 1519 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1520 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1521 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1522 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1523 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1524 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1525 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1526 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1527 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1528 | default: |
| 1529 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1530 | } |
| 1531 | |
| 1532 | /* fill fake args. */ |
| 1533 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1534 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1535 | args[1].type = ARGT_STOP; |
| 1536 | |
| 1537 | /* load the map. */ |
| 1538 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1539 | /* error case: we cant use luaL_error because we must |
| 1540 | * free the err variable. |
| 1541 | */ |
| 1542 | luaL_where(L, 1); |
| 1543 | lua_pushfstring(L, "'new': %s.", err); |
| 1544 | lua_concat(L, 2); |
| 1545 | free(err); |
| 1546 | WILL_LJMP(lua_error(L)); |
| 1547 | } |
| 1548 | |
| 1549 | /* create the lua object. */ |
| 1550 | lua_newtable(L); |
| 1551 | lua_pushlightuserdata(L, args[0].data.map); |
| 1552 | lua_rawseti(L, -2, 0); |
| 1553 | |
| 1554 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1555 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1556 | lua_setmetatable(L, -2); |
| 1557 | |
| 1558 | |
| 1559 | return 1; |
| 1560 | } |
| 1561 | |
| 1562 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1563 | { |
| 1564 | struct map_descriptor *desc; |
| 1565 | struct pattern *pat; |
| 1566 | struct sample smp; |
| 1567 | |
| 1568 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1569 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1570 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1571 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1572 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1573 | } |
| 1574 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1575 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1576 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1577 | 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] | 1578 | } |
| 1579 | |
| 1580 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1581 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1582 | if (str) |
| 1583 | lua_pushstring(L, ""); |
| 1584 | else |
| 1585 | lua_pushnil(L); |
| 1586 | return 1; |
| 1587 | } |
| 1588 | |
| 1589 | /* 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] | 1590 | 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] | 1591 | return 1; |
| 1592 | } |
| 1593 | |
| 1594 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1595 | { |
| 1596 | return _hlua_map_lookup(L, 0); |
| 1597 | } |
| 1598 | |
| 1599 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1600 | { |
| 1601 | return _hlua_map_lookup(L, 1); |
| 1602 | } |
| 1603 | |
| 1604 | /* |
| 1605 | * |
| 1606 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1607 | * Class Socket |
| 1608 | * |
| 1609 | * |
| 1610 | */ |
| 1611 | |
| 1612 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1613 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1614 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | /* 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] | 1618 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1619 | * received. |
| 1620 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1621 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1622 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1623 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1624 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1625 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1626 | if (appctx->ctx.hlua_cosocket.die) { |
| 1627 | si_shutw(si); |
| 1628 | si_shutr(si); |
| 1629 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1630 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1631 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1632 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1633 | } |
| 1634 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1635 | /* If the connection object is not available, close all the |
| 1636 | * streams and wakeup everything waiting for. |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1637 | */ |
| 1638 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1639 | si_shutw(si); |
| 1640 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1641 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1642 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1643 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1644 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1645 | } |
| 1646 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1647 | /* If we cant write, wakeup the pending write signals. */ |
| 1648 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1649 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1650 | |
| 1651 | /* If we cant read, wakeup the pending read signals. */ |
| 1652 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1653 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1654 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1655 | /* if the connection is not estabkished, inform the stream that we want |
| 1656 | * to be notified whenever the connection completes. |
| 1657 | */ |
| 1658 | if (!(c->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1659 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1660 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1661 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1662 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1663 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1664 | |
| 1665 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1666 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1667 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1668 | /* 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] | 1669 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1670 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1671 | |
| 1672 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1673 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1674 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1675 | |
| 1676 | /* Some data were injected in the buffer, notify the stream |
| 1677 | * interface. |
| 1678 | */ |
| 1679 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1680 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1681 | |
| 1682 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1683 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1684 | */ |
| 1685 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1686 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1687 | } |
| 1688 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1689 | /* This function is called when the "struct stream" is destroyed. |
| 1690 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1691 | * Wake all the pending signals. |
| 1692 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1693 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1694 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1695 | struct xref *peer; |
| 1696 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1697 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1698 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1699 | if (peer) |
| 1700 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1701 | |
| 1702 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1703 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1704 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1705 | } |
| 1706 | |
| 1707 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1708 | * uses this object. If the stream does not exists, just quit. |
| 1709 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1710 | * pending signal can rest in the read and write lists. destroy |
| 1711 | * it. |
| 1712 | */ |
| 1713 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1714 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1715 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1716 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1717 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1718 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1719 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1720 | |
| 1721 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1722 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1723 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1724 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1725 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1726 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1727 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1728 | appctx->ctx.hlua_cosocket.die = 1; |
| 1729 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1730 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1731 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1732 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1733 | return 0; |
| 1734 | } |
| 1735 | |
| 1736 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1737 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1738 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1739 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1740 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1741 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1742 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1743 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1744 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1745 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1746 | |
| 1747 | /* Check if we run on the same thread than the xreator thread. |
| 1748 | * We cannot access to the socket if the thread is different. |
| 1749 | */ |
| 1750 | if (socket->tid != tid) |
| 1751 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1752 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1753 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1754 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1755 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1756 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1757 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1758 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1759 | appctx->ctx.hlua_cosocket.die = 1; |
| 1760 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1761 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1762 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1763 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1764 | return 0; |
| 1765 | } |
| 1766 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1767 | /* The close function calls close_helper. |
| 1768 | */ |
| 1769 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1770 | { |
| 1771 | MAY_LJMP(check_args(L, 1, "close")); |
| 1772 | return hlua_socket_close_helper(L); |
| 1773 | } |
| 1774 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1775 | /* This Lua function assumes that the stack contain three parameters. |
| 1776 | * 1 - USERDATA containing a struct socket |
| 1777 | * 2 - INTEGER with values of the macro defined below |
| 1778 | * If the integer is -1, we must read at most one line. |
| 1779 | * If the integer is -2, we ust read all the data until the |
| 1780 | * end of the stream. |
| 1781 | * If the integer is positive value, we must read a number of |
| 1782 | * bytes corresponding to this value. |
| 1783 | */ |
| 1784 | #define HLSR_READ_LINE (-1) |
| 1785 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1786 | __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] | 1787 | { |
| 1788 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1789 | int wanted = lua_tointeger(L, 2); |
| 1790 | struct hlua *hlua = hlua_gethlua(L); |
| 1791 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1792 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1793 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1794 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1795 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1796 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1797 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1798 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1799 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1800 | struct stream_interface *si; |
| 1801 | struct stream *s; |
| 1802 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1803 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1804 | |
| 1805 | /* Check if this lua stack is schedulable. */ |
| 1806 | if (!hlua || !hlua->task) |
| 1807 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1808 | "'frontend', 'backend' or 'task'")); |
| 1809 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1810 | /* Check if we run on the same thread than the xreator thread. |
| 1811 | * We cannot access to the socket if the thread is different. |
| 1812 | */ |
| 1813 | if (socket->tid != tid) |
| 1814 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1815 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1816 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1817 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1818 | if (!peer) |
| 1819 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1820 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1821 | si = appctx->owner; |
| 1822 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1823 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1824 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1825 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1826 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1827 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1828 | if (nblk < 0) /* Connection close. */ |
| 1829 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1830 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1831 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1832 | |
| 1833 | /* remove final \r\n. */ |
| 1834 | if (nblk == 1) { |
| 1835 | if (blk1[len1-1] == '\n') { |
| 1836 | len1--; |
| 1837 | skip_at_end++; |
| 1838 | if (blk1[len1-1] == '\r') { |
| 1839 | len1--; |
| 1840 | skip_at_end++; |
| 1841 | } |
| 1842 | } |
| 1843 | } |
| 1844 | else { |
| 1845 | if (blk2[len2-1] == '\n') { |
| 1846 | len2--; |
| 1847 | skip_at_end++; |
| 1848 | if (blk2[len2-1] == '\r') { |
| 1849 | len2--; |
| 1850 | skip_at_end++; |
| 1851 | } |
| 1852 | } |
| 1853 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1857 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1858 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1859 | if (nblk < 0) /* Connection close. */ |
| 1860 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1861 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1862 | goto connection_empty; |
| 1863 | } |
| 1864 | |
| 1865 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1866 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1867 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1868 | if (nblk < 0) /* Connection close. */ |
| 1869 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1870 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1871 | goto connection_empty; |
| 1872 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1873 | missing_bytes = wanted - socket->b.n; |
| 1874 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1875 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1876 | len1 = missing_bytes; |
| 1877 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1878 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1879 | } |
| 1880 | |
| 1881 | len = len1; |
| 1882 | |
| 1883 | luaL_addlstring(&socket->b, blk1, len1); |
| 1884 | if (nblk == 2) { |
| 1885 | len += len2; |
| 1886 | luaL_addlstring(&socket->b, blk2, len2); |
| 1887 | } |
| 1888 | |
| 1889 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1890 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1891 | |
| 1892 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1893 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1894 | |
| 1895 | /* If the pattern reclaim to read all the data |
| 1896 | * in the connection, got out. |
| 1897 | */ |
| 1898 | if (wanted == HLSR_READ_ALL) |
| 1899 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1900 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1901 | goto connection_empty; |
| 1902 | |
| 1903 | /* Return result. */ |
| 1904 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1905 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1906 | return 1; |
| 1907 | |
| 1908 | connection_closed: |
| 1909 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1910 | xref_unlock(&socket->xref, peer); |
| 1911 | |
| 1912 | no_peer: |
| 1913 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1914 | /* If the buffer containds data. */ |
| 1915 | if (socket->b.n > 0) { |
| 1916 | luaL_pushresult(&socket->b); |
| 1917 | return 1; |
| 1918 | } |
| 1919 | lua_pushnil(L); |
| 1920 | lua_pushstring(L, "connection closed."); |
| 1921 | return 2; |
| 1922 | |
| 1923 | connection_empty: |
| 1924 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1925 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1926 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1927 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1928 | } |
| 1929 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1930 | 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] | 1931 | return 0; |
| 1932 | } |
| 1933 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1934 | /* This Lua function gets two parameters. The first one can be string |
| 1935 | * or a number. If the string is "*l", the user requires one line. If |
| 1936 | * 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] | 1937 | * If the value is a number, the user require a number of bytes equal |
| 1938 | * to the value. The default value is "*l" (a line). |
| 1939 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1940 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1941 | * integer takes this values: |
| 1942 | * -1 : read a line |
| 1943 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1944 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1945 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1946 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1947 | * concatenated with the read data. |
| 1948 | */ |
| 1949 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1950 | { |
| 1951 | int wanted = HLSR_READ_LINE; |
| 1952 | const char *pattern; |
| 1953 | int type; |
| 1954 | char *error; |
| 1955 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1956 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1957 | |
| 1958 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1959 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1960 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1961 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1962 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1963 | /* Check if we run on the same thread than the xreator thread. |
| 1964 | * We cannot access to the socket if the thread is different. |
| 1965 | */ |
| 1966 | if (socket->tid != tid) |
| 1967 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1968 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1969 | /* check for pattern. */ |
| 1970 | if (lua_gettop(L) >= 2) { |
| 1971 | type = lua_type(L, 2); |
| 1972 | if (type == LUA_TSTRING) { |
| 1973 | pattern = lua_tostring(L, 2); |
| 1974 | if (strcmp(pattern, "*a") == 0) |
| 1975 | wanted = HLSR_READ_ALL; |
| 1976 | else if (strcmp(pattern, "*l") == 0) |
| 1977 | wanted = HLSR_READ_LINE; |
| 1978 | else { |
| 1979 | wanted = strtoll(pattern, &error, 10); |
| 1980 | if (*error != '\0') |
| 1981 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1982 | } |
| 1983 | } |
| 1984 | else if (type == LUA_TNUMBER) { |
| 1985 | wanted = lua_tointeger(L, 2); |
| 1986 | if (wanted < 0) |
| 1987 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1988 | } |
| 1989 | } |
| 1990 | |
| 1991 | /* Set pattern. */ |
| 1992 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1993 | |
| 1994 | /* Check if we would replace the top by itself. */ |
| 1995 | if (lua_gettop(L) != 2) |
| 1996 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1997 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1998 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1999 | luaL_buffinit(L, &socket->b); |
| 2000 | |
| 2001 | /* Check prefix. */ |
| 2002 | if (lua_gettop(L) >= 3) { |
| 2003 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2004 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2005 | pattern = lua_tolstring(L, 3, &len); |
| 2006 | luaL_addlstring(&socket->b, pattern, len); |
| 2007 | } |
| 2008 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2009 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2010 | } |
| 2011 | |
| 2012 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2013 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2014 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2015 | 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] | 2016 | { |
| 2017 | struct hlua_socket *socket; |
| 2018 | struct hlua *hlua = hlua_gethlua(L); |
| 2019 | struct appctx *appctx; |
| 2020 | size_t buf_len; |
| 2021 | const char *buf; |
| 2022 | int len; |
| 2023 | int send_len; |
| 2024 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2025 | struct xref *peer; |
| 2026 | struct stream_interface *si; |
| 2027 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2028 | |
| 2029 | /* Check if this lua stack is schedulable. */ |
| 2030 | if (!hlua || !hlua->task) |
| 2031 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2032 | "'frontend', 'backend' or 'task'")); |
| 2033 | |
| 2034 | /* Get object */ |
| 2035 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2036 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2037 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2038 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2039 | /* Check if we run on the same thread than the xreator thread. |
| 2040 | * We cannot access to the socket if the thread is different. |
| 2041 | */ |
| 2042 | if (socket->tid != tid) |
| 2043 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2044 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2045 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2046 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2047 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2048 | lua_pushinteger(L, -1); |
| 2049 | return 1; |
| 2050 | } |
| 2051 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2052 | si = appctx->owner; |
| 2053 | s = si_strm(si); |
| 2054 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2055 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2056 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2057 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2058 | lua_pushinteger(L, -1); |
| 2059 | return 1; |
| 2060 | } |
| 2061 | |
| 2062 | /* Update the input buffer data. */ |
| 2063 | buf += sent; |
| 2064 | send_len = buf_len - sent; |
| 2065 | |
| 2066 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2067 | if (sent >= buf_len) { |
| 2068 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2069 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2070 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2071 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2072 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2073 | * the request buffer if its not required. |
| 2074 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2075 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2076 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2077 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2078 | } |
| 2079 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2080 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2081 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2082 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2083 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2084 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2085 | |
| 2086 | /* send data */ |
| 2087 | if (len < send_len) |
| 2088 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2089 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2090 | |
| 2091 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2092 | * the data" (-2) are not expected because the available length |
| 2093 | * is tested. |
| 2094 | * Other unknown error are also not expected. |
| 2095 | */ |
| 2096 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2097 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2098 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2099 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2100 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2101 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2102 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2103 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2104 | return 1; |
| 2105 | } |
| 2106 | |
| 2107 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2108 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2109 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2110 | s->req.rex = TICK_ETERNITY; |
| 2111 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2112 | |
| 2113 | /* Update length sent. */ |
| 2114 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2115 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2116 | |
| 2117 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2118 | if (sent + len >= buf_len) { |
| 2119 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2120 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2121 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2122 | |
| 2123 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2124 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2125 | xref_unlock(&socket->xref, peer); |
| 2126 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2127 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2128 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2129 | 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] | 2130 | return 0; |
| 2131 | } |
| 2132 | |
| 2133 | /* This function initiate the send of data. It just check the input |
| 2134 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2135 | * 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] | 2136 | * "hlua_socket_write_yield" that can yield. |
| 2137 | * |
| 2138 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2139 | * the associated object. The second is a string buffer. The third is |
| 2140 | * a facultative integer that represents where is the buffer position |
| 2141 | * of the start of the data that can send. The first byte is the |
| 2142 | * position "1". The default value is "1". The fourth argument is a |
| 2143 | * facultative integer that represents where is the buffer position |
| 2144 | * of the end of the data that can send. The default is the last byte. |
| 2145 | */ |
| 2146 | static int hlua_socket_send(struct lua_State *L) |
| 2147 | { |
| 2148 | int i; |
| 2149 | int j; |
| 2150 | const char *buf; |
| 2151 | size_t buf_len; |
| 2152 | |
| 2153 | /* Check number of arguments. */ |
| 2154 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2155 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2156 | |
| 2157 | /* Get the string. */ |
| 2158 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2159 | |
| 2160 | /* Get and check j. */ |
| 2161 | if (lua_gettop(L) == 4) { |
| 2162 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2163 | if (j < 0) |
| 2164 | j = buf_len + j + 1; |
| 2165 | if (j > buf_len) |
| 2166 | j = buf_len + 1; |
| 2167 | lua_pop(L, 1); |
| 2168 | } |
| 2169 | else |
| 2170 | j = buf_len; |
| 2171 | |
| 2172 | /* Get and check i. */ |
| 2173 | if (lua_gettop(L) == 3) { |
| 2174 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2175 | if (i < 0) |
| 2176 | i = buf_len + i + 1; |
| 2177 | if (i > buf_len) |
| 2178 | i = buf_len + 1; |
| 2179 | lua_pop(L, 1); |
| 2180 | } else |
| 2181 | i = 1; |
| 2182 | |
| 2183 | /* Check bth i and j. */ |
| 2184 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2185 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2186 | return 1; |
| 2187 | } |
| 2188 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2189 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2190 | return 1; |
| 2191 | } |
| 2192 | if (i == 0) |
| 2193 | i = 1; |
| 2194 | if (j == 0) |
| 2195 | j = 1; |
| 2196 | |
| 2197 | /* Pop the string. */ |
| 2198 | lua_pop(L, 1); |
| 2199 | |
| 2200 | /* Update the buffer length. */ |
| 2201 | buf += i - 1; |
| 2202 | buf_len = j - i + 1; |
| 2203 | lua_pushlstring(L, buf, buf_len); |
| 2204 | |
| 2205 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2206 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2207 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2208 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2209 | } |
| 2210 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2211 | #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] | 2212 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2213 | { |
| 2214 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2215 | int ret; |
| 2216 | int len; |
| 2217 | char *p; |
| 2218 | |
| 2219 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2220 | if (ret <= 0) { |
| 2221 | lua_pushnil(L); |
| 2222 | return 1; |
| 2223 | } |
| 2224 | |
| 2225 | if (ret == AF_UNIX) { |
| 2226 | lua_pushstring(L, buffer+1); |
| 2227 | return 1; |
| 2228 | } |
| 2229 | else if (ret == AF_INET6) { |
| 2230 | buffer[0] = '['; |
| 2231 | len = strlen(buffer); |
| 2232 | buffer[len] = ']'; |
| 2233 | len++; |
| 2234 | buffer[len] = ':'; |
| 2235 | len++; |
| 2236 | p = buffer; |
| 2237 | } |
| 2238 | else if (ret == AF_INET) { |
| 2239 | p = buffer + 1; |
| 2240 | len = strlen(p); |
| 2241 | p[len] = ':'; |
| 2242 | len++; |
| 2243 | } |
| 2244 | else { |
| 2245 | lua_pushnil(L); |
| 2246 | return 1; |
| 2247 | } |
| 2248 | |
| 2249 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2250 | lua_pushnil(L); |
| 2251 | return 1; |
| 2252 | } |
| 2253 | |
| 2254 | lua_pushstring(L, p); |
| 2255 | return 1; |
| 2256 | } |
| 2257 | |
| 2258 | /* Returns information about the peer of the connection. */ |
| 2259 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2260 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2261 | struct hlua_socket *socket; |
| 2262 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2263 | struct xref *peer; |
| 2264 | struct appctx *appctx; |
| 2265 | struct stream_interface *si; |
| 2266 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2267 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2268 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2269 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2270 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2271 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2272 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2273 | /* Check if we run on the same thread than the xreator thread. |
| 2274 | * We cannot access to the socket if the thread is different. |
| 2275 | */ |
| 2276 | if (socket->tid != tid) |
| 2277 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2278 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2279 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2280 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2281 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2282 | lua_pushnil(L); |
| 2283 | return 1; |
| 2284 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2285 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2286 | si = appctx->owner; |
| 2287 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2289 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2290 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2291 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2292 | lua_pushnil(L); |
| 2293 | return 1; |
| 2294 | } |
| 2295 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2296 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2298 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2299 | lua_pushnil(L); |
| 2300 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2301 | } |
| 2302 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2303 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2304 | xref_unlock(&socket->xref, peer); |
| 2305 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | } |
| 2307 | |
| 2308 | /* Returns information about my connection side. */ |
| 2309 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2310 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2311 | struct hlua_socket *socket; |
| 2312 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2313 | struct appctx *appctx; |
| 2314 | struct xref *peer; |
| 2315 | struct stream_interface *si; |
| 2316 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2317 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2318 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2319 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2320 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2321 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2322 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2323 | /* Check if we run on the same thread than the xreator thread. |
| 2324 | * We cannot access to the socket if the thread is different. |
| 2325 | */ |
| 2326 | if (socket->tid != tid) |
| 2327 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2328 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2329 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2330 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2331 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2332 | lua_pushnil(L); |
| 2333 | return 1; |
| 2334 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2335 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2336 | si = appctx->owner; |
| 2337 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2338 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2339 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2340 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2341 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2342 | lua_pushnil(L); |
| 2343 | return 1; |
| 2344 | } |
| 2345 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2346 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2347 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2348 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2349 | lua_pushnil(L); |
| 2350 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2351 | } |
| 2352 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2353 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2354 | xref_unlock(&socket->xref, peer); |
| 2355 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2359 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2360 | .obj_type = OBJ_TYPE_APPLET, |
| 2361 | .name = "<LUA_TCP>", |
| 2362 | .fct = hlua_socket_handler, |
| 2363 | .release = hlua_socket_release, |
| 2364 | }; |
| 2365 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2366 | __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] | 2367 | { |
| 2368 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2369 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2370 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2371 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2372 | struct stream_interface *si; |
| 2373 | struct stream *s; |
| 2374 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2375 | /* Check if we run on the same thread than the xreator thread. |
| 2376 | * We cannot access to the socket if the thread is different. |
| 2377 | */ |
| 2378 | if (socket->tid != tid) |
| 2379 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2380 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2381 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2382 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2383 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2384 | lua_pushnil(L); |
| 2385 | lua_pushstring(L, "Can't connect"); |
| 2386 | return 2; |
| 2387 | } |
| 2388 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2389 | si = appctx->owner; |
| 2390 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2391 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2392 | /* Check if we run on the same thread than the xreator thread. |
| 2393 | * We cannot access to the socket if the thread is different. |
| 2394 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2395 | if (socket->tid != tid) { |
| 2396 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2397 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2398 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2399 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2400 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2401 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2402 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2403 | lua_pushnil(L); |
| 2404 | lua_pushstring(L, "Can't connect"); |
| 2405 | return 2; |
| 2406 | } |
| 2407 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2408 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2409 | |
| 2410 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2411 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2412 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2413 | lua_pushinteger(L, 1); |
| 2414 | return 1; |
| 2415 | } |
| 2416 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2417 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2418 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2419 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2420 | } |
| 2421 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2422 | 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] | 2423 | return 0; |
| 2424 | } |
| 2425 | |
| 2426 | /* This function fail or initite the connection. */ |
| 2427 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2428 | { |
| 2429 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2430 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2431 | const char *ip; |
| 2432 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2433 | struct hlua *hlua; |
| 2434 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2435 | int low, high; |
| 2436 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2437 | struct xref *peer; |
| 2438 | struct stream_interface *si; |
| 2439 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2440 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2441 | if (lua_gettop(L) < 2) |
| 2442 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2443 | |
| 2444 | /* Get args. */ |
| 2445 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2446 | |
| 2447 | /* Check if we run on the same thread than the xreator thread. |
| 2448 | * We cannot access to the socket if the thread is different. |
| 2449 | */ |
| 2450 | if (socket->tid != tid) |
| 2451 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2452 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2453 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2454 | if (lua_gettop(L) >= 3) { |
| 2455 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2456 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2457 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2458 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2459 | * that are not enclosed within square brackets. |
| 2460 | */ |
| 2461 | if (port > 0) { |
| 2462 | luaL_buffinit(L, &b); |
| 2463 | luaL_addstring(&b, ip); |
| 2464 | luaL_addchar(&b, ':'); |
| 2465 | luaL_pushresult(&b); |
| 2466 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2467 | } |
| 2468 | } |
| 2469 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2470 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2471 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2472 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2473 | lua_pushnil(L); |
| 2474 | return 1; |
| 2475 | } |
| 2476 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2477 | si = appctx->owner; |
| 2478 | s = si_strm(si); |
| 2479 | |
| 2480 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2481 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2482 | if (!conn) { |
| 2483 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2484 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2485 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2486 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2487 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2488 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2489 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2490 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2491 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2492 | if (!addr) { |
| 2493 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2494 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2495 | } |
| 2496 | if (low != high) { |
| 2497 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2498 | 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] | 2499 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2500 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2501 | |
| 2502 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2503 | if (low == 0) { |
| 2504 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2505 | if (port == -1) { |
| 2506 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2507 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2508 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2509 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2510 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2511 | if (port == -1) { |
| 2512 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2513 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2514 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2515 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2516 | } |
| 2517 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2518 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2519 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2520 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2521 | |
| 2522 | /* inform the stream that we want to be notified whenever the |
| 2523 | * connection completes. |
| 2524 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2525 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2526 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2527 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2528 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2529 | hlua->flags |= HLUA_MUST_GC; |
| 2530 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2531 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2532 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2533 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2534 | } |
| 2535 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2536 | |
| 2537 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2538 | /* Return yield waiting for connection. */ |
| 2539 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2540 | 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] | 2541 | |
| 2542 | return 0; |
| 2543 | } |
| 2544 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2545 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2546 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2547 | { |
| 2548 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2549 | struct xref *peer; |
| 2550 | struct appctx *appctx; |
| 2551 | struct stream_interface *si; |
| 2552 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2553 | |
| 2554 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2555 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2556 | |
| 2557 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2558 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2559 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2560 | lua_pushnil(L); |
| 2561 | return 1; |
| 2562 | } |
| 2563 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2564 | si = appctx->owner; |
| 2565 | s = si_strm(si); |
| 2566 | |
| 2567 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2568 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2569 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2570 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2571 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2572 | |
| 2573 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2574 | { |
| 2575 | return 0; |
| 2576 | } |
| 2577 | |
| 2578 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2579 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2580 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2581 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2582 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2583 | struct xref *peer; |
| 2584 | struct appctx *appctx; |
| 2585 | struct stream_interface *si; |
| 2586 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2587 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2588 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2589 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2590 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2591 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2592 | /* convert the timeout to millis */ |
| 2593 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2594 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2595 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2596 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2597 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2598 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2599 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2600 | 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] | 2601 | |
| 2602 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2603 | if (tmout == 0) |
| 2604 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2605 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2606 | /* Check if we run on the same thread than the xreator thread. |
| 2607 | * We cannot access to the socket if the thread is different. |
| 2608 | */ |
| 2609 | if (socket->tid != tid) |
| 2610 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2611 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2612 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2613 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2614 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2615 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2616 | WILL_LJMP(lua_error(L)); |
| 2617 | return 0; |
| 2618 | } |
| 2619 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2620 | si = appctx->owner; |
| 2621 | s = si_strm(si); |
| 2622 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2623 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2624 | s->req.rto = tmout; |
| 2625 | s->req.wto = tmout; |
| 2626 | s->res.rto = tmout; |
| 2627 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2628 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2629 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2630 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2631 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2632 | |
| 2633 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2634 | task_queue(s->task); |
| 2635 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2636 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2637 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2638 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2639 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2640 | } |
| 2641 | |
| 2642 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2643 | { |
| 2644 | struct hlua_socket *socket; |
| 2645 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2646 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2647 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2648 | |
| 2649 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2650 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2651 | hlua_pusherror(L, "socket: full stack"); |
| 2652 | goto out_fail_conf; |
| 2653 | } |
| 2654 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2655 | /* Create the object: obj[0] = userdata. */ |
| 2656 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2657 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2658 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2659 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2660 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2661 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2662 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2663 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2664 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2665 | goto out_fail_conf; |
| 2666 | } |
| 2667 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2668 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2669 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2670 | lua_setmetatable(L, -2); |
| 2671 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2672 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2673 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2674 | if (!appctx) { |
| 2675 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2676 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2677 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2678 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2679 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2680 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2681 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2682 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2683 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2684 | /* Now create a session, task and stream for this applet */ |
| 2685 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2686 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2687 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2688 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2689 | } |
| 2690 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2691 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2692 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2693 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2694 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2695 | } |
| 2696 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2697 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2698 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2699 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2700 | /* Configure "right" stream interface. this "si" is used to connect |
| 2701 | * and retrieve data from the server. The connection is initialized |
| 2702 | * with the "struct server". |
| 2703 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2704 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2705 | |
| 2706 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2707 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2708 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2709 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2710 | return 1; |
| 2711 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2712 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2713 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2714 | out_fail_sess: |
| 2715 | appctx_free(appctx); |
| 2716 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2717 | WILL_LJMP(lua_error(L)); |
| 2718 | return 0; |
| 2719 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2720 | |
| 2721 | /* |
| 2722 | * |
| 2723 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2724 | * Class Channel |
| 2725 | * |
| 2726 | * |
| 2727 | */ |
| 2728 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2729 | /* This function is called before the Lua execution. It stores |
| 2730 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2731 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2732 | static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2733 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2734 | c->mode = stream->be->mode; |
| 2735 | switch (c->mode) { |
| 2736 | case PR_MODE_HTTP: |
| 2737 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2738 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2739 | c->data.http.state = stream->txn->req.msg_state; |
| 2740 | else |
| 2741 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2742 | break; |
| 2743 | default: |
| 2744 | break; |
| 2745 | } |
| 2746 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2747 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2748 | /* This function is called after the Lua execution. it |
| 2749 | * returns true if the parser state is consistent, otherwise, |
| 2750 | * it return false. |
| 2751 | * |
| 2752 | * In HTTP mode, the parser state must be in the same state |
| 2753 | * or greater when we exit the function. Even if we do a |
| 2754 | * control yield. This prevent to break the HTTP message |
| 2755 | * from the Lua code. |
| 2756 | */ |
| 2757 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2758 | { |
| 2759 | if (c->mode != stream->be->mode) |
| 2760 | return 0; |
| 2761 | |
| 2762 | switch (c->mode) { |
| 2763 | case PR_MODE_HTTP: |
| 2764 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2765 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2766 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2767 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2768 | else |
| 2769 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2770 | default: |
| 2771 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2772 | } |
| 2773 | return 1; |
| 2774 | } |
| 2775 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2776 | /* Returns the struct hlua_channel join to the class channel in the |
| 2777 | * stack entry "ud" or throws an argument error. |
| 2778 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2779 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2780 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2781 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2782 | } |
| 2783 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2784 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2785 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2786 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2787 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2788 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2789 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2790 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2791 | return 0; |
| 2792 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2793 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2794 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2795 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2796 | |
| 2797 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2798 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2799 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2800 | return 1; |
| 2801 | } |
| 2802 | |
| 2803 | /* Duplicate all the data present in the input channel and put it |
| 2804 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2805 | * the stack if the channel is closed and all the data are consumed, |
| 2806 | * returns 0 if no data are available, otherwise it returns the length |
| 2807 | * of the builded string. |
| 2808 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2809 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2810 | { |
| 2811 | char *blk1; |
| 2812 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2813 | size_t len1; |
| 2814 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2815 | int ret; |
| 2816 | luaL_Buffer b; |
| 2817 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2818 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2819 | if (unlikely(ret == 0)) |
| 2820 | return 0; |
| 2821 | |
| 2822 | if (unlikely(ret < 0)) { |
| 2823 | lua_pushnil(L); |
| 2824 | return -1; |
| 2825 | } |
| 2826 | |
| 2827 | luaL_buffinit(L, &b); |
| 2828 | luaL_addlstring(&b, blk1, len1); |
| 2829 | if (unlikely(ret == 2)) |
| 2830 | luaL_addlstring(&b, blk2, len2); |
| 2831 | luaL_pushresult(&b); |
| 2832 | |
| 2833 | if (unlikely(ret == 2)) |
| 2834 | return len1 + len2; |
| 2835 | return len1; |
| 2836 | } |
| 2837 | |
| 2838 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2839 | * a yield. This function keep the data in the buffer. |
| 2840 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2841 | __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] | 2842 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2843 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2844 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2845 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2846 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2847 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2848 | WILL_LJMP(lua_error(L)); |
| 2849 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2850 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2851 | 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] | 2852 | return 1; |
| 2853 | } |
| 2854 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2855 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2856 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2857 | { |
| 2858 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2859 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2860 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2861 | } |
| 2862 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2863 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2864 | * a yield. This function consumes the data in the buffer. It returns |
| 2865 | * a string containing the data or a nil pointer if no data are available |
| 2866 | * and the channel is closed. |
| 2867 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2868 | __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] | 2869 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2870 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2871 | int ret; |
| 2872 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2873 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2874 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2875 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2876 | WILL_LJMP(lua_error(L)); |
| 2877 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2878 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2879 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2880 | 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] | 2881 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2882 | if (unlikely(ret == -1)) |
| 2883 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2884 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2885 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2886 | return 1; |
| 2887 | } |
| 2888 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2889 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2890 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2891 | { |
| 2892 | MAY_LJMP(check_args(L, 1, "get")); |
| 2893 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2894 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2895 | } |
| 2896 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2897 | /* This functions consumes and returns one line. If the channel is closed, |
| 2898 | * 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] | 2899 | * 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] | 2900 | * value. |
| 2901 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2902 | __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] | 2903 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2904 | char *blk1; |
| 2905 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2906 | size_t len1; |
| 2907 | size_t len2; |
| 2908 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2909 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2910 | int ret; |
| 2911 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2912 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2913 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2914 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2915 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2916 | WILL_LJMP(lua_error(L)); |
| 2917 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2918 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2919 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2920 | 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] | 2921 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2922 | if (ret == -1) { |
| 2923 | lua_pushnil(L); |
| 2924 | return 1; |
| 2925 | } |
| 2926 | |
| 2927 | luaL_buffinit(L, &b); |
| 2928 | luaL_addlstring(&b, blk1, len1); |
| 2929 | len = len1; |
| 2930 | if (unlikely(ret == 2)) { |
| 2931 | luaL_addlstring(&b, blk2, len2); |
| 2932 | len += len2; |
| 2933 | } |
| 2934 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2935 | 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] | 2936 | return 1; |
| 2937 | } |
| 2938 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2939 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2940 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2941 | { |
| 2942 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2943 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2944 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2945 | } |
| 2946 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2947 | /* This function takes a string as input, and append it at the |
| 2948 | * input side of channel. If the data is too big, but a space |
| 2949 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2950 | * yields. If the data is bigger than the buffer, or if the |
| 2951 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2952 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2953 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2954 | __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] | 2955 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2956 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2957 | size_t len; |
| 2958 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2959 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2960 | int ret; |
| 2961 | int max; |
| 2962 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2963 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2964 | WILL_LJMP(lua_error(L)); |
| 2965 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2966 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2967 | * the request buffer if its not required. |
| 2968 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2969 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2970 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2971 | 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] | 2972 | } |
| 2973 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2974 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2975 | if (max > len - l) |
| 2976 | max = len - l; |
| 2977 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2978 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2979 | if (ret == -2 || ret == -3) { |
| 2980 | lua_pushinteger(L, -1); |
| 2981 | return 1; |
| 2982 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2983 | if (ret == -1) { |
| 2984 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2985 | 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] | 2986 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2987 | l += ret; |
| 2988 | lua_pop(L, 1); |
| 2989 | lua_pushinteger(L, l); |
| 2990 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2991 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2992 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2993 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2994 | * in this case, we cannot add more data, so we cannot yield, |
| 2995 | * we return the amount of copyied data. |
| 2996 | */ |
| 2997 | return 1; |
| 2998 | } |
| 2999 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3000 | 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] | 3001 | return 1; |
| 3002 | } |
| 3003 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3004 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3005 | * 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] | 3006 | * buffer size is too little for the data. |
| 3007 | */ |
| 3008 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3009 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3010 | size_t len; |
| 3011 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3012 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3013 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3014 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3015 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3016 | lua_pushinteger(L, 0); |
| 3017 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3018 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3019 | } |
| 3020 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3021 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3022 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3023 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3024 | * or -1 if the channel is closed or if the buffer size is too |
| 3025 | * little for the data. |
| 3026 | */ |
| 3027 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3028 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3029 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3030 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3031 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3032 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3033 | lua_pushinteger(L, 0); |
| 3034 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3035 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3036 | WILL_LJMP(lua_error(L)); |
| 3037 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3038 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3039 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3040 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3041 | } |
| 3042 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3043 | /* Append data in the output side of the buffer. This data is immediately |
| 3044 | * sent. The function returns the amount of data written. If the buffer |
| 3045 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3046 | * if the channel is closed. |
| 3047 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3048 | __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] | 3049 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3050 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3051 | size_t len; |
| 3052 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3053 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3054 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3055 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3056 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3057 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3058 | WILL_LJMP(lua_error(L)); |
| 3059 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3060 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3061 | lua_pushinteger(L, -1); |
| 3062 | return 1; |
| 3063 | } |
| 3064 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3065 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3066 | * the request buffer if its not required. |
| 3067 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3068 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3069 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3070 | 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] | 3071 | } |
| 3072 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3073 | /* The written data will be immediately sent, so we can check |
| 3074 | * the available space without taking in account the reserve. |
| 3075 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3076 | * data, because the buffer will be flushed. |
| 3077 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3078 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3079 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3080 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3081 | * in this case, we cannot add more data, so we cannot yield, |
| 3082 | * we return the amount of copyied data. |
| 3083 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3084 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3085 | return 1; |
| 3086 | |
| 3087 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3088 | if (max > len - l) |
| 3089 | max = len - l; |
| 3090 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3091 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3092 | * detects a non contiguous buffer and realign it. |
| 3093 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3094 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3095 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3096 | |
| 3097 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3098 | 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] | 3099 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3100 | /* buffer replace considers that the input part is filled. |
| 3101 | * so, I must forward these new data in the output part. |
| 3102 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3103 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3104 | |
| 3105 | l += max; |
| 3106 | lua_pop(L, 1); |
| 3107 | lua_pushinteger(L, l); |
| 3108 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3109 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3110 | * in this case, we cannot add more data, so we cannot yield, |
| 3111 | * we return the amount of copyied data. |
| 3112 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3113 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3114 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3115 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3116 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3117 | if (l < len) { |
| 3118 | /* If we are waiting for space in the response buffer, we |
| 3119 | * must set the flag WAKERESWR. This flag required the task |
| 3120 | * wake up if any activity is detected on the response buffer. |
| 3121 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3122 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3123 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3124 | else |
| 3125 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3126 | 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] | 3127 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3128 | |
| 3129 | return 1; |
| 3130 | } |
| 3131 | |
| 3132 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3133 | * yield the LUA process, and resume it without checking the |
| 3134 | * input arguments. |
| 3135 | */ |
| 3136 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3137 | { |
| 3138 | MAY_LJMP(check_args(L, 2, "send")); |
| 3139 | lua_pushinteger(L, 0); |
| 3140 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3141 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | /* This function forward and amount of butes. The data pass from |
| 3145 | * the input side of the buffer to the output side, and can be |
| 3146 | * forwarded. This function never fails. |
| 3147 | * |
| 3148 | * The Lua function takes an amount of bytes to be forwarded in |
| 3149 | * imput. It returns the number of bytes forwarded. |
| 3150 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3151 | __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] | 3152 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3153 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3154 | int len; |
| 3155 | int l; |
| 3156 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3157 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3158 | |
| 3159 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3160 | |
| 3161 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3162 | WILL_LJMP(lua_error(L)); |
| 3163 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3164 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3165 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3166 | |
| 3167 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3168 | if (max > ci_data(chn)) |
| 3169 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3170 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3171 | l += max; |
| 3172 | |
| 3173 | lua_pop(L, 1); |
| 3174 | lua_pushinteger(L, l); |
| 3175 | |
| 3176 | /* Check if it miss bytes to forward. */ |
| 3177 | if (l < len) { |
| 3178 | /* The the input channel or the output channel are closed, we |
| 3179 | * must return the amount of data forwarded. |
| 3180 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3181 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3182 | return 1; |
| 3183 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3184 | /* If we are waiting for space data in the response buffer, we |
| 3185 | * must set the flag WAKERESWR. This flag required the task |
| 3186 | * wake up if any activity is detected on the response buffer. |
| 3187 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3188 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3189 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3190 | else |
| 3191 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3192 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3193 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3194 | 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] | 3195 | } |
| 3196 | |
| 3197 | return 1; |
| 3198 | } |
| 3199 | |
| 3200 | /* Just check the input and prepare the stack for the previous |
| 3201 | * function "hlua_channel_forward_yield" |
| 3202 | */ |
| 3203 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3204 | { |
| 3205 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3206 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3207 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3208 | |
| 3209 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3210 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | /* Just returns the number of bytes available in the input |
| 3214 | * side of the buffer. This function never fails. |
| 3215 | */ |
| 3216 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3217 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3218 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3219 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3220 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3221 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3222 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3223 | struct htx *htx = htxbuf(&chn->buf); |
| 3224 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3225 | } |
| 3226 | else |
| 3227 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3228 | return 1; |
| 3229 | } |
| 3230 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3231 | /* Returns true if the channel is full. */ |
| 3232 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3233 | { |
| 3234 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3235 | |
| 3236 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3237 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0136ebb | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3238 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3239 | * applet). |
| 3240 | */ |
| 3241 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3242 | return 1; |
| 3243 | } |
| 3244 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3245 | /* Just returns the number of bytes available in the output |
| 3246 | * side of the buffer. This function never fails. |
| 3247 | */ |
| 3248 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3249 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3250 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3251 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3252 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3253 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3254 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3255 | return 1; |
| 3256 | } |
| 3257 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3258 | /* |
| 3259 | * |
| 3260 | * |
| 3261 | * Class Fetches |
| 3262 | * |
| 3263 | * |
| 3264 | */ |
| 3265 | |
| 3266 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3267 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3268 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3269 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3270 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3271 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | /* This function creates and push in the stack a fetch object according |
| 3275 | * with a current TXN. |
| 3276 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3277 | 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] | 3278 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3279 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3280 | |
| 3281 | /* Check stack size. */ |
| 3282 | if (!lua_checkstack(L, 3)) |
| 3283 | return 0; |
| 3284 | |
| 3285 | /* Create the object: obj[0] = userdata. |
| 3286 | * Note that the base of the Fetches object is the |
| 3287 | * transaction object. |
| 3288 | */ |
| 3289 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3290 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3291 | lua_rawseti(L, -2, 0); |
| 3292 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3293 | hsmp->s = txn->s; |
| 3294 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3295 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3296 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3297 | |
| 3298 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3299 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3300 | lua_setmetatable(L, -2); |
| 3301 | |
| 3302 | return 1; |
| 3303 | } |
| 3304 | |
| 3305 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3306 | * It uses closure argument to store the associated sample-fetch. It |
| 3307 | * returns only one argument or throws an error. An error is thrown |
| 3308 | * only if an error is encountered during the argument parsing. If |
| 3309 | * the "sample-fetch" function fails, nil is returned. |
| 3310 | */ |
| 3311 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3312 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3313 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3314 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3315 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3316 | int i; |
| 3317 | struct sample smp; |
| 3318 | |
| 3319 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3320 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3321 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3322 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3323 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3324 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3325 | /* Check execution authorization. */ |
| 3326 | if (f->use & SMP_USE_HTTP_ANY && |
| 3327 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3328 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3329 | "is not available in Lua services", f->kw); |
| 3330 | WILL_LJMP(lua_error(L)); |
| 3331 | } |
| 3332 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3333 | /* Get extra arguments. */ |
| 3334 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3335 | if (i >= ARGM_NBARGS) |
| 3336 | break; |
| 3337 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3338 | } |
| 3339 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3340 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3341 | |
| 3342 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3343 | 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] | 3344 | |
| 3345 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3346 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3347 | lua_pushfstring(L, "error in arguments"); |
| 3348 | WILL_LJMP(lua_error(L)); |
| 3349 | } |
| 3350 | |
| 3351 | /* Initialise the sample. */ |
| 3352 | memset(&smp, 0, sizeof(smp)); |
| 3353 | |
| 3354 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3355 | 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] | 3356 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3357 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3358 | lua_pushstring(L, ""); |
| 3359 | else |
| 3360 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3361 | return 1; |
| 3362 | } |
| 3363 | |
| 3364 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3365 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3366 | hlua_smp2lua_str(L, &smp); |
| 3367 | else |
| 3368 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3369 | return 1; |
| 3370 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3371 | |
| 3372 | /* |
| 3373 | * |
| 3374 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3375 | * Class Converters |
| 3376 | * |
| 3377 | * |
| 3378 | */ |
| 3379 | |
| 3380 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3381 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3382 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3383 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3384 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3385 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | /* This function creates and push in the stack a Converters object |
| 3389 | * according with a current TXN. |
| 3390 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3391 | 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] | 3392 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3393 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3394 | |
| 3395 | /* Check stack size. */ |
| 3396 | if (!lua_checkstack(L, 3)) |
| 3397 | return 0; |
| 3398 | |
| 3399 | /* Create the object: obj[0] = userdata. |
| 3400 | * Note that the base of the Converters object is the |
| 3401 | * same than the TXN object. |
| 3402 | */ |
| 3403 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3404 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3405 | lua_rawseti(L, -2, 0); |
| 3406 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3407 | hsmp->s = txn->s; |
| 3408 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3409 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3410 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3411 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3412 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3413 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3414 | lua_setmetatable(L, -2); |
| 3415 | |
| 3416 | return 1; |
| 3417 | } |
| 3418 | |
| 3419 | /* This function is an LUA binding. It is called with each converter. |
| 3420 | * It uses closure argument to store the associated converter. It |
| 3421 | * returns only one argument or throws an error. An error is thrown |
| 3422 | * only if an error is encountered during the argument parsing. If |
| 3423 | * the converter function function fails, nil is returned. |
| 3424 | */ |
| 3425 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3426 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3427 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3428 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3429 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3430 | int i; |
| 3431 | struct sample smp; |
| 3432 | |
| 3433 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3434 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3435 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3436 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3437 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3438 | |
| 3439 | /* Get extra arguments. */ |
| 3440 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3441 | if (i >= ARGM_NBARGS) |
| 3442 | break; |
| 3443 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3444 | } |
| 3445 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3446 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3447 | |
| 3448 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3449 | 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] | 3450 | |
| 3451 | /* Run the special args checker. */ |
| 3452 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3453 | hlua_pusherror(L, "error in arguments"); |
| 3454 | WILL_LJMP(lua_error(L)); |
| 3455 | } |
| 3456 | |
| 3457 | /* Initialise the sample. */ |
| 3458 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3459 | hlua_pusherror(L, "error in the input argument"); |
| 3460 | WILL_LJMP(lua_error(L)); |
| 3461 | } |
| 3462 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3463 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3464 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3465 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3466 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3467 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3468 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3469 | WILL_LJMP(lua_error(L)); |
| 3470 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3471 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3472 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3473 | hlua_pusherror(L, "error during the input argument casting"); |
| 3474 | WILL_LJMP(lua_error(L)); |
| 3475 | } |
| 3476 | |
| 3477 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3478 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3479 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3480 | lua_pushstring(L, ""); |
| 3481 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3482 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3483 | return 1; |
| 3484 | } |
| 3485 | |
| 3486 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3487 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3488 | hlua_smp2lua_str(L, &smp); |
| 3489 | else |
| 3490 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3491 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3492 | } |
| 3493 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3494 | /* |
| 3495 | * |
| 3496 | * |
| 3497 | * Class AppletTCP |
| 3498 | * |
| 3499 | * |
| 3500 | */ |
| 3501 | |
| 3502 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3503 | * a class stream, otherwise it throws an error. |
| 3504 | */ |
| 3505 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3506 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3507 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | /* This function creates and push in the stack an Applet object |
| 3511 | * according with a current TXN. |
| 3512 | */ |
| 3513 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3514 | { |
| 3515 | struct hlua_appctx *appctx; |
| 3516 | struct stream_interface *si = ctx->owner; |
| 3517 | struct stream *s = si_strm(si); |
| 3518 | struct proxy *p = s->be; |
| 3519 | |
| 3520 | /* Check stack size. */ |
| 3521 | if (!lua_checkstack(L, 3)) |
| 3522 | return 0; |
| 3523 | |
| 3524 | /* Create the object: obj[0] = userdata. |
| 3525 | * Note that the base of the Converters object is the |
| 3526 | * same than the TXN object. |
| 3527 | */ |
| 3528 | lua_newtable(L); |
| 3529 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3530 | lua_rawseti(L, -2, 0); |
| 3531 | appctx->appctx = ctx; |
| 3532 | appctx->htxn.s = s; |
| 3533 | appctx->htxn.p = p; |
| 3534 | |
| 3535 | /* Create the "f" field that contains a list of fetches. */ |
| 3536 | lua_pushstring(L, "f"); |
| 3537 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3538 | return 0; |
| 3539 | lua_settable(L, -3); |
| 3540 | |
| 3541 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3542 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3543 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3544 | return 0; |
| 3545 | lua_settable(L, -3); |
| 3546 | |
| 3547 | /* Create the "c" field that contains a list of converters. */ |
| 3548 | lua_pushstring(L, "c"); |
| 3549 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3550 | return 0; |
| 3551 | lua_settable(L, -3); |
| 3552 | |
| 3553 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3554 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3555 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3556 | return 0; |
| 3557 | lua_settable(L, -3); |
| 3558 | |
| 3559 | /* Pop a class stream metatable and affect it to the table. */ |
| 3560 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3561 | lua_setmetatable(L, -2); |
| 3562 | |
| 3563 | return 1; |
| 3564 | } |
| 3565 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3566 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3567 | { |
| 3568 | struct hlua_appctx *appctx; |
| 3569 | struct stream *s; |
| 3570 | const char *name; |
| 3571 | size_t len; |
| 3572 | struct sample smp; |
| 3573 | |
| 3574 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3575 | |
| 3576 | /* It is useles to retrieve the stream, but this function |
| 3577 | * runs only in a stream context. |
| 3578 | */ |
| 3579 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3580 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3581 | s = appctx->htxn.s; |
| 3582 | |
| 3583 | /* Converts the third argument in a sample. */ |
| 3584 | hlua_lua2smp(L, 3, &smp); |
| 3585 | |
| 3586 | /* Store the sample in a variable. */ |
| 3587 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3588 | vars_set_by_name(name, len, &smp); |
| 3589 | return 0; |
| 3590 | } |
| 3591 | |
| 3592 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3593 | { |
| 3594 | struct hlua_appctx *appctx; |
| 3595 | struct stream *s; |
| 3596 | const char *name; |
| 3597 | size_t len; |
| 3598 | struct sample smp; |
| 3599 | |
| 3600 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3601 | |
| 3602 | /* It is useles to retrieve the stream, but this function |
| 3603 | * runs only in a stream context. |
| 3604 | */ |
| 3605 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3606 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3607 | s = appctx->htxn.s; |
| 3608 | |
| 3609 | /* Unset the variable. */ |
| 3610 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3611 | vars_unset_by_name(name, len, &smp); |
| 3612 | return 0; |
| 3613 | } |
| 3614 | |
| 3615 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3616 | { |
| 3617 | struct hlua_appctx *appctx; |
| 3618 | struct stream *s; |
| 3619 | const char *name; |
| 3620 | size_t len; |
| 3621 | struct sample smp; |
| 3622 | |
| 3623 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3624 | |
| 3625 | /* It is useles to retrieve the stream, but this function |
| 3626 | * runs only in a stream context. |
| 3627 | */ |
| 3628 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3629 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3630 | s = appctx->htxn.s; |
| 3631 | |
| 3632 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3633 | if (!vars_get_by_name(name, len, &smp)) { |
| 3634 | lua_pushnil(L); |
| 3635 | return 1; |
| 3636 | } |
| 3637 | |
| 3638 | return hlua_smp2lua(L, &smp); |
| 3639 | } |
| 3640 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3641 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3642 | { |
| 3643 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3644 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3645 | struct hlua *hlua; |
| 3646 | |
| 3647 | /* 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] | 3648 | if (!s->hlua) |
| 3649 | return 0; |
| 3650 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3651 | |
| 3652 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3653 | |
| 3654 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3655 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3656 | |
| 3657 | /* Get and store new value. */ |
| 3658 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3659 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3660 | |
| 3661 | return 0; |
| 3662 | } |
| 3663 | |
| 3664 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3665 | { |
| 3666 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3667 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3668 | struct hlua *hlua; |
| 3669 | |
| 3670 | /* 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] | 3671 | if (!s->hlua) { |
| 3672 | lua_pushnil(L); |
| 3673 | return 1; |
| 3674 | } |
| 3675 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3676 | |
| 3677 | /* Push configuration index in the stack. */ |
| 3678 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3679 | |
| 3680 | return 1; |
| 3681 | } |
| 3682 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3683 | /* If expected data not yet available, it returns a yield. This function |
| 3684 | * consumes the data in the buffer. It returns a string containing the |
| 3685 | * data. This string can be empty. |
| 3686 | */ |
| 3687 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3688 | { |
| 3689 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3690 | struct stream_interface *si = appctx->appctx->owner; |
| 3691 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3692 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3693 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3694 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3695 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3696 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3697 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3698 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3699 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3700 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3701 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3702 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3703 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3704 | } |
| 3705 | |
| 3706 | /* End of data: commit the total strings and return. */ |
| 3707 | if (ret < 0) { |
| 3708 | luaL_pushresult(&appctx->b); |
| 3709 | return 1; |
| 3710 | } |
| 3711 | |
| 3712 | /* Ensure that the block 2 length is usable. */ |
| 3713 | if (ret == 1) |
| 3714 | len2 = 0; |
| 3715 | |
| 3716 | /* dont check the max length read and dont check. */ |
| 3717 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3718 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3719 | |
| 3720 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3721 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3722 | luaL_pushresult(&appctx->b); |
| 3723 | return 1; |
| 3724 | } |
| 3725 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3726 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3727 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3728 | { |
| 3729 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3730 | |
| 3731 | /* Initialise the string catenation. */ |
| 3732 | luaL_buffinit(L, &appctx->b); |
| 3733 | |
| 3734 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3735 | } |
| 3736 | |
| 3737 | /* If expected data not yet available, it returns a yield. This function |
| 3738 | * consumes the data in the buffer. It returns a string containing the |
| 3739 | * data. This string can be empty. |
| 3740 | */ |
| 3741 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3742 | { |
| 3743 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3744 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3745 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3746 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3747 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3748 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3749 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3750 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3751 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3752 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3753 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3754 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3755 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3756 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3757 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3758 | 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] | 3759 | } |
| 3760 | |
| 3761 | /* End of data: commit the total strings and return. */ |
| 3762 | if (ret < 0) { |
| 3763 | luaL_pushresult(&appctx->b); |
| 3764 | return 1; |
| 3765 | } |
| 3766 | |
| 3767 | /* Ensure that the block 2 length is usable. */ |
| 3768 | if (ret == 1) |
| 3769 | len2 = 0; |
| 3770 | |
| 3771 | if (len == -1) { |
| 3772 | |
| 3773 | /* If len == -1, catenate all the data avalaile and |
| 3774 | * yield because we want to get all the data until |
| 3775 | * the end of data stream. |
| 3776 | */ |
| 3777 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3778 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3779 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3780 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3781 | 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] | 3782 | |
| 3783 | } else { |
| 3784 | |
| 3785 | /* Copy the fisrt block caping to the length required. */ |
| 3786 | if (len1 > len) |
| 3787 | len1 = len; |
| 3788 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3789 | len -= len1; |
| 3790 | |
| 3791 | /* Copy the second block. */ |
| 3792 | if (len2 > len) |
| 3793 | len2 = len; |
| 3794 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3795 | len -= len2; |
| 3796 | |
| 3797 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3798 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3799 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3800 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3801 | if (len > 0) { |
| 3802 | lua_pushinteger(L, len); |
| 3803 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3804 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3805 | 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] | 3806 | } |
| 3807 | |
| 3808 | /* return the result. */ |
| 3809 | luaL_pushresult(&appctx->b); |
| 3810 | return 1; |
| 3811 | } |
| 3812 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3813 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3814 | hlua_pusherror(L, "Lua: internal error"); |
| 3815 | WILL_LJMP(lua_error(L)); |
| 3816 | return 0; |
| 3817 | } |
| 3818 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3819 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3820 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3821 | { |
| 3822 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3823 | int len = -1; |
| 3824 | |
| 3825 | if (lua_gettop(L) > 2) |
| 3826 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3827 | if (lua_gettop(L) >= 2) { |
| 3828 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3829 | lua_pop(L, 1); |
| 3830 | } |
| 3831 | |
| 3832 | /* Confirm or set the required length */ |
| 3833 | lua_pushinteger(L, len); |
| 3834 | |
| 3835 | /* Initialise the string catenation. */ |
| 3836 | luaL_buffinit(L, &appctx->b); |
| 3837 | |
| 3838 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3839 | } |
| 3840 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3841 | /* Append data in the output side of the buffer. This data is immediately |
| 3842 | * sent. The function returns the amount of data written. If the buffer |
| 3843 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3844 | * if the channel is closed. |
| 3845 | */ |
| 3846 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3847 | { |
| 3848 | size_t len; |
| 3849 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3850 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3851 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3852 | struct stream_interface *si = appctx->appctx->owner; |
| 3853 | struct channel *chn = si_ic(si); |
| 3854 | int max; |
| 3855 | |
| 3856 | /* Get the max amount of data which can write as input in the channel. */ |
| 3857 | max = channel_recv_max(chn); |
| 3858 | if (max > (len - l)) |
| 3859 | max = len - l; |
| 3860 | |
| 3861 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3862 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3863 | |
| 3864 | /* update counters. */ |
| 3865 | l += max; |
| 3866 | lua_pop(L, 1); |
| 3867 | lua_pushinteger(L, l); |
| 3868 | |
| 3869 | /* If some data is not send, declares the situation to the |
| 3870 | * applet, and returns a yield. |
| 3871 | */ |
| 3872 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3873 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3874 | 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] | 3875 | } |
| 3876 | |
| 3877 | return 1; |
| 3878 | } |
| 3879 | |
| 3880 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3881 | * yield the LUA process, and resume it without checking the |
| 3882 | * input arguments. |
| 3883 | */ |
| 3884 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3885 | { |
| 3886 | MAY_LJMP(check_args(L, 2, "send")); |
| 3887 | lua_pushinteger(L, 0); |
| 3888 | |
| 3889 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3890 | } |
| 3891 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3892 | /* |
| 3893 | * |
| 3894 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3895 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3896 | * |
| 3897 | * |
| 3898 | */ |
| 3899 | |
| 3900 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3901 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3902 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3903 | __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] | 3904 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3905 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3906 | } |
| 3907 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3908 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3909 | * according with a current TXN. |
| 3910 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3911 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3912 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3913 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3914 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3915 | struct stream_interface *si = ctx->owner; |
| 3916 | struct stream *s = si_strm(si); |
| 3917 | struct proxy *px = s->be; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3918 | |
| 3919 | /* Check stack size. */ |
| 3920 | if (!lua_checkstack(L, 3)) |
| 3921 | return 0; |
| 3922 | |
| 3923 | /* Create the object: obj[0] = userdata. |
| 3924 | * Note that the base of the Converters object is the |
| 3925 | * same than the TXN object. |
| 3926 | */ |
| 3927 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3928 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3929 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3930 | appctx->appctx = ctx; |
| 3931 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3932 | 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] | 3933 | appctx->htxn.s = s; |
| 3934 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3935 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3936 | /* Create the "f" field that contains a list of fetches. */ |
| 3937 | lua_pushstring(L, "f"); |
| 3938 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3939 | return 0; |
| 3940 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3941 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3942 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3943 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3944 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3945 | return 0; |
| 3946 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3947 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3948 | /* Create the "c" field that contains a list of converters. */ |
| 3949 | lua_pushstring(L, "c"); |
| 3950 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3951 | return 0; |
| 3952 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3953 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3954 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3955 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3956 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3957 | return 0; |
| 3958 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3959 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3960 | if (IS_HTX_STRM(s)) { |
| 3961 | /* HTX version */ |
| 3962 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3963 | struct htx_blk *blk; |
| 3964 | struct htx_sl *sl; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3965 | struct ist path; |
| 3966 | unsigned long long len = 0; |
| 3967 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3968 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3969 | blk = htx_get_first_blk(htx); |
Christopher Faulet | 43a686d | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 3970 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3971 | sl = htx_get_blk_ptr(htx, blk); |
| 3972 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3973 | /* Stores the request method. */ |
| 3974 | lua_pushstring(L, "method"); |
| 3975 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3976 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3977 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3978 | /* Stores the http version. */ |
| 3979 | lua_pushstring(L, "version"); |
| 3980 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3981 | lua_settable(L, -3); |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3982 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3983 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3984 | * the array on the top of the stack. |
| 3985 | */ |
| 3986 | lua_pushstring(L, "headers"); |
| 3987 | htxn.s = s; |
| 3988 | htxn.p = px; |
| 3989 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3990 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3991 | return 0; |
| 3992 | lua_settable(L, -3); |
| 3993 | |
| 3994 | path = http_get_path(htx_sl_req_uri(sl)); |
| 3995 | if (path.ptr) { |
| 3996 | char *p, *q, *end; |
| 3997 | |
| 3998 | p = path.ptr; |
| 3999 | end = path.ptr + path.len; |
| 4000 | q = p; |
| 4001 | while (q < end && *q != '?') |
| 4002 | q++; |
| 4003 | |
| 4004 | /* Stores the request path. */ |
| 4005 | lua_pushstring(L, "path"); |
| 4006 | lua_pushlstring(L, p, q - p); |
| 4007 | lua_settable(L, -3); |
| 4008 | |
| 4009 | /* Stores the query string. */ |
| 4010 | lua_pushstring(L, "qs"); |
| 4011 | if (*q == '?') |
| 4012 | q++; |
| 4013 | lua_pushlstring(L, q, end - q); |
| 4014 | lua_settable(L, -3); |
| 4015 | } |
| 4016 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4017 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4018 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4019 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4020 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4021 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4022 | break; |
| 4023 | if (type == HTX_BLK_DATA) |
| 4024 | len += htx_get_blksz(blk); |
| 4025 | } |
| 4026 | if (htx->extra != ULLONG_MAX) |
| 4027 | len += htx->extra; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4028 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4029 | /* Stores the request path. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4030 | lua_pushstring(L, "length"); |
| 4031 | lua_pushinteger(L, len); |
| 4032 | lua_settable(L, -3); |
| 4033 | } |
| 4034 | else { |
| 4035 | /* Legacy HTTP version */ |
| 4036 | struct http_txn *txn = s->txn; |
| 4037 | const char *path; |
| 4038 | const char *end; |
| 4039 | const char *p; |
| 4040 | |
| 4041 | /* Stores the request method. */ |
| 4042 | lua_pushstring(L, "method"); |
| 4043 | lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4044 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4045 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4046 | /* Stores the http version. */ |
| 4047 | lua_pushstring(L, "version"); |
| 4048 | lua_pushlstring(L, ci_head(txn->req.chn) + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4049 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4050 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4051 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4052 | * the array on the top of the stack. |
| 4053 | */ |
| 4054 | lua_pushstring(L, "headers"); |
| 4055 | htxn.s = s; |
| 4056 | htxn.p = px; |
| 4057 | htxn.dir = SMP_OPT_DIR_REQ; |
| 4058 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 4059 | return 0; |
| 4060 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4061 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4062 | /* Get path and qs */ |
| 4063 | path = http_txn_get_path(txn); |
| 4064 | if (path) { |
| 4065 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 4066 | p = path; |
| 4067 | while (p < end && *p != '?') |
| 4068 | p++; |
| 4069 | |
| 4070 | /* Stores the request path. */ |
| 4071 | lua_pushstring(L, "path"); |
| 4072 | lua_pushlstring(L, path, p - path); |
| 4073 | lua_settable(L, -3); |
| 4074 | |
| 4075 | /* Stores the query string. */ |
| 4076 | lua_pushstring(L, "qs"); |
| 4077 | if (*p == '?') |
| 4078 | p++; |
| 4079 | lua_pushlstring(L, p, end - p); |
| 4080 | lua_settable(L, -3); |
| 4081 | } |
| 4082 | |
| 4083 | /* Stores the request path. */ |
| 4084 | lua_pushstring(L, "length"); |
| 4085 | lua_pushinteger(L, txn->req.body_len); |
| 4086 | lua_settable(L, -3); |
| 4087 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4088 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4089 | /* Create an empty array of HTTP request headers. */ |
| 4090 | lua_pushstring(L, "response"); |
| 4091 | lua_newtable(L); |
| 4092 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4093 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4094 | /* Pop a class stream metatable and affect it to the table. */ |
| 4095 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4096 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4097 | |
| 4098 | return 1; |
| 4099 | } |
| 4100 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4101 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4102 | { |
| 4103 | struct hlua_appctx *appctx; |
| 4104 | struct stream *s; |
| 4105 | const char *name; |
| 4106 | size_t len; |
| 4107 | struct sample smp; |
| 4108 | |
| 4109 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4110 | |
| 4111 | /* It is useles to retrieve the stream, but this function |
| 4112 | * runs only in a stream context. |
| 4113 | */ |
| 4114 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4115 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4116 | s = appctx->htxn.s; |
| 4117 | |
| 4118 | /* Converts the third argument in a sample. */ |
| 4119 | hlua_lua2smp(L, 3, &smp); |
| 4120 | |
| 4121 | /* Store the sample in a variable. */ |
| 4122 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4123 | vars_set_by_name(name, len, &smp); |
| 4124 | return 0; |
| 4125 | } |
| 4126 | |
| 4127 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4128 | { |
| 4129 | struct hlua_appctx *appctx; |
| 4130 | struct stream *s; |
| 4131 | const char *name; |
| 4132 | size_t len; |
| 4133 | struct sample smp; |
| 4134 | |
| 4135 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4136 | |
| 4137 | /* It is useles to retrieve the stream, but this function |
| 4138 | * runs only in a stream context. |
| 4139 | */ |
| 4140 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4141 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4142 | s = appctx->htxn.s; |
| 4143 | |
| 4144 | /* Unset the variable. */ |
| 4145 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4146 | vars_unset_by_name(name, len, &smp); |
| 4147 | return 0; |
| 4148 | } |
| 4149 | |
| 4150 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4151 | { |
| 4152 | struct hlua_appctx *appctx; |
| 4153 | struct stream *s; |
| 4154 | const char *name; |
| 4155 | size_t len; |
| 4156 | struct sample smp; |
| 4157 | |
| 4158 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4159 | |
| 4160 | /* It is useles to retrieve the stream, but this function |
| 4161 | * runs only in a stream context. |
| 4162 | */ |
| 4163 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4164 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4165 | s = appctx->htxn.s; |
| 4166 | |
| 4167 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4168 | if (!vars_get_by_name(name, len, &smp)) { |
| 4169 | lua_pushnil(L); |
| 4170 | return 1; |
| 4171 | } |
| 4172 | |
| 4173 | return hlua_smp2lua(L, &smp); |
| 4174 | } |
| 4175 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4176 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4177 | { |
| 4178 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4179 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4180 | struct hlua *hlua; |
| 4181 | |
| 4182 | /* 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] | 4183 | if (!s->hlua) |
| 4184 | return 0; |
| 4185 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4186 | |
| 4187 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4188 | |
| 4189 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4190 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4191 | |
| 4192 | /* Get and store new value. */ |
| 4193 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4194 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4195 | |
| 4196 | return 0; |
| 4197 | } |
| 4198 | |
| 4199 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4200 | { |
| 4201 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4202 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4203 | struct hlua *hlua; |
| 4204 | |
| 4205 | /* 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] | 4206 | if (!s->hlua) { |
| 4207 | lua_pushnil(L); |
| 4208 | return 1; |
| 4209 | } |
| 4210 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4211 | |
| 4212 | /* Push configuration index in the stack. */ |
| 4213 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4214 | |
| 4215 | return 1; |
| 4216 | } |
| 4217 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4218 | /* If expected data not yet available, it returns a yield. This function |
| 4219 | * consumes the data in the buffer. It returns a string containing the |
| 4220 | * data. This string can be empty. |
| 4221 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4222 | __LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4223 | { |
| 4224 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4225 | struct stream_interface *si = appctx->appctx->owner; |
| 4226 | struct channel *req = si_oc(si); |
| 4227 | struct htx *htx; |
| 4228 | struct htx_blk *blk; |
| 4229 | size_t count; |
| 4230 | int stop = 0; |
| 4231 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4232 | htx = htx_from_buf(&req->buf); |
| 4233 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4234 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4235 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4236 | while (count && !stop && blk) { |
| 4237 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4238 | uint32_t sz = htx_get_blksz(blk); |
| 4239 | struct ist v; |
| 4240 | uint32_t vlen; |
| 4241 | char *nl; |
| 4242 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4243 | if (type == HTX_BLK_EOM) { |
| 4244 | stop = 1; |
| 4245 | break; |
| 4246 | } |
| 4247 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4248 | vlen = sz; |
| 4249 | if (vlen > count) { |
| 4250 | if (type != HTX_BLK_DATA) |
| 4251 | break; |
| 4252 | vlen = count; |
| 4253 | } |
| 4254 | |
| 4255 | switch (type) { |
| 4256 | case HTX_BLK_UNUSED: |
| 4257 | break; |
| 4258 | |
| 4259 | case HTX_BLK_DATA: |
| 4260 | v = htx_get_blk_value(htx, blk); |
| 4261 | v.len = vlen; |
| 4262 | nl = istchr(v, '\n'); |
| 4263 | if (nl != NULL) { |
| 4264 | stop = 1; |
| 4265 | vlen = nl - v.ptr + 1; |
| 4266 | } |
| 4267 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4268 | break; |
| 4269 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4270 | case HTX_BLK_TLR: |
| 4271 | case HTX_BLK_EOM: |
| 4272 | stop = 1; |
| 4273 | break; |
| 4274 | |
| 4275 | default: |
| 4276 | break; |
| 4277 | } |
| 4278 | |
| 4279 | co_set_data(req, co_data(req) - vlen); |
| 4280 | count -= vlen; |
| 4281 | if (sz == vlen) |
| 4282 | blk = htx_remove_blk(htx, blk); |
| 4283 | else { |
| 4284 | htx_cut_data_blk(htx, blk, vlen); |
| 4285 | break; |
| 4286 | } |
| 4287 | } |
| 4288 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4289 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4290 | if (!stop) { |
| 4291 | si_cant_get(si); |
| 4292 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0)); |
| 4293 | } |
| 4294 | |
| 4295 | /* return the result. */ |
| 4296 | luaL_pushresult(&appctx->b); |
| 4297 | return 1; |
| 4298 | } |
| 4299 | |
| 4300 | |
| 4301 | /* If expected data not yet available, it returns a yield. This function |
| 4302 | * consumes the data in the buffer. It returns a string containing the |
| 4303 | * data. This string can be empty. |
| 4304 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4305 | __LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4306 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4307 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4308 | struct stream_interface *si = appctx->appctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4309 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4310 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4311 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4312 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4313 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4314 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4315 | /* Check for the end of the data. */ |
| 4316 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4317 | luaL_pushresult(&appctx->b); |
| 4318 | return 1; |
| 4319 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4320 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4321 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4322 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4323 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4324 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4325 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4326 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4327 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4328 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4329 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4330 | /* End of data: commit the total strings and return. */ |
| 4331 | if (ret < 0) { |
| 4332 | luaL_pushresult(&appctx->b); |
| 4333 | return 1; |
| 4334 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4335 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4336 | /* Ensure that the block 2 length is usable. */ |
| 4337 | if (ret == 1) |
| 4338 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4339 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4340 | /* Copy the fisrt block caping to the length required. */ |
| 4341 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4342 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4343 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4344 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4345 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4346 | /* Copy the second block. */ |
| 4347 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4348 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4349 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4350 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4351 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4352 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4353 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4354 | luaL_pushresult(&appctx->b); |
| 4355 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4356 | } |
| 4357 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4358 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4359 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4360 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4361 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4362 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4363 | /* Initialise the string catenation. */ |
| 4364 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4365 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4366 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 4367 | return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0)); |
| 4368 | else |
| 4369 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4370 | } |
| 4371 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4372 | /* If expected data not yet available, it returns a yield. This function |
| 4373 | * consumes the data in the buffer. It returns a string containing the |
| 4374 | * data. This string can be empty. |
| 4375 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4376 | __LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4377 | { |
| 4378 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4379 | struct stream_interface *si = appctx->appctx->owner; |
| 4380 | struct channel *req = si_oc(si); |
| 4381 | struct htx *htx; |
| 4382 | struct htx_blk *blk; |
| 4383 | size_t count; |
| 4384 | int len; |
| 4385 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4386 | htx = htx_from_buf(&req->buf); |
| 4387 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4388 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4389 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4390 | while (count && len && blk) { |
| 4391 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4392 | uint32_t sz = htx_get_blksz(blk); |
| 4393 | struct ist v; |
| 4394 | uint32_t vlen; |
| 4395 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4396 | if (type == HTX_BLK_EOM) { |
| 4397 | len = 0; |
| 4398 | break; |
| 4399 | } |
| 4400 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4401 | vlen = sz; |
| 4402 | if (len > 0 && vlen > len) |
| 4403 | vlen = len; |
| 4404 | if (vlen > count) { |
| 4405 | if (type != HTX_BLK_DATA) |
| 4406 | break; |
| 4407 | vlen = count; |
| 4408 | } |
| 4409 | |
| 4410 | switch (type) { |
| 4411 | case HTX_BLK_UNUSED: |
| 4412 | break; |
| 4413 | |
| 4414 | case HTX_BLK_DATA: |
| 4415 | v = htx_get_blk_value(htx, blk); |
| 4416 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4417 | break; |
| 4418 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4419 | case HTX_BLK_TLR: |
| 4420 | case HTX_BLK_EOM: |
| 4421 | len = 0; |
| 4422 | break; |
| 4423 | |
| 4424 | default: |
| 4425 | break; |
| 4426 | } |
| 4427 | |
| 4428 | co_set_data(req, co_data(req) - vlen); |
| 4429 | count -= vlen; |
| 4430 | if (len > 0) |
| 4431 | len -= vlen; |
| 4432 | if (sz == vlen) |
| 4433 | blk = htx_remove_blk(htx, blk); |
| 4434 | else { |
| 4435 | htx_cut_data_blk(htx, blk, vlen); |
| 4436 | break; |
| 4437 | } |
| 4438 | } |
| 4439 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4440 | htx_to_buf(htx, &req->buf); |
| 4441 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4442 | /* If we are no other data available, yield waiting for new data. */ |
| 4443 | if (len) { |
| 4444 | if (len > 0) { |
| 4445 | lua_pushinteger(L, len); |
| 4446 | lua_replace(L, 2); |
| 4447 | } |
| 4448 | si_cant_get(si); |
| 4449 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0)); |
| 4450 | } |
| 4451 | |
| 4452 | /* return the result. */ |
| 4453 | luaL_pushresult(&appctx->b); |
| 4454 | return 1; |
| 4455 | } |
| 4456 | |
| 4457 | /* If expected data not yet available, it returns a yield. This function |
| 4458 | * consumes the data in the buffer. It returns a string containing the |
| 4459 | * data. This string can be empty. |
| 4460 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4461 | __LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4462 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4463 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4464 | struct stream_interface *si = appctx->appctx->owner; |
| 4465 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4466 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4467 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4468 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4469 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4470 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4471 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4472 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4473 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4474 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4475 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4476 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4477 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4478 | 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] | 4479 | } |
| 4480 | |
| 4481 | /* End of data: commit the total strings and return. */ |
| 4482 | if (ret < 0) { |
| 4483 | luaL_pushresult(&appctx->b); |
| 4484 | return 1; |
| 4485 | } |
| 4486 | |
| 4487 | /* Ensure that the block 2 length is usable. */ |
| 4488 | if (ret == 1) |
| 4489 | len2 = 0; |
| 4490 | |
| 4491 | /* Copy the fisrt block caping to the length required. */ |
| 4492 | if (len1 > len) |
| 4493 | len1 = len; |
| 4494 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4495 | len -= len1; |
| 4496 | |
| 4497 | /* Copy the second block. */ |
| 4498 | if (len2 > len) |
| 4499 | len2 = len; |
| 4500 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4501 | len -= len2; |
| 4502 | |
| 4503 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4504 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4505 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4506 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4507 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4508 | /* If we are no other data available, yield waiting for new data. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4509 | if (len > 0) { |
| 4510 | lua_pushinteger(L, len); |
| 4511 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4512 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4513 | 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] | 4514 | } |
| 4515 | |
| 4516 | /* return the result. */ |
| 4517 | luaL_pushresult(&appctx->b); |
| 4518 | return 1; |
| 4519 | } |
| 4520 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4521 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4522 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4523 | { |
| 4524 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4525 | int len = -1; |
| 4526 | |
| 4527 | /* Check arguments. */ |
| 4528 | if (lua_gettop(L) > 2) |
| 4529 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4530 | if (lua_gettop(L) >= 2) { |
| 4531 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4532 | lua_pop(L, 1); |
| 4533 | } |
| 4534 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4535 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4536 | /* HTX version */ |
| 4537 | lua_pushinteger(L, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4538 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4539 | /* Initialise the string catenation. */ |
| 4540 | luaL_buffinit(L, &appctx->b); |
| 4541 | |
| 4542 | return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0)); |
| 4543 | } |
| 4544 | else { |
| 4545 | /* Legacy HTTP version */ |
| 4546 | /* Check the required length */ |
| 4547 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4548 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4549 | lua_pushinteger(L, len); |
| 4550 | |
| 4551 | /* Initialise the string catenation. */ |
| 4552 | luaL_buffinit(L, &appctx->b); |
| 4553 | |
| 4554 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4555 | } |
| 4556 | } |
| 4557 | |
| 4558 | /* Append data in the output side of the buffer. This data is immediately |
| 4559 | * sent. The function returns the amount of data written. If the buffer |
| 4560 | * cannot contain the data, the function yields. The function returns -1 |
| 4561 | * if the channel is closed. |
| 4562 | */ |
| 4563 | __LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4564 | { |
| 4565 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4566 | struct stream_interface *si = appctx->appctx->owner; |
| 4567 | struct channel *res = si_ic(si); |
| 4568 | struct htx *htx = htx_from_buf(&res->buf); |
| 4569 | const char *data; |
| 4570 | size_t len; |
| 4571 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4572 | int max; |
| 4573 | |
Christopher Faulet | 4e96301 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4574 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4575 | if (!max) |
| 4576 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4577 | |
| 4578 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4579 | |
| 4580 | /* Get the max amount of data which can write as input in the channel. */ |
| 4581 | if (max > (len - l)) |
| 4582 | max = len - l; |
| 4583 | |
| 4584 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4585 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4586 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4587 | |
| 4588 | /* update counters. */ |
| 4589 | l += max; |
| 4590 | lua_pop(L, 1); |
| 4591 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4592 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4593 | /* If some data is not send, declares the situation to the |
| 4594 | * applet, and returns a yield. |
| 4595 | */ |
| 4596 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4597 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4598 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4599 | si_rx_room_blk(si); |
| 4600 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0)); |
| 4601 | } |
| 4602 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4603 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4604 | return 1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4605 | } |
| 4606 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4607 | /* Append data in the output side of the buffer. This data is immediately |
| 4608 | * sent. The function returns the amount of data written. If the buffer |
| 4609 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4610 | * if the channel is closed. |
| 4611 | */ |
| 4612 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4613 | { |
| 4614 | size_t len; |
| 4615 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4616 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4617 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4618 | struct stream_interface *si = appctx->appctx->owner; |
| 4619 | struct channel *chn = si_ic(si); |
| 4620 | int max; |
| 4621 | |
| 4622 | /* Get the max amount of data which can write as input in the channel. */ |
| 4623 | max = channel_recv_max(chn); |
| 4624 | if (max > (len - l)) |
| 4625 | max = len - l; |
| 4626 | |
| 4627 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4628 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4629 | |
| 4630 | /* update counters. */ |
| 4631 | l += max; |
| 4632 | lua_pop(L, 1); |
| 4633 | lua_pushinteger(L, l); |
| 4634 | |
| 4635 | /* If some data is not send, declares the situation to the |
| 4636 | * applet, and returns a yield. |
| 4637 | */ |
| 4638 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4639 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4640 | 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] | 4641 | } |
| 4642 | |
| 4643 | return 1; |
| 4644 | } |
| 4645 | |
| 4646 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4647 | * yield the LUA process, and resume it without checking the |
| 4648 | * input arguments. |
| 4649 | */ |
| 4650 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4651 | { |
| 4652 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4653 | |
| 4654 | /* We want to send some data. Headers must be sent. */ |
| 4655 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4656 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4657 | WILL_LJMP(lua_error(L)); |
| 4658 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4659 | |
| 4660 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4661 | /* HTX version */ |
| 4662 | /* This interger is used for followinf the amount of data sent. */ |
| 4663 | lua_pushinteger(L, 0); |
| 4664 | |
| 4665 | return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0)); |
| 4666 | } |
| 4667 | else { |
| 4668 | /* Legacy HTTP version */ |
| 4669 | size_t len; |
| 4670 | char hex[10]; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4671 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4672 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4673 | |
| 4674 | /* If transfer encoding chunked is selected, we surround the data |
| 4675 | * by chunk data. |
| 4676 | */ |
| 4677 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4678 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4679 | lua_pushfstring(L, "%s\r\n", hex); |
| 4680 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4681 | lua_pushstring(L, "\r\n"); |
| 4682 | lua_concat(L, 3); |
| 4683 | } |
| 4684 | |
| 4685 | /* This interger is used for followinf the amount of data sent. */ |
| 4686 | lua_pushinteger(L, 0); |
| 4687 | |
| 4688 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4689 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4690 | } |
| 4691 | |
| 4692 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4693 | { |
| 4694 | const char *name; |
| 4695 | int ret; |
| 4696 | |
| 4697 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4698 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4699 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4700 | |
| 4701 | /* Push in the stack the "response" entry. */ |
| 4702 | ret = lua_getfield(L, 1, "response"); |
| 4703 | if (ret != LUA_TTABLE) { |
| 4704 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4705 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4706 | WILL_LJMP(lua_error(L)); |
| 4707 | } |
| 4708 | |
| 4709 | /* check if the header is already registered if it is not |
| 4710 | * the case, register it. |
| 4711 | */ |
| 4712 | ret = lua_getfield(L, -1, name); |
| 4713 | if (ret == LUA_TNIL) { |
| 4714 | |
| 4715 | /* Entry not found. */ |
| 4716 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4717 | |
| 4718 | /* Insert the new header name in the array in the top of the stack. |
| 4719 | * It left the new array in the top of the stack. |
| 4720 | */ |
| 4721 | lua_newtable(L); |
| 4722 | lua_pushvalue(L, 2); |
| 4723 | lua_pushvalue(L, -2); |
| 4724 | lua_settable(L, -4); |
| 4725 | |
| 4726 | } else if (ret != LUA_TTABLE) { |
| 4727 | |
| 4728 | /* corruption error. */ |
| 4729 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4730 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4731 | WILL_LJMP(lua_error(L)); |
| 4732 | } |
| 4733 | |
| 4734 | /* Now the top od thestack is an array of values. We push |
| 4735 | * the header value as new entry. |
| 4736 | */ |
| 4737 | lua_pushvalue(L, 3); |
| 4738 | ret = lua_rawlen(L, -2); |
| 4739 | lua_rawseti(L, -2, ret + 1); |
| 4740 | lua_pushboolean(L, 1); |
| 4741 | return 1; |
| 4742 | } |
| 4743 | |
| 4744 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4745 | { |
| 4746 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4747 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4748 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4749 | |
| 4750 | if (status < 100 || status > 599) { |
| 4751 | lua_pushboolean(L, 0); |
| 4752 | return 1; |
| 4753 | } |
| 4754 | |
| 4755 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4756 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4757 | lua_pushboolean(L, 1); |
| 4758 | return 1; |
| 4759 | } |
| 4760 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4761 | |
| 4762 | __LJMP static int hlua_applet_htx_send_response(lua_State *L) |
| 4763 | { |
| 4764 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4765 | struct stream_interface *si = appctx->appctx->owner; |
| 4766 | struct channel *res = si_ic(si); |
| 4767 | struct htx *htx; |
| 4768 | struct htx_sl *sl; |
| 4769 | struct h1m h1m; |
| 4770 | const char *status, *reason; |
| 4771 | const char *name, *value; |
| 4772 | size_t nlen, vlen; |
| 4773 | unsigned int flags; |
| 4774 | |
| 4775 | /* Send the message at once. */ |
| 4776 | htx = htx_from_buf(&res->buf); |
| 4777 | h1m_init_res(&h1m); |
| 4778 | |
| 4779 | /* Use the same http version than the request. */ |
| 4780 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4781 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4782 | if (reason == NULL) |
| 4783 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4784 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4785 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4786 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4787 | } |
| 4788 | else { |
| 4789 | flags = HTX_SL_F_IS_RESP; |
| 4790 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4791 | } |
| 4792 | if (!sl) { |
| 4793 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4794 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4795 | WILL_LJMP(lua_error(L)); |
| 4796 | } |
| 4797 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4798 | |
| 4799 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4800 | lua_pushvalue(L, 0); |
| 4801 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4802 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4803 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4804 | WILL_LJMP(lua_error(L)); |
| 4805 | } |
| 4806 | |
| 4807 | /* Browse the list of headers. */ |
| 4808 | lua_pushnil(L); |
| 4809 | while(lua_next(L, -2) != 0) { |
| 4810 | /* We expect a string as -2. */ |
| 4811 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4812 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4813 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4814 | lua_typename(L, lua_type(L, -2))); |
| 4815 | WILL_LJMP(lua_error(L)); |
| 4816 | } |
| 4817 | name = lua_tolstring(L, -2, &nlen); |
| 4818 | |
| 4819 | /* We expect an array as -1. */ |
| 4820 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4821 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4822 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4823 | name, |
| 4824 | lua_typename(L, lua_type(L, -1))); |
| 4825 | WILL_LJMP(lua_error(L)); |
| 4826 | } |
| 4827 | |
| 4828 | /* Browse the table who is on the top of the stack. */ |
| 4829 | lua_pushnil(L); |
| 4830 | while(lua_next(L, -2) != 0) { |
| 4831 | int id; |
| 4832 | |
| 4833 | /* We expect a number as -2. */ |
| 4834 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4835 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4836 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4837 | name, |
| 4838 | lua_typename(L, lua_type(L, -2))); |
| 4839 | WILL_LJMP(lua_error(L)); |
| 4840 | } |
| 4841 | id = lua_tointeger(L, -2); |
| 4842 | |
| 4843 | /* We expect a string as -2. */ |
| 4844 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4845 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4846 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4847 | name, id, |
| 4848 | lua_typename(L, lua_type(L, -1))); |
| 4849 | WILL_LJMP(lua_error(L)); |
| 4850 | } |
| 4851 | value = lua_tolstring(L, -1, &vlen); |
| 4852 | |
| 4853 | /* Simple Protocol checks. */ |
| 4854 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
| 4855 | h1_parse_xfer_enc_header(&h1m, ist2(name, nlen)); |
| 4856 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4857 | struct ist v = ist2(value, vlen); |
| 4858 | int ret; |
| 4859 | |
| 4860 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4861 | if (ret < 0) { |
| 4862 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4863 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4864 | name); |
| 4865 | WILL_LJMP(lua_error(L)); |
| 4866 | } |
| 4867 | else if (ret == 0) |
| 4868 | goto next; /* Skip it */ |
| 4869 | } |
| 4870 | |
| 4871 | /* Add a new header */ |
| 4872 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4873 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4874 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4875 | name); |
| 4876 | WILL_LJMP(lua_error(L)); |
| 4877 | } |
| 4878 | next: |
| 4879 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4880 | lua_pop(L, 1); |
| 4881 | } |
| 4882 | |
| 4883 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4884 | lua_pop(L, 1); |
| 4885 | } |
| 4886 | |
| 4887 | if (h1m.flags & H1_MF_CHNK) |
| 4888 | h1m.flags &= ~H1_MF_CLEN; |
| 4889 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4890 | h1m.flags |= H1_MF_XFER_LEN; |
| 4891 | |
| 4892 | /* Uset HTX start-line flags */ |
| 4893 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4894 | flags |= HTX_SL_F_XFER_ENC; |
| 4895 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4896 | flags |= HTX_SL_F_XFER_LEN; |
| 4897 | if (h1m.flags & H1_MF_CHNK) |
| 4898 | flags |= HTX_SL_F_CHNK; |
| 4899 | else if (h1m.flags & H1_MF_CLEN) |
| 4900 | flags |= HTX_SL_F_CLEN; |
| 4901 | if (h1m.body_len == 0) |
| 4902 | flags |= HTX_SL_F_BODYLESS; |
| 4903 | } |
| 4904 | sl->flags |= flags; |
| 4905 | |
| 4906 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4907 | * and the status code implies the presence of a message body, we must |
| 4908 | * announce a transfer encoding chunked. This is required by haproxy |
| 4909 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4910 | * chunked itslef, don't do anything. |
| 4911 | */ |
| 4912 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4913 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4914 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4915 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4916 | /* Add a new header */ |
| 4917 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4918 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4919 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4920 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4921 | WILL_LJMP(lua_error(L)); |
| 4922 | } |
| 4923 | } |
| 4924 | |
| 4925 | /* Finalize headers. */ |
| 4926 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4927 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4928 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4929 | WILL_LJMP(lua_error(L)); |
| 4930 | } |
| 4931 | |
| 4932 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4933 | b_reset(&res->buf); |
| 4934 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4935 | WILL_LJMP(lua_error(L)); |
| 4936 | } |
| 4937 | |
| 4938 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4939 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4940 | |
| 4941 | /* Headers sent, set the flag. */ |
| 4942 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4943 | return 0; |
| 4944 | |
| 4945 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4946 | /* We will build the status line and the headers of the HTTP response. |
| 4947 | * We will try send at once if its not possible, we give back the hand |
| 4948 | * waiting for more room. |
| 4949 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4950 | __LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4951 | { |
| 4952 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4953 | struct stream_interface *si = appctx->appctx->owner; |
| 4954 | struct channel *res = si_ic(si); |
| 4955 | |
| 4956 | if (co_data(res)) { |
| 4957 | si_rx_room_blk(si); |
| 4958 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0)); |
| 4959 | } |
| 4960 | return MAY_LJMP(hlua_applet_htx_send_response(L)); |
| 4961 | } |
| 4962 | |
| 4963 | |
| 4964 | __LJMP static int hlua_applet_htx_start_response(lua_State *L) |
| 4965 | { |
| 4966 | return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0)); |
| 4967 | } |
| 4968 | |
| 4969 | /* We will build the status line and the headers of the HTTP response. |
| 4970 | * We will try send at once if its not possible, we give back the hand |
| 4971 | * waiting for more room. |
| 4972 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4973 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4974 | { |
| 4975 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4976 | struct stream_interface *si = appctx->appctx->owner; |
| 4977 | struct channel *chn = si_ic(si); |
| 4978 | int ret; |
| 4979 | size_t len; |
| 4980 | const char *msg; |
| 4981 | |
| 4982 | /* Get the message as the first argument on the stack. */ |
| 4983 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4984 | |
| 4985 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4986 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4987 | |
| 4988 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4989 | * big for the buffers. |
| 4990 | */ |
| 4991 | if (ret == -2 || ret == -3) { |
| 4992 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4993 | WILL_LJMP(lua_error(L)); |
| 4994 | } |
| 4995 | |
| 4996 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4997 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4998 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4999 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5000 | } |
| 5001 | |
| 5002 | /* Headers sent, set the flag. */ |
| 5003 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 5004 | return 0; |
| 5005 | } |
| 5006 | |
| 5007 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 5008 | { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5009 | struct buffer *tmp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5010 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5011 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5012 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5013 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5014 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5015 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5016 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5017 | int hdr_chunked = 0; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5018 | const char *reason; |
| 5019 | |
| 5020 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 5021 | return MAY_LJMP(hlua_applet_htx_start_response(L)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5022 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5023 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5024 | if (reason == NULL) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 5025 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5026 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5027 | tmp = get_trash_chunk(); |
| 5028 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5029 | /* Use the same http version than the request. */ |
| 5030 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 5031 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5032 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5033 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5034 | |
| 5035 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5036 | lua_pushvalue(L, 0); |
| 5037 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5038 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 5039 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 5040 | WILL_LJMP(lua_error(L)); |
| 5041 | } |
| 5042 | |
| 5043 | /* Browse the list of headers. */ |
| 5044 | lua_pushnil(L); |
| 5045 | while(lua_next(L, -2) != 0) { |
| 5046 | |
| 5047 | /* We expect a string as -2. */ |
| 5048 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5049 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 5050 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5051 | lua_typename(L, lua_type(L, -2))); |
| 5052 | WILL_LJMP(lua_error(L)); |
| 5053 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5054 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5055 | |
| 5056 | /* We expect an array as -1. */ |
| 5057 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5058 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 5059 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5060 | name, |
| 5061 | lua_typename(L, lua_type(L, -1))); |
| 5062 | WILL_LJMP(lua_error(L)); |
| 5063 | } |
| 5064 | |
| 5065 | /* Browse the table who is on the top of the stack. */ |
| 5066 | lua_pushnil(L); |
| 5067 | while(lua_next(L, -2) != 0) { |
| 5068 | |
| 5069 | /* We expect a number as -2. */ |
| 5070 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5071 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 5072 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5073 | name, |
| 5074 | lua_typename(L, lua_type(L, -2))); |
| 5075 | WILL_LJMP(lua_error(L)); |
| 5076 | } |
| 5077 | id = lua_tointeger(L, -2); |
| 5078 | |
| 5079 | /* We expect a string as -2. */ |
| 5080 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5081 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 5082 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5083 | name, id, |
| 5084 | lua_typename(L, lua_type(L, -1))); |
| 5085 | WILL_LJMP(lua_error(L)); |
| 5086 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5087 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5088 | |
| 5089 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5090 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 5091 | memcpy(tmp->area + tmp->data, name, name_len); |
| 5092 | tmp->data += name_len; |
| 5093 | tmp->area[tmp->data++] = ':'; |
| 5094 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5095 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5096 | memcpy(tmp->area + tmp->data, value, |
| 5097 | value_len); |
| 5098 | tmp->data += value_len; |
| 5099 | tmp->area[tmp->data++] = '\r'; |
| 5100 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5101 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5102 | |
| 5103 | /* Protocol checks. */ |
| 5104 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5105 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5106 | * is done without control. If it contains a bad value, |
| 5107 | * the content-length remains negative so that we can |
| 5108 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5109 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5110 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5111 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5112 | |
| 5113 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5114 | if (name_len == 17 && value_len == 7 && |
| 5115 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5116 | strcasecmp("chunked", value) == 0) |
| 5117 | hdr_chunked = 1; |
| 5118 | |
| 5119 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5120 | lua_pop(L, 1); |
| 5121 | } |
| 5122 | |
| 5123 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5124 | lua_pop(L, 1); |
| 5125 | } |
| 5126 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5127 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 5128 | * and the status code implies the presence of a message body, we must |
| 5129 | * announce a transfer encoding chunked. This is required by haproxy |
| 5130 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 5131 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5132 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5133 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5134 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 5135 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5136 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5137 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5138 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 5139 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 5140 | } |
| 5141 | |
| 5142 | /* Finalize headers. */ |
| 5143 | chunk_appendf(tmp, "\r\n"); |
| 5144 | |
| 5145 | /* Remove the last entry and the array of headers */ |
| 5146 | lua_pop(L, 2); |
| 5147 | |
| 5148 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5149 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5150 | |
| 5151 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 5152 | } |
| 5153 | |
| 5154 | /* |
| 5155 | * |
| 5156 | * |
| 5157 | * Class HTTP |
| 5158 | * |
| 5159 | * |
| 5160 | */ |
| 5161 | |
| 5162 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5163 | * a class stream, otherwise it throws an error. |
| 5164 | */ |
| 5165 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 5166 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5167 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5168 | } |
| 5169 | |
| 5170 | /* This function creates and push in the stack a HTTP object |
| 5171 | * according with a current TXN. |
| 5172 | */ |
| 5173 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5174 | { |
| 5175 | struct hlua_txn *htxn; |
| 5176 | |
| 5177 | /* Check stack size. */ |
| 5178 | if (!lua_checkstack(L, 3)) |
| 5179 | return 0; |
| 5180 | |
| 5181 | /* Create the object: obj[0] = userdata. |
| 5182 | * Note that the base of the Converters object is the |
| 5183 | * same than the TXN object. |
| 5184 | */ |
| 5185 | lua_newtable(L); |
| 5186 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5187 | lua_rawseti(L, -2, 0); |
| 5188 | |
| 5189 | htxn->s = txn->s; |
| 5190 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5191 | htxn->dir = txn->dir; |
| 5192 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5193 | |
| 5194 | /* Pop a class stream metatable and affect it to the table. */ |
| 5195 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5196 | lua_setmetatable(L, -2); |
| 5197 | |
| 5198 | return 1; |
| 5199 | } |
| 5200 | |
| 5201 | /* This function creates ans returns an array of HTTP headers. |
| 5202 | * This function does not fails. It is used as wrapper with the |
| 5203 | * 2 following functions. |
| 5204 | */ |
| 5205 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5206 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5207 | /* Create the table. */ |
| 5208 | lua_newtable(L); |
| 5209 | |
| 5210 | if (!htxn->s->txn) |
| 5211 | return 1; |
| 5212 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5213 | if (IS_HTX_STRM(htxn->s)) { |
| 5214 | /* HTX version */ |
| 5215 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5216 | int32_t pos; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5217 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5218 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5219 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5220 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5221 | struct ist n, v; |
| 5222 | int len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5223 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5224 | if (type == HTX_BLK_HDR) { |
| 5225 | n = htx_get_blk_name(htx,blk); |
| 5226 | v = htx_get_blk_value(htx, blk); |
| 5227 | } |
| 5228 | else if (type == HTX_BLK_EOH) |
| 5229 | break; |
| 5230 | else |
| 5231 | continue; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5232 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5233 | /* Check for existing entry: |
| 5234 | * assume that the table is on the top of the stack, and |
| 5235 | * push the key in the stack, the function lua_gettable() |
| 5236 | * perform the lookup. |
| 5237 | */ |
| 5238 | lua_pushlstring(L, n.ptr, n.len); |
| 5239 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5240 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5241 | switch (lua_type(L, -1)) { |
| 5242 | case LUA_TNIL: |
| 5243 | /* Table not found, create it. */ |
| 5244 | lua_pop(L, 1); /* remove the nil value. */ |
| 5245 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5246 | lua_newtable(L); /* create and push empty table. */ |
| 5247 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5248 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5249 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5250 | break; |
| 5251 | |
| 5252 | case LUA_TTABLE: |
| 5253 | /* Entry found: push the value in the table. */ |
| 5254 | len = lua_rawlen(L, -1); |
| 5255 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5256 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5257 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5258 | break; |
| 5259 | |
| 5260 | default: |
| 5261 | /* Other cases are errors. */ |
| 5262 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5263 | WILL_LJMP(lua_error(L)); |
| 5264 | } |
| 5265 | } |
| 5266 | } |
| 5267 | else { |
| 5268 | /* Legacy HTTP version */ |
| 5269 | const char *cur_ptr, *cur_next, *p; |
| 5270 | int old_idx, cur_idx; |
| 5271 | struct hdr_idx_elem *cur_hdr; |
| 5272 | const char *hn, *hv; |
| 5273 | int hnl, hvl; |
| 5274 | const char *in; |
| 5275 | char *out; |
| 5276 | int len; |
| 5277 | |
| 5278 | /* Build array of headers. */ |
| 5279 | old_idx = 0; |
| 5280 | cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 5281 | |
| 5282 | while (1) { |
| 5283 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 5284 | if (!cur_idx) |
| 5285 | break; |
| 5286 | old_idx = cur_idx; |
| 5287 | |
| 5288 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 5289 | cur_ptr = cur_next; |
| 5290 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 5291 | |
| 5292 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 5293 | * and the next header starts at cur_next. We'll check |
| 5294 | * this header in the list as well as against the default |
| 5295 | * rule. |
| 5296 | */ |
| 5297 | |
| 5298 | /* look for ': *'. */ |
| 5299 | hn = cur_ptr; |
| 5300 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 5301 | if (p >= cur_ptr+cur_hdr->len) |
| 5302 | continue; |
| 5303 | hnl = p - hn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5304 | p++; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5305 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 5306 | p++; |
| 5307 | if (p >= cur_ptr+cur_hdr->len) |
| 5308 | continue; |
| 5309 | hv = p; |
| 5310 | hvl = cur_ptr+cur_hdr->len-p; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5311 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5312 | /* Lowercase the key. Don't check the size of trash, it have |
| 5313 | * the size of one buffer and the input data contains in one |
| 5314 | * buffer. |
| 5315 | */ |
| 5316 | out = trash.area; |
| 5317 | for (in=hn; in<hn+hnl; in++, out++) |
| 5318 | *out = tolower(*in); |
| 5319 | *out = '\0'; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5320 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5321 | /* Check for existing entry: |
| 5322 | * assume that the table is on the top of the stack, and |
| 5323 | * push the key in the stack, the function lua_gettable() |
| 5324 | * perform the lookup. |
| 5325 | */ |
| 5326 | lua_pushlstring(L, trash.area, hnl); |
| 5327 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5328 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5329 | switch (lua_type(L, -1)) { |
| 5330 | case LUA_TNIL: |
| 5331 | /* Table not found, create it. */ |
| 5332 | lua_pop(L, 1); /* remove the nil value. */ |
| 5333 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
| 5334 | lua_newtable(L); /* create and push empty table. */ |
| 5335 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5336 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5337 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5338 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5339 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5340 | case LUA_TTABLE: |
| 5341 | /* Entry found: push the value in the table. */ |
| 5342 | len = lua_rawlen(L, -1); |
| 5343 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5344 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5345 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5346 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5347 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5348 | default: |
| 5349 | /* Other cases are errors. */ |
| 5350 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5351 | WILL_LJMP(lua_error(L)); |
| 5352 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5353 | } |
| 5354 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5355 | return 1; |
| 5356 | } |
| 5357 | |
| 5358 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5359 | { |
| 5360 | struct hlua_txn *htxn; |
| 5361 | |
| 5362 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5363 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5364 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5365 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5366 | WILL_LJMP(lua_error(L)); |
| 5367 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5368 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 5369 | } |
| 5370 | |
| 5371 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5372 | { |
| 5373 | struct hlua_txn *htxn; |
| 5374 | |
| 5375 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5376 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5377 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5378 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5379 | WILL_LJMP(lua_error(L)); |
| 5380 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5381 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 5382 | } |
| 5383 | |
| 5384 | /* This function replace full header, or just a value in |
| 5385 | * the request or in the response. It is a wrapper fir the |
| 5386 | * 4 following functions. |
| 5387 | */ |
| 5388 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 5389 | struct http_msg *msg, int action) |
| 5390 | { |
| 5391 | size_t name_len; |
| 5392 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5393 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5394 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5395 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5396 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5397 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5398 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5399 | |
Christopher Faulet | 5a8549e | 2019-06-17 13:36:06 +0200 | [diff] [blame] | 5400 | if (IS_HTX_STRM(htxn->s)) { |
| 5401 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5402 | |
| 5403 | htx_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action); |
| 5404 | } |
| 5405 | else |
| 5406 | http_transform_header_str(htxn->s, msg, name, name_len, value, re, action); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5407 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5408 | return 0; |
| 5409 | } |
| 5410 | |
| 5411 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5412 | { |
| 5413 | struct hlua_txn *htxn; |
| 5414 | |
| 5415 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5416 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5417 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5418 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5419 | WILL_LJMP(lua_error(L)); |
| 5420 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5421 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 5422 | } |
| 5423 | |
| 5424 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5425 | { |
| 5426 | struct hlua_txn *htxn; |
| 5427 | |
| 5428 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5429 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5430 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5431 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5432 | WILL_LJMP(lua_error(L)); |
| 5433 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5434 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 5435 | } |
| 5436 | |
| 5437 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5438 | { |
| 5439 | struct hlua_txn *htxn; |
| 5440 | |
| 5441 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5442 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5443 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5444 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5445 | WILL_LJMP(lua_error(L)); |
| 5446 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5447 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 5448 | } |
| 5449 | |
| 5450 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5451 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5452 | struct hlua_txn *htxn; |
| 5453 | |
| 5454 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5455 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5456 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5457 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5458 | WILL_LJMP(lua_error(L)); |
| 5459 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 5460 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5461 | } |
| 5462 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5463 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5464 | * It is a wrapper for the 2 following functions. |
| 5465 | */ |
| 5466 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5467 | { |
| 5468 | size_t len; |
| 5469 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5470 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5471 | if (IS_HTX_STRM(htxn->s)) { |
| 5472 | /* HTX version */ |
| 5473 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5474 | struct http_hdr_ctx ctx; |
| 5475 | |
| 5476 | ctx.blk = NULL; |
| 5477 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5478 | http_remove_header(htx, &ctx); |
| 5479 | } |
| 5480 | else { |
| 5481 | /* Legacy HTTP version */ |
| 5482 | struct hdr_ctx ctx; |
| 5483 | struct http_txn *txn = htxn->s->txn; |
| 5484 | |
| 5485 | ctx.idx = 0; |
| 5486 | while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) |
| 5487 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5488 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5489 | return 0; |
| 5490 | } |
| 5491 | |
| 5492 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5493 | { |
| 5494 | struct hlua_txn *htxn; |
| 5495 | |
| 5496 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5497 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5498 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5499 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5500 | WILL_LJMP(lua_error(L)); |
| 5501 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5502 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5503 | } |
| 5504 | |
| 5505 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5506 | { |
| 5507 | struct hlua_txn *htxn; |
| 5508 | |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5509 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5510 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5511 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5512 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5513 | WILL_LJMP(lua_error(L)); |
| 5514 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5515 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5516 | } |
| 5517 | |
| 5518 | /* This function adds an header. It is a wrapper used by |
| 5519 | * the 2 following functions. |
| 5520 | */ |
| 5521 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5522 | { |
| 5523 | size_t name_len; |
| 5524 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5525 | size_t value_len; |
| 5526 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 5527 | char *p; |
| 5528 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5529 | if (IS_HTX_STRM(htxn->s)) { |
| 5530 | /* HTX version */ |
| 5531 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5532 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5533 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5534 | ist2(value, value_len))); |
| 5535 | } |
| 5536 | else { |
| 5537 | /* Legacy HTTP version */ |
| 5538 | /* Check length. */ |
| 5539 | trash.data = value_len + name_len + 2; |
| 5540 | if (trash.data > trash.size) |
| 5541 | return 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5542 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5543 | /* Creates the header string. */ |
| 5544 | p = trash.area; |
| 5545 | memcpy(p, name, name_len); |
| 5546 | p += name_len; |
| 5547 | *p = ':'; |
| 5548 | p++; |
| 5549 | *p = ' '; |
| 5550 | p++; |
| 5551 | memcpy(p, value, value_len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5552 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5553 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
| 5554 | trash.area, trash.data) != 0); |
| 5555 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5556 | return 0; |
| 5557 | } |
| 5558 | |
| 5559 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5560 | { |
| 5561 | struct hlua_txn *htxn; |
| 5562 | |
| 5563 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5564 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5565 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5566 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5567 | WILL_LJMP(lua_error(L)); |
| 5568 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5569 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5570 | } |
| 5571 | |
| 5572 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5573 | { |
| 5574 | struct hlua_txn *htxn; |
| 5575 | |
| 5576 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5577 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5578 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5579 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5580 | WILL_LJMP(lua_error(L)); |
| 5581 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5582 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5583 | } |
| 5584 | |
| 5585 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5586 | { |
| 5587 | struct hlua_txn *htxn; |
| 5588 | |
| 5589 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5590 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5591 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5592 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5593 | WILL_LJMP(lua_error(L)); |
| 5594 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5595 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 5596 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5600 | { |
| 5601 | struct hlua_txn *htxn; |
| 5602 | |
| 5603 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5604 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5605 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5606 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5607 | WILL_LJMP(lua_error(L)); |
| 5608 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5609 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 5610 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5611 | } |
| 5612 | |
| 5613 | /* This function set the method. */ |
| 5614 | static int hlua_http_req_set_meth(lua_State *L) |
| 5615 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5616 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5617 | size_t name_len; |
| 5618 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5619 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5620 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5621 | WILL_LJMP(lua_error(L)); |
| 5622 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5623 | lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5624 | return 1; |
| 5625 | } |
| 5626 | |
| 5627 | /* This function set the method. */ |
| 5628 | static int hlua_http_req_set_path(lua_State *L) |
| 5629 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5630 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5631 | size_t name_len; |
| 5632 | 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] | 5633 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5634 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5635 | WILL_LJMP(lua_error(L)); |
| 5636 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5637 | lua_pushboolean(L, http_replace_req_line(1, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5638 | return 1; |
| 5639 | } |
| 5640 | |
| 5641 | /* This function set the query-string. */ |
| 5642 | static int hlua_http_req_set_query(lua_State *L) |
| 5643 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5644 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5645 | size_t name_len; |
| 5646 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5647 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5648 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5649 | WILL_LJMP(lua_error(L)); |
| 5650 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5651 | /* Check length. */ |
| 5652 | if (name_len > trash.size - 1) { |
| 5653 | lua_pushboolean(L, 0); |
| 5654 | return 1; |
| 5655 | } |
| 5656 | |
| 5657 | /* Add the mark question as prefix. */ |
| 5658 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5659 | trash.area[trash.data++] = '?'; |
| 5660 | memcpy(trash.area + trash.data, name, name_len); |
| 5661 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5662 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5663 | lua_pushboolean(L, |
| 5664 | http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5665 | return 1; |
| 5666 | } |
| 5667 | |
| 5668 | /* This function set the uri. */ |
| 5669 | static int hlua_http_req_set_uri(lua_State *L) |
| 5670 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5671 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5672 | size_t name_len; |
| 5673 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5674 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5675 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5676 | WILL_LJMP(lua_error(L)); |
| 5677 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5678 | lua_pushboolean(L, http_replace_req_line(3, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5679 | return 1; |
| 5680 | } |
| 5681 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5682 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5683 | static int hlua_http_res_set_status(lua_State *L) |
| 5684 | { |
| 5685 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5686 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5687 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5688 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5689 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5690 | WILL_LJMP(lua_error(L)); |
| 5691 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5692 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5693 | return 0; |
| 5694 | } |
| 5695 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5696 | /* |
| 5697 | * |
| 5698 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5699 | * Class TXN |
| 5700 | * |
| 5701 | * |
| 5702 | */ |
| 5703 | |
| 5704 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5705 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5706 | */ |
| 5707 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5708 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5709 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5710 | } |
| 5711 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5712 | __LJMP static int hlua_set_var(lua_State *L) |
| 5713 | { |
| 5714 | struct hlua_txn *htxn; |
| 5715 | const char *name; |
| 5716 | size_t len; |
| 5717 | struct sample smp; |
| 5718 | |
| 5719 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5720 | |
| 5721 | /* It is useles to retrieve the stream, but this function |
| 5722 | * runs only in a stream context. |
| 5723 | */ |
| 5724 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5725 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5726 | |
| 5727 | /* Converts the third argument in a sample. */ |
| 5728 | hlua_lua2smp(L, 3, &smp); |
| 5729 | |
| 5730 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5731 | 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] | 5732 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5733 | return 0; |
| 5734 | } |
| 5735 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5736 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5737 | { |
| 5738 | struct hlua_txn *htxn; |
| 5739 | const char *name; |
| 5740 | size_t len; |
| 5741 | struct sample smp; |
| 5742 | |
| 5743 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5744 | |
| 5745 | /* It is useles to retrieve the stream, but this function |
| 5746 | * runs only in a stream context. |
| 5747 | */ |
| 5748 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5749 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5750 | |
| 5751 | /* Unset the variable. */ |
| 5752 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5753 | vars_unset_by_name(name, len, &smp); |
| 5754 | return 0; |
| 5755 | } |
| 5756 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5757 | __LJMP static int hlua_get_var(lua_State *L) |
| 5758 | { |
| 5759 | struct hlua_txn *htxn; |
| 5760 | const char *name; |
| 5761 | size_t len; |
| 5762 | struct sample smp; |
| 5763 | |
| 5764 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5765 | |
| 5766 | /* It is useles to retrieve the stream, but this function |
| 5767 | * runs only in a stream context. |
| 5768 | */ |
| 5769 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5770 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5771 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5772 | 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] | 5773 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5774 | lua_pushnil(L); |
| 5775 | return 1; |
| 5776 | } |
| 5777 | |
| 5778 | return hlua_smp2lua(L, &smp); |
| 5779 | } |
| 5780 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5781 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5782 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5783 | struct hlua *hlua; |
| 5784 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5785 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5786 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5787 | /* It is useles to retrieve the stream, but this function |
| 5788 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5789 | */ |
| 5790 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5791 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5792 | |
| 5793 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5794 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5795 | |
| 5796 | /* Get and store new value. */ |
| 5797 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5798 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5799 | |
| 5800 | return 0; |
| 5801 | } |
| 5802 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5803 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5804 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5805 | struct hlua *hlua; |
| 5806 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5807 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5808 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5809 | /* It is useles to retrieve the stream, but this function |
| 5810 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5811 | */ |
| 5812 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5813 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5814 | |
| 5815 | /* Push configuration index in the stack. */ |
| 5816 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5817 | |
| 5818 | return 1; |
| 5819 | } |
| 5820 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5821 | /* Create stack entry containing a class TXN. This function |
| 5822 | * return 0 if the stack does not contains free slots, |
| 5823 | * otherwise it returns 1. |
| 5824 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5825 | 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] | 5826 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5827 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5828 | |
| 5829 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5830 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5831 | return 0; |
| 5832 | |
| 5833 | /* NOTE: The allocation never fails. The failure |
| 5834 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5835 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5836 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5837 | /* Create the object: obj[0] = userdata. */ |
| 5838 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5839 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5840 | lua_rawseti(L, -2, 0); |
| 5841 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5842 | htxn->s = s; |
| 5843 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5844 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5845 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5846 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5847 | /* Create the "f" field that contains a list of fetches. */ |
| 5848 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5849 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5850 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5851 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5852 | |
| 5853 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5854 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5855 | 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] | 5856 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5857 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5858 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5859 | /* Create the "c" field that contains a list of converters. */ |
| 5860 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5861 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5862 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5863 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5864 | |
| 5865 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5866 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5867 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5868 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5869 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5870 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5871 | /* Create the "req" field that contains the request channel object. */ |
| 5872 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5873 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5874 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5875 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5876 | |
| 5877 | /* Create the "res" field that contains the response channel object. */ |
| 5878 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5879 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5880 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5881 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5882 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5883 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5884 | lua_pushstring(L, "http"); |
| 5885 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5886 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5887 | return 0; |
| 5888 | } |
| 5889 | else |
| 5890 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5891 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5892 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5893 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5894 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5895 | lua_setmetatable(L, -2); |
| 5896 | |
| 5897 | return 1; |
| 5898 | } |
| 5899 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5900 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5901 | { |
| 5902 | const char *msg; |
| 5903 | struct hlua_txn *htxn; |
| 5904 | |
| 5905 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5906 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5907 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5908 | |
| 5909 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5910 | return 0; |
| 5911 | } |
| 5912 | |
| 5913 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5914 | { |
| 5915 | int level; |
| 5916 | const char *msg; |
| 5917 | struct hlua_txn *htxn; |
| 5918 | |
| 5919 | MAY_LJMP(check_args(L, 3, "log")); |
| 5920 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5921 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5922 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5923 | |
| 5924 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5925 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5926 | |
| 5927 | hlua_sendlog(htxn->s->be, level, msg); |
| 5928 | return 0; |
| 5929 | } |
| 5930 | |
| 5931 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5932 | { |
| 5933 | const char *msg; |
| 5934 | struct hlua_txn *htxn; |
| 5935 | |
| 5936 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5937 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5938 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5939 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5940 | return 0; |
| 5941 | } |
| 5942 | |
| 5943 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5944 | { |
| 5945 | const char *msg; |
| 5946 | struct hlua_txn *htxn; |
| 5947 | |
| 5948 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5949 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5950 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5951 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5952 | return 0; |
| 5953 | } |
| 5954 | |
| 5955 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5956 | { |
| 5957 | const char *msg; |
| 5958 | struct hlua_txn *htxn; |
| 5959 | |
| 5960 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5961 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5962 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5963 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5964 | return 0; |
| 5965 | } |
| 5966 | |
| 5967 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5968 | { |
| 5969 | const char *msg; |
| 5970 | struct hlua_txn *htxn; |
| 5971 | |
| 5972 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5973 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5974 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5975 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5976 | return 0; |
| 5977 | } |
| 5978 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5979 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5980 | { |
| 5981 | struct hlua_txn *htxn; |
| 5982 | int ll; |
| 5983 | |
| 5984 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5985 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5986 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5987 | |
| 5988 | if (ll < 0 || ll > 7) |
| 5989 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5990 | |
| 5991 | htxn->s->logs.level = ll; |
| 5992 | return 0; |
| 5993 | } |
| 5994 | |
| 5995 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5996 | { |
| 5997 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5998 | int tos; |
| 5999 | |
| 6000 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 6001 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6002 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6003 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 6004 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6005 | return 0; |
| 6006 | } |
| 6007 | |
| 6008 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 6009 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6010 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6011 | int mark; |
| 6012 | |
| 6013 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 6014 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6015 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6016 | |
Lukas Tribus | b59291a | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 6017 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6018 | return 0; |
| 6019 | } |
| 6020 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 6021 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 6022 | { |
| 6023 | struct hlua_txn *htxn; |
| 6024 | |
| 6025 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 6026 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6027 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6028 | return 0; |
| 6029 | } |
| 6030 | |
| 6031 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 6032 | { |
| 6033 | struct hlua_txn *htxn; |
| 6034 | |
| 6035 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 6036 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6037 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6038 | return 0; |
| 6039 | } |
| 6040 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6041 | /* This function is an Lua binding that send pending data |
| 6042 | * to the client, and close the stream interface. |
| 6043 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6044 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6045 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6046 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6047 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6048 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6049 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6050 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6051 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6052 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6053 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6054 | /* If the flags NOTERM is set, we cannot terminate the http |
| 6055 | * session, so we just end the execution of the current |
| 6056 | * lua code. |
| 6057 | */ |
| 6058 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 6059 | WILL_LJMP(hlua_done(L)); |
| 6060 | return 0; |
| 6061 | } |
| 6062 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6063 | ic = &htxn->s->req; |
| 6064 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6065 | |
Christopher Faulet | 72c6927 | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6066 | if (IS_HTX_STRM(htxn->s)) { |
| 6067 | htxn->s->txn->status = 0; |
| 6068 | http_reply_and_close(htxn->s, 0, NULL); |
| 6069 | ic->analysers &= AN_REQ_FLT_END; |
| 6070 | oc->analysers &= AN_RES_FLT_END; |
| 6071 | } |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6072 | else { |
| 6073 | if (htxn->s->txn) { |
| 6074 | /* HTTP mode, let's stay in sync with the stream */ |
| 6075 | b_del(&ic->buf, htxn->s->txn->req.sov); |
| 6076 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 6077 | htxn->s->txn->req.sov = 0; |
| 6078 | |
| 6079 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 6080 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 6081 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 6082 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6083 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6084 | /* Note that if we want to support keep-alive, we need |
| 6085 | * to bypass the close/shutr_now calls below, but that |
| 6086 | * may only be done if the HTTP request was already |
| 6087 | * processed and the connection header is known (ie |
| 6088 | * not during TCP rules). |
| 6089 | */ |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6090 | } |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6091 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6092 | channel_auto_read(ic); |
| 6093 | channel_abort(ic); |
| 6094 | channel_auto_close(ic); |
| 6095 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6096 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6097 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 6098 | channel_auto_read(oc); |
| 6099 | channel_auto_close(oc); |
| 6100 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6101 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6102 | ic->analysers = 0; |
| 6103 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6104 | |
Christopher Faulet | 72c6927 | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6105 | if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 6106 | htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 6107 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6108 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6109 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6110 | return 0; |
| 6111 | } |
| 6112 | |
| 6113 | __LJMP static int hlua_log(lua_State *L) |
| 6114 | { |
| 6115 | int level; |
| 6116 | const char *msg; |
| 6117 | |
| 6118 | MAY_LJMP(check_args(L, 2, "log")); |
| 6119 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6120 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6121 | |
| 6122 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6123 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6124 | |
| 6125 | hlua_sendlog(NULL, level, msg); |
| 6126 | return 0; |
| 6127 | } |
| 6128 | |
| 6129 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6130 | { |
| 6131 | const char *msg; |
| 6132 | |
| 6133 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6134 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6135 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6136 | return 0; |
| 6137 | } |
| 6138 | |
| 6139 | __LJMP static int hlua_log_info(lua_State *L) |
| 6140 | { |
| 6141 | const char *msg; |
| 6142 | |
| 6143 | MAY_LJMP(check_args(L, 1, "info")); |
| 6144 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6145 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6146 | return 0; |
| 6147 | } |
| 6148 | |
| 6149 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6150 | { |
| 6151 | const char *msg; |
| 6152 | |
| 6153 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6154 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6155 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6156 | return 0; |
| 6157 | } |
| 6158 | |
| 6159 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6160 | { |
| 6161 | const char *msg; |
| 6162 | |
| 6163 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6164 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6165 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6166 | return 0; |
| 6167 | } |
| 6168 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6169 | __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] | 6170 | { |
| 6171 | int wakeup_ms = lua_tointeger(L, -1); |
| 6172 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6173 | 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] | 6174 | return 0; |
| 6175 | } |
| 6176 | |
| 6177 | __LJMP static int hlua_sleep(lua_State *L) |
| 6178 | { |
| 6179 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6180 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6181 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6182 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6183 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6184 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6185 | wakeup_ms = tick_add(now_ms, delay); |
| 6186 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6187 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6188 | 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] | 6189 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6190 | } |
| 6191 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6192 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6193 | { |
| 6194 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6195 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6196 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6197 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6198 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6199 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6200 | wakeup_ms = tick_add(now_ms, delay); |
| 6201 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6202 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6203 | 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] | 6204 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6205 | } |
| 6206 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6207 | /* This functionis an LUA binding. it permits to give back |
| 6208 | * the hand at the HAProxy scheduler. It is used when the |
| 6209 | * LUA processing consumes a lot of time. |
| 6210 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6211 | __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] | 6212 | { |
| 6213 | return 0; |
| 6214 | } |
| 6215 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6216 | __LJMP static int hlua_yield(lua_State *L) |
| 6217 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6218 | 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] | 6219 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6220 | } |
| 6221 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6222 | /* This function change the nice of the currently executed |
| 6223 | * task. It is used set low or high priority at the current |
| 6224 | * task. |
| 6225 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6226 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6227 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6228 | struct hlua *hlua; |
| 6229 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6230 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6231 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6232 | hlua = hlua_gethlua(L); |
| 6233 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6234 | |
| 6235 | /* If he task is not set, I'm in a start mode. */ |
| 6236 | if (!hlua || !hlua->task) |
| 6237 | return 0; |
| 6238 | |
| 6239 | if (nice < -1024) |
| 6240 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6241 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6242 | nice = 1024; |
| 6243 | |
| 6244 | hlua->task->nice = nice; |
| 6245 | return 0; |
| 6246 | } |
| 6247 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6248 | /* 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] | 6249 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6250 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6251 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6252 | * |
| 6253 | * Task wrapper are longjmp safe because the only one Lua code |
| 6254 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6255 | */ |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6256 | 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] | 6257 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6258 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6259 | enum hlua_exec status; |
| 6260 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6261 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6262 | task_set_affinity(task, tid_bit); |
| 6263 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6264 | /* If it is the first call to the task, we must initialize the |
| 6265 | * execution timeouts. |
| 6266 | */ |
| 6267 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6268 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6269 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6270 | /* Execute the Lua code. */ |
| 6271 | status = hlua_ctx_resume(hlua, 1); |
| 6272 | |
| 6273 | switch (status) { |
| 6274 | /* finished or yield */ |
| 6275 | case HLUA_E_OK: |
| 6276 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6277 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6278 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6279 | break; |
| 6280 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6281 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6282 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6283 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6284 | break; |
| 6285 | |
| 6286 | /* finished with error. */ |
| 6287 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6288 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6289 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6290 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6291 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6292 | break; |
| 6293 | |
| 6294 | case HLUA_E_ERR: |
| 6295 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6296 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6297 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6298 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6299 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6300 | break; |
| 6301 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6302 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6303 | } |
| 6304 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6305 | /* This function is an LUA binding that register LUA function to be |
| 6306 | * executed after the HAProxy configuration parsing and before the |
| 6307 | * HAProxy scheduler starts. This function expect only one LUA |
| 6308 | * argument that is a function. This function returns nothing, but |
| 6309 | * throws if an error is encountered. |
| 6310 | */ |
| 6311 | __LJMP static int hlua_register_init(lua_State *L) |
| 6312 | { |
| 6313 | struct hlua_init_function *init; |
| 6314 | int ref; |
| 6315 | |
| 6316 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6317 | |
| 6318 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6319 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6320 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6321 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6322 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6323 | |
| 6324 | init->function_ref = ref; |
| 6325 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6326 | return 0; |
| 6327 | } |
| 6328 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6329 | /* This functio is an LUA binding. It permits to register a task |
| 6330 | * executed in parallel of the main HAroxy activity. The task is |
| 6331 | * created and it is set in the HAProxy scheduler. It can be called |
| 6332 | * from the "init" section, "post init" or during the runtime. |
| 6333 | * |
| 6334 | * Lua prototype: |
| 6335 | * |
| 6336 | * <none> core.register_task(<function>) |
| 6337 | */ |
| 6338 | static int hlua_register_task(lua_State *L) |
| 6339 | { |
| 6340 | struct hlua *hlua; |
| 6341 | struct task *task; |
| 6342 | int ref; |
| 6343 | |
| 6344 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6345 | |
| 6346 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6347 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6348 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6349 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6350 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6351 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6352 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6353 | if (!task) |
| 6354 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6355 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6356 | task->context = hlua; |
| 6357 | task->process = hlua_process_task; |
| 6358 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6359 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6360 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6361 | |
| 6362 | /* Restore the function in the stack. */ |
| 6363 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6364 | hlua->nargs = 0; |
| 6365 | |
| 6366 | /* Schedule task. */ |
| 6367 | task_schedule(task, now_ms); |
| 6368 | |
| 6369 | return 0; |
| 6370 | } |
| 6371 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6372 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6373 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6374 | * resume converters. |
| 6375 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6376 | 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] | 6377 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6378 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6379 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6380 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6381 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6382 | if (!stream) |
| 6383 | return 0; |
| 6384 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6385 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6386 | * Lua context can be not initialized. This behavior |
| 6387 | * permits to save performances because a systematic |
| 6388 | * Lua initialization cause 5% performances loss. |
| 6389 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6390 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6391 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6392 | if (!stream->hlua) { |
| 6393 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6394 | return 0; |
| 6395 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6396 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6397 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6398 | return 0; |
| 6399 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6400 | } |
| 6401 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6402 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6403 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6404 | |
| 6405 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6406 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6407 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6408 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6409 | else |
| 6410 | error = "critical error"; |
| 6411 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6412 | return 0; |
| 6413 | } |
| 6414 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6415 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6416 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6417 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6418 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6419 | return 0; |
| 6420 | } |
| 6421 | |
| 6422 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6423 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6424 | |
| 6425 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6426 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6427 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6428 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6429 | return 0; |
| 6430 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6431 | hlua_smp2lua(stream->hlua->T, smp); |
| 6432 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6433 | |
| 6434 | /* push keywords in the stack. */ |
| 6435 | if (arg_p) { |
| 6436 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6437 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6438 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6439 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6440 | return 0; |
| 6441 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6442 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6443 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6444 | } |
| 6445 | } |
| 6446 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6447 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6448 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6449 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6450 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6451 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6452 | } |
| 6453 | |
| 6454 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6455 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6456 | /* finished. */ |
| 6457 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6458 | /* If the stack is empty, the function fails. */ |
| 6459 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6460 | return 0; |
| 6461 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6462 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6463 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6464 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6465 | return 1; |
| 6466 | |
| 6467 | /* yield. */ |
| 6468 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6469 | 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] | 6470 | return 0; |
| 6471 | |
| 6472 | /* finished with error. */ |
| 6473 | case HLUA_E_ERRMSG: |
| 6474 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6475 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6476 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6477 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6478 | return 0; |
| 6479 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6480 | case HLUA_E_ETMOUT: |
| 6481 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6482 | return 0; |
| 6483 | |
| 6484 | case HLUA_E_NOMEM: |
| 6485 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6486 | return 0; |
| 6487 | |
| 6488 | case HLUA_E_YIELD: |
| 6489 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6490 | return 0; |
| 6491 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6492 | case HLUA_E_ERR: |
| 6493 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6494 | 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] | 6495 | |
| 6496 | default: |
| 6497 | return 0; |
| 6498 | } |
| 6499 | } |
| 6500 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6501 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6502 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6503 | * resume sample-fetches. This function will be called by the sample |
| 6504 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6505 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6506 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6507 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6508 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6509 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6510 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6511 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6512 | const struct buffer msg = { }; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6513 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6514 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6515 | if (!stream) |
| 6516 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6517 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6518 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6519 | * Lua context can be not initialized. This behavior |
| 6520 | * permits to save performances because a systematic |
| 6521 | * Lua initialization cause 5% performances loss. |
| 6522 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6523 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6524 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6525 | if (!stream->hlua) { |
| 6526 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6527 | return 0; |
| 6528 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6529 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6530 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6531 | return 0; |
| 6532 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6533 | } |
| 6534 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6535 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6536 | |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6537 | if (stream->be->mode == PR_MODE_HTTP) { |
| 6538 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
| 6539 | hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 6540 | else |
| 6541 | hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 6542 | } |
| 6543 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6544 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6545 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6546 | |
| 6547 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6548 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6549 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6550 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6551 | else |
| 6552 | error = "critical error"; |
| 6553 | 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] | 6554 | return 0; |
| 6555 | } |
| 6556 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6557 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6558 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6559 | 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] | 6560 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6561 | return 0; |
| 6562 | } |
| 6563 | |
| 6564 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6565 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6566 | |
| 6567 | /* push arguments in the stack. */ |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6568 | 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] | 6569 | 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] | 6570 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6571 | return 0; |
| 6572 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6573 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6574 | |
| 6575 | /* push keywords in the stack. */ |
| 6576 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6577 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6578 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6579 | 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] | 6580 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6581 | return 0; |
| 6582 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6583 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6584 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6585 | } |
| 6586 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6587 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6588 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6589 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6590 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6591 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6592 | } |
| 6593 | |
| 6594 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6595 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6596 | /* finished. */ |
| 6597 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6598 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6599 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6600 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6601 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6602 | /* If the stack is empty, the function fails. */ |
| 6603 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6604 | return 0; |
| 6605 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6606 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6607 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6608 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6609 | |
| 6610 | /* Set the end of execution flag. */ |
| 6611 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6612 | return 1; |
| 6613 | |
| 6614 | /* yield. */ |
| 6615 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6616 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6617 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6618 | 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] | 6619 | return 0; |
| 6620 | |
| 6621 | /* finished with error. */ |
| 6622 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6623 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6624 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6625 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6626 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6627 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6628 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6629 | return 0; |
| 6630 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6631 | case HLUA_E_ETMOUT: |
| 6632 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6633 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6634 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6635 | return 0; |
| 6636 | |
| 6637 | case HLUA_E_NOMEM: |
| 6638 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6639 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6640 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6641 | return 0; |
| 6642 | |
| 6643 | case HLUA_E_YIELD: |
| 6644 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6645 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6646 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6647 | return 0; |
| 6648 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6649 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6650 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6651 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6652 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6653 | 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] | 6654 | |
| 6655 | default: |
| 6656 | return 0; |
| 6657 | } |
| 6658 | } |
| 6659 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6660 | /* This function is an LUA binding used for registering |
| 6661 | * "sample-conv" functions. It expects a converter name used |
| 6662 | * in the haproxy configuration file, and an LUA function. |
| 6663 | */ |
| 6664 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6665 | { |
| 6666 | struct sample_conv_kw_list *sck; |
| 6667 | const char *name; |
| 6668 | int ref; |
| 6669 | int len; |
| 6670 | struct hlua_function *fcn; |
| 6671 | |
| 6672 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6673 | |
| 6674 | /* First argument : converter name. */ |
| 6675 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6676 | |
| 6677 | /* Second argument : lua function. */ |
| 6678 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6679 | |
| 6680 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6681 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6682 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6683 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6684 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6685 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6686 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6687 | |
| 6688 | /* Fill fcn. */ |
| 6689 | fcn->name = strdup(name); |
| 6690 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6691 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6692 | fcn->function_ref = ref; |
| 6693 | |
| 6694 | /* List head */ |
| 6695 | sck->list.n = sck->list.p = NULL; |
| 6696 | |
| 6697 | /* converter keyword. */ |
| 6698 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6699 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6700 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6701 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6702 | |
| 6703 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6704 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6705 | 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] | 6706 | sck->kw[0].val_args = NULL; |
| 6707 | sck->kw[0].in_type = SMP_T_STR; |
| 6708 | sck->kw[0].out_type = SMP_T_STR; |
| 6709 | sck->kw[0].private = fcn; |
| 6710 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6711 | /* Register this new converter */ |
| 6712 | sample_register_convs(sck); |
| 6713 | |
| 6714 | return 0; |
| 6715 | } |
| 6716 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6717 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6718 | * "sample-fetch" functions. It expects a converter name used |
| 6719 | * in the haproxy configuration file, and an LUA function. |
| 6720 | */ |
| 6721 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6722 | { |
| 6723 | const char *name; |
| 6724 | int ref; |
| 6725 | int len; |
| 6726 | struct sample_fetch_kw_list *sfk; |
| 6727 | struct hlua_function *fcn; |
| 6728 | |
| 6729 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6730 | |
| 6731 | /* First argument : sample-fetch name. */ |
| 6732 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6733 | |
| 6734 | /* Second argument : lua function. */ |
| 6735 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6736 | |
| 6737 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6738 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6739 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6740 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6741 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6742 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6743 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6744 | |
| 6745 | /* Fill fcn. */ |
| 6746 | fcn->name = strdup(name); |
| 6747 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6748 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6749 | fcn->function_ref = ref; |
| 6750 | |
| 6751 | /* List head */ |
| 6752 | sfk->list.n = sfk->list.p = NULL; |
| 6753 | |
| 6754 | /* sample-fetch keyword. */ |
| 6755 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6756 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6757 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6758 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6759 | |
| 6760 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6761 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6762 | 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] | 6763 | sfk->kw[0].val_args = NULL; |
| 6764 | sfk->kw[0].out_type = SMP_T_STR; |
| 6765 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6766 | sfk->kw[0].val = 0; |
| 6767 | sfk->kw[0].private = fcn; |
| 6768 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6769 | /* Register this new fetch. */ |
| 6770 | sample_register_fetches(sfk); |
| 6771 | |
| 6772 | return 0; |
| 6773 | } |
| 6774 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6775 | /* This function is a wrapper to execute each LUA function declared |
| 6776 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6777 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6778 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6779 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6780 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6781 | 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] | 6782 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6783 | { |
| 6784 | char **arg; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6785 | unsigned int hflags = 0; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6786 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6787 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6788 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6789 | |
| 6790 | switch (rule->from) { |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6791 | case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break; |
| 6792 | case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break; |
| 6793 | case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break; |
| 6794 | case ACT_F_HTTP_RES: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6795 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6796 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6797 | return ACT_RET_CONT; |
| 6798 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6799 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6800 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6801 | * Lua context can be not initialized. This behavior |
| 6802 | * permits to save performances because a systematic |
| 6803 | * Lua initialization cause 5% performances loss. |
| 6804 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6805 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6806 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6807 | if (!s->hlua) { |
| 6808 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6809 | rule->arg.hlua_rule->fcn.name); |
| 6810 | return ACT_RET_CONT; |
| 6811 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6812 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6813 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6814 | rule->arg.hlua_rule->fcn.name); |
| 6815 | return ACT_RET_CONT; |
| 6816 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6817 | } |
| 6818 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6819 | consistency_set(s, dir, &s->hlua->cons); |
| 6820 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6821 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6822 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6823 | |
| 6824 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6825 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6826 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6827 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6828 | else |
| 6829 | error = "critical error"; |
| 6830 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6831 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6832 | return ACT_RET_CONT; |
| 6833 | } |
| 6834 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6835 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6836 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6837 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6838 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6839 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6840 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6841 | } |
| 6842 | |
| 6843 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6844 | 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] | 6845 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6846 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6847 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6848 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6849 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6850 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6851 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6852 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6853 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6854 | |
| 6855 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6856 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6857 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6858 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6859 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6860 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6861 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6862 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6863 | lua_pushstring(s->hlua->T, *arg); |
| 6864 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6865 | } |
| 6866 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6867 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6868 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6869 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6870 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6871 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6872 | } |
| 6873 | |
Christopher Faulet | 719ddc8 | 2020-06-02 18:46:07 +0200 | [diff] [blame] | 6874 | /* Always reset the analyse expiration timeout for the corresponding |
| 6875 | * channel in case the lua script yield, to be sure to not keep an |
| 6876 | * expired timeout. |
| 6877 | */ |
| 6878 | if (dir == SMP_OPT_DIR_REQ) |
| 6879 | s->req.analyse_exp = TICK_ETERNITY; |
| 6880 | else |
| 6881 | s->res.analyse_exp = TICK_ETERNITY; |
| 6882 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6883 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6884 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6885 | /* finished. */ |
| 6886 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6887 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6888 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6889 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6890 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6891 | if (s->hlua->flags & HLUA_STOP) |
Christopher Faulet | 4629d08 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 6892 | return ACT_RET_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6893 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6894 | |
| 6895 | /* yield. */ |
| 6896 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6897 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6898 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Christopher Faulet | ea0b0e2 | 2019-08-14 23:19:45 +0200 | [diff] [blame] | 6899 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6900 | s->req.analyse_exp = s->hlua->wake_time; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6901 | else |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6902 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6903 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6904 | /* Some actions can be wake up when a "write" event |
| 6905 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6906 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6907 | */ |
Christopher Faulet | b22f650 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6908 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6909 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6910 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6911 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6912 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6913 | * because HAProxy is not able to manipulate data, it |
| 6914 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6915 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6916 | |
| 6917 | /* finished with error. */ |
| 6918 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6919 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6920 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6921 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6922 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6923 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6924 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6925 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6926 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6927 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6928 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6929 | case HLUA_E_ETMOUT: |
| 6930 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6931 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6932 | return ACT_RET_ERR; |
| 6933 | } |
| 6934 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6935 | return 0; |
| 6936 | |
| 6937 | case HLUA_E_NOMEM: |
| 6938 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6939 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6940 | return ACT_RET_ERR; |
| 6941 | } |
| 6942 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6943 | return 0; |
| 6944 | |
| 6945 | case HLUA_E_YIELD: |
| 6946 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6947 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6948 | return ACT_RET_ERR; |
| 6949 | } |
| 6950 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6951 | rule->arg.hlua_rule->fcn.name); |
| 6952 | return 0; |
| 6953 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6954 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6955 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6956 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6957 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6958 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6959 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6960 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6961 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6962 | |
| 6963 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6964 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6965 | } |
| 6966 | } |
| 6967 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6968 | 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] | 6969 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6970 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6971 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6972 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6973 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6974 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6975 | } |
| 6976 | |
| 6977 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6978 | { |
| 6979 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6980 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6981 | struct task *task; |
| 6982 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6983 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6984 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6985 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6986 | if (!hlua) { |
| 6987 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6988 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6989 | return 0; |
| 6990 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6991 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6992 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6993 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6994 | |
| 6995 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6996 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6997 | if (!task) { |
| 6998 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6999 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7000 | return 0; |
| 7001 | } |
| 7002 | task->nice = 0; |
| 7003 | task->context = ctx; |
| 7004 | task->process = hlua_applet_wakeup; |
| 7005 | ctx->ctx.hlua_apptcp.task = task; |
| 7006 | |
| 7007 | /* In the execution wrappers linked with a stream, the |
| 7008 | * Lua context can be not initialized. This behavior |
| 7009 | * permits to save performances because a systematic |
| 7010 | * Lua initialization cause 5% performances loss. |
| 7011 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7012 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7013 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 7014 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7015 | return 0; |
| 7016 | } |
| 7017 | |
| 7018 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7019 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7020 | |
| 7021 | /* The following Lua calls can fail. */ |
| 7022 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7023 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7024 | error = lua_tostring(hlua->T, -1); |
| 7025 | else |
| 7026 | error = "critical error"; |
| 7027 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7028 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7029 | return 0; |
| 7030 | } |
| 7031 | |
| 7032 | /* Check stack available size. */ |
| 7033 | if (!lua_checkstack(hlua->T, 1)) { |
| 7034 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7035 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7036 | RESET_SAFE_LJMP(hlua->T); |
| 7037 | return 0; |
| 7038 | } |
| 7039 | |
| 7040 | /* Restore the function in the stack. */ |
| 7041 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7042 | |
| 7043 | /* Create and and push object stream in the stack. */ |
| 7044 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7045 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7046 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7047 | RESET_SAFE_LJMP(hlua->T); |
| 7048 | return 0; |
| 7049 | } |
| 7050 | hlua->nargs = 1; |
| 7051 | |
| 7052 | /* push keywords in the stack. */ |
| 7053 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7054 | if (!lua_checkstack(hlua->T, 1)) { |
| 7055 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7056 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7057 | RESET_SAFE_LJMP(hlua->T); |
| 7058 | return 0; |
| 7059 | } |
| 7060 | lua_pushstring(hlua->T, *arg); |
| 7061 | hlua->nargs++; |
| 7062 | } |
| 7063 | |
| 7064 | RESET_SAFE_LJMP(hlua->T); |
| 7065 | |
| 7066 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7067 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7068 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7069 | |
| 7070 | return 1; |
| 7071 | } |
| 7072 | |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7073 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7074 | { |
| 7075 | struct stream_interface *si = ctx->owner; |
| 7076 | struct stream *strm = si_strm(si); |
| 7077 | struct channel *res = si_ic(si); |
| 7078 | struct act_rule *rule = ctx->rule; |
| 7079 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7080 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7081 | |
| 7082 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7083 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7084 | /* eat the whole request */ |
| 7085 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7086 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7087 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7088 | |
| 7089 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7090 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7091 | return; |
| 7092 | |
| 7093 | /* Execute the function. */ |
| 7094 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7095 | /* finished. */ |
| 7096 | case HLUA_E_OK: |
| 7097 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7098 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7099 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7100 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7101 | res->flags |= CF_READ_NULL; |
| 7102 | si_shutr(si); |
| 7103 | return; |
| 7104 | |
| 7105 | /* yield. */ |
| 7106 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7107 | if (hlua->wake_time != TICK_ETERNITY) |
| 7108 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7109 | return; |
| 7110 | |
| 7111 | /* finished with error. */ |
| 7112 | case HLUA_E_ERRMSG: |
| 7113 | /* Display log. */ |
| 7114 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7115 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7116 | lua_pop(hlua->T, 1); |
| 7117 | goto error; |
| 7118 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7119 | case HLUA_E_ETMOUT: |
| 7120 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 7121 | rule->arg.hlua_rule->fcn.name); |
| 7122 | goto error; |
| 7123 | |
| 7124 | case HLUA_E_NOMEM: |
| 7125 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 7126 | rule->arg.hlua_rule->fcn.name); |
| 7127 | goto error; |
| 7128 | |
| 7129 | case HLUA_E_YIELD: /* unexpected */ |
| 7130 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 7131 | rule->arg.hlua_rule->fcn.name); |
| 7132 | goto error; |
| 7133 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7134 | case HLUA_E_ERR: |
| 7135 | /* Display log. */ |
| 7136 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 7137 | rule->arg.hlua_rule->fcn.name); |
| 7138 | goto error; |
| 7139 | |
| 7140 | default: |
| 7141 | goto error; |
| 7142 | } |
| 7143 | |
| 7144 | error: |
| 7145 | |
| 7146 | /* For all other cases, just close the stream. */ |
| 7147 | si_shutw(si); |
| 7148 | si_shutr(si); |
| 7149 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7150 | } |
| 7151 | |
| 7152 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7153 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7154 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7155 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7156 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7157 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7158 | } |
| 7159 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7160 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7161 | * an errors occurs and -1 if more data are required for initializing |
| 7162 | * the applet. |
| 7163 | */ |
| 7164 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7165 | { |
| 7166 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7167 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7168 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7169 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7170 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7171 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7172 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7173 | txn = strm->txn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7174 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7175 | /* We want two things in HTTP mode : |
| 7176 | * - enforce server-close mode if we were in keep-alive, so that the |
| 7177 | * applet is released after each response ; |
| 7178 | * - enable request body transfer to the applet in order to resync |
| 7179 | * with the response body. |
| 7180 | */ |
| 7181 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 7182 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7183 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7184 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7185 | if (!hlua) { |
| 7186 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7187 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7188 | return 0; |
| 7189 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7190 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7191 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7192 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7193 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7194 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7195 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7196 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7197 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7198 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7199 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7200 | if (!task) { |
| 7201 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7202 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7203 | return 0; |
| 7204 | } |
| 7205 | task->nice = 0; |
| 7206 | task->context = ctx; |
| 7207 | task->process = hlua_applet_wakeup; |
| 7208 | ctx->ctx.hlua_apphttp.task = task; |
| 7209 | |
| 7210 | /* In the execution wrappers linked with a stream, the |
| 7211 | * Lua context can be not initialized. This behavior |
| 7212 | * permits to save performances because a systematic |
| 7213 | * Lua initialization cause 5% performances loss. |
| 7214 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7215 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7216 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 7217 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7218 | return 0; |
| 7219 | } |
| 7220 | |
| 7221 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7222 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7223 | |
| 7224 | /* The following Lua calls can fail. */ |
| 7225 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7226 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7227 | error = lua_tostring(hlua->T, -1); |
| 7228 | else |
| 7229 | error = "critical error"; |
| 7230 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7231 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7232 | return 0; |
| 7233 | } |
| 7234 | |
| 7235 | /* Check stack available size. */ |
| 7236 | if (!lua_checkstack(hlua->T, 1)) { |
| 7237 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7238 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7239 | RESET_SAFE_LJMP(hlua->T); |
| 7240 | return 0; |
| 7241 | } |
| 7242 | |
| 7243 | /* Restore the function in the stack. */ |
| 7244 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7245 | |
| 7246 | /* Create and and push object stream in the stack. */ |
| 7247 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7248 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7249 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7250 | RESET_SAFE_LJMP(hlua->T); |
| 7251 | return 0; |
| 7252 | } |
| 7253 | hlua->nargs = 1; |
| 7254 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7255 | /* push keywords in the stack. */ |
| 7256 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7257 | if (!lua_checkstack(hlua->T, 1)) { |
| 7258 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7259 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7260 | RESET_SAFE_LJMP(hlua->T); |
| 7261 | return 0; |
| 7262 | } |
| 7263 | lua_pushstring(hlua->T, *arg); |
| 7264 | hlua->nargs++; |
| 7265 | } |
| 7266 | |
| 7267 | RESET_SAFE_LJMP(hlua->T); |
| 7268 | |
| 7269 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7270 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7271 | |
| 7272 | return 1; |
| 7273 | } |
| 7274 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7275 | static void hlua_applet_htx_fct(struct appctx *ctx) |
| 7276 | { |
| 7277 | struct stream_interface *si = ctx->owner; |
| 7278 | struct stream *strm = si_strm(si); |
| 7279 | struct channel *req = si_oc(si); |
| 7280 | struct channel *res = si_ic(si); |
| 7281 | struct act_rule *rule = ctx->rule; |
| 7282 | struct proxy *px = strm->be; |
| 7283 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7284 | struct htx *req_htx, *res_htx; |
| 7285 | |
| 7286 | res_htx = htx_from_buf(&res->buf); |
| 7287 | |
| 7288 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7289 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7290 | goto out; |
| 7291 | |
| 7292 | /* Check if the input buffer is avalaible. */ |
| 7293 | if (!b_size(&res->buf)) { |
| 7294 | si_rx_room_blk(si); |
| 7295 | goto out; |
| 7296 | } |
| 7297 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7298 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7299 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7300 | |
| 7301 | /* Set the currently running flag. */ |
| 7302 | if (!HLUA_IS_RUNNING(hlua) && |
| 7303 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7304 | struct htx_blk *blk; |
| 7305 | size_t count = co_data(req); |
| 7306 | |
| 7307 | if (!count) { |
| 7308 | si_cant_get(si); |
| 7309 | goto out; |
| 7310 | } |
| 7311 | |
| 7312 | /* We need to flush the request header. This left the body for |
| 7313 | * the Lua. |
| 7314 | */ |
| 7315 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7316 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7317 | while (count && blk) { |
| 7318 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7319 | uint32_t sz = htx_get_blksz(blk); |
| 7320 | |
| 7321 | if (sz > count) { |
| 7322 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7323 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7324 | goto out; |
| 7325 | } |
| 7326 | |
| 7327 | count -= sz; |
| 7328 | co_set_data(req, co_data(req) - sz); |
| 7329 | blk = htx_remove_blk(req_htx, blk); |
| 7330 | |
| 7331 | if (type == HTX_BLK_EOH) |
| 7332 | break; |
| 7333 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7334 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7335 | } |
| 7336 | |
| 7337 | /* Executes The applet if it is not done. */ |
| 7338 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7339 | |
| 7340 | /* Execute the function. */ |
| 7341 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7342 | /* finished. */ |
| 7343 | case HLUA_E_OK: |
| 7344 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7345 | break; |
| 7346 | |
| 7347 | /* yield. */ |
| 7348 | case HLUA_E_AGAIN: |
| 7349 | if (hlua->wake_time != TICK_ETERNITY) |
| 7350 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7351 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7352 | |
| 7353 | /* finished with error. */ |
| 7354 | case HLUA_E_ERRMSG: |
| 7355 | /* Display log. */ |
| 7356 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7357 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7358 | lua_pop(hlua->T, 1); |
| 7359 | goto error; |
| 7360 | |
| 7361 | case HLUA_E_ETMOUT: |
| 7362 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7363 | rule->arg.hlua_rule->fcn.name); |
| 7364 | goto error; |
| 7365 | |
| 7366 | case HLUA_E_NOMEM: |
| 7367 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7368 | rule->arg.hlua_rule->fcn.name); |
| 7369 | goto error; |
| 7370 | |
| 7371 | case HLUA_E_YIELD: /* unexpected */ |
| 7372 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7373 | rule->arg.hlua_rule->fcn.name); |
| 7374 | goto error; |
| 7375 | |
| 7376 | case HLUA_E_ERR: |
| 7377 | /* Display log. */ |
| 7378 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7379 | rule->arg.hlua_rule->fcn.name); |
| 7380 | goto error; |
| 7381 | |
| 7382 | default: |
| 7383 | goto error; |
| 7384 | } |
| 7385 | } |
| 7386 | |
| 7387 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7388 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7389 | goto done; |
| 7390 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7391 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7392 | goto error; |
| 7393 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7394 | /* Don't add TLR because mux-h1 will take care of it */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7395 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7396 | si_rx_room_blk(si); |
| 7397 | goto out; |
| 7398 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7399 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7400 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7401 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7402 | } |
| 7403 | |
| 7404 | done: |
| 7405 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7406 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7407 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7408 | si_shutr(si); |
| 7409 | } |
| 7410 | |
| 7411 | /* eat the whole request */ |
| 7412 | if (co_data(req)) { |
| 7413 | req_htx = htx_from_buf(&req->buf); |
| 7414 | co_htx_skip(req, req_htx, co_data(req)); |
| 7415 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7416 | } |
| 7417 | } |
| 7418 | |
| 7419 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7420 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7421 | return; |
| 7422 | |
| 7423 | error: |
| 7424 | |
| 7425 | /* If we are in HTTP mode, and we are not send any |
| 7426 | * data, return a 500 server error in best effort: |
| 7427 | * if there is no room available in the buffer, |
| 7428 | * just close the connection. |
| 7429 | */ |
| 7430 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7431 | struct buffer *err = &htx_err_chunks[HTTP_ERR_500]; |
| 7432 | |
| 7433 | channel_erase(res); |
| 7434 | res->buf.data = b_data(err); |
| 7435 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7436 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7437 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7438 | } |
| 7439 | if (!(strm->flags & SF_ERR_MASK)) |
| 7440 | strm->flags |= SF_ERR_RESOURCE; |
| 7441 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7442 | goto done; |
| 7443 | } |
| 7444 | |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7445 | void hlua_applet_http_fct(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7446 | { |
| 7447 | struct stream_interface *si = ctx->owner; |
| 7448 | struct stream *strm = si_strm(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7449 | struct channel *req = si_oc(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7450 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7451 | struct act_rule *rule = ctx->rule; |
| 7452 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7453 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7454 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7455 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7456 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7457 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7458 | int ret; |
| 7459 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7460 | if (IS_HTX_STRM(strm)) |
| 7461 | return hlua_applet_htx_fct(ctx); |
| 7462 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7463 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7464 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7465 | goto out; |
| 7466 | |
| 7467 | /* Check if the input buffer is avalaible. */ |
| 7468 | if (!b_size(&res->buf)) { |
| 7469 | si_rx_room_blk(si); |
| 7470 | goto out; |
| 7471 | } |
| 7472 | /* check that the output is not closed */ |
| 7473 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
| 7474 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7475 | |
| 7476 | /* Set the currently running flag. */ |
| 7477 | if (!HLUA_IS_RUNNING(hlua) && |
| 7478 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7479 | /* Store the max amount of bytes that we can read. */ |
| 7480 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 7481 | |
| 7482 | /* We need to flush the request header. This left the body |
| 7483 | * for the Lua. |
| 7484 | */ |
| 7485 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7486 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7487 | ret = co_getblk_nc(req, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7488 | if (ret == -1) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7489 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7490 | |
| 7491 | /* No data available, ask for more data. */ |
| 7492 | if (ret == 1) |
| 7493 | len2 = 0; |
| 7494 | if (ret == 0) |
| 7495 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 7496 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7497 | si_cant_get(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7498 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7499 | } |
| 7500 | |
| 7501 | /* skip the requests bytes. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7502 | co_skip(req, strm->txn->req.eoh + strm->txn->req.eol); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7503 | } |
| 7504 | |
| 7505 | /* Executes The applet if it is not done. */ |
| 7506 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7507 | |
| 7508 | /* Execute the function. */ |
| 7509 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7510 | /* finished. */ |
| 7511 | case HLUA_E_OK: |
| 7512 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7513 | break; |
| 7514 | |
| 7515 | /* yield. */ |
| 7516 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7517 | if (hlua->wake_time != TICK_ETERNITY) |
| 7518 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7519 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7520 | |
| 7521 | /* finished with error. */ |
| 7522 | case HLUA_E_ERRMSG: |
| 7523 | /* Display log. */ |
| 7524 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7525 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7526 | lua_pop(hlua->T, 1); |
| 7527 | goto error; |
| 7528 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7529 | case HLUA_E_ETMOUT: |
| 7530 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7531 | rule->arg.hlua_rule->fcn.name); |
| 7532 | goto error; |
| 7533 | |
| 7534 | case HLUA_E_NOMEM: |
| 7535 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7536 | rule->arg.hlua_rule->fcn.name); |
| 7537 | goto error; |
| 7538 | |
| 7539 | case HLUA_E_YIELD: /* unexpected */ |
| 7540 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7541 | rule->arg.hlua_rule->fcn.name); |
| 7542 | goto error; |
| 7543 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7544 | case HLUA_E_ERR: |
| 7545 | /* Display log. */ |
| 7546 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7547 | rule->arg.hlua_rule->fcn.name); |
| 7548 | goto error; |
| 7549 | |
| 7550 | default: |
| 7551 | goto error; |
| 7552 | } |
| 7553 | } |
| 7554 | |
| 7555 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7556 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7557 | goto done; |
| 7558 | |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 7559 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7560 | goto error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7561 | |
| 7562 | /* We must send the final chunk. */ |
| 7563 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 7564 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 7565 | |
| 7566 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 7567 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7568 | |
| 7569 | /* critical error. */ |
| 7570 | if (ret == -2 || ret == -3) { |
| 7571 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 7572 | rule->arg.hlua_rule->fcn.name); |
| 7573 | goto error; |
| 7574 | } |
| 7575 | |
| 7576 | /* no enough space error. */ |
| 7577 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7578 | si_rx_room_blk(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7579 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7580 | } |
| 7581 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7582 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7583 | ctx->ctx.hlua_apphttp.flags |= (APPLET_LAST_CHK|APPLET_RSP_SENT); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7584 | } |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7585 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7586 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7587 | done: |
| 7588 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 7589 | if (!(res->flags & CF_SHUTR)) { |
| 7590 | res->flags |= CF_READ_NULL; |
| 7591 | si_shutr(si); |
| 7592 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7593 | |
| 7594 | /* eat the whole request */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7595 | if (co_data(req)) |
| 7596 | co_skip(req, co_data(req)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7597 | } |
| 7598 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7599 | out: |
| 7600 | return; |
| 7601 | |
| 7602 | error: |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7603 | |
| 7604 | /* If we are in HTTP mode, and we are not send any |
| 7605 | * data, return a 500 server error in best effort: |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7606 | * if there is no room available in the buffer, |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7607 | * just close the connection. |
| 7608 | */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7609 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7610 | channel_erase(res); |
| 7611 | ci_putblk(res, error_500, strlen(error_500)); |
| 7612 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7613 | if (!(strm->flags & SF_ERR_MASK)) |
| 7614 | strm->flags |= SF_ERR_RESOURCE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7615 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7616 | goto done; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7617 | } |
| 7618 | |
| 7619 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7620 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7621 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7622 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7623 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7624 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7625 | } |
| 7626 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7627 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 7628 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7629 | * |
| 7630 | * This function can fail with an abort() due to an Lua critical error. |
| 7631 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7632 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7633 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7634 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7635 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7636 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7637 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7638 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7639 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7640 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7641 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7642 | if (!rule->arg.hlua_rule) { |
| 7643 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7644 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7645 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7646 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7647 | /* Memory for arguments. */ |
| 7648 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7649 | if (!rule->arg.hlua_rule->args) { |
| 7650 | memprintf(err, "out of memory error"); |
| 7651 | return ACT_RET_PRS_ERR; |
| 7652 | } |
| 7653 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7654 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7655 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7656 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7657 | /* Expect some arguments */ |
| 7658 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7659 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7660 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7661 | return ACT_RET_PRS_ERR; |
| 7662 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7663 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7664 | if (!rule->arg.hlua_rule->args[i]) { |
| 7665 | memprintf(err, "out of memory error"); |
| 7666 | return ACT_RET_PRS_ERR; |
| 7667 | } |
| 7668 | (*cur_arg)++; |
| 7669 | } |
| 7670 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7671 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7672 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7673 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7674 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7675 | } |
| 7676 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7677 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7678 | struct act_rule *rule, char **err) |
| 7679 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7680 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7681 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7682 | /* HTTP applets are forbidden in tcp-request rules. |
| 7683 | * HTTP applet request requires everything initilized by |
| 7684 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 7685 | * The applet will be immediately initilized, but its before |
| 7686 | * the call of this analyzer. |
| 7687 | */ |
| 7688 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7689 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7690 | return ACT_RET_PRS_ERR; |
| 7691 | } |
| 7692 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7693 | /* Memory for the rule. */ |
| 7694 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7695 | if (!rule->arg.hlua_rule) { |
| 7696 | memprintf(err, "out of memory error"); |
| 7697 | return ACT_RET_PRS_ERR; |
| 7698 | } |
| 7699 | |
| 7700 | /* Reference the Lua function and store the reference. */ |
| 7701 | rule->arg.hlua_rule->fcn = *fcn; |
| 7702 | |
| 7703 | /* TODO: later accept arguments. */ |
| 7704 | rule->arg.hlua_rule->args = NULL; |
| 7705 | |
| 7706 | /* Add applet pointer in the rule. */ |
| 7707 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7708 | rule->applet.name = fcn->name; |
| 7709 | rule->applet.init = hlua_applet_http_init; |
| 7710 | rule->applet.fct = hlua_applet_http_fct; |
| 7711 | rule->applet.release = hlua_applet_http_release; |
| 7712 | rule->applet.timeout = hlua_timeout_applet; |
| 7713 | |
| 7714 | return ACT_RET_PRS_OK; |
| 7715 | } |
| 7716 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7717 | /* This function is an LUA binding used for registering |
| 7718 | * "sample-conv" functions. It expects a converter name used |
| 7719 | * in the haproxy configuration file, and an LUA function. |
| 7720 | */ |
| 7721 | __LJMP static int hlua_register_action(lua_State *L) |
| 7722 | { |
| 7723 | struct action_kw_list *akl; |
| 7724 | const char *name; |
| 7725 | int ref; |
| 7726 | int len; |
| 7727 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7728 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7729 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7730 | /* Initialise the number of expected arguments at 0. */ |
| 7731 | nargs = 0; |
| 7732 | |
| 7733 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7734 | 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] | 7735 | |
| 7736 | /* First argument : converter name. */ |
| 7737 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7738 | |
| 7739 | /* Second argument : environment. */ |
| 7740 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7741 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7742 | |
| 7743 | /* Third argument : lua function. */ |
| 7744 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7745 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7746 | /* 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] | 7747 | if (lua_gettop(L) >= 4) |
| 7748 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7749 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7750 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7751 | lua_pushnil(L); |
| 7752 | while (lua_next(L, 2) != 0) { |
| 7753 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7754 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7755 | |
| 7756 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7757 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7758 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7759 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7760 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7761 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7762 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7763 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7764 | |
| 7765 | /* Fill fcn. */ |
| 7766 | fcn->name = strdup(name); |
| 7767 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7768 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7769 | fcn->function_ref = ref; |
| 7770 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7771 | /* Set the expected number od arguments. */ |
| 7772 | fcn->nargs = nargs; |
| 7773 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7774 | /* List head */ |
| 7775 | akl->list.n = akl->list.p = NULL; |
| 7776 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7777 | /* action keyword. */ |
| 7778 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7779 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7780 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7781 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7782 | |
| 7783 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7784 | |
| 7785 | akl->kw[0].match_pfx = 0; |
| 7786 | akl->kw[0].private = fcn; |
| 7787 | akl->kw[0].parse = action_register_lua; |
| 7788 | |
| 7789 | /* select the action registering point. */ |
| 7790 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7791 | tcp_req_cont_keywords_register(akl); |
| 7792 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7793 | tcp_res_cont_keywords_register(akl); |
| 7794 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7795 | http_req_keywords_register(akl); |
| 7796 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7797 | http_res_keywords_register(akl); |
| 7798 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7799 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7800 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7801 | "are expected.", lua_tostring(L, -1))); |
| 7802 | |
| 7803 | /* pop the environment string. */ |
| 7804 | lua_pop(L, 1); |
| 7805 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7806 | return ACT_RET_PRS_OK; |
| 7807 | } |
| 7808 | |
| 7809 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7810 | struct act_rule *rule, char **err) |
| 7811 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7812 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7813 | |
Christopher Faulet | eb70980 | 2019-04-11 22:04:08 +0200 | [diff] [blame] | 7814 | if (px->mode == PR_MODE_HTTP && (px->options2 & PR_O2_USE_HTX)) { |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7815 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 7816 | return ACT_RET_PRS_ERR; |
| 7817 | } |
| 7818 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7819 | /* Memory for the rule. */ |
| 7820 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7821 | if (!rule->arg.hlua_rule) { |
| 7822 | memprintf(err, "out of memory error"); |
| 7823 | return ACT_RET_PRS_ERR; |
| 7824 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7825 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7826 | /* Reference the Lua function and store the reference. */ |
| 7827 | rule->arg.hlua_rule->fcn = *fcn; |
| 7828 | |
| 7829 | /* TODO: later accept arguments. */ |
| 7830 | rule->arg.hlua_rule->args = NULL; |
| 7831 | |
| 7832 | /* Add applet pointer in the rule. */ |
| 7833 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7834 | rule->applet.name = fcn->name; |
| 7835 | rule->applet.init = hlua_applet_tcp_init; |
| 7836 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7837 | rule->applet.release = hlua_applet_tcp_release; |
| 7838 | rule->applet.timeout = hlua_timeout_applet; |
| 7839 | |
| 7840 | return 0; |
| 7841 | } |
| 7842 | |
| 7843 | /* This function is an LUA binding used for registering |
| 7844 | * "sample-conv" functions. It expects a converter name used |
| 7845 | * in the haproxy configuration file, and an LUA function. |
| 7846 | */ |
| 7847 | __LJMP static int hlua_register_service(lua_State *L) |
| 7848 | { |
| 7849 | struct action_kw_list *akl; |
| 7850 | const char *name; |
| 7851 | const char *env; |
| 7852 | int ref; |
| 7853 | int len; |
| 7854 | struct hlua_function *fcn; |
| 7855 | |
| 7856 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7857 | |
| 7858 | /* First argument : converter name. */ |
| 7859 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7860 | |
| 7861 | /* Second argument : environment. */ |
| 7862 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7863 | |
| 7864 | /* Third argument : lua function. */ |
| 7865 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7866 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7867 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7868 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7869 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7870 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7871 | fcn = calloc(1, sizeof(*fcn)); |
| 7872 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7873 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7874 | |
| 7875 | /* Fill fcn. */ |
| 7876 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7877 | fcn->name = calloc(1, len); |
| 7878 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7879 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7880 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7881 | fcn->function_ref = ref; |
| 7882 | |
| 7883 | /* List head */ |
| 7884 | akl->list.n = akl->list.p = NULL; |
| 7885 | |
| 7886 | /* converter keyword. */ |
| 7887 | len = strlen("lua.") + strlen(name) + 1; |
| 7888 | akl->kw[0].kw = calloc(1, len); |
| 7889 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7890 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7891 | |
| 7892 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7893 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7894 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7895 | if (strcmp(env, "tcp") == 0) |
| 7896 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7897 | else if (strcmp(env, "http") == 0) |
| 7898 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7899 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7900 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7901 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7902 | |
| 7903 | akl->kw[0].match_pfx = 0; |
| 7904 | akl->kw[0].private = fcn; |
| 7905 | |
| 7906 | /* End of array. */ |
| 7907 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7908 | |
| 7909 | /* Register this new converter */ |
| 7910 | service_keywords_register(akl); |
| 7911 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7912 | return 0; |
| 7913 | } |
| 7914 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7915 | /* This function initialises Lua cli handler. It copies the |
| 7916 | * arguments in the Lua stack and create channel IO objects. |
| 7917 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7918 | 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] | 7919 | { |
| 7920 | struct hlua *hlua; |
| 7921 | struct hlua_function *fcn; |
| 7922 | int i; |
| 7923 | const char *error; |
| 7924 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7925 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7926 | appctx->ctx.hlua_cli.fcn = private; |
| 7927 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7928 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7929 | if (!hlua) { |
| 7930 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7931 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7932 | } |
| 7933 | HLUA_INIT(hlua); |
| 7934 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7935 | |
| 7936 | /* Create task used by signal to wakeup applets. |
| 7937 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7938 | * applet_http. It is absolutely compatible. |
| 7939 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7940 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7941 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7942 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7943 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7944 | } |
| 7945 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7946 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7947 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7948 | |
| 7949 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7950 | 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] | 7951 | 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] | 7952 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7953 | } |
| 7954 | |
| 7955 | /* The following Lua calls can fail. */ |
| 7956 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7957 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7958 | error = lua_tostring(hlua->T, -1); |
| 7959 | else |
| 7960 | error = "critical error"; |
| 7961 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7962 | goto error; |
| 7963 | } |
| 7964 | |
| 7965 | /* Check stack available size. */ |
| 7966 | if (!lua_checkstack(hlua->T, 2)) { |
| 7967 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7968 | goto error; |
| 7969 | } |
| 7970 | |
| 7971 | /* Restore the function in the stack. */ |
| 7972 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7973 | |
| 7974 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7975 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7976 | */ |
| 7977 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7978 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7979 | goto error; |
| 7980 | } |
| 7981 | hlua->nargs = 1; |
| 7982 | |
| 7983 | /* push keywords in the stack. */ |
| 7984 | for (i = 0; *args[i]; i++) { |
| 7985 | /* Check stack available size. */ |
| 7986 | if (!lua_checkstack(hlua->T, 1)) { |
| 7987 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7988 | goto error; |
| 7989 | } |
| 7990 | lua_pushstring(hlua->T, args[i]); |
| 7991 | hlua->nargs++; |
| 7992 | } |
| 7993 | |
| 7994 | /* We must initialize the execution timeouts. */ |
| 7995 | hlua->max_time = hlua_timeout_session; |
| 7996 | |
| 7997 | /* At this point the execution is safe. */ |
| 7998 | RESET_SAFE_LJMP(hlua->T); |
| 7999 | |
| 8000 | /* It's ok */ |
| 8001 | return 0; |
| 8002 | |
| 8003 | /* It's not ok. */ |
| 8004 | error: |
| 8005 | RESET_SAFE_LJMP(hlua->T); |
| 8006 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8007 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8008 | return 1; |
| 8009 | } |
| 8010 | |
| 8011 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8012 | { |
| 8013 | struct hlua *hlua; |
| 8014 | struct stream_interface *si; |
| 8015 | struct hlua_function *fcn; |
| 8016 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8017 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8018 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8019 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8020 | |
| 8021 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8022 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8023 | return 1; |
| 8024 | |
| 8025 | /* Execute the function. */ |
| 8026 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8027 | |
| 8028 | /* finished. */ |
| 8029 | case HLUA_E_OK: |
| 8030 | return 1; |
| 8031 | |
| 8032 | /* yield. */ |
| 8033 | case HLUA_E_AGAIN: |
| 8034 | /* We want write. */ |
| 8035 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8036 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8037 | /* Set the timeout. */ |
| 8038 | if (hlua->wake_time != TICK_ETERNITY) |
| 8039 | task_schedule(hlua->task, hlua->wake_time); |
| 8040 | return 0; |
| 8041 | |
| 8042 | /* finished with error. */ |
| 8043 | case HLUA_E_ERRMSG: |
| 8044 | /* Display log. */ |
| 8045 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8046 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8047 | lua_pop(hlua->T, 1); |
| 8048 | return 1; |
| 8049 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8050 | case HLUA_E_ETMOUT: |
| 8051 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8052 | fcn->name); |
| 8053 | return 1; |
| 8054 | |
| 8055 | case HLUA_E_NOMEM: |
| 8056 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8057 | fcn->name); |
| 8058 | return 1; |
| 8059 | |
| 8060 | case HLUA_E_YIELD: /* unexpected */ |
| 8061 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8062 | fcn->name); |
| 8063 | return 1; |
| 8064 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8065 | case HLUA_E_ERR: |
| 8066 | /* Display log. */ |
| 8067 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8068 | fcn->name); |
| 8069 | return 1; |
| 8070 | |
| 8071 | default: |
| 8072 | return 1; |
| 8073 | } |
| 8074 | |
| 8075 | return 1; |
| 8076 | } |
| 8077 | |
| 8078 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8079 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8080 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8081 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8082 | } |
| 8083 | |
| 8084 | /* This function is an LUA binding used for registering |
| 8085 | * new keywords in the cli. It expects a list of keywords |
| 8086 | * which are the "path". It is limited to 5 keywords. A |
| 8087 | * description of the command, a function to be executed |
| 8088 | * for the parsing and a function for io handlers. |
| 8089 | */ |
| 8090 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8091 | { |
| 8092 | struct cli_kw_list *cli_kws; |
| 8093 | const char *message; |
| 8094 | int ref_io; |
| 8095 | int len; |
| 8096 | struct hlua_function *fcn; |
| 8097 | int index; |
| 8098 | int i; |
| 8099 | |
| 8100 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8101 | |
| 8102 | /* First argument : an array of maximum 5 keywords. */ |
| 8103 | if (!lua_istable(L, 1)) |
| 8104 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8105 | |
| 8106 | /* Second argument : string with contextual message. */ |
| 8107 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8108 | |
| 8109 | /* Third and fourth argument : lua function. */ |
| 8110 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8111 | |
| 8112 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8113 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 8114 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8115 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8116 | fcn = calloc(1, sizeof(*fcn)); |
| 8117 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8118 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8119 | |
| 8120 | /* Fill path. */ |
| 8121 | index = 0; |
| 8122 | lua_pushnil(L); |
| 8123 | while(lua_next(L, 1) != 0) { |
| 8124 | if (index >= 5) |
| 8125 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8126 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8127 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8128 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 8129 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8130 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8131 | index++; |
| 8132 | lua_pop(L, 1); |
| 8133 | } |
| 8134 | |
| 8135 | /* Copy help message. */ |
| 8136 | cli_kws->kw[0].usage = strdup(message); |
| 8137 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8138 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8139 | |
| 8140 | /* Fill fcn io handler. */ |
| 8141 | len = strlen("<lua.cli>") + 1; |
| 8142 | for (i = 0; i < index; i++) |
| 8143 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8144 | fcn->name = calloc(1, len); |
| 8145 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8146 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8147 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8148 | for (i = 0; i < index; i++) { |
| 8149 | strncat((char *)fcn->name, ".", len); |
| 8150 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8151 | } |
| 8152 | strncat((char *)fcn->name, ">", len); |
| 8153 | fcn->function_ref = ref_io; |
| 8154 | |
| 8155 | /* Fill last entries. */ |
| 8156 | cli_kws->kw[0].private = fcn; |
| 8157 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8158 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8159 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8160 | |
| 8161 | /* Register this new converter */ |
| 8162 | cli_register_kw(cli_kws); |
| 8163 | |
| 8164 | return 0; |
| 8165 | } |
| 8166 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8167 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 8168 | struct proxy *defpx, const char *file, int line, |
| 8169 | char **err, unsigned int *timeout) |
| 8170 | { |
| 8171 | const char *error; |
| 8172 | |
| 8173 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8174 | if (error == PARSE_TIME_OVER) { |
| 8175 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8176 | args[1], args[0]); |
| 8177 | return -1; |
| 8178 | } |
| 8179 | else if (error == PARSE_TIME_UNDER) { |
| 8180 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8181 | args[1], args[0]); |
| 8182 | return -1; |
| 8183 | } |
| 8184 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8185 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8186 | return -1; |
| 8187 | } |
| 8188 | return 0; |
| 8189 | } |
| 8190 | |
| 8191 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 8192 | struct proxy *defpx, const char *file, int line, |
| 8193 | char **err) |
| 8194 | { |
| 8195 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8196 | file, line, err, &hlua_timeout_session); |
| 8197 | } |
| 8198 | |
| 8199 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 8200 | struct proxy *defpx, const char *file, int line, |
| 8201 | char **err) |
| 8202 | { |
| 8203 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8204 | file, line, err, &hlua_timeout_task); |
| 8205 | } |
| 8206 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8207 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 8208 | struct proxy *defpx, const char *file, int line, |
| 8209 | char **err) |
| 8210 | { |
| 8211 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8212 | file, line, err, &hlua_timeout_applet); |
| 8213 | } |
| 8214 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8215 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 8216 | struct proxy *defpx, const char *file, int line, |
| 8217 | char **err) |
| 8218 | { |
| 8219 | char *error; |
| 8220 | |
| 8221 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8222 | if (*error != '\0') { |
| 8223 | memprintf(err, "%s: invalid number", args[0]); |
| 8224 | return -1; |
| 8225 | } |
| 8226 | return 0; |
| 8227 | } |
| 8228 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8229 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 8230 | struct proxy *defpx, const char *file, int line, |
| 8231 | char **err) |
| 8232 | { |
| 8233 | char *error; |
| 8234 | |
| 8235 | if (*(args[1]) == 0) { |
| 8236 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8237 | return -1; |
| 8238 | } |
| 8239 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8240 | if (*error != '\0') { |
| 8241 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8242 | return -1; |
| 8243 | } |
| 8244 | return 0; |
| 8245 | } |
| 8246 | |
| 8247 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8248 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8249 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8250 | * the main lua entry point. |
| 8251 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8252 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8253 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8254 | * |
| 8255 | * In some error case, LUA set an error message in top of the stack. This function |
| 8256 | * 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] | 8257 | * |
| 8258 | * This function can fail with an abort() due to an Lua critical error. |
| 8259 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8260 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8261 | */ |
| 8262 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 8263 | struct proxy *defpx, const char *file, int line, |
| 8264 | char **err) |
| 8265 | { |
| 8266 | int error; |
| 8267 | |
| 8268 | /* Just load and compile the file. */ |
| 8269 | error = luaL_loadfile(gL.T, args[1]); |
| 8270 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8271 | 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] | 8272 | lua_pop(gL.T, 1); |
| 8273 | return -1; |
| 8274 | } |
| 8275 | |
| 8276 | /* If no syntax error where detected, execute the code. */ |
| 8277 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 8278 | switch (error) { |
| 8279 | case LUA_OK: |
| 8280 | break; |
| 8281 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8282 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8283 | lua_pop(gL.T, 1); |
| 8284 | return -1; |
| 8285 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8286 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8287 | return -1; |
| 8288 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8289 | 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] | 8290 | lua_pop(gL.T, 1); |
| 8291 | return -1; |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8292 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8293 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8294 | 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] | 8295 | lua_pop(gL.T, 1); |
| 8296 | return -1; |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8297 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8298 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8299 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8300 | lua_pop(gL.T, 1); |
| 8301 | return -1; |
| 8302 | } |
| 8303 | |
| 8304 | return 0; |
| 8305 | } |
| 8306 | |
| 8307 | /* configuration keywords declaration */ |
| 8308 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8309 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8310 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8311 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8312 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8313 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8314 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8315 | { 0, NULL, NULL }, |
| 8316 | }}; |
| 8317 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8318 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8319 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8320 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8321 | /* This function can fail with an abort() due to an Lua critical error. |
| 8322 | * We are in the initialisation process of HAProxy, this abort() is |
| 8323 | * tolerated. |
| 8324 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8325 | int hlua_post_init() |
| 8326 | { |
| 8327 | struct hlua_init_function *init; |
| 8328 | const char *msg; |
| 8329 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8330 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8331 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8332 | /* Call post initialisation function in safe environement. */ |
| 8333 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8334 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8335 | error = lua_tostring(gL.T, -1); |
| 8336 | else |
| 8337 | error = "critical error"; |
| 8338 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8339 | exit(1); |
| 8340 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8341 | |
| 8342 | #if USE_OPENSSL |
| 8343 | /* Initialize SSL server. */ |
| 8344 | if (socket_ssl.xprt->prepare_srv) { |
| 8345 | int saved_used_backed = global.ssl_used_backend; |
| 8346 | // don't affect maxconn automatic computation |
| 8347 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8348 | global.ssl_used_backend = saved_used_backed; |
| 8349 | } |
| 8350 | #endif |
| 8351 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8352 | hlua_fcn_post_init(gL.T); |
| 8353 | RESET_SAFE_LJMP(gL.T); |
| 8354 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8355 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8356 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 8357 | ret = hlua_ctx_resume(&gL, 0); |
| 8358 | switch (ret) { |
| 8359 | case HLUA_E_OK: |
| 8360 | lua_pop(gL.T, -1); |
| 8361 | return 1; |
| 8362 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8363 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8364 | return 0; |
| 8365 | case HLUA_E_ERRMSG: |
| 8366 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8367 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8368 | return 0; |
| 8369 | case HLUA_E_ERR: |
| 8370 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8371 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8372 | return 0; |
| 8373 | } |
| 8374 | } |
| 8375 | return 1; |
| 8376 | } |
| 8377 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8378 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8379 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8380 | * is the previously allocated size or the kind of object in case of a new |
| 8381 | * allocation. <nsize> is the requested new size. |
| 8382 | */ |
| 8383 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8384 | { |
| 8385 | struct hlua_mem_allocator *zone = ud; |
| 8386 | |
| 8387 | if (nsize == 0) { |
| 8388 | /* it's a free */ |
| 8389 | if (ptr) |
| 8390 | zone->allocated -= osize; |
| 8391 | free(ptr); |
| 8392 | return NULL; |
| 8393 | } |
| 8394 | |
| 8395 | if (!ptr) { |
| 8396 | /* it's a new allocation */ |
| 8397 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8398 | return NULL; |
| 8399 | |
| 8400 | ptr = malloc(nsize); |
| 8401 | if (ptr) |
| 8402 | zone->allocated += nsize; |
| 8403 | return ptr; |
| 8404 | } |
| 8405 | |
| 8406 | /* it's a realloc */ |
| 8407 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8408 | return NULL; |
| 8409 | |
| 8410 | ptr = realloc(ptr, nsize); |
| 8411 | if (ptr) |
| 8412 | zone->allocated += nsize - osize; |
| 8413 | return ptr; |
| 8414 | } |
| 8415 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8416 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8417 | * We are in the initialisation process of HAProxy, this abort() is |
| 8418 | * tolerated. |
| 8419 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8420 | void hlua_init(void) |
| 8421 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8422 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8423 | int idx; |
| 8424 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8425 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8426 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8427 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8428 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8429 | struct srv_kw *kw; |
| 8430 | int tmp_error; |
| 8431 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8432 | char *args[] = { /* SSL client configuration. */ |
| 8433 | "ssl", |
| 8434 | "verify", |
| 8435 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8436 | NULL |
| 8437 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8438 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8439 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8440 | /* Init main lua stack. */ |
| 8441 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8442 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8443 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8444 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8445 | hlua_sethlua(&gL); |
| 8446 | gL.Tref = LUA_REFNIL; |
| 8447 | gL.task = NULL; |
| 8448 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8449 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8450 | * the Lua function can fail with an abort. We are in the initialisation |
| 8451 | * process of HAProxy, this abort() is tolerated. |
| 8452 | */ |
| 8453 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8454 | /* Initialise lua. */ |
| 8455 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8456 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8457 | /* Set safe environment for the initialisation. */ |
| 8458 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8459 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8460 | error_msg = lua_tostring(gL.T, -1); |
| 8461 | else |
| 8462 | error_msg = "critical error"; |
| 8463 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8464 | exit(1); |
| 8465 | } |
| 8466 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8467 | /* |
| 8468 | * |
| 8469 | * Create "core" object. |
| 8470 | * |
| 8471 | */ |
| 8472 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8473 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8474 | lua_newtable(gL.T); |
| 8475 | |
| 8476 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8477 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8478 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8479 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8480 | /* Register special functions. */ |
| 8481 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8482 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8483 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8484 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8485 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8486 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8487 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8488 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8489 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8490 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8491 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8492 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8493 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8494 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8495 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8496 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8497 | hlua_class_function(gL.T, "log", hlua_log); |
| 8498 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8499 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8500 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8501 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8502 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8503 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8504 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8505 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8506 | |
| 8507 | /* |
| 8508 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8509 | * Register class Map |
| 8510 | * |
| 8511 | */ |
| 8512 | |
| 8513 | /* This table entry is the object "Map" base. */ |
| 8514 | lua_newtable(gL.T); |
| 8515 | |
| 8516 | /* register pattern types. */ |
| 8517 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8518 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8519 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8520 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8521 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8522 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8523 | |
| 8524 | /* register constructor. */ |
| 8525 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8526 | |
| 8527 | /* Create and fill the metatable. */ |
| 8528 | lua_newtable(gL.T); |
| 8529 | |
| 8530 | /* Create and fille the __index entry. */ |
| 8531 | lua_pushstring(gL.T, "__index"); |
| 8532 | lua_newtable(gL.T); |
| 8533 | |
| 8534 | /* Register . */ |
| 8535 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8536 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8537 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8538 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8539 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8540 | /* Register previous table in the registry with reference and named entry. |
| 8541 | * The function hlua_register_metatable() pops the stack, so we |
| 8542 | * previously create a copy of the table. |
| 8543 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8544 | 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] | 8545 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8546 | |
| 8547 | /* Assign the metatable to the mai Map object. */ |
| 8548 | lua_setmetatable(gL.T, -2); |
| 8549 | |
| 8550 | /* Set a name to the table. */ |
| 8551 | lua_setglobal(gL.T, "Map"); |
| 8552 | |
| 8553 | /* |
| 8554 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8555 | * Register class Channel |
| 8556 | * |
| 8557 | */ |
| 8558 | |
| 8559 | /* Create and fill the metatable. */ |
| 8560 | lua_newtable(gL.T); |
| 8561 | |
| 8562 | /* Create and fille the __index entry. */ |
| 8563 | lua_pushstring(gL.T, "__index"); |
| 8564 | lua_newtable(gL.T); |
| 8565 | |
| 8566 | /* Register . */ |
| 8567 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8568 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8569 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8570 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8571 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8572 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8573 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8574 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8575 | 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] | 8576 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8577 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8578 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8579 | |
| 8580 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8581 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8582 | |
| 8583 | /* |
| 8584 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8585 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8586 | * |
| 8587 | */ |
| 8588 | |
| 8589 | /* Create and fill the metatable. */ |
| 8590 | lua_newtable(gL.T); |
| 8591 | |
| 8592 | /* Create and fille the __index entry. */ |
| 8593 | lua_pushstring(gL.T, "__index"); |
| 8594 | lua_newtable(gL.T); |
| 8595 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8596 | /* Browse existing fetches and create the associated |
| 8597 | * object method. |
| 8598 | */ |
| 8599 | sf = NULL; |
| 8600 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8601 | |
| 8602 | /* Dont register the keywork if the arguments check function are |
| 8603 | * not safe during the runtime. |
| 8604 | */ |
| 8605 | if ((sf->val_args != NULL) && |
| 8606 | (sf->val_args != val_payload_lv) && |
| 8607 | (sf->val_args != val_hdr)) |
| 8608 | continue; |
| 8609 | |
| 8610 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8611 | * by an underscore. |
| 8612 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8613 | strncpy(trash.area, sf->kw, trash.size); |
| 8614 | trash.area[trash.size - 1] = '\0'; |
| 8615 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8616 | if (*p == '.' || *p == '-' || *p == '+') |
| 8617 | *p = '_'; |
| 8618 | |
| 8619 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8620 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8621 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8622 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8623 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8624 | } |
| 8625 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8626 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8627 | |
| 8628 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8629 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8630 | |
| 8631 | /* |
| 8632 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8633 | * Register class Converters |
| 8634 | * |
| 8635 | */ |
| 8636 | |
| 8637 | /* Create and fill the metatable. */ |
| 8638 | lua_newtable(gL.T); |
| 8639 | |
| 8640 | /* Create and fill the __index entry. */ |
| 8641 | lua_pushstring(gL.T, "__index"); |
| 8642 | lua_newtable(gL.T); |
| 8643 | |
| 8644 | /* Browse existing converters and create the associated |
| 8645 | * object method. |
| 8646 | */ |
| 8647 | sc = NULL; |
| 8648 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8649 | /* Dont register the keywork if the arguments check function are |
| 8650 | * not safe during the runtime. |
| 8651 | */ |
| 8652 | if (sc->val_args != NULL) |
| 8653 | continue; |
| 8654 | |
| 8655 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8656 | * by an underscore. |
| 8657 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8658 | strncpy(trash.area, sc->kw, trash.size); |
| 8659 | trash.area[trash.size - 1] = '\0'; |
| 8660 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8661 | if (*p == '.' || *p == '-' || *p == '+') |
| 8662 | *p = '_'; |
| 8663 | |
| 8664 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8665 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8666 | lua_pushlightuserdata(gL.T, sc); |
| 8667 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8668 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8669 | } |
| 8670 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8671 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8672 | |
| 8673 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8674 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8675 | |
| 8676 | /* |
| 8677 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8678 | * Register class HTTP |
| 8679 | * |
| 8680 | */ |
| 8681 | |
| 8682 | /* Create and fill the metatable. */ |
| 8683 | lua_newtable(gL.T); |
| 8684 | |
| 8685 | /* Create and fille the __index entry. */ |
| 8686 | lua_pushstring(gL.T, "__index"); |
| 8687 | lua_newtable(gL.T); |
| 8688 | |
| 8689 | /* Register Lua functions. */ |
| 8690 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8691 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8692 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8693 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8694 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8695 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8696 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8697 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8698 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8699 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8700 | |
| 8701 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8702 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8703 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8704 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8705 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8706 | 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] | 8707 | 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] | 8708 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8709 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8710 | |
| 8711 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8712 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8713 | |
| 8714 | /* |
| 8715 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8716 | * Register class AppletTCP |
| 8717 | * |
| 8718 | */ |
| 8719 | |
| 8720 | /* Create and fill the metatable. */ |
| 8721 | lua_newtable(gL.T); |
| 8722 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8723 | /* Create and fille the __index entry. */ |
| 8724 | lua_pushstring(gL.T, "__index"); |
| 8725 | lua_newtable(gL.T); |
| 8726 | |
| 8727 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8728 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8729 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8730 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8731 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8732 | 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] | 8733 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8734 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8735 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8736 | |
| 8737 | lua_settable(gL.T, -3); |
| 8738 | |
| 8739 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8740 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8741 | |
| 8742 | /* |
| 8743 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8744 | * Register class AppletHTTP |
| 8745 | * |
| 8746 | */ |
| 8747 | |
| 8748 | /* Create and fill the metatable. */ |
| 8749 | lua_newtable(gL.T); |
| 8750 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8751 | /* Create and fille the __index entry. */ |
| 8752 | lua_pushstring(gL.T, "__index"); |
| 8753 | lua_newtable(gL.T); |
| 8754 | |
| 8755 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8756 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8757 | 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] | 8758 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8759 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8760 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8761 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8762 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8763 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8764 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8765 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8766 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8767 | |
| 8768 | lua_settable(gL.T, -3); |
| 8769 | |
| 8770 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8771 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8772 | |
| 8773 | /* |
| 8774 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8775 | * Register class TXN |
| 8776 | * |
| 8777 | */ |
| 8778 | |
| 8779 | /* Create and fill the metatable. */ |
| 8780 | lua_newtable(gL.T); |
| 8781 | |
| 8782 | /* Create and fille the __index entry. */ |
| 8783 | lua_pushstring(gL.T, "__index"); |
| 8784 | lua_newtable(gL.T); |
| 8785 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8786 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8787 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8788 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8789 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8790 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8791 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8792 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 8793 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8794 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8795 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8796 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8797 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8798 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8799 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8800 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8801 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8802 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8803 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8804 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8805 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8806 | |
| 8807 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8808 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8809 | |
| 8810 | /* |
| 8811 | * |
| 8812 | * Register class Socket |
| 8813 | * |
| 8814 | */ |
| 8815 | |
| 8816 | /* Create and fill the metatable. */ |
| 8817 | lua_newtable(gL.T); |
| 8818 | |
| 8819 | /* Create and fille the __index entry. */ |
| 8820 | lua_pushstring(gL.T, "__index"); |
| 8821 | lua_newtable(gL.T); |
| 8822 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8823 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8824 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8825 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8826 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8827 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8828 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8829 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8830 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8831 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8832 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8833 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8834 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8835 | 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] | 8836 | |
| 8837 | /* Register the garbage collector entry. */ |
| 8838 | lua_pushstring(gL.T, "__gc"); |
| 8839 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8840 | 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] | 8841 | |
| 8842 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8843 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8844 | |
| 8845 | /* Proxy and server configuration initialisation. */ |
| 8846 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8847 | init_new_proxy(&socket_proxy); |
| 8848 | socket_proxy.parent = NULL; |
| 8849 | socket_proxy.last_change = now.tv_sec; |
| 8850 | socket_proxy.id = "LUA-SOCKET"; |
| 8851 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8852 | socket_proxy.maxconn = 0; |
| 8853 | socket_proxy.accept = NULL; |
| 8854 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8855 | socket_proxy.srv = NULL; |
| 8856 | socket_proxy.conn_retries = 0; |
| 8857 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8858 | |
| 8859 | /* Init TCP server: unchanged parameters */ |
| 8860 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8861 | socket_tcp.next = NULL; |
| 8862 | socket_tcp.proxy = &socket_proxy; |
| 8863 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8864 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8865 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8866 | socket_tcp.priv_conns = NULL; |
| 8867 | socket_tcp.idle_conns = NULL; |
| 8868 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8869 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8870 | socket_tcp.last_change = 0; |
| 8871 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8872 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8873 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8874 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8875 | |
| 8876 | /* XXX: Copy default parameter from default server, |
| 8877 | * but the default server is not initialized. |
| 8878 | */ |
| 8879 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8880 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8881 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8882 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8883 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8884 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8885 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8886 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8887 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8888 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8889 | |
| 8890 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8891 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8892 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8893 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8894 | socket_tcp.check.server = &socket_tcp; |
| 8895 | |
| 8896 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8897 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8898 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8899 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8900 | socket_tcp.agent.server = &socket_tcp; |
| 8901 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8902 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8903 | |
| 8904 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8905 | /* Init TCP server: unchanged parameters */ |
| 8906 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8907 | socket_ssl.next = NULL; |
| 8908 | socket_ssl.proxy = &socket_proxy; |
| 8909 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8910 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8911 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8912 | socket_ssl.priv_conns = NULL; |
| 8913 | socket_ssl.idle_conns = NULL; |
| 8914 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8915 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8916 | socket_ssl.last_change = 0; |
| 8917 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8918 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8919 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8920 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8921 | |
| 8922 | /* XXX: Copy default parameter from default server, |
| 8923 | * but the default server is not initialized. |
| 8924 | */ |
| 8925 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8926 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8927 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8928 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8929 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8930 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8931 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8932 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8933 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8934 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8935 | |
| 8936 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8937 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8938 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8939 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8940 | socket_ssl.check.server = &socket_ssl; |
| 8941 | |
| 8942 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8943 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8944 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8945 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8946 | socket_ssl.agent.server = &socket_ssl; |
| 8947 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8948 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8949 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8950 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8951 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8952 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8953 | /* |
| 8954 | * |
| 8955 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8956 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8957 | * features like client certificates and ssl_verify. |
| 8958 | * |
| 8959 | */ |
| 8960 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8961 | if (tmp_error != 0) { |
| 8962 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8963 | abort(); /* This must be never arrives because the command line |
| 8964 | not editable by the user. */ |
| 8965 | } |
| 8966 | idx += kw->skip; |
| 8967 | } |
| 8968 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8969 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8970 | |
| 8971 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8972 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8973 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8974 | static void hlua_register_build_options(void) |
| 8975 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8976 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8977 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8978 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8979 | hap_register_build_opts(ptr, 1); |
| 8980 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8981 | |
| 8982 | INITCALL0(STG_REGISTER, hlua_register_build_options); |