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. */ |
| 157 | #define APPLET_100C 0x02 /* 100 continue expected. */ |
| 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. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 162 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 163 | /* The main Lua execution context. */ |
| 164 | struct hlua gL; |
| 165 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 166 | /* This is the memory pool containing struct lua for applets |
| 167 | * (including cli). |
| 168 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 169 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 170 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 171 | /* Used for Socket connection. */ |
| 172 | static struct proxy socket_proxy; |
| 173 | static struct server socket_tcp; |
| 174 | #ifdef USE_OPENSSL |
| 175 | static struct server socket_ssl; |
| 176 | #endif |
| 177 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 178 | /* List head of the function called at the initialisation time. */ |
| 179 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 180 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 181 | /* The following variables contains the reference of the different |
| 182 | * Lua classes. These references are useful for identify metadata |
| 183 | * associated with an object. |
| 184 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 185 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 186 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 187 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 188 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 189 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 190 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 191 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 192 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 193 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 194 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 195 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 196 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 197 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 198 | * because a task may remain alive during all the haproxy execution. |
| 199 | */ |
| 200 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 201 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 202 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 203 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 204 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 205 | * it is used for preventing infinite loops. |
| 206 | * |
| 207 | * I test the scheer with an infinite loop containing one incrementation |
| 208 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 209 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 210 | * to one interrupt each 10 000 instructions. |
| 211 | * |
| 212 | * configured | Number of |
| 213 | * instructions | loops executed |
| 214 | * between two | in milions |
| 215 | * forced yields | |
| 216 | * ---------------+--------------- |
| 217 | * 10 | 160 |
| 218 | * 500 | 670 |
| 219 | * 1000 | 680 |
| 220 | * 5000 | 700 |
| 221 | * 7000 | 700 |
| 222 | * 8000 | 700 |
| 223 | * 9000 | 710 <- ceil |
| 224 | * 10000 | 710 |
| 225 | * 100000 | 710 |
| 226 | * 1000000 | 710 |
| 227 | * |
| 228 | */ |
| 229 | static unsigned int hlua_nb_instruction = 10000; |
| 230 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 231 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 232 | * be enforced on any memory allocation. |
| 233 | */ |
| 234 | struct hlua_mem_allocator { |
| 235 | size_t allocated; |
| 236 | size_t limit; |
| 237 | }; |
| 238 | |
| 239 | static struct hlua_mem_allocator hlua_global_allocator; |
| 240 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 241 | static const char error_500[] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 242 | "HTTP/1.0 500 Internal Server Error\r\n" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 243 | "Cache-Control: no-cache\r\n" |
| 244 | "Connection: close\r\n" |
| 245 | "Content-Type: text/html\r\n" |
| 246 | "\r\n" |
Joseph Herlant | 7fe1577 | 2018-11-15 10:07:32 -0800 | [diff] [blame] | 247 | "<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] | 248 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 249 | /* These functions converts types between HAProxy internal args or |
| 250 | * sample and LUA types. Another function permits to check if the |
| 251 | * LUA stack contains arguments according with an required ARG_T |
| 252 | * format. |
| 253 | */ |
| 254 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 255 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 256 | __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] | 257 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 258 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 259 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 260 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 261 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 262 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 263 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 264 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 265 | do { \ |
| 266 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 267 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 268 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 269 | } while (0) |
| 270 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 271 | /* Used to check an Lua function type in the stack. It creates and |
| 272 | * returns a reference of the function. This function throws an |
| 273 | * error if the rgument is not a "function". |
| 274 | */ |
| 275 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 276 | { |
| 277 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 278 | 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] | 279 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 280 | } |
| 281 | lua_pushvalue(L, argno); |
| 282 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 283 | } |
| 284 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 285 | /* Return the string that is of the top of the stack. */ |
| 286 | const char *hlua_get_top_error_string(lua_State *L) |
| 287 | { |
| 288 | if (lua_gettop(L) < 1) |
| 289 | return "unknown error"; |
| 290 | if (lua_type(L, -1) != LUA_TSTRING) |
| 291 | return "unknown error"; |
| 292 | return lua_tostring(L, -1); |
| 293 | } |
| 294 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 295 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 296 | { |
| 297 | lua_Debug ar; |
| 298 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 299 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 300 | int filled = 0; |
| 301 | |
| 302 | while (lua_getstack(L, level++, &ar)) { |
| 303 | |
| 304 | /* Add separator */ |
| 305 | if (filled) |
| 306 | chunk_appendf(msg, ", "); |
| 307 | filled = 1; |
| 308 | |
| 309 | /* Fill fields: |
| 310 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 311 | * 'l': fills in the field currentline; |
| 312 | * 'n': fills in the field name and namewhat; |
| 313 | * 't': fills in the field istailcall; |
| 314 | */ |
| 315 | lua_getinfo(L, "Slnt", &ar); |
| 316 | |
| 317 | /* Append code localisation */ |
| 318 | if (ar.currentline > 0) |
| 319 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 320 | else |
| 321 | chunk_appendf(msg, "%s ", ar.short_src); |
| 322 | |
| 323 | /* |
| 324 | * Get function name |
| 325 | * |
| 326 | * if namewhat is no empty, name is defined. |
| 327 | * what contains "Lua" for Lua function, "C" for C function, |
| 328 | * or "main" for main code. |
| 329 | */ |
| 330 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 331 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 332 | |
| 333 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 334 | chunk_appendf(msg, "main chunk"); |
| 335 | |
| 336 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 337 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 338 | |
| 339 | else /* nothing left... */ |
| 340 | chunk_appendf(msg, "?"); |
| 341 | |
| 342 | |
| 343 | /* Display tailed call */ |
| 344 | if (ar.istailcall) |
| 345 | chunk_appendf(msg, " ..."); |
| 346 | } |
| 347 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 348 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 352 | /* This function check the number of arguments available in the |
| 353 | * stack. If the number of arguments available is not the same |
| 354 | * then <nb> an error is throwed. |
| 355 | */ |
| 356 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 357 | { |
| 358 | if (lua_gettop(L) == nb) |
| 359 | return; |
| 360 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 361 | } |
| 362 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 363 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 364 | * and the line number where the error is encountered. |
| 365 | */ |
| 366 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 367 | { |
| 368 | va_list argp; |
| 369 | va_start(argp, fmt); |
| 370 | luaL_where(L, 1); |
| 371 | lua_pushvfstring(L, fmt, argp); |
| 372 | va_end(argp); |
| 373 | lua_concat(L, 2); |
| 374 | return 1; |
| 375 | } |
| 376 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 377 | /* This functions is used with sample fetch and converters. It |
| 378 | * converts the HAProxy configuration argument in a lua stack |
| 379 | * values. |
| 380 | * |
| 381 | * It takes an array of "arg", and each entry of the array is |
| 382 | * converted and pushed in the LUA stack. |
| 383 | */ |
| 384 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 385 | { |
| 386 | switch (arg->type) { |
| 387 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 388 | case ARGT_TIME: |
| 389 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 390 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 391 | break; |
| 392 | |
| 393 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 394 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 395 | break; |
| 396 | |
| 397 | case ARGT_IPV4: |
| 398 | case ARGT_IPV6: |
| 399 | case ARGT_MSK4: |
| 400 | case ARGT_MSK6: |
| 401 | case ARGT_FE: |
| 402 | case ARGT_BE: |
| 403 | case ARGT_TAB: |
| 404 | case ARGT_SRV: |
| 405 | case ARGT_USR: |
| 406 | case ARGT_MAP: |
| 407 | default: |
| 408 | lua_pushnil(L); |
| 409 | break; |
| 410 | } |
| 411 | return 1; |
| 412 | } |
| 413 | |
| 414 | /* This function take one entrie in an LUA stack at the index "ud", |
| 415 | * and try to convert it in an HAProxy argument entry. This is useful |
| 416 | * with sample fetch wrappers. The input arguments are gived to the |
| 417 | * lua wrapper and converted as arg list by thi function. |
| 418 | */ |
| 419 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 420 | { |
| 421 | switch (lua_type(L, ud)) { |
| 422 | |
| 423 | case LUA_TNUMBER: |
| 424 | case LUA_TBOOLEAN: |
| 425 | arg->type = ARGT_SINT; |
| 426 | arg->data.sint = lua_tointeger(L, ud); |
| 427 | break; |
| 428 | |
| 429 | case LUA_TSTRING: |
| 430 | arg->type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 431 | arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 432 | break; |
| 433 | |
| 434 | case LUA_TUSERDATA: |
| 435 | case LUA_TNIL: |
| 436 | case LUA_TTABLE: |
| 437 | case LUA_TFUNCTION: |
| 438 | case LUA_TTHREAD: |
| 439 | case LUA_TLIGHTUSERDATA: |
| 440 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 441 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 442 | break; |
| 443 | } |
| 444 | return 1; |
| 445 | } |
| 446 | |
| 447 | /* the following functions are used to convert a struct sample |
| 448 | * in Lua type. This useful to convert the return of the |
| 449 | * fetchs or converters. |
| 450 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 451 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 452 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 453 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 454 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 455 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 456 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 457 | break; |
| 458 | |
| 459 | case SMP_T_BIN: |
| 460 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 461 | 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] | 462 | break; |
| 463 | |
| 464 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 465 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 466 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 467 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 468 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 469 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 470 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 471 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 472 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 473 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 474 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 475 | 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] | 476 | break; |
| 477 | default: |
| 478 | lua_pushnil(L); |
| 479 | break; |
| 480 | } |
| 481 | break; |
| 482 | |
| 483 | case SMP_T_IPV4: |
| 484 | case SMP_T_IPV6: |
| 485 | 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] | 486 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 487 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 488 | 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] | 489 | else |
| 490 | lua_pushnil(L); |
| 491 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 492 | default: |
| 493 | lua_pushnil(L); |
| 494 | break; |
| 495 | } |
| 496 | return 1; |
| 497 | } |
| 498 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 499 | /* the following functions are used to convert a struct sample |
| 500 | * in Lua strings. This is useful to convert the return of the |
| 501 | * fetchs or converters. |
| 502 | */ |
| 503 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 504 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 505 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 506 | |
| 507 | case SMP_T_BIN: |
| 508 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 509 | 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] | 510 | break; |
| 511 | |
| 512 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 513 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 514 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 515 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 516 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 517 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 518 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 519 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 520 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 521 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 522 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 523 | 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] | 524 | break; |
| 525 | default: |
| 526 | lua_pushstring(L, ""); |
| 527 | break; |
| 528 | } |
| 529 | break; |
| 530 | |
| 531 | case SMP_T_SINT: |
| 532 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 533 | case SMP_T_IPV4: |
| 534 | case SMP_T_IPV6: |
| 535 | 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] | 536 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 537 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 538 | 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] | 539 | else |
| 540 | lua_pushstring(L, ""); |
| 541 | break; |
| 542 | default: |
| 543 | lua_pushstring(L, ""); |
| 544 | break; |
| 545 | } |
| 546 | return 1; |
| 547 | } |
| 548 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 549 | /* the following functions are used to convert an Lua type in a |
| 550 | * struct sample. This is useful to provide data from a converter |
| 551 | * to the LUA code. |
| 552 | */ |
| 553 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 554 | { |
| 555 | switch (lua_type(L, ud)) { |
| 556 | |
| 557 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 558 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 559 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 560 | break; |
| 561 | |
| 562 | |
| 563 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 564 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 565 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 566 | break; |
| 567 | |
| 568 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 569 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 570 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 571 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 572 | break; |
| 573 | |
| 574 | case LUA_TUSERDATA: |
| 575 | case LUA_TNIL: |
| 576 | case LUA_TTABLE: |
| 577 | case LUA_TFUNCTION: |
| 578 | case LUA_TTHREAD: |
| 579 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 580 | case LUA_TNONE: |
| 581 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 582 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 583 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 584 | break; |
| 585 | } |
| 586 | return 1; |
| 587 | } |
| 588 | |
| 589 | /* This function check the "argp" builded by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 590 | * 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] | 591 | * 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] | 592 | * |
| 593 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 594 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 595 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 596 | __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] | 597 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 598 | { |
| 599 | int min_arg; |
| 600 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 601 | struct proxy *px; |
| 602 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 603 | |
| 604 | idx = 0; |
| 605 | min_arg = ARGM(mask); |
| 606 | mask >>= ARGM_BITS; |
| 607 | |
| 608 | while (1) { |
| 609 | |
| 610 | /* Check oversize. */ |
| 611 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 612 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /* Check for mandatory arguments. */ |
| 616 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 617 | if (idx < min_arg) { |
| 618 | |
| 619 | /* If miss other argument than the first one, we return an error. */ |
| 620 | if (idx > 0) |
| 621 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 622 | |
| 623 | /* If first argument have a certain type, some default values |
| 624 | * may be used. See the function smp_resolve_args(). |
| 625 | */ |
| 626 | switch (mask & ARGT_MASK) { |
| 627 | |
| 628 | case ARGT_FE: |
| 629 | if (!(p->cap & PR_CAP_FE)) |
| 630 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 631 | argp[idx].data.prx = p; |
| 632 | argp[idx].type = ARGT_FE; |
| 633 | argp[idx+1].type = ARGT_STOP; |
| 634 | break; |
| 635 | |
| 636 | case ARGT_BE: |
| 637 | if (!(p->cap & PR_CAP_BE)) |
| 638 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 639 | argp[idx].data.prx = p; |
| 640 | argp[idx].type = ARGT_BE; |
| 641 | argp[idx+1].type = ARGT_STOP; |
| 642 | break; |
| 643 | |
| 644 | case ARGT_TAB: |
| 645 | argp[idx].data.prx = p; |
| 646 | argp[idx].type = ARGT_TAB; |
| 647 | argp[idx+1].type = ARGT_STOP; |
| 648 | break; |
| 649 | |
| 650 | default: |
| 651 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 652 | break; |
| 653 | } |
| 654 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | /* Check for exceed the number of requiered argument. */ |
| 659 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 660 | argp[idx].type != ARGT_STOP) { |
| 661 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 662 | } |
| 663 | |
| 664 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 665 | argp[idx].type == ARGT_STOP) { |
| 666 | return 0; |
| 667 | } |
| 668 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 669 | /* Convert some argument types. */ |
| 670 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 671 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 672 | if (argp[idx].type != ARGT_SINT) |
| 673 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 674 | argp[idx].type = ARGT_SINT; |
| 675 | break; |
| 676 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 677 | case ARGT_TIME: |
| 678 | if (argp[idx].type != ARGT_SINT) |
| 679 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 680 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 681 | break; |
| 682 | |
| 683 | case ARGT_SIZE: |
| 684 | if (argp[idx].type != ARGT_SINT) |
| 685 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 686 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 687 | break; |
| 688 | |
| 689 | case ARGT_FE: |
| 690 | if (argp[idx].type != ARGT_STR) |
| 691 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 692 | memcpy(trash.area, argp[idx].data.str.area, |
| 693 | argp[idx].data.str.data); |
| 694 | trash.area[argp[idx].data.str.data] = 0; |
| 695 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 696 | if (!argp[idx].data.prx) |
| 697 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 698 | argp[idx].type = ARGT_FE; |
| 699 | break; |
| 700 | |
| 701 | case ARGT_BE: |
| 702 | if (argp[idx].type != ARGT_STR) |
| 703 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 704 | memcpy(trash.area, argp[idx].data.str.area, |
| 705 | argp[idx].data.str.data); |
| 706 | trash.area[argp[idx].data.str.data] = 0; |
| 707 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 708 | if (!argp[idx].data.prx) |
| 709 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 710 | argp[idx].type = ARGT_BE; |
| 711 | break; |
| 712 | |
| 713 | case ARGT_TAB: |
| 714 | if (argp[idx].type != ARGT_STR) |
| 715 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 716 | memcpy(trash.area, argp[idx].data.str.area, |
| 717 | argp[idx].data.str.data); |
| 718 | trash.area[argp[idx].data.str.data] = 0; |
| 719 | argp[idx].data.prx = proxy_tbl_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 720 | if (!argp[idx].data.prx) |
| 721 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 722 | argp[idx].type = ARGT_TAB; |
| 723 | break; |
| 724 | |
| 725 | case ARGT_SRV: |
| 726 | if (argp[idx].type != ARGT_STR) |
| 727 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 728 | memcpy(trash.area, argp[idx].data.str.area, |
| 729 | argp[idx].data.str.data); |
| 730 | trash.area[argp[idx].data.str.data] = 0; |
| 731 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 732 | if (sname) { |
| 733 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 734 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 735 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 736 | if (!px) |
| 737 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 738 | } |
| 739 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 740 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 741 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 742 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 743 | argp[idx].data.srv = findserver(px, sname); |
| 744 | if (!argp[idx].data.srv) |
| 745 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 746 | argp[idx].type = ARGT_SRV; |
| 747 | break; |
| 748 | |
| 749 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 750 | memcpy(trash.area, argp[idx].data.str.area, |
| 751 | argp[idx].data.str.data); |
| 752 | trash.area[argp[idx].data.str.data] = 0; |
| 753 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 754 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 755 | argp[idx].type = ARGT_IPV4; |
| 756 | break; |
| 757 | |
| 758 | case ARGT_MSK4: |
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 (!str2mask(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 mask")); |
| 764 | argp[idx].type = ARGT_MSK4; |
| 765 | break; |
| 766 | |
| 767 | case ARGT_IPV6: |
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 (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 772 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 773 | argp[idx].type = ARGT_IPV6; |
| 774 | break; |
| 775 | |
| 776 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 777 | memcpy(trash.area, argp[idx].data.str.area, |
| 778 | argp[idx].data.str.data); |
| 779 | trash.area[argp[idx].data.str.data] = 0; |
| 780 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 781 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 782 | argp[idx].type = ARGT_MSK6; |
| 783 | break; |
| 784 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 785 | case ARGT_MAP: |
| 786 | case ARGT_REG: |
| 787 | case ARGT_USR: |
| 788 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 789 | break; |
| 790 | } |
| 791 | |
| 792 | /* Check for type of argument. */ |
| 793 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 794 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 795 | arg_type_names[(mask & ARGT_MASK)], |
| 796 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 797 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 798 | } |
| 799 | |
| 800 | /* Next argument. */ |
| 801 | mask >>= ARGT_BITS; |
| 802 | idx++; |
| 803 | } |
| 804 | } |
| 805 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 806 | /* |
| 807 | * The following functions are used to make correspondance between the the |
| 808 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 809 | * |
| 810 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 811 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 812 | */ |
| 813 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 814 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 815 | struct hlua **hlua = lua_getextraspace(L); |
| 816 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 817 | } |
| 818 | static inline void hlua_sethlua(struct hlua *hlua) |
| 819 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 820 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 821 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 822 | } |
| 823 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 824 | /* This function is used to send logs. It try to send on screen (stderr) |
| 825 | * and on the default syslog server. |
| 826 | */ |
| 827 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 828 | { |
| 829 | struct tm tm; |
| 830 | char *p; |
| 831 | |
| 832 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 833 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 834 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 835 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 836 | /* Break the message if exceed the buffer size. */ |
| 837 | *(p-4) = ' '; |
| 838 | *(p-3) = '.'; |
| 839 | *(p-2) = '.'; |
| 840 | *(p-1) = '.'; |
| 841 | break; |
| 842 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 843 | if (isprint(*msg)) |
| 844 | *p = *msg; |
| 845 | else |
| 846 | *p = '.'; |
| 847 | } |
| 848 | *p = '\0'; |
| 849 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 850 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 851 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 852 | get_localtime(date.tv_sec, &tm); |
| 853 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 854 | 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] | 855 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 856 | fflush(stderr); |
| 857 | } |
| 858 | } |
| 859 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 860 | /* This function just ensure that the yield will be always |
| 861 | * returned with a timeout and permit to set some flags |
| 862 | */ |
| 863 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 864 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 865 | { |
| 866 | struct hlua *hlua = hlua_gethlua(L); |
| 867 | |
| 868 | /* Set the wake timeout. If timeout is required, we set |
| 869 | * the expiration time. |
| 870 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 871 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 872 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 873 | hlua->flags |= flags; |
| 874 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 875 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 876 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 877 | } |
| 878 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 879 | /* This function initialises the Lua environment stored in the stream. |
| 880 | * 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] | 881 | * 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] | 882 | * |
| 883 | * This function is particular. it initialises a new Lua thread. If the |
| 884 | * initialisation fails (example: out of memory error), the lua function |
| 885 | * throws an error (longjmp). |
| 886 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 887 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 888 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 889 | * MUST NOT manipulate the created thread stack state, because it is not |
| 890 | * proctected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 891 | */ |
| 892 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 893 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 894 | if (!SET_SAFE_LJMP(gL.T)) { |
| 895 | lua->Tref = LUA_REFNIL; |
| 896 | return 0; |
| 897 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 898 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 899 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 900 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 901 | lua->T = lua_newthread(gL.T); |
| 902 | if (!lua->T) { |
| 903 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | 0a97620 | 2017-07-12 11:18:00 +0200 | [diff] [blame] | 904 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 905 | return 0; |
| 906 | } |
| 907 | hlua_sethlua(lua); |
| 908 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 909 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 910 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 911 | return 1; |
| 912 | } |
| 913 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 914 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 915 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 916 | * is not freed. |
| 917 | */ |
| 918 | void hlua_ctx_destroy(struct hlua *lua) |
| 919 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 920 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 921 | return; |
| 922 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 923 | if (!lua->T) |
| 924 | goto end; |
| 925 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 926 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 927 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 928 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 929 | if (!SET_SAFE_LJMP(lua->T)) |
| 930 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 931 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 932 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 933 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 934 | if (!SET_SAFE_LJMP(gL.T)) |
| 935 | return; |
| 936 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 937 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 938 | /* Forces a garbage collecting process. If the Lua program is finished |
| 939 | * without error, we run the GC on the thread pointer. Its freed all |
| 940 | * the unused memory. |
| 941 | * If the thread is finnish with an error or is currently yielded, |
| 942 | * it seems that the GC applied on the thread doesn't clean anything, |
| 943 | * so e run the GC on the main thread. |
| 944 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 945 | * the garbage collection. |
| 946 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 947 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 948 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 949 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 950 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 951 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 952 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 953 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 954 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 955 | |
| 956 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 957 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /* This function is used to restore the Lua context when a coroutine |
| 961 | * fails. This function copy the common memory between old coroutine |
| 962 | * and the new coroutine. The old coroutine is destroyed, and its |
| 963 | * replaced by the new coroutine. |
| 964 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 965 | * as string error message and it is copied in the new stack. |
| 966 | */ |
| 967 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 968 | { |
| 969 | lua_State *T; |
| 970 | int new_ref; |
| 971 | |
| 972 | /* Renew the main LUA stack doesn't have sense. */ |
| 973 | if (lua == &gL) |
| 974 | return 0; |
| 975 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 976 | /* New Lua coroutine. */ |
| 977 | T = lua_newthread(gL.T); |
| 978 | if (!T) |
| 979 | return 0; |
| 980 | |
| 981 | /* Copy last error message. */ |
| 982 | if (keep_msg) |
| 983 | lua_xmove(lua->T, T, 1); |
| 984 | |
| 985 | /* Copy data between the coroutines. */ |
| 986 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 987 | lua_xmove(lua->T, T, 1); |
| 988 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 989 | |
| 990 | /* Destroy old data. */ |
| 991 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 992 | |
| 993 | /* The thread is garbage collected by Lua. */ |
| 994 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 995 | |
| 996 | /* Fill the struct with the new coroutine values. */ |
| 997 | lua->Mref = new_ref; |
| 998 | lua->T = T; |
| 999 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1000 | |
| 1001 | /* Set context. */ |
| 1002 | hlua_sethlua(lua); |
| 1003 | |
| 1004 | return 1; |
| 1005 | } |
| 1006 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1007 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1008 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1009 | struct hlua *hlua = hlua_gethlua(L); |
| 1010 | |
| 1011 | /* Lua cannot yield when its returning from a function, |
| 1012 | * so, we can fix the interrupt hook to 1 instruction, |
| 1013 | * expecting that the function is finnished. |
| 1014 | */ |
| 1015 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1016 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1017 | return; |
| 1018 | } |
| 1019 | |
| 1020 | /* restore the interrupt condition. */ |
| 1021 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1022 | |
| 1023 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1024 | * If the state is not yieldable, trying yield causes an error. |
| 1025 | */ |
| 1026 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1027 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1028 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1029 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1030 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1031 | hlua->run_time += now_ms - hlua->start_time; |
| 1032 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1033 | lua_pushfstring(L, "execution timeout"); |
| 1034 | WILL_LJMP(lua_error(L)); |
| 1035 | } |
| 1036 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1037 | /* Update the start time. */ |
| 1038 | hlua->start_time = now_ms; |
| 1039 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1040 | /* Try to interrupt the process at the end of the current |
| 1041 | * unyieldable function. |
| 1042 | */ |
| 1043 | 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] | 1044 | } |
| 1045 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1046 | /* This function start or resumes the Lua stack execution. If the flag |
| 1047 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1048 | * The function return an error. |
| 1049 | * |
| 1050 | * The function can returns 4 values: |
| 1051 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1052 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1053 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1054 | * - 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] | 1055 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1056 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1057 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1058 | * 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] | 1059 | * LUA code. |
| 1060 | */ |
| 1061 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1062 | { |
| 1063 | int ret; |
| 1064 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1065 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1066 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1067 | /* Initialise run time counter. */ |
| 1068 | if (!HLUA_IS_RUNNING(lua)) |
| 1069 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1070 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1071 | /* Lock the whole Lua execution. This lock must be before the |
| 1072 | * label "resume_execution". |
| 1073 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1074 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1075 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1076 | resume_execution: |
| 1077 | |
| 1078 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1079 | * instructions. it is used for preventing infinite loops. |
| 1080 | */ |
| 1081 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1082 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1083 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1084 | HLUA_SET_RUN(lua); |
| 1085 | HLUA_CLR_CTRLYIELD(lua); |
| 1086 | HLUA_CLR_WAKERESWR(lua); |
| 1087 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1088 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1089 | /* Update the start time. */ |
| 1090 | lua->start_time = now_ms; |
| 1091 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1092 | /* Call the function. */ |
| 1093 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1094 | switch (ret) { |
| 1095 | |
| 1096 | case LUA_OK: |
| 1097 | ret = HLUA_E_OK; |
| 1098 | break; |
| 1099 | |
| 1100 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1101 | /* Check if the execution timeout is expired. It it is the case, we |
| 1102 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1103 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1104 | tv_update_date(0, 1); |
| 1105 | lua->run_time += now_ms - lua->start_time; |
| 1106 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1107 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1108 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1109 | break; |
| 1110 | } |
| 1111 | /* Process the forced yield. if the general yield is not allowed or |
| 1112 | * if no task were associated this the current Lua execution |
| 1113 | * coroutine, we resume the execution. Else we want to return in the |
| 1114 | * scheduler and we want to be waked up again, to continue the |
| 1115 | * current Lua execution. So we schedule our own task. |
| 1116 | */ |
| 1117 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1118 | if (!yield_allowed || !lua->task) |
| 1119 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1120 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1121 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1122 | if (!yield_allowed) { |
| 1123 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1124 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1125 | break; |
| 1126 | } |
| 1127 | ret = HLUA_E_AGAIN; |
| 1128 | break; |
| 1129 | |
| 1130 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1131 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1132 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1133 | * because the errors ares the only one mean to return immediately |
| 1134 | * from and lua execution. |
| 1135 | */ |
| 1136 | if (lua->flags & HLUA_EXIT) { |
| 1137 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1138 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1139 | break; |
| 1140 | } |
| 1141 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1142 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1143 | if (!lua_checkstack(lua->T, 1)) { |
| 1144 | ret = HLUA_E_ERR; |
| 1145 | break; |
| 1146 | } |
| 1147 | msg = lua_tostring(lua->T, -1); |
| 1148 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1149 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1150 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1151 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1152 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1153 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1154 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1155 | ret = HLUA_E_ERRMSG; |
| 1156 | break; |
| 1157 | |
| 1158 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1159 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1160 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1161 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1162 | break; |
| 1163 | |
| 1164 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1165 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1166 | if (!lua_checkstack(lua->T, 1)) { |
| 1167 | ret = HLUA_E_ERR; |
| 1168 | break; |
| 1169 | } |
| 1170 | msg = lua_tostring(lua->T, -1); |
| 1171 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1172 | lua_pop(lua->T, 1); |
| 1173 | if (msg) |
| 1174 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1175 | else |
| 1176 | lua_pushfstring(lua->T, "message handler error"); |
| 1177 | ret = HLUA_E_ERRMSG; |
| 1178 | break; |
| 1179 | |
| 1180 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1181 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1182 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1183 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1184 | break; |
| 1185 | } |
| 1186 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1187 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1188 | if (lua->flags & HLUA_MUST_GC && |
| 1189 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1190 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1191 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1192 | switch (ret) { |
| 1193 | case HLUA_E_AGAIN: |
| 1194 | break; |
| 1195 | |
| 1196 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1197 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1198 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1199 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1200 | break; |
| 1201 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1202 | case HLUA_E_ETMOUT: |
| 1203 | case HLUA_E_NOMEM: |
| 1204 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1205 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1206 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1207 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1208 | hlua_ctx_renew(lua, 0); |
| 1209 | break; |
| 1210 | |
| 1211 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1212 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1213 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1214 | break; |
| 1215 | } |
| 1216 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1217 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1218 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1219 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1220 | return ret; |
| 1221 | } |
| 1222 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1223 | /* This function exit the current code. */ |
| 1224 | __LJMP static int hlua_done(lua_State *L) |
| 1225 | { |
| 1226 | struct hlua *hlua = hlua_gethlua(L); |
| 1227 | |
| 1228 | hlua->flags |= HLUA_EXIT; |
| 1229 | WILL_LJMP(lua_error(L)); |
| 1230 | |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1234 | /* This function is an LUA binding. It provides a function |
| 1235 | * for deleting ACL from a referenced ACL file. |
| 1236 | */ |
| 1237 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1238 | { |
| 1239 | const char *name; |
| 1240 | const char *key; |
| 1241 | struct pat_ref *ref; |
| 1242 | |
| 1243 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1244 | |
| 1245 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1246 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1247 | |
| 1248 | ref = pat_ref_lookup(name); |
| 1249 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1250 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1251 | |
| 1252 | pat_ref_delete(ref, key); |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
| 1256 | /* This function is an LUA binding. It provides a function |
| 1257 | * for deleting map entry from a referenced map file. |
| 1258 | */ |
| 1259 | static int hlua_del_map(lua_State *L) |
| 1260 | { |
| 1261 | const char *name; |
| 1262 | const char *key; |
| 1263 | struct pat_ref *ref; |
| 1264 | |
| 1265 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1266 | |
| 1267 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1268 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1269 | |
| 1270 | ref = pat_ref_lookup(name); |
| 1271 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1272 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1273 | |
| 1274 | pat_ref_delete(ref, key); |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
| 1278 | /* This function is an LUA binding. It provides a function |
| 1279 | * for adding ACL pattern from a referenced ACL file. |
| 1280 | */ |
| 1281 | static int hlua_add_acl(lua_State *L) |
| 1282 | { |
| 1283 | const char *name; |
| 1284 | const char *key; |
| 1285 | struct pat_ref *ref; |
| 1286 | |
| 1287 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1288 | |
| 1289 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1290 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1291 | |
| 1292 | ref = pat_ref_lookup(name); |
| 1293 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1294 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1295 | |
| 1296 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1297 | pat_ref_add(ref, key, NULL, NULL); |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | /* This function is an LUA binding. It provides a function |
| 1302 | * for setting map pattern and sample from a referenced map |
| 1303 | * file. |
| 1304 | */ |
| 1305 | static int hlua_set_map(lua_State *L) |
| 1306 | { |
| 1307 | const char *name; |
| 1308 | const char *key; |
| 1309 | const char *value; |
| 1310 | struct pat_ref *ref; |
| 1311 | |
| 1312 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1313 | |
| 1314 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1315 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1316 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1317 | |
| 1318 | ref = pat_ref_lookup(name); |
| 1319 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1320 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1321 | |
| 1322 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1323 | pat_ref_set(ref, key, value, NULL); |
| 1324 | else |
| 1325 | pat_ref_add(ref, key, value, NULL); |
| 1326 | return 0; |
| 1327 | } |
| 1328 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1329 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1330 | * an integer or user data. This data is associated with a metatable. This |
| 1331 | * metatable have an original version registred in the global context with |
| 1332 | * the name of the object (_G[<name>] = <metable> ). |
| 1333 | * |
| 1334 | * A metable is a table that modify the standard behavior of a standard |
| 1335 | * access to the associated data. The entries of this new metatable are |
| 1336 | * defined as is: |
| 1337 | * |
| 1338 | * http://lua-users.org/wiki/MetatableEvents |
| 1339 | * |
| 1340 | * __index |
| 1341 | * |
| 1342 | * we access an absent field in a table, the result is nil. This is |
| 1343 | * true, but it is not the whole truth. Actually, such access triggers |
| 1344 | * the interpreter to look for an __index metamethod: If there is no |
| 1345 | * such method, as usually happens, then the access results in nil; |
| 1346 | * otherwise, the metamethod will provide the result. |
| 1347 | * |
| 1348 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1349 | * the key does not appear in the table, but the metatable has an __index |
| 1350 | * property: |
| 1351 | * |
| 1352 | * - if the value is a function, the function is called, passing in the |
| 1353 | * table and the key; the return value of that function is returned as |
| 1354 | * the result. |
| 1355 | * |
| 1356 | * - if the value is another table, the value of the key in that table is |
| 1357 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1358 | * table's metatable has an __index property, then it continues on up) |
| 1359 | * |
| 1360 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1361 | * |
| 1362 | * http://www.lua.org/pil/13.4.1.html |
| 1363 | * |
| 1364 | * __newindex |
| 1365 | * |
| 1366 | * Like __index, but control property assignment. |
| 1367 | * |
| 1368 | * __mode - Control weak references. A string value with one or both |
| 1369 | * of the characters 'k' and 'v' which specifies that the the |
| 1370 | * keys and/or values in the table are weak references. |
| 1371 | * |
| 1372 | * __call - Treat a table like a function. When a table is followed by |
| 1373 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1374 | * a __call key pointing to a function, that function is invoked |
| 1375 | * (passing any specified arguments) and the return value is |
| 1376 | * returned. |
| 1377 | * |
| 1378 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1379 | * called, if the metatable for myTable has a __metatable |
| 1380 | * key, the value of that key is returned instead of the |
| 1381 | * actual metatable. |
| 1382 | * |
| 1383 | * __tostring - Control string representation. When the builtin |
| 1384 | * "tostring( myTable )" function is called, if the metatable |
| 1385 | * for myTable has a __tostring property set to a function, |
| 1386 | * that function is invoked (passing myTable to it) and the |
| 1387 | * return value is used as the string representation. |
| 1388 | * |
| 1389 | * __len - Control table length. When the table length is requested using |
| 1390 | * the length operator ( '#' ), if the metatable for myTable has |
| 1391 | * a __len key pointing to a function, that function is invoked |
| 1392 | * (passing myTable to it) and the return value used as the value |
| 1393 | * of "#myTable". |
| 1394 | * |
| 1395 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1396 | * collected, if the metatable has a __gc field pointing to a |
| 1397 | * function, that function is first invoked, passing the userdata |
| 1398 | * to it. The __gc metamethod is not called for tables. |
| 1399 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1400 | * |
| 1401 | * Special metamethods for redefining standard operators: |
| 1402 | * http://www.lua.org/pil/13.1.html |
| 1403 | * |
| 1404 | * __add "+" |
| 1405 | * __sub "-" |
| 1406 | * __mul "*" |
| 1407 | * __div "/" |
| 1408 | * __unm "!" |
| 1409 | * __pow "^" |
| 1410 | * __concat ".." |
| 1411 | * |
| 1412 | * Special methods for redfining standar relations |
| 1413 | * http://www.lua.org/pil/13.2.html |
| 1414 | * |
| 1415 | * __eq "==" |
| 1416 | * __lt "<" |
| 1417 | * __le "<=" |
| 1418 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1419 | |
| 1420 | /* |
| 1421 | * |
| 1422 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1423 | * Class Map |
| 1424 | * |
| 1425 | * |
| 1426 | */ |
| 1427 | |
| 1428 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1429 | * a class session, otherwise it throws an error. |
| 1430 | */ |
| 1431 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1432 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1433 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | /* This function is the map constructor. It don't need |
| 1437 | * the class Map object. It creates and return a new Map |
| 1438 | * object. It must be called only during "body" or "init" |
| 1439 | * context because it process some filesystem accesses. |
| 1440 | */ |
| 1441 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1442 | { |
| 1443 | const char *fn; |
| 1444 | int match = PAT_MATCH_STR; |
| 1445 | struct sample_conv conv; |
| 1446 | const char *file = ""; |
| 1447 | int line = 0; |
| 1448 | lua_Debug ar; |
| 1449 | char *err = NULL; |
| 1450 | struct arg args[2]; |
| 1451 | |
| 1452 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1453 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1454 | |
| 1455 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1456 | |
| 1457 | if (lua_gettop(L) >= 2) { |
| 1458 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1459 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1460 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1461 | } |
| 1462 | |
| 1463 | /* Get Lua filename and line number. */ |
| 1464 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1465 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1466 | if (ar.currentline > 0) { /* is there info? */ |
| 1467 | file = ar.short_src; |
| 1468 | line = ar.currentline; |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | /* fill fake sample_conv struct. */ |
| 1473 | conv.kw = ""; /* unused. */ |
| 1474 | conv.process = NULL; /* unused. */ |
| 1475 | conv.arg_mask = 0; /* unused. */ |
| 1476 | conv.val_args = NULL; /* unused. */ |
| 1477 | conv.out_type = SMP_T_STR; |
| 1478 | conv.private = (void *)(long)match; |
| 1479 | switch (match) { |
| 1480 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1481 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1482 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1483 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1484 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1485 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1486 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1487 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1488 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1489 | default: |
| 1490 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1491 | } |
| 1492 | |
| 1493 | /* fill fake args. */ |
| 1494 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1495 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1496 | args[1].type = ARGT_STOP; |
| 1497 | |
| 1498 | /* load the map. */ |
| 1499 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1500 | /* error case: we cant use luaL_error because we must |
| 1501 | * free the err variable. |
| 1502 | */ |
| 1503 | luaL_where(L, 1); |
| 1504 | lua_pushfstring(L, "'new': %s.", err); |
| 1505 | lua_concat(L, 2); |
| 1506 | free(err); |
| 1507 | WILL_LJMP(lua_error(L)); |
| 1508 | } |
| 1509 | |
| 1510 | /* create the lua object. */ |
| 1511 | lua_newtable(L); |
| 1512 | lua_pushlightuserdata(L, args[0].data.map); |
| 1513 | lua_rawseti(L, -2, 0); |
| 1514 | |
| 1515 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1516 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1517 | lua_setmetatable(L, -2); |
| 1518 | |
| 1519 | |
| 1520 | return 1; |
| 1521 | } |
| 1522 | |
| 1523 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1524 | { |
| 1525 | struct map_descriptor *desc; |
| 1526 | struct pattern *pat; |
| 1527 | struct sample smp; |
| 1528 | |
| 1529 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1530 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1531 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1532 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1533 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1534 | } |
| 1535 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1536 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1537 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1538 | 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] | 1539 | } |
| 1540 | |
| 1541 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1542 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1543 | if (str) |
| 1544 | lua_pushstring(L, ""); |
| 1545 | else |
| 1546 | lua_pushnil(L); |
| 1547 | return 1; |
| 1548 | } |
| 1549 | |
| 1550 | /* 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] | 1551 | 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] | 1552 | return 1; |
| 1553 | } |
| 1554 | |
| 1555 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1556 | { |
| 1557 | return _hlua_map_lookup(L, 0); |
| 1558 | } |
| 1559 | |
| 1560 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1561 | { |
| 1562 | return _hlua_map_lookup(L, 1); |
| 1563 | } |
| 1564 | |
| 1565 | /* |
| 1566 | * |
| 1567 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1568 | * Class Socket |
| 1569 | * |
| 1570 | * |
| 1571 | */ |
| 1572 | |
| 1573 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1574 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1575 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | /* 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] | 1579 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1580 | * received. |
| 1581 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1582 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1583 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1584 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1585 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1586 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1587 | if (appctx->ctx.hlua_cosocket.die) { |
| 1588 | si_shutw(si); |
| 1589 | si_shutr(si); |
| 1590 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1591 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1592 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1593 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1594 | } |
| 1595 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1596 | /* If the connection object is not available, close all the |
| 1597 | * streams and wakeup everything waiting for. |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1598 | */ |
| 1599 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1600 | si_shutw(si); |
| 1601 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1602 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1603 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1604 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1605 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1606 | } |
| 1607 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1608 | /* If we cant write, wakeup the pending write signals. */ |
| 1609 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1610 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1611 | |
| 1612 | /* If we cant read, wakeup the pending read signals. */ |
| 1613 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1614 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1615 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1616 | /* if the connection is not estabkished, inform the stream that we want |
| 1617 | * to be notified whenever the connection completes. |
| 1618 | */ |
| 1619 | if (!(c->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1620 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1621 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1622 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1623 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1624 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1625 | |
| 1626 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1627 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1628 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1629 | /* 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] | 1630 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1631 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1632 | |
| 1633 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1634 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1635 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1636 | |
| 1637 | /* Some data were injected in the buffer, notify the stream |
| 1638 | * interface. |
| 1639 | */ |
| 1640 | if (!channel_is_empty(si_ic(si))) |
| 1641 | stream_int_update(si); |
| 1642 | |
| 1643 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1644 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1645 | */ |
| 1646 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1647 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1648 | } |
| 1649 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1650 | /* This function is called when the "struct stream" is destroyed. |
| 1651 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1652 | * Wake all the pending signals. |
| 1653 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1654 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1655 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1656 | struct xref *peer; |
| 1657 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1658 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1659 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1660 | if (peer) |
| 1661 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1662 | |
| 1663 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1664 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1665 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1669 | * uses this object. If the stream does not exists, just quit. |
| 1670 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1671 | * pending signal can rest in the read and write lists. destroy |
| 1672 | * it. |
| 1673 | */ |
| 1674 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1675 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1676 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1677 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1678 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1679 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1680 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1681 | |
| 1682 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1683 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1684 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1685 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1686 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1687 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1688 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1689 | appctx->ctx.hlua_cosocket.die = 1; |
| 1690 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1691 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1692 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1693 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1694 | return 0; |
| 1695 | } |
| 1696 | |
| 1697 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1698 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1699 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1700 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1701 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1702 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1703 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1704 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1705 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1706 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1707 | |
| 1708 | /* Check if we run on the same thread than the xreator thread. |
| 1709 | * We cannot access to the socket if the thread is different. |
| 1710 | */ |
| 1711 | if (socket->tid != tid) |
| 1712 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1713 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1714 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1715 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1716 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1717 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1718 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1719 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1720 | appctx->ctx.hlua_cosocket.die = 1; |
| 1721 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1722 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1723 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1724 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1725 | return 0; |
| 1726 | } |
| 1727 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1728 | /* The close function calls close_helper. |
| 1729 | */ |
| 1730 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1731 | { |
| 1732 | MAY_LJMP(check_args(L, 1, "close")); |
| 1733 | return hlua_socket_close_helper(L); |
| 1734 | } |
| 1735 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1736 | /* This Lua function assumes that the stack contain three parameters. |
| 1737 | * 1 - USERDATA containing a struct socket |
| 1738 | * 2 - INTEGER with values of the macro defined below |
| 1739 | * If the integer is -1, we must read at most one line. |
| 1740 | * If the integer is -2, we ust read all the data until the |
| 1741 | * end of the stream. |
| 1742 | * If the integer is positive value, we must read a number of |
| 1743 | * bytes corresponding to this value. |
| 1744 | */ |
| 1745 | #define HLSR_READ_LINE (-1) |
| 1746 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1747 | __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] | 1748 | { |
| 1749 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1750 | int wanted = lua_tointeger(L, 2); |
| 1751 | struct hlua *hlua = hlua_gethlua(L); |
| 1752 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1753 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1754 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1755 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1756 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1757 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1758 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1759 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1760 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1761 | struct stream_interface *si; |
| 1762 | struct stream *s; |
| 1763 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1764 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1765 | |
| 1766 | /* Check if this lua stack is schedulable. */ |
| 1767 | if (!hlua || !hlua->task) |
| 1768 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1769 | "'frontend', 'backend' or 'task'")); |
| 1770 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1771 | /* Check if we run on the same thread than the xreator thread. |
| 1772 | * We cannot access to the socket if the thread is different. |
| 1773 | */ |
| 1774 | if (socket->tid != tid) |
| 1775 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1776 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1777 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1778 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1779 | if (!peer) |
| 1780 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1781 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1782 | si = appctx->owner; |
| 1783 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1784 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1785 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1786 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1787 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1788 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1789 | if (nblk < 0) /* Connection close. */ |
| 1790 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1791 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1792 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1793 | |
| 1794 | /* remove final \r\n. */ |
| 1795 | if (nblk == 1) { |
| 1796 | if (blk1[len1-1] == '\n') { |
| 1797 | len1--; |
| 1798 | skip_at_end++; |
| 1799 | if (blk1[len1-1] == '\r') { |
| 1800 | len1--; |
| 1801 | skip_at_end++; |
| 1802 | } |
| 1803 | } |
| 1804 | } |
| 1805 | else { |
| 1806 | if (blk2[len2-1] == '\n') { |
| 1807 | len2--; |
| 1808 | skip_at_end++; |
| 1809 | if (blk2[len2-1] == '\r') { |
| 1810 | len2--; |
| 1811 | skip_at_end++; |
| 1812 | } |
| 1813 | } |
| 1814 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1818 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1819 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1820 | if (nblk < 0) /* Connection close. */ |
| 1821 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1822 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1823 | goto connection_empty; |
| 1824 | } |
| 1825 | |
| 1826 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1827 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1828 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1829 | if (nblk < 0) /* Connection close. */ |
| 1830 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1831 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1832 | goto connection_empty; |
| 1833 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1834 | missing_bytes = wanted - socket->b.n; |
| 1835 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1836 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1837 | len1 = missing_bytes; |
| 1838 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1839 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | len = len1; |
| 1843 | |
| 1844 | luaL_addlstring(&socket->b, blk1, len1); |
| 1845 | if (nblk == 2) { |
| 1846 | len += len2; |
| 1847 | luaL_addlstring(&socket->b, blk2, len2); |
| 1848 | } |
| 1849 | |
| 1850 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1851 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1852 | |
| 1853 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1854 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1855 | |
| 1856 | /* If the pattern reclaim to read all the data |
| 1857 | * in the connection, got out. |
| 1858 | */ |
| 1859 | if (wanted == HLSR_READ_ALL) |
| 1860 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1861 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1862 | goto connection_empty; |
| 1863 | |
| 1864 | /* Return result. */ |
| 1865 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1866 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1867 | return 1; |
| 1868 | |
| 1869 | connection_closed: |
| 1870 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1871 | xref_unlock(&socket->xref, peer); |
| 1872 | |
| 1873 | no_peer: |
| 1874 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1875 | /* If the buffer containds data. */ |
| 1876 | if (socket->b.n > 0) { |
| 1877 | luaL_pushresult(&socket->b); |
| 1878 | return 1; |
| 1879 | } |
| 1880 | lua_pushnil(L); |
| 1881 | lua_pushstring(L, "connection closed."); |
| 1882 | return 2; |
| 1883 | |
| 1884 | connection_empty: |
| 1885 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1886 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1887 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1888 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1889 | } |
| 1890 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1891 | 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] | 1892 | return 0; |
| 1893 | } |
| 1894 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1895 | /* This Lua function gets two parameters. The first one can be string |
| 1896 | * or a number. If the string is "*l", the user requires one line. If |
| 1897 | * 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] | 1898 | * If the value is a number, the user require a number of bytes equal |
| 1899 | * to the value. The default value is "*l" (a line). |
| 1900 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1901 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1902 | * integer takes this values: |
| 1903 | * -1 : read a line |
| 1904 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1905 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1906 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1907 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1908 | * concatenated with the read data. |
| 1909 | */ |
| 1910 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1911 | { |
| 1912 | int wanted = HLSR_READ_LINE; |
| 1913 | const char *pattern; |
| 1914 | int type; |
| 1915 | char *error; |
| 1916 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1917 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1918 | |
| 1919 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1920 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1921 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1922 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1923 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1924 | /* Check if we run on the same thread than the xreator thread. |
| 1925 | * We cannot access to the socket if the thread is different. |
| 1926 | */ |
| 1927 | if (socket->tid != tid) |
| 1928 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1929 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1930 | /* check for pattern. */ |
| 1931 | if (lua_gettop(L) >= 2) { |
| 1932 | type = lua_type(L, 2); |
| 1933 | if (type == LUA_TSTRING) { |
| 1934 | pattern = lua_tostring(L, 2); |
| 1935 | if (strcmp(pattern, "*a") == 0) |
| 1936 | wanted = HLSR_READ_ALL; |
| 1937 | else if (strcmp(pattern, "*l") == 0) |
| 1938 | wanted = HLSR_READ_LINE; |
| 1939 | else { |
| 1940 | wanted = strtoll(pattern, &error, 10); |
| 1941 | if (*error != '\0') |
| 1942 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1943 | } |
| 1944 | } |
| 1945 | else if (type == LUA_TNUMBER) { |
| 1946 | wanted = lua_tointeger(L, 2); |
| 1947 | if (wanted < 0) |
| 1948 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | /* Set pattern. */ |
| 1953 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1954 | |
| 1955 | /* Check if we would replace the top by itself. */ |
| 1956 | if (lua_gettop(L) != 2) |
| 1957 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1958 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1959 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1960 | luaL_buffinit(L, &socket->b); |
| 1961 | |
| 1962 | /* Check prefix. */ |
| 1963 | if (lua_gettop(L) >= 3) { |
| 1964 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1965 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1966 | pattern = lua_tolstring(L, 3, &len); |
| 1967 | luaL_addlstring(&socket->b, pattern, len); |
| 1968 | } |
| 1969 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1970 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1974 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1975 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1976 | 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] | 1977 | { |
| 1978 | struct hlua_socket *socket; |
| 1979 | struct hlua *hlua = hlua_gethlua(L); |
| 1980 | struct appctx *appctx; |
| 1981 | size_t buf_len; |
| 1982 | const char *buf; |
| 1983 | int len; |
| 1984 | int send_len; |
| 1985 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1986 | struct xref *peer; |
| 1987 | struct stream_interface *si; |
| 1988 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1989 | |
| 1990 | /* Check if this lua stack is schedulable. */ |
| 1991 | if (!hlua || !hlua->task) |
| 1992 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1993 | "'frontend', 'backend' or 'task'")); |
| 1994 | |
| 1995 | /* Get object */ |
| 1996 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1997 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1998 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1999 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2000 | /* Check if we run on the same thread than the xreator thread. |
| 2001 | * We cannot access to the socket if the thread is different. |
| 2002 | */ |
| 2003 | if (socket->tid != tid) |
| 2004 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2005 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2006 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2007 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2008 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2009 | lua_pushinteger(L, -1); |
| 2010 | return 1; |
| 2011 | } |
| 2012 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2013 | si = appctx->owner; |
| 2014 | s = si_strm(si); |
| 2015 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2016 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2017 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2018 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2019 | lua_pushinteger(L, -1); |
| 2020 | return 1; |
| 2021 | } |
| 2022 | |
| 2023 | /* Update the input buffer data. */ |
| 2024 | buf += sent; |
| 2025 | send_len = buf_len - sent; |
| 2026 | |
| 2027 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2028 | if (sent >= buf_len) { |
| 2029 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2030 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2031 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2032 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2033 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2034 | * the request buffer if its not required. |
| 2035 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2036 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2037 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2038 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2039 | } |
| 2040 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2041 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2042 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2043 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2044 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2045 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2046 | |
| 2047 | /* send data */ |
| 2048 | if (len < send_len) |
| 2049 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2050 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2051 | |
| 2052 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2053 | * the data" (-2) are not expected because the available length |
| 2054 | * is tested. |
| 2055 | * Other unknown error are also not expected. |
| 2056 | */ |
| 2057 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2058 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2059 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2060 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2061 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2062 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2063 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2064 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2065 | return 1; |
| 2066 | } |
| 2067 | |
| 2068 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2069 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2070 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2071 | s->req.rex = TICK_ETERNITY; |
| 2072 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2073 | |
| 2074 | /* Update length sent. */ |
| 2075 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2076 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2077 | |
| 2078 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2079 | if (sent + len >= buf_len) { |
| 2080 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2081 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2082 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2083 | |
| 2084 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2085 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2086 | xref_unlock(&socket->xref, peer); |
| 2087 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2088 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2089 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2090 | 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] | 2091 | return 0; |
| 2092 | } |
| 2093 | |
| 2094 | /* This function initiate the send of data. It just check the input |
| 2095 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2096 | * 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] | 2097 | * "hlua_socket_write_yield" that can yield. |
| 2098 | * |
| 2099 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2100 | * the associated object. The second is a string buffer. The third is |
| 2101 | * a facultative integer that represents where is the buffer position |
| 2102 | * of the start of the data that can send. The first byte is the |
| 2103 | * position "1". The default value is "1". The fourth argument is a |
| 2104 | * facultative integer that represents where is the buffer position |
| 2105 | * of the end of the data that can send. The default is the last byte. |
| 2106 | */ |
| 2107 | static int hlua_socket_send(struct lua_State *L) |
| 2108 | { |
| 2109 | int i; |
| 2110 | int j; |
| 2111 | const char *buf; |
| 2112 | size_t buf_len; |
| 2113 | |
| 2114 | /* Check number of arguments. */ |
| 2115 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2116 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2117 | |
| 2118 | /* Get the string. */ |
| 2119 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2120 | |
| 2121 | /* Get and check j. */ |
| 2122 | if (lua_gettop(L) == 4) { |
| 2123 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2124 | if (j < 0) |
| 2125 | j = buf_len + j + 1; |
| 2126 | if (j > buf_len) |
| 2127 | j = buf_len + 1; |
| 2128 | lua_pop(L, 1); |
| 2129 | } |
| 2130 | else |
| 2131 | j = buf_len; |
| 2132 | |
| 2133 | /* Get and check i. */ |
| 2134 | if (lua_gettop(L) == 3) { |
| 2135 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2136 | if (i < 0) |
| 2137 | i = buf_len + i + 1; |
| 2138 | if (i > buf_len) |
| 2139 | i = buf_len + 1; |
| 2140 | lua_pop(L, 1); |
| 2141 | } else |
| 2142 | i = 1; |
| 2143 | |
| 2144 | /* Check bth i and j. */ |
| 2145 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2146 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2147 | return 1; |
| 2148 | } |
| 2149 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2150 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2151 | return 1; |
| 2152 | } |
| 2153 | if (i == 0) |
| 2154 | i = 1; |
| 2155 | if (j == 0) |
| 2156 | j = 1; |
| 2157 | |
| 2158 | /* Pop the string. */ |
| 2159 | lua_pop(L, 1); |
| 2160 | |
| 2161 | /* Update the buffer length. */ |
| 2162 | buf += i - 1; |
| 2163 | buf_len = j - i + 1; |
| 2164 | lua_pushlstring(L, buf, buf_len); |
| 2165 | |
| 2166 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2167 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2168 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2169 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2170 | } |
| 2171 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2172 | #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] | 2173 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2174 | { |
| 2175 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2176 | int ret; |
| 2177 | int len; |
| 2178 | char *p; |
| 2179 | |
| 2180 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2181 | if (ret <= 0) { |
| 2182 | lua_pushnil(L); |
| 2183 | return 1; |
| 2184 | } |
| 2185 | |
| 2186 | if (ret == AF_UNIX) { |
| 2187 | lua_pushstring(L, buffer+1); |
| 2188 | return 1; |
| 2189 | } |
| 2190 | else if (ret == AF_INET6) { |
| 2191 | buffer[0] = '['; |
| 2192 | len = strlen(buffer); |
| 2193 | buffer[len] = ']'; |
| 2194 | len++; |
| 2195 | buffer[len] = ':'; |
| 2196 | len++; |
| 2197 | p = buffer; |
| 2198 | } |
| 2199 | else if (ret == AF_INET) { |
| 2200 | p = buffer + 1; |
| 2201 | len = strlen(p); |
| 2202 | p[len] = ':'; |
| 2203 | len++; |
| 2204 | } |
| 2205 | else { |
| 2206 | lua_pushnil(L); |
| 2207 | return 1; |
| 2208 | } |
| 2209 | |
| 2210 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2211 | lua_pushnil(L); |
| 2212 | return 1; |
| 2213 | } |
| 2214 | |
| 2215 | lua_pushstring(L, p); |
| 2216 | return 1; |
| 2217 | } |
| 2218 | |
| 2219 | /* Returns information about the peer of the connection. */ |
| 2220 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2221 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2222 | struct hlua_socket *socket; |
| 2223 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2224 | struct xref *peer; |
| 2225 | struct appctx *appctx; |
| 2226 | struct stream_interface *si; |
| 2227 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2228 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2229 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2230 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2231 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2232 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2233 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2234 | /* Check if we run on the same thread than the xreator thread. |
| 2235 | * We cannot access to the socket if the thread is different. |
| 2236 | */ |
| 2237 | if (socket->tid != tid) |
| 2238 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2239 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2240 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2241 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2242 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2243 | lua_pushnil(L); |
| 2244 | return 1; |
| 2245 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2246 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2247 | si = appctx->owner; |
| 2248 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2249 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2250 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2251 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2252 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2253 | lua_pushnil(L); |
| 2254 | return 1; |
| 2255 | } |
| 2256 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2257 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2258 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2259 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2260 | lua_pushnil(L); |
| 2261 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2262 | } |
| 2263 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2264 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2265 | xref_unlock(&socket->xref, peer); |
| 2266 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | /* Returns information about my connection side. */ |
| 2270 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2271 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2272 | struct hlua_socket *socket; |
| 2273 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2274 | struct appctx *appctx; |
| 2275 | struct xref *peer; |
| 2276 | struct stream_interface *si; |
| 2277 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2278 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2279 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2280 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2281 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2282 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2283 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2284 | /* Check if we run on the same thread than the xreator thread. |
| 2285 | * We cannot access to the socket if the thread is different. |
| 2286 | */ |
| 2287 | if (socket->tid != tid) |
| 2288 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2289 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2290 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2291 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2292 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | lua_pushnil(L); |
| 2294 | return 1; |
| 2295 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2296 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2297 | si = appctx->owner; |
| 2298 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2299 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2300 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2301 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2302 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2303 | lua_pushnil(L); |
| 2304 | return 1; |
| 2305 | } |
| 2306 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2307 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2308 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2309 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2310 | lua_pushnil(L); |
| 2311 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2312 | } |
| 2313 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2314 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2315 | xref_unlock(&socket->xref, peer); |
| 2316 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2320 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | .obj_type = OBJ_TYPE_APPLET, |
| 2322 | .name = "<LUA_TCP>", |
| 2323 | .fct = hlua_socket_handler, |
| 2324 | .release = hlua_socket_release, |
| 2325 | }; |
| 2326 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2327 | __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] | 2328 | { |
| 2329 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2330 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2331 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2332 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2333 | struct stream_interface *si; |
| 2334 | struct stream *s; |
| 2335 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2336 | /* Check if we run on the same thread than the xreator thread. |
| 2337 | * We cannot access to the socket if the thread is different. |
| 2338 | */ |
| 2339 | if (socket->tid != tid) |
| 2340 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2341 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2342 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2343 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2344 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2345 | lua_pushnil(L); |
| 2346 | lua_pushstring(L, "Can't connect"); |
| 2347 | return 2; |
| 2348 | } |
| 2349 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2350 | si = appctx->owner; |
| 2351 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2352 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2353 | /* Check if we run on the same thread than the xreator thread. |
| 2354 | * We cannot access to the socket if the thread is different. |
| 2355 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2356 | if (socket->tid != tid) { |
| 2357 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2358 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2359 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2360 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2361 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2362 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2363 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2364 | lua_pushnil(L); |
| 2365 | lua_pushstring(L, "Can't connect"); |
| 2366 | return 2; |
| 2367 | } |
| 2368 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2369 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2370 | |
| 2371 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2372 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2373 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2374 | lua_pushinteger(L, 1); |
| 2375 | return 1; |
| 2376 | } |
| 2377 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2378 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2379 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2380 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2381 | } |
| 2382 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2383 | 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] | 2384 | return 0; |
| 2385 | } |
| 2386 | |
| 2387 | /* This function fail or initite the connection. */ |
| 2388 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2389 | { |
| 2390 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2391 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2392 | const char *ip; |
| 2393 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2394 | struct hlua *hlua; |
| 2395 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2396 | int low, high; |
| 2397 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2398 | struct xref *peer; |
| 2399 | struct stream_interface *si; |
| 2400 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2401 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2402 | if (lua_gettop(L) < 2) |
| 2403 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2404 | |
| 2405 | /* Get args. */ |
| 2406 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2407 | |
| 2408 | /* Check if we run on the same thread than the xreator thread. |
| 2409 | * We cannot access to the socket if the thread is different. |
| 2410 | */ |
| 2411 | if (socket->tid != tid) |
| 2412 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2413 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2414 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2415 | if (lua_gettop(L) >= 3) { |
| 2416 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2417 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2418 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2419 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2420 | * that are not enclosed within square brackets. |
| 2421 | */ |
| 2422 | if (port > 0) { |
| 2423 | luaL_buffinit(L, &b); |
| 2424 | luaL_addstring(&b, ip); |
| 2425 | luaL_addchar(&b, ':'); |
| 2426 | luaL_pushresult(&b); |
| 2427 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2428 | } |
| 2429 | } |
| 2430 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2431 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2432 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2433 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2434 | lua_pushnil(L); |
| 2435 | return 1; |
| 2436 | } |
| 2437 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2438 | si = appctx->owner; |
| 2439 | s = si_strm(si); |
| 2440 | |
| 2441 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2442 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2443 | if (!conn) { |
| 2444 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2445 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2446 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2447 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2448 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2449 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2450 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2451 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2452 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2453 | if (!addr) { |
| 2454 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2455 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2456 | } |
| 2457 | if (low != high) { |
| 2458 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2459 | 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] | 2460 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2461 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2462 | |
| 2463 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2464 | if (low == 0) { |
| 2465 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2466 | if (port == -1) { |
| 2467 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2468 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2469 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2470 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2471 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2472 | if (port == -1) { |
| 2473 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2474 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2475 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2476 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2477 | } |
| 2478 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2479 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2480 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2481 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2482 | |
| 2483 | /* inform the stream that we want to be notified whenever the |
| 2484 | * connection completes. |
| 2485 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2486 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2487 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2488 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2489 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2490 | hlua->flags |= HLUA_MUST_GC; |
| 2491 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2492 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2493 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2494 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2495 | } |
| 2496 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2497 | |
| 2498 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2499 | /* Return yield waiting for connection. */ |
| 2500 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2501 | 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] | 2502 | |
| 2503 | return 0; |
| 2504 | } |
| 2505 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2506 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2507 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2508 | { |
| 2509 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2510 | struct xref *peer; |
| 2511 | struct appctx *appctx; |
| 2512 | struct stream_interface *si; |
| 2513 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2514 | |
| 2515 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2516 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2517 | |
| 2518 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2519 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2520 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2521 | lua_pushnil(L); |
| 2522 | return 1; |
| 2523 | } |
| 2524 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2525 | si = appctx->owner; |
| 2526 | s = si_strm(si); |
| 2527 | |
| 2528 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2529 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2530 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2531 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2532 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2533 | |
| 2534 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2535 | { |
| 2536 | return 0; |
| 2537 | } |
| 2538 | |
| 2539 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2540 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2541 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2542 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2543 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2544 | struct xref *peer; |
| 2545 | struct appctx *appctx; |
| 2546 | struct stream_interface *si; |
| 2547 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2548 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2549 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2550 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2551 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2552 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2553 | /* convert the timeout to millis */ |
| 2554 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2555 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2556 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2557 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2558 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2559 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2560 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2561 | 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] | 2562 | |
| 2563 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2564 | if (tmout == 0) |
| 2565 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2566 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2567 | /* Check if we run on the same thread than the xreator thread. |
| 2568 | * We cannot access to the socket if the thread is different. |
| 2569 | */ |
| 2570 | if (socket->tid != tid) |
| 2571 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2572 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2573 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2574 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2575 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2576 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2577 | WILL_LJMP(lua_error(L)); |
| 2578 | return 0; |
| 2579 | } |
| 2580 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2581 | si = appctx->owner; |
| 2582 | s = si_strm(si); |
| 2583 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2584 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2585 | s->req.rto = tmout; |
| 2586 | s->req.wto = tmout; |
| 2587 | s->res.rto = tmout; |
| 2588 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2589 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2590 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2591 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2592 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2593 | |
| 2594 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2595 | task_queue(s->task); |
| 2596 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2597 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2598 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2599 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2600 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2604 | { |
| 2605 | struct hlua_socket *socket; |
| 2606 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2607 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2608 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2609 | |
| 2610 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2611 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2612 | hlua_pusherror(L, "socket: full stack"); |
| 2613 | goto out_fail_conf; |
| 2614 | } |
| 2615 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2616 | /* Create the object: obj[0] = userdata. */ |
| 2617 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2618 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2619 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2620 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2621 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2622 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2623 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2624 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2625 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2626 | goto out_fail_conf; |
| 2627 | } |
| 2628 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2629 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2630 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2631 | lua_setmetatable(L, -2); |
| 2632 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2633 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2634 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2635 | if (!appctx) { |
| 2636 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2637 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2638 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2639 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2640 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2641 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2642 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2643 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2644 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2645 | /* Now create a session, task and stream for this applet */ |
| 2646 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2647 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2648 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2649 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2650 | } |
| 2651 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2652 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2653 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2654 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2655 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2656 | } |
| 2657 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2658 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2659 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2660 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2661 | /* Configure "right" stream interface. this "si" is used to connect |
| 2662 | * and retrieve data from the server. The connection is initialized |
| 2663 | * with the "struct server". |
| 2664 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2665 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2666 | |
| 2667 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2668 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2669 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2670 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2671 | return 1; |
| 2672 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2673 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2674 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2675 | out_fail_sess: |
| 2676 | appctx_free(appctx); |
| 2677 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2678 | WILL_LJMP(lua_error(L)); |
| 2679 | return 0; |
| 2680 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2681 | |
| 2682 | /* |
| 2683 | * |
| 2684 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2685 | * Class Channel |
| 2686 | * |
| 2687 | * |
| 2688 | */ |
| 2689 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2690 | /* This function is called before the Lua execution. It stores |
| 2691 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2692 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2693 | 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] | 2694 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2695 | c->mode = stream->be->mode; |
| 2696 | switch (c->mode) { |
| 2697 | case PR_MODE_HTTP: |
| 2698 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2699 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2700 | c->data.http.state = stream->txn->req.msg_state; |
| 2701 | else |
| 2702 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2703 | break; |
| 2704 | default: |
| 2705 | break; |
| 2706 | } |
| 2707 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2708 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2709 | /* This function is called after the Lua execution. it |
| 2710 | * returns true if the parser state is consistent, otherwise, |
| 2711 | * it return false. |
| 2712 | * |
| 2713 | * In HTTP mode, the parser state must be in the same state |
| 2714 | * or greater when we exit the function. Even if we do a |
| 2715 | * control yield. This prevent to break the HTTP message |
| 2716 | * from the Lua code. |
| 2717 | */ |
| 2718 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2719 | { |
| 2720 | if (c->mode != stream->be->mode) |
| 2721 | return 0; |
| 2722 | |
| 2723 | switch (c->mode) { |
| 2724 | case PR_MODE_HTTP: |
| 2725 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2726 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2727 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2728 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2729 | else |
| 2730 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2731 | default: |
| 2732 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2733 | } |
| 2734 | return 1; |
| 2735 | } |
| 2736 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2737 | /* Returns the struct hlua_channel join to the class channel in the |
| 2738 | * stack entry "ud" or throws an argument error. |
| 2739 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2740 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2741 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2742 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2743 | } |
| 2744 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2745 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2746 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2747 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2748 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2749 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2750 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2751 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2752 | return 0; |
| 2753 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2754 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2755 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2756 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2757 | |
| 2758 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2759 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2760 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2761 | return 1; |
| 2762 | } |
| 2763 | |
| 2764 | /* Duplicate all the data present in the input channel and put it |
| 2765 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2766 | * the stack if the channel is closed and all the data are consumed, |
| 2767 | * returns 0 if no data are available, otherwise it returns the length |
| 2768 | * of the builded string. |
| 2769 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2770 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2771 | { |
| 2772 | char *blk1; |
| 2773 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2774 | size_t len1; |
| 2775 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2776 | int ret; |
| 2777 | luaL_Buffer b; |
| 2778 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2779 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2780 | if (unlikely(ret == 0)) |
| 2781 | return 0; |
| 2782 | |
| 2783 | if (unlikely(ret < 0)) { |
| 2784 | lua_pushnil(L); |
| 2785 | return -1; |
| 2786 | } |
| 2787 | |
| 2788 | luaL_buffinit(L, &b); |
| 2789 | luaL_addlstring(&b, blk1, len1); |
| 2790 | if (unlikely(ret == 2)) |
| 2791 | luaL_addlstring(&b, blk2, len2); |
| 2792 | luaL_pushresult(&b); |
| 2793 | |
| 2794 | if (unlikely(ret == 2)) |
| 2795 | return len1 + len2; |
| 2796 | return len1; |
| 2797 | } |
| 2798 | |
| 2799 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2800 | * a yield. This function keep the data in the buffer. |
| 2801 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2802 | __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] | 2803 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2804 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2805 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2806 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2807 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2808 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2809 | WILL_LJMP(lua_error(L)); |
| 2810 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2811 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2812 | 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] | 2813 | return 1; |
| 2814 | } |
| 2815 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2816 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2817 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2818 | { |
| 2819 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2820 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2821 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2822 | } |
| 2823 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2824 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2825 | * a yield. This function consumes the data in the buffer. It returns |
| 2826 | * a string containing the data or a nil pointer if no data are available |
| 2827 | * and the channel is closed. |
| 2828 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2829 | __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] | 2830 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2831 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2832 | int ret; |
| 2833 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2834 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2835 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2836 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2837 | WILL_LJMP(lua_error(L)); |
| 2838 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2839 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2840 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2841 | 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] | 2842 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2843 | if (unlikely(ret == -1)) |
| 2844 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2845 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2846 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2847 | return 1; |
| 2848 | } |
| 2849 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2850 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2851 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2852 | { |
| 2853 | MAY_LJMP(check_args(L, 1, "get")); |
| 2854 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2855 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2856 | } |
| 2857 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2858 | /* This functions consumes and returns one line. If the channel is closed, |
| 2859 | * 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] | 2860 | * 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] | 2861 | * value. |
| 2862 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2863 | __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] | 2864 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2865 | char *blk1; |
| 2866 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2867 | size_t len1; |
| 2868 | size_t len2; |
| 2869 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2870 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2871 | int ret; |
| 2872 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2873 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2874 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2875 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2876 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2877 | WILL_LJMP(lua_error(L)); |
| 2878 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2879 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2880 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2881 | 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] | 2882 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2883 | if (ret == -1) { |
| 2884 | lua_pushnil(L); |
| 2885 | return 1; |
| 2886 | } |
| 2887 | |
| 2888 | luaL_buffinit(L, &b); |
| 2889 | luaL_addlstring(&b, blk1, len1); |
| 2890 | len = len1; |
| 2891 | if (unlikely(ret == 2)) { |
| 2892 | luaL_addlstring(&b, blk2, len2); |
| 2893 | len += len2; |
| 2894 | } |
| 2895 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2896 | 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] | 2897 | return 1; |
| 2898 | } |
| 2899 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2900 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2901 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2902 | { |
| 2903 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2904 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2905 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2906 | } |
| 2907 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2908 | /* This function takes a string as input, and append it at the |
| 2909 | * input side of channel. If the data is too big, but a space |
| 2910 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2911 | * yields. If the data is bigger than the buffer, or if the |
| 2912 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2913 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2914 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2915 | __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] | 2916 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2917 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2918 | size_t len; |
| 2919 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2920 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2921 | int ret; |
| 2922 | int max; |
| 2923 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2924 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2925 | WILL_LJMP(lua_error(L)); |
| 2926 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2927 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2928 | * the request buffer if its not required. |
| 2929 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2930 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2931 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2932 | 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] | 2933 | } |
| 2934 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2935 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2936 | if (max > len - l) |
| 2937 | max = len - l; |
| 2938 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2939 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2940 | if (ret == -2 || ret == -3) { |
| 2941 | lua_pushinteger(L, -1); |
| 2942 | return 1; |
| 2943 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2944 | if (ret == -1) { |
| 2945 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2946 | 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] | 2947 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2948 | l += ret; |
| 2949 | lua_pop(L, 1); |
| 2950 | lua_pushinteger(L, l); |
| 2951 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2952 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2953 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2954 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2955 | * in this case, we cannot add more data, so we cannot yield, |
| 2956 | * we return the amount of copyied data. |
| 2957 | */ |
| 2958 | return 1; |
| 2959 | } |
| 2960 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2961 | 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] | 2962 | return 1; |
| 2963 | } |
| 2964 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2965 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2966 | * 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] | 2967 | * buffer size is too little for the data. |
| 2968 | */ |
| 2969 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2970 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2971 | size_t len; |
| 2972 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2973 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2974 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2975 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2976 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2977 | lua_pushinteger(L, 0); |
| 2978 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2979 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2980 | } |
| 2981 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2982 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2983 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2984 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2985 | * or -1 if the channel is closed or if the buffer size is too |
| 2986 | * little for the data. |
| 2987 | */ |
| 2988 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2989 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2990 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2991 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2992 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2993 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2994 | lua_pushinteger(L, 0); |
| 2995 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2996 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2997 | WILL_LJMP(lua_error(L)); |
| 2998 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2999 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3000 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3001 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3002 | } |
| 3003 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3004 | /* Append data in the output side of the buffer. This data is immediately |
| 3005 | * sent. The function returns the amount of data written. If the buffer |
| 3006 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3007 | * if the channel is closed. |
| 3008 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3009 | __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] | 3010 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3011 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3012 | size_t len; |
| 3013 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3014 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3015 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3016 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3017 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3018 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3019 | WILL_LJMP(lua_error(L)); |
| 3020 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3021 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3022 | lua_pushinteger(L, -1); |
| 3023 | return 1; |
| 3024 | } |
| 3025 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3026 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3027 | * the request buffer if its not required. |
| 3028 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3029 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3030 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3031 | 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] | 3032 | } |
| 3033 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3034 | /* The written data will be immediately sent, so we can check |
| 3035 | * the available space without taking in account the reserve. |
| 3036 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3037 | * data, because the buffer will be flushed. |
| 3038 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3039 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3040 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3041 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3042 | * in this case, we cannot add more data, so we cannot yield, |
| 3043 | * we return the amount of copyied data. |
| 3044 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3045 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3046 | return 1; |
| 3047 | |
| 3048 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3049 | if (max > len - l) |
| 3050 | max = len - l; |
| 3051 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3052 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3053 | * detects a non contiguous buffer and realign it. |
| 3054 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3055 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3056 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3057 | |
| 3058 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3059 | 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] | 3060 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3061 | /* buffer replace considers that the input part is filled. |
| 3062 | * so, I must forward these new data in the output part. |
| 3063 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3064 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3065 | |
| 3066 | l += max; |
| 3067 | lua_pop(L, 1); |
| 3068 | lua_pushinteger(L, l); |
| 3069 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3070 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3071 | * in this case, we cannot add more data, so we cannot yield, |
| 3072 | * we return the amount of copyied data. |
| 3073 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3074 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3075 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3076 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3077 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3078 | if (l < len) { |
| 3079 | /* If we are waiting for space in the response buffer, we |
| 3080 | * must set the flag WAKERESWR. This flag required the task |
| 3081 | * wake up if any activity is detected on the response buffer. |
| 3082 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3083 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3084 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3085 | else |
| 3086 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3087 | 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] | 3088 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3089 | |
| 3090 | return 1; |
| 3091 | } |
| 3092 | |
| 3093 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3094 | * yield the LUA process, and resume it without checking the |
| 3095 | * input arguments. |
| 3096 | */ |
| 3097 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3098 | { |
| 3099 | MAY_LJMP(check_args(L, 2, "send")); |
| 3100 | lua_pushinteger(L, 0); |
| 3101 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3102 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | /* This function forward and amount of butes. The data pass from |
| 3106 | * the input side of the buffer to the output side, and can be |
| 3107 | * forwarded. This function never fails. |
| 3108 | * |
| 3109 | * The Lua function takes an amount of bytes to be forwarded in |
| 3110 | * imput. It returns the number of bytes forwarded. |
| 3111 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3112 | __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] | 3113 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3114 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3115 | int len; |
| 3116 | int l; |
| 3117 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3118 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3119 | |
| 3120 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3121 | |
| 3122 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3123 | WILL_LJMP(lua_error(L)); |
| 3124 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3125 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3126 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3127 | |
| 3128 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3129 | if (max > ci_data(chn)) |
| 3130 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3131 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3132 | l += max; |
| 3133 | |
| 3134 | lua_pop(L, 1); |
| 3135 | lua_pushinteger(L, l); |
| 3136 | |
| 3137 | /* Check if it miss bytes to forward. */ |
| 3138 | if (l < len) { |
| 3139 | /* The the input channel or the output channel are closed, we |
| 3140 | * must return the amount of data forwarded. |
| 3141 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3142 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3143 | return 1; |
| 3144 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3145 | /* If we are waiting for space data in the response buffer, we |
| 3146 | * must set the flag WAKERESWR. This flag required the task |
| 3147 | * wake up if any activity is detected on the response buffer. |
| 3148 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3149 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3150 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3151 | else |
| 3152 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3153 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3154 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3155 | 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] | 3156 | } |
| 3157 | |
| 3158 | return 1; |
| 3159 | } |
| 3160 | |
| 3161 | /* Just check the input and prepare the stack for the previous |
| 3162 | * function "hlua_channel_forward_yield" |
| 3163 | */ |
| 3164 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3165 | { |
| 3166 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3167 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3168 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3169 | |
| 3170 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3171 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | /* Just returns the number of bytes available in the input |
| 3175 | * side of the buffer. This function never fails. |
| 3176 | */ |
| 3177 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3178 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3179 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3180 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3181 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3182 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3183 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3184 | return 1; |
| 3185 | } |
| 3186 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3187 | /* Returns true if the channel is full. */ |
| 3188 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3189 | { |
| 3190 | struct channel *chn; |
| 3191 | int rem; |
| 3192 | |
| 3193 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3194 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3195 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3196 | rem = b_room(&chn->buf); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3197 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3198 | |
| 3199 | lua_pushboolean(L, rem <= 0); |
| 3200 | return 1; |
| 3201 | } |
| 3202 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3203 | /* Just returns the number of bytes available in the output |
| 3204 | * side of the buffer. This function never fails. |
| 3205 | */ |
| 3206 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3207 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3208 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3209 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3210 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3211 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3212 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3213 | return 1; |
| 3214 | } |
| 3215 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3216 | /* |
| 3217 | * |
| 3218 | * |
| 3219 | * Class Fetches |
| 3220 | * |
| 3221 | * |
| 3222 | */ |
| 3223 | |
| 3224 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3225 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3226 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3227 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3228 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3229 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3230 | } |
| 3231 | |
| 3232 | /* This function creates and push in the stack a fetch object according |
| 3233 | * with a current TXN. |
| 3234 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3235 | 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] | 3236 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3237 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3238 | |
| 3239 | /* Check stack size. */ |
| 3240 | if (!lua_checkstack(L, 3)) |
| 3241 | return 0; |
| 3242 | |
| 3243 | /* Create the object: obj[0] = userdata. |
| 3244 | * Note that the base of the Fetches object is the |
| 3245 | * transaction object. |
| 3246 | */ |
| 3247 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3248 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3249 | lua_rawseti(L, -2, 0); |
| 3250 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3251 | hsmp->s = txn->s; |
| 3252 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3253 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3254 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3255 | |
| 3256 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3257 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3258 | lua_setmetatable(L, -2); |
| 3259 | |
| 3260 | return 1; |
| 3261 | } |
| 3262 | |
| 3263 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3264 | * It uses closure argument to store the associated sample-fetch. It |
| 3265 | * returns only one argument or throws an error. An error is thrown |
| 3266 | * only if an error is encountered during the argument parsing. If |
| 3267 | * the "sample-fetch" function fails, nil is returned. |
| 3268 | */ |
| 3269 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3270 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3271 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3272 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3273 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3274 | int i; |
| 3275 | struct sample smp; |
| 3276 | |
| 3277 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3278 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3279 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3280 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3281 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3282 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3283 | /* Check execution authorization. */ |
| 3284 | if (f->use & SMP_USE_HTTP_ANY && |
| 3285 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3286 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3287 | "is not available in Lua services", f->kw); |
| 3288 | WILL_LJMP(lua_error(L)); |
| 3289 | } |
| 3290 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3291 | /* Get extra arguments. */ |
| 3292 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3293 | if (i >= ARGM_NBARGS) |
| 3294 | break; |
| 3295 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3296 | } |
| 3297 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3298 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3299 | |
| 3300 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3301 | 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] | 3302 | |
| 3303 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3304 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3305 | lua_pushfstring(L, "error in arguments"); |
| 3306 | WILL_LJMP(lua_error(L)); |
| 3307 | } |
| 3308 | |
| 3309 | /* Initialise the sample. */ |
| 3310 | memset(&smp, 0, sizeof(smp)); |
| 3311 | |
| 3312 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3313 | 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] | 3314 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3315 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3316 | lua_pushstring(L, ""); |
| 3317 | else |
| 3318 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3319 | return 1; |
| 3320 | } |
| 3321 | |
| 3322 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3323 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3324 | hlua_smp2lua_str(L, &smp); |
| 3325 | else |
| 3326 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3327 | return 1; |
| 3328 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3329 | |
| 3330 | /* |
| 3331 | * |
| 3332 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3333 | * Class Converters |
| 3334 | * |
| 3335 | * |
| 3336 | */ |
| 3337 | |
| 3338 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3339 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3340 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3341 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3342 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3343 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3344 | } |
| 3345 | |
| 3346 | /* This function creates and push in the stack a Converters object |
| 3347 | * according with a current TXN. |
| 3348 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3349 | 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] | 3350 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3351 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3352 | |
| 3353 | /* Check stack size. */ |
| 3354 | if (!lua_checkstack(L, 3)) |
| 3355 | return 0; |
| 3356 | |
| 3357 | /* Create the object: obj[0] = userdata. |
| 3358 | * Note that the base of the Converters object is the |
| 3359 | * same than the TXN object. |
| 3360 | */ |
| 3361 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3362 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3363 | lua_rawseti(L, -2, 0); |
| 3364 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3365 | hsmp->s = txn->s; |
| 3366 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3367 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3368 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3369 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3370 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3371 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3372 | lua_setmetatable(L, -2); |
| 3373 | |
| 3374 | return 1; |
| 3375 | } |
| 3376 | |
| 3377 | /* This function is an LUA binding. It is called with each converter. |
| 3378 | * It uses closure argument to store the associated converter. It |
| 3379 | * returns only one argument or throws an error. An error is thrown |
| 3380 | * only if an error is encountered during the argument parsing. If |
| 3381 | * the converter function function fails, nil is returned. |
| 3382 | */ |
| 3383 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3384 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3385 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3386 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3387 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3388 | int i; |
| 3389 | struct sample smp; |
| 3390 | |
| 3391 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3392 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3393 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3394 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3395 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3396 | |
| 3397 | /* Get extra arguments. */ |
| 3398 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3399 | if (i >= ARGM_NBARGS) |
| 3400 | break; |
| 3401 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3402 | } |
| 3403 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3404 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3405 | |
| 3406 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3407 | 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] | 3408 | |
| 3409 | /* Run the special args checker. */ |
| 3410 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3411 | hlua_pusherror(L, "error in arguments"); |
| 3412 | WILL_LJMP(lua_error(L)); |
| 3413 | } |
| 3414 | |
| 3415 | /* Initialise the sample. */ |
| 3416 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3417 | hlua_pusherror(L, "error in the input argument"); |
| 3418 | WILL_LJMP(lua_error(L)); |
| 3419 | } |
| 3420 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3421 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3422 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3423 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3424 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3425 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3426 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3427 | WILL_LJMP(lua_error(L)); |
| 3428 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3429 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3430 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3431 | hlua_pusherror(L, "error during the input argument casting"); |
| 3432 | WILL_LJMP(lua_error(L)); |
| 3433 | } |
| 3434 | |
| 3435 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3436 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3437 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3438 | lua_pushstring(L, ""); |
| 3439 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3440 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3441 | return 1; |
| 3442 | } |
| 3443 | |
| 3444 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3445 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3446 | hlua_smp2lua_str(L, &smp); |
| 3447 | else |
| 3448 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3449 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3450 | } |
| 3451 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3452 | /* |
| 3453 | * |
| 3454 | * |
| 3455 | * Class AppletTCP |
| 3456 | * |
| 3457 | * |
| 3458 | */ |
| 3459 | |
| 3460 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3461 | * a class stream, otherwise it throws an error. |
| 3462 | */ |
| 3463 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3464 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3465 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3466 | } |
| 3467 | |
| 3468 | /* This function creates and push in the stack an Applet object |
| 3469 | * according with a current TXN. |
| 3470 | */ |
| 3471 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3472 | { |
| 3473 | struct hlua_appctx *appctx; |
| 3474 | struct stream_interface *si = ctx->owner; |
| 3475 | struct stream *s = si_strm(si); |
| 3476 | struct proxy *p = s->be; |
| 3477 | |
| 3478 | /* Check stack size. */ |
| 3479 | if (!lua_checkstack(L, 3)) |
| 3480 | return 0; |
| 3481 | |
| 3482 | /* Create the object: obj[0] = userdata. |
| 3483 | * Note that the base of the Converters object is the |
| 3484 | * same than the TXN object. |
| 3485 | */ |
| 3486 | lua_newtable(L); |
| 3487 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3488 | lua_rawseti(L, -2, 0); |
| 3489 | appctx->appctx = ctx; |
| 3490 | appctx->htxn.s = s; |
| 3491 | appctx->htxn.p = p; |
| 3492 | |
| 3493 | /* Create the "f" field that contains a list of fetches. */ |
| 3494 | lua_pushstring(L, "f"); |
| 3495 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3496 | return 0; |
| 3497 | lua_settable(L, -3); |
| 3498 | |
| 3499 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3500 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3501 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3502 | return 0; |
| 3503 | lua_settable(L, -3); |
| 3504 | |
| 3505 | /* Create the "c" field that contains a list of converters. */ |
| 3506 | lua_pushstring(L, "c"); |
| 3507 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3508 | return 0; |
| 3509 | lua_settable(L, -3); |
| 3510 | |
| 3511 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3512 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3513 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3514 | return 0; |
| 3515 | lua_settable(L, -3); |
| 3516 | |
| 3517 | /* Pop a class stream metatable and affect it to the table. */ |
| 3518 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3519 | lua_setmetatable(L, -2); |
| 3520 | |
| 3521 | return 1; |
| 3522 | } |
| 3523 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3524 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3525 | { |
| 3526 | struct hlua_appctx *appctx; |
| 3527 | struct stream *s; |
| 3528 | const char *name; |
| 3529 | size_t len; |
| 3530 | struct sample smp; |
| 3531 | |
| 3532 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3533 | |
| 3534 | /* It is useles to retrieve the stream, but this function |
| 3535 | * runs only in a stream context. |
| 3536 | */ |
| 3537 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3538 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3539 | s = appctx->htxn.s; |
| 3540 | |
| 3541 | /* Converts the third argument in a sample. */ |
| 3542 | hlua_lua2smp(L, 3, &smp); |
| 3543 | |
| 3544 | /* Store the sample in a variable. */ |
| 3545 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3546 | vars_set_by_name(name, len, &smp); |
| 3547 | return 0; |
| 3548 | } |
| 3549 | |
| 3550 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3551 | { |
| 3552 | struct hlua_appctx *appctx; |
| 3553 | struct stream *s; |
| 3554 | const char *name; |
| 3555 | size_t len; |
| 3556 | struct sample smp; |
| 3557 | |
| 3558 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3559 | |
| 3560 | /* It is useles to retrieve the stream, but this function |
| 3561 | * runs only in a stream context. |
| 3562 | */ |
| 3563 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3564 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3565 | s = appctx->htxn.s; |
| 3566 | |
| 3567 | /* Unset the variable. */ |
| 3568 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3569 | vars_unset_by_name(name, len, &smp); |
| 3570 | return 0; |
| 3571 | } |
| 3572 | |
| 3573 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3574 | { |
| 3575 | struct hlua_appctx *appctx; |
| 3576 | struct stream *s; |
| 3577 | const char *name; |
| 3578 | size_t len; |
| 3579 | struct sample smp; |
| 3580 | |
| 3581 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3582 | |
| 3583 | /* It is useles to retrieve the stream, but this function |
| 3584 | * runs only in a stream context. |
| 3585 | */ |
| 3586 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3587 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3588 | s = appctx->htxn.s; |
| 3589 | |
| 3590 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3591 | if (!vars_get_by_name(name, len, &smp)) { |
| 3592 | lua_pushnil(L); |
| 3593 | return 1; |
| 3594 | } |
| 3595 | |
| 3596 | return hlua_smp2lua(L, &smp); |
| 3597 | } |
| 3598 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3599 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3600 | { |
| 3601 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3602 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3603 | struct hlua *hlua; |
| 3604 | |
| 3605 | /* 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] | 3606 | if (!s->hlua) |
| 3607 | return 0; |
| 3608 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3609 | |
| 3610 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3611 | |
| 3612 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3613 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3614 | |
| 3615 | /* Get and store new value. */ |
| 3616 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3617 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3618 | |
| 3619 | return 0; |
| 3620 | } |
| 3621 | |
| 3622 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3623 | { |
| 3624 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3625 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3626 | struct hlua *hlua; |
| 3627 | |
| 3628 | /* 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] | 3629 | if (!s->hlua) { |
| 3630 | lua_pushnil(L); |
| 3631 | return 1; |
| 3632 | } |
| 3633 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3634 | |
| 3635 | /* Push configuration index in the stack. */ |
| 3636 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3637 | |
| 3638 | return 1; |
| 3639 | } |
| 3640 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3641 | /* If expected data not yet available, it returns a yield. This function |
| 3642 | * consumes the data in the buffer. It returns a string containing the |
| 3643 | * data. This string can be empty. |
| 3644 | */ |
| 3645 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3646 | { |
| 3647 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3648 | struct stream_interface *si = appctx->appctx->owner; |
| 3649 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3650 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3651 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3652 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3653 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3654 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3655 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3656 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3657 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3658 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3659 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3660 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3661 | 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] | 3662 | } |
| 3663 | |
| 3664 | /* End of data: commit the total strings and return. */ |
| 3665 | if (ret < 0) { |
| 3666 | luaL_pushresult(&appctx->b); |
| 3667 | return 1; |
| 3668 | } |
| 3669 | |
| 3670 | /* Ensure that the block 2 length is usable. */ |
| 3671 | if (ret == 1) |
| 3672 | len2 = 0; |
| 3673 | |
| 3674 | /* dont check the max length read and dont check. */ |
| 3675 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3676 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3677 | |
| 3678 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3679 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3680 | luaL_pushresult(&appctx->b); |
| 3681 | return 1; |
| 3682 | } |
| 3683 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3684 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3685 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3686 | { |
| 3687 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3688 | |
| 3689 | /* Initialise the string catenation. */ |
| 3690 | luaL_buffinit(L, &appctx->b); |
| 3691 | |
| 3692 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3693 | } |
| 3694 | |
| 3695 | /* If expected data not yet available, it returns a yield. This function |
| 3696 | * consumes the data in the buffer. It returns a string containing the |
| 3697 | * data. This string can be empty. |
| 3698 | */ |
| 3699 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3700 | { |
| 3701 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3702 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3703 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3704 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3705 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3706 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3707 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3708 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3709 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3710 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3711 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3712 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3713 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3714 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3715 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3716 | 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] | 3717 | } |
| 3718 | |
| 3719 | /* End of data: commit the total strings and return. */ |
| 3720 | if (ret < 0) { |
| 3721 | luaL_pushresult(&appctx->b); |
| 3722 | return 1; |
| 3723 | } |
| 3724 | |
| 3725 | /* Ensure that the block 2 length is usable. */ |
| 3726 | if (ret == 1) |
| 3727 | len2 = 0; |
| 3728 | |
| 3729 | if (len == -1) { |
| 3730 | |
| 3731 | /* If len == -1, catenate all the data avalaile and |
| 3732 | * yield because we want to get all the data until |
| 3733 | * the end of data stream. |
| 3734 | */ |
| 3735 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3736 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3737 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3738 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3739 | 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] | 3740 | |
| 3741 | } else { |
| 3742 | |
| 3743 | /* Copy the fisrt block caping to the length required. */ |
| 3744 | if (len1 > len) |
| 3745 | len1 = len; |
| 3746 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3747 | len -= len1; |
| 3748 | |
| 3749 | /* Copy the second block. */ |
| 3750 | if (len2 > len) |
| 3751 | len2 = len; |
| 3752 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3753 | len -= len2; |
| 3754 | |
| 3755 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3756 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3757 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3758 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3759 | if (len > 0) { |
| 3760 | lua_pushinteger(L, len); |
| 3761 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3762 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3763 | 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] | 3764 | } |
| 3765 | |
| 3766 | /* return the result. */ |
| 3767 | luaL_pushresult(&appctx->b); |
| 3768 | return 1; |
| 3769 | } |
| 3770 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3771 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3772 | hlua_pusherror(L, "Lua: internal error"); |
| 3773 | WILL_LJMP(lua_error(L)); |
| 3774 | return 0; |
| 3775 | } |
| 3776 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3777 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3778 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3779 | { |
| 3780 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3781 | int len = -1; |
| 3782 | |
| 3783 | if (lua_gettop(L) > 2) |
| 3784 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3785 | if (lua_gettop(L) >= 2) { |
| 3786 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3787 | lua_pop(L, 1); |
| 3788 | } |
| 3789 | |
| 3790 | /* Confirm or set the required length */ |
| 3791 | lua_pushinteger(L, len); |
| 3792 | |
| 3793 | /* Initialise the string catenation. */ |
| 3794 | luaL_buffinit(L, &appctx->b); |
| 3795 | |
| 3796 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3797 | } |
| 3798 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3799 | /* Append data in the output side of the buffer. This data is immediately |
| 3800 | * sent. The function returns the amount of data written. If the buffer |
| 3801 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3802 | * if the channel is closed. |
| 3803 | */ |
| 3804 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3805 | { |
| 3806 | size_t len; |
| 3807 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3808 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3809 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3810 | struct stream_interface *si = appctx->appctx->owner; |
| 3811 | struct channel *chn = si_ic(si); |
| 3812 | int max; |
| 3813 | |
| 3814 | /* Get the max amount of data which can write as input in the channel. */ |
| 3815 | max = channel_recv_max(chn); |
| 3816 | if (max > (len - l)) |
| 3817 | max = len - l; |
| 3818 | |
| 3819 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3820 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3821 | |
| 3822 | /* update counters. */ |
| 3823 | l += max; |
| 3824 | lua_pop(L, 1); |
| 3825 | lua_pushinteger(L, l); |
| 3826 | |
| 3827 | /* If some data is not send, declares the situation to the |
| 3828 | * applet, and returns a yield. |
| 3829 | */ |
| 3830 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3831 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3832 | 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] | 3833 | } |
| 3834 | |
| 3835 | return 1; |
| 3836 | } |
| 3837 | |
| 3838 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3839 | * yield the LUA process, and resume it without checking the |
| 3840 | * input arguments. |
| 3841 | */ |
| 3842 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3843 | { |
| 3844 | MAY_LJMP(check_args(L, 2, "send")); |
| 3845 | lua_pushinteger(L, 0); |
| 3846 | |
| 3847 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3848 | } |
| 3849 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3850 | /* |
| 3851 | * |
| 3852 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3853 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3854 | * |
| 3855 | * |
| 3856 | */ |
| 3857 | |
| 3858 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3859 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3860 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3861 | __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] | 3862 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3863 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3864 | } |
| 3865 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3866 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3867 | * according with a current TXN. |
| 3868 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3869 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3870 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3871 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3872 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3873 | struct stream_interface *si = ctx->owner; |
| 3874 | struct stream *s = si_strm(si); |
| 3875 | struct proxy *px = s->be; |
| 3876 | struct http_txn *txn = s->txn; |
| 3877 | const char *path; |
| 3878 | const char *end; |
| 3879 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3880 | |
| 3881 | /* Check stack size. */ |
| 3882 | if (!lua_checkstack(L, 3)) |
| 3883 | return 0; |
| 3884 | |
| 3885 | /* Create the object: obj[0] = userdata. |
| 3886 | * Note that the base of the Converters object is the |
| 3887 | * same than the TXN object. |
| 3888 | */ |
| 3889 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3890 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3891 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3892 | appctx->appctx = ctx; |
| 3893 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3894 | 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] | 3895 | appctx->htxn.s = s; |
| 3896 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3897 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3898 | /* Create the "f" field that contains a list of fetches. */ |
| 3899 | lua_pushstring(L, "f"); |
| 3900 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3901 | return 0; |
| 3902 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3903 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3904 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3905 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3906 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3907 | return 0; |
| 3908 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3909 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3910 | /* Create the "c" field that contains a list of converters. */ |
| 3911 | lua_pushstring(L, "c"); |
| 3912 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3913 | return 0; |
| 3914 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3915 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3916 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3917 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3918 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3919 | return 0; |
| 3920 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3921 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3922 | /* Stores the request method. */ |
| 3923 | lua_pushstring(L, "method"); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3924 | lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3925 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3926 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3927 | /* Stores the http version. */ |
| 3928 | lua_pushstring(L, "version"); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3929 | lua_pushlstring(L, ci_head(txn->req.chn) + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3930 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3931 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3932 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3933 | * the array on the top of the stack. |
| 3934 | */ |
| 3935 | lua_pushstring(L, "headers"); |
| 3936 | htxn.s = s; |
| 3937 | htxn.p = px; |
| 3938 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3939 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3940 | return 0; |
| 3941 | lua_settable(L, -3); |
| 3942 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3943 | /* Get path and qs */ |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 3944 | path = http_txn_get_path(txn); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3945 | if (path) { |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3946 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3947 | p = path; |
| 3948 | while (p < end && *p != '?') |
| 3949 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3950 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3951 | /* Stores the request path. */ |
| 3952 | lua_pushstring(L, "path"); |
| 3953 | lua_pushlstring(L, path, p - path); |
| 3954 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3955 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3956 | /* Stores the query string. */ |
| 3957 | lua_pushstring(L, "qs"); |
| 3958 | if (*p == '?') |
| 3959 | p++; |
| 3960 | lua_pushlstring(L, p, end - p); |
| 3961 | lua_settable(L, -3); |
| 3962 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3963 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3964 | /* Stores the request path. */ |
| 3965 | lua_pushstring(L, "length"); |
| 3966 | lua_pushinteger(L, txn->req.body_len); |
| 3967 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3968 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3969 | /* Create an array of HTTP request headers. */ |
| 3970 | lua_pushstring(L, "headers"); |
| 3971 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3972 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3973 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3974 | /* Create an empty array of HTTP request headers. */ |
| 3975 | lua_pushstring(L, "response"); |
| 3976 | lua_newtable(L); |
| 3977 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3978 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3979 | /* Pop a class stream metatable and affect it to the table. */ |
| 3980 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3981 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3982 | |
| 3983 | return 1; |
| 3984 | } |
| 3985 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3986 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3987 | { |
| 3988 | struct hlua_appctx *appctx; |
| 3989 | struct stream *s; |
| 3990 | const char *name; |
| 3991 | size_t len; |
| 3992 | struct sample smp; |
| 3993 | |
| 3994 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3995 | |
| 3996 | /* It is useles to retrieve the stream, but this function |
| 3997 | * runs only in a stream context. |
| 3998 | */ |
| 3999 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4000 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4001 | s = appctx->htxn.s; |
| 4002 | |
| 4003 | /* Converts the third argument in a sample. */ |
| 4004 | hlua_lua2smp(L, 3, &smp); |
| 4005 | |
| 4006 | /* Store the sample in a variable. */ |
| 4007 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4008 | vars_set_by_name(name, len, &smp); |
| 4009 | return 0; |
| 4010 | } |
| 4011 | |
| 4012 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4013 | { |
| 4014 | struct hlua_appctx *appctx; |
| 4015 | struct stream *s; |
| 4016 | const char *name; |
| 4017 | size_t len; |
| 4018 | struct sample smp; |
| 4019 | |
| 4020 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4021 | |
| 4022 | /* It is useles to retrieve the stream, but this function |
| 4023 | * runs only in a stream context. |
| 4024 | */ |
| 4025 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4026 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4027 | s = appctx->htxn.s; |
| 4028 | |
| 4029 | /* Unset the variable. */ |
| 4030 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4031 | vars_unset_by_name(name, len, &smp); |
| 4032 | return 0; |
| 4033 | } |
| 4034 | |
| 4035 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4036 | { |
| 4037 | struct hlua_appctx *appctx; |
| 4038 | struct stream *s; |
| 4039 | const char *name; |
| 4040 | size_t len; |
| 4041 | struct sample smp; |
| 4042 | |
| 4043 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4044 | |
| 4045 | /* It is useles to retrieve the stream, but this function |
| 4046 | * runs only in a stream context. |
| 4047 | */ |
| 4048 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4049 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4050 | s = appctx->htxn.s; |
| 4051 | |
| 4052 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4053 | if (!vars_get_by_name(name, len, &smp)) { |
| 4054 | lua_pushnil(L); |
| 4055 | return 1; |
| 4056 | } |
| 4057 | |
| 4058 | return hlua_smp2lua(L, &smp); |
| 4059 | } |
| 4060 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4061 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4062 | { |
| 4063 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4064 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4065 | struct hlua *hlua; |
| 4066 | |
| 4067 | /* 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] | 4068 | if (!s->hlua) |
| 4069 | return 0; |
| 4070 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4071 | |
| 4072 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4073 | |
| 4074 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4075 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4076 | |
| 4077 | /* Get and store new value. */ |
| 4078 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4079 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4080 | |
| 4081 | return 0; |
| 4082 | } |
| 4083 | |
| 4084 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4085 | { |
| 4086 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4087 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4088 | struct hlua *hlua; |
| 4089 | |
| 4090 | /* 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] | 4091 | if (!s->hlua) { |
| 4092 | lua_pushnil(L); |
| 4093 | return 1; |
| 4094 | } |
| 4095 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4096 | |
| 4097 | /* Push configuration index in the stack. */ |
| 4098 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4099 | |
| 4100 | return 1; |
| 4101 | } |
| 4102 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4103 | /* If expected data not yet available, it returns a yield. This function |
| 4104 | * consumes the data in the buffer. It returns a string containing the |
| 4105 | * data. This string can be empty. |
| 4106 | */ |
| 4107 | __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] | 4108 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4109 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4110 | struct stream_interface *si = appctx->appctx->owner; |
| 4111 | struct channel *chn = si_ic(si); |
| 4112 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4113 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4114 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4115 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4116 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4117 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4118 | /* Maybe we cant send a 100-continue ? */ |
| 4119 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4120 | ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4121 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4122 | * big for the buffers. We cant send anything. So, we ignoring |
| 4123 | * the error, considers that the 100-continue is sent, and try |
| 4124 | * to receive. |
| 4125 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4126 | */ |
| 4127 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4128 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4129 | 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] | 4130 | } |
| 4131 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4132 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4133 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4134 | /* Check for the end of the data. */ |
| 4135 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4136 | luaL_pushresult(&appctx->b); |
| 4137 | return 1; |
| 4138 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4139 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4140 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4141 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4142 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4143 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4144 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4145 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4146 | 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] | 4147 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4148 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4149 | /* End of data: commit the total strings and return. */ |
| 4150 | if (ret < 0) { |
| 4151 | luaL_pushresult(&appctx->b); |
| 4152 | return 1; |
| 4153 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4154 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4155 | /* Ensure that the block 2 length is usable. */ |
| 4156 | if (ret == 1) |
| 4157 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4158 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4159 | /* Copy the fisrt block caping to the length required. */ |
| 4160 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4161 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4162 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4163 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4164 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4165 | /* Copy the second block. */ |
| 4166 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4167 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4168 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4169 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4170 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4171 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4172 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4173 | luaL_pushresult(&appctx->b); |
| 4174 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4175 | } |
| 4176 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4177 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4178 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4179 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4180 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4181 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4182 | /* Initialise the string catenation. */ |
| 4183 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4184 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4185 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4186 | } |
| 4187 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4188 | /* If expected data not yet available, it returns a yield. This function |
| 4189 | * consumes the data in the buffer. It returns a string containing the |
| 4190 | * data. This string can be empty. |
| 4191 | */ |
| 4192 | __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] | 4193 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4194 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4195 | struct stream_interface *si = appctx->appctx->owner; |
| 4196 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4197 | struct channel *chn = si_ic(si); |
| 4198 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4199 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4200 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4201 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4202 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4203 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4204 | /* Maybe we cant send a 100-continue ? */ |
| 4205 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4206 | ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4207 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4208 | * big for the buffers. We cant send anything. So, we ignoring |
| 4209 | * the error, considers that the 100-continue is sent, and try |
| 4210 | * to receive. |
| 4211 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4212 | */ |
| 4213 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4214 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4215 | 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] | 4216 | } |
| 4217 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4218 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4219 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4220 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4221 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4222 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4223 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4224 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4225 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4226 | 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] | 4227 | } |
| 4228 | |
| 4229 | /* End of data: commit the total strings and return. */ |
| 4230 | if (ret < 0) { |
| 4231 | luaL_pushresult(&appctx->b); |
| 4232 | return 1; |
| 4233 | } |
| 4234 | |
| 4235 | /* Ensure that the block 2 length is usable. */ |
| 4236 | if (ret == 1) |
| 4237 | len2 = 0; |
| 4238 | |
| 4239 | /* Copy the fisrt block caping to the length required. */ |
| 4240 | if (len1 > len) |
| 4241 | len1 = len; |
| 4242 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4243 | len -= len1; |
| 4244 | |
| 4245 | /* Copy the second block. */ |
| 4246 | if (len2 > len) |
| 4247 | len2 = len; |
| 4248 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4249 | len -= len2; |
| 4250 | |
| 4251 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4252 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4253 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4254 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4255 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4256 | /* If we are no other data available, yield waiting for new data. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4257 | if (len > 0) { |
| 4258 | lua_pushinteger(L, len); |
| 4259 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4260 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4261 | 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] | 4262 | } |
| 4263 | |
| 4264 | /* return the result. */ |
| 4265 | luaL_pushresult(&appctx->b); |
| 4266 | return 1; |
| 4267 | } |
| 4268 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4269 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4270 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4271 | { |
| 4272 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4273 | int len = -1; |
| 4274 | |
| 4275 | /* Check arguments. */ |
| 4276 | if (lua_gettop(L) > 2) |
| 4277 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4278 | if (lua_gettop(L) >= 2) { |
| 4279 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4280 | lua_pop(L, 1); |
| 4281 | } |
| 4282 | |
| 4283 | /* Check the required length */ |
| 4284 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4285 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4286 | lua_pushinteger(L, len); |
| 4287 | |
| 4288 | /* Initialise the string catenation. */ |
| 4289 | luaL_buffinit(L, &appctx->b); |
| 4290 | |
| 4291 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4292 | } |
| 4293 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4294 | /* Append data in the output side of the buffer. This data is immediately |
| 4295 | * sent. The function returns the amount of data written. If the buffer |
| 4296 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4297 | * if the channel is closed. |
| 4298 | */ |
| 4299 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4300 | { |
| 4301 | size_t len; |
| 4302 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4303 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4304 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4305 | struct stream_interface *si = appctx->appctx->owner; |
| 4306 | struct channel *chn = si_ic(si); |
| 4307 | int max; |
| 4308 | |
| 4309 | /* Get the max amount of data which can write as input in the channel. */ |
| 4310 | max = channel_recv_max(chn); |
| 4311 | if (max > (len - l)) |
| 4312 | max = len - l; |
| 4313 | |
| 4314 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4315 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4316 | |
| 4317 | /* update counters. */ |
| 4318 | l += max; |
| 4319 | lua_pop(L, 1); |
| 4320 | lua_pushinteger(L, l); |
| 4321 | |
| 4322 | /* If some data is not send, declares the situation to the |
| 4323 | * applet, and returns a yield. |
| 4324 | */ |
| 4325 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4326 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4327 | 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] | 4328 | } |
| 4329 | |
| 4330 | return 1; |
| 4331 | } |
| 4332 | |
| 4333 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4334 | * yield the LUA process, and resume it without checking the |
| 4335 | * input arguments. |
| 4336 | */ |
| 4337 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4338 | { |
| 4339 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4340 | size_t len; |
| 4341 | char hex[10]; |
| 4342 | |
| 4343 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4344 | |
| 4345 | /* If transfer encoding chunked is selected, we surround the data |
| 4346 | * by chunk data. |
| 4347 | */ |
| 4348 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4349 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4350 | lua_pushfstring(L, "%s\r\n", hex); |
| 4351 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4352 | lua_pushstring(L, "\r\n"); |
| 4353 | lua_concat(L, 3); |
| 4354 | } |
| 4355 | |
| 4356 | /* This interger is used for followinf the amount of data sent. */ |
| 4357 | lua_pushinteger(L, 0); |
| 4358 | |
| 4359 | /* We want to send some data. Headers must be sent. */ |
| 4360 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4361 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4362 | WILL_LJMP(lua_error(L)); |
| 4363 | } |
| 4364 | |
| 4365 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4366 | } |
| 4367 | |
| 4368 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4369 | { |
| 4370 | const char *name; |
| 4371 | int ret; |
| 4372 | |
| 4373 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4374 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4375 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4376 | |
| 4377 | /* Push in the stack the "response" entry. */ |
| 4378 | ret = lua_getfield(L, 1, "response"); |
| 4379 | if (ret != LUA_TTABLE) { |
| 4380 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4381 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4382 | WILL_LJMP(lua_error(L)); |
| 4383 | } |
| 4384 | |
| 4385 | /* check if the header is already registered if it is not |
| 4386 | * the case, register it. |
| 4387 | */ |
| 4388 | ret = lua_getfield(L, -1, name); |
| 4389 | if (ret == LUA_TNIL) { |
| 4390 | |
| 4391 | /* Entry not found. */ |
| 4392 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4393 | |
| 4394 | /* Insert the new header name in the array in the top of the stack. |
| 4395 | * It left the new array in the top of the stack. |
| 4396 | */ |
| 4397 | lua_newtable(L); |
| 4398 | lua_pushvalue(L, 2); |
| 4399 | lua_pushvalue(L, -2); |
| 4400 | lua_settable(L, -4); |
| 4401 | |
| 4402 | } else if (ret != LUA_TTABLE) { |
| 4403 | |
| 4404 | /* corruption error. */ |
| 4405 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4406 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4407 | WILL_LJMP(lua_error(L)); |
| 4408 | } |
| 4409 | |
| 4410 | /* Now the top od thestack is an array of values. We push |
| 4411 | * the header value as new entry. |
| 4412 | */ |
| 4413 | lua_pushvalue(L, 3); |
| 4414 | ret = lua_rawlen(L, -2); |
| 4415 | lua_rawseti(L, -2, ret + 1); |
| 4416 | lua_pushboolean(L, 1); |
| 4417 | return 1; |
| 4418 | } |
| 4419 | |
| 4420 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4421 | { |
| 4422 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4423 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4424 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4425 | |
| 4426 | if (status < 100 || status > 599) { |
| 4427 | lua_pushboolean(L, 0); |
| 4428 | return 1; |
| 4429 | } |
| 4430 | |
| 4431 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4432 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4433 | lua_pushboolean(L, 1); |
| 4434 | return 1; |
| 4435 | } |
| 4436 | |
| 4437 | /* We will build the status line and the headers of the HTTP response. |
| 4438 | * We will try send at once if its not possible, we give back the hand |
| 4439 | * waiting for more room. |
| 4440 | */ |
| 4441 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4442 | { |
| 4443 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4444 | struct stream_interface *si = appctx->appctx->owner; |
| 4445 | struct channel *chn = si_ic(si); |
| 4446 | int ret; |
| 4447 | size_t len; |
| 4448 | const char *msg; |
| 4449 | |
| 4450 | /* Get the message as the first argument on the stack. */ |
| 4451 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4452 | |
| 4453 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4454 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4455 | |
| 4456 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4457 | * big for the buffers. |
| 4458 | */ |
| 4459 | if (ret == -2 || ret == -3) { |
| 4460 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4461 | WILL_LJMP(lua_error(L)); |
| 4462 | } |
| 4463 | |
| 4464 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4465 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4466 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4467 | 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] | 4468 | } |
| 4469 | |
| 4470 | /* Headers sent, set the flag. */ |
| 4471 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4472 | return 0; |
| 4473 | } |
| 4474 | |
| 4475 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4476 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4477 | struct buffer *tmp = get_trash_chunk(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4478 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4479 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4480 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4481 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4482 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4483 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4484 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4485 | int hdr_chunked = 0; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4486 | const char *reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4487 | |
| 4488 | if (reason == NULL) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4489 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4490 | |
| 4491 | /* Use the same http version than the request. */ |
| 4492 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4493 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4494 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4495 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4496 | |
| 4497 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4498 | lua_pushvalue(L, 0); |
| 4499 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4500 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4501 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4502 | WILL_LJMP(lua_error(L)); |
| 4503 | } |
| 4504 | |
| 4505 | /* Browse the list of headers. */ |
| 4506 | lua_pushnil(L); |
| 4507 | while(lua_next(L, -2) != 0) { |
| 4508 | |
| 4509 | /* We expect a string as -2. */ |
| 4510 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4511 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4512 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4513 | lua_typename(L, lua_type(L, -2))); |
| 4514 | WILL_LJMP(lua_error(L)); |
| 4515 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4516 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4517 | |
| 4518 | /* We expect an array as -1. */ |
| 4519 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4520 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4521 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4522 | name, |
| 4523 | lua_typename(L, lua_type(L, -1))); |
| 4524 | WILL_LJMP(lua_error(L)); |
| 4525 | } |
| 4526 | |
| 4527 | /* Browse the table who is on the top of the stack. */ |
| 4528 | lua_pushnil(L); |
| 4529 | while(lua_next(L, -2) != 0) { |
| 4530 | |
| 4531 | /* We expect a number as -2. */ |
| 4532 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4533 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4534 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4535 | name, |
| 4536 | lua_typename(L, lua_type(L, -2))); |
| 4537 | WILL_LJMP(lua_error(L)); |
| 4538 | } |
| 4539 | id = lua_tointeger(L, -2); |
| 4540 | |
| 4541 | /* We expect a string as -2. */ |
| 4542 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4543 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4544 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4545 | name, id, |
| 4546 | lua_typename(L, lua_type(L, -1))); |
| 4547 | WILL_LJMP(lua_error(L)); |
| 4548 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4549 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4550 | |
| 4551 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4552 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 4553 | memcpy(tmp->area + tmp->data, name, name_len); |
| 4554 | tmp->data += name_len; |
| 4555 | tmp->area[tmp->data++] = ':'; |
| 4556 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4557 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4558 | memcpy(tmp->area + tmp->data, value, |
| 4559 | value_len); |
| 4560 | tmp->data += value_len; |
| 4561 | tmp->area[tmp->data++] = '\r'; |
| 4562 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4563 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4564 | |
| 4565 | /* Protocol checks. */ |
| 4566 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4567 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4568 | * is done without control. If it contains a bad value, |
| 4569 | * the content-length remains negative so that we can |
| 4570 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4571 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4572 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4573 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4574 | |
| 4575 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4576 | if (name_len == 17 && value_len == 7 && |
| 4577 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4578 | strcasecmp("chunked", value) == 0) |
| 4579 | hdr_chunked = 1; |
| 4580 | |
| 4581 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4582 | lua_pop(L, 1); |
| 4583 | } |
| 4584 | |
| 4585 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4586 | lua_pop(L, 1); |
| 4587 | } |
| 4588 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4589 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4590 | * and the status code implies the presence of a message body, we must |
| 4591 | * announce a transfer encoding chunked. This is required by haproxy |
| 4592 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4593 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4594 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4595 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4596 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 4597 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4598 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4599 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4600 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4601 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4602 | } |
| 4603 | |
| 4604 | /* Finalize headers. */ |
| 4605 | chunk_appendf(tmp, "\r\n"); |
| 4606 | |
| 4607 | /* Remove the last entry and the array of headers */ |
| 4608 | lua_pop(L, 2); |
| 4609 | |
| 4610 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4611 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4612 | |
| 4613 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4614 | } |
| 4615 | |
| 4616 | /* |
| 4617 | * |
| 4618 | * |
| 4619 | * Class HTTP |
| 4620 | * |
| 4621 | * |
| 4622 | */ |
| 4623 | |
| 4624 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4625 | * a class stream, otherwise it throws an error. |
| 4626 | */ |
| 4627 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4628 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4629 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4630 | } |
| 4631 | |
| 4632 | /* This function creates and push in the stack a HTTP object |
| 4633 | * according with a current TXN. |
| 4634 | */ |
| 4635 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4636 | { |
| 4637 | struct hlua_txn *htxn; |
| 4638 | |
| 4639 | /* Check stack size. */ |
| 4640 | if (!lua_checkstack(L, 3)) |
| 4641 | return 0; |
| 4642 | |
| 4643 | /* Create the object: obj[0] = userdata. |
| 4644 | * Note that the base of the Converters object is the |
| 4645 | * same than the TXN object. |
| 4646 | */ |
| 4647 | lua_newtable(L); |
| 4648 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4649 | lua_rawseti(L, -2, 0); |
| 4650 | |
| 4651 | htxn->s = txn->s; |
| 4652 | htxn->p = txn->p; |
| 4653 | |
| 4654 | /* Pop a class stream metatable and affect it to the table. */ |
| 4655 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4656 | lua_setmetatable(L, -2); |
| 4657 | |
| 4658 | return 1; |
| 4659 | } |
| 4660 | |
| 4661 | /* This function creates ans returns an array of HTTP headers. |
| 4662 | * This function does not fails. It is used as wrapper with the |
| 4663 | * 2 following functions. |
| 4664 | */ |
| 4665 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4666 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4667 | /* Create the table. */ |
| 4668 | lua_newtable(L); |
| 4669 | |
| 4670 | if (!htxn->s->txn) |
| 4671 | return 1; |
| 4672 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4673 | /* Check if a valid response is parsed */ |
| 4674 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4675 | return 1; |
| 4676 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4677 | if (IS_HTX_STRM(htxn->s)) { |
| 4678 | /* HTX version */ |
| 4679 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 4680 | int32_t pos; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4681 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4682 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4683 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4684 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4685 | struct ist n, v; |
| 4686 | int len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4687 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4688 | if (type == HTX_BLK_HDR) { |
| 4689 | n = htx_get_blk_name(htx,blk); |
| 4690 | v = htx_get_blk_value(htx, blk); |
| 4691 | } |
| 4692 | else if (type == HTX_BLK_EOH) |
| 4693 | break; |
| 4694 | else |
| 4695 | continue; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4696 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4697 | /* Check for existing entry: |
| 4698 | * assume that the table is on the top of the stack, and |
| 4699 | * push the key in the stack, the function lua_gettable() |
| 4700 | * perform the lookup. |
| 4701 | */ |
| 4702 | lua_pushlstring(L, n.ptr, n.len); |
| 4703 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4704 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4705 | switch (lua_type(L, -1)) { |
| 4706 | case LUA_TNIL: |
| 4707 | /* Table not found, create it. */ |
| 4708 | lua_pop(L, 1); /* remove the nil value. */ |
| 4709 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4710 | lua_newtable(L); /* create and push empty table. */ |
| 4711 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4712 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4713 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4714 | break; |
| 4715 | |
| 4716 | case LUA_TTABLE: |
| 4717 | /* Entry found: push the value in the table. */ |
| 4718 | len = lua_rawlen(L, -1); |
| 4719 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4720 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4721 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4722 | break; |
| 4723 | |
| 4724 | default: |
| 4725 | /* Other cases are errors. */ |
| 4726 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4727 | WILL_LJMP(lua_error(L)); |
| 4728 | } |
| 4729 | } |
| 4730 | } |
| 4731 | else { |
| 4732 | /* Legacy HTTP version */ |
| 4733 | const char *cur_ptr, *cur_next, *p; |
| 4734 | int old_idx, cur_idx; |
| 4735 | struct hdr_idx_elem *cur_hdr; |
| 4736 | const char *hn, *hv; |
| 4737 | int hnl, hvl; |
| 4738 | const char *in; |
| 4739 | char *out; |
| 4740 | int len; |
| 4741 | |
| 4742 | /* Build array of headers. */ |
| 4743 | old_idx = 0; |
| 4744 | cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 4745 | |
| 4746 | while (1) { |
| 4747 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4748 | if (!cur_idx) |
| 4749 | break; |
| 4750 | old_idx = cur_idx; |
| 4751 | |
| 4752 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4753 | cur_ptr = cur_next; |
| 4754 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4755 | |
| 4756 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4757 | * and the next header starts at cur_next. We'll check |
| 4758 | * this header in the list as well as against the default |
| 4759 | * rule. |
| 4760 | */ |
| 4761 | |
| 4762 | /* look for ': *'. */ |
| 4763 | hn = cur_ptr; |
| 4764 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4765 | if (p >= cur_ptr+cur_hdr->len) |
| 4766 | continue; |
| 4767 | hnl = p - hn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4768 | p++; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4769 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4770 | p++; |
| 4771 | if (p >= cur_ptr+cur_hdr->len) |
| 4772 | continue; |
| 4773 | hv = p; |
| 4774 | hvl = cur_ptr+cur_hdr->len-p; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4775 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4776 | /* Lowercase the key. Don't check the size of trash, it have |
| 4777 | * the size of one buffer and the input data contains in one |
| 4778 | * buffer. |
| 4779 | */ |
| 4780 | out = trash.area; |
| 4781 | for (in=hn; in<hn+hnl; in++, out++) |
| 4782 | *out = tolower(*in); |
| 4783 | *out = '\0'; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4784 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4785 | /* Check for existing entry: |
| 4786 | * assume that the table is on the top of the stack, and |
| 4787 | * push the key in the stack, the function lua_gettable() |
| 4788 | * perform the lookup. |
| 4789 | */ |
| 4790 | lua_pushlstring(L, trash.area, hnl); |
| 4791 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4792 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4793 | switch (lua_type(L, -1)) { |
| 4794 | case LUA_TNIL: |
| 4795 | /* Table not found, create it. */ |
| 4796 | lua_pop(L, 1); /* remove the nil value. */ |
| 4797 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
| 4798 | lua_newtable(L); /* create and push empty table. */ |
| 4799 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4800 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4801 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4802 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4803 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4804 | case LUA_TTABLE: |
| 4805 | /* Entry found: push the value in the table. */ |
| 4806 | len = lua_rawlen(L, -1); |
| 4807 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4808 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4809 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4810 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4811 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4812 | default: |
| 4813 | /* Other cases are errors. */ |
| 4814 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4815 | WILL_LJMP(lua_error(L)); |
| 4816 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4817 | } |
| 4818 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4819 | return 1; |
| 4820 | } |
| 4821 | |
| 4822 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4823 | { |
| 4824 | struct hlua_txn *htxn; |
| 4825 | |
| 4826 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4827 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4828 | |
| 4829 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4830 | } |
| 4831 | |
| 4832 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4833 | { |
| 4834 | struct hlua_txn *htxn; |
| 4835 | |
| 4836 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4837 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4838 | |
| 4839 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4840 | } |
| 4841 | |
| 4842 | /* This function replace full header, or just a value in |
| 4843 | * the request or in the response. It is a wrapper fir the |
| 4844 | * 4 following functions. |
| 4845 | */ |
| 4846 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4847 | struct http_msg *msg, int action) |
| 4848 | { |
| 4849 | size_t name_len; |
| 4850 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4851 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4852 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4853 | struct my_regex re; |
| 4854 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4855 | /* Check if a valid response is parsed */ |
| 4856 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4857 | return 0; |
| 4858 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4859 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4860 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4861 | |
| 4862 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4863 | regex_free(&re); |
| 4864 | return 0; |
| 4865 | } |
| 4866 | |
| 4867 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4868 | { |
| 4869 | struct hlua_txn *htxn; |
| 4870 | |
| 4871 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4872 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4873 | |
| 4874 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4875 | } |
| 4876 | |
| 4877 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4878 | { |
| 4879 | struct hlua_txn *htxn; |
| 4880 | |
| 4881 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4882 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4883 | |
| 4884 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4885 | } |
| 4886 | |
| 4887 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4888 | { |
| 4889 | struct hlua_txn *htxn; |
| 4890 | |
| 4891 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4892 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4893 | |
| 4894 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4895 | } |
| 4896 | |
| 4897 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4898 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4899 | struct hlua_txn *htxn; |
| 4900 | |
| 4901 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4902 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4903 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4904 | 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] | 4905 | } |
| 4906 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4907 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4908 | * It is a wrapper for the 2 following functions. |
| 4909 | */ |
| 4910 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4911 | { |
| 4912 | size_t len; |
| 4913 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4914 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4915 | /* Check if a valid response is parsed */ |
| 4916 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4917 | return 0; |
| 4918 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4919 | if (IS_HTX_STRM(htxn->s)) { |
| 4920 | /* HTX version */ |
| 4921 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 4922 | struct http_hdr_ctx ctx; |
| 4923 | |
| 4924 | ctx.blk = NULL; |
| 4925 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 4926 | http_remove_header(htx, &ctx); |
| 4927 | } |
| 4928 | else { |
| 4929 | /* Legacy HTTP version */ |
| 4930 | struct hdr_ctx ctx; |
| 4931 | struct http_txn *txn = htxn->s->txn; |
| 4932 | |
| 4933 | ctx.idx = 0; |
| 4934 | while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) |
| 4935 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4936 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4937 | return 0; |
| 4938 | } |
| 4939 | |
| 4940 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4941 | { |
| 4942 | struct hlua_txn *htxn; |
| 4943 | |
| 4944 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4945 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4946 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4947 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4948 | } |
| 4949 | |
| 4950 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4951 | { |
| 4952 | struct hlua_txn *htxn; |
| 4953 | |
| 4954 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4955 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4956 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4957 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4958 | } |
| 4959 | |
| 4960 | /* This function adds an header. It is a wrapper used by |
| 4961 | * the 2 following functions. |
| 4962 | */ |
| 4963 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4964 | { |
| 4965 | size_t name_len; |
| 4966 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4967 | size_t value_len; |
| 4968 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4969 | char *p; |
| 4970 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4971 | /* Check if a valid message is parsed */ |
| 4972 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4973 | return 0; |
| 4974 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4975 | if (IS_HTX_STRM(htxn->s)) { |
| 4976 | /* HTX version */ |
| 4977 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4978 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4979 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 4980 | ist2(value, value_len))); |
| 4981 | } |
| 4982 | else { |
| 4983 | /* Legacy HTTP version */ |
| 4984 | /* Check length. */ |
| 4985 | trash.data = value_len + name_len + 2; |
| 4986 | if (trash.data > trash.size) |
| 4987 | return 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4988 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4989 | /* Creates the header string. */ |
| 4990 | p = trash.area; |
| 4991 | memcpy(p, name, name_len); |
| 4992 | p += name_len; |
| 4993 | *p = ':'; |
| 4994 | p++; |
| 4995 | *p = ' '; |
| 4996 | p++; |
| 4997 | memcpy(p, value, value_len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4998 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame^] | 4999 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
| 5000 | trash.area, trash.data) != 0); |
| 5001 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5002 | return 0; |
| 5003 | } |
| 5004 | |
| 5005 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5006 | { |
| 5007 | struct hlua_txn *htxn; |
| 5008 | |
| 5009 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5010 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5011 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5012 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5013 | } |
| 5014 | |
| 5015 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5016 | { |
| 5017 | struct hlua_txn *htxn; |
| 5018 | |
| 5019 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5020 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5021 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5022 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5023 | } |
| 5024 | |
| 5025 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5026 | { |
| 5027 | struct hlua_txn *htxn; |
| 5028 | |
| 5029 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5030 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5031 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5032 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 5033 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5034 | } |
| 5035 | |
| 5036 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5037 | { |
| 5038 | struct hlua_txn *htxn; |
| 5039 | |
| 5040 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5041 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5042 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5043 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 5044 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5045 | } |
| 5046 | |
| 5047 | /* This function set the method. */ |
| 5048 | static int hlua_http_req_set_meth(lua_State *L) |
| 5049 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5050 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5051 | size_t name_len; |
| 5052 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5053 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5054 | /* Check if a valid request is parsed */ |
| 5055 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5056 | lua_pushboolean(L, 0); |
| 5057 | return 1; |
| 5058 | } |
| 5059 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5060 | 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] | 5061 | return 1; |
| 5062 | } |
| 5063 | |
| 5064 | /* This function set the method. */ |
| 5065 | static int hlua_http_req_set_path(lua_State *L) |
| 5066 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5067 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5068 | size_t name_len; |
| 5069 | 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] | 5070 | |
| 5071 | /* Check if a valid request is parsed */ |
| 5072 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5073 | lua_pushboolean(L, 0); |
| 5074 | return 1; |
| 5075 | } |
| 5076 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5077 | 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] | 5078 | return 1; |
| 5079 | } |
| 5080 | |
| 5081 | /* This function set the query-string. */ |
| 5082 | static int hlua_http_req_set_query(lua_State *L) |
| 5083 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5084 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5085 | size_t name_len; |
| 5086 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5087 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5088 | /* Check if a valid request is parsed */ |
| 5089 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5090 | lua_pushboolean(L, 0); |
| 5091 | return 1; |
| 5092 | } |
| 5093 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5094 | /* Check length. */ |
| 5095 | if (name_len > trash.size - 1) { |
| 5096 | lua_pushboolean(L, 0); |
| 5097 | return 1; |
| 5098 | } |
| 5099 | |
| 5100 | /* Add the mark question as prefix. */ |
| 5101 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5102 | trash.area[trash.data++] = '?'; |
| 5103 | memcpy(trash.area + trash.data, name, name_len); |
| 5104 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5105 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5106 | lua_pushboolean(L, |
| 5107 | 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] | 5108 | return 1; |
| 5109 | } |
| 5110 | |
| 5111 | /* This function set the uri. */ |
| 5112 | static int hlua_http_req_set_uri(lua_State *L) |
| 5113 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5114 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5115 | size_t name_len; |
| 5116 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5117 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5118 | /* Check if a valid request is parsed */ |
| 5119 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5120 | lua_pushboolean(L, 0); |
| 5121 | return 1; |
| 5122 | } |
| 5123 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5124 | 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] | 5125 | return 1; |
| 5126 | } |
| 5127 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5128 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5129 | static int hlua_http_res_set_status(lua_State *L) |
| 5130 | { |
| 5131 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5132 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5133 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5134 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5135 | /* Check if a valid response is parsed */ |
| 5136 | if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5137 | return 0; |
| 5138 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5139 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5140 | return 0; |
| 5141 | } |
| 5142 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5143 | /* |
| 5144 | * |
| 5145 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5146 | * Class TXN |
| 5147 | * |
| 5148 | * |
| 5149 | */ |
| 5150 | |
| 5151 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5152 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5153 | */ |
| 5154 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5155 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5156 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5157 | } |
| 5158 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5159 | __LJMP static int hlua_set_var(lua_State *L) |
| 5160 | { |
| 5161 | struct hlua_txn *htxn; |
| 5162 | const char *name; |
| 5163 | size_t len; |
| 5164 | struct sample smp; |
| 5165 | |
| 5166 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5167 | |
| 5168 | /* It is useles to retrieve the stream, but this function |
| 5169 | * runs only in a stream context. |
| 5170 | */ |
| 5171 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5172 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5173 | |
| 5174 | /* Converts the third argument in a sample. */ |
| 5175 | hlua_lua2smp(L, 3, &smp); |
| 5176 | |
| 5177 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5178 | 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] | 5179 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5180 | return 0; |
| 5181 | } |
| 5182 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5183 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5184 | { |
| 5185 | struct hlua_txn *htxn; |
| 5186 | const char *name; |
| 5187 | size_t len; |
| 5188 | struct sample smp; |
| 5189 | |
| 5190 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5191 | |
| 5192 | /* It is useles to retrieve the stream, but this function |
| 5193 | * runs only in a stream context. |
| 5194 | */ |
| 5195 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5196 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5197 | |
| 5198 | /* Unset the variable. */ |
| 5199 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5200 | vars_unset_by_name(name, len, &smp); |
| 5201 | return 0; |
| 5202 | } |
| 5203 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5204 | __LJMP static int hlua_get_var(lua_State *L) |
| 5205 | { |
| 5206 | struct hlua_txn *htxn; |
| 5207 | const char *name; |
| 5208 | size_t len; |
| 5209 | struct sample smp; |
| 5210 | |
| 5211 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5212 | |
| 5213 | /* It is useles to retrieve the stream, but this function |
| 5214 | * runs only in a stream context. |
| 5215 | */ |
| 5216 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5217 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5218 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5219 | 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] | 5220 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5221 | lua_pushnil(L); |
| 5222 | return 1; |
| 5223 | } |
| 5224 | |
| 5225 | return hlua_smp2lua(L, &smp); |
| 5226 | } |
| 5227 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5228 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5229 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5230 | struct hlua *hlua; |
| 5231 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5232 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5233 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5234 | /* It is useles to retrieve the stream, but this function |
| 5235 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5236 | */ |
| 5237 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5238 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5239 | |
| 5240 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5241 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5242 | |
| 5243 | /* Get and store new value. */ |
| 5244 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5245 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5246 | |
| 5247 | return 0; |
| 5248 | } |
| 5249 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5250 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5251 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5252 | struct hlua *hlua; |
| 5253 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5254 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5255 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5256 | /* It is useles to retrieve the stream, but this function |
| 5257 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5258 | */ |
| 5259 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5260 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5261 | |
| 5262 | /* Push configuration index in the stack. */ |
| 5263 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5264 | |
| 5265 | return 1; |
| 5266 | } |
| 5267 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5268 | /* Create stack entry containing a class TXN. This function |
| 5269 | * return 0 if the stack does not contains free slots, |
| 5270 | * otherwise it returns 1. |
| 5271 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5272 | 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] | 5273 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5274 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5275 | |
| 5276 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5277 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5278 | return 0; |
| 5279 | |
| 5280 | /* NOTE: The allocation never fails. The failure |
| 5281 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5282 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5283 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5284 | /* Create the object: obj[0] = userdata. */ |
| 5285 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5286 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5287 | lua_rawseti(L, -2, 0); |
| 5288 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5289 | htxn->s = s; |
| 5290 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5291 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5292 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5293 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5294 | /* Create the "f" field that contains a list of fetches. */ |
| 5295 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5296 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5297 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5298 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5299 | |
| 5300 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5301 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5302 | 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] | 5303 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5304 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5305 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5306 | /* Create the "c" field that contains a list of converters. */ |
| 5307 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5308 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5309 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5310 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5311 | |
| 5312 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5313 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5314 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5315 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5316 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5317 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5318 | /* Create the "req" field that contains the request channel object. */ |
| 5319 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5320 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5321 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5322 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5323 | |
| 5324 | /* Create the "res" field that contains the response channel object. */ |
| 5325 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5326 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5327 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5328 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5329 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5330 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5331 | lua_pushstring(L, "http"); |
| 5332 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5333 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5334 | return 0; |
| 5335 | } |
| 5336 | else |
| 5337 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5338 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5339 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5340 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5341 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5342 | lua_setmetatable(L, -2); |
| 5343 | |
| 5344 | return 1; |
| 5345 | } |
| 5346 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5347 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5348 | { |
| 5349 | const char *msg; |
| 5350 | struct hlua_txn *htxn; |
| 5351 | |
| 5352 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5353 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5354 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5355 | |
| 5356 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5357 | return 0; |
| 5358 | } |
| 5359 | |
| 5360 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5361 | { |
| 5362 | int level; |
| 5363 | const char *msg; |
| 5364 | struct hlua_txn *htxn; |
| 5365 | |
| 5366 | MAY_LJMP(check_args(L, 3, "log")); |
| 5367 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5368 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5369 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5370 | |
| 5371 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5372 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5373 | |
| 5374 | hlua_sendlog(htxn->s->be, level, msg); |
| 5375 | return 0; |
| 5376 | } |
| 5377 | |
| 5378 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5379 | { |
| 5380 | const char *msg; |
| 5381 | struct hlua_txn *htxn; |
| 5382 | |
| 5383 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5384 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5385 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5386 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5387 | return 0; |
| 5388 | } |
| 5389 | |
| 5390 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5391 | { |
| 5392 | const char *msg; |
| 5393 | struct hlua_txn *htxn; |
| 5394 | |
| 5395 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5396 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5397 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5398 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5399 | return 0; |
| 5400 | } |
| 5401 | |
| 5402 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5403 | { |
| 5404 | const char *msg; |
| 5405 | struct hlua_txn *htxn; |
| 5406 | |
| 5407 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5408 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5409 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5410 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5411 | return 0; |
| 5412 | } |
| 5413 | |
| 5414 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5415 | { |
| 5416 | const char *msg; |
| 5417 | struct hlua_txn *htxn; |
| 5418 | |
| 5419 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5420 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5421 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5422 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5423 | return 0; |
| 5424 | } |
| 5425 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5426 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5427 | { |
| 5428 | struct hlua_txn *htxn; |
| 5429 | int ll; |
| 5430 | |
| 5431 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5432 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5433 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5434 | |
| 5435 | if (ll < 0 || ll > 7) |
| 5436 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5437 | |
| 5438 | htxn->s->logs.level = ll; |
| 5439 | return 0; |
| 5440 | } |
| 5441 | |
| 5442 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5443 | { |
| 5444 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5445 | int tos; |
| 5446 | |
| 5447 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5448 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5449 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5450 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5451 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5452 | return 0; |
| 5453 | } |
| 5454 | |
| 5455 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5456 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5457 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5458 | int mark; |
| 5459 | |
| 5460 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5461 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5462 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5463 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5464 | conn_set_tos(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5465 | return 0; |
| 5466 | } |
| 5467 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5468 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5469 | { |
| 5470 | struct hlua_txn *htxn; |
| 5471 | |
| 5472 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5473 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5474 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5475 | return 0; |
| 5476 | } |
| 5477 | |
| 5478 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5479 | { |
| 5480 | struct hlua_txn *htxn; |
| 5481 | |
| 5482 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5483 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5484 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5485 | return 0; |
| 5486 | } |
| 5487 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5488 | /* This function is an Lua binding that send pending data |
| 5489 | * to the client, and close the stream interface. |
| 5490 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5491 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5492 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5493 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5494 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5495 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5496 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5497 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5498 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5499 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5500 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5501 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5502 | * session, so we just end the execution of the current |
| 5503 | * lua code. |
| 5504 | */ |
| 5505 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5506 | WILL_LJMP(hlua_done(L)); |
| 5507 | return 0; |
| 5508 | } |
| 5509 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5510 | ic = &htxn->s->req; |
| 5511 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5512 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5513 | if (htxn->s->txn) { |
| 5514 | /* HTTP mode, let's stay in sync with the stream */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5515 | b_del(&ic->buf, htxn->s->txn->req.sov); |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5516 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 5517 | htxn->s->txn->req.sov = 0; |
| 5518 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 5519 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 5520 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5521 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 5522 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5523 | /* Note that if we want to support keep-alive, we need |
| 5524 | * to bypass the close/shutr_now calls below, but that |
| 5525 | * may only be done if the HTTP request was already |
| 5526 | * processed and the connection header is known (ie |
| 5527 | * not during TCP rules). |
| 5528 | */ |
| 5529 | } |
| 5530 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5531 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5532 | channel_abort(ic); |
| 5533 | channel_auto_close(ic); |
| 5534 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5535 | |
| 5536 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5537 | channel_auto_read(oc); |
| 5538 | channel_auto_close(oc); |
| 5539 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5540 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 5541 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5542 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5543 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5544 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5545 | return 0; |
| 5546 | } |
| 5547 | |
| 5548 | __LJMP static int hlua_log(lua_State *L) |
| 5549 | { |
| 5550 | int level; |
| 5551 | const char *msg; |
| 5552 | |
| 5553 | MAY_LJMP(check_args(L, 2, "log")); |
| 5554 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5555 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5556 | |
| 5557 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5558 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5559 | |
| 5560 | hlua_sendlog(NULL, level, msg); |
| 5561 | return 0; |
| 5562 | } |
| 5563 | |
| 5564 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5565 | { |
| 5566 | const char *msg; |
| 5567 | |
| 5568 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5569 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5570 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5571 | return 0; |
| 5572 | } |
| 5573 | |
| 5574 | __LJMP static int hlua_log_info(lua_State *L) |
| 5575 | { |
| 5576 | const char *msg; |
| 5577 | |
| 5578 | MAY_LJMP(check_args(L, 1, "info")); |
| 5579 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5580 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5581 | return 0; |
| 5582 | } |
| 5583 | |
| 5584 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5585 | { |
| 5586 | const char *msg; |
| 5587 | |
| 5588 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5589 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5590 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5591 | return 0; |
| 5592 | } |
| 5593 | |
| 5594 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5595 | { |
| 5596 | const char *msg; |
| 5597 | |
| 5598 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5599 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5600 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5601 | return 0; |
| 5602 | } |
| 5603 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5604 | __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] | 5605 | { |
| 5606 | int wakeup_ms = lua_tointeger(L, -1); |
| 5607 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5608 | 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] | 5609 | return 0; |
| 5610 | } |
| 5611 | |
| 5612 | __LJMP static int hlua_sleep(lua_State *L) |
| 5613 | { |
| 5614 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5615 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5616 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5617 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5618 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5619 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5620 | wakeup_ms = tick_add(now_ms, delay); |
| 5621 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5622 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5623 | 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] | 5624 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5625 | } |
| 5626 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5627 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5628 | { |
| 5629 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5630 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5631 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5632 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5633 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5634 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5635 | wakeup_ms = tick_add(now_ms, delay); |
| 5636 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5637 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5638 | 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] | 5639 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5640 | } |
| 5641 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5642 | /* This functionis an LUA binding. it permits to give back |
| 5643 | * the hand at the HAProxy scheduler. It is used when the |
| 5644 | * LUA processing consumes a lot of time. |
| 5645 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5646 | __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] | 5647 | { |
| 5648 | return 0; |
| 5649 | } |
| 5650 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5651 | __LJMP static int hlua_yield(lua_State *L) |
| 5652 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5653 | 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] | 5654 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5655 | } |
| 5656 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5657 | /* This function change the nice of the currently executed |
| 5658 | * task. It is used set low or high priority at the current |
| 5659 | * task. |
| 5660 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5661 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5662 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5663 | struct hlua *hlua; |
| 5664 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5665 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5666 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5667 | hlua = hlua_gethlua(L); |
| 5668 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5669 | |
| 5670 | /* If he task is not set, I'm in a start mode. */ |
| 5671 | if (!hlua || !hlua->task) |
| 5672 | return 0; |
| 5673 | |
| 5674 | if (nice < -1024) |
| 5675 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5676 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5677 | nice = 1024; |
| 5678 | |
| 5679 | hlua->task->nice = nice; |
| 5680 | return 0; |
| 5681 | } |
| 5682 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5683 | /* 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] | 5684 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5685 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5686 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5687 | * |
| 5688 | * Task wrapper are longjmp safe because the only one Lua code |
| 5689 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5690 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5691 | static struct task *hlua_process_task(struct task *task, void *context, unsigned short state) |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5692 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5693 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5694 | enum hlua_exec status; |
| 5695 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 5696 | if (task->thread_mask == MAX_THREADS_MASK) |
| 5697 | task_set_affinity(task, tid_bit); |
| 5698 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5699 | /* If it is the first call to the task, we must initialize the |
| 5700 | * execution timeouts. |
| 5701 | */ |
| 5702 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5703 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5704 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5705 | /* Execute the Lua code. */ |
| 5706 | status = hlua_ctx_resume(hlua, 1); |
| 5707 | |
| 5708 | switch (status) { |
| 5709 | /* finished or yield */ |
| 5710 | case HLUA_E_OK: |
| 5711 | hlua_ctx_destroy(hlua); |
| 5712 | task_delete(task); |
| 5713 | task_free(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 5714 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5715 | break; |
| 5716 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5717 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 5718 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 5719 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5720 | break; |
| 5721 | |
| 5722 | /* finished with error. */ |
| 5723 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5724 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5725 | hlua_ctx_destroy(hlua); |
| 5726 | task_delete(task); |
| 5727 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5728 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5729 | break; |
| 5730 | |
| 5731 | case HLUA_E_ERR: |
| 5732 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5733 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5734 | hlua_ctx_destroy(hlua); |
| 5735 | task_delete(task); |
| 5736 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5737 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5738 | break; |
| 5739 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5740 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5741 | } |
| 5742 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5743 | /* This function is an LUA binding that register LUA function to be |
| 5744 | * executed after the HAProxy configuration parsing and before the |
| 5745 | * HAProxy scheduler starts. This function expect only one LUA |
| 5746 | * argument that is a function. This function returns nothing, but |
| 5747 | * throws if an error is encountered. |
| 5748 | */ |
| 5749 | __LJMP static int hlua_register_init(lua_State *L) |
| 5750 | { |
| 5751 | struct hlua_init_function *init; |
| 5752 | int ref; |
| 5753 | |
| 5754 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5755 | |
| 5756 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5757 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5758 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5759 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5760 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5761 | |
| 5762 | init->function_ref = ref; |
| 5763 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5764 | return 0; |
| 5765 | } |
| 5766 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5767 | /* This functio is an LUA binding. It permits to register a task |
| 5768 | * executed in parallel of the main HAroxy activity. The task is |
| 5769 | * created and it is set in the HAProxy scheduler. It can be called |
| 5770 | * from the "init" section, "post init" or during the runtime. |
| 5771 | * |
| 5772 | * Lua prototype: |
| 5773 | * |
| 5774 | * <none> core.register_task(<function>) |
| 5775 | */ |
| 5776 | static int hlua_register_task(lua_State *L) |
| 5777 | { |
| 5778 | struct hlua *hlua; |
| 5779 | struct task *task; |
| 5780 | int ref; |
| 5781 | |
| 5782 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5783 | |
| 5784 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5785 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5786 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5787 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5788 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5789 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 5790 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 5791 | if (!task) |
| 5792 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 5793 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5794 | task->context = hlua; |
| 5795 | task->process = hlua_process_task; |
| 5796 | |
| 5797 | if (!hlua_ctx_init(hlua, task)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5798 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5799 | |
| 5800 | /* Restore the function in the stack. */ |
| 5801 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5802 | hlua->nargs = 0; |
| 5803 | |
| 5804 | /* Schedule task. */ |
| 5805 | task_schedule(task, now_ms); |
| 5806 | |
| 5807 | return 0; |
| 5808 | } |
| 5809 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5810 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5811 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5812 | * resume converters. |
| 5813 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5814 | 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] | 5815 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5816 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5817 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5818 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5819 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5820 | if (!stream) |
| 5821 | return 0; |
| 5822 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5823 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5824 | * Lua context can be not initialized. This behavior |
| 5825 | * permits to save performances because a systematic |
| 5826 | * Lua initialization cause 5% performances loss. |
| 5827 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5828 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5829 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5830 | if (!stream->hlua) { |
| 5831 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5832 | return 0; |
| 5833 | } |
| 5834 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5835 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5836 | return 0; |
| 5837 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5838 | } |
| 5839 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5840 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5841 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5842 | |
| 5843 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5844 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5845 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5846 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5847 | else |
| 5848 | error = "critical error"; |
| 5849 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5850 | return 0; |
| 5851 | } |
| 5852 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5853 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5854 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5855 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5856 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5857 | return 0; |
| 5858 | } |
| 5859 | |
| 5860 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5861 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5862 | |
| 5863 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5864 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5865 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5866 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5867 | return 0; |
| 5868 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5869 | hlua_smp2lua(stream->hlua->T, smp); |
| 5870 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5871 | |
| 5872 | /* push keywords in the stack. */ |
| 5873 | if (arg_p) { |
| 5874 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5875 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5876 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5877 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5878 | return 0; |
| 5879 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5880 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5881 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5882 | } |
| 5883 | } |
| 5884 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5885 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5886 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5887 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5888 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5889 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5890 | } |
| 5891 | |
| 5892 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5893 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5894 | /* finished. */ |
| 5895 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5896 | /* If the stack is empty, the function fails. */ |
| 5897 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5898 | return 0; |
| 5899 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5900 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5901 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5902 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5903 | return 1; |
| 5904 | |
| 5905 | /* yield. */ |
| 5906 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5907 | 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] | 5908 | return 0; |
| 5909 | |
| 5910 | /* finished with error. */ |
| 5911 | case HLUA_E_ERRMSG: |
| 5912 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5913 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5914 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5915 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5916 | return 0; |
| 5917 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5918 | case HLUA_E_ETMOUT: |
| 5919 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 5920 | return 0; |
| 5921 | |
| 5922 | case HLUA_E_NOMEM: |
| 5923 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 5924 | return 0; |
| 5925 | |
| 5926 | case HLUA_E_YIELD: |
| 5927 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 5928 | return 0; |
| 5929 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5930 | case HLUA_E_ERR: |
| 5931 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5932 | 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] | 5933 | |
| 5934 | default: |
| 5935 | return 0; |
| 5936 | } |
| 5937 | } |
| 5938 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5939 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5940 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5941 | * resume sample-fetches. This function will be called by the sample |
| 5942 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5943 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5944 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5945 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5946 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5947 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5948 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5949 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5950 | const struct buffer msg = { }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5951 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5952 | if (!stream) |
| 5953 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 5954 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5955 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5956 | * Lua context can be not initialized. This behavior |
| 5957 | * permits to save performances because a systematic |
| 5958 | * Lua initialization cause 5% performances loss. |
| 5959 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5960 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5961 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5962 | if (!stream->hlua) { |
| 5963 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5964 | return 0; |
| 5965 | } |
| 5966 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5967 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5968 | return 0; |
| 5969 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5970 | } |
| 5971 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5972 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5973 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5974 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5975 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5976 | |
| 5977 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5978 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5979 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5980 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5981 | else |
| 5982 | error = "critical error"; |
| 5983 | 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] | 5984 | return 0; |
| 5985 | } |
| 5986 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5987 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5988 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5989 | 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] | 5990 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5991 | return 0; |
| 5992 | } |
| 5993 | |
| 5994 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5995 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5996 | |
| 5997 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5998 | if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5999 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6000 | 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] | 6001 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6002 | return 0; |
| 6003 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6004 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6005 | |
| 6006 | /* push keywords in the stack. */ |
| 6007 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6008 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6009 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6010 | 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] | 6011 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6012 | return 0; |
| 6013 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6014 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6015 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6016 | } |
| 6017 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6018 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6019 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6020 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6021 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6022 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6023 | } |
| 6024 | |
| 6025 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6026 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6027 | /* finished. */ |
| 6028 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6029 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6030 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6031 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6032 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6033 | /* If the stack is empty, the function fails. */ |
| 6034 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6035 | return 0; |
| 6036 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6037 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6038 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6039 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6040 | |
| 6041 | /* Set the end of execution flag. */ |
| 6042 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6043 | return 1; |
| 6044 | |
| 6045 | /* yield. */ |
| 6046 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6047 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6048 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6049 | 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] | 6050 | return 0; |
| 6051 | |
| 6052 | /* finished with error. */ |
| 6053 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6054 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6055 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6056 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6057 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6058 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6059 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6060 | return 0; |
| 6061 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6062 | case HLUA_E_ETMOUT: |
| 6063 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 6064 | stream_int_retnclose(&stream->si[0], &msg); |
| 6065 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6066 | return 0; |
| 6067 | |
| 6068 | case HLUA_E_NOMEM: |
| 6069 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 6070 | stream_int_retnclose(&stream->si[0], &msg); |
| 6071 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6072 | return 0; |
| 6073 | |
| 6074 | case HLUA_E_YIELD: |
| 6075 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 6076 | stream_int_retnclose(&stream->si[0], &msg); |
| 6077 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6078 | return 0; |
| 6079 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6080 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6081 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6082 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6083 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6084 | 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] | 6085 | |
| 6086 | default: |
| 6087 | return 0; |
| 6088 | } |
| 6089 | } |
| 6090 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6091 | /* This function is an LUA binding used for registering |
| 6092 | * "sample-conv" functions. It expects a converter name used |
| 6093 | * in the haproxy configuration file, and an LUA function. |
| 6094 | */ |
| 6095 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6096 | { |
| 6097 | struct sample_conv_kw_list *sck; |
| 6098 | const char *name; |
| 6099 | int ref; |
| 6100 | int len; |
| 6101 | struct hlua_function *fcn; |
| 6102 | |
| 6103 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6104 | |
| 6105 | /* First argument : converter name. */ |
| 6106 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6107 | |
| 6108 | /* Second argument : lua function. */ |
| 6109 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6110 | |
| 6111 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6112 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6113 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6114 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6115 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6116 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6117 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6118 | |
| 6119 | /* Fill fcn. */ |
| 6120 | fcn->name = strdup(name); |
| 6121 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6122 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6123 | fcn->function_ref = ref; |
| 6124 | |
| 6125 | /* List head */ |
| 6126 | sck->list.n = sck->list.p = NULL; |
| 6127 | |
| 6128 | /* converter keyword. */ |
| 6129 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6130 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6131 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6132 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6133 | |
| 6134 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6135 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6136 | 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] | 6137 | sck->kw[0].val_args = NULL; |
| 6138 | sck->kw[0].in_type = SMP_T_STR; |
| 6139 | sck->kw[0].out_type = SMP_T_STR; |
| 6140 | sck->kw[0].private = fcn; |
| 6141 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6142 | /* Register this new converter */ |
| 6143 | sample_register_convs(sck); |
| 6144 | |
| 6145 | return 0; |
| 6146 | } |
| 6147 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6148 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6149 | * "sample-fetch" functions. It expects a converter name used |
| 6150 | * in the haproxy configuration file, and an LUA function. |
| 6151 | */ |
| 6152 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6153 | { |
| 6154 | const char *name; |
| 6155 | int ref; |
| 6156 | int len; |
| 6157 | struct sample_fetch_kw_list *sfk; |
| 6158 | struct hlua_function *fcn; |
| 6159 | |
| 6160 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6161 | |
| 6162 | /* First argument : sample-fetch name. */ |
| 6163 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6164 | |
| 6165 | /* Second argument : lua function. */ |
| 6166 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6167 | |
| 6168 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6169 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6170 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6171 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6172 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6173 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6174 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6175 | |
| 6176 | /* Fill fcn. */ |
| 6177 | fcn->name = strdup(name); |
| 6178 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6179 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6180 | fcn->function_ref = ref; |
| 6181 | |
| 6182 | /* List head */ |
| 6183 | sfk->list.n = sfk->list.p = NULL; |
| 6184 | |
| 6185 | /* sample-fetch keyword. */ |
| 6186 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6187 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6188 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6189 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6190 | |
| 6191 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6192 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6193 | 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] | 6194 | sfk->kw[0].val_args = NULL; |
| 6195 | sfk->kw[0].out_type = SMP_T_STR; |
| 6196 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6197 | sfk->kw[0].val = 0; |
| 6198 | sfk->kw[0].private = fcn; |
| 6199 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6200 | /* Register this new fetch. */ |
| 6201 | sample_register_fetches(sfk); |
| 6202 | |
| 6203 | return 0; |
| 6204 | } |
| 6205 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6206 | /* This function is a wrapper to execute each LUA function declared |
| 6207 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6208 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6209 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6210 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6211 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6212 | 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] | 6213 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6214 | { |
| 6215 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6216 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6217 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6218 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6219 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6220 | |
| 6221 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 6222 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 6223 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 6224 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 6225 | case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6226 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6227 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6228 | return ACT_RET_CONT; |
| 6229 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6230 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6231 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6232 | * Lua context can be not initialized. This behavior |
| 6233 | * permits to save performances because a systematic |
| 6234 | * Lua initialization cause 5% performances loss. |
| 6235 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6236 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6237 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6238 | if (!s->hlua) { |
| 6239 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6240 | rule->arg.hlua_rule->fcn.name); |
| 6241 | return ACT_RET_CONT; |
| 6242 | } |
| 6243 | if (!hlua_ctx_init(s->hlua, s->task)) { |
| 6244 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6245 | rule->arg.hlua_rule->fcn.name); |
| 6246 | return ACT_RET_CONT; |
| 6247 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6248 | } |
| 6249 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6250 | consistency_set(s, dir, &s->hlua->cons); |
| 6251 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6252 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6253 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6254 | |
| 6255 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6256 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6257 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6258 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6259 | else |
| 6260 | error = "critical error"; |
| 6261 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6262 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6263 | return ACT_RET_CONT; |
| 6264 | } |
| 6265 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6266 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6267 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6268 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6269 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6270 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6271 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6272 | } |
| 6273 | |
| 6274 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6275 | 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] | 6276 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6277 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6278 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6279 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6280 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6281 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6282 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6283 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6284 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6285 | |
| 6286 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6287 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6288 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6289 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6290 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6291 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6292 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6293 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6294 | lua_pushstring(s->hlua->T, *arg); |
| 6295 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6296 | } |
| 6297 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6298 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6299 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6300 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6301 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6302 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6303 | } |
| 6304 | |
| 6305 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6306 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6307 | /* finished. */ |
| 6308 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6309 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6310 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6311 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6312 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6313 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6314 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6315 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6316 | |
| 6317 | /* yield. */ |
| 6318 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6319 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6320 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6321 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6322 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6323 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6324 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6325 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6326 | /* Some actions can be wake up when a "write" event |
| 6327 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6328 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6329 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6330 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6331 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6332 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6333 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6334 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6335 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6336 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6337 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6338 | * because HAProxy is not able to manipulate data, it |
| 6339 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6340 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6341 | |
| 6342 | /* finished with error. */ |
| 6343 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6344 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6345 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6346 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6347 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6348 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6349 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6350 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6351 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6352 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6353 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6354 | case HLUA_E_ETMOUT: |
| 6355 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6356 | stream_int_retnclose(&s->si[0], &msg); |
| 6357 | return ACT_RET_ERR; |
| 6358 | } |
| 6359 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6360 | return 0; |
| 6361 | |
| 6362 | case HLUA_E_NOMEM: |
| 6363 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6364 | stream_int_retnclose(&s->si[0], &msg); |
| 6365 | return ACT_RET_ERR; |
| 6366 | } |
| 6367 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6368 | return 0; |
| 6369 | |
| 6370 | case HLUA_E_YIELD: |
| 6371 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6372 | stream_int_retnclose(&s->si[0], &msg); |
| 6373 | return ACT_RET_ERR; |
| 6374 | } |
| 6375 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6376 | rule->arg.hlua_rule->fcn.name); |
| 6377 | return 0; |
| 6378 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6379 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6380 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6381 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6382 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6383 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6384 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6385 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6386 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6387 | |
| 6388 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6389 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6390 | } |
| 6391 | } |
| 6392 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6393 | 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] | 6394 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6395 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6396 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6397 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6398 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6399 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6400 | } |
| 6401 | |
| 6402 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6403 | { |
| 6404 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6405 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6406 | struct task *task; |
| 6407 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6408 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6409 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6410 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6411 | if (!hlua) { |
| 6412 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6413 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6414 | return 0; |
| 6415 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6416 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6417 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6418 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6419 | |
| 6420 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6421 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6422 | if (!task) { |
| 6423 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6424 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6425 | return 0; |
| 6426 | } |
| 6427 | task->nice = 0; |
| 6428 | task->context = ctx; |
| 6429 | task->process = hlua_applet_wakeup; |
| 6430 | ctx->ctx.hlua_apptcp.task = task; |
| 6431 | |
| 6432 | /* In the execution wrappers linked with a stream, the |
| 6433 | * Lua context can be not initialized. This behavior |
| 6434 | * permits to save performances because a systematic |
| 6435 | * Lua initialization cause 5% performances loss. |
| 6436 | */ |
| 6437 | if (!hlua_ctx_init(hlua, task)) { |
| 6438 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6439 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6440 | return 0; |
| 6441 | } |
| 6442 | |
| 6443 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6444 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6445 | |
| 6446 | /* The following Lua calls can fail. */ |
| 6447 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6448 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6449 | error = lua_tostring(hlua->T, -1); |
| 6450 | else |
| 6451 | error = "critical error"; |
| 6452 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6453 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6454 | return 0; |
| 6455 | } |
| 6456 | |
| 6457 | /* Check stack available size. */ |
| 6458 | if (!lua_checkstack(hlua->T, 1)) { |
| 6459 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6460 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6461 | RESET_SAFE_LJMP(hlua->T); |
| 6462 | return 0; |
| 6463 | } |
| 6464 | |
| 6465 | /* Restore the function in the stack. */ |
| 6466 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6467 | |
| 6468 | /* Create and and push object stream in the stack. */ |
| 6469 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6470 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6471 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6472 | RESET_SAFE_LJMP(hlua->T); |
| 6473 | return 0; |
| 6474 | } |
| 6475 | hlua->nargs = 1; |
| 6476 | |
| 6477 | /* push keywords in the stack. */ |
| 6478 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6479 | if (!lua_checkstack(hlua->T, 1)) { |
| 6480 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6481 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6482 | RESET_SAFE_LJMP(hlua->T); |
| 6483 | return 0; |
| 6484 | } |
| 6485 | lua_pushstring(hlua->T, *arg); |
| 6486 | hlua->nargs++; |
| 6487 | } |
| 6488 | |
| 6489 | RESET_SAFE_LJMP(hlua->T); |
| 6490 | |
| 6491 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6492 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6493 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6494 | |
| 6495 | return 1; |
| 6496 | } |
| 6497 | |
| 6498 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6499 | { |
| 6500 | struct stream_interface *si = ctx->owner; |
| 6501 | struct stream *strm = si_strm(si); |
| 6502 | struct channel *res = si_ic(si); |
| 6503 | struct act_rule *rule = ctx->rule; |
| 6504 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6505 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6506 | |
| 6507 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6508 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6509 | /* eat the whole request */ |
| 6510 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6511 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6512 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6513 | |
| 6514 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6515 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6516 | return; |
| 6517 | |
| 6518 | /* Execute the function. */ |
| 6519 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6520 | /* finished. */ |
| 6521 | case HLUA_E_OK: |
| 6522 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6523 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6524 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6525 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6526 | res->flags |= CF_READ_NULL; |
| 6527 | si_shutr(si); |
| 6528 | return; |
| 6529 | |
| 6530 | /* yield. */ |
| 6531 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6532 | if (hlua->wake_time != TICK_ETERNITY) |
| 6533 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6534 | return; |
| 6535 | |
| 6536 | /* finished with error. */ |
| 6537 | case HLUA_E_ERRMSG: |
| 6538 | /* Display log. */ |
| 6539 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6540 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6541 | lua_pop(hlua->T, 1); |
| 6542 | goto error; |
| 6543 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6544 | case HLUA_E_ETMOUT: |
| 6545 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6546 | rule->arg.hlua_rule->fcn.name); |
| 6547 | goto error; |
| 6548 | |
| 6549 | case HLUA_E_NOMEM: |
| 6550 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6551 | rule->arg.hlua_rule->fcn.name); |
| 6552 | goto error; |
| 6553 | |
| 6554 | case HLUA_E_YIELD: /* unexpected */ |
| 6555 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6556 | rule->arg.hlua_rule->fcn.name); |
| 6557 | goto error; |
| 6558 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6559 | case HLUA_E_ERR: |
| 6560 | /* Display log. */ |
| 6561 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6562 | rule->arg.hlua_rule->fcn.name); |
| 6563 | goto error; |
| 6564 | |
| 6565 | default: |
| 6566 | goto error; |
| 6567 | } |
| 6568 | |
| 6569 | error: |
| 6570 | |
| 6571 | /* For all other cases, just close the stream. */ |
| 6572 | si_shutw(si); |
| 6573 | si_shutr(si); |
| 6574 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6575 | } |
| 6576 | |
| 6577 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6578 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6579 | task_delete(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6580 | task_free(ctx->ctx.hlua_apptcp.task); |
| 6581 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6582 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6583 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6584 | } |
| 6585 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6586 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6587 | * an errors occurs and -1 if more data are required for initializing |
| 6588 | * the applet. |
| 6589 | */ |
| 6590 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6591 | { |
| 6592 | struct stream_interface *si = ctx->owner; |
| 6593 | struct channel *req = si_oc(si); |
| 6594 | struct http_msg *msg; |
| 6595 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6596 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6597 | char **arg; |
| 6598 | struct hdr_ctx hdr; |
| 6599 | struct task *task; |
| 6600 | struct sample smp; /* just used for a valid call to smp_prefetch_http. */ |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6601 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6602 | |
| 6603 | /* Wait for a full HTTP request. */ |
| 6604 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 6605 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 6606 | return -1; |
| 6607 | return 0; |
| 6608 | } |
| 6609 | txn = strm->txn; |
| 6610 | msg = &txn->req; |
| 6611 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6612 | /* We want two things in HTTP mode : |
| 6613 | * - enforce server-close mode if we were in keep-alive, so that the |
| 6614 | * applet is released after each response ; |
| 6615 | * - enable request body transfer to the applet in order to resync |
| 6616 | * with the response body. |
| 6617 | */ |
| 6618 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 6619 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6620 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6621 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6622 | if (!hlua) { |
| 6623 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6624 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6625 | return 0; |
| 6626 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6627 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6628 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6629 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6630 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6631 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6632 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6633 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6634 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6635 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6636 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6637 | if (!task) { |
| 6638 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6639 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6640 | return 0; |
| 6641 | } |
| 6642 | task->nice = 0; |
| 6643 | task->context = ctx; |
| 6644 | task->process = hlua_applet_wakeup; |
| 6645 | ctx->ctx.hlua_apphttp.task = task; |
| 6646 | |
| 6647 | /* In the execution wrappers linked with a stream, the |
| 6648 | * Lua context can be not initialized. This behavior |
| 6649 | * permits to save performances because a systematic |
| 6650 | * Lua initialization cause 5% performances loss. |
| 6651 | */ |
| 6652 | if (!hlua_ctx_init(hlua, task)) { |
| 6653 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6654 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6655 | return 0; |
| 6656 | } |
| 6657 | |
| 6658 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6659 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6660 | |
| 6661 | /* The following Lua calls can fail. */ |
| 6662 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6663 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6664 | error = lua_tostring(hlua->T, -1); |
| 6665 | else |
| 6666 | error = "critical error"; |
| 6667 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6668 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6669 | return 0; |
| 6670 | } |
| 6671 | |
| 6672 | /* Check stack available size. */ |
| 6673 | if (!lua_checkstack(hlua->T, 1)) { |
| 6674 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6675 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6676 | RESET_SAFE_LJMP(hlua->T); |
| 6677 | return 0; |
| 6678 | } |
| 6679 | |
| 6680 | /* Restore the function in the stack. */ |
| 6681 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6682 | |
| 6683 | /* Create and and push object stream in the stack. */ |
| 6684 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6685 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6686 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6687 | RESET_SAFE_LJMP(hlua->T); |
| 6688 | return 0; |
| 6689 | } |
| 6690 | hlua->nargs = 1; |
| 6691 | |
| 6692 | /* Look for a 100-continue expected. */ |
| 6693 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 6694 | hdr.idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6695 | if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &hdr) && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6696 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 6697 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 6698 | } |
| 6699 | |
| 6700 | /* push keywords in the stack. */ |
| 6701 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6702 | if (!lua_checkstack(hlua->T, 1)) { |
| 6703 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6704 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6705 | RESET_SAFE_LJMP(hlua->T); |
| 6706 | return 0; |
| 6707 | } |
| 6708 | lua_pushstring(hlua->T, *arg); |
| 6709 | hlua->nargs++; |
| 6710 | } |
| 6711 | |
| 6712 | RESET_SAFE_LJMP(hlua->T); |
| 6713 | |
| 6714 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6715 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6716 | |
| 6717 | return 1; |
| 6718 | } |
| 6719 | |
| 6720 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 6721 | { |
| 6722 | struct stream_interface *si = ctx->owner; |
| 6723 | struct stream *strm = si_strm(si); |
| 6724 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6725 | struct act_rule *rule = ctx->rule; |
| 6726 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6727 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 6728 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 6729 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 6730 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 6731 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6732 | int ret; |
| 6733 | |
| 6734 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6735 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6736 | return; |
| 6737 | |
| 6738 | /* Set the currently running flag. */ |
| 6739 | if (!HLUA_IS_RUNNING(hlua) && |
| 6740 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6741 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6742 | /* Wait for full HTTP analysys. */ |
| 6743 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6744 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6745 | return; |
| 6746 | } |
| 6747 | |
| 6748 | /* Store the max amount of bytes that we can read. */ |
| 6749 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 6750 | |
| 6751 | /* We need to flush the request header. This left the body |
| 6752 | * for the Lua. |
| 6753 | */ |
| 6754 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6755 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6756 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6757 | if (ret == -1) |
| 6758 | return; |
| 6759 | |
| 6760 | /* No data available, ask for more data. */ |
| 6761 | if (ret == 1) |
| 6762 | len2 = 0; |
| 6763 | if (ret == 0) |
| 6764 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 6765 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6766 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6767 | return; |
| 6768 | } |
| 6769 | |
| 6770 | /* skip the requests bytes. */ |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 6771 | co_skip(si_oc(si), strm->txn->req.eoh + strm->txn->req.eol); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6772 | } |
| 6773 | |
| 6774 | /* Executes The applet if it is not done. */ |
| 6775 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6776 | |
| 6777 | /* Execute the function. */ |
| 6778 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6779 | /* finished. */ |
| 6780 | case HLUA_E_OK: |
| 6781 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6782 | break; |
| 6783 | |
| 6784 | /* yield. */ |
| 6785 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6786 | if (hlua->wake_time != TICK_ETERNITY) |
| 6787 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6788 | return; |
| 6789 | |
| 6790 | /* finished with error. */ |
| 6791 | case HLUA_E_ERRMSG: |
| 6792 | /* Display log. */ |
| 6793 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6794 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6795 | lua_pop(hlua->T, 1); |
| 6796 | goto error; |
| 6797 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6798 | case HLUA_E_ETMOUT: |
| 6799 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 6800 | rule->arg.hlua_rule->fcn.name); |
| 6801 | goto error; |
| 6802 | |
| 6803 | case HLUA_E_NOMEM: |
| 6804 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 6805 | rule->arg.hlua_rule->fcn.name); |
| 6806 | goto error; |
| 6807 | |
| 6808 | case HLUA_E_YIELD: /* unexpected */ |
| 6809 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 6810 | rule->arg.hlua_rule->fcn.name); |
| 6811 | goto error; |
| 6812 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6813 | case HLUA_E_ERR: |
| 6814 | /* Display log. */ |
| 6815 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6816 | rule->arg.hlua_rule->fcn.name); |
| 6817 | goto error; |
| 6818 | |
| 6819 | default: |
| 6820 | goto error; |
| 6821 | } |
| 6822 | } |
| 6823 | |
| 6824 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 6825 | |
| 6826 | /* We must send the final chunk. */ |
| 6827 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 6828 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 6829 | |
| 6830 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6831 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6832 | |
| 6833 | /* critical error. */ |
| 6834 | if (ret == -2 || ret == -3) { |
| 6835 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 6836 | rule->arg.hlua_rule->fcn.name); |
| 6837 | goto error; |
| 6838 | } |
| 6839 | |
| 6840 | /* no enough space error. */ |
| 6841 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6842 | si_rx_room_blk(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6843 | return; |
| 6844 | } |
| 6845 | |
| 6846 | /* set the last chunk sent. */ |
| 6847 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6848 | } |
| 6849 | |
| 6850 | /* close the connection. */ |
| 6851 | |
Frédéric Lécaille | 83ed5d5 | 2018-07-18 14:25:26 +0200 | [diff] [blame] | 6852 | /* status */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6853 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6854 | |
| 6855 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6856 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6857 | res->flags |= CF_READ_NULL; |
| 6858 | si_shutr(si); |
| 6859 | |
| 6860 | return; |
| 6861 | } |
| 6862 | |
| 6863 | error: |
| 6864 | |
| 6865 | /* If we are in HTTP mode, and we are not send any |
| 6866 | * data, return a 500 server error in best effort: |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6867 | * if there is no room available in the buffer, |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6868 | * just close the connection. |
| 6869 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6870 | ci_putblk(res, error_500, strlen(error_500)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6871 | if (!(strm->flags & SF_ERR_MASK)) |
| 6872 | strm->flags |= SF_ERR_RESOURCE; |
| 6873 | si_shutw(si); |
| 6874 | si_shutr(si); |
| 6875 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6876 | } |
| 6877 | |
| 6878 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6879 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6880 | task_delete(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6881 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6882 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6883 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6884 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6885 | } |
| 6886 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6887 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6888 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6889 | * |
| 6890 | * This function can fail with an abort() due to an Lua critical error. |
| 6891 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6892 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6893 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6894 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6895 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6896 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6897 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6898 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6899 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6900 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6901 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6902 | if (!rule->arg.hlua_rule) { |
| 6903 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6904 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6905 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6906 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6907 | /* Memory for arguments. */ |
| 6908 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6909 | if (!rule->arg.hlua_rule->args) { |
| 6910 | memprintf(err, "out of memory error"); |
| 6911 | return ACT_RET_PRS_ERR; |
| 6912 | } |
| 6913 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6914 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6915 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6916 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6917 | /* Expect some arguments */ |
| 6918 | for (i = 0; i < fcn->nargs; i++) { |
| 6919 | if (*args[i+1] == '\0') { |
| 6920 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6921 | return ACT_RET_PRS_ERR; |
| 6922 | } |
| 6923 | rule->arg.hlua_rule->args[i] = strdup(args[i + 1]); |
| 6924 | if (!rule->arg.hlua_rule->args[i]) { |
| 6925 | memprintf(err, "out of memory error"); |
| 6926 | return ACT_RET_PRS_ERR; |
| 6927 | } |
| 6928 | (*cur_arg)++; |
| 6929 | } |
| 6930 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6931 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6932 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6933 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6934 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6935 | } |
| 6936 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6937 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6938 | struct act_rule *rule, char **err) |
| 6939 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6940 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6941 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6942 | if (px->options2 & PR_O2_USE_HTX) { |
| 6943 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 6944 | return ACT_RET_PRS_ERR; |
| 6945 | } |
| 6946 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6947 | /* HTTP applets are forbidden in tcp-request rules. |
| 6948 | * HTTP applet request requires everything initilized by |
| 6949 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6950 | * The applet will be immediately initilized, but its before |
| 6951 | * the call of this analyzer. |
| 6952 | */ |
| 6953 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6954 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6955 | return ACT_RET_PRS_ERR; |
| 6956 | } |
| 6957 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6958 | /* Memory for the rule. */ |
| 6959 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6960 | if (!rule->arg.hlua_rule) { |
| 6961 | memprintf(err, "out of memory error"); |
| 6962 | return ACT_RET_PRS_ERR; |
| 6963 | } |
| 6964 | |
| 6965 | /* Reference the Lua function and store the reference. */ |
| 6966 | rule->arg.hlua_rule->fcn = *fcn; |
| 6967 | |
| 6968 | /* TODO: later accept arguments. */ |
| 6969 | rule->arg.hlua_rule->args = NULL; |
| 6970 | |
| 6971 | /* Add applet pointer in the rule. */ |
| 6972 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6973 | rule->applet.name = fcn->name; |
| 6974 | rule->applet.init = hlua_applet_http_init; |
| 6975 | rule->applet.fct = hlua_applet_http_fct; |
| 6976 | rule->applet.release = hlua_applet_http_release; |
| 6977 | rule->applet.timeout = hlua_timeout_applet; |
| 6978 | |
| 6979 | return ACT_RET_PRS_OK; |
| 6980 | } |
| 6981 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6982 | /* This function is an LUA binding used for registering |
| 6983 | * "sample-conv" functions. It expects a converter name used |
| 6984 | * in the haproxy configuration file, and an LUA function. |
| 6985 | */ |
| 6986 | __LJMP static int hlua_register_action(lua_State *L) |
| 6987 | { |
| 6988 | struct action_kw_list *akl; |
| 6989 | const char *name; |
| 6990 | int ref; |
| 6991 | int len; |
| 6992 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6993 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6994 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6995 | /* Initialise the number of expected arguments at 0. */ |
| 6996 | nargs = 0; |
| 6997 | |
| 6998 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6999 | 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] | 7000 | |
| 7001 | /* First argument : converter name. */ |
| 7002 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7003 | |
| 7004 | /* Second argument : environment. */ |
| 7005 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7006 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7007 | |
| 7008 | /* Third argument : lua function. */ |
| 7009 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7010 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7011 | /* 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] | 7012 | if (lua_gettop(L) >= 4) |
| 7013 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7014 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7015 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7016 | lua_pushnil(L); |
| 7017 | while (lua_next(L, 2) != 0) { |
| 7018 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7019 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7020 | |
| 7021 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7022 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7023 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7024 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7025 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7026 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7027 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7028 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7029 | |
| 7030 | /* Fill fcn. */ |
| 7031 | fcn->name = strdup(name); |
| 7032 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7033 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7034 | fcn->function_ref = ref; |
| 7035 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7036 | /* Set the expected number od arguments. */ |
| 7037 | fcn->nargs = nargs; |
| 7038 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7039 | /* List head */ |
| 7040 | akl->list.n = akl->list.p = NULL; |
| 7041 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7042 | /* action keyword. */ |
| 7043 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7044 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7045 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7046 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7047 | |
| 7048 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7049 | |
| 7050 | akl->kw[0].match_pfx = 0; |
| 7051 | akl->kw[0].private = fcn; |
| 7052 | akl->kw[0].parse = action_register_lua; |
| 7053 | |
| 7054 | /* select the action registering point. */ |
| 7055 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7056 | tcp_req_cont_keywords_register(akl); |
| 7057 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7058 | tcp_res_cont_keywords_register(akl); |
| 7059 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7060 | http_req_keywords_register(akl); |
| 7061 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7062 | http_res_keywords_register(akl); |
| 7063 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7064 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7065 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7066 | "are expected.", lua_tostring(L, -1))); |
| 7067 | |
| 7068 | /* pop the environment string. */ |
| 7069 | lua_pop(L, 1); |
| 7070 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7071 | return ACT_RET_PRS_OK; |
| 7072 | } |
| 7073 | |
| 7074 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7075 | struct act_rule *rule, char **err) |
| 7076 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7077 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7078 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7079 | if (px->options2 & PR_O2_USE_HTX) { |
| 7080 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 7081 | return ACT_RET_PRS_ERR; |
| 7082 | } |
| 7083 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7084 | /* Memory for the rule. */ |
| 7085 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7086 | if (!rule->arg.hlua_rule) { |
| 7087 | memprintf(err, "out of memory error"); |
| 7088 | return ACT_RET_PRS_ERR; |
| 7089 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7090 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7091 | /* Reference the Lua function and store the reference. */ |
| 7092 | rule->arg.hlua_rule->fcn = *fcn; |
| 7093 | |
| 7094 | /* TODO: later accept arguments. */ |
| 7095 | rule->arg.hlua_rule->args = NULL; |
| 7096 | |
| 7097 | /* Add applet pointer in the rule. */ |
| 7098 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7099 | rule->applet.name = fcn->name; |
| 7100 | rule->applet.init = hlua_applet_tcp_init; |
| 7101 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7102 | rule->applet.release = hlua_applet_tcp_release; |
| 7103 | rule->applet.timeout = hlua_timeout_applet; |
| 7104 | |
| 7105 | return 0; |
| 7106 | } |
| 7107 | |
| 7108 | /* This function is an LUA binding used for registering |
| 7109 | * "sample-conv" functions. It expects a converter name used |
| 7110 | * in the haproxy configuration file, and an LUA function. |
| 7111 | */ |
| 7112 | __LJMP static int hlua_register_service(lua_State *L) |
| 7113 | { |
| 7114 | struct action_kw_list *akl; |
| 7115 | const char *name; |
| 7116 | const char *env; |
| 7117 | int ref; |
| 7118 | int len; |
| 7119 | struct hlua_function *fcn; |
| 7120 | |
| 7121 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7122 | |
| 7123 | /* First argument : converter name. */ |
| 7124 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7125 | |
| 7126 | /* Second argument : environment. */ |
| 7127 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7128 | |
| 7129 | /* Third argument : lua function. */ |
| 7130 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7131 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7132 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7133 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7134 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7135 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7136 | fcn = calloc(1, sizeof(*fcn)); |
| 7137 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7138 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7139 | |
| 7140 | /* Fill fcn. */ |
| 7141 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7142 | fcn->name = calloc(1, len); |
| 7143 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7144 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7145 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7146 | fcn->function_ref = ref; |
| 7147 | |
| 7148 | /* List head */ |
| 7149 | akl->list.n = akl->list.p = NULL; |
| 7150 | |
| 7151 | /* converter keyword. */ |
| 7152 | len = strlen("lua.") + strlen(name) + 1; |
| 7153 | akl->kw[0].kw = calloc(1, len); |
| 7154 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7155 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7156 | |
| 7157 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7158 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7159 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7160 | if (strcmp(env, "tcp") == 0) |
| 7161 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7162 | else if (strcmp(env, "http") == 0) |
| 7163 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7164 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7165 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7166 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7167 | |
| 7168 | akl->kw[0].match_pfx = 0; |
| 7169 | akl->kw[0].private = fcn; |
| 7170 | |
| 7171 | /* End of array. */ |
| 7172 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7173 | |
| 7174 | /* Register this new converter */ |
| 7175 | service_keywords_register(akl); |
| 7176 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7177 | return 0; |
| 7178 | } |
| 7179 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7180 | /* This function initialises Lua cli handler. It copies the |
| 7181 | * arguments in the Lua stack and create channel IO objects. |
| 7182 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7183 | 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] | 7184 | { |
| 7185 | struct hlua *hlua; |
| 7186 | struct hlua_function *fcn; |
| 7187 | int i; |
| 7188 | const char *error; |
| 7189 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7190 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7191 | appctx->ctx.hlua_cli.fcn = private; |
| 7192 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7193 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7194 | if (!hlua) { |
| 7195 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7196 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7197 | } |
| 7198 | HLUA_INIT(hlua); |
| 7199 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7200 | |
| 7201 | /* Create task used by signal to wakeup applets. |
| 7202 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7203 | * applet_http. It is absolutely compatible. |
| 7204 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7205 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7206 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7207 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7208 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7209 | } |
| 7210 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7211 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7212 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7213 | |
| 7214 | /* Initialises the Lua context */ |
| 7215 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 7216 | 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] | 7217 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7218 | } |
| 7219 | |
| 7220 | /* The following Lua calls can fail. */ |
| 7221 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7222 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7223 | error = lua_tostring(hlua->T, -1); |
| 7224 | else |
| 7225 | error = "critical error"; |
| 7226 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7227 | goto error; |
| 7228 | } |
| 7229 | |
| 7230 | /* Check stack available size. */ |
| 7231 | if (!lua_checkstack(hlua->T, 2)) { |
| 7232 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7233 | goto error; |
| 7234 | } |
| 7235 | |
| 7236 | /* Restore the function in the stack. */ |
| 7237 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7238 | |
| 7239 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7240 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7241 | */ |
| 7242 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7243 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7244 | goto error; |
| 7245 | } |
| 7246 | hlua->nargs = 1; |
| 7247 | |
| 7248 | /* push keywords in the stack. */ |
| 7249 | for (i = 0; *args[i]; i++) { |
| 7250 | /* Check stack available size. */ |
| 7251 | if (!lua_checkstack(hlua->T, 1)) { |
| 7252 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7253 | goto error; |
| 7254 | } |
| 7255 | lua_pushstring(hlua->T, args[i]); |
| 7256 | hlua->nargs++; |
| 7257 | } |
| 7258 | |
| 7259 | /* We must initialize the execution timeouts. */ |
| 7260 | hlua->max_time = hlua_timeout_session; |
| 7261 | |
| 7262 | /* At this point the execution is safe. */ |
| 7263 | RESET_SAFE_LJMP(hlua->T); |
| 7264 | |
| 7265 | /* It's ok */ |
| 7266 | return 0; |
| 7267 | |
| 7268 | /* It's not ok. */ |
| 7269 | error: |
| 7270 | RESET_SAFE_LJMP(hlua->T); |
| 7271 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7272 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7273 | return 1; |
| 7274 | } |
| 7275 | |
| 7276 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7277 | { |
| 7278 | struct hlua *hlua; |
| 7279 | struct stream_interface *si; |
| 7280 | struct hlua_function *fcn; |
| 7281 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7282 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7283 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7284 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7285 | |
| 7286 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7287 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7288 | return 1; |
| 7289 | |
| 7290 | /* Execute the function. */ |
| 7291 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7292 | |
| 7293 | /* finished. */ |
| 7294 | case HLUA_E_OK: |
| 7295 | return 1; |
| 7296 | |
| 7297 | /* yield. */ |
| 7298 | case HLUA_E_AGAIN: |
| 7299 | /* We want write. */ |
| 7300 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7301 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7302 | /* Set the timeout. */ |
| 7303 | if (hlua->wake_time != TICK_ETERNITY) |
| 7304 | task_schedule(hlua->task, hlua->wake_time); |
| 7305 | return 0; |
| 7306 | |
| 7307 | /* finished with error. */ |
| 7308 | case HLUA_E_ERRMSG: |
| 7309 | /* Display log. */ |
| 7310 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7311 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7312 | lua_pop(hlua->T, 1); |
| 7313 | return 1; |
| 7314 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7315 | case HLUA_E_ETMOUT: |
| 7316 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7317 | fcn->name); |
| 7318 | return 1; |
| 7319 | |
| 7320 | case HLUA_E_NOMEM: |
| 7321 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7322 | fcn->name); |
| 7323 | return 1; |
| 7324 | |
| 7325 | case HLUA_E_YIELD: /* unexpected */ |
| 7326 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7327 | fcn->name); |
| 7328 | return 1; |
| 7329 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7330 | case HLUA_E_ERR: |
| 7331 | /* Display log. */ |
| 7332 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7333 | fcn->name); |
| 7334 | return 1; |
| 7335 | |
| 7336 | default: |
| 7337 | return 1; |
| 7338 | } |
| 7339 | |
| 7340 | return 1; |
| 7341 | } |
| 7342 | |
| 7343 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7344 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7345 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7346 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7347 | } |
| 7348 | |
| 7349 | /* This function is an LUA binding used for registering |
| 7350 | * new keywords in the cli. It expects a list of keywords |
| 7351 | * which are the "path". It is limited to 5 keywords. A |
| 7352 | * description of the command, a function to be executed |
| 7353 | * for the parsing and a function for io handlers. |
| 7354 | */ |
| 7355 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7356 | { |
| 7357 | struct cli_kw_list *cli_kws; |
| 7358 | const char *message; |
| 7359 | int ref_io; |
| 7360 | int len; |
| 7361 | struct hlua_function *fcn; |
| 7362 | int index; |
| 7363 | int i; |
| 7364 | |
| 7365 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7366 | |
| 7367 | /* First argument : an array of maximum 5 keywords. */ |
| 7368 | if (!lua_istable(L, 1)) |
| 7369 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7370 | |
| 7371 | /* Second argument : string with contextual message. */ |
| 7372 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7373 | |
| 7374 | /* Third and fourth argument : lua function. */ |
| 7375 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7376 | |
| 7377 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7378 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7379 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7380 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7381 | fcn = calloc(1, sizeof(*fcn)); |
| 7382 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7383 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7384 | |
| 7385 | /* Fill path. */ |
| 7386 | index = 0; |
| 7387 | lua_pushnil(L); |
| 7388 | while(lua_next(L, 1) != 0) { |
| 7389 | if (index >= 5) |
| 7390 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7391 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7392 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7393 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7394 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7395 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7396 | index++; |
| 7397 | lua_pop(L, 1); |
| 7398 | } |
| 7399 | |
| 7400 | /* Copy help message. */ |
| 7401 | cli_kws->kw[0].usage = strdup(message); |
| 7402 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7403 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7404 | |
| 7405 | /* Fill fcn io handler. */ |
| 7406 | len = strlen("<lua.cli>") + 1; |
| 7407 | for (i = 0; i < index; i++) |
| 7408 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7409 | fcn->name = calloc(1, len); |
| 7410 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7411 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7412 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7413 | for (i = 0; i < index; i++) { |
| 7414 | strncat((char *)fcn->name, ".", len); |
| 7415 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7416 | } |
| 7417 | strncat((char *)fcn->name, ">", len); |
| 7418 | fcn->function_ref = ref_io; |
| 7419 | |
| 7420 | /* Fill last entries. */ |
| 7421 | cli_kws->kw[0].private = fcn; |
| 7422 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7423 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7424 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7425 | |
| 7426 | /* Register this new converter */ |
| 7427 | cli_register_kw(cli_kws); |
| 7428 | |
| 7429 | return 0; |
| 7430 | } |
| 7431 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7432 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7433 | struct proxy *defpx, const char *file, int line, |
| 7434 | char **err, unsigned int *timeout) |
| 7435 | { |
| 7436 | const char *error; |
| 7437 | |
| 7438 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 7439 | if (error && *error != '\0') { |
| 7440 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7441 | return -1; |
| 7442 | } |
| 7443 | return 0; |
| 7444 | } |
| 7445 | |
| 7446 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7447 | struct proxy *defpx, const char *file, int line, |
| 7448 | char **err) |
| 7449 | { |
| 7450 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7451 | file, line, err, &hlua_timeout_session); |
| 7452 | } |
| 7453 | |
| 7454 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7455 | struct proxy *defpx, const char *file, int line, |
| 7456 | char **err) |
| 7457 | { |
| 7458 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7459 | file, line, err, &hlua_timeout_task); |
| 7460 | } |
| 7461 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7462 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7463 | struct proxy *defpx, const char *file, int line, |
| 7464 | char **err) |
| 7465 | { |
| 7466 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7467 | file, line, err, &hlua_timeout_applet); |
| 7468 | } |
| 7469 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7470 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7471 | struct proxy *defpx, const char *file, int line, |
| 7472 | char **err) |
| 7473 | { |
| 7474 | char *error; |
| 7475 | |
| 7476 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7477 | if (*error != '\0') { |
| 7478 | memprintf(err, "%s: invalid number", args[0]); |
| 7479 | return -1; |
| 7480 | } |
| 7481 | return 0; |
| 7482 | } |
| 7483 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7484 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7485 | struct proxy *defpx, const char *file, int line, |
| 7486 | char **err) |
| 7487 | { |
| 7488 | char *error; |
| 7489 | |
| 7490 | if (*(args[1]) == 0) { |
| 7491 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7492 | return -1; |
| 7493 | } |
| 7494 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7495 | if (*error != '\0') { |
| 7496 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7497 | return -1; |
| 7498 | } |
| 7499 | return 0; |
| 7500 | } |
| 7501 | |
| 7502 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7503 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7504 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7505 | * the main lua entry point. |
| 7506 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7507 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7508 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7509 | * |
| 7510 | * In some error case, LUA set an error message in top of the stack. This function |
| 7511 | * 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] | 7512 | * |
| 7513 | * This function can fail with an abort() due to an Lua critical error. |
| 7514 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7515 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7516 | */ |
| 7517 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7518 | struct proxy *defpx, const char *file, int line, |
| 7519 | char **err) |
| 7520 | { |
| 7521 | int error; |
| 7522 | |
| 7523 | /* Just load and compile the file. */ |
| 7524 | error = luaL_loadfile(gL.T, args[1]); |
| 7525 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7526 | 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] | 7527 | lua_pop(gL.T, 1); |
| 7528 | return -1; |
| 7529 | } |
| 7530 | |
| 7531 | /* If no syntax error where detected, execute the code. */ |
| 7532 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7533 | switch (error) { |
| 7534 | case LUA_OK: |
| 7535 | break; |
| 7536 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7537 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7538 | lua_pop(gL.T, 1); |
| 7539 | return -1; |
| 7540 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7541 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7542 | return -1; |
| 7543 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7544 | 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] | 7545 | lua_pop(gL.T, 1); |
| 7546 | return -1; |
| 7547 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7548 | 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] | 7549 | lua_pop(gL.T, 1); |
| 7550 | return -1; |
| 7551 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7552 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7553 | lua_pop(gL.T, 1); |
| 7554 | return -1; |
| 7555 | } |
| 7556 | |
| 7557 | return 0; |
| 7558 | } |
| 7559 | |
| 7560 | /* configuration keywords declaration */ |
| 7561 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7562 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7563 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7564 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7565 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7566 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7567 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7568 | { 0, NULL, NULL }, |
| 7569 | }}; |
| 7570 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7571 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 7572 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7573 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7574 | /* This function can fail with an abort() due to an Lua critical error. |
| 7575 | * We are in the initialisation process of HAProxy, this abort() is |
| 7576 | * tolerated. |
| 7577 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7578 | int hlua_post_init() |
| 7579 | { |
| 7580 | struct hlua_init_function *init; |
| 7581 | const char *msg; |
| 7582 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7583 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7584 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7585 | /* Call post initialisation function in safe environement. */ |
| 7586 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7587 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7588 | error = lua_tostring(gL.T, -1); |
| 7589 | else |
| 7590 | error = "critical error"; |
| 7591 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7592 | exit(1); |
| 7593 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7594 | |
| 7595 | #if USE_OPENSSL |
| 7596 | /* Initialize SSL server. */ |
| 7597 | if (socket_ssl.xprt->prepare_srv) { |
| 7598 | int saved_used_backed = global.ssl_used_backend; |
| 7599 | // don't affect maxconn automatic computation |
| 7600 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7601 | global.ssl_used_backend = saved_used_backed; |
| 7602 | } |
| 7603 | #endif |
| 7604 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7605 | hlua_fcn_post_init(gL.T); |
| 7606 | RESET_SAFE_LJMP(gL.T); |
| 7607 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7608 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7609 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7610 | ret = hlua_ctx_resume(&gL, 0); |
| 7611 | switch (ret) { |
| 7612 | case HLUA_E_OK: |
| 7613 | lua_pop(gL.T, -1); |
| 7614 | return 1; |
| 7615 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7616 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7617 | return 0; |
| 7618 | case HLUA_E_ERRMSG: |
| 7619 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7620 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7621 | return 0; |
| 7622 | case HLUA_E_ERR: |
| 7623 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7624 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7625 | return 0; |
| 7626 | } |
| 7627 | } |
| 7628 | return 1; |
| 7629 | } |
| 7630 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7631 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7632 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7633 | * is the previously allocated size or the kind of object in case of a new |
| 7634 | * allocation. <nsize> is the requested new size. |
| 7635 | */ |
| 7636 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7637 | { |
| 7638 | struct hlua_mem_allocator *zone = ud; |
| 7639 | |
| 7640 | if (nsize == 0) { |
| 7641 | /* it's a free */ |
| 7642 | if (ptr) |
| 7643 | zone->allocated -= osize; |
| 7644 | free(ptr); |
| 7645 | return NULL; |
| 7646 | } |
| 7647 | |
| 7648 | if (!ptr) { |
| 7649 | /* it's a new allocation */ |
| 7650 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7651 | return NULL; |
| 7652 | |
| 7653 | ptr = malloc(nsize); |
| 7654 | if (ptr) |
| 7655 | zone->allocated += nsize; |
| 7656 | return ptr; |
| 7657 | } |
| 7658 | |
| 7659 | /* it's a realloc */ |
| 7660 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7661 | return NULL; |
| 7662 | |
| 7663 | ptr = realloc(ptr, nsize); |
| 7664 | if (ptr) |
| 7665 | zone->allocated += nsize - osize; |
| 7666 | return ptr; |
| 7667 | } |
| 7668 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7669 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7670 | * We are in the initialisation process of HAProxy, this abort() is |
| 7671 | * tolerated. |
| 7672 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7673 | void hlua_init(void) |
| 7674 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7675 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7676 | int idx; |
| 7677 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7678 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7679 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7680 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7681 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7682 | struct srv_kw *kw; |
| 7683 | int tmp_error; |
| 7684 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7685 | char *args[] = { /* SSL client configuration. */ |
| 7686 | "ssl", |
| 7687 | "verify", |
| 7688 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7689 | NULL |
| 7690 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7691 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7692 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7693 | /* Init main lua stack. */ |
| 7694 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7695 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7696 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7697 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7698 | hlua_sethlua(&gL); |
| 7699 | gL.Tref = LUA_REFNIL; |
| 7700 | gL.task = NULL; |
| 7701 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7702 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7703 | * the Lua function can fail with an abort. We are in the initialisation |
| 7704 | * process of HAProxy, this abort() is tolerated. |
| 7705 | */ |
| 7706 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7707 | /* Initialise lua. */ |
| 7708 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7709 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7710 | /* Set safe environment for the initialisation. */ |
| 7711 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7712 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7713 | error_msg = lua_tostring(gL.T, -1); |
| 7714 | else |
| 7715 | error_msg = "critical error"; |
| 7716 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7717 | exit(1); |
| 7718 | } |
| 7719 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7720 | /* |
| 7721 | * |
| 7722 | * Create "core" object. |
| 7723 | * |
| 7724 | */ |
| 7725 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7726 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7727 | lua_newtable(gL.T); |
| 7728 | |
| 7729 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7730 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7731 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7732 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7733 | /* Register special functions. */ |
| 7734 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7735 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7736 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7737 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7738 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7739 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7740 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7741 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7742 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7743 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7744 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7745 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7746 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7747 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7748 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7749 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7750 | hlua_class_function(gL.T, "log", hlua_log); |
| 7751 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7752 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7753 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7754 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7755 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7756 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7757 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7758 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7759 | |
| 7760 | /* |
| 7761 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7762 | * Register class Map |
| 7763 | * |
| 7764 | */ |
| 7765 | |
| 7766 | /* This table entry is the object "Map" base. */ |
| 7767 | lua_newtable(gL.T); |
| 7768 | |
| 7769 | /* register pattern types. */ |
| 7770 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7771 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7772 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7773 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 7774 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7775 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7776 | |
| 7777 | /* register constructor. */ |
| 7778 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7779 | |
| 7780 | /* Create and fill the metatable. */ |
| 7781 | lua_newtable(gL.T); |
| 7782 | |
| 7783 | /* Create and fille the __index entry. */ |
| 7784 | lua_pushstring(gL.T, "__index"); |
| 7785 | lua_newtable(gL.T); |
| 7786 | |
| 7787 | /* Register . */ |
| 7788 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7789 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7790 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7791 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7792 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7793 | /* Register previous table in the registry with reference and named entry. |
| 7794 | * The function hlua_register_metatable() pops the stack, so we |
| 7795 | * previously create a copy of the table. |
| 7796 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7797 | 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] | 7798 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7799 | |
| 7800 | /* Assign the metatable to the mai Map object. */ |
| 7801 | lua_setmetatable(gL.T, -2); |
| 7802 | |
| 7803 | /* Set a name to the table. */ |
| 7804 | lua_setglobal(gL.T, "Map"); |
| 7805 | |
| 7806 | /* |
| 7807 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7808 | * Register class Channel |
| 7809 | * |
| 7810 | */ |
| 7811 | |
| 7812 | /* Create and fill the metatable. */ |
| 7813 | lua_newtable(gL.T); |
| 7814 | |
| 7815 | /* Create and fille the __index entry. */ |
| 7816 | lua_pushstring(gL.T, "__index"); |
| 7817 | lua_newtable(gL.T); |
| 7818 | |
| 7819 | /* Register . */ |
| 7820 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7821 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7822 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7823 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7824 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7825 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7826 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7827 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7828 | 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] | 7829 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7830 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7831 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7832 | |
| 7833 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7834 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7835 | |
| 7836 | /* |
| 7837 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7838 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7839 | * |
| 7840 | */ |
| 7841 | |
| 7842 | /* Create and fill the metatable. */ |
| 7843 | lua_newtable(gL.T); |
| 7844 | |
| 7845 | /* Create and fille the __index entry. */ |
| 7846 | lua_pushstring(gL.T, "__index"); |
| 7847 | lua_newtable(gL.T); |
| 7848 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7849 | /* Browse existing fetches and create the associated |
| 7850 | * object method. |
| 7851 | */ |
| 7852 | sf = NULL; |
| 7853 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7854 | |
| 7855 | /* Dont register the keywork if the arguments check function are |
| 7856 | * not safe during the runtime. |
| 7857 | */ |
| 7858 | if ((sf->val_args != NULL) && |
| 7859 | (sf->val_args != val_payload_lv) && |
| 7860 | (sf->val_args != val_hdr)) |
| 7861 | continue; |
| 7862 | |
| 7863 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7864 | * by an underscore. |
| 7865 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7866 | strncpy(trash.area, sf->kw, trash.size); |
| 7867 | trash.area[trash.size - 1] = '\0'; |
| 7868 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7869 | if (*p == '.' || *p == '-' || *p == '+') |
| 7870 | *p = '_'; |
| 7871 | |
| 7872 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7873 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7874 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7875 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7876 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7877 | } |
| 7878 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7879 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7880 | |
| 7881 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7882 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7883 | |
| 7884 | /* |
| 7885 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7886 | * Register class Converters |
| 7887 | * |
| 7888 | */ |
| 7889 | |
| 7890 | /* Create and fill the metatable. */ |
| 7891 | lua_newtable(gL.T); |
| 7892 | |
| 7893 | /* Create and fill the __index entry. */ |
| 7894 | lua_pushstring(gL.T, "__index"); |
| 7895 | lua_newtable(gL.T); |
| 7896 | |
| 7897 | /* Browse existing converters and create the associated |
| 7898 | * object method. |
| 7899 | */ |
| 7900 | sc = NULL; |
| 7901 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7902 | /* Dont register the keywork if the arguments check function are |
| 7903 | * not safe during the runtime. |
| 7904 | */ |
| 7905 | if (sc->val_args != NULL) |
| 7906 | continue; |
| 7907 | |
| 7908 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7909 | * by an underscore. |
| 7910 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7911 | strncpy(trash.area, sc->kw, trash.size); |
| 7912 | trash.area[trash.size - 1] = '\0'; |
| 7913 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7914 | if (*p == '.' || *p == '-' || *p == '+') |
| 7915 | *p = '_'; |
| 7916 | |
| 7917 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7918 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7919 | lua_pushlightuserdata(gL.T, sc); |
| 7920 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7921 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7922 | } |
| 7923 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7924 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7925 | |
| 7926 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7927 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7928 | |
| 7929 | /* |
| 7930 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7931 | * Register class HTTP |
| 7932 | * |
| 7933 | */ |
| 7934 | |
| 7935 | /* Create and fill the metatable. */ |
| 7936 | lua_newtable(gL.T); |
| 7937 | |
| 7938 | /* Create and fille the __index entry. */ |
| 7939 | lua_pushstring(gL.T, "__index"); |
| 7940 | lua_newtable(gL.T); |
| 7941 | |
| 7942 | /* Register Lua functions. */ |
| 7943 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7944 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7945 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7946 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7947 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7948 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7949 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7950 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7951 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7952 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7953 | |
| 7954 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7955 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7956 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7957 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7958 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7959 | 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] | 7960 | 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] | 7961 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7962 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7963 | |
| 7964 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7965 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7966 | |
| 7967 | /* |
| 7968 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7969 | * Register class AppletTCP |
| 7970 | * |
| 7971 | */ |
| 7972 | |
| 7973 | /* Create and fill the metatable. */ |
| 7974 | lua_newtable(gL.T); |
| 7975 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7976 | /* Create and fille the __index entry. */ |
| 7977 | lua_pushstring(gL.T, "__index"); |
| 7978 | lua_newtable(gL.T); |
| 7979 | |
| 7980 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7981 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7982 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7983 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7984 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7985 | 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] | 7986 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7987 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7988 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7989 | |
| 7990 | lua_settable(gL.T, -3); |
| 7991 | |
| 7992 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7993 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7994 | |
| 7995 | /* |
| 7996 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7997 | * Register class AppletHTTP |
| 7998 | * |
| 7999 | */ |
| 8000 | |
| 8001 | /* Create and fill the metatable. */ |
| 8002 | lua_newtable(gL.T); |
| 8003 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8004 | /* Create and fille the __index entry. */ |
| 8005 | lua_pushstring(gL.T, "__index"); |
| 8006 | lua_newtable(gL.T); |
| 8007 | |
| 8008 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8009 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8010 | 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] | 8011 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8012 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8013 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8014 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8015 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8016 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8017 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8018 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8019 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8020 | |
| 8021 | lua_settable(gL.T, -3); |
| 8022 | |
| 8023 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8024 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8025 | |
| 8026 | /* |
| 8027 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8028 | * Register class TXN |
| 8029 | * |
| 8030 | */ |
| 8031 | |
| 8032 | /* Create and fill the metatable. */ |
| 8033 | lua_newtable(gL.T); |
| 8034 | |
| 8035 | /* Create and fille the __index entry. */ |
| 8036 | lua_pushstring(gL.T, "__index"); |
| 8037 | lua_newtable(gL.T); |
| 8038 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8039 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8040 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8041 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8042 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8043 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8044 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8045 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 8046 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8047 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8048 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8049 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8050 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8051 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8052 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8053 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8054 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8055 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8056 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8057 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8058 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8059 | |
| 8060 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8061 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8062 | |
| 8063 | /* |
| 8064 | * |
| 8065 | * Register class Socket |
| 8066 | * |
| 8067 | */ |
| 8068 | |
| 8069 | /* Create and fill the metatable. */ |
| 8070 | lua_newtable(gL.T); |
| 8071 | |
| 8072 | /* Create and fille the __index entry. */ |
| 8073 | lua_pushstring(gL.T, "__index"); |
| 8074 | lua_newtable(gL.T); |
| 8075 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8076 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8077 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8078 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8079 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8080 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8081 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8082 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8083 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8084 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8085 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8086 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8087 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8088 | 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] | 8089 | |
| 8090 | /* Register the garbage collector entry. */ |
| 8091 | lua_pushstring(gL.T, "__gc"); |
| 8092 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8093 | 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] | 8094 | |
| 8095 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8096 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8097 | |
| 8098 | /* Proxy and server configuration initialisation. */ |
| 8099 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8100 | init_new_proxy(&socket_proxy); |
| 8101 | socket_proxy.parent = NULL; |
| 8102 | socket_proxy.last_change = now.tv_sec; |
| 8103 | socket_proxy.id = "LUA-SOCKET"; |
| 8104 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8105 | socket_proxy.maxconn = 0; |
| 8106 | socket_proxy.accept = NULL; |
| 8107 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8108 | socket_proxy.srv = NULL; |
| 8109 | socket_proxy.conn_retries = 0; |
| 8110 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8111 | |
| 8112 | /* Init TCP server: unchanged parameters */ |
| 8113 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8114 | socket_tcp.next = NULL; |
| 8115 | socket_tcp.proxy = &socket_proxy; |
| 8116 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8117 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8118 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8119 | socket_tcp.priv_conns = NULL; |
| 8120 | socket_tcp.idle_conns = NULL; |
| 8121 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8122 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8123 | socket_tcp.last_change = 0; |
| 8124 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8125 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8126 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8127 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8128 | |
| 8129 | /* XXX: Copy default parameter from default server, |
| 8130 | * but the default server is not initialized. |
| 8131 | */ |
| 8132 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8133 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8134 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8135 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8136 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8137 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8138 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8139 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8140 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8141 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8142 | |
| 8143 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8144 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8145 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8146 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8147 | socket_tcp.check.server = &socket_tcp; |
| 8148 | |
| 8149 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8150 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8151 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8152 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8153 | socket_tcp.agent.server = &socket_tcp; |
| 8154 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8155 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8156 | |
| 8157 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8158 | /* Init TCP server: unchanged parameters */ |
| 8159 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8160 | socket_ssl.next = NULL; |
| 8161 | socket_ssl.proxy = &socket_proxy; |
| 8162 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8163 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8164 | socket_ssl.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8165 | socket_tcp.priv_conns = NULL; |
| 8166 | socket_tcp.idle_conns = NULL; |
| 8167 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8168 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8169 | socket_ssl.last_change = 0; |
| 8170 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8171 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8172 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8173 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8174 | |
| 8175 | /* XXX: Copy default parameter from default server, |
| 8176 | * but the default server is not initialized. |
| 8177 | */ |
| 8178 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8179 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8180 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8181 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8182 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8183 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8184 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8185 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8186 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8187 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8188 | |
| 8189 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8190 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8191 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8192 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8193 | socket_ssl.check.server = &socket_ssl; |
| 8194 | |
| 8195 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8196 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8197 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8198 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8199 | socket_ssl.agent.server = &socket_ssl; |
| 8200 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8201 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8202 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8203 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8204 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8205 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8206 | /* |
| 8207 | * |
| 8208 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8209 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8210 | * features like client certificates and ssl_verify. |
| 8211 | * |
| 8212 | */ |
| 8213 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8214 | if (tmp_error != 0) { |
| 8215 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8216 | abort(); /* This must be never arrives because the command line |
| 8217 | not editable by the user. */ |
| 8218 | } |
| 8219 | idx += kw->skip; |
| 8220 | } |
| 8221 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8222 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8223 | |
| 8224 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8225 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8226 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8227 | static void hlua_register_build_options(void) |
| 8228 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8229 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8230 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8231 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8232 | hap_register_build_opts(ptr, 1); |
| 8233 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8234 | |
| 8235 | INITCALL0(STG_REGISTER, hlua_register_build_options); |