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> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 32 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 33 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 34 | #include <types/hlua.h> |
| 35 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 36 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 37 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 38 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 39 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 40 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 41 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 42 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 43 | #include <proto/stats.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 44 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 45 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 46 | #include <proto/hlua_fcn.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 47 | #include <proto/http_fetch.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 48 | #include <proto/http_rules.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 49 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 50 | #include <proto/obj_type.h> |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 51 | #include <proto/queue.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 52 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 53 | #include <proto/payload.h> |
| 54 | #include <proto/proto_http.h> |
| 55 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 56 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 57 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 58 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 59 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 60 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 61 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 62 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 63 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 64 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 65 | * macro is used only for identifying the function that can |
| 66 | * not return because a longjmp is executed. |
| 67 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 68 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 69 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 70 | */ |
| 71 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 72 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 73 | #define MAY_LJMP(func) func |
| 74 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 75 | /* This couple of function executes securely some Lua calls outside of |
| 76 | * the lua runtime environment. Each Lua call can return a longjmp |
| 77 | * if it encounter a memory error. |
| 78 | * |
| 79 | * Lua documentation extract: |
| 80 | * |
| 81 | * If an error happens outside any protected environment, Lua calls |
| 82 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 83 | * exiting the host application. Your panic function can avoid this |
| 84 | * exit by never returning (e.g., doing a long jump to your own |
| 85 | * recovery point outside Lua). |
| 86 | * |
| 87 | * The panic function runs as if it were a message handler (see |
| 88 | * §2.3); in particular, the error message is at the top of the |
| 89 | * stack. However, there is no guarantee about stack space. To push |
| 90 | * anything on the stack, the panic function must first check the |
| 91 | * available space (see §4.2). |
| 92 | * |
| 93 | * We must check all the Lua entry point. This includes: |
| 94 | * - The include/proto/hlua.h exported functions |
| 95 | * - the task wrapper function |
| 96 | * - The action wrapper function |
| 97 | * - The converters wrapper function |
| 98 | * - The sample-fetch wrapper functions |
| 99 | * |
| 100 | * It is tolerated that the initilisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 101 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 102 | * |
| 103 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 104 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 105 | * because they must be exists in the program stack when the longjmp |
| 106 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 107 | * |
| 108 | * Note that the Lua processing is not really thread safe. It provides |
| 109 | * heavy system which consists to add our own lock function in the Lua |
| 110 | * code and recompile the library. This system will probably not accepted |
| 111 | * by maintainers of various distribs. |
| 112 | * |
| 113 | * Our main excution point of the Lua is the function lua_resume(). A |
| 114 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 115 | * of function and a lua_unlock() at the end of the function. So I |
| 116 | * conclude that the Lua thread safe mode just perform a mutex around |
| 117 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 118 | * easier for distro maintainers. |
| 119 | * |
| 120 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 121 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 122 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 123 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 124 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 125 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 126 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 127 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 128 | |
| 129 | #define SET_SAFE_LJMP(__L) \ |
| 130 | ({ \ |
| 131 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 132 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 133 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 134 | lua_atpanic(__L, hlua_panic_safe); \ |
| 135 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 136 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 137 | } else { \ |
| 138 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 139 | ret = 1; \ |
| 140 | } \ |
| 141 | ret; \ |
| 142 | }) |
| 143 | |
| 144 | /* If we are the last function catching Lua errors, we |
| 145 | * must reset the panic function. |
| 146 | */ |
| 147 | #define RESET_SAFE_LJMP(__L) \ |
| 148 | do { \ |
| 149 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 150 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 151 | } while(0) |
| 152 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 153 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 154 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
| 155 | #define APPLET_100C 0x02 /* 100 continue expected. */ |
| 156 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 157 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 158 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 159 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 160 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 161 | /* The main Lua execution context. */ |
| 162 | struct hlua gL; |
| 163 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 164 | /* This is the memory pool containing struct lua for applets |
| 165 | * (including cli). |
| 166 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 167 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 168 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 169 | /* Used for Socket connection. */ |
| 170 | static struct proxy socket_proxy; |
| 171 | static struct server socket_tcp; |
| 172 | #ifdef USE_OPENSSL |
| 173 | static struct server socket_ssl; |
| 174 | #endif |
| 175 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 176 | /* List head of the function called at the initialisation time. */ |
| 177 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 178 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 179 | /* The following variables contains the reference of the different |
| 180 | * Lua classes. These references are useful for identify metadata |
| 181 | * associated with an object. |
| 182 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 183 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 184 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 185 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 186 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 187 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 188 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 189 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 190 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 191 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 192 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 193 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 194 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 195 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 196 | * because a task may remain alive during all the haproxy execution. |
| 197 | */ |
| 198 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 199 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 200 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 201 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 202 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 203 | * it is used for preventing infinite loops. |
| 204 | * |
| 205 | * I test the scheer with an infinite loop containing one incrementation |
| 206 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 207 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 208 | * to one interrupt each 10 000 instructions. |
| 209 | * |
| 210 | * configured | Number of |
| 211 | * instructions | loops executed |
| 212 | * between two | in milions |
| 213 | * forced yields | |
| 214 | * ---------------+--------------- |
| 215 | * 10 | 160 |
| 216 | * 500 | 670 |
| 217 | * 1000 | 680 |
| 218 | * 5000 | 700 |
| 219 | * 7000 | 700 |
| 220 | * 8000 | 700 |
| 221 | * 9000 | 710 <- ceil |
| 222 | * 10000 | 710 |
| 223 | * 100000 | 710 |
| 224 | * 1000000 | 710 |
| 225 | * |
| 226 | */ |
| 227 | static unsigned int hlua_nb_instruction = 10000; |
| 228 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 229 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 230 | * be enforced on any memory allocation. |
| 231 | */ |
| 232 | struct hlua_mem_allocator { |
| 233 | size_t allocated; |
| 234 | size_t limit; |
| 235 | }; |
| 236 | |
| 237 | static struct hlua_mem_allocator hlua_global_allocator; |
| 238 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 239 | static const char error_500[] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 240 | "HTTP/1.0 500 Internal Server Error\r\n" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 241 | "Cache-Control: no-cache\r\n" |
| 242 | "Connection: close\r\n" |
| 243 | "Content-Type: text/html\r\n" |
| 244 | "\r\n" |
Joseph Herlant | 7fe1577 | 2018-11-15 10:07:32 -0800 | [diff] [blame] | 245 | "<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] | 246 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 247 | /* These functions converts types between HAProxy internal args or |
| 248 | * sample and LUA types. Another function permits to check if the |
| 249 | * LUA stack contains arguments according with an required ARG_T |
| 250 | * format. |
| 251 | */ |
| 252 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 253 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 254 | __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] | 255 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 256 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 257 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 258 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 259 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 260 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 261 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 262 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 263 | do { \ |
| 264 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 265 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 266 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 267 | } while (0) |
| 268 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 269 | /* Used to check an Lua function type in the stack. It creates and |
| 270 | * returns a reference of the function. This function throws an |
| 271 | * error if the rgument is not a "function". |
| 272 | */ |
| 273 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 274 | { |
| 275 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 276 | 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] | 277 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 278 | } |
| 279 | lua_pushvalue(L, argno); |
| 280 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 281 | } |
| 282 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 283 | /* Return the string that is of the top of the stack. */ |
| 284 | const char *hlua_get_top_error_string(lua_State *L) |
| 285 | { |
| 286 | if (lua_gettop(L) < 1) |
| 287 | return "unknown error"; |
| 288 | if (lua_type(L, -1) != LUA_TSTRING) |
| 289 | return "unknown error"; |
| 290 | return lua_tostring(L, -1); |
| 291 | } |
| 292 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 293 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 294 | { |
| 295 | lua_Debug ar; |
| 296 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 297 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 298 | int filled = 0; |
| 299 | |
| 300 | while (lua_getstack(L, level++, &ar)) { |
| 301 | |
| 302 | /* Add separator */ |
| 303 | if (filled) |
| 304 | chunk_appendf(msg, ", "); |
| 305 | filled = 1; |
| 306 | |
| 307 | /* Fill fields: |
| 308 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 309 | * 'l': fills in the field currentline; |
| 310 | * 'n': fills in the field name and namewhat; |
| 311 | * 't': fills in the field istailcall; |
| 312 | */ |
| 313 | lua_getinfo(L, "Slnt", &ar); |
| 314 | |
| 315 | /* Append code localisation */ |
| 316 | if (ar.currentline > 0) |
| 317 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 318 | else |
| 319 | chunk_appendf(msg, "%s ", ar.short_src); |
| 320 | |
| 321 | /* |
| 322 | * Get function name |
| 323 | * |
| 324 | * if namewhat is no empty, name is defined. |
| 325 | * what contains "Lua" for Lua function, "C" for C function, |
| 326 | * or "main" for main code. |
| 327 | */ |
| 328 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 329 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 330 | |
| 331 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 332 | chunk_appendf(msg, "main chunk"); |
| 333 | |
| 334 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 335 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 336 | |
| 337 | else /* nothing left... */ |
| 338 | chunk_appendf(msg, "?"); |
| 339 | |
| 340 | |
| 341 | /* Display tailed call */ |
| 342 | if (ar.istailcall) |
| 343 | chunk_appendf(msg, " ..."); |
| 344 | } |
| 345 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 346 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 350 | /* This function check the number of arguments available in the |
| 351 | * stack. If the number of arguments available is not the same |
| 352 | * then <nb> an error is throwed. |
| 353 | */ |
| 354 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 355 | { |
| 356 | if (lua_gettop(L) == nb) |
| 357 | return; |
| 358 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 359 | } |
| 360 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 361 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 362 | * and the line number where the error is encountered. |
| 363 | */ |
| 364 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 365 | { |
| 366 | va_list argp; |
| 367 | va_start(argp, fmt); |
| 368 | luaL_where(L, 1); |
| 369 | lua_pushvfstring(L, fmt, argp); |
| 370 | va_end(argp); |
| 371 | lua_concat(L, 2); |
| 372 | return 1; |
| 373 | } |
| 374 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 375 | /* This functions is used with sample fetch and converters. It |
| 376 | * converts the HAProxy configuration argument in a lua stack |
| 377 | * values. |
| 378 | * |
| 379 | * It takes an array of "arg", and each entry of the array is |
| 380 | * converted and pushed in the LUA stack. |
| 381 | */ |
| 382 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 383 | { |
| 384 | switch (arg->type) { |
| 385 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 386 | case ARGT_TIME: |
| 387 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 388 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 389 | break; |
| 390 | |
| 391 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 392 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 393 | break; |
| 394 | |
| 395 | case ARGT_IPV4: |
| 396 | case ARGT_IPV6: |
| 397 | case ARGT_MSK4: |
| 398 | case ARGT_MSK6: |
| 399 | case ARGT_FE: |
| 400 | case ARGT_BE: |
| 401 | case ARGT_TAB: |
| 402 | case ARGT_SRV: |
| 403 | case ARGT_USR: |
| 404 | case ARGT_MAP: |
| 405 | default: |
| 406 | lua_pushnil(L); |
| 407 | break; |
| 408 | } |
| 409 | return 1; |
| 410 | } |
| 411 | |
| 412 | /* This function take one entrie in an LUA stack at the index "ud", |
| 413 | * and try to convert it in an HAProxy argument entry. This is useful |
| 414 | * with sample fetch wrappers. The input arguments are gived to the |
| 415 | * lua wrapper and converted as arg list by thi function. |
| 416 | */ |
| 417 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 418 | { |
| 419 | switch (lua_type(L, ud)) { |
| 420 | |
| 421 | case LUA_TNUMBER: |
| 422 | case LUA_TBOOLEAN: |
| 423 | arg->type = ARGT_SINT; |
| 424 | arg->data.sint = lua_tointeger(L, ud); |
| 425 | break; |
| 426 | |
| 427 | case LUA_TSTRING: |
| 428 | arg->type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 429 | 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] | 430 | break; |
| 431 | |
| 432 | case LUA_TUSERDATA: |
| 433 | case LUA_TNIL: |
| 434 | case LUA_TTABLE: |
| 435 | case LUA_TFUNCTION: |
| 436 | case LUA_TTHREAD: |
| 437 | case LUA_TLIGHTUSERDATA: |
| 438 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 439 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 440 | break; |
| 441 | } |
| 442 | return 1; |
| 443 | } |
| 444 | |
| 445 | /* the following functions are used to convert a struct sample |
| 446 | * in Lua type. This useful to convert the return of the |
| 447 | * fetchs or converters. |
| 448 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 449 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 450 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 451 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 452 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 453 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 454 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 455 | break; |
| 456 | |
| 457 | case SMP_T_BIN: |
| 458 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 459 | 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] | 460 | break; |
| 461 | |
| 462 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 463 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 464 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 465 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 466 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 467 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 468 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 469 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 470 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 471 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 472 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 473 | 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] | 474 | break; |
| 475 | default: |
| 476 | lua_pushnil(L); |
| 477 | break; |
| 478 | } |
| 479 | break; |
| 480 | |
| 481 | case SMP_T_IPV4: |
| 482 | case SMP_T_IPV6: |
| 483 | 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] | 484 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 485 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 486 | 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] | 487 | else |
| 488 | lua_pushnil(L); |
| 489 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 490 | default: |
| 491 | lua_pushnil(L); |
| 492 | break; |
| 493 | } |
| 494 | return 1; |
| 495 | } |
| 496 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 497 | /* the following functions are used to convert a struct sample |
| 498 | * in Lua strings. This is useful to convert the return of the |
| 499 | * fetchs or converters. |
| 500 | */ |
| 501 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 502 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 503 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 504 | |
| 505 | case SMP_T_BIN: |
| 506 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 507 | 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] | 508 | break; |
| 509 | |
| 510 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 511 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 512 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 513 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 514 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 515 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 516 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 517 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 518 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 519 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 520 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 521 | 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] | 522 | break; |
| 523 | default: |
| 524 | lua_pushstring(L, ""); |
| 525 | break; |
| 526 | } |
| 527 | break; |
| 528 | |
| 529 | case SMP_T_SINT: |
| 530 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 531 | case SMP_T_IPV4: |
| 532 | case SMP_T_IPV6: |
| 533 | 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] | 534 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 535 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 536 | 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] | 537 | else |
| 538 | lua_pushstring(L, ""); |
| 539 | break; |
| 540 | default: |
| 541 | lua_pushstring(L, ""); |
| 542 | break; |
| 543 | } |
| 544 | return 1; |
| 545 | } |
| 546 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 547 | /* the following functions are used to convert an Lua type in a |
| 548 | * struct sample. This is useful to provide data from a converter |
| 549 | * to the LUA code. |
| 550 | */ |
| 551 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 552 | { |
| 553 | switch (lua_type(L, ud)) { |
| 554 | |
| 555 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 556 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 557 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 558 | break; |
| 559 | |
| 560 | |
| 561 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 562 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 563 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 564 | break; |
| 565 | |
| 566 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 567 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 568 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 569 | 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] | 570 | break; |
| 571 | |
| 572 | case LUA_TUSERDATA: |
| 573 | case LUA_TNIL: |
| 574 | case LUA_TTABLE: |
| 575 | case LUA_TFUNCTION: |
| 576 | case LUA_TTHREAD: |
| 577 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 578 | case LUA_TNONE: |
| 579 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 580 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 581 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 582 | break; |
| 583 | } |
| 584 | return 1; |
| 585 | } |
| 586 | |
| 587 | /* This function check the "argp" builded by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 588 | * is in accord with the expected argp defined by the "mask". The function |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 589 | * returns true or false. It can be adjust the types if there compatibles. |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 590 | * |
| 591 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 592 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 593 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 594 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 595 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 596 | { |
| 597 | int min_arg; |
| 598 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 599 | struct proxy *px; |
| 600 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 601 | |
| 602 | idx = 0; |
| 603 | min_arg = ARGM(mask); |
| 604 | mask >>= ARGM_BITS; |
| 605 | |
| 606 | while (1) { |
| 607 | |
| 608 | /* Check oversize. */ |
| 609 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 610 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /* Check for mandatory arguments. */ |
| 614 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 615 | if (idx < min_arg) { |
| 616 | |
| 617 | /* If miss other argument than the first one, we return an error. */ |
| 618 | if (idx > 0) |
| 619 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 620 | |
| 621 | /* If first argument have a certain type, some default values |
| 622 | * may be used. See the function smp_resolve_args(). |
| 623 | */ |
| 624 | switch (mask & ARGT_MASK) { |
| 625 | |
| 626 | case ARGT_FE: |
| 627 | if (!(p->cap & PR_CAP_FE)) |
| 628 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 629 | argp[idx].data.prx = p; |
| 630 | argp[idx].type = ARGT_FE; |
| 631 | argp[idx+1].type = ARGT_STOP; |
| 632 | break; |
| 633 | |
| 634 | case ARGT_BE: |
| 635 | if (!(p->cap & PR_CAP_BE)) |
| 636 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 637 | argp[idx].data.prx = p; |
| 638 | argp[idx].type = ARGT_BE; |
| 639 | argp[idx+1].type = ARGT_STOP; |
| 640 | break; |
| 641 | |
| 642 | case ARGT_TAB: |
| 643 | argp[idx].data.prx = p; |
| 644 | argp[idx].type = ARGT_TAB; |
| 645 | argp[idx+1].type = ARGT_STOP; |
| 646 | break; |
| 647 | |
| 648 | default: |
| 649 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 650 | break; |
| 651 | } |
| 652 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | /* Check for exceed the number of requiered argument. */ |
| 657 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 658 | argp[idx].type != ARGT_STOP) { |
| 659 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 660 | } |
| 661 | |
| 662 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 663 | argp[idx].type == ARGT_STOP) { |
| 664 | return 0; |
| 665 | } |
| 666 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 667 | /* Convert some argument types. */ |
| 668 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 669 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 670 | if (argp[idx].type != ARGT_SINT) |
| 671 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 672 | argp[idx].type = ARGT_SINT; |
| 673 | break; |
| 674 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 675 | case ARGT_TIME: |
| 676 | if (argp[idx].type != ARGT_SINT) |
| 677 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 678 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 679 | break; |
| 680 | |
| 681 | case ARGT_SIZE: |
| 682 | if (argp[idx].type != ARGT_SINT) |
| 683 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 684 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 685 | break; |
| 686 | |
| 687 | case ARGT_FE: |
| 688 | if (argp[idx].type != ARGT_STR) |
| 689 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 690 | memcpy(trash.area, argp[idx].data.str.area, |
| 691 | argp[idx].data.str.data); |
| 692 | trash.area[argp[idx].data.str.data] = 0; |
| 693 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 694 | if (!argp[idx].data.prx) |
| 695 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 696 | argp[idx].type = ARGT_FE; |
| 697 | break; |
| 698 | |
| 699 | case ARGT_BE: |
| 700 | if (argp[idx].type != ARGT_STR) |
| 701 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 702 | memcpy(trash.area, argp[idx].data.str.area, |
| 703 | argp[idx].data.str.data); |
| 704 | trash.area[argp[idx].data.str.data] = 0; |
| 705 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 706 | if (!argp[idx].data.prx) |
| 707 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 708 | argp[idx].type = ARGT_BE; |
| 709 | break; |
| 710 | |
| 711 | case ARGT_TAB: |
| 712 | if (argp[idx].type != ARGT_STR) |
| 713 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 714 | memcpy(trash.area, argp[idx].data.str.area, |
| 715 | argp[idx].data.str.data); |
| 716 | trash.area[argp[idx].data.str.data] = 0; |
| 717 | argp[idx].data.prx = proxy_tbl_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 718 | if (!argp[idx].data.prx) |
| 719 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 720 | argp[idx].type = ARGT_TAB; |
| 721 | break; |
| 722 | |
| 723 | case ARGT_SRV: |
| 724 | if (argp[idx].type != ARGT_STR) |
| 725 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 726 | memcpy(trash.area, argp[idx].data.str.area, |
| 727 | argp[idx].data.str.data); |
| 728 | trash.area[argp[idx].data.str.data] = 0; |
| 729 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 730 | if (sname) { |
| 731 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 732 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 733 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 734 | if (!px) |
| 735 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 736 | } |
| 737 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 738 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 739 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 740 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 741 | argp[idx].data.srv = findserver(px, sname); |
| 742 | if (!argp[idx].data.srv) |
| 743 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 744 | argp[idx].type = ARGT_SRV; |
| 745 | break; |
| 746 | |
| 747 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 748 | memcpy(trash.area, argp[idx].data.str.area, |
| 749 | argp[idx].data.str.data); |
| 750 | trash.area[argp[idx].data.str.data] = 0; |
| 751 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 752 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 753 | argp[idx].type = ARGT_IPV4; |
| 754 | break; |
| 755 | |
| 756 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 757 | memcpy(trash.area, argp[idx].data.str.area, |
| 758 | argp[idx].data.str.data); |
| 759 | trash.area[argp[idx].data.str.data] = 0; |
| 760 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 761 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 762 | argp[idx].type = ARGT_MSK4; |
| 763 | break; |
| 764 | |
| 765 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 766 | memcpy(trash.area, argp[idx].data.str.area, |
| 767 | argp[idx].data.str.data); |
| 768 | trash.area[argp[idx].data.str.data] = 0; |
| 769 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 770 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 771 | argp[idx].type = ARGT_IPV6; |
| 772 | break; |
| 773 | |
| 774 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 775 | memcpy(trash.area, argp[idx].data.str.area, |
| 776 | argp[idx].data.str.data); |
| 777 | trash.area[argp[idx].data.str.data] = 0; |
| 778 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 779 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 780 | argp[idx].type = ARGT_MSK6; |
| 781 | break; |
| 782 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 783 | case ARGT_MAP: |
| 784 | case ARGT_REG: |
| 785 | case ARGT_USR: |
| 786 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 787 | break; |
| 788 | } |
| 789 | |
| 790 | /* Check for type of argument. */ |
| 791 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 792 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 793 | arg_type_names[(mask & ARGT_MASK)], |
| 794 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 795 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 796 | } |
| 797 | |
| 798 | /* Next argument. */ |
| 799 | mask >>= ARGT_BITS; |
| 800 | idx++; |
| 801 | } |
| 802 | } |
| 803 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 804 | /* |
| 805 | * The following functions are used to make correspondance between the the |
| 806 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 807 | * |
| 808 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 809 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 810 | */ |
| 811 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 812 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 813 | struct hlua **hlua = lua_getextraspace(L); |
| 814 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 815 | } |
| 816 | static inline void hlua_sethlua(struct hlua *hlua) |
| 817 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 818 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 819 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 820 | } |
| 821 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 822 | /* This function is used to send logs. It try to send on screen (stderr) |
| 823 | * and on the default syslog server. |
| 824 | */ |
| 825 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 826 | { |
| 827 | struct tm tm; |
| 828 | char *p; |
| 829 | |
| 830 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 831 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 832 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 833 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 834 | /* Break the message if exceed the buffer size. */ |
| 835 | *(p-4) = ' '; |
| 836 | *(p-3) = '.'; |
| 837 | *(p-2) = '.'; |
| 838 | *(p-1) = '.'; |
| 839 | break; |
| 840 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 841 | if (isprint(*msg)) |
| 842 | *p = *msg; |
| 843 | else |
| 844 | *p = '.'; |
| 845 | } |
| 846 | *p = '\0'; |
| 847 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 848 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 849 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 850 | get_localtime(date.tv_sec, &tm); |
| 851 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 852 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 853 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 854 | fflush(stderr); |
| 855 | } |
| 856 | } |
| 857 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 858 | /* This function just ensure that the yield will be always |
| 859 | * returned with a timeout and permit to set some flags |
| 860 | */ |
| 861 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 862 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 863 | { |
| 864 | struct hlua *hlua = hlua_gethlua(L); |
| 865 | |
| 866 | /* Set the wake timeout. If timeout is required, we set |
| 867 | * the expiration time. |
| 868 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 869 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 870 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 871 | hlua->flags |= flags; |
| 872 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 873 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 874 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 875 | } |
| 876 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 877 | /* This function initialises the Lua environment stored in the stream. |
| 878 | * It must be called at the start of the stream. This function creates |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 879 | * an LUA coroutine. It can not be use to crete the main LUA context. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 880 | * |
| 881 | * This function is particular. it initialises a new Lua thread. If the |
| 882 | * initialisation fails (example: out of memory error), the lua function |
| 883 | * throws an error (longjmp). |
| 884 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 885 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 886 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 887 | * MUST NOT manipulate the created thread stack state, because it is not |
| 888 | * proctected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 889 | */ |
| 890 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 891 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 892 | if (!SET_SAFE_LJMP(gL.T)) { |
| 893 | lua->Tref = LUA_REFNIL; |
| 894 | return 0; |
| 895 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 896 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 897 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 898 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 899 | lua->T = lua_newthread(gL.T); |
| 900 | if (!lua->T) { |
| 901 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | 0a97620 | 2017-07-12 11:18:00 +0200 | [diff] [blame] | 902 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 903 | return 0; |
| 904 | } |
| 905 | hlua_sethlua(lua); |
| 906 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 907 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 908 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 909 | return 1; |
| 910 | } |
| 911 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 912 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 913 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 914 | * is not freed. |
| 915 | */ |
| 916 | void hlua_ctx_destroy(struct hlua *lua) |
| 917 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 918 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 919 | return; |
| 920 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 921 | if (!lua->T) |
| 922 | goto end; |
| 923 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 924 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 925 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 926 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 927 | if (!SET_SAFE_LJMP(lua->T)) |
| 928 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 929 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 930 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 931 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 932 | if (!SET_SAFE_LJMP(gL.T)) |
| 933 | return; |
| 934 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 935 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 936 | /* Forces a garbage collecting process. If the Lua program is finished |
| 937 | * without error, we run the GC on the thread pointer. Its freed all |
| 938 | * the unused memory. |
| 939 | * If the thread is finnish with an error or is currently yielded, |
| 940 | * it seems that the GC applied on the thread doesn't clean anything, |
| 941 | * so e run the GC on the main thread. |
| 942 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 943 | * the garbage collection. |
| 944 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 945 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 946 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 947 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 948 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 949 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 950 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 951 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 952 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 953 | |
| 954 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 955 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | /* This function is used to restore the Lua context when a coroutine |
| 959 | * fails. This function copy the common memory between old coroutine |
| 960 | * and the new coroutine. The old coroutine is destroyed, and its |
| 961 | * replaced by the new coroutine. |
| 962 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 963 | * as string error message and it is copied in the new stack. |
| 964 | */ |
| 965 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 966 | { |
| 967 | lua_State *T; |
| 968 | int new_ref; |
| 969 | |
| 970 | /* Renew the main LUA stack doesn't have sense. */ |
| 971 | if (lua == &gL) |
| 972 | return 0; |
| 973 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 974 | /* New Lua coroutine. */ |
| 975 | T = lua_newthread(gL.T); |
| 976 | if (!T) |
| 977 | return 0; |
| 978 | |
| 979 | /* Copy last error message. */ |
| 980 | if (keep_msg) |
| 981 | lua_xmove(lua->T, T, 1); |
| 982 | |
| 983 | /* Copy data between the coroutines. */ |
| 984 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 985 | lua_xmove(lua->T, T, 1); |
| 986 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 987 | |
| 988 | /* Destroy old data. */ |
| 989 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 990 | |
| 991 | /* The thread is garbage collected by Lua. */ |
| 992 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 993 | |
| 994 | /* Fill the struct with the new coroutine values. */ |
| 995 | lua->Mref = new_ref; |
| 996 | lua->T = T; |
| 997 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 998 | |
| 999 | /* Set context. */ |
| 1000 | hlua_sethlua(lua); |
| 1001 | |
| 1002 | return 1; |
| 1003 | } |
| 1004 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1005 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1006 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1007 | struct hlua *hlua = hlua_gethlua(L); |
| 1008 | |
| 1009 | /* Lua cannot yield when its returning from a function, |
| 1010 | * so, we can fix the interrupt hook to 1 instruction, |
| 1011 | * expecting that the function is finnished. |
| 1012 | */ |
| 1013 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1014 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1015 | return; |
| 1016 | } |
| 1017 | |
| 1018 | /* restore the interrupt condition. */ |
| 1019 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1020 | |
| 1021 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1022 | * If the state is not yieldable, trying yield causes an error. |
| 1023 | */ |
| 1024 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1025 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1026 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1027 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1028 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1029 | hlua->run_time += now_ms - hlua->start_time; |
| 1030 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1031 | lua_pushfstring(L, "execution timeout"); |
| 1032 | WILL_LJMP(lua_error(L)); |
| 1033 | } |
| 1034 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1035 | /* Update the start time. */ |
| 1036 | hlua->start_time = now_ms; |
| 1037 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1038 | /* Try to interrupt the process at the end of the current |
| 1039 | * unyieldable function. |
| 1040 | */ |
| 1041 | 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] | 1042 | } |
| 1043 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1044 | /* This function start or resumes the Lua stack execution. If the flag |
| 1045 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1046 | * The function return an error. |
| 1047 | * |
| 1048 | * The function can returns 4 values: |
| 1049 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1050 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1051 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1052 | * - 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] | 1053 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1054 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1055 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1056 | * 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] | 1057 | * LUA code. |
| 1058 | */ |
| 1059 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1060 | { |
| 1061 | int ret; |
| 1062 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1063 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1064 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1065 | /* Initialise run time counter. */ |
| 1066 | if (!HLUA_IS_RUNNING(lua)) |
| 1067 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1068 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1069 | /* Lock the whole Lua execution. This lock must be before the |
| 1070 | * label "resume_execution". |
| 1071 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1072 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1073 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1074 | resume_execution: |
| 1075 | |
| 1076 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1077 | * instructions. it is used for preventing infinite loops. |
| 1078 | */ |
| 1079 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1080 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1081 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1082 | HLUA_SET_RUN(lua); |
| 1083 | HLUA_CLR_CTRLYIELD(lua); |
| 1084 | HLUA_CLR_WAKERESWR(lua); |
| 1085 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1086 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1087 | /* Update the start time. */ |
| 1088 | lua->start_time = now_ms; |
| 1089 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1090 | /* Call the function. */ |
| 1091 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1092 | switch (ret) { |
| 1093 | |
| 1094 | case LUA_OK: |
| 1095 | ret = HLUA_E_OK; |
| 1096 | break; |
| 1097 | |
| 1098 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1099 | /* Check if the execution timeout is expired. It it is the case, we |
| 1100 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1101 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1102 | tv_update_date(0, 1); |
| 1103 | lua->run_time += now_ms - lua->start_time; |
| 1104 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1105 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1106 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1107 | break; |
| 1108 | } |
| 1109 | /* Process the forced yield. if the general yield is not allowed or |
| 1110 | * if no task were associated this the current Lua execution |
| 1111 | * coroutine, we resume the execution. Else we want to return in the |
| 1112 | * scheduler and we want to be waked up again, to continue the |
| 1113 | * current Lua execution. So we schedule our own task. |
| 1114 | */ |
| 1115 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1116 | if (!yield_allowed || !lua->task) |
| 1117 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1118 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1119 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1120 | if (!yield_allowed) { |
| 1121 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1122 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1123 | break; |
| 1124 | } |
| 1125 | ret = HLUA_E_AGAIN; |
| 1126 | break; |
| 1127 | |
| 1128 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1129 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1130 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1131 | * because the errors ares the only one mean to return immediately |
| 1132 | * from and lua execution. |
| 1133 | */ |
| 1134 | if (lua->flags & HLUA_EXIT) { |
| 1135 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1136 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1137 | break; |
| 1138 | } |
| 1139 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1140 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1141 | if (!lua_checkstack(lua->T, 1)) { |
| 1142 | ret = HLUA_E_ERR; |
| 1143 | break; |
| 1144 | } |
| 1145 | msg = lua_tostring(lua->T, -1); |
| 1146 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1147 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1148 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1149 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1150 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1151 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1152 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1153 | ret = HLUA_E_ERRMSG; |
| 1154 | break; |
| 1155 | |
| 1156 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1157 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1158 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1159 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1160 | break; |
| 1161 | |
| 1162 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1163 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1164 | if (!lua_checkstack(lua->T, 1)) { |
| 1165 | ret = HLUA_E_ERR; |
| 1166 | break; |
| 1167 | } |
| 1168 | msg = lua_tostring(lua->T, -1); |
| 1169 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1170 | lua_pop(lua->T, 1); |
| 1171 | if (msg) |
| 1172 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1173 | else |
| 1174 | lua_pushfstring(lua->T, "message handler error"); |
| 1175 | ret = HLUA_E_ERRMSG; |
| 1176 | break; |
| 1177 | |
| 1178 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1179 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1180 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1181 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1182 | break; |
| 1183 | } |
| 1184 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1185 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1186 | if (lua->flags & HLUA_MUST_GC && |
| 1187 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1188 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1189 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1190 | switch (ret) { |
| 1191 | case HLUA_E_AGAIN: |
| 1192 | break; |
| 1193 | |
| 1194 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1195 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1196 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1197 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1198 | break; |
| 1199 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1200 | case HLUA_E_ETMOUT: |
| 1201 | case HLUA_E_NOMEM: |
| 1202 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1203 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1204 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1205 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1206 | hlua_ctx_renew(lua, 0); |
| 1207 | break; |
| 1208 | |
| 1209 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1210 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1211 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1212 | break; |
| 1213 | } |
| 1214 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1215 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1216 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1217 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1218 | return ret; |
| 1219 | } |
| 1220 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1221 | /* This function exit the current code. */ |
| 1222 | __LJMP static int hlua_done(lua_State *L) |
| 1223 | { |
| 1224 | struct hlua *hlua = hlua_gethlua(L); |
| 1225 | |
| 1226 | hlua->flags |= HLUA_EXIT; |
| 1227 | WILL_LJMP(lua_error(L)); |
| 1228 | |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1232 | /* This function is an LUA binding. It provides a function |
| 1233 | * for deleting ACL from a referenced ACL file. |
| 1234 | */ |
| 1235 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1236 | { |
| 1237 | const char *name; |
| 1238 | const char *key; |
| 1239 | struct pat_ref *ref; |
| 1240 | |
| 1241 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1242 | |
| 1243 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1244 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1245 | |
| 1246 | ref = pat_ref_lookup(name); |
| 1247 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1248 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1249 | |
| 1250 | pat_ref_delete(ref, key); |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
| 1254 | /* This function is an LUA binding. It provides a function |
| 1255 | * for deleting map entry from a referenced map file. |
| 1256 | */ |
| 1257 | static int hlua_del_map(lua_State *L) |
| 1258 | { |
| 1259 | const char *name; |
| 1260 | const char *key; |
| 1261 | struct pat_ref *ref; |
| 1262 | |
| 1263 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1264 | |
| 1265 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1266 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1267 | |
| 1268 | ref = pat_ref_lookup(name); |
| 1269 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1270 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1271 | |
| 1272 | pat_ref_delete(ref, key); |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | /* This function is an LUA binding. It provides a function |
| 1277 | * for adding ACL pattern from a referenced ACL file. |
| 1278 | */ |
| 1279 | static int hlua_add_acl(lua_State *L) |
| 1280 | { |
| 1281 | const char *name; |
| 1282 | const char *key; |
| 1283 | struct pat_ref *ref; |
| 1284 | |
| 1285 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1286 | |
| 1287 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1288 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1289 | |
| 1290 | ref = pat_ref_lookup(name); |
| 1291 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1292 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1293 | |
| 1294 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1295 | pat_ref_add(ref, key, NULL, NULL); |
| 1296 | return 0; |
| 1297 | } |
| 1298 | |
| 1299 | /* This function is an LUA binding. It provides a function |
| 1300 | * for setting map pattern and sample from a referenced map |
| 1301 | * file. |
| 1302 | */ |
| 1303 | static int hlua_set_map(lua_State *L) |
| 1304 | { |
| 1305 | const char *name; |
| 1306 | const char *key; |
| 1307 | const char *value; |
| 1308 | struct pat_ref *ref; |
| 1309 | |
| 1310 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1311 | |
| 1312 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1313 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1314 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1315 | |
| 1316 | ref = pat_ref_lookup(name); |
| 1317 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1318 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1319 | |
| 1320 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1321 | pat_ref_set(ref, key, value, NULL); |
| 1322 | else |
| 1323 | pat_ref_add(ref, key, value, NULL); |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1327 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1328 | * an integer or user data. This data is associated with a metatable. This |
| 1329 | * metatable have an original version registred in the global context with |
| 1330 | * the name of the object (_G[<name>] = <metable> ). |
| 1331 | * |
| 1332 | * A metable is a table that modify the standard behavior of a standard |
| 1333 | * access to the associated data. The entries of this new metatable are |
| 1334 | * defined as is: |
| 1335 | * |
| 1336 | * http://lua-users.org/wiki/MetatableEvents |
| 1337 | * |
| 1338 | * __index |
| 1339 | * |
| 1340 | * we access an absent field in a table, the result is nil. This is |
| 1341 | * true, but it is not the whole truth. Actually, such access triggers |
| 1342 | * the interpreter to look for an __index metamethod: If there is no |
| 1343 | * such method, as usually happens, then the access results in nil; |
| 1344 | * otherwise, the metamethod will provide the result. |
| 1345 | * |
| 1346 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1347 | * the key does not appear in the table, but the metatable has an __index |
| 1348 | * property: |
| 1349 | * |
| 1350 | * - if the value is a function, the function is called, passing in the |
| 1351 | * table and the key; the return value of that function is returned as |
| 1352 | * the result. |
| 1353 | * |
| 1354 | * - if the value is another table, the value of the key in that table is |
| 1355 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1356 | * table's metatable has an __index property, then it continues on up) |
| 1357 | * |
| 1358 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1359 | * |
| 1360 | * http://www.lua.org/pil/13.4.1.html |
| 1361 | * |
| 1362 | * __newindex |
| 1363 | * |
| 1364 | * Like __index, but control property assignment. |
| 1365 | * |
| 1366 | * __mode - Control weak references. A string value with one or both |
| 1367 | * of the characters 'k' and 'v' which specifies that the the |
| 1368 | * keys and/or values in the table are weak references. |
| 1369 | * |
| 1370 | * __call - Treat a table like a function. When a table is followed by |
| 1371 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1372 | * a __call key pointing to a function, that function is invoked |
| 1373 | * (passing any specified arguments) and the return value is |
| 1374 | * returned. |
| 1375 | * |
| 1376 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1377 | * called, if the metatable for myTable has a __metatable |
| 1378 | * key, the value of that key is returned instead of the |
| 1379 | * actual metatable. |
| 1380 | * |
| 1381 | * __tostring - Control string representation. When the builtin |
| 1382 | * "tostring( myTable )" function is called, if the metatable |
| 1383 | * for myTable has a __tostring property set to a function, |
| 1384 | * that function is invoked (passing myTable to it) and the |
| 1385 | * return value is used as the string representation. |
| 1386 | * |
| 1387 | * __len - Control table length. When the table length is requested using |
| 1388 | * the length operator ( '#' ), if the metatable for myTable has |
| 1389 | * a __len key pointing to a function, that function is invoked |
| 1390 | * (passing myTable to it) and the return value used as the value |
| 1391 | * of "#myTable". |
| 1392 | * |
| 1393 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1394 | * collected, if the metatable has a __gc field pointing to a |
| 1395 | * function, that function is first invoked, passing the userdata |
| 1396 | * to it. The __gc metamethod is not called for tables. |
| 1397 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1398 | * |
| 1399 | * Special metamethods for redefining standard operators: |
| 1400 | * http://www.lua.org/pil/13.1.html |
| 1401 | * |
| 1402 | * __add "+" |
| 1403 | * __sub "-" |
| 1404 | * __mul "*" |
| 1405 | * __div "/" |
| 1406 | * __unm "!" |
| 1407 | * __pow "^" |
| 1408 | * __concat ".." |
| 1409 | * |
| 1410 | * Special methods for redfining standar relations |
| 1411 | * http://www.lua.org/pil/13.2.html |
| 1412 | * |
| 1413 | * __eq "==" |
| 1414 | * __lt "<" |
| 1415 | * __le "<=" |
| 1416 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1417 | |
| 1418 | /* |
| 1419 | * |
| 1420 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1421 | * Class Map |
| 1422 | * |
| 1423 | * |
| 1424 | */ |
| 1425 | |
| 1426 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1427 | * a class session, otherwise it throws an error. |
| 1428 | */ |
| 1429 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1430 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1431 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1432 | } |
| 1433 | |
| 1434 | /* This function is the map constructor. It don't need |
| 1435 | * the class Map object. It creates and return a new Map |
| 1436 | * object. It must be called only during "body" or "init" |
| 1437 | * context because it process some filesystem accesses. |
| 1438 | */ |
| 1439 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1440 | { |
| 1441 | const char *fn; |
| 1442 | int match = PAT_MATCH_STR; |
| 1443 | struct sample_conv conv; |
| 1444 | const char *file = ""; |
| 1445 | int line = 0; |
| 1446 | lua_Debug ar; |
| 1447 | char *err = NULL; |
| 1448 | struct arg args[2]; |
| 1449 | |
| 1450 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1451 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1452 | |
| 1453 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1454 | |
| 1455 | if (lua_gettop(L) >= 2) { |
| 1456 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1457 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1458 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1459 | } |
| 1460 | |
| 1461 | /* Get Lua filename and line number. */ |
| 1462 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1463 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1464 | if (ar.currentline > 0) { /* is there info? */ |
| 1465 | file = ar.short_src; |
| 1466 | line = ar.currentline; |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | /* fill fake sample_conv struct. */ |
| 1471 | conv.kw = ""; /* unused. */ |
| 1472 | conv.process = NULL; /* unused. */ |
| 1473 | conv.arg_mask = 0; /* unused. */ |
| 1474 | conv.val_args = NULL; /* unused. */ |
| 1475 | conv.out_type = SMP_T_STR; |
| 1476 | conv.private = (void *)(long)match; |
| 1477 | switch (match) { |
| 1478 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1479 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1480 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1481 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1482 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1483 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1484 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1485 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1486 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1487 | default: |
| 1488 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1489 | } |
| 1490 | |
| 1491 | /* fill fake args. */ |
| 1492 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1493 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1494 | args[1].type = ARGT_STOP; |
| 1495 | |
| 1496 | /* load the map. */ |
| 1497 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1498 | /* error case: we cant use luaL_error because we must |
| 1499 | * free the err variable. |
| 1500 | */ |
| 1501 | luaL_where(L, 1); |
| 1502 | lua_pushfstring(L, "'new': %s.", err); |
| 1503 | lua_concat(L, 2); |
| 1504 | free(err); |
| 1505 | WILL_LJMP(lua_error(L)); |
| 1506 | } |
| 1507 | |
| 1508 | /* create the lua object. */ |
| 1509 | lua_newtable(L); |
| 1510 | lua_pushlightuserdata(L, args[0].data.map); |
| 1511 | lua_rawseti(L, -2, 0); |
| 1512 | |
| 1513 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1514 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1515 | lua_setmetatable(L, -2); |
| 1516 | |
| 1517 | |
| 1518 | return 1; |
| 1519 | } |
| 1520 | |
| 1521 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1522 | { |
| 1523 | struct map_descriptor *desc; |
| 1524 | struct pattern *pat; |
| 1525 | struct sample smp; |
| 1526 | |
| 1527 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1528 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1529 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1530 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1531 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1532 | } |
| 1533 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1534 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1535 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1536 | 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] | 1537 | } |
| 1538 | |
| 1539 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1540 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1541 | if (str) |
| 1542 | lua_pushstring(L, ""); |
| 1543 | else |
| 1544 | lua_pushnil(L); |
| 1545 | return 1; |
| 1546 | } |
| 1547 | |
| 1548 | /* 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] | 1549 | 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] | 1550 | return 1; |
| 1551 | } |
| 1552 | |
| 1553 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1554 | { |
| 1555 | return _hlua_map_lookup(L, 0); |
| 1556 | } |
| 1557 | |
| 1558 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1559 | { |
| 1560 | return _hlua_map_lookup(L, 1); |
| 1561 | } |
| 1562 | |
| 1563 | /* |
| 1564 | * |
| 1565 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1566 | * Class Socket |
| 1567 | * |
| 1568 | * |
| 1569 | */ |
| 1570 | |
| 1571 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1572 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1573 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | /* 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] | 1577 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1578 | * received. |
| 1579 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1580 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1581 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1582 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1583 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1584 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1585 | if (appctx->ctx.hlua_cosocket.die) { |
| 1586 | si_shutw(si); |
| 1587 | si_shutr(si); |
| 1588 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1589 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1590 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1591 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1592 | } |
| 1593 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1594 | /* If the connection object is not available, close all the |
| 1595 | * streams and wakeup everything waiting for. |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1596 | */ |
| 1597 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1598 | si_shutw(si); |
| 1599 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1600 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1601 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1602 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1603 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1604 | } |
| 1605 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1606 | /* If we cant write, wakeup the pending write signals. */ |
| 1607 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1608 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1609 | |
| 1610 | /* If we cant read, wakeup the pending read signals. */ |
| 1611 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1612 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1613 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1614 | /* if the connection is not estabkished, inform the stream that we want |
| 1615 | * to be notified whenever the connection completes. |
| 1616 | */ |
| 1617 | if (!(c->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1618 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1619 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1620 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1621 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1622 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1623 | |
| 1624 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1625 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1626 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1627 | /* 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] | 1628 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1629 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1630 | |
| 1631 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1632 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1633 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1634 | |
| 1635 | /* Some data were injected in the buffer, notify the stream |
| 1636 | * interface. |
| 1637 | */ |
| 1638 | if (!channel_is_empty(si_ic(si))) |
| 1639 | stream_int_update(si); |
| 1640 | |
| 1641 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1642 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1643 | */ |
| 1644 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1645 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1646 | } |
| 1647 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1648 | /* This function is called when the "struct stream" is destroyed. |
| 1649 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1650 | * Wake all the pending signals. |
| 1651 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1652 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1653 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1654 | struct xref *peer; |
| 1655 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1656 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1657 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1658 | if (peer) |
| 1659 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1660 | |
| 1661 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1662 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1663 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1664 | } |
| 1665 | |
| 1666 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1667 | * uses this object. If the stream does not exists, just quit. |
| 1668 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1669 | * pending signal can rest in the read and write lists. destroy |
| 1670 | * it. |
| 1671 | */ |
| 1672 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1673 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1674 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1675 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1676 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1677 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1678 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1679 | |
| 1680 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1681 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1682 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1683 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1684 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1685 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1686 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1687 | appctx->ctx.hlua_cosocket.die = 1; |
| 1688 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1689 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1690 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1691 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1692 | return 0; |
| 1693 | } |
| 1694 | |
| 1695 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1696 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1697 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1698 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1699 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1700 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1701 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1702 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1703 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1704 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1705 | |
| 1706 | /* Check if we run on the same thread than the xreator thread. |
| 1707 | * We cannot access to the socket if the thread is different. |
| 1708 | */ |
| 1709 | if (socket->tid != tid) |
| 1710 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1711 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1712 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1713 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1714 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1715 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1716 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1717 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1718 | appctx->ctx.hlua_cosocket.die = 1; |
| 1719 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1720 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1721 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1722 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1723 | return 0; |
| 1724 | } |
| 1725 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1726 | /* The close function calls close_helper. |
| 1727 | */ |
| 1728 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1729 | { |
| 1730 | MAY_LJMP(check_args(L, 1, "close")); |
| 1731 | return hlua_socket_close_helper(L); |
| 1732 | } |
| 1733 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1734 | /* This Lua function assumes that the stack contain three parameters. |
| 1735 | * 1 - USERDATA containing a struct socket |
| 1736 | * 2 - INTEGER with values of the macro defined below |
| 1737 | * If the integer is -1, we must read at most one line. |
| 1738 | * If the integer is -2, we ust read all the data until the |
| 1739 | * end of the stream. |
| 1740 | * If the integer is positive value, we must read a number of |
| 1741 | * bytes corresponding to this value. |
| 1742 | */ |
| 1743 | #define HLSR_READ_LINE (-1) |
| 1744 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1745 | __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] | 1746 | { |
| 1747 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1748 | int wanted = lua_tointeger(L, 2); |
| 1749 | struct hlua *hlua = hlua_gethlua(L); |
| 1750 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1751 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1752 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1753 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1754 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1755 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1756 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1757 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1758 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1759 | struct stream_interface *si; |
| 1760 | struct stream *s; |
| 1761 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1762 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1763 | |
| 1764 | /* Check if this lua stack is schedulable. */ |
| 1765 | if (!hlua || !hlua->task) |
| 1766 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1767 | "'frontend', 'backend' or 'task'")); |
| 1768 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1769 | /* Check if we run on the same thread than the xreator thread. |
| 1770 | * We cannot access to the socket if the thread is different. |
| 1771 | */ |
| 1772 | if (socket->tid != tid) |
| 1773 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1774 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1775 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1776 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1777 | if (!peer) |
| 1778 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1779 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1780 | si = appctx->owner; |
| 1781 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1782 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1783 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1784 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1785 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1786 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1787 | if (nblk < 0) /* Connection close. */ |
| 1788 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1789 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1790 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1791 | |
| 1792 | /* remove final \r\n. */ |
| 1793 | if (nblk == 1) { |
| 1794 | if (blk1[len1-1] == '\n') { |
| 1795 | len1--; |
| 1796 | skip_at_end++; |
| 1797 | if (blk1[len1-1] == '\r') { |
| 1798 | len1--; |
| 1799 | skip_at_end++; |
| 1800 | } |
| 1801 | } |
| 1802 | } |
| 1803 | else { |
| 1804 | if (blk2[len2-1] == '\n') { |
| 1805 | len2--; |
| 1806 | skip_at_end++; |
| 1807 | if (blk2[len2-1] == '\r') { |
| 1808 | len2--; |
| 1809 | skip_at_end++; |
| 1810 | } |
| 1811 | } |
| 1812 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1816 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1817 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1818 | if (nblk < 0) /* Connection close. */ |
| 1819 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1820 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1821 | goto connection_empty; |
| 1822 | } |
| 1823 | |
| 1824 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1825 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1826 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1827 | if (nblk < 0) /* Connection close. */ |
| 1828 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1829 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1830 | goto connection_empty; |
| 1831 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1832 | missing_bytes = wanted - socket->b.n; |
| 1833 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1834 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1835 | len1 = missing_bytes; |
| 1836 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1837 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | len = len1; |
| 1841 | |
| 1842 | luaL_addlstring(&socket->b, blk1, len1); |
| 1843 | if (nblk == 2) { |
| 1844 | len += len2; |
| 1845 | luaL_addlstring(&socket->b, blk2, len2); |
| 1846 | } |
| 1847 | |
| 1848 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1849 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1850 | |
| 1851 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1852 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1853 | |
| 1854 | /* If the pattern reclaim to read all the data |
| 1855 | * in the connection, got out. |
| 1856 | */ |
| 1857 | if (wanted == HLSR_READ_ALL) |
| 1858 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1859 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1860 | goto connection_empty; |
| 1861 | |
| 1862 | /* Return result. */ |
| 1863 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1864 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1865 | return 1; |
| 1866 | |
| 1867 | connection_closed: |
| 1868 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1869 | xref_unlock(&socket->xref, peer); |
| 1870 | |
| 1871 | no_peer: |
| 1872 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1873 | /* If the buffer containds data. */ |
| 1874 | if (socket->b.n > 0) { |
| 1875 | luaL_pushresult(&socket->b); |
| 1876 | return 1; |
| 1877 | } |
| 1878 | lua_pushnil(L); |
| 1879 | lua_pushstring(L, "connection closed."); |
| 1880 | return 2; |
| 1881 | |
| 1882 | connection_empty: |
| 1883 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1884 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1885 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1886 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1887 | } |
| 1888 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1889 | 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] | 1890 | return 0; |
| 1891 | } |
| 1892 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1893 | /* This Lua function gets two parameters. The first one can be string |
| 1894 | * or a number. If the string is "*l", the user requires one line. If |
| 1895 | * 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] | 1896 | * If the value is a number, the user require a number of bytes equal |
| 1897 | * to the value. The default value is "*l" (a line). |
| 1898 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1899 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1900 | * integer takes this values: |
| 1901 | * -1 : read a line |
| 1902 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1903 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1904 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1905 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1906 | * concatenated with the read data. |
| 1907 | */ |
| 1908 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1909 | { |
| 1910 | int wanted = HLSR_READ_LINE; |
| 1911 | const char *pattern; |
| 1912 | int type; |
| 1913 | char *error; |
| 1914 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1915 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1916 | |
| 1917 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1918 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1919 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1920 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1921 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1922 | /* Check if we run on the same thread than the xreator thread. |
| 1923 | * We cannot access to the socket if the thread is different. |
| 1924 | */ |
| 1925 | if (socket->tid != tid) |
| 1926 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1927 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1928 | /* check for pattern. */ |
| 1929 | if (lua_gettop(L) >= 2) { |
| 1930 | type = lua_type(L, 2); |
| 1931 | if (type == LUA_TSTRING) { |
| 1932 | pattern = lua_tostring(L, 2); |
| 1933 | if (strcmp(pattern, "*a") == 0) |
| 1934 | wanted = HLSR_READ_ALL; |
| 1935 | else if (strcmp(pattern, "*l") == 0) |
| 1936 | wanted = HLSR_READ_LINE; |
| 1937 | else { |
| 1938 | wanted = strtoll(pattern, &error, 10); |
| 1939 | if (*error != '\0') |
| 1940 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1941 | } |
| 1942 | } |
| 1943 | else if (type == LUA_TNUMBER) { |
| 1944 | wanted = lua_tointeger(L, 2); |
| 1945 | if (wanted < 0) |
| 1946 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1947 | } |
| 1948 | } |
| 1949 | |
| 1950 | /* Set pattern. */ |
| 1951 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1952 | |
| 1953 | /* Check if we would replace the top by itself. */ |
| 1954 | if (lua_gettop(L) != 2) |
| 1955 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1956 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1957 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1958 | luaL_buffinit(L, &socket->b); |
| 1959 | |
| 1960 | /* Check prefix. */ |
| 1961 | if (lua_gettop(L) >= 3) { |
| 1962 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1963 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1964 | pattern = lua_tolstring(L, 3, &len); |
| 1965 | luaL_addlstring(&socket->b, pattern, len); |
| 1966 | } |
| 1967 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1968 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1969 | } |
| 1970 | |
| 1971 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1972 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1974 | 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] | 1975 | { |
| 1976 | struct hlua_socket *socket; |
| 1977 | struct hlua *hlua = hlua_gethlua(L); |
| 1978 | struct appctx *appctx; |
| 1979 | size_t buf_len; |
| 1980 | const char *buf; |
| 1981 | int len; |
| 1982 | int send_len; |
| 1983 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1984 | struct xref *peer; |
| 1985 | struct stream_interface *si; |
| 1986 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1987 | |
| 1988 | /* Check if this lua stack is schedulable. */ |
| 1989 | if (!hlua || !hlua->task) |
| 1990 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1991 | "'frontend', 'backend' or 'task'")); |
| 1992 | |
| 1993 | /* Get object */ |
| 1994 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1995 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1996 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1997 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1998 | /* Check if we run on the same thread than the xreator thread. |
| 1999 | * We cannot access to the socket if the thread is different. |
| 2000 | */ |
| 2001 | if (socket->tid != tid) |
| 2002 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2003 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2004 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2005 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2006 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2007 | lua_pushinteger(L, -1); |
| 2008 | return 1; |
| 2009 | } |
| 2010 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2011 | si = appctx->owner; |
| 2012 | s = si_strm(si); |
| 2013 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2014 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2015 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2016 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2017 | lua_pushinteger(L, -1); |
| 2018 | return 1; |
| 2019 | } |
| 2020 | |
| 2021 | /* Update the input buffer data. */ |
| 2022 | buf += sent; |
| 2023 | send_len = buf_len - sent; |
| 2024 | |
| 2025 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2026 | if (sent >= buf_len) { |
| 2027 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2028 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2029 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2030 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2031 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2032 | * the request buffer if its not required. |
| 2033 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2034 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2035 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2036 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2037 | } |
| 2038 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2039 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2040 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2041 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2042 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2043 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2044 | |
| 2045 | /* send data */ |
| 2046 | if (len < send_len) |
| 2047 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2048 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2049 | |
| 2050 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2051 | * the data" (-2) are not expected because the available length |
| 2052 | * is tested. |
| 2053 | * Other unknown error are also not expected. |
| 2054 | */ |
| 2055 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2056 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2057 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2058 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2059 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2060 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2061 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2062 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2063 | return 1; |
| 2064 | } |
| 2065 | |
| 2066 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2067 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2068 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2069 | s->req.rex = TICK_ETERNITY; |
| 2070 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2071 | |
| 2072 | /* Update length sent. */ |
| 2073 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2074 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2075 | |
| 2076 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2077 | if (sent + len >= buf_len) { |
| 2078 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2079 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2080 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2081 | |
| 2082 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2083 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2084 | xref_unlock(&socket->xref, peer); |
| 2085 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2086 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2087 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2088 | 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] | 2089 | return 0; |
| 2090 | } |
| 2091 | |
| 2092 | /* This function initiate the send of data. It just check the input |
| 2093 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2094 | * 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] | 2095 | * "hlua_socket_write_yield" that can yield. |
| 2096 | * |
| 2097 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2098 | * the associated object. The second is a string buffer. The third is |
| 2099 | * a facultative integer that represents where is the buffer position |
| 2100 | * of the start of the data that can send. The first byte is the |
| 2101 | * position "1". The default value is "1". The fourth argument is a |
| 2102 | * facultative integer that represents where is the buffer position |
| 2103 | * of the end of the data that can send. The default is the last byte. |
| 2104 | */ |
| 2105 | static int hlua_socket_send(struct lua_State *L) |
| 2106 | { |
| 2107 | int i; |
| 2108 | int j; |
| 2109 | const char *buf; |
| 2110 | size_t buf_len; |
| 2111 | |
| 2112 | /* Check number of arguments. */ |
| 2113 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2114 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2115 | |
| 2116 | /* Get the string. */ |
| 2117 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2118 | |
| 2119 | /* Get and check j. */ |
| 2120 | if (lua_gettop(L) == 4) { |
| 2121 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2122 | if (j < 0) |
| 2123 | j = buf_len + j + 1; |
| 2124 | if (j > buf_len) |
| 2125 | j = buf_len + 1; |
| 2126 | lua_pop(L, 1); |
| 2127 | } |
| 2128 | else |
| 2129 | j = buf_len; |
| 2130 | |
| 2131 | /* Get and check i. */ |
| 2132 | if (lua_gettop(L) == 3) { |
| 2133 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2134 | if (i < 0) |
| 2135 | i = buf_len + i + 1; |
| 2136 | if (i > buf_len) |
| 2137 | i = buf_len + 1; |
| 2138 | lua_pop(L, 1); |
| 2139 | } else |
| 2140 | i = 1; |
| 2141 | |
| 2142 | /* Check bth i and j. */ |
| 2143 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2144 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2145 | return 1; |
| 2146 | } |
| 2147 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2148 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2149 | return 1; |
| 2150 | } |
| 2151 | if (i == 0) |
| 2152 | i = 1; |
| 2153 | if (j == 0) |
| 2154 | j = 1; |
| 2155 | |
| 2156 | /* Pop the string. */ |
| 2157 | lua_pop(L, 1); |
| 2158 | |
| 2159 | /* Update the buffer length. */ |
| 2160 | buf += i - 1; |
| 2161 | buf_len = j - i + 1; |
| 2162 | lua_pushlstring(L, buf, buf_len); |
| 2163 | |
| 2164 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2165 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2166 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2167 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2168 | } |
| 2169 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2170 | #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] | 2171 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2172 | { |
| 2173 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2174 | int ret; |
| 2175 | int len; |
| 2176 | char *p; |
| 2177 | |
| 2178 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2179 | if (ret <= 0) { |
| 2180 | lua_pushnil(L); |
| 2181 | return 1; |
| 2182 | } |
| 2183 | |
| 2184 | if (ret == AF_UNIX) { |
| 2185 | lua_pushstring(L, buffer+1); |
| 2186 | return 1; |
| 2187 | } |
| 2188 | else if (ret == AF_INET6) { |
| 2189 | buffer[0] = '['; |
| 2190 | len = strlen(buffer); |
| 2191 | buffer[len] = ']'; |
| 2192 | len++; |
| 2193 | buffer[len] = ':'; |
| 2194 | len++; |
| 2195 | p = buffer; |
| 2196 | } |
| 2197 | else if (ret == AF_INET) { |
| 2198 | p = buffer + 1; |
| 2199 | len = strlen(p); |
| 2200 | p[len] = ':'; |
| 2201 | len++; |
| 2202 | } |
| 2203 | else { |
| 2204 | lua_pushnil(L); |
| 2205 | return 1; |
| 2206 | } |
| 2207 | |
| 2208 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2209 | lua_pushnil(L); |
| 2210 | return 1; |
| 2211 | } |
| 2212 | |
| 2213 | lua_pushstring(L, p); |
| 2214 | return 1; |
| 2215 | } |
| 2216 | |
| 2217 | /* Returns information about the peer of the connection. */ |
| 2218 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2219 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2220 | struct hlua_socket *socket; |
| 2221 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2222 | struct xref *peer; |
| 2223 | struct appctx *appctx; |
| 2224 | struct stream_interface *si; |
| 2225 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2226 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2227 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2228 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2229 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2230 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2231 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2232 | /* Check if we run on the same thread than the xreator thread. |
| 2233 | * We cannot access to the socket if the thread is different. |
| 2234 | */ |
| 2235 | if (socket->tid != tid) |
| 2236 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2237 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2238 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2239 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2240 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2241 | lua_pushnil(L); |
| 2242 | return 1; |
| 2243 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2244 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2245 | si = appctx->owner; |
| 2246 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2247 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2248 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2249 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2250 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2251 | lua_pushnil(L); |
| 2252 | return 1; |
| 2253 | } |
| 2254 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2255 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2256 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2257 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2258 | lua_pushnil(L); |
| 2259 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2260 | } |
| 2261 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2262 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2263 | xref_unlock(&socket->xref, peer); |
| 2264 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | /* Returns information about my connection side. */ |
| 2268 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2269 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2270 | struct hlua_socket *socket; |
| 2271 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2272 | struct appctx *appctx; |
| 2273 | struct xref *peer; |
| 2274 | struct stream_interface *si; |
| 2275 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2276 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2277 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2278 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2279 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2280 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2281 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2282 | /* Check if we run on the same thread than the xreator thread. |
| 2283 | * We cannot access to the socket if the thread is different. |
| 2284 | */ |
| 2285 | if (socket->tid != tid) |
| 2286 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2287 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2288 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2289 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2290 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2291 | lua_pushnil(L); |
| 2292 | return 1; |
| 2293 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2294 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2295 | si = appctx->owner; |
| 2296 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2298 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2299 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2300 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2301 | lua_pushnil(L); |
| 2302 | return 1; |
| 2303 | } |
| 2304 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2305 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2307 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2308 | lua_pushnil(L); |
| 2309 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2310 | } |
| 2311 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2312 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2313 | xref_unlock(&socket->xref, peer); |
| 2314 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2318 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2319 | .obj_type = OBJ_TYPE_APPLET, |
| 2320 | .name = "<LUA_TCP>", |
| 2321 | .fct = hlua_socket_handler, |
| 2322 | .release = hlua_socket_release, |
| 2323 | }; |
| 2324 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2325 | __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] | 2326 | { |
| 2327 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2328 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2329 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2330 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2331 | struct stream_interface *si; |
| 2332 | struct stream *s; |
| 2333 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2334 | /* Check if we run on the same thread than the xreator thread. |
| 2335 | * We cannot access to the socket if the thread is different. |
| 2336 | */ |
| 2337 | if (socket->tid != tid) |
| 2338 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2339 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2340 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2341 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2342 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2343 | lua_pushnil(L); |
| 2344 | lua_pushstring(L, "Can't connect"); |
| 2345 | return 2; |
| 2346 | } |
| 2347 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2348 | si = appctx->owner; |
| 2349 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2350 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2351 | /* Check if we run on the same thread than the xreator thread. |
| 2352 | * We cannot access to the socket if the thread is different. |
| 2353 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2354 | if (socket->tid != tid) { |
| 2355 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2356 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2357 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2358 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2359 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2360 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2361 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2362 | lua_pushnil(L); |
| 2363 | lua_pushstring(L, "Can't connect"); |
| 2364 | return 2; |
| 2365 | } |
| 2366 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2367 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2368 | |
| 2369 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2370 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2371 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2372 | lua_pushinteger(L, 1); |
| 2373 | return 1; |
| 2374 | } |
| 2375 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2376 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2377 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2378 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2379 | } |
| 2380 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2381 | 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] | 2382 | return 0; |
| 2383 | } |
| 2384 | |
| 2385 | /* This function fail or initite the connection. */ |
| 2386 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2387 | { |
| 2388 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2389 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2390 | const char *ip; |
| 2391 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2392 | struct hlua *hlua; |
| 2393 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2394 | int low, high; |
| 2395 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2396 | struct xref *peer; |
| 2397 | struct stream_interface *si; |
| 2398 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2399 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2400 | if (lua_gettop(L) < 2) |
| 2401 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2402 | |
| 2403 | /* Get args. */ |
| 2404 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2405 | |
| 2406 | /* Check if we run on the same thread than the xreator thread. |
| 2407 | * We cannot access to the socket if the thread is different. |
| 2408 | */ |
| 2409 | if (socket->tid != tid) |
| 2410 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2411 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2412 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2413 | if (lua_gettop(L) >= 3) { |
| 2414 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2415 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2416 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2417 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2418 | * that are not enclosed within square brackets. |
| 2419 | */ |
| 2420 | if (port > 0) { |
| 2421 | luaL_buffinit(L, &b); |
| 2422 | luaL_addstring(&b, ip); |
| 2423 | luaL_addchar(&b, ':'); |
| 2424 | luaL_pushresult(&b); |
| 2425 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2426 | } |
| 2427 | } |
| 2428 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2429 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2430 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2431 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2432 | lua_pushnil(L); |
| 2433 | return 1; |
| 2434 | } |
| 2435 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2436 | si = appctx->owner; |
| 2437 | s = si_strm(si); |
| 2438 | |
| 2439 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2440 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2441 | if (!conn) { |
| 2442 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2443 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2444 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2445 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2446 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2447 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2448 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2449 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2450 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2451 | if (!addr) { |
| 2452 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2453 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2454 | } |
| 2455 | if (low != high) { |
| 2456 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2457 | WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2458 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2459 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2460 | |
| 2461 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2462 | if (low == 0) { |
| 2463 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2464 | if (port == -1) { |
| 2465 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2466 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2467 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2468 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2469 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2470 | if (port == -1) { |
| 2471 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2472 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2473 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2474 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2475 | } |
| 2476 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2477 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2478 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2479 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2480 | |
| 2481 | /* inform the stream that we want to be notified whenever the |
| 2482 | * connection completes. |
| 2483 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2484 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2485 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2486 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2487 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2488 | hlua->flags |= HLUA_MUST_GC; |
| 2489 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2490 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2491 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2492 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2493 | } |
| 2494 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2495 | |
| 2496 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2497 | /* Return yield waiting for connection. */ |
| 2498 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2499 | 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] | 2500 | |
| 2501 | return 0; |
| 2502 | } |
| 2503 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2504 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2505 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2506 | { |
| 2507 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2508 | struct xref *peer; |
| 2509 | struct appctx *appctx; |
| 2510 | struct stream_interface *si; |
| 2511 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2512 | |
| 2513 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2514 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2515 | |
| 2516 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2517 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2518 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2519 | lua_pushnil(L); |
| 2520 | return 1; |
| 2521 | } |
| 2522 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2523 | si = appctx->owner; |
| 2524 | s = si_strm(si); |
| 2525 | |
| 2526 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2527 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2528 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2529 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2530 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2531 | |
| 2532 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2533 | { |
| 2534 | return 0; |
| 2535 | } |
| 2536 | |
| 2537 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2538 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2539 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2540 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2541 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2542 | struct xref *peer; |
| 2543 | struct appctx *appctx; |
| 2544 | struct stream_interface *si; |
| 2545 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2546 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2547 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2548 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2549 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2550 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2551 | /* convert the timeout to millis */ |
| 2552 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2553 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2554 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2555 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2556 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2557 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2558 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2559 | 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] | 2560 | |
| 2561 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2562 | if (tmout == 0) |
| 2563 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2564 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2565 | /* Check if we run on the same thread than the xreator thread. |
| 2566 | * We cannot access to the socket if the thread is different. |
| 2567 | */ |
| 2568 | if (socket->tid != tid) |
| 2569 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2570 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2571 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2572 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2573 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2574 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2575 | WILL_LJMP(lua_error(L)); |
| 2576 | return 0; |
| 2577 | } |
| 2578 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2579 | si = appctx->owner; |
| 2580 | s = si_strm(si); |
| 2581 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2582 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2583 | s->req.rto = tmout; |
| 2584 | s->req.wto = tmout; |
| 2585 | s->res.rto = tmout; |
| 2586 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2587 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2588 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2589 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2590 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2591 | |
| 2592 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2593 | task_queue(s->task); |
| 2594 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2595 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2596 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2597 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2598 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2602 | { |
| 2603 | struct hlua_socket *socket; |
| 2604 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2605 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2606 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2607 | |
| 2608 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2609 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2610 | hlua_pusherror(L, "socket: full stack"); |
| 2611 | goto out_fail_conf; |
| 2612 | } |
| 2613 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2614 | /* Create the object: obj[0] = userdata. */ |
| 2615 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2616 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2617 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2618 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2619 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2620 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2621 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2622 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2623 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2624 | goto out_fail_conf; |
| 2625 | } |
| 2626 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2627 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2628 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2629 | lua_setmetatable(L, -2); |
| 2630 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2631 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2632 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2633 | if (!appctx) { |
| 2634 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2635 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2636 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2637 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2638 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2639 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2640 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2641 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2642 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2643 | /* Now create a session, task and stream for this applet */ |
| 2644 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2645 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2646 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2647 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2648 | } |
| 2649 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2650 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2651 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2652 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2653 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2654 | } |
| 2655 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2656 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2657 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2658 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2659 | /* Configure "right" stream interface. this "si" is used to connect |
| 2660 | * and retrieve data from the server. The connection is initialized |
| 2661 | * with the "struct server". |
| 2662 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2663 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2664 | |
| 2665 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2666 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2667 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2668 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2669 | return 1; |
| 2670 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2671 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2672 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2673 | out_fail_sess: |
| 2674 | appctx_free(appctx); |
| 2675 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2676 | WILL_LJMP(lua_error(L)); |
| 2677 | return 0; |
| 2678 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2679 | |
| 2680 | /* |
| 2681 | * |
| 2682 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2683 | * Class Channel |
| 2684 | * |
| 2685 | * |
| 2686 | */ |
| 2687 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2688 | /* The state between the channel data and the HTTP parser state can be |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2689 | * inconsistent, so reset the parser and call it again. Warning, this |
| 2690 | * action does not revalidate the request and does not send a 400 if the modified |
| 2691 | * request is not valid. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2692 | * |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2693 | * This function never fails. The direction is set using dir, which equals |
| 2694 | * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2695 | */ |
| 2696 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2697 | { |
| 2698 | /* Protocol HTTP. */ |
| 2699 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2700 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2701 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2702 | http_txn_reset_req(stream->txn); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2703 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2704 | http_txn_reset_res(stream->txn); |
| 2705 | |
| 2706 | if (stream->txn->hdr_idx.v) |
| 2707 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2708 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2709 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2710 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2711 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2712 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2713 | } |
| 2714 | } |
| 2715 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2716 | /* This function is called before the Lua execution. It stores |
| 2717 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2718 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2719 | 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] | 2720 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2721 | c->mode = stream->be->mode; |
| 2722 | switch (c->mode) { |
| 2723 | case PR_MODE_HTTP: |
| 2724 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2725 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2726 | c->data.http.state = stream->txn->req.msg_state; |
| 2727 | else |
| 2728 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2729 | break; |
| 2730 | default: |
| 2731 | break; |
| 2732 | } |
| 2733 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2734 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2735 | /* This function is called after the Lua execution. it |
| 2736 | * returns true if the parser state is consistent, otherwise, |
| 2737 | * it return false. |
| 2738 | * |
| 2739 | * In HTTP mode, the parser state must be in the same state |
| 2740 | * or greater when we exit the function. Even if we do a |
| 2741 | * control yield. This prevent to break the HTTP message |
| 2742 | * from the Lua code. |
| 2743 | */ |
| 2744 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2745 | { |
| 2746 | if (c->mode != stream->be->mode) |
| 2747 | return 0; |
| 2748 | |
| 2749 | switch (c->mode) { |
| 2750 | case PR_MODE_HTTP: |
| 2751 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2752 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2753 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2754 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2755 | else |
| 2756 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2757 | default: |
| 2758 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2759 | } |
| 2760 | return 1; |
| 2761 | } |
| 2762 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2763 | /* Returns the struct hlua_channel join to the class channel in the |
| 2764 | * stack entry "ud" or throws an argument error. |
| 2765 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2766 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2767 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2768 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2769 | } |
| 2770 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2771 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2772 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2773 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2774 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2775 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2776 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2777 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2778 | return 0; |
| 2779 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2780 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2781 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2782 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2783 | |
| 2784 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2785 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2786 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2787 | return 1; |
| 2788 | } |
| 2789 | |
| 2790 | /* Duplicate all the data present in the input channel and put it |
| 2791 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2792 | * the stack if the channel is closed and all the data are consumed, |
| 2793 | * returns 0 if no data are available, otherwise it returns the length |
| 2794 | * of the builded string. |
| 2795 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2796 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2797 | { |
| 2798 | char *blk1; |
| 2799 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2800 | size_t len1; |
| 2801 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2802 | int ret; |
| 2803 | luaL_Buffer b; |
| 2804 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2805 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2806 | if (unlikely(ret == 0)) |
| 2807 | return 0; |
| 2808 | |
| 2809 | if (unlikely(ret < 0)) { |
| 2810 | lua_pushnil(L); |
| 2811 | return -1; |
| 2812 | } |
| 2813 | |
| 2814 | luaL_buffinit(L, &b); |
| 2815 | luaL_addlstring(&b, blk1, len1); |
| 2816 | if (unlikely(ret == 2)) |
| 2817 | luaL_addlstring(&b, blk2, len2); |
| 2818 | luaL_pushresult(&b); |
| 2819 | |
| 2820 | if (unlikely(ret == 2)) |
| 2821 | return len1 + len2; |
| 2822 | return len1; |
| 2823 | } |
| 2824 | |
| 2825 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2826 | * a yield. This function keep the data in the buffer. |
| 2827 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2828 | __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] | 2829 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2830 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2831 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2832 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2833 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2834 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2835 | 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] | 2836 | return 1; |
| 2837 | } |
| 2838 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2839 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2840 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2841 | { |
| 2842 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2843 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2844 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2845 | } |
| 2846 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2847 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2848 | * a yield. This function consumes the data in the buffer. It returns |
| 2849 | * a string containing the data or a nil pointer if no data are available |
| 2850 | * and the channel is closed. |
| 2851 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2852 | __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] | 2853 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2854 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2855 | int ret; |
| 2856 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2857 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2858 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2859 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2860 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2861 | 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] | 2862 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2863 | if (unlikely(ret == -1)) |
| 2864 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2865 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2866 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2867 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2868 | return 1; |
| 2869 | } |
| 2870 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2871 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2872 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2873 | { |
| 2874 | MAY_LJMP(check_args(L, 1, "get")); |
| 2875 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2876 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2877 | } |
| 2878 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2879 | /* This functions consumes and returns one line. If the channel is closed, |
| 2880 | * 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] | 2881 | * 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] | 2882 | * value. |
| 2883 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2884 | __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] | 2885 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2886 | char *blk1; |
| 2887 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2888 | size_t len1; |
| 2889 | size_t len2; |
| 2890 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2891 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2892 | int ret; |
| 2893 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2894 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2895 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2896 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2897 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2898 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2899 | 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] | 2900 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2901 | if (ret == -1) { |
| 2902 | lua_pushnil(L); |
| 2903 | return 1; |
| 2904 | } |
| 2905 | |
| 2906 | luaL_buffinit(L, &b); |
| 2907 | luaL_addlstring(&b, blk1, len1); |
| 2908 | len = len1; |
| 2909 | if (unlikely(ret == 2)) { |
| 2910 | luaL_addlstring(&b, blk2, len2); |
| 2911 | len += len2; |
| 2912 | } |
| 2913 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2914 | b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2915 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2916 | return 1; |
| 2917 | } |
| 2918 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2919 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2920 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2921 | { |
| 2922 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2923 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2924 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2925 | } |
| 2926 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2927 | /* This function takes a string as input, and append it at the |
| 2928 | * input side of channel. If the data is too big, but a space |
| 2929 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2930 | * yields. If the data is bigger than the buffer, or if the |
| 2931 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2932 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2933 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2934 | __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] | 2935 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2936 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2937 | size_t len; |
| 2938 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2939 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2940 | int ret; |
| 2941 | int max; |
| 2942 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2943 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2944 | * the request buffer if its not required. |
| 2945 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2946 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2947 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2948 | 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] | 2949 | } |
| 2950 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2951 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2952 | if (max > len - l) |
| 2953 | max = len - l; |
| 2954 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2955 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2956 | if (ret == -2 || ret == -3) { |
| 2957 | lua_pushinteger(L, -1); |
| 2958 | return 1; |
| 2959 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2960 | if (ret == -1) { |
| 2961 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2962 | 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] | 2963 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2964 | l += ret; |
| 2965 | lua_pop(L, 1); |
| 2966 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2967 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2968 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2969 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2970 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2971 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2972 | * in this case, we cannot add more data, so we cannot yield, |
| 2973 | * we return the amount of copyied data. |
| 2974 | */ |
| 2975 | return 1; |
| 2976 | } |
| 2977 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2978 | 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] | 2979 | return 1; |
| 2980 | } |
| 2981 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2982 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2983 | * 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] | 2984 | * buffer size is too little for the data. |
| 2985 | */ |
| 2986 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2987 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2988 | size_t len; |
| 2989 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2990 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2991 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2992 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2993 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2994 | lua_pushinteger(L, 0); |
| 2995 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2996 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2997 | } |
| 2998 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2999 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3000 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3001 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3002 | * or -1 if the channel is closed or if the buffer size is too |
| 3003 | * little for the data. |
| 3004 | */ |
| 3005 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3006 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3007 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3008 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3009 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3010 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3011 | lua_pushinteger(L, 0); |
| 3012 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3013 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3014 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3015 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3016 | } |
| 3017 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3018 | /* Append data in the output side of the buffer. This data is immediately |
| 3019 | * sent. The function returns the amount of data written. If the buffer |
| 3020 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3021 | * if the channel is closed. |
| 3022 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3023 | __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] | 3024 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3025 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3026 | size_t len; |
| 3027 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3028 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3029 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3030 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3031 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3032 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3033 | lua_pushinteger(L, -1); |
| 3034 | return 1; |
| 3035 | } |
| 3036 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3037 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3038 | * the request buffer if its not required. |
| 3039 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3040 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3041 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3042 | 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] | 3043 | } |
| 3044 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3045 | /* The written data will be immediately sent, so we can check |
| 3046 | * the available space without taking in account the reserve. |
| 3047 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3048 | * data, because the buffer will be flushed. |
| 3049 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3050 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3051 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3052 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3053 | * in this case, we cannot add more data, so we cannot yield, |
| 3054 | * we return the amount of copyied data. |
| 3055 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3056 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3057 | return 1; |
| 3058 | |
| 3059 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3060 | if (max > len - l) |
| 3061 | max = len - l; |
| 3062 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3063 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3064 | * detects a non contiguous buffer and realign it. |
| 3065 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3066 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3067 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3068 | |
| 3069 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3070 | 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] | 3071 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3072 | /* buffer replace considers that the input part is filled. |
| 3073 | * so, I must forward these new data in the output part. |
| 3074 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3075 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3076 | |
| 3077 | l += max; |
| 3078 | lua_pop(L, 1); |
| 3079 | lua_pushinteger(L, l); |
| 3080 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3081 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3082 | * in this case, we cannot add more data, so we cannot yield, |
| 3083 | * we return the amount of copyied data. |
| 3084 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3085 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3086 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3087 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3088 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3089 | if (l < len) { |
| 3090 | /* If we are waiting for space in the response buffer, we |
| 3091 | * must set the flag WAKERESWR. This flag required the task |
| 3092 | * wake up if any activity is detected on the response buffer. |
| 3093 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3094 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3095 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3096 | else |
| 3097 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3098 | 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] | 3099 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3100 | |
| 3101 | return 1; |
| 3102 | } |
| 3103 | |
| 3104 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3105 | * yield the LUA process, and resume it without checking the |
| 3106 | * input arguments. |
| 3107 | */ |
| 3108 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3109 | { |
| 3110 | MAY_LJMP(check_args(L, 2, "send")); |
| 3111 | lua_pushinteger(L, 0); |
| 3112 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3113 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3114 | } |
| 3115 | |
| 3116 | /* This function forward and amount of butes. The data pass from |
| 3117 | * the input side of the buffer to the output side, and can be |
| 3118 | * forwarded. This function never fails. |
| 3119 | * |
| 3120 | * The Lua function takes an amount of bytes to be forwarded in |
| 3121 | * imput. It returns the number of bytes forwarded. |
| 3122 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3123 | __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] | 3124 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3125 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3126 | int len; |
| 3127 | int l; |
| 3128 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3129 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3130 | |
| 3131 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3132 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3133 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3134 | |
| 3135 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3136 | if (max > ci_data(chn)) |
| 3137 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3138 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3139 | l += max; |
| 3140 | |
| 3141 | lua_pop(L, 1); |
| 3142 | lua_pushinteger(L, l); |
| 3143 | |
| 3144 | /* Check if it miss bytes to forward. */ |
| 3145 | if (l < len) { |
| 3146 | /* The the input channel or the output channel are closed, we |
| 3147 | * must return the amount of data forwarded. |
| 3148 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3149 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3150 | return 1; |
| 3151 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3152 | /* If we are waiting for space data in the response buffer, we |
| 3153 | * must set the flag WAKERESWR. This flag required the task |
| 3154 | * wake up if any activity is detected on the response buffer. |
| 3155 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3156 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3157 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3158 | else |
| 3159 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3160 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3161 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3162 | 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] | 3163 | } |
| 3164 | |
| 3165 | return 1; |
| 3166 | } |
| 3167 | |
| 3168 | /* Just check the input and prepare the stack for the previous |
| 3169 | * function "hlua_channel_forward_yield" |
| 3170 | */ |
| 3171 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3172 | { |
| 3173 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3174 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3175 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3176 | |
| 3177 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3178 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3179 | } |
| 3180 | |
| 3181 | /* Just returns the number of bytes available in the input |
| 3182 | * side of the buffer. This function never fails. |
| 3183 | */ |
| 3184 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3185 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3186 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3187 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3188 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3189 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3190 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3191 | return 1; |
| 3192 | } |
| 3193 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3194 | /* Returns true if the channel is full. */ |
| 3195 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3196 | { |
| 3197 | struct channel *chn; |
| 3198 | int rem; |
| 3199 | |
| 3200 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3201 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3202 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3203 | rem = b_room(&chn->buf); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3204 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3205 | |
| 3206 | lua_pushboolean(L, rem <= 0); |
| 3207 | return 1; |
| 3208 | } |
| 3209 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3210 | /* Just returns the number of bytes available in the output |
| 3211 | * side of the buffer. This function never fails. |
| 3212 | */ |
| 3213 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3214 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3215 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3216 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3217 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3218 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3219 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3220 | return 1; |
| 3221 | } |
| 3222 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3223 | /* |
| 3224 | * |
| 3225 | * |
| 3226 | * Class Fetches |
| 3227 | * |
| 3228 | * |
| 3229 | */ |
| 3230 | |
| 3231 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3232 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3233 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3234 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3235 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3236 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3237 | } |
| 3238 | |
| 3239 | /* This function creates and push in the stack a fetch object according |
| 3240 | * with a current TXN. |
| 3241 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3242 | 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] | 3243 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3244 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3245 | |
| 3246 | /* Check stack size. */ |
| 3247 | if (!lua_checkstack(L, 3)) |
| 3248 | return 0; |
| 3249 | |
| 3250 | /* Create the object: obj[0] = userdata. |
| 3251 | * Note that the base of the Fetches object is the |
| 3252 | * transaction object. |
| 3253 | */ |
| 3254 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3255 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3256 | lua_rawseti(L, -2, 0); |
| 3257 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3258 | hsmp->s = txn->s; |
| 3259 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3260 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3261 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3262 | |
| 3263 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3264 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3265 | lua_setmetatable(L, -2); |
| 3266 | |
| 3267 | return 1; |
| 3268 | } |
| 3269 | |
| 3270 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3271 | * It uses closure argument to store the associated sample-fetch. It |
| 3272 | * returns only one argument or throws an error. An error is thrown |
| 3273 | * only if an error is encountered during the argument parsing. If |
| 3274 | * the "sample-fetch" function fails, nil is returned. |
| 3275 | */ |
| 3276 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3277 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3278 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3279 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3280 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3281 | int i; |
| 3282 | struct sample smp; |
| 3283 | |
| 3284 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3285 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3286 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3287 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3288 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3289 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3290 | /* Check execution authorization. */ |
| 3291 | if (f->use & SMP_USE_HTTP_ANY && |
| 3292 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3293 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3294 | "is not available in Lua services", f->kw); |
| 3295 | WILL_LJMP(lua_error(L)); |
| 3296 | } |
| 3297 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3298 | /* Get extra arguments. */ |
| 3299 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3300 | if (i >= ARGM_NBARGS) |
| 3301 | break; |
| 3302 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3303 | } |
| 3304 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3305 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3306 | |
| 3307 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3308 | 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] | 3309 | |
| 3310 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3311 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3312 | lua_pushfstring(L, "error in arguments"); |
| 3313 | WILL_LJMP(lua_error(L)); |
| 3314 | } |
| 3315 | |
| 3316 | /* Initialise the sample. */ |
| 3317 | memset(&smp, 0, sizeof(smp)); |
| 3318 | |
| 3319 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3320 | 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] | 3321 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3322 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3323 | lua_pushstring(L, ""); |
| 3324 | else |
| 3325 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3326 | return 1; |
| 3327 | } |
| 3328 | |
| 3329 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3330 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3331 | hlua_smp2lua_str(L, &smp); |
| 3332 | else |
| 3333 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3334 | return 1; |
| 3335 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3336 | |
| 3337 | /* |
| 3338 | * |
| 3339 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3340 | * Class Converters |
| 3341 | * |
| 3342 | * |
| 3343 | */ |
| 3344 | |
| 3345 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3346 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3347 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3348 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3349 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3350 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3351 | } |
| 3352 | |
| 3353 | /* This function creates and push in the stack a Converters object |
| 3354 | * according with a current TXN. |
| 3355 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3356 | 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] | 3357 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3358 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3359 | |
| 3360 | /* Check stack size. */ |
| 3361 | if (!lua_checkstack(L, 3)) |
| 3362 | return 0; |
| 3363 | |
| 3364 | /* Create the object: obj[0] = userdata. |
| 3365 | * Note that the base of the Converters object is the |
| 3366 | * same than the TXN object. |
| 3367 | */ |
| 3368 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3369 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3370 | lua_rawseti(L, -2, 0); |
| 3371 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3372 | hsmp->s = txn->s; |
| 3373 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3374 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3375 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3376 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3377 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3378 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3379 | lua_setmetatable(L, -2); |
| 3380 | |
| 3381 | return 1; |
| 3382 | } |
| 3383 | |
| 3384 | /* This function is an LUA binding. It is called with each converter. |
| 3385 | * It uses closure argument to store the associated converter. It |
| 3386 | * returns only one argument or throws an error. An error is thrown |
| 3387 | * only if an error is encountered during the argument parsing. If |
| 3388 | * the converter function function fails, nil is returned. |
| 3389 | */ |
| 3390 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3391 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3392 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3393 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3394 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3395 | int i; |
| 3396 | struct sample smp; |
| 3397 | |
| 3398 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3399 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3400 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3401 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3402 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3403 | |
| 3404 | /* Get extra arguments. */ |
| 3405 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3406 | if (i >= ARGM_NBARGS) |
| 3407 | break; |
| 3408 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3409 | } |
| 3410 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3411 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3412 | |
| 3413 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3414 | 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] | 3415 | |
| 3416 | /* Run the special args checker. */ |
| 3417 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3418 | hlua_pusherror(L, "error in arguments"); |
| 3419 | WILL_LJMP(lua_error(L)); |
| 3420 | } |
| 3421 | |
| 3422 | /* Initialise the sample. */ |
| 3423 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3424 | hlua_pusherror(L, "error in the input argument"); |
| 3425 | WILL_LJMP(lua_error(L)); |
| 3426 | } |
| 3427 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3428 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3429 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3430 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3431 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3432 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3433 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3434 | WILL_LJMP(lua_error(L)); |
| 3435 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3436 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3437 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3438 | hlua_pusherror(L, "error during the input argument casting"); |
| 3439 | WILL_LJMP(lua_error(L)); |
| 3440 | } |
| 3441 | |
| 3442 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3443 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3444 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3445 | lua_pushstring(L, ""); |
| 3446 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3447 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3448 | return 1; |
| 3449 | } |
| 3450 | |
| 3451 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3452 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3453 | hlua_smp2lua_str(L, &smp); |
| 3454 | else |
| 3455 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3456 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3457 | } |
| 3458 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3459 | /* |
| 3460 | * |
| 3461 | * |
| 3462 | * Class AppletTCP |
| 3463 | * |
| 3464 | * |
| 3465 | */ |
| 3466 | |
| 3467 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3468 | * a class stream, otherwise it throws an error. |
| 3469 | */ |
| 3470 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3471 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3472 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3473 | } |
| 3474 | |
| 3475 | /* This function creates and push in the stack an Applet object |
| 3476 | * according with a current TXN. |
| 3477 | */ |
| 3478 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3479 | { |
| 3480 | struct hlua_appctx *appctx; |
| 3481 | struct stream_interface *si = ctx->owner; |
| 3482 | struct stream *s = si_strm(si); |
| 3483 | struct proxy *p = s->be; |
| 3484 | |
| 3485 | /* Check stack size. */ |
| 3486 | if (!lua_checkstack(L, 3)) |
| 3487 | return 0; |
| 3488 | |
| 3489 | /* Create the object: obj[0] = userdata. |
| 3490 | * Note that the base of the Converters object is the |
| 3491 | * same than the TXN object. |
| 3492 | */ |
| 3493 | lua_newtable(L); |
| 3494 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3495 | lua_rawseti(L, -2, 0); |
| 3496 | appctx->appctx = ctx; |
| 3497 | appctx->htxn.s = s; |
| 3498 | appctx->htxn.p = p; |
| 3499 | |
| 3500 | /* Create the "f" field that contains a list of fetches. */ |
| 3501 | lua_pushstring(L, "f"); |
| 3502 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3503 | return 0; |
| 3504 | lua_settable(L, -3); |
| 3505 | |
| 3506 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3507 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3508 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3509 | return 0; |
| 3510 | lua_settable(L, -3); |
| 3511 | |
| 3512 | /* Create the "c" field that contains a list of converters. */ |
| 3513 | lua_pushstring(L, "c"); |
| 3514 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3515 | return 0; |
| 3516 | lua_settable(L, -3); |
| 3517 | |
| 3518 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3519 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3520 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3521 | return 0; |
| 3522 | lua_settable(L, -3); |
| 3523 | |
| 3524 | /* Pop a class stream metatable and affect it to the table. */ |
| 3525 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3526 | lua_setmetatable(L, -2); |
| 3527 | |
| 3528 | return 1; |
| 3529 | } |
| 3530 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3531 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3532 | { |
| 3533 | struct hlua_appctx *appctx; |
| 3534 | struct stream *s; |
| 3535 | const char *name; |
| 3536 | size_t len; |
| 3537 | struct sample smp; |
| 3538 | |
| 3539 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3540 | |
| 3541 | /* It is useles to retrieve the stream, but this function |
| 3542 | * runs only in a stream context. |
| 3543 | */ |
| 3544 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3545 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3546 | s = appctx->htxn.s; |
| 3547 | |
| 3548 | /* Converts the third argument in a sample. */ |
| 3549 | hlua_lua2smp(L, 3, &smp); |
| 3550 | |
| 3551 | /* Store the sample in a variable. */ |
| 3552 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3553 | vars_set_by_name(name, len, &smp); |
| 3554 | return 0; |
| 3555 | } |
| 3556 | |
| 3557 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3558 | { |
| 3559 | struct hlua_appctx *appctx; |
| 3560 | struct stream *s; |
| 3561 | const char *name; |
| 3562 | size_t len; |
| 3563 | struct sample smp; |
| 3564 | |
| 3565 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3566 | |
| 3567 | /* It is useles to retrieve the stream, but this function |
| 3568 | * runs only in a stream context. |
| 3569 | */ |
| 3570 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3571 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3572 | s = appctx->htxn.s; |
| 3573 | |
| 3574 | /* Unset the variable. */ |
| 3575 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3576 | vars_unset_by_name(name, len, &smp); |
| 3577 | return 0; |
| 3578 | } |
| 3579 | |
| 3580 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3581 | { |
| 3582 | struct hlua_appctx *appctx; |
| 3583 | struct stream *s; |
| 3584 | const char *name; |
| 3585 | size_t len; |
| 3586 | struct sample smp; |
| 3587 | |
| 3588 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3589 | |
| 3590 | /* It is useles to retrieve the stream, but this function |
| 3591 | * runs only in a stream context. |
| 3592 | */ |
| 3593 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3594 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3595 | s = appctx->htxn.s; |
| 3596 | |
| 3597 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3598 | if (!vars_get_by_name(name, len, &smp)) { |
| 3599 | lua_pushnil(L); |
| 3600 | return 1; |
| 3601 | } |
| 3602 | |
| 3603 | return hlua_smp2lua(L, &smp); |
| 3604 | } |
| 3605 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3606 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3607 | { |
| 3608 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3609 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3610 | struct hlua *hlua; |
| 3611 | |
| 3612 | /* 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] | 3613 | if (!s->hlua) |
| 3614 | return 0; |
| 3615 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3616 | |
| 3617 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3618 | |
| 3619 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3620 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3621 | |
| 3622 | /* Get and store new value. */ |
| 3623 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3624 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3625 | |
| 3626 | return 0; |
| 3627 | } |
| 3628 | |
| 3629 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3630 | { |
| 3631 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3632 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3633 | struct hlua *hlua; |
| 3634 | |
| 3635 | /* 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] | 3636 | if (!s->hlua) { |
| 3637 | lua_pushnil(L); |
| 3638 | return 1; |
| 3639 | } |
| 3640 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3641 | |
| 3642 | /* Push configuration index in the stack. */ |
| 3643 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3644 | |
| 3645 | return 1; |
| 3646 | } |
| 3647 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3648 | /* If expected data not yet available, it returns a yield. This function |
| 3649 | * consumes the data in the buffer. It returns a string containing the |
| 3650 | * data. This string can be empty. |
| 3651 | */ |
| 3652 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3653 | { |
| 3654 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3655 | struct stream_interface *si = appctx->appctx->owner; |
| 3656 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3657 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3658 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3659 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3660 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3661 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3662 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3663 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3664 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3665 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3666 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3667 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3668 | 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] | 3669 | } |
| 3670 | |
| 3671 | /* End of data: commit the total strings and return. */ |
| 3672 | if (ret < 0) { |
| 3673 | luaL_pushresult(&appctx->b); |
| 3674 | return 1; |
| 3675 | } |
| 3676 | |
| 3677 | /* Ensure that the block 2 length is usable. */ |
| 3678 | if (ret == 1) |
| 3679 | len2 = 0; |
| 3680 | |
| 3681 | /* dont check the max length read and dont check. */ |
| 3682 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3683 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3684 | |
| 3685 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3686 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3687 | luaL_pushresult(&appctx->b); |
| 3688 | return 1; |
| 3689 | } |
| 3690 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3691 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3692 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3693 | { |
| 3694 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3695 | |
| 3696 | /* Initialise the string catenation. */ |
| 3697 | luaL_buffinit(L, &appctx->b); |
| 3698 | |
| 3699 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3700 | } |
| 3701 | |
| 3702 | /* If expected data not yet available, it returns a yield. This function |
| 3703 | * consumes the data in the buffer. It returns a string containing the |
| 3704 | * data. This string can be empty. |
| 3705 | */ |
| 3706 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3707 | { |
| 3708 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3709 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3710 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3711 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3712 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3713 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3714 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3715 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3716 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3717 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3718 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3719 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3720 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3721 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3722 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3723 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3724 | } |
| 3725 | |
| 3726 | /* End of data: commit the total strings and return. */ |
| 3727 | if (ret < 0) { |
| 3728 | luaL_pushresult(&appctx->b); |
| 3729 | return 1; |
| 3730 | } |
| 3731 | |
| 3732 | /* Ensure that the block 2 length is usable. */ |
| 3733 | if (ret == 1) |
| 3734 | len2 = 0; |
| 3735 | |
| 3736 | if (len == -1) { |
| 3737 | |
| 3738 | /* If len == -1, catenate all the data avalaile and |
| 3739 | * yield because we want to get all the data until |
| 3740 | * the end of data stream. |
| 3741 | */ |
| 3742 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3743 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3744 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3745 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3746 | 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] | 3747 | |
| 3748 | } else { |
| 3749 | |
| 3750 | /* Copy the fisrt block caping to the length required. */ |
| 3751 | if (len1 > len) |
| 3752 | len1 = len; |
| 3753 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3754 | len -= len1; |
| 3755 | |
| 3756 | /* Copy the second block. */ |
| 3757 | if (len2 > len) |
| 3758 | len2 = len; |
| 3759 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3760 | len -= len2; |
| 3761 | |
| 3762 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3763 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3764 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3765 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3766 | if (len > 0) { |
| 3767 | lua_pushinteger(L, len); |
| 3768 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3769 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3770 | 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] | 3771 | } |
| 3772 | |
| 3773 | /* return the result. */ |
| 3774 | luaL_pushresult(&appctx->b); |
| 3775 | return 1; |
| 3776 | } |
| 3777 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3778 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3779 | hlua_pusherror(L, "Lua: internal error"); |
| 3780 | WILL_LJMP(lua_error(L)); |
| 3781 | return 0; |
| 3782 | } |
| 3783 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3784 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3785 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3786 | { |
| 3787 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3788 | int len = -1; |
| 3789 | |
| 3790 | if (lua_gettop(L) > 2) |
| 3791 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3792 | if (lua_gettop(L) >= 2) { |
| 3793 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3794 | lua_pop(L, 1); |
| 3795 | } |
| 3796 | |
| 3797 | /* Confirm or set the required length */ |
| 3798 | lua_pushinteger(L, len); |
| 3799 | |
| 3800 | /* Initialise the string catenation. */ |
| 3801 | luaL_buffinit(L, &appctx->b); |
| 3802 | |
| 3803 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3804 | } |
| 3805 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3806 | /* Append data in the output side of the buffer. This data is immediately |
| 3807 | * sent. The function returns the amount of data written. If the buffer |
| 3808 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3809 | * if the channel is closed. |
| 3810 | */ |
| 3811 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3812 | { |
| 3813 | size_t len; |
| 3814 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3815 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3816 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3817 | struct stream_interface *si = appctx->appctx->owner; |
| 3818 | struct channel *chn = si_ic(si); |
| 3819 | int max; |
| 3820 | |
| 3821 | /* Get the max amount of data which can write as input in the channel. */ |
| 3822 | max = channel_recv_max(chn); |
| 3823 | if (max > (len - l)) |
| 3824 | max = len - l; |
| 3825 | |
| 3826 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3827 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3828 | |
| 3829 | /* update counters. */ |
| 3830 | l += max; |
| 3831 | lua_pop(L, 1); |
| 3832 | lua_pushinteger(L, l); |
| 3833 | |
| 3834 | /* If some data is not send, declares the situation to the |
| 3835 | * applet, and returns a yield. |
| 3836 | */ |
| 3837 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3838 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3839 | 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] | 3840 | } |
| 3841 | |
| 3842 | return 1; |
| 3843 | } |
| 3844 | |
| 3845 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3846 | * yield the LUA process, and resume it without checking the |
| 3847 | * input arguments. |
| 3848 | */ |
| 3849 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3850 | { |
| 3851 | MAY_LJMP(check_args(L, 2, "send")); |
| 3852 | lua_pushinteger(L, 0); |
| 3853 | |
| 3854 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3855 | } |
| 3856 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3857 | /* |
| 3858 | * |
| 3859 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3860 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3861 | * |
| 3862 | * |
| 3863 | */ |
| 3864 | |
| 3865 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3866 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3867 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3868 | __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] | 3869 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3870 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3871 | } |
| 3872 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3873 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3874 | * according with a current TXN. |
| 3875 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3876 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3877 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3878 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3879 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3880 | struct stream_interface *si = ctx->owner; |
| 3881 | struct stream *s = si_strm(si); |
| 3882 | struct proxy *px = s->be; |
| 3883 | struct http_txn *txn = s->txn; |
| 3884 | const char *path; |
| 3885 | const char *end; |
| 3886 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3887 | |
| 3888 | /* Check stack size. */ |
| 3889 | if (!lua_checkstack(L, 3)) |
| 3890 | return 0; |
| 3891 | |
| 3892 | /* Create the object: obj[0] = userdata. |
| 3893 | * Note that the base of the Converters object is the |
| 3894 | * same than the TXN object. |
| 3895 | */ |
| 3896 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3897 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3898 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3899 | appctx->appctx = ctx; |
| 3900 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3901 | 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] | 3902 | appctx->htxn.s = s; |
| 3903 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3904 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3905 | /* Create the "f" field that contains a list of fetches. */ |
| 3906 | lua_pushstring(L, "f"); |
| 3907 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3908 | return 0; |
| 3909 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3910 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3911 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3912 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3913 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3914 | return 0; |
| 3915 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3916 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3917 | /* Create the "c" field that contains a list of converters. */ |
| 3918 | lua_pushstring(L, "c"); |
| 3919 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3920 | return 0; |
| 3921 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3922 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3923 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3924 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3925 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3926 | return 0; |
| 3927 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3928 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3929 | /* Stores the request method. */ |
| 3930 | lua_pushstring(L, "method"); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3931 | 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] | 3932 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3933 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3934 | /* Stores the http version. */ |
| 3935 | lua_pushstring(L, "version"); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3936 | 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] | 3937 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3938 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3939 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3940 | * the array on the top of the stack. |
| 3941 | */ |
| 3942 | lua_pushstring(L, "headers"); |
| 3943 | htxn.s = s; |
| 3944 | htxn.p = px; |
| 3945 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3946 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3947 | return 0; |
| 3948 | lua_settable(L, -3); |
| 3949 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3950 | /* Get path and qs */ |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 3951 | path = http_txn_get_path(txn); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3952 | if (path) { |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3953 | 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] | 3954 | p = path; |
| 3955 | while (p < end && *p != '?') |
| 3956 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3957 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3958 | /* Stores the request path. */ |
| 3959 | lua_pushstring(L, "path"); |
| 3960 | lua_pushlstring(L, path, p - path); |
| 3961 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3962 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3963 | /* Stores the query string. */ |
| 3964 | lua_pushstring(L, "qs"); |
| 3965 | if (*p == '?') |
| 3966 | p++; |
| 3967 | lua_pushlstring(L, p, end - p); |
| 3968 | lua_settable(L, -3); |
| 3969 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3970 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3971 | /* Stores the request path. */ |
| 3972 | lua_pushstring(L, "length"); |
| 3973 | lua_pushinteger(L, txn->req.body_len); |
| 3974 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3975 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3976 | /* Create an array of HTTP request headers. */ |
| 3977 | lua_pushstring(L, "headers"); |
| 3978 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3979 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3980 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3981 | /* Create an empty array of HTTP request headers. */ |
| 3982 | lua_pushstring(L, "response"); |
| 3983 | lua_newtable(L); |
| 3984 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3985 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3986 | /* Pop a class stream metatable and affect it to the table. */ |
| 3987 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3988 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3989 | |
| 3990 | return 1; |
| 3991 | } |
| 3992 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3993 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3994 | { |
| 3995 | struct hlua_appctx *appctx; |
| 3996 | struct stream *s; |
| 3997 | const char *name; |
| 3998 | size_t len; |
| 3999 | struct sample smp; |
| 4000 | |
| 4001 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4002 | |
| 4003 | /* It is useles to retrieve the stream, but this function |
| 4004 | * runs only in a stream context. |
| 4005 | */ |
| 4006 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4007 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4008 | s = appctx->htxn.s; |
| 4009 | |
| 4010 | /* Converts the third argument in a sample. */ |
| 4011 | hlua_lua2smp(L, 3, &smp); |
| 4012 | |
| 4013 | /* Store the sample in a variable. */ |
| 4014 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4015 | vars_set_by_name(name, len, &smp); |
| 4016 | return 0; |
| 4017 | } |
| 4018 | |
| 4019 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4020 | { |
| 4021 | struct hlua_appctx *appctx; |
| 4022 | struct stream *s; |
| 4023 | const char *name; |
| 4024 | size_t len; |
| 4025 | struct sample smp; |
| 4026 | |
| 4027 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4028 | |
| 4029 | /* It is useles to retrieve the stream, but this function |
| 4030 | * runs only in a stream context. |
| 4031 | */ |
| 4032 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4033 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4034 | s = appctx->htxn.s; |
| 4035 | |
| 4036 | /* Unset the variable. */ |
| 4037 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4038 | vars_unset_by_name(name, len, &smp); |
| 4039 | return 0; |
| 4040 | } |
| 4041 | |
| 4042 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4043 | { |
| 4044 | struct hlua_appctx *appctx; |
| 4045 | struct stream *s; |
| 4046 | const char *name; |
| 4047 | size_t len; |
| 4048 | struct sample smp; |
| 4049 | |
| 4050 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4051 | |
| 4052 | /* It is useles to retrieve the stream, but this function |
| 4053 | * runs only in a stream context. |
| 4054 | */ |
| 4055 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4056 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4057 | s = appctx->htxn.s; |
| 4058 | |
| 4059 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4060 | if (!vars_get_by_name(name, len, &smp)) { |
| 4061 | lua_pushnil(L); |
| 4062 | return 1; |
| 4063 | } |
| 4064 | |
| 4065 | return hlua_smp2lua(L, &smp); |
| 4066 | } |
| 4067 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4068 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4069 | { |
| 4070 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4071 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4072 | struct hlua *hlua; |
| 4073 | |
| 4074 | /* 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] | 4075 | if (!s->hlua) |
| 4076 | return 0; |
| 4077 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4078 | |
| 4079 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4080 | |
| 4081 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4082 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4083 | |
| 4084 | /* Get and store new value. */ |
| 4085 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4086 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4087 | |
| 4088 | return 0; |
| 4089 | } |
| 4090 | |
| 4091 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4092 | { |
| 4093 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4094 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4095 | struct hlua *hlua; |
| 4096 | |
| 4097 | /* 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] | 4098 | if (!s->hlua) { |
| 4099 | lua_pushnil(L); |
| 4100 | return 1; |
| 4101 | } |
| 4102 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4103 | |
| 4104 | /* Push configuration index in the stack. */ |
| 4105 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4106 | |
| 4107 | return 1; |
| 4108 | } |
| 4109 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4110 | /* If expected data not yet available, it returns a yield. This function |
| 4111 | * consumes the data in the buffer. It returns a string containing the |
| 4112 | * data. This string can be empty. |
| 4113 | */ |
| 4114 | __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] | 4115 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4116 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4117 | struct stream_interface *si = appctx->appctx->owner; |
| 4118 | struct channel *chn = si_ic(si); |
| 4119 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4120 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4121 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4122 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4123 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4124 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4125 | /* Maybe we cant send a 100-continue ? */ |
| 4126 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4127 | ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4128 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4129 | * big for the buffers. We cant send anything. So, we ignoring |
| 4130 | * the error, considers that the 100-continue is sent, and try |
| 4131 | * to receive. |
| 4132 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4133 | */ |
| 4134 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4135 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4136 | 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] | 4137 | } |
| 4138 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4139 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4140 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4141 | /* Check for the end of the data. */ |
| 4142 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4143 | luaL_pushresult(&appctx->b); |
| 4144 | return 1; |
| 4145 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4146 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4147 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4148 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4149 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4150 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4151 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4152 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4153 | 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] | 4154 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4155 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4156 | /* End of data: commit the total strings and return. */ |
| 4157 | if (ret < 0) { |
| 4158 | luaL_pushresult(&appctx->b); |
| 4159 | return 1; |
| 4160 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4161 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4162 | /* Ensure that the block 2 length is usable. */ |
| 4163 | if (ret == 1) |
| 4164 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4165 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4166 | /* Copy the fisrt block caping to the length required. */ |
| 4167 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4168 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4169 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4170 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4171 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4172 | /* Copy the second block. */ |
| 4173 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4174 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4175 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4176 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4177 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4178 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4179 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4180 | luaL_pushresult(&appctx->b); |
| 4181 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4182 | } |
| 4183 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4184 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4185 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4186 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4187 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4188 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4189 | /* Initialise the string catenation. */ |
| 4190 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4191 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4192 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4193 | } |
| 4194 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4195 | /* If expected data not yet available, it returns a yield. This function |
| 4196 | * consumes the data in the buffer. It returns a string containing the |
| 4197 | * data. This string can be empty. |
| 4198 | */ |
| 4199 | __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] | 4200 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4201 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4202 | struct stream_interface *si = appctx->appctx->owner; |
| 4203 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4204 | struct channel *chn = si_ic(si); |
| 4205 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4206 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4207 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4208 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4209 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4210 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4211 | /* Maybe we cant send a 100-continue ? */ |
| 4212 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4213 | ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4214 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4215 | * big for the buffers. We cant send anything. So, we ignoring |
| 4216 | * the error, considers that the 100-continue is sent, and try |
| 4217 | * to receive. |
| 4218 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4219 | */ |
| 4220 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4221 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4222 | 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] | 4223 | } |
| 4224 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4225 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4226 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4227 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4228 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4229 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4230 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4231 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4232 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4233 | 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] | 4234 | } |
| 4235 | |
| 4236 | /* End of data: commit the total strings and return. */ |
| 4237 | if (ret < 0) { |
| 4238 | luaL_pushresult(&appctx->b); |
| 4239 | return 1; |
| 4240 | } |
| 4241 | |
| 4242 | /* Ensure that the block 2 length is usable. */ |
| 4243 | if (ret == 1) |
| 4244 | len2 = 0; |
| 4245 | |
| 4246 | /* Copy the fisrt block caping to the length required. */ |
| 4247 | if (len1 > len) |
| 4248 | len1 = len; |
| 4249 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4250 | len -= len1; |
| 4251 | |
| 4252 | /* Copy the second block. */ |
| 4253 | if (len2 > len) |
| 4254 | len2 = len; |
| 4255 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4256 | len -= len2; |
| 4257 | |
| 4258 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4259 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4260 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4261 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4262 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4263 | /* If we are no other data available, yield waiting for new data. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4264 | if (len > 0) { |
| 4265 | lua_pushinteger(L, len); |
| 4266 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4267 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4268 | 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] | 4269 | } |
| 4270 | |
| 4271 | /* return the result. */ |
| 4272 | luaL_pushresult(&appctx->b); |
| 4273 | return 1; |
| 4274 | } |
| 4275 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4276 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4277 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4278 | { |
| 4279 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4280 | int len = -1; |
| 4281 | |
| 4282 | /* Check arguments. */ |
| 4283 | if (lua_gettop(L) > 2) |
| 4284 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4285 | if (lua_gettop(L) >= 2) { |
| 4286 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4287 | lua_pop(L, 1); |
| 4288 | } |
| 4289 | |
| 4290 | /* Check the required length */ |
| 4291 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4292 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4293 | lua_pushinteger(L, len); |
| 4294 | |
| 4295 | /* Initialise the string catenation. */ |
| 4296 | luaL_buffinit(L, &appctx->b); |
| 4297 | |
| 4298 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4299 | } |
| 4300 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4301 | /* Append data in the output side of the buffer. This data is immediately |
| 4302 | * sent. The function returns the amount of data written. If the buffer |
| 4303 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4304 | * if the channel is closed. |
| 4305 | */ |
| 4306 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4307 | { |
| 4308 | size_t len; |
| 4309 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4310 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4311 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4312 | struct stream_interface *si = appctx->appctx->owner; |
| 4313 | struct channel *chn = si_ic(si); |
| 4314 | int max; |
| 4315 | |
| 4316 | /* Get the max amount of data which can write as input in the channel. */ |
| 4317 | max = channel_recv_max(chn); |
| 4318 | if (max > (len - l)) |
| 4319 | max = len - l; |
| 4320 | |
| 4321 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4322 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4323 | |
| 4324 | /* update counters. */ |
| 4325 | l += max; |
| 4326 | lua_pop(L, 1); |
| 4327 | lua_pushinteger(L, l); |
| 4328 | |
| 4329 | /* If some data is not send, declares the situation to the |
| 4330 | * applet, and returns a yield. |
| 4331 | */ |
| 4332 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4333 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4334 | 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] | 4335 | } |
| 4336 | |
| 4337 | return 1; |
| 4338 | } |
| 4339 | |
| 4340 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4341 | * yield the LUA process, and resume it without checking the |
| 4342 | * input arguments. |
| 4343 | */ |
| 4344 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4345 | { |
| 4346 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4347 | size_t len; |
| 4348 | char hex[10]; |
| 4349 | |
| 4350 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4351 | |
| 4352 | /* If transfer encoding chunked is selected, we surround the data |
| 4353 | * by chunk data. |
| 4354 | */ |
| 4355 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4356 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4357 | lua_pushfstring(L, "%s\r\n", hex); |
| 4358 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4359 | lua_pushstring(L, "\r\n"); |
| 4360 | lua_concat(L, 3); |
| 4361 | } |
| 4362 | |
| 4363 | /* This interger is used for followinf the amount of data sent. */ |
| 4364 | lua_pushinteger(L, 0); |
| 4365 | |
| 4366 | /* We want to send some data. Headers must be sent. */ |
| 4367 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4368 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4369 | WILL_LJMP(lua_error(L)); |
| 4370 | } |
| 4371 | |
| 4372 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4373 | } |
| 4374 | |
| 4375 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4376 | { |
| 4377 | const char *name; |
| 4378 | int ret; |
| 4379 | |
| 4380 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4381 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4382 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4383 | |
| 4384 | /* Push in the stack the "response" entry. */ |
| 4385 | ret = lua_getfield(L, 1, "response"); |
| 4386 | if (ret != LUA_TTABLE) { |
| 4387 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4388 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4389 | WILL_LJMP(lua_error(L)); |
| 4390 | } |
| 4391 | |
| 4392 | /* check if the header is already registered if it is not |
| 4393 | * the case, register it. |
| 4394 | */ |
| 4395 | ret = lua_getfield(L, -1, name); |
| 4396 | if (ret == LUA_TNIL) { |
| 4397 | |
| 4398 | /* Entry not found. */ |
| 4399 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4400 | |
| 4401 | /* Insert the new header name in the array in the top of the stack. |
| 4402 | * It left the new array in the top of the stack. |
| 4403 | */ |
| 4404 | lua_newtable(L); |
| 4405 | lua_pushvalue(L, 2); |
| 4406 | lua_pushvalue(L, -2); |
| 4407 | lua_settable(L, -4); |
| 4408 | |
| 4409 | } else if (ret != LUA_TTABLE) { |
| 4410 | |
| 4411 | /* corruption error. */ |
| 4412 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4413 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4414 | WILL_LJMP(lua_error(L)); |
| 4415 | } |
| 4416 | |
| 4417 | /* Now the top od thestack is an array of values. We push |
| 4418 | * the header value as new entry. |
| 4419 | */ |
| 4420 | lua_pushvalue(L, 3); |
| 4421 | ret = lua_rawlen(L, -2); |
| 4422 | lua_rawseti(L, -2, ret + 1); |
| 4423 | lua_pushboolean(L, 1); |
| 4424 | return 1; |
| 4425 | } |
| 4426 | |
| 4427 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4428 | { |
| 4429 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4430 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4431 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4432 | |
| 4433 | if (status < 100 || status > 599) { |
| 4434 | lua_pushboolean(L, 0); |
| 4435 | return 1; |
| 4436 | } |
| 4437 | |
| 4438 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4439 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4440 | lua_pushboolean(L, 1); |
| 4441 | return 1; |
| 4442 | } |
| 4443 | |
| 4444 | /* We will build the status line and the headers of the HTTP response. |
| 4445 | * We will try send at once if its not possible, we give back the hand |
| 4446 | * waiting for more room. |
| 4447 | */ |
| 4448 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4449 | { |
| 4450 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4451 | struct stream_interface *si = appctx->appctx->owner; |
| 4452 | struct channel *chn = si_ic(si); |
| 4453 | int ret; |
| 4454 | size_t len; |
| 4455 | const char *msg; |
| 4456 | |
| 4457 | /* Get the message as the first argument on the stack. */ |
| 4458 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4459 | |
| 4460 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4461 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4462 | |
| 4463 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4464 | * big for the buffers. |
| 4465 | */ |
| 4466 | if (ret == -2 || ret == -3) { |
| 4467 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4468 | WILL_LJMP(lua_error(L)); |
| 4469 | } |
| 4470 | |
| 4471 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4472 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4473 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4474 | 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] | 4475 | } |
| 4476 | |
| 4477 | /* Headers sent, set the flag. */ |
| 4478 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4479 | return 0; |
| 4480 | } |
| 4481 | |
| 4482 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4483 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4484 | struct buffer *tmp = get_trash_chunk(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4485 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4486 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4487 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4488 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4489 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4490 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4491 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4492 | int hdr_chunked = 0; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4493 | const char *reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4494 | |
| 4495 | if (reason == NULL) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4496 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4497 | |
| 4498 | /* Use the same http version than the request. */ |
| 4499 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4500 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4501 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4502 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4503 | |
| 4504 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4505 | lua_pushvalue(L, 0); |
| 4506 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4507 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4508 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4509 | WILL_LJMP(lua_error(L)); |
| 4510 | } |
| 4511 | |
| 4512 | /* Browse the list of headers. */ |
| 4513 | lua_pushnil(L); |
| 4514 | while(lua_next(L, -2) != 0) { |
| 4515 | |
| 4516 | /* We expect a string as -2. */ |
| 4517 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4518 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4519 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4520 | lua_typename(L, lua_type(L, -2))); |
| 4521 | WILL_LJMP(lua_error(L)); |
| 4522 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4523 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4524 | |
| 4525 | /* We expect an array as -1. */ |
| 4526 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4527 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4528 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4529 | name, |
| 4530 | lua_typename(L, lua_type(L, -1))); |
| 4531 | WILL_LJMP(lua_error(L)); |
| 4532 | } |
| 4533 | |
| 4534 | /* Browse the table who is on the top of the stack. */ |
| 4535 | lua_pushnil(L); |
| 4536 | while(lua_next(L, -2) != 0) { |
| 4537 | |
| 4538 | /* We expect a number as -2. */ |
| 4539 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4540 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4541 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4542 | name, |
| 4543 | lua_typename(L, lua_type(L, -2))); |
| 4544 | WILL_LJMP(lua_error(L)); |
| 4545 | } |
| 4546 | id = lua_tointeger(L, -2); |
| 4547 | |
| 4548 | /* We expect a string as -2. */ |
| 4549 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4550 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4551 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4552 | name, id, |
| 4553 | lua_typename(L, lua_type(L, -1))); |
| 4554 | WILL_LJMP(lua_error(L)); |
| 4555 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4556 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4557 | |
| 4558 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4559 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 4560 | memcpy(tmp->area + tmp->data, name, name_len); |
| 4561 | tmp->data += name_len; |
| 4562 | tmp->area[tmp->data++] = ':'; |
| 4563 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4564 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4565 | memcpy(tmp->area + tmp->data, value, |
| 4566 | value_len); |
| 4567 | tmp->data += value_len; |
| 4568 | tmp->area[tmp->data++] = '\r'; |
| 4569 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4570 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4571 | |
| 4572 | /* Protocol checks. */ |
| 4573 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4574 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4575 | * is done without control. If it contains a bad value, |
| 4576 | * the content-length remains negative so that we can |
| 4577 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4578 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4579 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4580 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4581 | |
| 4582 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4583 | if (name_len == 17 && value_len == 7 && |
| 4584 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4585 | strcasecmp("chunked", value) == 0) |
| 4586 | hdr_chunked = 1; |
| 4587 | |
| 4588 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4589 | lua_pop(L, 1); |
| 4590 | } |
| 4591 | |
| 4592 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4593 | lua_pop(L, 1); |
| 4594 | } |
| 4595 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4596 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4597 | * and the status code implies the presence of a message body, we must |
| 4598 | * announce a transfer encoding chunked. This is required by haproxy |
| 4599 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4600 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4601 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4602 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4603 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 4604 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4605 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4606 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4607 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4608 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4609 | } |
| 4610 | |
| 4611 | /* Finalize headers. */ |
| 4612 | chunk_appendf(tmp, "\r\n"); |
| 4613 | |
| 4614 | /* Remove the last entry and the array of headers */ |
| 4615 | lua_pop(L, 2); |
| 4616 | |
| 4617 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4618 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4619 | |
| 4620 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4621 | } |
| 4622 | |
| 4623 | /* |
| 4624 | * |
| 4625 | * |
| 4626 | * Class HTTP |
| 4627 | * |
| 4628 | * |
| 4629 | */ |
| 4630 | |
| 4631 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4632 | * a class stream, otherwise it throws an error. |
| 4633 | */ |
| 4634 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4635 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4636 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4637 | } |
| 4638 | |
| 4639 | /* This function creates and push in the stack a HTTP object |
| 4640 | * according with a current TXN. |
| 4641 | */ |
| 4642 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4643 | { |
| 4644 | struct hlua_txn *htxn; |
| 4645 | |
| 4646 | /* Check stack size. */ |
| 4647 | if (!lua_checkstack(L, 3)) |
| 4648 | return 0; |
| 4649 | |
| 4650 | /* Create the object: obj[0] = userdata. |
| 4651 | * Note that the base of the Converters object is the |
| 4652 | * same than the TXN object. |
| 4653 | */ |
| 4654 | lua_newtable(L); |
| 4655 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4656 | lua_rawseti(L, -2, 0); |
| 4657 | |
| 4658 | htxn->s = txn->s; |
| 4659 | htxn->p = txn->p; |
| 4660 | |
| 4661 | /* Pop a class stream metatable and affect it to the table. */ |
| 4662 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4663 | lua_setmetatable(L, -2); |
| 4664 | |
| 4665 | return 1; |
| 4666 | } |
| 4667 | |
| 4668 | /* This function creates ans returns an array of HTTP headers. |
| 4669 | * This function does not fails. It is used as wrapper with the |
| 4670 | * 2 following functions. |
| 4671 | */ |
| 4672 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4673 | { |
| 4674 | const char *cur_ptr, *cur_next, *p; |
| 4675 | int old_idx, cur_idx; |
| 4676 | struct hdr_idx_elem *cur_hdr; |
| 4677 | const char *hn, *hv; |
| 4678 | int hnl, hvl; |
| 4679 | int type; |
| 4680 | const char *in; |
| 4681 | char *out; |
| 4682 | int len; |
| 4683 | |
| 4684 | /* Create the table. */ |
| 4685 | lua_newtable(L); |
| 4686 | |
| 4687 | if (!htxn->s->txn) |
| 4688 | return 1; |
| 4689 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4690 | /* Check if a valid response is parsed */ |
| 4691 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4692 | return 1; |
| 4693 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4694 | /* Build array of headers. */ |
| 4695 | old_idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 4696 | cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4697 | |
| 4698 | while (1) { |
| 4699 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4700 | if (!cur_idx) |
| 4701 | break; |
| 4702 | old_idx = cur_idx; |
| 4703 | |
| 4704 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4705 | cur_ptr = cur_next; |
| 4706 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4707 | |
| 4708 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4709 | * and the next header starts at cur_next. We'll check |
| 4710 | * this header in the list as well as against the default |
| 4711 | * rule. |
| 4712 | */ |
| 4713 | |
| 4714 | /* look for ': *'. */ |
| 4715 | hn = cur_ptr; |
| 4716 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4717 | if (p >= cur_ptr+cur_hdr->len) |
| 4718 | continue; |
| 4719 | hnl = p - hn; |
| 4720 | p++; |
| 4721 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4722 | p++; |
| 4723 | if (p >= cur_ptr+cur_hdr->len) |
| 4724 | continue; |
| 4725 | hv = p; |
| 4726 | hvl = cur_ptr+cur_hdr->len-p; |
| 4727 | |
| 4728 | /* Lowercase the key. Don't check the size of trash, it have |
| 4729 | * the size of one buffer and the input data contains in one |
| 4730 | * buffer. |
| 4731 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4732 | out = trash.area; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4733 | for (in=hn; in<hn+hnl; in++, out++) |
| 4734 | *out = tolower(*in); |
| 4735 | *out = '\0'; |
| 4736 | |
| 4737 | /* Check for existing entry: |
| 4738 | * assume that the table is on the top of the stack, and |
| 4739 | * push the key in the stack, the function lua_gettable() |
| 4740 | * perform the lookup. |
| 4741 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4742 | lua_pushlstring(L, trash.area, hnl); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4743 | lua_gettable(L, -2); |
| 4744 | type = lua_type(L, -1); |
| 4745 | |
| 4746 | switch (type) { |
| 4747 | case LUA_TNIL: |
| 4748 | /* Table not found, create it. */ |
| 4749 | lua_pop(L, 1); /* remove the nil value. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4750 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4751 | lua_newtable(L); /* create and push empty table. */ |
| 4752 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4753 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4754 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4755 | break; |
| 4756 | |
| 4757 | case LUA_TTABLE: |
| 4758 | /* Entry found: push the value in the table. */ |
| 4759 | len = lua_rawlen(L, -1); |
| 4760 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4761 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4762 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4763 | break; |
| 4764 | |
| 4765 | default: |
| 4766 | /* Other cases are errors. */ |
| 4767 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4768 | WILL_LJMP(lua_error(L)); |
| 4769 | } |
| 4770 | } |
| 4771 | |
| 4772 | return 1; |
| 4773 | } |
| 4774 | |
| 4775 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4776 | { |
| 4777 | struct hlua_txn *htxn; |
| 4778 | |
| 4779 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4780 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4781 | |
| 4782 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4783 | } |
| 4784 | |
| 4785 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4786 | { |
| 4787 | struct hlua_txn *htxn; |
| 4788 | |
| 4789 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4790 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4791 | |
| 4792 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4793 | } |
| 4794 | |
| 4795 | /* This function replace full header, or just a value in |
| 4796 | * the request or in the response. It is a wrapper fir the |
| 4797 | * 4 following functions. |
| 4798 | */ |
| 4799 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4800 | struct http_msg *msg, int action) |
| 4801 | { |
| 4802 | size_t name_len; |
| 4803 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4804 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4805 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4806 | struct my_regex re; |
| 4807 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4808 | /* Check if a valid response is parsed */ |
| 4809 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4810 | return 0; |
| 4811 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4812 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4813 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4814 | |
| 4815 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4816 | regex_free(&re); |
| 4817 | return 0; |
| 4818 | } |
| 4819 | |
| 4820 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4821 | { |
| 4822 | struct hlua_txn *htxn; |
| 4823 | |
| 4824 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4825 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4826 | |
| 4827 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4828 | } |
| 4829 | |
| 4830 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4831 | { |
| 4832 | struct hlua_txn *htxn; |
| 4833 | |
| 4834 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4835 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4836 | |
| 4837 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4838 | } |
| 4839 | |
| 4840 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4841 | { |
| 4842 | struct hlua_txn *htxn; |
| 4843 | |
| 4844 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4845 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4846 | |
| 4847 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4848 | } |
| 4849 | |
| 4850 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4851 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4852 | struct hlua_txn *htxn; |
| 4853 | |
| 4854 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4855 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4856 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4857 | 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] | 4858 | } |
| 4859 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4860 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4861 | * It is a wrapper for the 2 following functions. |
| 4862 | */ |
| 4863 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4864 | { |
| 4865 | size_t len; |
| 4866 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4867 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4868 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4869 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4870 | /* Check if a valid response is parsed */ |
| 4871 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4872 | return 0; |
| 4873 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4874 | ctx.idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 4875 | while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4876 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4877 | return 0; |
| 4878 | } |
| 4879 | |
| 4880 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4881 | { |
| 4882 | struct hlua_txn *htxn; |
| 4883 | |
| 4884 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4885 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4886 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4887 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4888 | } |
| 4889 | |
| 4890 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4891 | { |
| 4892 | struct hlua_txn *htxn; |
| 4893 | |
| 4894 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4895 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4896 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4897 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4898 | } |
| 4899 | |
| 4900 | /* This function adds an header. It is a wrapper used by |
| 4901 | * the 2 following functions. |
| 4902 | */ |
| 4903 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4904 | { |
| 4905 | size_t name_len; |
| 4906 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4907 | size_t value_len; |
| 4908 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4909 | char *p; |
| 4910 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4911 | /* Check if a valid message is parsed */ |
| 4912 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4913 | return 0; |
| 4914 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4915 | /* Check length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4916 | trash.data = value_len + name_len + 2; |
| 4917 | if (trash.data > trash.size) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4918 | return 0; |
| 4919 | |
| 4920 | /* Creates the header string. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4921 | p = trash.area; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4922 | memcpy(p, name, name_len); |
| 4923 | p += name_len; |
| 4924 | *p = ':'; |
| 4925 | p++; |
| 4926 | *p = ' '; |
| 4927 | p++; |
| 4928 | memcpy(p, value, value_len); |
| 4929 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4930 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4931 | trash.area, trash.data) != 0); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4932 | |
| 4933 | return 0; |
| 4934 | } |
| 4935 | |
| 4936 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4937 | { |
| 4938 | struct hlua_txn *htxn; |
| 4939 | |
| 4940 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4941 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4942 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4943 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4944 | } |
| 4945 | |
| 4946 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4947 | { |
| 4948 | struct hlua_txn *htxn; |
| 4949 | |
| 4950 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4951 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4952 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4953 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4954 | } |
| 4955 | |
| 4956 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4957 | { |
| 4958 | struct hlua_txn *htxn; |
| 4959 | |
| 4960 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4961 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4962 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4963 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4964 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4965 | } |
| 4966 | |
| 4967 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4968 | { |
| 4969 | struct hlua_txn *htxn; |
| 4970 | |
| 4971 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4972 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4973 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4974 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4975 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4976 | } |
| 4977 | |
| 4978 | /* This function set the method. */ |
| 4979 | static int hlua_http_req_set_meth(lua_State *L) |
| 4980 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4981 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4982 | size_t name_len; |
| 4983 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4984 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4985 | /* Check if a valid request is parsed */ |
| 4986 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4987 | lua_pushboolean(L, 0); |
| 4988 | return 1; |
| 4989 | } |
| 4990 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4991 | 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] | 4992 | return 1; |
| 4993 | } |
| 4994 | |
| 4995 | /* This function set the method. */ |
| 4996 | static int hlua_http_req_set_path(lua_State *L) |
| 4997 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4998 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4999 | size_t name_len; |
| 5000 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5001 | |
| 5002 | /* Check if a valid request is parsed */ |
| 5003 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5004 | lua_pushboolean(L, 0); |
| 5005 | return 1; |
| 5006 | } |
| 5007 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5008 | 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] | 5009 | return 1; |
| 5010 | } |
| 5011 | |
| 5012 | /* This function set the query-string. */ |
| 5013 | static int hlua_http_req_set_query(lua_State *L) |
| 5014 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5015 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5016 | size_t name_len; |
| 5017 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5018 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5019 | /* Check if a valid request is parsed */ |
| 5020 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5021 | lua_pushboolean(L, 0); |
| 5022 | return 1; |
| 5023 | } |
| 5024 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5025 | /* Check length. */ |
| 5026 | if (name_len > trash.size - 1) { |
| 5027 | lua_pushboolean(L, 0); |
| 5028 | return 1; |
| 5029 | } |
| 5030 | |
| 5031 | /* Add the mark question as prefix. */ |
| 5032 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5033 | trash.area[trash.data++] = '?'; |
| 5034 | memcpy(trash.area + trash.data, name, name_len); |
| 5035 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5036 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5037 | lua_pushboolean(L, |
| 5038 | 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] | 5039 | return 1; |
| 5040 | } |
| 5041 | |
| 5042 | /* This function set the uri. */ |
| 5043 | static int hlua_http_req_set_uri(lua_State *L) |
| 5044 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5045 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5046 | size_t name_len; |
| 5047 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5048 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5049 | /* Check if a valid request is parsed */ |
| 5050 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5051 | lua_pushboolean(L, 0); |
| 5052 | return 1; |
| 5053 | } |
| 5054 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5055 | 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] | 5056 | return 1; |
| 5057 | } |
| 5058 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5059 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5060 | static int hlua_http_res_set_status(lua_State *L) |
| 5061 | { |
| 5062 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5063 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5064 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5065 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5066 | /* Check if a valid response is parsed */ |
| 5067 | if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5068 | return 0; |
| 5069 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5070 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5071 | return 0; |
| 5072 | } |
| 5073 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5074 | /* |
| 5075 | * |
| 5076 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5077 | * Class TXN |
| 5078 | * |
| 5079 | * |
| 5080 | */ |
| 5081 | |
| 5082 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5083 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5084 | */ |
| 5085 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5086 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5087 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5088 | } |
| 5089 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5090 | __LJMP static int hlua_set_var(lua_State *L) |
| 5091 | { |
| 5092 | struct hlua_txn *htxn; |
| 5093 | const char *name; |
| 5094 | size_t len; |
| 5095 | struct sample smp; |
| 5096 | |
| 5097 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5098 | |
| 5099 | /* It is useles to retrieve the stream, but this function |
| 5100 | * runs only in a stream context. |
| 5101 | */ |
| 5102 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5103 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5104 | |
| 5105 | /* Converts the third argument in a sample. */ |
| 5106 | hlua_lua2smp(L, 3, &smp); |
| 5107 | |
| 5108 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5109 | 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] | 5110 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5111 | return 0; |
| 5112 | } |
| 5113 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5114 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5115 | { |
| 5116 | struct hlua_txn *htxn; |
| 5117 | const char *name; |
| 5118 | size_t len; |
| 5119 | struct sample smp; |
| 5120 | |
| 5121 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5122 | |
| 5123 | /* It is useles to retrieve the stream, but this function |
| 5124 | * runs only in a stream context. |
| 5125 | */ |
| 5126 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5127 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5128 | |
| 5129 | /* Unset the variable. */ |
| 5130 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5131 | vars_unset_by_name(name, len, &smp); |
| 5132 | return 0; |
| 5133 | } |
| 5134 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5135 | __LJMP static int hlua_get_var(lua_State *L) |
| 5136 | { |
| 5137 | struct hlua_txn *htxn; |
| 5138 | const char *name; |
| 5139 | size_t len; |
| 5140 | struct sample smp; |
| 5141 | |
| 5142 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5143 | |
| 5144 | /* It is useles to retrieve the stream, but this function |
| 5145 | * runs only in a stream context. |
| 5146 | */ |
| 5147 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5148 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5149 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5150 | 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] | 5151 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5152 | lua_pushnil(L); |
| 5153 | return 1; |
| 5154 | } |
| 5155 | |
| 5156 | return hlua_smp2lua(L, &smp); |
| 5157 | } |
| 5158 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5159 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5160 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5161 | struct hlua *hlua; |
| 5162 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5163 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5164 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5165 | /* It is useles to retrieve the stream, but this function |
| 5166 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5167 | */ |
| 5168 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5169 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5170 | |
| 5171 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5172 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5173 | |
| 5174 | /* Get and store new value. */ |
| 5175 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5176 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5177 | |
| 5178 | return 0; |
| 5179 | } |
| 5180 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5181 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5182 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5183 | struct hlua *hlua; |
| 5184 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5185 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5186 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5187 | /* It is useles to retrieve the stream, but this function |
| 5188 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5189 | */ |
| 5190 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5191 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5192 | |
| 5193 | /* Push configuration index in the stack. */ |
| 5194 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5195 | |
| 5196 | return 1; |
| 5197 | } |
| 5198 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5199 | /* Create stack entry containing a class TXN. This function |
| 5200 | * return 0 if the stack does not contains free slots, |
| 5201 | * otherwise it returns 1. |
| 5202 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5203 | 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] | 5204 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5205 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5206 | |
| 5207 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5208 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5209 | return 0; |
| 5210 | |
| 5211 | /* NOTE: The allocation never fails. The failure |
| 5212 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5213 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5214 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5215 | /* Create the object: obj[0] = userdata. */ |
| 5216 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5217 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5218 | lua_rawseti(L, -2, 0); |
| 5219 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5220 | htxn->s = s; |
| 5221 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5222 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5223 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5224 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5225 | /* Create the "f" field that contains a list of fetches. */ |
| 5226 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5227 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5228 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5229 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5230 | |
| 5231 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5232 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5233 | 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] | 5234 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5235 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5236 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5237 | /* Create the "c" field that contains a list of converters. */ |
| 5238 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5239 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5240 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5241 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5242 | |
| 5243 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5244 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5245 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5246 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5247 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5248 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5249 | /* Create the "req" field that contains the request channel object. */ |
| 5250 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5251 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5252 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5253 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5254 | |
| 5255 | /* Create the "res" field that contains the response channel object. */ |
| 5256 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5257 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5258 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5259 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5260 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5261 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5262 | lua_pushstring(L, "http"); |
| 5263 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5264 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5265 | return 0; |
| 5266 | } |
| 5267 | else |
| 5268 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5269 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5270 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5271 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5272 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5273 | lua_setmetatable(L, -2); |
| 5274 | |
| 5275 | return 1; |
| 5276 | } |
| 5277 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5278 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5279 | { |
| 5280 | const char *msg; |
| 5281 | struct hlua_txn *htxn; |
| 5282 | |
| 5283 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5284 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5285 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5286 | |
| 5287 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5288 | return 0; |
| 5289 | } |
| 5290 | |
| 5291 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5292 | { |
| 5293 | int level; |
| 5294 | const char *msg; |
| 5295 | struct hlua_txn *htxn; |
| 5296 | |
| 5297 | MAY_LJMP(check_args(L, 3, "log")); |
| 5298 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5299 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5300 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5301 | |
| 5302 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5303 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5304 | |
| 5305 | hlua_sendlog(htxn->s->be, level, msg); |
| 5306 | return 0; |
| 5307 | } |
| 5308 | |
| 5309 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5310 | { |
| 5311 | const char *msg; |
| 5312 | struct hlua_txn *htxn; |
| 5313 | |
| 5314 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5315 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5316 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5317 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5318 | return 0; |
| 5319 | } |
| 5320 | |
| 5321 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5322 | { |
| 5323 | const char *msg; |
| 5324 | struct hlua_txn *htxn; |
| 5325 | |
| 5326 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5327 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5328 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5329 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5330 | return 0; |
| 5331 | } |
| 5332 | |
| 5333 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5334 | { |
| 5335 | const char *msg; |
| 5336 | struct hlua_txn *htxn; |
| 5337 | |
| 5338 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5339 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5340 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5341 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5342 | return 0; |
| 5343 | } |
| 5344 | |
| 5345 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5346 | { |
| 5347 | const char *msg; |
| 5348 | struct hlua_txn *htxn; |
| 5349 | |
| 5350 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5351 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5352 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5353 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5354 | return 0; |
| 5355 | } |
| 5356 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5357 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5358 | { |
| 5359 | struct hlua_txn *htxn; |
| 5360 | int ll; |
| 5361 | |
| 5362 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5363 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5364 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5365 | |
| 5366 | if (ll < 0 || ll > 7) |
| 5367 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5368 | |
| 5369 | htxn->s->logs.level = ll; |
| 5370 | return 0; |
| 5371 | } |
| 5372 | |
| 5373 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5374 | { |
| 5375 | struct hlua_txn *htxn; |
| 5376 | struct connection *cli_conn; |
| 5377 | int tos; |
| 5378 | |
| 5379 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5380 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5381 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5382 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5383 | if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5384 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5385 | |
| 5386 | return 0; |
| 5387 | } |
| 5388 | |
| 5389 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5390 | { |
| 5391 | #ifdef SO_MARK |
| 5392 | struct hlua_txn *htxn; |
| 5393 | struct connection *cli_conn; |
| 5394 | int mark; |
| 5395 | |
| 5396 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5397 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5398 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5399 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5400 | if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5401 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5402 | #endif |
| 5403 | return 0; |
| 5404 | } |
| 5405 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5406 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5407 | { |
| 5408 | struct hlua_txn *htxn; |
| 5409 | |
| 5410 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5411 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5412 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5413 | return 0; |
| 5414 | } |
| 5415 | |
| 5416 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5417 | { |
| 5418 | struct hlua_txn *htxn; |
| 5419 | |
| 5420 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5421 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5422 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5423 | return 0; |
| 5424 | } |
| 5425 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5426 | /* This function is an Lua binding that send pending data |
| 5427 | * to the client, and close the stream interface. |
| 5428 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5429 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5430 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5431 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5432 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5433 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5434 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5435 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5436 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5437 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5438 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5439 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5440 | * session, so we just end the execution of the current |
| 5441 | * lua code. |
| 5442 | */ |
| 5443 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5444 | WILL_LJMP(hlua_done(L)); |
| 5445 | return 0; |
| 5446 | } |
| 5447 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5448 | ic = &htxn->s->req; |
| 5449 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5450 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5451 | if (htxn->s->txn) { |
| 5452 | /* HTTP mode, let's stay in sync with the stream */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5453 | b_del(&ic->buf, htxn->s->txn->req.sov); |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5454 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 5455 | htxn->s->txn->req.sov = 0; |
| 5456 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 5457 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 5458 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5459 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 5460 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5461 | /* Note that if we want to support keep-alive, we need |
| 5462 | * to bypass the close/shutr_now calls below, but that |
| 5463 | * may only be done if the HTTP request was already |
| 5464 | * processed and the connection header is known (ie |
| 5465 | * not during TCP rules). |
| 5466 | */ |
| 5467 | } |
| 5468 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5469 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5470 | channel_abort(ic); |
| 5471 | channel_auto_close(ic); |
| 5472 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5473 | |
| 5474 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5475 | channel_auto_read(oc); |
| 5476 | channel_auto_close(oc); |
| 5477 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5478 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 5479 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5480 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5481 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5482 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5483 | return 0; |
| 5484 | } |
| 5485 | |
| 5486 | __LJMP static int hlua_log(lua_State *L) |
| 5487 | { |
| 5488 | int level; |
| 5489 | const char *msg; |
| 5490 | |
| 5491 | MAY_LJMP(check_args(L, 2, "log")); |
| 5492 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5493 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5494 | |
| 5495 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5496 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5497 | |
| 5498 | hlua_sendlog(NULL, level, msg); |
| 5499 | return 0; |
| 5500 | } |
| 5501 | |
| 5502 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5503 | { |
| 5504 | const char *msg; |
| 5505 | |
| 5506 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5507 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5508 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5509 | return 0; |
| 5510 | } |
| 5511 | |
| 5512 | __LJMP static int hlua_log_info(lua_State *L) |
| 5513 | { |
| 5514 | const char *msg; |
| 5515 | |
| 5516 | MAY_LJMP(check_args(L, 1, "info")); |
| 5517 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5518 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5519 | return 0; |
| 5520 | } |
| 5521 | |
| 5522 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5523 | { |
| 5524 | const char *msg; |
| 5525 | |
| 5526 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5527 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5528 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5529 | return 0; |
| 5530 | } |
| 5531 | |
| 5532 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5533 | { |
| 5534 | const char *msg; |
| 5535 | |
| 5536 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5537 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5538 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5539 | return 0; |
| 5540 | } |
| 5541 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5542 | __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] | 5543 | { |
| 5544 | int wakeup_ms = lua_tointeger(L, -1); |
| 5545 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5546 | 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] | 5547 | return 0; |
| 5548 | } |
| 5549 | |
| 5550 | __LJMP static int hlua_sleep(lua_State *L) |
| 5551 | { |
| 5552 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5553 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5554 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5555 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5556 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5557 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5558 | wakeup_ms = tick_add(now_ms, delay); |
| 5559 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5560 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5561 | 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] | 5562 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5563 | } |
| 5564 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5565 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5566 | { |
| 5567 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5568 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5569 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5570 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5571 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5572 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5573 | wakeup_ms = tick_add(now_ms, delay); |
| 5574 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5575 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5576 | 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] | 5577 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5578 | } |
| 5579 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5580 | /* This functionis an LUA binding. it permits to give back |
| 5581 | * the hand at the HAProxy scheduler. It is used when the |
| 5582 | * LUA processing consumes a lot of time. |
| 5583 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5584 | __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] | 5585 | { |
| 5586 | return 0; |
| 5587 | } |
| 5588 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5589 | __LJMP static int hlua_yield(lua_State *L) |
| 5590 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5591 | 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] | 5592 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5593 | } |
| 5594 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5595 | /* This function change the nice of the currently executed |
| 5596 | * task. It is used set low or high priority at the current |
| 5597 | * task. |
| 5598 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5599 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5600 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5601 | struct hlua *hlua; |
| 5602 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5603 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5604 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5605 | hlua = hlua_gethlua(L); |
| 5606 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5607 | |
| 5608 | /* If he task is not set, I'm in a start mode. */ |
| 5609 | if (!hlua || !hlua->task) |
| 5610 | return 0; |
| 5611 | |
| 5612 | if (nice < -1024) |
| 5613 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5614 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5615 | nice = 1024; |
| 5616 | |
| 5617 | hlua->task->nice = nice; |
| 5618 | return 0; |
| 5619 | } |
| 5620 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5621 | /* 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] | 5622 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5623 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5624 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5625 | * |
| 5626 | * Task wrapper are longjmp safe because the only one Lua code |
| 5627 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5628 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5629 | 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] | 5630 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5631 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5632 | enum hlua_exec status; |
| 5633 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 5634 | if (task->thread_mask == MAX_THREADS_MASK) |
| 5635 | task_set_affinity(task, tid_bit); |
| 5636 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5637 | /* If it is the first call to the task, we must initialize the |
| 5638 | * execution timeouts. |
| 5639 | */ |
| 5640 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5641 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5642 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5643 | /* Execute the Lua code. */ |
| 5644 | status = hlua_ctx_resume(hlua, 1); |
| 5645 | |
| 5646 | switch (status) { |
| 5647 | /* finished or yield */ |
| 5648 | case HLUA_E_OK: |
| 5649 | hlua_ctx_destroy(hlua); |
| 5650 | task_delete(task); |
| 5651 | task_free(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 5652 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5653 | break; |
| 5654 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5655 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 5656 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 5657 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5658 | break; |
| 5659 | |
| 5660 | /* finished with error. */ |
| 5661 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5662 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5663 | hlua_ctx_destroy(hlua); |
| 5664 | task_delete(task); |
| 5665 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5666 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5667 | break; |
| 5668 | |
| 5669 | case HLUA_E_ERR: |
| 5670 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5671 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5672 | hlua_ctx_destroy(hlua); |
| 5673 | task_delete(task); |
| 5674 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5675 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5676 | break; |
| 5677 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5678 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5679 | } |
| 5680 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5681 | /* This function is an LUA binding that register LUA function to be |
| 5682 | * executed after the HAProxy configuration parsing and before the |
| 5683 | * HAProxy scheduler starts. This function expect only one LUA |
| 5684 | * argument that is a function. This function returns nothing, but |
| 5685 | * throws if an error is encountered. |
| 5686 | */ |
| 5687 | __LJMP static int hlua_register_init(lua_State *L) |
| 5688 | { |
| 5689 | struct hlua_init_function *init; |
| 5690 | int ref; |
| 5691 | |
| 5692 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5693 | |
| 5694 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5695 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5696 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5697 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5698 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5699 | |
| 5700 | init->function_ref = ref; |
| 5701 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5702 | return 0; |
| 5703 | } |
| 5704 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5705 | /* This functio is an LUA binding. It permits to register a task |
| 5706 | * executed in parallel of the main HAroxy activity. The task is |
| 5707 | * created and it is set in the HAProxy scheduler. It can be called |
| 5708 | * from the "init" section, "post init" or during the runtime. |
| 5709 | * |
| 5710 | * Lua prototype: |
| 5711 | * |
| 5712 | * <none> core.register_task(<function>) |
| 5713 | */ |
| 5714 | static int hlua_register_task(lua_State *L) |
| 5715 | { |
| 5716 | struct hlua *hlua; |
| 5717 | struct task *task; |
| 5718 | int ref; |
| 5719 | |
| 5720 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5721 | |
| 5722 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5723 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5724 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5725 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5726 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5727 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 5728 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 5729 | if (!task) |
| 5730 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 5731 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5732 | task->context = hlua; |
| 5733 | task->process = hlua_process_task; |
| 5734 | |
| 5735 | if (!hlua_ctx_init(hlua, task)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5736 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5737 | |
| 5738 | /* Restore the function in the stack. */ |
| 5739 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5740 | hlua->nargs = 0; |
| 5741 | |
| 5742 | /* Schedule task. */ |
| 5743 | task_schedule(task, now_ms); |
| 5744 | |
| 5745 | return 0; |
| 5746 | } |
| 5747 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5748 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5749 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5750 | * resume converters. |
| 5751 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5752 | 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] | 5753 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5754 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5755 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5756 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5757 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5758 | if (!stream) |
| 5759 | return 0; |
| 5760 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 5761 | if (IS_HTX_STRM(stream)) { |
| 5762 | SEND_ERR(stream->be, "Lua converter '%s': Lua fetches cannot be used when the" |
| 5763 | " HTX internal representation is enabled.\n", fcn->name); |
| 5764 | return 0; |
| 5765 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5766 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5767 | * Lua context can be not initialized. This behavior |
| 5768 | * permits to save performances because a systematic |
| 5769 | * Lua initialization cause 5% performances loss. |
| 5770 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5771 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5772 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5773 | if (!stream->hlua) { |
| 5774 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5775 | return 0; |
| 5776 | } |
| 5777 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5778 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5779 | return 0; |
| 5780 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5781 | } |
| 5782 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5783 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5784 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5785 | |
| 5786 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5787 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5788 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5789 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5790 | else |
| 5791 | error = "critical error"; |
| 5792 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5793 | return 0; |
| 5794 | } |
| 5795 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5796 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5797 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5798 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5799 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5800 | return 0; |
| 5801 | } |
| 5802 | |
| 5803 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5804 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5805 | |
| 5806 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5807 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5808 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5809 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5810 | return 0; |
| 5811 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5812 | hlua_smp2lua(stream->hlua->T, smp); |
| 5813 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5814 | |
| 5815 | /* push keywords in the stack. */ |
| 5816 | if (arg_p) { |
| 5817 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5818 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5819 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5820 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5821 | return 0; |
| 5822 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5823 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5824 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5825 | } |
| 5826 | } |
| 5827 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5828 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5829 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5830 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5831 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5832 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5833 | } |
| 5834 | |
| 5835 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5836 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5837 | /* finished. */ |
| 5838 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5839 | /* If the stack is empty, the function fails. */ |
| 5840 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5841 | return 0; |
| 5842 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5843 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5844 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5845 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5846 | return 1; |
| 5847 | |
| 5848 | /* yield. */ |
| 5849 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5850 | 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] | 5851 | return 0; |
| 5852 | |
| 5853 | /* finished with error. */ |
| 5854 | case HLUA_E_ERRMSG: |
| 5855 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5856 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5857 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5858 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5859 | return 0; |
| 5860 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5861 | case HLUA_E_ETMOUT: |
| 5862 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 5863 | return 0; |
| 5864 | |
| 5865 | case HLUA_E_NOMEM: |
| 5866 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 5867 | return 0; |
| 5868 | |
| 5869 | case HLUA_E_YIELD: |
| 5870 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 5871 | return 0; |
| 5872 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5873 | case HLUA_E_ERR: |
| 5874 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5875 | 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] | 5876 | |
| 5877 | default: |
| 5878 | return 0; |
| 5879 | } |
| 5880 | } |
| 5881 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5882 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5883 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5884 | * resume sample-fetches. This function will be called by the sample |
| 5885 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5886 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5887 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5888 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5889 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5890 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5891 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5892 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5893 | const struct buffer msg = { }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5894 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5895 | if (!stream) |
| 5896 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 5897 | |
| 5898 | if (IS_HTX_STRM(stream)) { |
| 5899 | SEND_ERR(stream->be, "Lua sample-fetch '%s': Lua fetches cannot be used when the" |
| 5900 | " HTX internal representation is enabled.\n", fcn->name); |
| 5901 | return 0; |
| 5902 | } |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5903 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5904 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5905 | * Lua context can be not initialized. This behavior |
| 5906 | * permits to save performances because a systematic |
| 5907 | * Lua initialization cause 5% performances loss. |
| 5908 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5909 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5910 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5911 | if (!stream->hlua) { |
| 5912 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5913 | return 0; |
| 5914 | } |
| 5915 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5916 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5917 | return 0; |
| 5918 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5919 | } |
| 5920 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5921 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5922 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5923 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5924 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5925 | |
| 5926 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5927 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5928 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5929 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5930 | else |
| 5931 | error = "critical error"; |
| 5932 | 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] | 5933 | return 0; |
| 5934 | } |
| 5935 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5936 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5937 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5938 | 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] | 5939 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5940 | return 0; |
| 5941 | } |
| 5942 | |
| 5943 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5944 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5945 | |
| 5946 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5947 | 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] | 5948 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5949 | 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] | 5950 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5951 | return 0; |
| 5952 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5953 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5954 | |
| 5955 | /* push keywords in the stack. */ |
| 5956 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5957 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5958 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5959 | 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] | 5960 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5961 | return 0; |
| 5962 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5963 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5964 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5965 | } |
| 5966 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5967 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5968 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5969 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5970 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5971 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5972 | } |
| 5973 | |
| 5974 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5975 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5976 | /* finished. */ |
| 5977 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5978 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5979 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5980 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5981 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5982 | /* If the stack is empty, the function fails. */ |
| 5983 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5984 | return 0; |
| 5985 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5986 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5987 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5988 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5989 | |
| 5990 | /* Set the end of execution flag. */ |
| 5991 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5992 | return 1; |
| 5993 | |
| 5994 | /* yield. */ |
| 5995 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5996 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5997 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5998 | 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] | 5999 | return 0; |
| 6000 | |
| 6001 | /* finished with error. */ |
| 6002 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6003 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6004 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6005 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6006 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6007 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6008 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6009 | return 0; |
| 6010 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6011 | case HLUA_E_ETMOUT: |
| 6012 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 6013 | stream_int_retnclose(&stream->si[0], &msg); |
| 6014 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6015 | return 0; |
| 6016 | |
| 6017 | case HLUA_E_NOMEM: |
| 6018 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 6019 | stream_int_retnclose(&stream->si[0], &msg); |
| 6020 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6021 | return 0; |
| 6022 | |
| 6023 | case HLUA_E_YIELD: |
| 6024 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 6025 | stream_int_retnclose(&stream->si[0], &msg); |
| 6026 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6027 | return 0; |
| 6028 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6029 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6030 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6031 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6032 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6033 | 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] | 6034 | |
| 6035 | default: |
| 6036 | return 0; |
| 6037 | } |
| 6038 | } |
| 6039 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6040 | /* This function is an LUA binding used for registering |
| 6041 | * "sample-conv" functions. It expects a converter name used |
| 6042 | * in the haproxy configuration file, and an LUA function. |
| 6043 | */ |
| 6044 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6045 | { |
| 6046 | struct sample_conv_kw_list *sck; |
| 6047 | const char *name; |
| 6048 | int ref; |
| 6049 | int len; |
| 6050 | struct hlua_function *fcn; |
| 6051 | |
| 6052 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6053 | |
| 6054 | /* First argument : converter name. */ |
| 6055 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6056 | |
| 6057 | /* Second argument : lua function. */ |
| 6058 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6059 | |
| 6060 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6061 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6062 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6063 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6064 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6065 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6066 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6067 | |
| 6068 | /* Fill fcn. */ |
| 6069 | fcn->name = strdup(name); |
| 6070 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6071 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6072 | fcn->function_ref = ref; |
| 6073 | |
| 6074 | /* List head */ |
| 6075 | sck->list.n = sck->list.p = NULL; |
| 6076 | |
| 6077 | /* converter keyword. */ |
| 6078 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6079 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6080 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6081 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6082 | |
| 6083 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6084 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6085 | 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] | 6086 | sck->kw[0].val_args = NULL; |
| 6087 | sck->kw[0].in_type = SMP_T_STR; |
| 6088 | sck->kw[0].out_type = SMP_T_STR; |
| 6089 | sck->kw[0].private = fcn; |
| 6090 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6091 | /* Register this new converter */ |
| 6092 | sample_register_convs(sck); |
| 6093 | |
| 6094 | return 0; |
| 6095 | } |
| 6096 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6097 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6098 | * "sample-fetch" functions. It expects a converter name used |
| 6099 | * in the haproxy configuration file, and an LUA function. |
| 6100 | */ |
| 6101 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6102 | { |
| 6103 | const char *name; |
| 6104 | int ref; |
| 6105 | int len; |
| 6106 | struct sample_fetch_kw_list *sfk; |
| 6107 | struct hlua_function *fcn; |
| 6108 | |
| 6109 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6110 | |
| 6111 | /* First argument : sample-fetch name. */ |
| 6112 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6113 | |
| 6114 | /* Second argument : lua function. */ |
| 6115 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6116 | |
| 6117 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6118 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6119 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6120 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6121 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6122 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6123 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6124 | |
| 6125 | /* Fill fcn. */ |
| 6126 | fcn->name = strdup(name); |
| 6127 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6128 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6129 | fcn->function_ref = ref; |
| 6130 | |
| 6131 | /* List head */ |
| 6132 | sfk->list.n = sfk->list.p = NULL; |
| 6133 | |
| 6134 | /* sample-fetch keyword. */ |
| 6135 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6136 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6137 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6138 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6139 | |
| 6140 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6141 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6142 | 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] | 6143 | sfk->kw[0].val_args = NULL; |
| 6144 | sfk->kw[0].out_type = SMP_T_STR; |
| 6145 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6146 | sfk->kw[0].val = 0; |
| 6147 | sfk->kw[0].private = fcn; |
| 6148 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6149 | /* Register this new fetch. */ |
| 6150 | sample_register_fetches(sfk); |
| 6151 | |
| 6152 | return 0; |
| 6153 | } |
| 6154 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6155 | /* This function is a wrapper to execute each LUA function declared |
| 6156 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6157 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6158 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6159 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6160 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6161 | 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] | 6162 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6163 | { |
| 6164 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6165 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6166 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6167 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6168 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6169 | |
| 6170 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 6171 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 6172 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 6173 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 6174 | 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] | 6175 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6176 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6177 | return ACT_RET_CONT; |
| 6178 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6179 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6180 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6181 | * Lua context can be not initialized. This behavior |
| 6182 | * permits to save performances because a systematic |
| 6183 | * Lua initialization cause 5% performances loss. |
| 6184 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6185 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6186 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6187 | if (!s->hlua) { |
| 6188 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6189 | rule->arg.hlua_rule->fcn.name); |
| 6190 | return ACT_RET_CONT; |
| 6191 | } |
| 6192 | if (!hlua_ctx_init(s->hlua, s->task)) { |
| 6193 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6194 | rule->arg.hlua_rule->fcn.name); |
| 6195 | return ACT_RET_CONT; |
| 6196 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6197 | } |
| 6198 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6199 | consistency_set(s, dir, &s->hlua->cons); |
| 6200 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6201 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6202 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6203 | |
| 6204 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6205 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6206 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6207 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6208 | else |
| 6209 | error = "critical error"; |
| 6210 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6211 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6212 | return ACT_RET_CONT; |
| 6213 | } |
| 6214 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6215 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6216 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6217 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6218 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6219 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6220 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6221 | } |
| 6222 | |
| 6223 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6224 | 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] | 6225 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6226 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6227 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6228 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6229 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6230 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6231 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6232 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6233 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6234 | |
| 6235 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6236 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6237 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6238 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6239 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6240 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6241 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6242 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6243 | lua_pushstring(s->hlua->T, *arg); |
| 6244 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6245 | } |
| 6246 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6247 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6248 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6249 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6250 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6251 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6252 | } |
| 6253 | |
| 6254 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6255 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6256 | /* finished. */ |
| 6257 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6258 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6259 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6260 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6261 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6262 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6263 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6264 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6265 | |
| 6266 | /* yield. */ |
| 6267 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6268 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6269 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6270 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6271 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6272 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6273 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6274 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6275 | /* Some actions can be wake up when a "write" event |
| 6276 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6277 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6278 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6279 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6280 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6281 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6282 | s->res.analysers |= analyzer; |
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 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6285 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6286 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6287 | * because HAProxy is not able to manipulate data, it |
| 6288 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6289 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6290 | |
| 6291 | /* finished with error. */ |
| 6292 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6293 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6294 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6295 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6296 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6297 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6298 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6299 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6300 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6301 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6302 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6303 | case HLUA_E_ETMOUT: |
| 6304 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6305 | stream_int_retnclose(&s->si[0], &msg); |
| 6306 | return ACT_RET_ERR; |
| 6307 | } |
| 6308 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6309 | return 0; |
| 6310 | |
| 6311 | case HLUA_E_NOMEM: |
| 6312 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6313 | stream_int_retnclose(&s->si[0], &msg); |
| 6314 | return ACT_RET_ERR; |
| 6315 | } |
| 6316 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6317 | return 0; |
| 6318 | |
| 6319 | case HLUA_E_YIELD: |
| 6320 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6321 | stream_int_retnclose(&s->si[0], &msg); |
| 6322 | return ACT_RET_ERR; |
| 6323 | } |
| 6324 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6325 | rule->arg.hlua_rule->fcn.name); |
| 6326 | return 0; |
| 6327 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6328 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6329 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6330 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6331 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6332 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6333 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6334 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6335 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6336 | |
| 6337 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6338 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6339 | } |
| 6340 | } |
| 6341 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6342 | 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] | 6343 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6344 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6345 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6346 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6347 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6348 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6349 | } |
| 6350 | |
| 6351 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6352 | { |
| 6353 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6354 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6355 | struct task *task; |
| 6356 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6357 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6358 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6359 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6360 | if (!hlua) { |
| 6361 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6362 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6363 | return 0; |
| 6364 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6365 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6366 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6367 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6368 | |
| 6369 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6370 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6371 | if (!task) { |
| 6372 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6373 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6374 | return 0; |
| 6375 | } |
| 6376 | task->nice = 0; |
| 6377 | task->context = ctx; |
| 6378 | task->process = hlua_applet_wakeup; |
| 6379 | ctx->ctx.hlua_apptcp.task = task; |
| 6380 | |
| 6381 | /* In the execution wrappers linked with a stream, the |
| 6382 | * Lua context can be not initialized. This behavior |
| 6383 | * permits to save performances because a systematic |
| 6384 | * Lua initialization cause 5% performances loss. |
| 6385 | */ |
| 6386 | if (!hlua_ctx_init(hlua, task)) { |
| 6387 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6388 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6389 | return 0; |
| 6390 | } |
| 6391 | |
| 6392 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6393 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6394 | |
| 6395 | /* The following Lua calls can fail. */ |
| 6396 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6397 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6398 | error = lua_tostring(hlua->T, -1); |
| 6399 | else |
| 6400 | error = "critical error"; |
| 6401 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6402 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6403 | return 0; |
| 6404 | } |
| 6405 | |
| 6406 | /* Check stack available size. */ |
| 6407 | if (!lua_checkstack(hlua->T, 1)) { |
| 6408 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6409 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6410 | RESET_SAFE_LJMP(hlua->T); |
| 6411 | return 0; |
| 6412 | } |
| 6413 | |
| 6414 | /* Restore the function in the stack. */ |
| 6415 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6416 | |
| 6417 | /* Create and and push object stream in the stack. */ |
| 6418 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6419 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6420 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6421 | RESET_SAFE_LJMP(hlua->T); |
| 6422 | return 0; |
| 6423 | } |
| 6424 | hlua->nargs = 1; |
| 6425 | |
| 6426 | /* push keywords in the stack. */ |
| 6427 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6428 | if (!lua_checkstack(hlua->T, 1)) { |
| 6429 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6430 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6431 | RESET_SAFE_LJMP(hlua->T); |
| 6432 | return 0; |
| 6433 | } |
| 6434 | lua_pushstring(hlua->T, *arg); |
| 6435 | hlua->nargs++; |
| 6436 | } |
| 6437 | |
| 6438 | RESET_SAFE_LJMP(hlua->T); |
| 6439 | |
| 6440 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6441 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6442 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6443 | |
| 6444 | return 1; |
| 6445 | } |
| 6446 | |
| 6447 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6448 | { |
| 6449 | struct stream_interface *si = ctx->owner; |
| 6450 | struct stream *strm = si_strm(si); |
| 6451 | struct channel *res = si_ic(si); |
| 6452 | struct act_rule *rule = ctx->rule; |
| 6453 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6454 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6455 | |
| 6456 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6457 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6458 | /* eat the whole request */ |
| 6459 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6460 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6461 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6462 | |
| 6463 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6464 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6465 | return; |
| 6466 | |
| 6467 | /* Execute the function. */ |
| 6468 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6469 | /* finished. */ |
| 6470 | case HLUA_E_OK: |
| 6471 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6472 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6473 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6474 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6475 | res->flags |= CF_READ_NULL; |
| 6476 | si_shutr(si); |
| 6477 | return; |
| 6478 | |
| 6479 | /* yield. */ |
| 6480 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6481 | if (hlua->wake_time != TICK_ETERNITY) |
| 6482 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6483 | return; |
| 6484 | |
| 6485 | /* finished with error. */ |
| 6486 | case HLUA_E_ERRMSG: |
| 6487 | /* Display log. */ |
| 6488 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6489 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6490 | lua_pop(hlua->T, 1); |
| 6491 | goto error; |
| 6492 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6493 | case HLUA_E_ETMOUT: |
| 6494 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6495 | rule->arg.hlua_rule->fcn.name); |
| 6496 | goto error; |
| 6497 | |
| 6498 | case HLUA_E_NOMEM: |
| 6499 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6500 | rule->arg.hlua_rule->fcn.name); |
| 6501 | goto error; |
| 6502 | |
| 6503 | case HLUA_E_YIELD: /* unexpected */ |
| 6504 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6505 | rule->arg.hlua_rule->fcn.name); |
| 6506 | goto error; |
| 6507 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6508 | case HLUA_E_ERR: |
| 6509 | /* Display log. */ |
| 6510 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6511 | rule->arg.hlua_rule->fcn.name); |
| 6512 | goto error; |
| 6513 | |
| 6514 | default: |
| 6515 | goto error; |
| 6516 | } |
| 6517 | |
| 6518 | error: |
| 6519 | |
| 6520 | /* For all other cases, just close the stream. */ |
| 6521 | si_shutw(si); |
| 6522 | si_shutr(si); |
| 6523 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6524 | } |
| 6525 | |
| 6526 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6527 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6528 | task_delete(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6529 | task_free(ctx->ctx.hlua_apptcp.task); |
| 6530 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6531 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6532 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6533 | } |
| 6534 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6535 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6536 | * an errors occurs and -1 if more data are required for initializing |
| 6537 | * the applet. |
| 6538 | */ |
| 6539 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6540 | { |
| 6541 | struct stream_interface *si = ctx->owner; |
| 6542 | struct channel *req = si_oc(si); |
| 6543 | struct http_msg *msg; |
| 6544 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6545 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6546 | char **arg; |
| 6547 | struct hdr_ctx hdr; |
| 6548 | struct task *task; |
| 6549 | 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] | 6550 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6551 | |
| 6552 | /* Wait for a full HTTP request. */ |
| 6553 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 6554 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 6555 | return -1; |
| 6556 | return 0; |
| 6557 | } |
| 6558 | txn = strm->txn; |
| 6559 | msg = &txn->req; |
| 6560 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6561 | /* We want two things in HTTP mode : |
| 6562 | * - enforce server-close mode if we were in keep-alive, so that the |
| 6563 | * applet is released after each response ; |
| 6564 | * - enable request body transfer to the applet in order to resync |
| 6565 | * with the response body. |
| 6566 | */ |
| 6567 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 6568 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6569 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6570 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6571 | if (!hlua) { |
| 6572 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6573 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6574 | return 0; |
| 6575 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6576 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6577 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6578 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6579 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6580 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6581 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6582 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6583 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6584 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6585 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6586 | if (!task) { |
| 6587 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6588 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6589 | return 0; |
| 6590 | } |
| 6591 | task->nice = 0; |
| 6592 | task->context = ctx; |
| 6593 | task->process = hlua_applet_wakeup; |
| 6594 | ctx->ctx.hlua_apphttp.task = task; |
| 6595 | |
| 6596 | /* In the execution wrappers linked with a stream, the |
| 6597 | * Lua context can be not initialized. This behavior |
| 6598 | * permits to save performances because a systematic |
| 6599 | * Lua initialization cause 5% performances loss. |
| 6600 | */ |
| 6601 | if (!hlua_ctx_init(hlua, task)) { |
| 6602 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6603 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6604 | return 0; |
| 6605 | } |
| 6606 | |
| 6607 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6608 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6609 | |
| 6610 | /* The following Lua calls can fail. */ |
| 6611 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6612 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6613 | error = lua_tostring(hlua->T, -1); |
| 6614 | else |
| 6615 | error = "critical error"; |
| 6616 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6617 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6618 | return 0; |
| 6619 | } |
| 6620 | |
| 6621 | /* Check stack available size. */ |
| 6622 | if (!lua_checkstack(hlua->T, 1)) { |
| 6623 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6624 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6625 | RESET_SAFE_LJMP(hlua->T); |
| 6626 | return 0; |
| 6627 | } |
| 6628 | |
| 6629 | /* Restore the function in the stack. */ |
| 6630 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6631 | |
| 6632 | /* Create and and push object stream in the stack. */ |
| 6633 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6634 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6635 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6636 | RESET_SAFE_LJMP(hlua->T); |
| 6637 | return 0; |
| 6638 | } |
| 6639 | hlua->nargs = 1; |
| 6640 | |
| 6641 | /* Look for a 100-continue expected. */ |
| 6642 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 6643 | hdr.idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6644 | 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] | 6645 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 6646 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 6647 | } |
| 6648 | |
| 6649 | /* push keywords in the stack. */ |
| 6650 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6651 | if (!lua_checkstack(hlua->T, 1)) { |
| 6652 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6653 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6654 | RESET_SAFE_LJMP(hlua->T); |
| 6655 | return 0; |
| 6656 | } |
| 6657 | lua_pushstring(hlua->T, *arg); |
| 6658 | hlua->nargs++; |
| 6659 | } |
| 6660 | |
| 6661 | RESET_SAFE_LJMP(hlua->T); |
| 6662 | |
| 6663 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6664 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6665 | |
| 6666 | return 1; |
| 6667 | } |
| 6668 | |
| 6669 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 6670 | { |
| 6671 | struct stream_interface *si = ctx->owner; |
| 6672 | struct stream *strm = si_strm(si); |
| 6673 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6674 | struct act_rule *rule = ctx->rule; |
| 6675 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6676 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 6677 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 6678 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 6679 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 6680 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6681 | int ret; |
| 6682 | |
| 6683 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6684 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6685 | return; |
| 6686 | |
| 6687 | /* Set the currently running flag. */ |
| 6688 | if (!HLUA_IS_RUNNING(hlua) && |
| 6689 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6690 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6691 | /* Wait for full HTTP analysys. */ |
| 6692 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6693 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6694 | return; |
| 6695 | } |
| 6696 | |
| 6697 | /* Store the max amount of bytes that we can read. */ |
| 6698 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 6699 | |
| 6700 | /* We need to flush the request header. This left the body |
| 6701 | * for the Lua. |
| 6702 | */ |
| 6703 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6704 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6705 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6706 | if (ret == -1) |
| 6707 | return; |
| 6708 | |
| 6709 | /* No data available, ask for more data. */ |
| 6710 | if (ret == 1) |
| 6711 | len2 = 0; |
| 6712 | if (ret == 0) |
| 6713 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 6714 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
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 | return; |
| 6717 | } |
| 6718 | |
| 6719 | /* skip the requests bytes. */ |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 6720 | 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] | 6721 | } |
| 6722 | |
| 6723 | /* Executes The applet if it is not done. */ |
| 6724 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6725 | |
| 6726 | /* Execute the function. */ |
| 6727 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6728 | /* finished. */ |
| 6729 | case HLUA_E_OK: |
| 6730 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6731 | break; |
| 6732 | |
| 6733 | /* yield. */ |
| 6734 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6735 | if (hlua->wake_time != TICK_ETERNITY) |
| 6736 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6737 | return; |
| 6738 | |
| 6739 | /* finished with error. */ |
| 6740 | case HLUA_E_ERRMSG: |
| 6741 | /* Display log. */ |
| 6742 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6743 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6744 | lua_pop(hlua->T, 1); |
| 6745 | goto error; |
| 6746 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6747 | case HLUA_E_ETMOUT: |
| 6748 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 6749 | rule->arg.hlua_rule->fcn.name); |
| 6750 | goto error; |
| 6751 | |
| 6752 | case HLUA_E_NOMEM: |
| 6753 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 6754 | rule->arg.hlua_rule->fcn.name); |
| 6755 | goto error; |
| 6756 | |
| 6757 | case HLUA_E_YIELD: /* unexpected */ |
| 6758 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 6759 | rule->arg.hlua_rule->fcn.name); |
| 6760 | goto error; |
| 6761 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6762 | case HLUA_E_ERR: |
| 6763 | /* Display log. */ |
| 6764 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6765 | rule->arg.hlua_rule->fcn.name); |
| 6766 | goto error; |
| 6767 | |
| 6768 | default: |
| 6769 | goto error; |
| 6770 | } |
| 6771 | } |
| 6772 | |
| 6773 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 6774 | |
| 6775 | /* We must send the final chunk. */ |
| 6776 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 6777 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 6778 | |
| 6779 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6780 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6781 | |
| 6782 | /* critical error. */ |
| 6783 | if (ret == -2 || ret == -3) { |
| 6784 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 6785 | rule->arg.hlua_rule->fcn.name); |
| 6786 | goto error; |
| 6787 | } |
| 6788 | |
| 6789 | /* no enough space error. */ |
| 6790 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6791 | si_rx_room_blk(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6792 | return; |
| 6793 | } |
| 6794 | |
| 6795 | /* set the last chunk sent. */ |
| 6796 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6797 | } |
| 6798 | |
| 6799 | /* close the connection. */ |
| 6800 | |
Frédéric Lécaille | 83ed5d5 | 2018-07-18 14:25:26 +0200 | [diff] [blame] | 6801 | /* status */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6802 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6803 | |
| 6804 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6805 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6806 | res->flags |= CF_READ_NULL; |
| 6807 | si_shutr(si); |
| 6808 | |
| 6809 | return; |
| 6810 | } |
| 6811 | |
| 6812 | error: |
| 6813 | |
| 6814 | /* If we are in HTTP mode, and we are not send any |
| 6815 | * data, return a 500 server error in best effort: |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6816 | * if there is no room available in the buffer, |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6817 | * just close the connection. |
| 6818 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6819 | ci_putblk(res, error_500, strlen(error_500)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6820 | if (!(strm->flags & SF_ERR_MASK)) |
| 6821 | strm->flags |= SF_ERR_RESOURCE; |
| 6822 | si_shutw(si); |
| 6823 | si_shutr(si); |
| 6824 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6825 | } |
| 6826 | |
| 6827 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6828 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6829 | task_delete(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6830 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6831 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6832 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6833 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6834 | } |
| 6835 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6836 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6837 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6838 | * |
| 6839 | * This function can fail with an abort() due to an Lua critical error. |
| 6840 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6841 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6842 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6843 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6844 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6845 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6846 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6847 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6848 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6849 | if (px->options2 & PR_O2_USE_HTX) { |
| 6850 | memprintf(err, "Lua actions cannot be used when the HTX internal representation is enabled"); |
| 6851 | return ACT_RET_PRS_ERR; |
| 6852 | } |
| 6853 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6854 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6855 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6856 | if (!rule->arg.hlua_rule) { |
| 6857 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6858 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6859 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6860 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6861 | /* Memory for arguments. */ |
| 6862 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6863 | if (!rule->arg.hlua_rule->args) { |
| 6864 | memprintf(err, "out of memory error"); |
| 6865 | return ACT_RET_PRS_ERR; |
| 6866 | } |
| 6867 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6868 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6869 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6870 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6871 | /* Expect some arguments */ |
| 6872 | for (i = 0; i < fcn->nargs; i++) { |
| 6873 | if (*args[i+1] == '\0') { |
| 6874 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6875 | return ACT_RET_PRS_ERR; |
| 6876 | } |
| 6877 | rule->arg.hlua_rule->args[i] = strdup(args[i + 1]); |
| 6878 | if (!rule->arg.hlua_rule->args[i]) { |
| 6879 | memprintf(err, "out of memory error"); |
| 6880 | return ACT_RET_PRS_ERR; |
| 6881 | } |
| 6882 | (*cur_arg)++; |
| 6883 | } |
| 6884 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6885 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6886 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6887 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6888 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6889 | } |
| 6890 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6891 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6892 | struct act_rule *rule, char **err) |
| 6893 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6894 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6895 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6896 | if (px->options2 & PR_O2_USE_HTX) { |
| 6897 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 6898 | return ACT_RET_PRS_ERR; |
| 6899 | } |
| 6900 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6901 | /* HTTP applets are forbidden in tcp-request rules. |
| 6902 | * HTTP applet request requires everything initilized by |
| 6903 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6904 | * The applet will be immediately initilized, but its before |
| 6905 | * the call of this analyzer. |
| 6906 | */ |
| 6907 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6908 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6909 | return ACT_RET_PRS_ERR; |
| 6910 | } |
| 6911 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6912 | /* Memory for the rule. */ |
| 6913 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6914 | if (!rule->arg.hlua_rule) { |
| 6915 | memprintf(err, "out of memory error"); |
| 6916 | return ACT_RET_PRS_ERR; |
| 6917 | } |
| 6918 | |
| 6919 | /* Reference the Lua function and store the reference. */ |
| 6920 | rule->arg.hlua_rule->fcn = *fcn; |
| 6921 | |
| 6922 | /* TODO: later accept arguments. */ |
| 6923 | rule->arg.hlua_rule->args = NULL; |
| 6924 | |
| 6925 | /* Add applet pointer in the rule. */ |
| 6926 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6927 | rule->applet.name = fcn->name; |
| 6928 | rule->applet.init = hlua_applet_http_init; |
| 6929 | rule->applet.fct = hlua_applet_http_fct; |
| 6930 | rule->applet.release = hlua_applet_http_release; |
| 6931 | rule->applet.timeout = hlua_timeout_applet; |
| 6932 | |
| 6933 | return ACT_RET_PRS_OK; |
| 6934 | } |
| 6935 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6936 | /* This function is an LUA binding used for registering |
| 6937 | * "sample-conv" functions. It expects a converter name used |
| 6938 | * in the haproxy configuration file, and an LUA function. |
| 6939 | */ |
| 6940 | __LJMP static int hlua_register_action(lua_State *L) |
| 6941 | { |
| 6942 | struct action_kw_list *akl; |
| 6943 | const char *name; |
| 6944 | int ref; |
| 6945 | int len; |
| 6946 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6947 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6948 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6949 | /* Initialise the number of expected arguments at 0. */ |
| 6950 | nargs = 0; |
| 6951 | |
| 6952 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6953 | 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] | 6954 | |
| 6955 | /* First argument : converter name. */ |
| 6956 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6957 | |
| 6958 | /* Second argument : environment. */ |
| 6959 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6960 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6961 | |
| 6962 | /* Third argument : lua function. */ |
| 6963 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6964 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6965 | /* 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] | 6966 | if (lua_gettop(L) >= 4) |
| 6967 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6968 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6969 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6970 | lua_pushnil(L); |
| 6971 | while (lua_next(L, 2) != 0) { |
| 6972 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6973 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6974 | |
| 6975 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6976 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6977 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6978 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6979 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6980 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6981 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6982 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6983 | |
| 6984 | /* Fill fcn. */ |
| 6985 | fcn->name = strdup(name); |
| 6986 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6987 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6988 | fcn->function_ref = ref; |
| 6989 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6990 | /* Set the expected number od arguments. */ |
| 6991 | fcn->nargs = nargs; |
| 6992 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6993 | /* List head */ |
| 6994 | akl->list.n = akl->list.p = NULL; |
| 6995 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6996 | /* action keyword. */ |
| 6997 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6998 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6999 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7000 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7001 | |
| 7002 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7003 | |
| 7004 | akl->kw[0].match_pfx = 0; |
| 7005 | akl->kw[0].private = fcn; |
| 7006 | akl->kw[0].parse = action_register_lua; |
| 7007 | |
| 7008 | /* select the action registering point. */ |
| 7009 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7010 | tcp_req_cont_keywords_register(akl); |
| 7011 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7012 | tcp_res_cont_keywords_register(akl); |
| 7013 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7014 | http_req_keywords_register(akl); |
| 7015 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7016 | http_res_keywords_register(akl); |
| 7017 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7018 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7019 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7020 | "are expected.", lua_tostring(L, -1))); |
| 7021 | |
| 7022 | /* pop the environment string. */ |
| 7023 | lua_pop(L, 1); |
| 7024 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7025 | return ACT_RET_PRS_OK; |
| 7026 | } |
| 7027 | |
| 7028 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7029 | struct act_rule *rule, char **err) |
| 7030 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7031 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7032 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7033 | if (px->options2 & PR_O2_USE_HTX) { |
| 7034 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 7035 | return ACT_RET_PRS_ERR; |
| 7036 | } |
| 7037 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7038 | /* Memory for the rule. */ |
| 7039 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7040 | if (!rule->arg.hlua_rule) { |
| 7041 | memprintf(err, "out of memory error"); |
| 7042 | return ACT_RET_PRS_ERR; |
| 7043 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7044 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7045 | /* Reference the Lua function and store the reference. */ |
| 7046 | rule->arg.hlua_rule->fcn = *fcn; |
| 7047 | |
| 7048 | /* TODO: later accept arguments. */ |
| 7049 | rule->arg.hlua_rule->args = NULL; |
| 7050 | |
| 7051 | /* Add applet pointer in the rule. */ |
| 7052 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7053 | rule->applet.name = fcn->name; |
| 7054 | rule->applet.init = hlua_applet_tcp_init; |
| 7055 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7056 | rule->applet.release = hlua_applet_tcp_release; |
| 7057 | rule->applet.timeout = hlua_timeout_applet; |
| 7058 | |
| 7059 | return 0; |
| 7060 | } |
| 7061 | |
| 7062 | /* This function is an LUA binding used for registering |
| 7063 | * "sample-conv" functions. It expects a converter name used |
| 7064 | * in the haproxy configuration file, and an LUA function. |
| 7065 | */ |
| 7066 | __LJMP static int hlua_register_service(lua_State *L) |
| 7067 | { |
| 7068 | struct action_kw_list *akl; |
| 7069 | const char *name; |
| 7070 | const char *env; |
| 7071 | int ref; |
| 7072 | int len; |
| 7073 | struct hlua_function *fcn; |
| 7074 | |
| 7075 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7076 | |
| 7077 | /* First argument : converter name. */ |
| 7078 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7079 | |
| 7080 | /* Second argument : environment. */ |
| 7081 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7082 | |
| 7083 | /* Third argument : lua function. */ |
| 7084 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7085 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7086 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7087 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7088 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7089 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7090 | fcn = calloc(1, sizeof(*fcn)); |
| 7091 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7092 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7093 | |
| 7094 | /* Fill fcn. */ |
| 7095 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7096 | fcn->name = calloc(1, len); |
| 7097 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7098 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7099 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7100 | fcn->function_ref = ref; |
| 7101 | |
| 7102 | /* List head */ |
| 7103 | akl->list.n = akl->list.p = NULL; |
| 7104 | |
| 7105 | /* converter keyword. */ |
| 7106 | len = strlen("lua.") + strlen(name) + 1; |
| 7107 | akl->kw[0].kw = calloc(1, len); |
| 7108 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7109 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7110 | |
| 7111 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7112 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7113 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7114 | if (strcmp(env, "tcp") == 0) |
| 7115 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7116 | else if (strcmp(env, "http") == 0) |
| 7117 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7118 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7119 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7120 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7121 | |
| 7122 | akl->kw[0].match_pfx = 0; |
| 7123 | akl->kw[0].private = fcn; |
| 7124 | |
| 7125 | /* End of array. */ |
| 7126 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7127 | |
| 7128 | /* Register this new converter */ |
| 7129 | service_keywords_register(akl); |
| 7130 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7131 | return 0; |
| 7132 | } |
| 7133 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7134 | /* This function initialises Lua cli handler. It copies the |
| 7135 | * arguments in the Lua stack and create channel IO objects. |
| 7136 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7137 | 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] | 7138 | { |
| 7139 | struct hlua *hlua; |
| 7140 | struct hlua_function *fcn; |
| 7141 | int i; |
| 7142 | const char *error; |
| 7143 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7144 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7145 | appctx->ctx.hlua_cli.fcn = private; |
| 7146 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7147 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7148 | if (!hlua) { |
| 7149 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7150 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7151 | } |
| 7152 | HLUA_INIT(hlua); |
| 7153 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7154 | |
| 7155 | /* Create task used by signal to wakeup applets. |
| 7156 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7157 | * applet_http. It is absolutely compatible. |
| 7158 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7159 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7160 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7161 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7162 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7163 | } |
| 7164 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7165 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7166 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7167 | |
| 7168 | /* Initialises the Lua context */ |
| 7169 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 7170 | 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] | 7171 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7172 | } |
| 7173 | |
| 7174 | /* The following Lua calls can fail. */ |
| 7175 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7176 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7177 | error = lua_tostring(hlua->T, -1); |
| 7178 | else |
| 7179 | error = "critical error"; |
| 7180 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7181 | goto error; |
| 7182 | } |
| 7183 | |
| 7184 | /* Check stack available size. */ |
| 7185 | if (!lua_checkstack(hlua->T, 2)) { |
| 7186 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7187 | goto error; |
| 7188 | } |
| 7189 | |
| 7190 | /* Restore the function in the stack. */ |
| 7191 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7192 | |
| 7193 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7194 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7195 | */ |
| 7196 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7197 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7198 | goto error; |
| 7199 | } |
| 7200 | hlua->nargs = 1; |
| 7201 | |
| 7202 | /* push keywords in the stack. */ |
| 7203 | for (i = 0; *args[i]; i++) { |
| 7204 | /* Check stack available size. */ |
| 7205 | if (!lua_checkstack(hlua->T, 1)) { |
| 7206 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7207 | goto error; |
| 7208 | } |
| 7209 | lua_pushstring(hlua->T, args[i]); |
| 7210 | hlua->nargs++; |
| 7211 | } |
| 7212 | |
| 7213 | /* We must initialize the execution timeouts. */ |
| 7214 | hlua->max_time = hlua_timeout_session; |
| 7215 | |
| 7216 | /* At this point the execution is safe. */ |
| 7217 | RESET_SAFE_LJMP(hlua->T); |
| 7218 | |
| 7219 | /* It's ok */ |
| 7220 | return 0; |
| 7221 | |
| 7222 | /* It's not ok. */ |
| 7223 | error: |
| 7224 | RESET_SAFE_LJMP(hlua->T); |
| 7225 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7226 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7227 | return 1; |
| 7228 | } |
| 7229 | |
| 7230 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7231 | { |
| 7232 | struct hlua *hlua; |
| 7233 | struct stream_interface *si; |
| 7234 | struct hlua_function *fcn; |
| 7235 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7236 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7237 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7238 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7239 | |
| 7240 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7241 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7242 | return 1; |
| 7243 | |
| 7244 | /* Execute the function. */ |
| 7245 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7246 | |
| 7247 | /* finished. */ |
| 7248 | case HLUA_E_OK: |
| 7249 | return 1; |
| 7250 | |
| 7251 | /* yield. */ |
| 7252 | case HLUA_E_AGAIN: |
| 7253 | /* We want write. */ |
| 7254 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7255 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7256 | /* Set the timeout. */ |
| 7257 | if (hlua->wake_time != TICK_ETERNITY) |
| 7258 | task_schedule(hlua->task, hlua->wake_time); |
| 7259 | return 0; |
| 7260 | |
| 7261 | /* finished with error. */ |
| 7262 | case HLUA_E_ERRMSG: |
| 7263 | /* Display log. */ |
| 7264 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7265 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7266 | lua_pop(hlua->T, 1); |
| 7267 | return 1; |
| 7268 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7269 | case HLUA_E_ETMOUT: |
| 7270 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7271 | fcn->name); |
| 7272 | return 1; |
| 7273 | |
| 7274 | case HLUA_E_NOMEM: |
| 7275 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7276 | fcn->name); |
| 7277 | return 1; |
| 7278 | |
| 7279 | case HLUA_E_YIELD: /* unexpected */ |
| 7280 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7281 | fcn->name); |
| 7282 | return 1; |
| 7283 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7284 | case HLUA_E_ERR: |
| 7285 | /* Display log. */ |
| 7286 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7287 | fcn->name); |
| 7288 | return 1; |
| 7289 | |
| 7290 | default: |
| 7291 | return 1; |
| 7292 | } |
| 7293 | |
| 7294 | return 1; |
| 7295 | } |
| 7296 | |
| 7297 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7298 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7299 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7300 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7301 | } |
| 7302 | |
| 7303 | /* This function is an LUA binding used for registering |
| 7304 | * new keywords in the cli. It expects a list of keywords |
| 7305 | * which are the "path". It is limited to 5 keywords. A |
| 7306 | * description of the command, a function to be executed |
| 7307 | * for the parsing and a function for io handlers. |
| 7308 | */ |
| 7309 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7310 | { |
| 7311 | struct cli_kw_list *cli_kws; |
| 7312 | const char *message; |
| 7313 | int ref_io; |
| 7314 | int len; |
| 7315 | struct hlua_function *fcn; |
| 7316 | int index; |
| 7317 | int i; |
| 7318 | |
| 7319 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7320 | |
| 7321 | /* First argument : an array of maximum 5 keywords. */ |
| 7322 | if (!lua_istable(L, 1)) |
| 7323 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7324 | |
| 7325 | /* Second argument : string with contextual message. */ |
| 7326 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7327 | |
| 7328 | /* Third and fourth argument : lua function. */ |
| 7329 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7330 | |
| 7331 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7332 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7333 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7334 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7335 | fcn = calloc(1, sizeof(*fcn)); |
| 7336 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7337 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7338 | |
| 7339 | /* Fill path. */ |
| 7340 | index = 0; |
| 7341 | lua_pushnil(L); |
| 7342 | while(lua_next(L, 1) != 0) { |
| 7343 | if (index >= 5) |
| 7344 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7345 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7346 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7347 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7348 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7349 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7350 | index++; |
| 7351 | lua_pop(L, 1); |
| 7352 | } |
| 7353 | |
| 7354 | /* Copy help message. */ |
| 7355 | cli_kws->kw[0].usage = strdup(message); |
| 7356 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7357 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7358 | |
| 7359 | /* Fill fcn io handler. */ |
| 7360 | len = strlen("<lua.cli>") + 1; |
| 7361 | for (i = 0; i < index; i++) |
| 7362 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7363 | fcn->name = calloc(1, len); |
| 7364 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7365 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7366 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7367 | for (i = 0; i < index; i++) { |
| 7368 | strncat((char *)fcn->name, ".", len); |
| 7369 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7370 | } |
| 7371 | strncat((char *)fcn->name, ">", len); |
| 7372 | fcn->function_ref = ref_io; |
| 7373 | |
| 7374 | /* Fill last entries. */ |
| 7375 | cli_kws->kw[0].private = fcn; |
| 7376 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7377 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7378 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7379 | |
| 7380 | /* Register this new converter */ |
| 7381 | cli_register_kw(cli_kws); |
| 7382 | |
| 7383 | return 0; |
| 7384 | } |
| 7385 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7386 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7387 | struct proxy *defpx, const char *file, int line, |
| 7388 | char **err, unsigned int *timeout) |
| 7389 | { |
| 7390 | const char *error; |
| 7391 | |
| 7392 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 7393 | if (error && *error != '\0') { |
| 7394 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7395 | return -1; |
| 7396 | } |
| 7397 | return 0; |
| 7398 | } |
| 7399 | |
| 7400 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7401 | struct proxy *defpx, const char *file, int line, |
| 7402 | char **err) |
| 7403 | { |
| 7404 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7405 | file, line, err, &hlua_timeout_session); |
| 7406 | } |
| 7407 | |
| 7408 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7409 | struct proxy *defpx, const char *file, int line, |
| 7410 | char **err) |
| 7411 | { |
| 7412 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7413 | file, line, err, &hlua_timeout_task); |
| 7414 | } |
| 7415 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7416 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7417 | struct proxy *defpx, const char *file, int line, |
| 7418 | char **err) |
| 7419 | { |
| 7420 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7421 | file, line, err, &hlua_timeout_applet); |
| 7422 | } |
| 7423 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7424 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7425 | struct proxy *defpx, const char *file, int line, |
| 7426 | char **err) |
| 7427 | { |
| 7428 | char *error; |
| 7429 | |
| 7430 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7431 | if (*error != '\0') { |
| 7432 | memprintf(err, "%s: invalid number", args[0]); |
| 7433 | return -1; |
| 7434 | } |
| 7435 | return 0; |
| 7436 | } |
| 7437 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7438 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7439 | struct proxy *defpx, const char *file, int line, |
| 7440 | char **err) |
| 7441 | { |
| 7442 | char *error; |
| 7443 | |
| 7444 | if (*(args[1]) == 0) { |
| 7445 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7446 | return -1; |
| 7447 | } |
| 7448 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7449 | if (*error != '\0') { |
| 7450 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7451 | return -1; |
| 7452 | } |
| 7453 | return 0; |
| 7454 | } |
| 7455 | |
| 7456 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7457 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7458 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7459 | * the main lua entry point. |
| 7460 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7461 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7462 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7463 | * |
| 7464 | * In some error case, LUA set an error message in top of the stack. This function |
| 7465 | * 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] | 7466 | * |
| 7467 | * This function can fail with an abort() due to an Lua critical error. |
| 7468 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7469 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7470 | */ |
| 7471 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7472 | struct proxy *defpx, const char *file, int line, |
| 7473 | char **err) |
| 7474 | { |
| 7475 | int error; |
| 7476 | |
| 7477 | /* Just load and compile the file. */ |
| 7478 | error = luaL_loadfile(gL.T, args[1]); |
| 7479 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7480 | 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] | 7481 | lua_pop(gL.T, 1); |
| 7482 | return -1; |
| 7483 | } |
| 7484 | |
| 7485 | /* If no syntax error where detected, execute the code. */ |
| 7486 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7487 | switch (error) { |
| 7488 | case LUA_OK: |
| 7489 | break; |
| 7490 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7491 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7492 | lua_pop(gL.T, 1); |
| 7493 | return -1; |
| 7494 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7495 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7496 | return -1; |
| 7497 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7498 | 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] | 7499 | lua_pop(gL.T, 1); |
| 7500 | return -1; |
| 7501 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7502 | 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] | 7503 | lua_pop(gL.T, 1); |
| 7504 | return -1; |
| 7505 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7506 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7507 | lua_pop(gL.T, 1); |
| 7508 | return -1; |
| 7509 | } |
| 7510 | |
| 7511 | return 0; |
| 7512 | } |
| 7513 | |
| 7514 | /* configuration keywords declaration */ |
| 7515 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7516 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7517 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7518 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7519 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7520 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7521 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7522 | { 0, NULL, NULL }, |
| 7523 | }}; |
| 7524 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7525 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 7526 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7527 | static int hlua_check_config() |
| 7528 | { |
| 7529 | struct proxy *px; |
| 7530 | struct acl *acl; |
| 7531 | struct acl_expr *expr; |
| 7532 | struct sample_fetch *fetch; |
| 7533 | int err = 0; |
| 7534 | |
| 7535 | for (px = proxies_list; px; px = px->next) { |
| 7536 | if (!(px->options2 & PR_O2_USE_HTX)) |
| 7537 | continue; |
| 7538 | |
| 7539 | list_for_each_entry(acl, &px->acl, list) { |
| 7540 | list_for_each_entry(expr, &acl->expr, list) { |
| 7541 | fetch = expr->smp->fetch; |
| 7542 | if (fetch->process != hlua_sample_fetch_wrapper) |
| 7543 | continue; |
| 7544 | |
| 7545 | ha_alert("config: %s '%s': sample-fetch '%s' cannot be used used " |
| 7546 | "when the HTX internal representation is enabled.\n", |
| 7547 | proxy_type_str(px), px->id, fetch->kw); |
| 7548 | err++; |
| 7549 | } |
| 7550 | } |
| 7551 | } |
| 7552 | return err; |
| 7553 | } |
| 7554 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7555 | /* This function can fail with an abort() due to an Lua critical error. |
| 7556 | * We are in the initialisation process of HAProxy, this abort() is |
| 7557 | * tolerated. |
| 7558 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7559 | int hlua_post_init() |
| 7560 | { |
| 7561 | struct hlua_init_function *init; |
| 7562 | const char *msg; |
| 7563 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7564 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7565 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7566 | /* Call post initialisation function in safe environement. */ |
| 7567 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7568 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7569 | error = lua_tostring(gL.T, -1); |
| 7570 | else |
| 7571 | error = "critical error"; |
| 7572 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7573 | exit(1); |
| 7574 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7575 | |
| 7576 | #if USE_OPENSSL |
| 7577 | /* Initialize SSL server. */ |
| 7578 | if (socket_ssl.xprt->prepare_srv) { |
| 7579 | int saved_used_backed = global.ssl_used_backend; |
| 7580 | // don't affect maxconn automatic computation |
| 7581 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7582 | global.ssl_used_backend = saved_used_backed; |
| 7583 | } |
| 7584 | #endif |
| 7585 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7586 | hlua_fcn_post_init(gL.T); |
| 7587 | RESET_SAFE_LJMP(gL.T); |
| 7588 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7589 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7590 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7591 | ret = hlua_ctx_resume(&gL, 0); |
| 7592 | switch (ret) { |
| 7593 | case HLUA_E_OK: |
| 7594 | lua_pop(gL.T, -1); |
| 7595 | return 1; |
| 7596 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7597 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7598 | return 0; |
| 7599 | case HLUA_E_ERRMSG: |
| 7600 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7601 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7602 | return 0; |
| 7603 | case HLUA_E_ERR: |
| 7604 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7605 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7606 | return 0; |
| 7607 | } |
| 7608 | } |
| 7609 | return 1; |
| 7610 | } |
| 7611 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7612 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7613 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7614 | * is the previously allocated size or the kind of object in case of a new |
| 7615 | * allocation. <nsize> is the requested new size. |
| 7616 | */ |
| 7617 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7618 | { |
| 7619 | struct hlua_mem_allocator *zone = ud; |
| 7620 | |
| 7621 | if (nsize == 0) { |
| 7622 | /* it's a free */ |
| 7623 | if (ptr) |
| 7624 | zone->allocated -= osize; |
| 7625 | free(ptr); |
| 7626 | return NULL; |
| 7627 | } |
| 7628 | |
| 7629 | if (!ptr) { |
| 7630 | /* it's a new allocation */ |
| 7631 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7632 | return NULL; |
| 7633 | |
| 7634 | ptr = malloc(nsize); |
| 7635 | if (ptr) |
| 7636 | zone->allocated += nsize; |
| 7637 | return ptr; |
| 7638 | } |
| 7639 | |
| 7640 | /* it's a realloc */ |
| 7641 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7642 | return NULL; |
| 7643 | |
| 7644 | ptr = realloc(ptr, nsize); |
| 7645 | if (ptr) |
| 7646 | zone->allocated += nsize - osize; |
| 7647 | return ptr; |
| 7648 | } |
| 7649 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7650 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7651 | * We are in the initialisation process of HAProxy, this abort() is |
| 7652 | * tolerated. |
| 7653 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7654 | void hlua_init(void) |
| 7655 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7656 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7657 | int idx; |
| 7658 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7659 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7660 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7661 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7662 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7663 | struct srv_kw *kw; |
| 7664 | int tmp_error; |
| 7665 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7666 | char *args[] = { /* SSL client configuration. */ |
| 7667 | "ssl", |
| 7668 | "verify", |
| 7669 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7670 | NULL |
| 7671 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7672 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7673 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7674 | /* Init main lua stack. */ |
| 7675 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7676 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7677 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7678 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7679 | hlua_sethlua(&gL); |
| 7680 | gL.Tref = LUA_REFNIL; |
| 7681 | gL.task = NULL; |
| 7682 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7683 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7684 | * the Lua function can fail with an abort. We are in the initialisation |
| 7685 | * process of HAProxy, this abort() is tolerated. |
| 7686 | */ |
| 7687 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7688 | /* Initialise lua. */ |
| 7689 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7690 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7691 | /* Set safe environment for the initialisation. */ |
| 7692 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7693 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7694 | error_msg = lua_tostring(gL.T, -1); |
| 7695 | else |
| 7696 | error_msg = "critical error"; |
| 7697 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7698 | exit(1); |
| 7699 | } |
| 7700 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7701 | /* |
| 7702 | * |
| 7703 | * Create "core" object. |
| 7704 | * |
| 7705 | */ |
| 7706 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7707 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7708 | lua_newtable(gL.T); |
| 7709 | |
| 7710 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7711 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7712 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7713 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7714 | /* Register special functions. */ |
| 7715 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7716 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7717 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7718 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7719 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7720 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7721 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7722 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7723 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7724 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7725 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7726 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7727 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7728 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7729 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7730 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7731 | hlua_class_function(gL.T, "log", hlua_log); |
| 7732 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7733 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7734 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7735 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7736 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7737 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7738 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7739 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7740 | |
| 7741 | /* |
| 7742 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7743 | * Register class Map |
| 7744 | * |
| 7745 | */ |
| 7746 | |
| 7747 | /* This table entry is the object "Map" base. */ |
| 7748 | lua_newtable(gL.T); |
| 7749 | |
| 7750 | /* register pattern types. */ |
| 7751 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7752 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7753 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7754 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 7755 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7756 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7757 | |
| 7758 | /* register constructor. */ |
| 7759 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7760 | |
| 7761 | /* Create and fill the metatable. */ |
| 7762 | lua_newtable(gL.T); |
| 7763 | |
| 7764 | /* Create and fille the __index entry. */ |
| 7765 | lua_pushstring(gL.T, "__index"); |
| 7766 | lua_newtable(gL.T); |
| 7767 | |
| 7768 | /* Register . */ |
| 7769 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7770 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7771 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7772 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7773 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7774 | /* Register previous table in the registry with reference and named entry. |
| 7775 | * The function hlua_register_metatable() pops the stack, so we |
| 7776 | * previously create a copy of the table. |
| 7777 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7778 | 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] | 7779 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7780 | |
| 7781 | /* Assign the metatable to the mai Map object. */ |
| 7782 | lua_setmetatable(gL.T, -2); |
| 7783 | |
| 7784 | /* Set a name to the table. */ |
| 7785 | lua_setglobal(gL.T, "Map"); |
| 7786 | |
| 7787 | /* |
| 7788 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7789 | * Register class Channel |
| 7790 | * |
| 7791 | */ |
| 7792 | |
| 7793 | /* Create and fill the metatable. */ |
| 7794 | lua_newtable(gL.T); |
| 7795 | |
| 7796 | /* Create and fille the __index entry. */ |
| 7797 | lua_pushstring(gL.T, "__index"); |
| 7798 | lua_newtable(gL.T); |
| 7799 | |
| 7800 | /* Register . */ |
| 7801 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7802 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7803 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7804 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7805 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7806 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7807 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7808 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7809 | 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] | 7810 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7811 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7812 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7813 | |
| 7814 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7815 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7816 | |
| 7817 | /* |
| 7818 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7819 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7820 | * |
| 7821 | */ |
| 7822 | |
| 7823 | /* Create and fill the metatable. */ |
| 7824 | lua_newtable(gL.T); |
| 7825 | |
| 7826 | /* Create and fille the __index entry. */ |
| 7827 | lua_pushstring(gL.T, "__index"); |
| 7828 | lua_newtable(gL.T); |
| 7829 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7830 | /* Browse existing fetches and create the associated |
| 7831 | * object method. |
| 7832 | */ |
| 7833 | sf = NULL; |
| 7834 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7835 | |
| 7836 | /* Dont register the keywork if the arguments check function are |
| 7837 | * not safe during the runtime. |
| 7838 | */ |
| 7839 | if ((sf->val_args != NULL) && |
| 7840 | (sf->val_args != val_payload_lv) && |
| 7841 | (sf->val_args != val_hdr)) |
| 7842 | continue; |
| 7843 | |
| 7844 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7845 | * by an underscore. |
| 7846 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7847 | strncpy(trash.area, sf->kw, trash.size); |
| 7848 | trash.area[trash.size - 1] = '\0'; |
| 7849 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7850 | if (*p == '.' || *p == '-' || *p == '+') |
| 7851 | *p = '_'; |
| 7852 | |
| 7853 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7854 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7855 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7856 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7857 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7858 | } |
| 7859 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7860 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7861 | |
| 7862 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7863 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7864 | |
| 7865 | /* |
| 7866 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7867 | * Register class Converters |
| 7868 | * |
| 7869 | */ |
| 7870 | |
| 7871 | /* Create and fill the metatable. */ |
| 7872 | lua_newtable(gL.T); |
| 7873 | |
| 7874 | /* Create and fill the __index entry. */ |
| 7875 | lua_pushstring(gL.T, "__index"); |
| 7876 | lua_newtable(gL.T); |
| 7877 | |
| 7878 | /* Browse existing converters and create the associated |
| 7879 | * object method. |
| 7880 | */ |
| 7881 | sc = NULL; |
| 7882 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7883 | /* Dont register the keywork if the arguments check function are |
| 7884 | * not safe during the runtime. |
| 7885 | */ |
| 7886 | if (sc->val_args != NULL) |
| 7887 | continue; |
| 7888 | |
| 7889 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7890 | * by an underscore. |
| 7891 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7892 | strncpy(trash.area, sc->kw, trash.size); |
| 7893 | trash.area[trash.size - 1] = '\0'; |
| 7894 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7895 | if (*p == '.' || *p == '-' || *p == '+') |
| 7896 | *p = '_'; |
| 7897 | |
| 7898 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7899 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7900 | lua_pushlightuserdata(gL.T, sc); |
| 7901 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7902 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7903 | } |
| 7904 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7905 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7906 | |
| 7907 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7908 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7909 | |
| 7910 | /* |
| 7911 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7912 | * Register class HTTP |
| 7913 | * |
| 7914 | */ |
| 7915 | |
| 7916 | /* Create and fill the metatable. */ |
| 7917 | lua_newtable(gL.T); |
| 7918 | |
| 7919 | /* Create and fille the __index entry. */ |
| 7920 | lua_pushstring(gL.T, "__index"); |
| 7921 | lua_newtable(gL.T); |
| 7922 | |
| 7923 | /* Register Lua functions. */ |
| 7924 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7925 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7926 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7927 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7928 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7929 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7930 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7931 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7932 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7933 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7934 | |
| 7935 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7936 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7937 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7938 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7939 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7940 | 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] | 7941 | 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] | 7942 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7943 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7944 | |
| 7945 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7946 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7947 | |
| 7948 | /* |
| 7949 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7950 | * Register class AppletTCP |
| 7951 | * |
| 7952 | */ |
| 7953 | |
| 7954 | /* Create and fill the metatable. */ |
| 7955 | lua_newtable(gL.T); |
| 7956 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7957 | /* Create and fille the __index entry. */ |
| 7958 | lua_pushstring(gL.T, "__index"); |
| 7959 | lua_newtable(gL.T); |
| 7960 | |
| 7961 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7962 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7963 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7964 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7965 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7966 | 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] | 7967 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7968 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7969 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7970 | |
| 7971 | lua_settable(gL.T, -3); |
| 7972 | |
| 7973 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7974 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7975 | |
| 7976 | /* |
| 7977 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7978 | * Register class AppletHTTP |
| 7979 | * |
| 7980 | */ |
| 7981 | |
| 7982 | /* Create and fill the metatable. */ |
| 7983 | lua_newtable(gL.T); |
| 7984 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7985 | /* Create and fille the __index entry. */ |
| 7986 | lua_pushstring(gL.T, "__index"); |
| 7987 | lua_newtable(gL.T); |
| 7988 | |
| 7989 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7990 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7991 | 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] | 7992 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7993 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7994 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7995 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7996 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7997 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7998 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7999 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8000 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8001 | |
| 8002 | lua_settable(gL.T, -3); |
| 8003 | |
| 8004 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8005 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8006 | |
| 8007 | /* |
| 8008 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8009 | * Register class TXN |
| 8010 | * |
| 8011 | */ |
| 8012 | |
| 8013 | /* Create and fill the metatable. */ |
| 8014 | lua_newtable(gL.T); |
| 8015 | |
| 8016 | /* Create and fille the __index entry. */ |
| 8017 | lua_pushstring(gL.T, "__index"); |
| 8018 | lua_newtable(gL.T); |
| 8019 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8020 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8021 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8022 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8023 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8024 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8025 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8026 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 8027 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8028 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8029 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8030 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8031 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8032 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8033 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8034 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8035 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8036 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8037 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8038 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8039 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8040 | |
| 8041 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8042 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8043 | |
| 8044 | /* |
| 8045 | * |
| 8046 | * Register class Socket |
| 8047 | * |
| 8048 | */ |
| 8049 | |
| 8050 | /* Create and fill the metatable. */ |
| 8051 | lua_newtable(gL.T); |
| 8052 | |
| 8053 | /* Create and fille the __index entry. */ |
| 8054 | lua_pushstring(gL.T, "__index"); |
| 8055 | lua_newtable(gL.T); |
| 8056 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8057 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8058 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8059 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8060 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8061 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8062 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8063 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8064 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8065 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8066 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8067 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8068 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8069 | 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] | 8070 | |
| 8071 | /* Register the garbage collector entry. */ |
| 8072 | lua_pushstring(gL.T, "__gc"); |
| 8073 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8074 | 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] | 8075 | |
| 8076 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8077 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8078 | |
| 8079 | /* Proxy and server configuration initialisation. */ |
| 8080 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8081 | init_new_proxy(&socket_proxy); |
| 8082 | socket_proxy.parent = NULL; |
| 8083 | socket_proxy.last_change = now.tv_sec; |
| 8084 | socket_proxy.id = "LUA-SOCKET"; |
| 8085 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8086 | socket_proxy.maxconn = 0; |
| 8087 | socket_proxy.accept = NULL; |
| 8088 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8089 | socket_proxy.srv = NULL; |
| 8090 | socket_proxy.conn_retries = 0; |
| 8091 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8092 | |
| 8093 | /* Init TCP server: unchanged parameters */ |
| 8094 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8095 | socket_tcp.next = NULL; |
| 8096 | socket_tcp.proxy = &socket_proxy; |
| 8097 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8098 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8099 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8100 | socket_tcp.priv_conns = NULL; |
| 8101 | socket_tcp.idle_conns = NULL; |
| 8102 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8103 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8104 | socket_tcp.last_change = 0; |
| 8105 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8106 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8107 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8108 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8109 | |
| 8110 | /* XXX: Copy default parameter from default server, |
| 8111 | * but the default server is not initialized. |
| 8112 | */ |
| 8113 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8114 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8115 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8116 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8117 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8118 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8119 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8120 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8121 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8122 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8123 | |
| 8124 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8125 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8126 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8127 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8128 | socket_tcp.check.server = &socket_tcp; |
| 8129 | |
| 8130 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8131 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8132 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8133 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8134 | socket_tcp.agent.server = &socket_tcp; |
| 8135 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8136 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8137 | |
| 8138 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8139 | /* Init TCP server: unchanged parameters */ |
| 8140 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8141 | socket_ssl.next = NULL; |
| 8142 | socket_ssl.proxy = &socket_proxy; |
| 8143 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8144 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8145 | socket_ssl.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8146 | socket_tcp.priv_conns = NULL; |
| 8147 | socket_tcp.idle_conns = NULL; |
| 8148 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8149 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8150 | socket_ssl.last_change = 0; |
| 8151 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8152 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8153 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8154 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8155 | |
| 8156 | /* XXX: Copy default parameter from default server, |
| 8157 | * but the default server is not initialized. |
| 8158 | */ |
| 8159 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8160 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8161 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8162 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8163 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8164 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8165 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8166 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8167 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8168 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8169 | |
| 8170 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8171 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8172 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8173 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8174 | socket_ssl.check.server = &socket_ssl; |
| 8175 | |
| 8176 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8177 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8178 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8179 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8180 | socket_ssl.agent.server = &socket_ssl; |
| 8181 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8182 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8183 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8184 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8185 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8186 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8187 | /* |
| 8188 | * |
| 8189 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8190 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8191 | * features like client certificates and ssl_verify. |
| 8192 | * |
| 8193 | */ |
| 8194 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8195 | if (tmp_error != 0) { |
| 8196 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8197 | abort(); /* This must be never arrives because the command line |
| 8198 | not editable by the user. */ |
| 8199 | } |
| 8200 | idx += kw->skip; |
| 8201 | } |
| 8202 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8203 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8204 | |
| 8205 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8206 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8207 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8208 | static void hlua_register_build_options(void) |
| 8209 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8210 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8211 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8212 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8213 | hap_register_build_opts(ptr, 1); |
| 8214 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8215 | |
| 8216 | INITCALL0(STG_REGISTER, hlua_register_build_options); |
Willy Tarreau | e655251 | 2018-11-26 11:33:13 +0100 | [diff] [blame] | 8217 | REGISTER_CONFIG_POSTPARSER("hlua", hlua_check_config); |