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 | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1619 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1620 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1621 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1622 | |
| 1623 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1624 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1625 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1626 | /* Wake the tasks which wants to write if the buffer have available space. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1627 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1628 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1629 | |
| 1630 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1631 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1632 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1633 | |
| 1634 | /* Some data were injected in the buffer, notify the stream |
| 1635 | * interface. |
| 1636 | */ |
| 1637 | if (!channel_is_empty(si_ic(si))) |
| 1638 | stream_int_update(si); |
| 1639 | |
| 1640 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1641 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1642 | */ |
| 1643 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1644 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1645 | } |
| 1646 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1647 | /* This function is called when the "struct stream" is destroyed. |
| 1648 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1649 | * Wake all the pending signals. |
| 1650 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1651 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1652 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1653 | struct xref *peer; |
| 1654 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1655 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1656 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1657 | if (peer) |
| 1658 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1659 | |
| 1660 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1661 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1662 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1666 | * uses this object. If the stream does not exists, just quit. |
| 1667 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1668 | * pending signal can rest in the read and write lists. destroy |
| 1669 | * it. |
| 1670 | */ |
| 1671 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1672 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1673 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1674 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1675 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1676 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1677 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1678 | |
| 1679 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1680 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1681 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1682 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1683 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1684 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1685 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1686 | appctx->ctx.hlua_cosocket.die = 1; |
| 1687 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1688 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1689 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1690 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1691 | return 0; |
| 1692 | } |
| 1693 | |
| 1694 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1695 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1696 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1697 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1698 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1699 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1700 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1701 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1702 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1703 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1704 | |
| 1705 | /* Check if we run on the same thread than the xreator thread. |
| 1706 | * We cannot access to the socket if the thread is different. |
| 1707 | */ |
| 1708 | if (socket->tid != tid) |
| 1709 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1710 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1711 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1712 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1713 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1714 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1715 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1716 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1717 | appctx->ctx.hlua_cosocket.die = 1; |
| 1718 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1719 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1720 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1721 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1722 | return 0; |
| 1723 | } |
| 1724 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1725 | /* The close function calls close_helper. |
| 1726 | */ |
| 1727 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1728 | { |
| 1729 | MAY_LJMP(check_args(L, 1, "close")); |
| 1730 | return hlua_socket_close_helper(L); |
| 1731 | } |
| 1732 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1733 | /* This Lua function assumes that the stack contain three parameters. |
| 1734 | * 1 - USERDATA containing a struct socket |
| 1735 | * 2 - INTEGER with values of the macro defined below |
| 1736 | * If the integer is -1, we must read at most one line. |
| 1737 | * If the integer is -2, we ust read all the data until the |
| 1738 | * end of the stream. |
| 1739 | * If the integer is positive value, we must read a number of |
| 1740 | * bytes corresponding to this value. |
| 1741 | */ |
| 1742 | #define HLSR_READ_LINE (-1) |
| 1743 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1744 | __LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1745 | { |
| 1746 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1747 | int wanted = lua_tointeger(L, 2); |
| 1748 | struct hlua *hlua = hlua_gethlua(L); |
| 1749 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1750 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1751 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1752 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1753 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1754 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1755 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1756 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1757 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1758 | struct stream_interface *si; |
| 1759 | struct stream *s; |
| 1760 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1761 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1762 | |
| 1763 | /* Check if this lua stack is schedulable. */ |
| 1764 | if (!hlua || !hlua->task) |
| 1765 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1766 | "'frontend', 'backend' or 'task'")); |
| 1767 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1768 | /* Check if we run on the same thread than the xreator thread. |
| 1769 | * We cannot access to the socket if the thread is different. |
| 1770 | */ |
| 1771 | if (socket->tid != tid) |
| 1772 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1773 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1774 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1775 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1776 | if (!peer) |
| 1777 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1778 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1779 | si = appctx->owner; |
| 1780 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1781 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1782 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1783 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1784 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1785 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1786 | if (nblk < 0) /* Connection close. */ |
| 1787 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1788 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1789 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1790 | |
| 1791 | /* remove final \r\n. */ |
| 1792 | if (nblk == 1) { |
| 1793 | if (blk1[len1-1] == '\n') { |
| 1794 | len1--; |
| 1795 | skip_at_end++; |
| 1796 | if (blk1[len1-1] == '\r') { |
| 1797 | len1--; |
| 1798 | skip_at_end++; |
| 1799 | } |
| 1800 | } |
| 1801 | } |
| 1802 | else { |
| 1803 | if (blk2[len2-1] == '\n') { |
| 1804 | len2--; |
| 1805 | skip_at_end++; |
| 1806 | if (blk2[len2-1] == '\r') { |
| 1807 | len2--; |
| 1808 | skip_at_end++; |
| 1809 | } |
| 1810 | } |
| 1811 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1815 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1816 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1817 | if (nblk < 0) /* Connection close. */ |
| 1818 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1819 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1820 | goto connection_empty; |
| 1821 | } |
| 1822 | |
| 1823 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1824 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1825 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1826 | if (nblk < 0) /* Connection close. */ |
| 1827 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1828 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1829 | goto connection_empty; |
| 1830 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1831 | missing_bytes = wanted - socket->b.n; |
| 1832 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1833 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1834 | len1 = missing_bytes; |
| 1835 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1836 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | len = len1; |
| 1840 | |
| 1841 | luaL_addlstring(&socket->b, blk1, len1); |
| 1842 | if (nblk == 2) { |
| 1843 | len += len2; |
| 1844 | luaL_addlstring(&socket->b, blk2, len2); |
| 1845 | } |
| 1846 | |
| 1847 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1848 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1849 | |
| 1850 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1851 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1852 | |
| 1853 | /* If the pattern reclaim to read all the data |
| 1854 | * in the connection, got out. |
| 1855 | */ |
| 1856 | if (wanted == HLSR_READ_ALL) |
| 1857 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1858 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1859 | goto connection_empty; |
| 1860 | |
| 1861 | /* Return result. */ |
| 1862 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1863 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1864 | return 1; |
| 1865 | |
| 1866 | connection_closed: |
| 1867 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1868 | xref_unlock(&socket->xref, peer); |
| 1869 | |
| 1870 | no_peer: |
| 1871 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1872 | /* If the buffer containds data. */ |
| 1873 | if (socket->b.n > 0) { |
| 1874 | luaL_pushresult(&socket->b); |
| 1875 | return 1; |
| 1876 | } |
| 1877 | lua_pushnil(L); |
| 1878 | lua_pushstring(L, "connection closed."); |
| 1879 | return 2; |
| 1880 | |
| 1881 | connection_empty: |
| 1882 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1883 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1884 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1885 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1886 | } |
| 1887 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1888 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1889 | return 0; |
| 1890 | } |
| 1891 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1892 | /* This Lua function gets two parameters. The first one can be string |
| 1893 | * or a number. If the string is "*l", the user requires one line. If |
| 1894 | * the string is "*a", the user requires all the contents of the stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1895 | * If the value is a number, the user require a number of bytes equal |
| 1896 | * to the value. The default value is "*l" (a line). |
| 1897 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1898 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1899 | * integer takes this values: |
| 1900 | * -1 : read a line |
| 1901 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1902 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1903 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1904 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1905 | * concatenated with the read data. |
| 1906 | */ |
| 1907 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1908 | { |
| 1909 | int wanted = HLSR_READ_LINE; |
| 1910 | const char *pattern; |
| 1911 | int type; |
| 1912 | char *error; |
| 1913 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1914 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1915 | |
| 1916 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1917 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1918 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1919 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1920 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1921 | /* Check if we run on the same thread than the xreator thread. |
| 1922 | * We cannot access to the socket if the thread is different. |
| 1923 | */ |
| 1924 | if (socket->tid != tid) |
| 1925 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1926 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1927 | /* check for pattern. */ |
| 1928 | if (lua_gettop(L) >= 2) { |
| 1929 | type = lua_type(L, 2); |
| 1930 | if (type == LUA_TSTRING) { |
| 1931 | pattern = lua_tostring(L, 2); |
| 1932 | if (strcmp(pattern, "*a") == 0) |
| 1933 | wanted = HLSR_READ_ALL; |
| 1934 | else if (strcmp(pattern, "*l") == 0) |
| 1935 | wanted = HLSR_READ_LINE; |
| 1936 | else { |
| 1937 | wanted = strtoll(pattern, &error, 10); |
| 1938 | if (*error != '\0') |
| 1939 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1940 | } |
| 1941 | } |
| 1942 | else if (type == LUA_TNUMBER) { |
| 1943 | wanted = lua_tointeger(L, 2); |
| 1944 | if (wanted < 0) |
| 1945 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | /* Set pattern. */ |
| 1950 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1951 | |
| 1952 | /* Check if we would replace the top by itself. */ |
| 1953 | if (lua_gettop(L) != 2) |
| 1954 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1955 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1956 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1957 | luaL_buffinit(L, &socket->b); |
| 1958 | |
| 1959 | /* Check prefix. */ |
| 1960 | if (lua_gettop(L) >= 3) { |
| 1961 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1962 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1963 | pattern = lua_tolstring(L, 3, &len); |
| 1964 | luaL_addlstring(&socket->b, pattern, len); |
| 1965 | } |
| 1966 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1967 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1971 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1972 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1973 | static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | { |
| 1975 | struct hlua_socket *socket; |
| 1976 | struct hlua *hlua = hlua_gethlua(L); |
| 1977 | struct appctx *appctx; |
| 1978 | size_t buf_len; |
| 1979 | const char *buf; |
| 1980 | int len; |
| 1981 | int send_len; |
| 1982 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1983 | struct xref *peer; |
| 1984 | struct stream_interface *si; |
| 1985 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1986 | |
| 1987 | /* Check if this lua stack is schedulable. */ |
| 1988 | if (!hlua || !hlua->task) |
| 1989 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1990 | "'frontend', 'backend' or 'task'")); |
| 1991 | |
| 1992 | /* Get object */ |
| 1993 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1994 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1995 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1996 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1997 | /* Check if we run on the same thread than the xreator thread. |
| 1998 | * We cannot access to the socket if the thread is different. |
| 1999 | */ |
| 2000 | if (socket->tid != tid) |
| 2001 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2002 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2003 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2004 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2005 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2006 | lua_pushinteger(L, -1); |
| 2007 | return 1; |
| 2008 | } |
| 2009 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2010 | si = appctx->owner; |
| 2011 | s = si_strm(si); |
| 2012 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2013 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2014 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2015 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2016 | lua_pushinteger(L, -1); |
| 2017 | return 1; |
| 2018 | } |
| 2019 | |
| 2020 | /* Update the input buffer data. */ |
| 2021 | buf += sent; |
| 2022 | send_len = buf_len - sent; |
| 2023 | |
| 2024 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2025 | if (sent >= buf_len) { |
| 2026 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2027 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2028 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2029 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2030 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2031 | * the request buffer if its not required. |
| 2032 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2033 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2034 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2035 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2036 | } |
| 2037 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2038 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2039 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2040 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2041 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2042 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2043 | |
| 2044 | /* send data */ |
| 2045 | if (len < send_len) |
| 2046 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2047 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2048 | |
| 2049 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2050 | * the data" (-2) are not expected because the available length |
| 2051 | * is tested. |
| 2052 | * Other unknown error are also not expected. |
| 2053 | */ |
| 2054 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2055 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2056 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2057 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2058 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2059 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2060 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2061 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2062 | return 1; |
| 2063 | } |
| 2064 | |
| 2065 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2066 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2067 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2068 | s->req.rex = TICK_ETERNITY; |
| 2069 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2070 | |
| 2071 | /* Update length sent. */ |
| 2072 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2073 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2074 | |
| 2075 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2076 | if (sent + len >= buf_len) { |
| 2077 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2078 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2079 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2080 | |
| 2081 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2082 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2083 | xref_unlock(&socket->xref, peer); |
| 2084 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2085 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2086 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2087 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2088 | return 0; |
| 2089 | } |
| 2090 | |
| 2091 | /* This function initiate the send of data. It just check the input |
| 2092 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2093 | * amount of data written to the buffer. This is used by the function |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2094 | * "hlua_socket_write_yield" that can yield. |
| 2095 | * |
| 2096 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2097 | * the associated object. The second is a string buffer. The third is |
| 2098 | * a facultative integer that represents where is the buffer position |
| 2099 | * of the start of the data that can send. The first byte is the |
| 2100 | * position "1". The default value is "1". The fourth argument is a |
| 2101 | * facultative integer that represents where is the buffer position |
| 2102 | * of the end of the data that can send. The default is the last byte. |
| 2103 | */ |
| 2104 | static int hlua_socket_send(struct lua_State *L) |
| 2105 | { |
| 2106 | int i; |
| 2107 | int j; |
| 2108 | const char *buf; |
| 2109 | size_t buf_len; |
| 2110 | |
| 2111 | /* Check number of arguments. */ |
| 2112 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2113 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2114 | |
| 2115 | /* Get the string. */ |
| 2116 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2117 | |
| 2118 | /* Get and check j. */ |
| 2119 | if (lua_gettop(L) == 4) { |
| 2120 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2121 | if (j < 0) |
| 2122 | j = buf_len + j + 1; |
| 2123 | if (j > buf_len) |
| 2124 | j = buf_len + 1; |
| 2125 | lua_pop(L, 1); |
| 2126 | } |
| 2127 | else |
| 2128 | j = buf_len; |
| 2129 | |
| 2130 | /* Get and check i. */ |
| 2131 | if (lua_gettop(L) == 3) { |
| 2132 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2133 | if (i < 0) |
| 2134 | i = buf_len + i + 1; |
| 2135 | if (i > buf_len) |
| 2136 | i = buf_len + 1; |
| 2137 | lua_pop(L, 1); |
| 2138 | } else |
| 2139 | i = 1; |
| 2140 | |
| 2141 | /* Check bth i and j. */ |
| 2142 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2143 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2144 | return 1; |
| 2145 | } |
| 2146 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2147 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2148 | return 1; |
| 2149 | } |
| 2150 | if (i == 0) |
| 2151 | i = 1; |
| 2152 | if (j == 0) |
| 2153 | j = 1; |
| 2154 | |
| 2155 | /* Pop the string. */ |
| 2156 | lua_pop(L, 1); |
| 2157 | |
| 2158 | /* Update the buffer length. */ |
| 2159 | buf += i - 1; |
| 2160 | buf_len = j - i + 1; |
| 2161 | lua_pushlstring(L, buf, buf_len); |
| 2162 | |
| 2163 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2164 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2165 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2166 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2167 | } |
| 2168 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2169 | #define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345") |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2170 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2171 | { |
| 2172 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2173 | int ret; |
| 2174 | int len; |
| 2175 | char *p; |
| 2176 | |
| 2177 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2178 | if (ret <= 0) { |
| 2179 | lua_pushnil(L); |
| 2180 | return 1; |
| 2181 | } |
| 2182 | |
| 2183 | if (ret == AF_UNIX) { |
| 2184 | lua_pushstring(L, buffer+1); |
| 2185 | return 1; |
| 2186 | } |
| 2187 | else if (ret == AF_INET6) { |
| 2188 | buffer[0] = '['; |
| 2189 | len = strlen(buffer); |
| 2190 | buffer[len] = ']'; |
| 2191 | len++; |
| 2192 | buffer[len] = ':'; |
| 2193 | len++; |
| 2194 | p = buffer; |
| 2195 | } |
| 2196 | else if (ret == AF_INET) { |
| 2197 | p = buffer + 1; |
| 2198 | len = strlen(p); |
| 2199 | p[len] = ':'; |
| 2200 | len++; |
| 2201 | } |
| 2202 | else { |
| 2203 | lua_pushnil(L); |
| 2204 | return 1; |
| 2205 | } |
| 2206 | |
| 2207 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2208 | lua_pushnil(L); |
| 2209 | return 1; |
| 2210 | } |
| 2211 | |
| 2212 | lua_pushstring(L, p); |
| 2213 | return 1; |
| 2214 | } |
| 2215 | |
| 2216 | /* Returns information about the peer of the connection. */ |
| 2217 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2218 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2219 | struct hlua_socket *socket; |
| 2220 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2221 | struct xref *peer; |
| 2222 | struct appctx *appctx; |
| 2223 | struct stream_interface *si; |
| 2224 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2225 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2226 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2227 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2228 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2229 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2230 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2231 | /* Check if we run on the same thread than the xreator thread. |
| 2232 | * We cannot access to the socket if the thread is different. |
| 2233 | */ |
| 2234 | if (socket->tid != tid) |
| 2235 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2236 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2237 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2238 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2239 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2240 | lua_pushnil(L); |
| 2241 | return 1; |
| 2242 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2243 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2244 | si = appctx->owner; |
| 2245 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2246 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2247 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2248 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2249 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2250 | lua_pushnil(L); |
| 2251 | return 1; |
| 2252 | } |
| 2253 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2254 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2255 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2256 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2257 | lua_pushnil(L); |
| 2258 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2259 | } |
| 2260 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2261 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2262 | xref_unlock(&socket->xref, peer); |
| 2263 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | /* Returns information about my connection side. */ |
| 2267 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2268 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2269 | struct hlua_socket *socket; |
| 2270 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2271 | struct appctx *appctx; |
| 2272 | struct xref *peer; |
| 2273 | struct stream_interface *si; |
| 2274 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2275 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2276 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2277 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2278 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2279 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2280 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2281 | /* Check if we run on the same thread than the xreator thread. |
| 2282 | * We cannot access to the socket if the thread is different. |
| 2283 | */ |
| 2284 | if (socket->tid != tid) |
| 2285 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2286 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2287 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2288 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2289 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2290 | lua_pushnil(L); |
| 2291 | return 1; |
| 2292 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2293 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2294 | si = appctx->owner; |
| 2295 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2296 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2297 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2298 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2299 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | lua_pushnil(L); |
| 2301 | return 1; |
| 2302 | } |
| 2303 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2304 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2305 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2306 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2307 | lua_pushnil(L); |
| 2308 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2309 | } |
| 2310 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2311 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2312 | xref_unlock(&socket->xref, peer); |
| 2313 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2314 | } |
| 2315 | |
| 2316 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2317 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2318 | .obj_type = OBJ_TYPE_APPLET, |
| 2319 | .name = "<LUA_TCP>", |
| 2320 | .fct = hlua_socket_handler, |
| 2321 | .release = hlua_socket_release, |
| 2322 | }; |
| 2323 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2324 | __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] | 2325 | { |
| 2326 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2327 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2328 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2329 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2330 | struct stream_interface *si; |
| 2331 | struct stream *s; |
| 2332 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2333 | /* Check if we run on the same thread than the xreator thread. |
| 2334 | * We cannot access to the socket if the thread is different. |
| 2335 | */ |
| 2336 | if (socket->tid != tid) |
| 2337 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2338 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2339 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2340 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2341 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2342 | lua_pushnil(L); |
| 2343 | lua_pushstring(L, "Can't connect"); |
| 2344 | return 2; |
| 2345 | } |
| 2346 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2347 | si = appctx->owner; |
| 2348 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2349 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2350 | /* Check if we run on the same thread than the xreator thread. |
| 2351 | * We cannot access to the socket if the thread is different. |
| 2352 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2353 | if (socket->tid != tid) { |
| 2354 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2355 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2356 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2357 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2358 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2359 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2360 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2361 | lua_pushnil(L); |
| 2362 | lua_pushstring(L, "Can't connect"); |
| 2363 | return 2; |
| 2364 | } |
| 2365 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2366 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2367 | |
| 2368 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2369 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2370 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2371 | lua_pushinteger(L, 1); |
| 2372 | return 1; |
| 2373 | } |
| 2374 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2375 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2376 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2377 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2378 | } |
| 2379 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2380 | 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] | 2381 | return 0; |
| 2382 | } |
| 2383 | |
| 2384 | /* This function fail or initite the connection. */ |
| 2385 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2386 | { |
| 2387 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2388 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | const char *ip; |
| 2390 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2391 | struct hlua *hlua; |
| 2392 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2393 | int low, high; |
| 2394 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2395 | struct xref *peer; |
| 2396 | struct stream_interface *si; |
| 2397 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2398 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2399 | if (lua_gettop(L) < 2) |
| 2400 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2401 | |
| 2402 | /* Get args. */ |
| 2403 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2404 | |
| 2405 | /* Check if we run on the same thread than the xreator thread. |
| 2406 | * We cannot access to the socket if the thread is different. |
| 2407 | */ |
| 2408 | if (socket->tid != tid) |
| 2409 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2410 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2411 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2412 | if (lua_gettop(L) >= 3) { |
| 2413 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2414 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2415 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2416 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2417 | * that are not enclosed within square brackets. |
| 2418 | */ |
| 2419 | if (port > 0) { |
| 2420 | luaL_buffinit(L, &b); |
| 2421 | luaL_addstring(&b, ip); |
| 2422 | luaL_addchar(&b, ':'); |
| 2423 | luaL_pushresult(&b); |
| 2424 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2425 | } |
| 2426 | } |
| 2427 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2428 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2429 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2430 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2431 | lua_pushnil(L); |
| 2432 | return 1; |
| 2433 | } |
| 2434 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2435 | si = appctx->owner; |
| 2436 | s = si_strm(si); |
| 2437 | |
| 2438 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2439 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2440 | if (!conn) { |
| 2441 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2442 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2443 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2444 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2445 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2446 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2447 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2448 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2449 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2450 | if (!addr) { |
| 2451 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2452 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2453 | } |
| 2454 | if (low != high) { |
| 2455 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2456 | 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] | 2457 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2458 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2459 | |
| 2460 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2461 | if (low == 0) { |
| 2462 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2463 | if (port == -1) { |
| 2464 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2465 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2466 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2467 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2468 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2469 | if (port == -1) { |
| 2470 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2471 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2472 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2473 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2474 | } |
| 2475 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2476 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2477 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2478 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2479 | |
| 2480 | /* inform the stream that we want to be notified whenever the |
| 2481 | * connection completes. |
| 2482 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2483 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2484 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2485 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2486 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2487 | hlua->flags |= HLUA_MUST_GC; |
| 2488 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2489 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2490 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2491 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2492 | } |
| 2493 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2494 | |
| 2495 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2496 | /* Return yield waiting for connection. */ |
| 2497 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2498 | 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] | 2499 | |
| 2500 | return 0; |
| 2501 | } |
| 2502 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2503 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2504 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2505 | { |
| 2506 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2507 | struct xref *peer; |
| 2508 | struct appctx *appctx; |
| 2509 | struct stream_interface *si; |
| 2510 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2511 | |
| 2512 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2513 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2514 | |
| 2515 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2516 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2517 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2518 | lua_pushnil(L); |
| 2519 | return 1; |
| 2520 | } |
| 2521 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2522 | si = appctx->owner; |
| 2523 | s = si_strm(si); |
| 2524 | |
| 2525 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2526 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2527 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2528 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2529 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2530 | |
| 2531 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2532 | { |
| 2533 | return 0; |
| 2534 | } |
| 2535 | |
| 2536 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2537 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2538 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2539 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2540 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2541 | struct xref *peer; |
| 2542 | struct appctx *appctx; |
| 2543 | struct stream_interface *si; |
| 2544 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2545 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2546 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2547 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2548 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2549 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2550 | /* convert the timeout to millis */ |
| 2551 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2552 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2553 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2554 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2555 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2556 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2557 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2558 | 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] | 2559 | |
| 2560 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2561 | if (tmout == 0) |
| 2562 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2563 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2564 | /* Check if we run on the same thread than the xreator thread. |
| 2565 | * We cannot access to the socket if the thread is different. |
| 2566 | */ |
| 2567 | if (socket->tid != tid) |
| 2568 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2569 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2570 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2571 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2572 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2573 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2574 | WILL_LJMP(lua_error(L)); |
| 2575 | return 0; |
| 2576 | } |
| 2577 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2578 | si = appctx->owner; |
| 2579 | s = si_strm(si); |
| 2580 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2581 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2582 | s->req.rto = tmout; |
| 2583 | s->req.wto = tmout; |
| 2584 | s->res.rto = tmout; |
| 2585 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2586 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2587 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2588 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2589 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2590 | |
| 2591 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2592 | task_queue(s->task); |
| 2593 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2594 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2595 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2596 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2597 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2601 | { |
| 2602 | struct hlua_socket *socket; |
| 2603 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2604 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2605 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | |
| 2607 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2608 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2609 | hlua_pusherror(L, "socket: full stack"); |
| 2610 | goto out_fail_conf; |
| 2611 | } |
| 2612 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2613 | /* Create the object: obj[0] = userdata. */ |
| 2614 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2615 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2616 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2617 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2618 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2619 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2620 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2621 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2622 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2623 | goto out_fail_conf; |
| 2624 | } |
| 2625 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2626 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2627 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2628 | lua_setmetatable(L, -2); |
| 2629 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2630 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2631 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2632 | if (!appctx) { |
| 2633 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2634 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2635 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2636 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2637 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2638 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2639 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2640 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2641 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2642 | /* Now create a session, task and stream for this applet */ |
| 2643 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2644 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2645 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2646 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2647 | } |
| 2648 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2649 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2650 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2651 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2652 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2653 | } |
| 2654 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2655 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2656 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2657 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2658 | /* Configure "right" stream interface. this "si" is used to connect |
| 2659 | * and retrieve data from the server. The connection is initialized |
| 2660 | * with the "struct server". |
| 2661 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2662 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2663 | |
| 2664 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2665 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2666 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2667 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2668 | return 1; |
| 2669 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2670 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2671 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2672 | out_fail_sess: |
| 2673 | appctx_free(appctx); |
| 2674 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2675 | WILL_LJMP(lua_error(L)); |
| 2676 | return 0; |
| 2677 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2678 | |
| 2679 | /* |
| 2680 | * |
| 2681 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2682 | * Class Channel |
| 2683 | * |
| 2684 | * |
| 2685 | */ |
| 2686 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2687 | /* 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] | 2688 | * inconsistent, so reset the parser and call it again. Warning, this |
| 2689 | * action does not revalidate the request and does not send a 400 if the modified |
| 2690 | * request is not valid. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2691 | * |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2692 | * This function never fails. The direction is set using dir, which equals |
| 2693 | * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2694 | */ |
| 2695 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2696 | { |
| 2697 | /* Protocol HTTP. */ |
| 2698 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2699 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2700 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2701 | http_txn_reset_req(stream->txn); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2702 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2703 | http_txn_reset_res(stream->txn); |
| 2704 | |
| 2705 | if (stream->txn->hdr_idx.v) |
| 2706 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2707 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2708 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2709 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2710 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2711 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2712 | } |
| 2713 | } |
| 2714 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2715 | /* This function is called before the Lua execution. It stores |
| 2716 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2717 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2718 | 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] | 2719 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2720 | c->mode = stream->be->mode; |
| 2721 | switch (c->mode) { |
| 2722 | case PR_MODE_HTTP: |
| 2723 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2724 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2725 | c->data.http.state = stream->txn->req.msg_state; |
| 2726 | else |
| 2727 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2728 | break; |
| 2729 | default: |
| 2730 | break; |
| 2731 | } |
| 2732 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2733 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2734 | /* This function is called after the Lua execution. it |
| 2735 | * returns true if the parser state is consistent, otherwise, |
| 2736 | * it return false. |
| 2737 | * |
| 2738 | * In HTTP mode, the parser state must be in the same state |
| 2739 | * or greater when we exit the function. Even if we do a |
| 2740 | * control yield. This prevent to break the HTTP message |
| 2741 | * from the Lua code. |
| 2742 | */ |
| 2743 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2744 | { |
| 2745 | if (c->mode != stream->be->mode) |
| 2746 | return 0; |
| 2747 | |
| 2748 | switch (c->mode) { |
| 2749 | case PR_MODE_HTTP: |
| 2750 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2751 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2752 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2753 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2754 | else |
| 2755 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2756 | default: |
| 2757 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2758 | } |
| 2759 | return 1; |
| 2760 | } |
| 2761 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2762 | /* Returns the struct hlua_channel join to the class channel in the |
| 2763 | * stack entry "ud" or throws an argument error. |
| 2764 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2765 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2766 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2767 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2768 | } |
| 2769 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2770 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2771 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2772 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2773 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2774 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2775 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2776 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2777 | return 0; |
| 2778 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2779 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2780 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2781 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2782 | |
| 2783 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2784 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2785 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2786 | return 1; |
| 2787 | } |
| 2788 | |
| 2789 | /* Duplicate all the data present in the input channel and put it |
| 2790 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2791 | * the stack if the channel is closed and all the data are consumed, |
| 2792 | * returns 0 if no data are available, otherwise it returns the length |
| 2793 | * of the builded string. |
| 2794 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2795 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2796 | { |
| 2797 | char *blk1; |
| 2798 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2799 | size_t len1; |
| 2800 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2801 | int ret; |
| 2802 | luaL_Buffer b; |
| 2803 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2804 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2805 | if (unlikely(ret == 0)) |
| 2806 | return 0; |
| 2807 | |
| 2808 | if (unlikely(ret < 0)) { |
| 2809 | lua_pushnil(L); |
| 2810 | return -1; |
| 2811 | } |
| 2812 | |
| 2813 | luaL_buffinit(L, &b); |
| 2814 | luaL_addlstring(&b, blk1, len1); |
| 2815 | if (unlikely(ret == 2)) |
| 2816 | luaL_addlstring(&b, blk2, len2); |
| 2817 | luaL_pushresult(&b); |
| 2818 | |
| 2819 | if (unlikely(ret == 2)) |
| 2820 | return len1 + len2; |
| 2821 | return len1; |
| 2822 | } |
| 2823 | |
| 2824 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2825 | * a yield. This function keep the data in the buffer. |
| 2826 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2827 | __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] | 2828 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2829 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2830 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2831 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2832 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2833 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2834 | 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] | 2835 | return 1; |
| 2836 | } |
| 2837 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2838 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2839 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2840 | { |
| 2841 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2842 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2843 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2844 | } |
| 2845 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2846 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2847 | * a yield. This function consumes the data in the buffer. It returns |
| 2848 | * a string containing the data or a nil pointer if no data are available |
| 2849 | * and the channel is closed. |
| 2850 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2851 | __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] | 2852 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2853 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2854 | int ret; |
| 2855 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2856 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2857 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2858 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2859 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2860 | 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] | 2861 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2862 | if (unlikely(ret == -1)) |
| 2863 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2864 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2865 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2866 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2867 | return 1; |
| 2868 | } |
| 2869 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2870 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2871 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2872 | { |
| 2873 | MAY_LJMP(check_args(L, 1, "get")); |
| 2874 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2875 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2876 | } |
| 2877 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2878 | /* This functions consumes and returns one line. If the channel is closed, |
| 2879 | * 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] | 2880 | * 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] | 2881 | * value. |
| 2882 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2883 | __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] | 2884 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2885 | char *blk1; |
| 2886 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2887 | size_t len1; |
| 2888 | size_t len2; |
| 2889 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2890 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2891 | int ret; |
| 2892 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2893 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2894 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2895 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2896 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2897 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2898 | 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] | 2899 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2900 | if (ret == -1) { |
| 2901 | lua_pushnil(L); |
| 2902 | return 1; |
| 2903 | } |
| 2904 | |
| 2905 | luaL_buffinit(L, &b); |
| 2906 | luaL_addlstring(&b, blk1, len1); |
| 2907 | len = len1; |
| 2908 | if (unlikely(ret == 2)) { |
| 2909 | luaL_addlstring(&b, blk2, len2); |
| 2910 | len += len2; |
| 2911 | } |
| 2912 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2913 | 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] | 2914 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2915 | return 1; |
| 2916 | } |
| 2917 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2918 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2919 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2920 | { |
| 2921 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2922 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2923 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2924 | } |
| 2925 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2926 | /* This function takes a string as input, and append it at the |
| 2927 | * input side of channel. If the data is too big, but a space |
| 2928 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2929 | * yields. If the data is bigger than the buffer, or if the |
| 2930 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2931 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2932 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2933 | __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] | 2934 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2935 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2936 | size_t len; |
| 2937 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2938 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2939 | int ret; |
| 2940 | int max; |
| 2941 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2942 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2943 | * the request buffer if its not required. |
| 2944 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2945 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2946 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2947 | 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] | 2948 | } |
| 2949 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2950 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2951 | if (max > len - l) |
| 2952 | max = len - l; |
| 2953 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2954 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2955 | if (ret == -2 || ret == -3) { |
| 2956 | lua_pushinteger(L, -1); |
| 2957 | return 1; |
| 2958 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2959 | if (ret == -1) { |
| 2960 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2961 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2962 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2963 | l += ret; |
| 2964 | lua_pop(L, 1); |
| 2965 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2966 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2967 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2968 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2969 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2970 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2971 | * in this case, we cannot add more data, so we cannot yield, |
| 2972 | * we return the amount of copyied data. |
| 2973 | */ |
| 2974 | return 1; |
| 2975 | } |
| 2976 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2977 | 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] | 2978 | return 1; |
| 2979 | } |
| 2980 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2981 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2982 | * 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] | 2983 | * buffer size is too little for the data. |
| 2984 | */ |
| 2985 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2986 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2987 | size_t len; |
| 2988 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2989 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2990 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2991 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2992 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2993 | lua_pushinteger(L, 0); |
| 2994 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2995 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2996 | } |
| 2997 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2998 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2999 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3000 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3001 | * or -1 if the channel is closed or if the buffer size is too |
| 3002 | * little for the data. |
| 3003 | */ |
| 3004 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3005 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3006 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3007 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3008 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3009 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3010 | lua_pushinteger(L, 0); |
| 3011 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3012 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3013 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3014 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3015 | } |
| 3016 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3017 | /* Append data in the output side of the buffer. This data is immediately |
| 3018 | * sent. The function returns the amount of data written. If the buffer |
| 3019 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3020 | * if the channel is closed. |
| 3021 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3022 | __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] | 3023 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3024 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3025 | size_t len; |
| 3026 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3027 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3028 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3029 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3030 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3031 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3032 | lua_pushinteger(L, -1); |
| 3033 | return 1; |
| 3034 | } |
| 3035 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3036 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3037 | * the request buffer if its not required. |
| 3038 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3039 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3040 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3041 | 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] | 3042 | } |
| 3043 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3044 | /* The written data will be immediately sent, so we can check |
| 3045 | * the available space without taking in account the reserve. |
| 3046 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3047 | * data, because the buffer will be flushed. |
| 3048 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3049 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3050 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3051 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3052 | * in this case, we cannot add more data, so we cannot yield, |
| 3053 | * we return the amount of copyied data. |
| 3054 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3055 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3056 | return 1; |
| 3057 | |
| 3058 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3059 | if (max > len - l) |
| 3060 | max = len - l; |
| 3061 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3062 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3063 | * detects a non contiguous buffer and realign it. |
| 3064 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3065 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3066 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3067 | |
| 3068 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3069 | 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] | 3070 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3071 | /* buffer replace considers that the input part is filled. |
| 3072 | * so, I must forward these new data in the output part. |
| 3073 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3074 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3075 | |
| 3076 | l += max; |
| 3077 | lua_pop(L, 1); |
| 3078 | lua_pushinteger(L, l); |
| 3079 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3080 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3081 | * in this case, we cannot add more data, so we cannot yield, |
| 3082 | * we return the amount of copyied data. |
| 3083 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3084 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3085 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3086 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3087 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3088 | if (l < len) { |
| 3089 | /* If we are waiting for space in the response buffer, we |
| 3090 | * must set the flag WAKERESWR. This flag required the task |
| 3091 | * wake up if any activity is detected on the response buffer. |
| 3092 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3093 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3094 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3095 | else |
| 3096 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3097 | 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] | 3098 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3099 | |
| 3100 | return 1; |
| 3101 | } |
| 3102 | |
| 3103 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3104 | * yield the LUA process, and resume it without checking the |
| 3105 | * input arguments. |
| 3106 | */ |
| 3107 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3108 | { |
| 3109 | MAY_LJMP(check_args(L, 2, "send")); |
| 3110 | lua_pushinteger(L, 0); |
| 3111 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3112 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3113 | } |
| 3114 | |
| 3115 | /* This function forward and amount of butes. The data pass from |
| 3116 | * the input side of the buffer to the output side, and can be |
| 3117 | * forwarded. This function never fails. |
| 3118 | * |
| 3119 | * The Lua function takes an amount of bytes to be forwarded in |
| 3120 | * imput. It returns the number of bytes forwarded. |
| 3121 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3122 | __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] | 3123 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3124 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3125 | int len; |
| 3126 | int l; |
| 3127 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3128 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3129 | |
| 3130 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3131 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3132 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3133 | |
| 3134 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3135 | if (max > ci_data(chn)) |
| 3136 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3137 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3138 | l += max; |
| 3139 | |
| 3140 | lua_pop(L, 1); |
| 3141 | lua_pushinteger(L, l); |
| 3142 | |
| 3143 | /* Check if it miss bytes to forward. */ |
| 3144 | if (l < len) { |
| 3145 | /* The the input channel or the output channel are closed, we |
| 3146 | * must return the amount of data forwarded. |
| 3147 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3148 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3149 | return 1; |
| 3150 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3151 | /* If we are waiting for space data in the response buffer, we |
| 3152 | * must set the flag WAKERESWR. This flag required the task |
| 3153 | * wake up if any activity is detected on the response buffer. |
| 3154 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3155 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3156 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3157 | else |
| 3158 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3159 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3160 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3161 | 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] | 3162 | } |
| 3163 | |
| 3164 | return 1; |
| 3165 | } |
| 3166 | |
| 3167 | /* Just check the input and prepare the stack for the previous |
| 3168 | * function "hlua_channel_forward_yield" |
| 3169 | */ |
| 3170 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3171 | { |
| 3172 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3173 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3174 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3175 | |
| 3176 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3177 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3178 | } |
| 3179 | |
| 3180 | /* Just returns the number of bytes available in the input |
| 3181 | * side of the buffer. This function never fails. |
| 3182 | */ |
| 3183 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3184 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3185 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3186 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3187 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3188 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3189 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3190 | return 1; |
| 3191 | } |
| 3192 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3193 | /* Returns true if the channel is full. */ |
| 3194 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3195 | { |
| 3196 | struct channel *chn; |
| 3197 | int rem; |
| 3198 | |
| 3199 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3200 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3201 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3202 | rem = b_room(&chn->buf); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3203 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3204 | |
| 3205 | lua_pushboolean(L, rem <= 0); |
| 3206 | return 1; |
| 3207 | } |
| 3208 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3209 | /* Just returns the number of bytes available in the output |
| 3210 | * side of the buffer. This function never fails. |
| 3211 | */ |
| 3212 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3213 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3214 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3215 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3216 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3217 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3218 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3219 | return 1; |
| 3220 | } |
| 3221 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3222 | /* |
| 3223 | * |
| 3224 | * |
| 3225 | * Class Fetches |
| 3226 | * |
| 3227 | * |
| 3228 | */ |
| 3229 | |
| 3230 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3231 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3232 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3233 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3234 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3235 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | /* This function creates and push in the stack a fetch object according |
| 3239 | * with a current TXN. |
| 3240 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3241 | 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] | 3242 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3243 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3244 | |
| 3245 | /* Check stack size. */ |
| 3246 | if (!lua_checkstack(L, 3)) |
| 3247 | return 0; |
| 3248 | |
| 3249 | /* Create the object: obj[0] = userdata. |
| 3250 | * Note that the base of the Fetches object is the |
| 3251 | * transaction object. |
| 3252 | */ |
| 3253 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3254 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3255 | lua_rawseti(L, -2, 0); |
| 3256 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3257 | hsmp->s = txn->s; |
| 3258 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3259 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3260 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3261 | |
| 3262 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3263 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3264 | lua_setmetatable(L, -2); |
| 3265 | |
| 3266 | return 1; |
| 3267 | } |
| 3268 | |
| 3269 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3270 | * It uses closure argument to store the associated sample-fetch. It |
| 3271 | * returns only one argument or throws an error. An error is thrown |
| 3272 | * only if an error is encountered during the argument parsing. If |
| 3273 | * the "sample-fetch" function fails, nil is returned. |
| 3274 | */ |
| 3275 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3276 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3277 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3278 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3279 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3280 | int i; |
| 3281 | struct sample smp; |
| 3282 | |
| 3283 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3284 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3285 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3286 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3287 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3288 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3289 | /* Check execution authorization. */ |
| 3290 | if (f->use & SMP_USE_HTTP_ANY && |
| 3291 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3292 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3293 | "is not available in Lua services", f->kw); |
| 3294 | WILL_LJMP(lua_error(L)); |
| 3295 | } |
| 3296 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3297 | /* Get extra arguments. */ |
| 3298 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3299 | if (i >= ARGM_NBARGS) |
| 3300 | break; |
| 3301 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3302 | } |
| 3303 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3304 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3305 | |
| 3306 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3307 | 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] | 3308 | |
| 3309 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3310 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3311 | lua_pushfstring(L, "error in arguments"); |
| 3312 | WILL_LJMP(lua_error(L)); |
| 3313 | } |
| 3314 | |
| 3315 | /* Initialise the sample. */ |
| 3316 | memset(&smp, 0, sizeof(smp)); |
| 3317 | |
| 3318 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3319 | 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] | 3320 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3321 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3322 | lua_pushstring(L, ""); |
| 3323 | else |
| 3324 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3325 | return 1; |
| 3326 | } |
| 3327 | |
| 3328 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3329 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3330 | hlua_smp2lua_str(L, &smp); |
| 3331 | else |
| 3332 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3333 | return 1; |
| 3334 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3335 | |
| 3336 | /* |
| 3337 | * |
| 3338 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3339 | * Class Converters |
| 3340 | * |
| 3341 | * |
| 3342 | */ |
| 3343 | |
| 3344 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3345 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3346 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3347 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3348 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3349 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | /* This function creates and push in the stack a Converters object |
| 3353 | * according with a current TXN. |
| 3354 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3355 | 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] | 3356 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3357 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3358 | |
| 3359 | /* Check stack size. */ |
| 3360 | if (!lua_checkstack(L, 3)) |
| 3361 | return 0; |
| 3362 | |
| 3363 | /* Create the object: obj[0] = userdata. |
| 3364 | * Note that the base of the Converters object is the |
| 3365 | * same than the TXN object. |
| 3366 | */ |
| 3367 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3368 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3369 | lua_rawseti(L, -2, 0); |
| 3370 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3371 | hsmp->s = txn->s; |
| 3372 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3373 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3374 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3375 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3376 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3377 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3378 | lua_setmetatable(L, -2); |
| 3379 | |
| 3380 | return 1; |
| 3381 | } |
| 3382 | |
| 3383 | /* This function is an LUA binding. It is called with each converter. |
| 3384 | * It uses closure argument to store the associated converter. It |
| 3385 | * returns only one argument or throws an error. An error is thrown |
| 3386 | * only if an error is encountered during the argument parsing. If |
| 3387 | * the converter function function fails, nil is returned. |
| 3388 | */ |
| 3389 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3390 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3391 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3392 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3393 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3394 | int i; |
| 3395 | struct sample smp; |
| 3396 | |
| 3397 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3398 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3399 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3400 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3401 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3402 | |
| 3403 | /* Get extra arguments. */ |
| 3404 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3405 | if (i >= ARGM_NBARGS) |
| 3406 | break; |
| 3407 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3408 | } |
| 3409 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3410 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3411 | |
| 3412 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3413 | 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] | 3414 | |
| 3415 | /* Run the special args checker. */ |
| 3416 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3417 | hlua_pusherror(L, "error in arguments"); |
| 3418 | WILL_LJMP(lua_error(L)); |
| 3419 | } |
| 3420 | |
| 3421 | /* Initialise the sample. */ |
| 3422 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3423 | hlua_pusherror(L, "error in the input argument"); |
| 3424 | WILL_LJMP(lua_error(L)); |
| 3425 | } |
| 3426 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3427 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3428 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3429 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3430 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3431 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3432 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3433 | WILL_LJMP(lua_error(L)); |
| 3434 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3435 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3436 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3437 | hlua_pusherror(L, "error during the input argument casting"); |
| 3438 | WILL_LJMP(lua_error(L)); |
| 3439 | } |
| 3440 | |
| 3441 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3442 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3443 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3444 | lua_pushstring(L, ""); |
| 3445 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3446 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3447 | return 1; |
| 3448 | } |
| 3449 | |
| 3450 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3451 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3452 | hlua_smp2lua_str(L, &smp); |
| 3453 | else |
| 3454 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3455 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3456 | } |
| 3457 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3458 | /* |
| 3459 | * |
| 3460 | * |
| 3461 | * Class AppletTCP |
| 3462 | * |
| 3463 | * |
| 3464 | */ |
| 3465 | |
| 3466 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3467 | * a class stream, otherwise it throws an error. |
| 3468 | */ |
| 3469 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3470 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3471 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3472 | } |
| 3473 | |
| 3474 | /* This function creates and push in the stack an Applet object |
| 3475 | * according with a current TXN. |
| 3476 | */ |
| 3477 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3478 | { |
| 3479 | struct hlua_appctx *appctx; |
| 3480 | struct stream_interface *si = ctx->owner; |
| 3481 | struct stream *s = si_strm(si); |
| 3482 | struct proxy *p = s->be; |
| 3483 | |
| 3484 | /* Check stack size. */ |
| 3485 | if (!lua_checkstack(L, 3)) |
| 3486 | return 0; |
| 3487 | |
| 3488 | /* Create the object: obj[0] = userdata. |
| 3489 | * Note that the base of the Converters object is the |
| 3490 | * same than the TXN object. |
| 3491 | */ |
| 3492 | lua_newtable(L); |
| 3493 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3494 | lua_rawseti(L, -2, 0); |
| 3495 | appctx->appctx = ctx; |
| 3496 | appctx->htxn.s = s; |
| 3497 | appctx->htxn.p = p; |
| 3498 | |
| 3499 | /* Create the "f" field that contains a list of fetches. */ |
| 3500 | lua_pushstring(L, "f"); |
| 3501 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3502 | return 0; |
| 3503 | lua_settable(L, -3); |
| 3504 | |
| 3505 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3506 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3507 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3508 | return 0; |
| 3509 | lua_settable(L, -3); |
| 3510 | |
| 3511 | /* Create the "c" field that contains a list of converters. */ |
| 3512 | lua_pushstring(L, "c"); |
| 3513 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3514 | return 0; |
| 3515 | lua_settable(L, -3); |
| 3516 | |
| 3517 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3518 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3519 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3520 | return 0; |
| 3521 | lua_settable(L, -3); |
| 3522 | |
| 3523 | /* Pop a class stream metatable and affect it to the table. */ |
| 3524 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3525 | lua_setmetatable(L, -2); |
| 3526 | |
| 3527 | return 1; |
| 3528 | } |
| 3529 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3530 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3531 | { |
| 3532 | struct hlua_appctx *appctx; |
| 3533 | struct stream *s; |
| 3534 | const char *name; |
| 3535 | size_t len; |
| 3536 | struct sample smp; |
| 3537 | |
| 3538 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3539 | |
| 3540 | /* It is useles to retrieve the stream, but this function |
| 3541 | * runs only in a stream context. |
| 3542 | */ |
| 3543 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3544 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3545 | s = appctx->htxn.s; |
| 3546 | |
| 3547 | /* Converts the third argument in a sample. */ |
| 3548 | hlua_lua2smp(L, 3, &smp); |
| 3549 | |
| 3550 | /* Store the sample in a variable. */ |
| 3551 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3552 | vars_set_by_name(name, len, &smp); |
| 3553 | return 0; |
| 3554 | } |
| 3555 | |
| 3556 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3557 | { |
| 3558 | struct hlua_appctx *appctx; |
| 3559 | struct stream *s; |
| 3560 | const char *name; |
| 3561 | size_t len; |
| 3562 | struct sample smp; |
| 3563 | |
| 3564 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3565 | |
| 3566 | /* It is useles to retrieve the stream, but this function |
| 3567 | * runs only in a stream context. |
| 3568 | */ |
| 3569 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3570 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3571 | s = appctx->htxn.s; |
| 3572 | |
| 3573 | /* Unset the variable. */ |
| 3574 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3575 | vars_unset_by_name(name, len, &smp); |
| 3576 | return 0; |
| 3577 | } |
| 3578 | |
| 3579 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3580 | { |
| 3581 | struct hlua_appctx *appctx; |
| 3582 | struct stream *s; |
| 3583 | const char *name; |
| 3584 | size_t len; |
| 3585 | struct sample smp; |
| 3586 | |
| 3587 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3588 | |
| 3589 | /* It is useles to retrieve the stream, but this function |
| 3590 | * runs only in a stream context. |
| 3591 | */ |
| 3592 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3593 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3594 | s = appctx->htxn.s; |
| 3595 | |
| 3596 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3597 | if (!vars_get_by_name(name, len, &smp)) { |
| 3598 | lua_pushnil(L); |
| 3599 | return 1; |
| 3600 | } |
| 3601 | |
| 3602 | return hlua_smp2lua(L, &smp); |
| 3603 | } |
| 3604 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3605 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3606 | { |
| 3607 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3608 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3609 | struct hlua *hlua; |
| 3610 | |
| 3611 | /* 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] | 3612 | if (!s->hlua) |
| 3613 | return 0; |
| 3614 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3615 | |
| 3616 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3617 | |
| 3618 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3619 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3620 | |
| 3621 | /* Get and store new value. */ |
| 3622 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3623 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3624 | |
| 3625 | return 0; |
| 3626 | } |
| 3627 | |
| 3628 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3629 | { |
| 3630 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3631 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3632 | struct hlua *hlua; |
| 3633 | |
| 3634 | /* 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] | 3635 | if (!s->hlua) { |
| 3636 | lua_pushnil(L); |
| 3637 | return 1; |
| 3638 | } |
| 3639 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3640 | |
| 3641 | /* Push configuration index in the stack. */ |
| 3642 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3643 | |
| 3644 | return 1; |
| 3645 | } |
| 3646 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3647 | /* If expected data not yet available, it returns a yield. This function |
| 3648 | * consumes the data in the buffer. It returns a string containing the |
| 3649 | * data. This string can be empty. |
| 3650 | */ |
| 3651 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3652 | { |
| 3653 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3654 | struct stream_interface *si = appctx->appctx->owner; |
| 3655 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3656 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3657 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3658 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3659 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3660 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3661 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3662 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3663 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3664 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3665 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3666 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3667 | 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] | 3668 | } |
| 3669 | |
| 3670 | /* End of data: commit the total strings and return. */ |
| 3671 | if (ret < 0) { |
| 3672 | luaL_pushresult(&appctx->b); |
| 3673 | return 1; |
| 3674 | } |
| 3675 | |
| 3676 | /* Ensure that the block 2 length is usable. */ |
| 3677 | if (ret == 1) |
| 3678 | len2 = 0; |
| 3679 | |
| 3680 | /* dont check the max length read and dont check. */ |
| 3681 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3682 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3683 | |
| 3684 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3685 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3686 | luaL_pushresult(&appctx->b); |
| 3687 | return 1; |
| 3688 | } |
| 3689 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3690 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3691 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3692 | { |
| 3693 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3694 | |
| 3695 | /* Initialise the string catenation. */ |
| 3696 | luaL_buffinit(L, &appctx->b); |
| 3697 | |
| 3698 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3699 | } |
| 3700 | |
| 3701 | /* If expected data not yet available, it returns a yield. This function |
| 3702 | * consumes the data in the buffer. It returns a string containing the |
| 3703 | * data. This string can be empty. |
| 3704 | */ |
| 3705 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3706 | { |
| 3707 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3708 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3709 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3710 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3711 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3712 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3713 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3714 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3715 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3716 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3717 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3718 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3719 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3720 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3721 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3722 | 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] | 3723 | } |
| 3724 | |
| 3725 | /* End of data: commit the total strings and return. */ |
| 3726 | if (ret < 0) { |
| 3727 | luaL_pushresult(&appctx->b); |
| 3728 | return 1; |
| 3729 | } |
| 3730 | |
| 3731 | /* Ensure that the block 2 length is usable. */ |
| 3732 | if (ret == 1) |
| 3733 | len2 = 0; |
| 3734 | |
| 3735 | if (len == -1) { |
| 3736 | |
| 3737 | /* If len == -1, catenate all the data avalaile and |
| 3738 | * yield because we want to get all the data until |
| 3739 | * the end of data stream. |
| 3740 | */ |
| 3741 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3742 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3743 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3744 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3745 | 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] | 3746 | |
| 3747 | } else { |
| 3748 | |
| 3749 | /* Copy the fisrt block caping to the length required. */ |
| 3750 | if (len1 > len) |
| 3751 | len1 = len; |
| 3752 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3753 | len -= len1; |
| 3754 | |
| 3755 | /* Copy the second block. */ |
| 3756 | if (len2 > len) |
| 3757 | len2 = len; |
| 3758 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3759 | len -= len2; |
| 3760 | |
| 3761 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3762 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3763 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3764 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3765 | if (len > 0) { |
| 3766 | lua_pushinteger(L, len); |
| 3767 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3768 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3769 | 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] | 3770 | } |
| 3771 | |
| 3772 | /* return the result. */ |
| 3773 | luaL_pushresult(&appctx->b); |
| 3774 | return 1; |
| 3775 | } |
| 3776 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3777 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3778 | hlua_pusherror(L, "Lua: internal error"); |
| 3779 | WILL_LJMP(lua_error(L)); |
| 3780 | return 0; |
| 3781 | } |
| 3782 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3783 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3784 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3785 | { |
| 3786 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3787 | int len = -1; |
| 3788 | |
| 3789 | if (lua_gettop(L) > 2) |
| 3790 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3791 | if (lua_gettop(L) >= 2) { |
| 3792 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3793 | lua_pop(L, 1); |
| 3794 | } |
| 3795 | |
| 3796 | /* Confirm or set the required length */ |
| 3797 | lua_pushinteger(L, len); |
| 3798 | |
| 3799 | /* Initialise the string catenation. */ |
| 3800 | luaL_buffinit(L, &appctx->b); |
| 3801 | |
| 3802 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3803 | } |
| 3804 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3805 | /* Append data in the output side of the buffer. This data is immediately |
| 3806 | * sent. The function returns the amount of data written. If the buffer |
| 3807 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3808 | * if the channel is closed. |
| 3809 | */ |
| 3810 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3811 | { |
| 3812 | size_t len; |
| 3813 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3814 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3815 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3816 | struct stream_interface *si = appctx->appctx->owner; |
| 3817 | struct channel *chn = si_ic(si); |
| 3818 | int max; |
| 3819 | |
| 3820 | /* Get the max amount of data which can write as input in the channel. */ |
| 3821 | max = channel_recv_max(chn); |
| 3822 | if (max > (len - l)) |
| 3823 | max = len - l; |
| 3824 | |
| 3825 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3826 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3827 | |
| 3828 | /* update counters. */ |
| 3829 | l += max; |
| 3830 | lua_pop(L, 1); |
| 3831 | lua_pushinteger(L, l); |
| 3832 | |
| 3833 | /* If some data is not send, declares the situation to the |
| 3834 | * applet, and returns a yield. |
| 3835 | */ |
| 3836 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3837 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3838 | 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] | 3839 | } |
| 3840 | |
| 3841 | return 1; |
| 3842 | } |
| 3843 | |
| 3844 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3845 | * yield the LUA process, and resume it without checking the |
| 3846 | * input arguments. |
| 3847 | */ |
| 3848 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3849 | { |
| 3850 | MAY_LJMP(check_args(L, 2, "send")); |
| 3851 | lua_pushinteger(L, 0); |
| 3852 | |
| 3853 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3854 | } |
| 3855 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3856 | /* |
| 3857 | * |
| 3858 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3859 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3860 | * |
| 3861 | * |
| 3862 | */ |
| 3863 | |
| 3864 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3865 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3866 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3867 | __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] | 3868 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3869 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3870 | } |
| 3871 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3872 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3873 | * according with a current TXN. |
| 3874 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3875 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3876 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3877 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3878 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3879 | struct stream_interface *si = ctx->owner; |
| 3880 | struct stream *s = si_strm(si); |
| 3881 | struct proxy *px = s->be; |
| 3882 | struct http_txn *txn = s->txn; |
| 3883 | const char *path; |
| 3884 | const char *end; |
| 3885 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3886 | |
| 3887 | /* Check stack size. */ |
| 3888 | if (!lua_checkstack(L, 3)) |
| 3889 | return 0; |
| 3890 | |
| 3891 | /* Create the object: obj[0] = userdata. |
| 3892 | * Note that the base of the Converters object is the |
| 3893 | * same than the TXN object. |
| 3894 | */ |
| 3895 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3896 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3897 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3898 | appctx->appctx = ctx; |
| 3899 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3900 | 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] | 3901 | appctx->htxn.s = s; |
| 3902 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3903 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3904 | /* Create the "f" field that contains a list of fetches. */ |
| 3905 | lua_pushstring(L, "f"); |
| 3906 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3907 | return 0; |
| 3908 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3909 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3910 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3911 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3912 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3913 | return 0; |
| 3914 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3915 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3916 | /* Create the "c" field that contains a list of converters. */ |
| 3917 | lua_pushstring(L, "c"); |
| 3918 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3919 | return 0; |
| 3920 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3921 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3922 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3923 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3924 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3925 | return 0; |
| 3926 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3927 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3928 | /* Stores the request method. */ |
| 3929 | lua_pushstring(L, "method"); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3930 | 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] | 3931 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3932 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3933 | /* Stores the http version. */ |
| 3934 | lua_pushstring(L, "version"); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3935 | 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] | 3936 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3937 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3938 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3939 | * the array on the top of the stack. |
| 3940 | */ |
| 3941 | lua_pushstring(L, "headers"); |
| 3942 | htxn.s = s; |
| 3943 | htxn.p = px; |
| 3944 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3945 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3946 | return 0; |
| 3947 | lua_settable(L, -3); |
| 3948 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3949 | /* Get path and qs */ |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 3950 | path = http_txn_get_path(txn); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3951 | if (path) { |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3952 | 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] | 3953 | p = path; |
| 3954 | while (p < end && *p != '?') |
| 3955 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3956 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3957 | /* Stores the request path. */ |
| 3958 | lua_pushstring(L, "path"); |
| 3959 | lua_pushlstring(L, path, p - path); |
| 3960 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3961 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3962 | /* Stores the query string. */ |
| 3963 | lua_pushstring(L, "qs"); |
| 3964 | if (*p == '?') |
| 3965 | p++; |
| 3966 | lua_pushlstring(L, p, end - p); |
| 3967 | lua_settable(L, -3); |
| 3968 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3969 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3970 | /* Stores the request path. */ |
| 3971 | lua_pushstring(L, "length"); |
| 3972 | lua_pushinteger(L, txn->req.body_len); |
| 3973 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3974 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3975 | /* Create an array of HTTP request headers. */ |
| 3976 | lua_pushstring(L, "headers"); |
| 3977 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3978 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3979 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3980 | /* Create an empty array of HTTP request headers. */ |
| 3981 | lua_pushstring(L, "response"); |
| 3982 | lua_newtable(L); |
| 3983 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3984 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3985 | /* Pop a class stream metatable and affect it to the table. */ |
| 3986 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3987 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3988 | |
| 3989 | return 1; |
| 3990 | } |
| 3991 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3992 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3993 | { |
| 3994 | struct hlua_appctx *appctx; |
| 3995 | struct stream *s; |
| 3996 | const char *name; |
| 3997 | size_t len; |
| 3998 | struct sample smp; |
| 3999 | |
| 4000 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4001 | |
| 4002 | /* It is useles to retrieve the stream, but this function |
| 4003 | * runs only in a stream context. |
| 4004 | */ |
| 4005 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4006 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4007 | s = appctx->htxn.s; |
| 4008 | |
| 4009 | /* Converts the third argument in a sample. */ |
| 4010 | hlua_lua2smp(L, 3, &smp); |
| 4011 | |
| 4012 | /* Store the sample in a variable. */ |
| 4013 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4014 | vars_set_by_name(name, len, &smp); |
| 4015 | return 0; |
| 4016 | } |
| 4017 | |
| 4018 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4019 | { |
| 4020 | struct hlua_appctx *appctx; |
| 4021 | struct stream *s; |
| 4022 | const char *name; |
| 4023 | size_t len; |
| 4024 | struct sample smp; |
| 4025 | |
| 4026 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4027 | |
| 4028 | /* It is useles to retrieve the stream, but this function |
| 4029 | * runs only in a stream context. |
| 4030 | */ |
| 4031 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4032 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4033 | s = appctx->htxn.s; |
| 4034 | |
| 4035 | /* Unset the variable. */ |
| 4036 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4037 | vars_unset_by_name(name, len, &smp); |
| 4038 | return 0; |
| 4039 | } |
| 4040 | |
| 4041 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4042 | { |
| 4043 | struct hlua_appctx *appctx; |
| 4044 | struct stream *s; |
| 4045 | const char *name; |
| 4046 | size_t len; |
| 4047 | struct sample smp; |
| 4048 | |
| 4049 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4050 | |
| 4051 | /* It is useles to retrieve the stream, but this function |
| 4052 | * runs only in a stream context. |
| 4053 | */ |
| 4054 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4055 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4056 | s = appctx->htxn.s; |
| 4057 | |
| 4058 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4059 | if (!vars_get_by_name(name, len, &smp)) { |
| 4060 | lua_pushnil(L); |
| 4061 | return 1; |
| 4062 | } |
| 4063 | |
| 4064 | return hlua_smp2lua(L, &smp); |
| 4065 | } |
| 4066 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4067 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4068 | { |
| 4069 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4070 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4071 | struct hlua *hlua; |
| 4072 | |
| 4073 | /* 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] | 4074 | if (!s->hlua) |
| 4075 | return 0; |
| 4076 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4077 | |
| 4078 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4079 | |
| 4080 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4081 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4082 | |
| 4083 | /* Get and store new value. */ |
| 4084 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4085 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4086 | |
| 4087 | return 0; |
| 4088 | } |
| 4089 | |
| 4090 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4091 | { |
| 4092 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4093 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4094 | struct hlua *hlua; |
| 4095 | |
| 4096 | /* 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] | 4097 | if (!s->hlua) { |
| 4098 | lua_pushnil(L); |
| 4099 | return 1; |
| 4100 | } |
| 4101 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4102 | |
| 4103 | /* Push configuration index in the stack. */ |
| 4104 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4105 | |
| 4106 | return 1; |
| 4107 | } |
| 4108 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4109 | /* If expected data not yet available, it returns a yield. This function |
| 4110 | * consumes the data in the buffer. It returns a string containing the |
| 4111 | * data. This string can be empty. |
| 4112 | */ |
| 4113 | __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] | 4114 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4115 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4116 | struct stream_interface *si = appctx->appctx->owner; |
| 4117 | struct channel *chn = si_ic(si); |
| 4118 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4119 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4120 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4121 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4122 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4123 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4124 | /* Maybe we cant send a 100-continue ? */ |
| 4125 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4126 | ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4127 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4128 | * big for the buffers. We cant send anything. So, we ignoring |
| 4129 | * the error, considers that the 100-continue is sent, and try |
| 4130 | * to receive. |
| 4131 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4132 | */ |
| 4133 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4134 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4135 | 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] | 4136 | } |
| 4137 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4138 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4139 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4140 | /* Check for the end of the data. */ |
| 4141 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4142 | luaL_pushresult(&appctx->b); |
| 4143 | return 1; |
| 4144 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4145 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4146 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4147 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4148 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4149 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4150 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4151 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4152 | 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] | 4153 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4154 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4155 | /* End of data: commit the total strings and return. */ |
| 4156 | if (ret < 0) { |
| 4157 | luaL_pushresult(&appctx->b); |
| 4158 | return 1; |
| 4159 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4160 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4161 | /* Ensure that the block 2 length is usable. */ |
| 4162 | if (ret == 1) |
| 4163 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4164 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4165 | /* Copy the fisrt block caping to the length required. */ |
| 4166 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4167 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4168 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4169 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4170 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4171 | /* Copy the second block. */ |
| 4172 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4173 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4174 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4175 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4176 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4177 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4178 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4179 | luaL_pushresult(&appctx->b); |
| 4180 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4181 | } |
| 4182 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4183 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4184 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4185 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4186 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4187 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4188 | /* Initialise the string catenation. */ |
| 4189 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4190 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4191 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4192 | } |
| 4193 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4194 | /* If expected data not yet available, it returns a yield. This function |
| 4195 | * consumes the data in the buffer. It returns a string containing the |
| 4196 | * data. This string can be empty. |
| 4197 | */ |
| 4198 | __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] | 4199 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4200 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4201 | struct stream_interface *si = appctx->appctx->owner; |
| 4202 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4203 | struct channel *chn = si_ic(si); |
| 4204 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4205 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4206 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4207 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4208 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4209 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4210 | /* Maybe we cant send a 100-continue ? */ |
| 4211 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4212 | ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4213 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4214 | * big for the buffers. We cant send anything. So, we ignoring |
| 4215 | * the error, considers that the 100-continue is sent, and try |
| 4216 | * to receive. |
| 4217 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4218 | */ |
| 4219 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4220 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4221 | 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] | 4222 | } |
| 4223 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4224 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4225 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4226 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4227 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4228 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4229 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4230 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4231 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4232 | 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] | 4233 | } |
| 4234 | |
| 4235 | /* End of data: commit the total strings and return. */ |
| 4236 | if (ret < 0) { |
| 4237 | luaL_pushresult(&appctx->b); |
| 4238 | return 1; |
| 4239 | } |
| 4240 | |
| 4241 | /* Ensure that the block 2 length is usable. */ |
| 4242 | if (ret == 1) |
| 4243 | len2 = 0; |
| 4244 | |
| 4245 | /* Copy the fisrt block caping to the length required. */ |
| 4246 | if (len1 > len) |
| 4247 | len1 = len; |
| 4248 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4249 | len -= len1; |
| 4250 | |
| 4251 | /* Copy the second block. */ |
| 4252 | if (len2 > len) |
| 4253 | len2 = len; |
| 4254 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4255 | len -= len2; |
| 4256 | |
| 4257 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4258 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4259 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4260 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4261 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4262 | /* If we are no other data available, yield waiting for new data. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4263 | if (len > 0) { |
| 4264 | lua_pushinteger(L, len); |
| 4265 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4266 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4267 | 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] | 4268 | } |
| 4269 | |
| 4270 | /* return the result. */ |
| 4271 | luaL_pushresult(&appctx->b); |
| 4272 | return 1; |
| 4273 | } |
| 4274 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4275 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4276 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4277 | { |
| 4278 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4279 | int len = -1; |
| 4280 | |
| 4281 | /* Check arguments. */ |
| 4282 | if (lua_gettop(L) > 2) |
| 4283 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4284 | if (lua_gettop(L) >= 2) { |
| 4285 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4286 | lua_pop(L, 1); |
| 4287 | } |
| 4288 | |
| 4289 | /* Check the required length */ |
| 4290 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4291 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4292 | lua_pushinteger(L, len); |
| 4293 | |
| 4294 | /* Initialise the string catenation. */ |
| 4295 | luaL_buffinit(L, &appctx->b); |
| 4296 | |
| 4297 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4298 | } |
| 4299 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4300 | /* Append data in the output side of the buffer. This data is immediately |
| 4301 | * sent. The function returns the amount of data written. If the buffer |
| 4302 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4303 | * if the channel is closed. |
| 4304 | */ |
| 4305 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4306 | { |
| 4307 | size_t len; |
| 4308 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4309 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4310 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4311 | struct stream_interface *si = appctx->appctx->owner; |
| 4312 | struct channel *chn = si_ic(si); |
| 4313 | int max; |
| 4314 | |
| 4315 | /* Get the max amount of data which can write as input in the channel. */ |
| 4316 | max = channel_recv_max(chn); |
| 4317 | if (max > (len - l)) |
| 4318 | max = len - l; |
| 4319 | |
| 4320 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4321 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4322 | |
| 4323 | /* update counters. */ |
| 4324 | l += max; |
| 4325 | lua_pop(L, 1); |
| 4326 | lua_pushinteger(L, l); |
| 4327 | |
| 4328 | /* If some data is not send, declares the situation to the |
| 4329 | * applet, and returns a yield. |
| 4330 | */ |
| 4331 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4332 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4333 | 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] | 4334 | } |
| 4335 | |
| 4336 | return 1; |
| 4337 | } |
| 4338 | |
| 4339 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4340 | * yield the LUA process, and resume it without checking the |
| 4341 | * input arguments. |
| 4342 | */ |
| 4343 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4344 | { |
| 4345 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4346 | size_t len; |
| 4347 | char hex[10]; |
| 4348 | |
| 4349 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4350 | |
| 4351 | /* If transfer encoding chunked is selected, we surround the data |
| 4352 | * by chunk data. |
| 4353 | */ |
| 4354 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4355 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4356 | lua_pushfstring(L, "%s\r\n", hex); |
| 4357 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4358 | lua_pushstring(L, "\r\n"); |
| 4359 | lua_concat(L, 3); |
| 4360 | } |
| 4361 | |
| 4362 | /* This interger is used for followinf the amount of data sent. */ |
| 4363 | lua_pushinteger(L, 0); |
| 4364 | |
| 4365 | /* We want to send some data. Headers must be sent. */ |
| 4366 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4367 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4368 | WILL_LJMP(lua_error(L)); |
| 4369 | } |
| 4370 | |
| 4371 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4372 | } |
| 4373 | |
| 4374 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4375 | { |
| 4376 | const char *name; |
| 4377 | int ret; |
| 4378 | |
| 4379 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4380 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4381 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4382 | |
| 4383 | /* Push in the stack the "response" entry. */ |
| 4384 | ret = lua_getfield(L, 1, "response"); |
| 4385 | if (ret != LUA_TTABLE) { |
| 4386 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4387 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4388 | WILL_LJMP(lua_error(L)); |
| 4389 | } |
| 4390 | |
| 4391 | /* check if the header is already registered if it is not |
| 4392 | * the case, register it. |
| 4393 | */ |
| 4394 | ret = lua_getfield(L, -1, name); |
| 4395 | if (ret == LUA_TNIL) { |
| 4396 | |
| 4397 | /* Entry not found. */ |
| 4398 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4399 | |
| 4400 | /* Insert the new header name in the array in the top of the stack. |
| 4401 | * It left the new array in the top of the stack. |
| 4402 | */ |
| 4403 | lua_newtable(L); |
| 4404 | lua_pushvalue(L, 2); |
| 4405 | lua_pushvalue(L, -2); |
| 4406 | lua_settable(L, -4); |
| 4407 | |
| 4408 | } else if (ret != LUA_TTABLE) { |
| 4409 | |
| 4410 | /* corruption error. */ |
| 4411 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4412 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4413 | WILL_LJMP(lua_error(L)); |
| 4414 | } |
| 4415 | |
| 4416 | /* Now the top od thestack is an array of values. We push |
| 4417 | * the header value as new entry. |
| 4418 | */ |
| 4419 | lua_pushvalue(L, 3); |
| 4420 | ret = lua_rawlen(L, -2); |
| 4421 | lua_rawseti(L, -2, ret + 1); |
| 4422 | lua_pushboolean(L, 1); |
| 4423 | return 1; |
| 4424 | } |
| 4425 | |
| 4426 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4427 | { |
| 4428 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4429 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4430 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4431 | |
| 4432 | if (status < 100 || status > 599) { |
| 4433 | lua_pushboolean(L, 0); |
| 4434 | return 1; |
| 4435 | } |
| 4436 | |
| 4437 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4438 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4439 | lua_pushboolean(L, 1); |
| 4440 | return 1; |
| 4441 | } |
| 4442 | |
| 4443 | /* We will build the status line and the headers of the HTTP response. |
| 4444 | * We will try send at once if its not possible, we give back the hand |
| 4445 | * waiting for more room. |
| 4446 | */ |
| 4447 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4448 | { |
| 4449 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4450 | struct stream_interface *si = appctx->appctx->owner; |
| 4451 | struct channel *chn = si_ic(si); |
| 4452 | int ret; |
| 4453 | size_t len; |
| 4454 | const char *msg; |
| 4455 | |
| 4456 | /* Get the message as the first argument on the stack. */ |
| 4457 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4458 | |
| 4459 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4460 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4461 | |
| 4462 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4463 | * big for the buffers. |
| 4464 | */ |
| 4465 | if (ret == -2 || ret == -3) { |
| 4466 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4467 | WILL_LJMP(lua_error(L)); |
| 4468 | } |
| 4469 | |
| 4470 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4471 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4472 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4473 | 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] | 4474 | } |
| 4475 | |
| 4476 | /* Headers sent, set the flag. */ |
| 4477 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4478 | return 0; |
| 4479 | } |
| 4480 | |
| 4481 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4482 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4483 | struct buffer *tmp = get_trash_chunk(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4484 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4485 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4486 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4487 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4488 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4489 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4490 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4491 | int hdr_chunked = 0; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4492 | const char *reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4493 | |
| 4494 | if (reason == NULL) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4495 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4496 | |
| 4497 | /* Use the same http version than the request. */ |
| 4498 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4499 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4500 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4501 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4502 | |
| 4503 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4504 | lua_pushvalue(L, 0); |
| 4505 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4506 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4507 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4508 | WILL_LJMP(lua_error(L)); |
| 4509 | } |
| 4510 | |
| 4511 | /* Browse the list of headers. */ |
| 4512 | lua_pushnil(L); |
| 4513 | while(lua_next(L, -2) != 0) { |
| 4514 | |
| 4515 | /* We expect a string as -2. */ |
| 4516 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4517 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4518 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4519 | lua_typename(L, lua_type(L, -2))); |
| 4520 | WILL_LJMP(lua_error(L)); |
| 4521 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4522 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4523 | |
| 4524 | /* We expect an array as -1. */ |
| 4525 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4526 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4527 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4528 | name, |
| 4529 | lua_typename(L, lua_type(L, -1))); |
| 4530 | WILL_LJMP(lua_error(L)); |
| 4531 | } |
| 4532 | |
| 4533 | /* Browse the table who is on the top of the stack. */ |
| 4534 | lua_pushnil(L); |
| 4535 | while(lua_next(L, -2) != 0) { |
| 4536 | |
| 4537 | /* We expect a number as -2. */ |
| 4538 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4539 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4540 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4541 | name, |
| 4542 | lua_typename(L, lua_type(L, -2))); |
| 4543 | WILL_LJMP(lua_error(L)); |
| 4544 | } |
| 4545 | id = lua_tointeger(L, -2); |
| 4546 | |
| 4547 | /* We expect a string as -2. */ |
| 4548 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4549 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4550 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4551 | name, id, |
| 4552 | lua_typename(L, lua_type(L, -1))); |
| 4553 | WILL_LJMP(lua_error(L)); |
| 4554 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4555 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4556 | |
| 4557 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4558 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 4559 | memcpy(tmp->area + tmp->data, name, name_len); |
| 4560 | tmp->data += name_len; |
| 4561 | tmp->area[tmp->data++] = ':'; |
| 4562 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4563 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4564 | memcpy(tmp->area + tmp->data, value, |
| 4565 | value_len); |
| 4566 | tmp->data += value_len; |
| 4567 | tmp->area[tmp->data++] = '\r'; |
| 4568 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4569 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4570 | |
| 4571 | /* Protocol checks. */ |
| 4572 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4573 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4574 | * is done without control. If it contains a bad value, |
| 4575 | * the content-length remains negative so that we can |
| 4576 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4577 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4578 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4579 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4580 | |
| 4581 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4582 | if (name_len == 17 && value_len == 7 && |
| 4583 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4584 | strcasecmp("chunked", value) == 0) |
| 4585 | hdr_chunked = 1; |
| 4586 | |
| 4587 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4588 | lua_pop(L, 1); |
| 4589 | } |
| 4590 | |
| 4591 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4592 | lua_pop(L, 1); |
| 4593 | } |
| 4594 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4595 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4596 | * and the status code implies the presence of a message body, we must |
| 4597 | * announce a transfer encoding chunked. This is required by haproxy |
| 4598 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4599 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4600 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4601 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4602 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 4603 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4604 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4605 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4606 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4607 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4608 | } |
| 4609 | |
| 4610 | /* Finalize headers. */ |
| 4611 | chunk_appendf(tmp, "\r\n"); |
| 4612 | |
| 4613 | /* Remove the last entry and the array of headers */ |
| 4614 | lua_pop(L, 2); |
| 4615 | |
| 4616 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4617 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4618 | |
| 4619 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4620 | } |
| 4621 | |
| 4622 | /* |
| 4623 | * |
| 4624 | * |
| 4625 | * Class HTTP |
| 4626 | * |
| 4627 | * |
| 4628 | */ |
| 4629 | |
| 4630 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4631 | * a class stream, otherwise it throws an error. |
| 4632 | */ |
| 4633 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4634 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4635 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4636 | } |
| 4637 | |
| 4638 | /* This function creates and push in the stack a HTTP object |
| 4639 | * according with a current TXN. |
| 4640 | */ |
| 4641 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4642 | { |
| 4643 | struct hlua_txn *htxn; |
| 4644 | |
| 4645 | /* Check stack size. */ |
| 4646 | if (!lua_checkstack(L, 3)) |
| 4647 | return 0; |
| 4648 | |
| 4649 | /* Create the object: obj[0] = userdata. |
| 4650 | * Note that the base of the Converters object is the |
| 4651 | * same than the TXN object. |
| 4652 | */ |
| 4653 | lua_newtable(L); |
| 4654 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4655 | lua_rawseti(L, -2, 0); |
| 4656 | |
| 4657 | htxn->s = txn->s; |
| 4658 | htxn->p = txn->p; |
| 4659 | |
| 4660 | /* Pop a class stream metatable and affect it to the table. */ |
| 4661 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4662 | lua_setmetatable(L, -2); |
| 4663 | |
| 4664 | return 1; |
| 4665 | } |
| 4666 | |
| 4667 | /* This function creates ans returns an array of HTTP headers. |
| 4668 | * This function does not fails. It is used as wrapper with the |
| 4669 | * 2 following functions. |
| 4670 | */ |
| 4671 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4672 | { |
| 4673 | const char *cur_ptr, *cur_next, *p; |
| 4674 | int old_idx, cur_idx; |
| 4675 | struct hdr_idx_elem *cur_hdr; |
| 4676 | const char *hn, *hv; |
| 4677 | int hnl, hvl; |
| 4678 | int type; |
| 4679 | const char *in; |
| 4680 | char *out; |
| 4681 | int len; |
| 4682 | |
| 4683 | /* Create the table. */ |
| 4684 | lua_newtable(L); |
| 4685 | |
| 4686 | if (!htxn->s->txn) |
| 4687 | return 1; |
| 4688 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4689 | /* Check if a valid response is parsed */ |
| 4690 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4691 | return 1; |
| 4692 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4693 | /* Build array of headers. */ |
| 4694 | old_idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 4695 | 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] | 4696 | |
| 4697 | while (1) { |
| 4698 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4699 | if (!cur_idx) |
| 4700 | break; |
| 4701 | old_idx = cur_idx; |
| 4702 | |
| 4703 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4704 | cur_ptr = cur_next; |
| 4705 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4706 | |
| 4707 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4708 | * and the next header starts at cur_next. We'll check |
| 4709 | * this header in the list as well as against the default |
| 4710 | * rule. |
| 4711 | */ |
| 4712 | |
| 4713 | /* look for ': *'. */ |
| 4714 | hn = cur_ptr; |
| 4715 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4716 | if (p >= cur_ptr+cur_hdr->len) |
| 4717 | continue; |
| 4718 | hnl = p - hn; |
| 4719 | p++; |
| 4720 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4721 | p++; |
| 4722 | if (p >= cur_ptr+cur_hdr->len) |
| 4723 | continue; |
| 4724 | hv = p; |
| 4725 | hvl = cur_ptr+cur_hdr->len-p; |
| 4726 | |
| 4727 | /* Lowercase the key. Don't check the size of trash, it have |
| 4728 | * the size of one buffer and the input data contains in one |
| 4729 | * buffer. |
| 4730 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4731 | out = trash.area; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4732 | for (in=hn; in<hn+hnl; in++, out++) |
| 4733 | *out = tolower(*in); |
| 4734 | *out = '\0'; |
| 4735 | |
| 4736 | /* Check for existing entry: |
| 4737 | * assume that the table is on the top of the stack, and |
| 4738 | * push the key in the stack, the function lua_gettable() |
| 4739 | * perform the lookup. |
| 4740 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4741 | lua_pushlstring(L, trash.area, hnl); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4742 | lua_gettable(L, -2); |
| 4743 | type = lua_type(L, -1); |
| 4744 | |
| 4745 | switch (type) { |
| 4746 | case LUA_TNIL: |
| 4747 | /* Table not found, create it. */ |
| 4748 | lua_pop(L, 1); /* remove the nil value. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4749 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4750 | lua_newtable(L); /* create and push empty table. */ |
| 4751 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4752 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4753 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4754 | break; |
| 4755 | |
| 4756 | case LUA_TTABLE: |
| 4757 | /* Entry found: push the value in the table. */ |
| 4758 | len = lua_rawlen(L, -1); |
| 4759 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4760 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4761 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4762 | break; |
| 4763 | |
| 4764 | default: |
| 4765 | /* Other cases are errors. */ |
| 4766 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4767 | WILL_LJMP(lua_error(L)); |
| 4768 | } |
| 4769 | } |
| 4770 | |
| 4771 | return 1; |
| 4772 | } |
| 4773 | |
| 4774 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4775 | { |
| 4776 | struct hlua_txn *htxn; |
| 4777 | |
| 4778 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4779 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4780 | |
| 4781 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4782 | } |
| 4783 | |
| 4784 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4785 | { |
| 4786 | struct hlua_txn *htxn; |
| 4787 | |
| 4788 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4789 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4790 | |
| 4791 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4792 | } |
| 4793 | |
| 4794 | /* This function replace full header, or just a value in |
| 4795 | * the request or in the response. It is a wrapper fir the |
| 4796 | * 4 following functions. |
| 4797 | */ |
| 4798 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4799 | struct http_msg *msg, int action) |
| 4800 | { |
| 4801 | size_t name_len; |
| 4802 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4803 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4804 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4805 | struct my_regex re; |
| 4806 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4807 | /* Check if a valid response is parsed */ |
| 4808 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4809 | return 0; |
| 4810 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4811 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4812 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4813 | |
| 4814 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4815 | regex_free(&re); |
| 4816 | return 0; |
| 4817 | } |
| 4818 | |
| 4819 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4820 | { |
| 4821 | struct hlua_txn *htxn; |
| 4822 | |
| 4823 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4824 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4825 | |
| 4826 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4827 | } |
| 4828 | |
| 4829 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4830 | { |
| 4831 | struct hlua_txn *htxn; |
| 4832 | |
| 4833 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4834 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4835 | |
| 4836 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4837 | } |
| 4838 | |
| 4839 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4840 | { |
| 4841 | struct hlua_txn *htxn; |
| 4842 | |
| 4843 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4844 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4845 | |
| 4846 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4847 | } |
| 4848 | |
| 4849 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4850 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4851 | struct hlua_txn *htxn; |
| 4852 | |
| 4853 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4854 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4855 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4856 | 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] | 4857 | } |
| 4858 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4859 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4860 | * It is a wrapper for the 2 following functions. |
| 4861 | */ |
| 4862 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4863 | { |
| 4864 | size_t len; |
| 4865 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4866 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4867 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4868 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4869 | /* Check if a valid response is parsed */ |
| 4870 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4871 | return 0; |
| 4872 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4873 | ctx.idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 4874 | 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] | 4875 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4876 | return 0; |
| 4877 | } |
| 4878 | |
| 4879 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4880 | { |
| 4881 | struct hlua_txn *htxn; |
| 4882 | |
| 4883 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4884 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4885 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4886 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4887 | } |
| 4888 | |
| 4889 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4890 | { |
| 4891 | struct hlua_txn *htxn; |
| 4892 | |
| 4893 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4894 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4895 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4896 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4897 | } |
| 4898 | |
| 4899 | /* This function adds an header. It is a wrapper used by |
| 4900 | * the 2 following functions. |
| 4901 | */ |
| 4902 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4903 | { |
| 4904 | size_t name_len; |
| 4905 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4906 | size_t value_len; |
| 4907 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4908 | char *p; |
| 4909 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4910 | /* Check if a valid message is parsed */ |
| 4911 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4912 | return 0; |
| 4913 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4914 | /* Check length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4915 | trash.data = value_len + name_len + 2; |
| 4916 | if (trash.data > trash.size) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4917 | return 0; |
| 4918 | |
| 4919 | /* Creates the header string. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4920 | p = trash.area; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4921 | memcpy(p, name, name_len); |
| 4922 | p += name_len; |
| 4923 | *p = ':'; |
| 4924 | p++; |
| 4925 | *p = ' '; |
| 4926 | p++; |
| 4927 | memcpy(p, value, value_len); |
| 4928 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4929 | 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] | 4930 | trash.area, trash.data) != 0); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4931 | |
| 4932 | return 0; |
| 4933 | } |
| 4934 | |
| 4935 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4936 | { |
| 4937 | struct hlua_txn *htxn; |
| 4938 | |
| 4939 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4940 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4941 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4942 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4943 | } |
| 4944 | |
| 4945 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4946 | { |
| 4947 | struct hlua_txn *htxn; |
| 4948 | |
| 4949 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4950 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4951 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4952 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4953 | } |
| 4954 | |
| 4955 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4956 | { |
| 4957 | struct hlua_txn *htxn; |
| 4958 | |
| 4959 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4960 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4961 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4962 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4963 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4964 | } |
| 4965 | |
| 4966 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4967 | { |
| 4968 | struct hlua_txn *htxn; |
| 4969 | |
| 4970 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4971 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4972 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4973 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4974 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4975 | } |
| 4976 | |
| 4977 | /* This function set the method. */ |
| 4978 | static int hlua_http_req_set_meth(lua_State *L) |
| 4979 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4980 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4981 | size_t name_len; |
| 4982 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4983 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4984 | /* Check if a valid request is parsed */ |
| 4985 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4986 | lua_pushboolean(L, 0); |
| 4987 | return 1; |
| 4988 | } |
| 4989 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4990 | 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] | 4991 | return 1; |
| 4992 | } |
| 4993 | |
| 4994 | /* This function set the method. */ |
| 4995 | static int hlua_http_req_set_path(lua_State *L) |
| 4996 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4997 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4998 | size_t name_len; |
| 4999 | 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] | 5000 | |
| 5001 | /* Check if a valid request is parsed */ |
| 5002 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5003 | lua_pushboolean(L, 0); |
| 5004 | return 1; |
| 5005 | } |
| 5006 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5007 | 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] | 5008 | return 1; |
| 5009 | } |
| 5010 | |
| 5011 | /* This function set the query-string. */ |
| 5012 | static int hlua_http_req_set_query(lua_State *L) |
| 5013 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5014 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5015 | size_t name_len; |
| 5016 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5017 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5018 | /* Check if a valid request is parsed */ |
| 5019 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5020 | lua_pushboolean(L, 0); |
| 5021 | return 1; |
| 5022 | } |
| 5023 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5024 | /* Check length. */ |
| 5025 | if (name_len > trash.size - 1) { |
| 5026 | lua_pushboolean(L, 0); |
| 5027 | return 1; |
| 5028 | } |
| 5029 | |
| 5030 | /* Add the mark question as prefix. */ |
| 5031 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5032 | trash.area[trash.data++] = '?'; |
| 5033 | memcpy(trash.area + trash.data, name, name_len); |
| 5034 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5035 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5036 | lua_pushboolean(L, |
| 5037 | 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] | 5038 | return 1; |
| 5039 | } |
| 5040 | |
| 5041 | /* This function set the uri. */ |
| 5042 | static int hlua_http_req_set_uri(lua_State *L) |
| 5043 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5044 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5045 | size_t name_len; |
| 5046 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5047 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5048 | /* Check if a valid request is parsed */ |
| 5049 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5050 | lua_pushboolean(L, 0); |
| 5051 | return 1; |
| 5052 | } |
| 5053 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5054 | 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] | 5055 | return 1; |
| 5056 | } |
| 5057 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5058 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5059 | static int hlua_http_res_set_status(lua_State *L) |
| 5060 | { |
| 5061 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5062 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5063 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5064 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5065 | /* Check if a valid response is parsed */ |
| 5066 | if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5067 | return 0; |
| 5068 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5069 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5070 | return 0; |
| 5071 | } |
| 5072 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5073 | /* |
| 5074 | * |
| 5075 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5076 | * Class TXN |
| 5077 | * |
| 5078 | * |
| 5079 | */ |
| 5080 | |
| 5081 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5082 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5083 | */ |
| 5084 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5085 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5086 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5087 | } |
| 5088 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5089 | __LJMP static int hlua_set_var(lua_State *L) |
| 5090 | { |
| 5091 | struct hlua_txn *htxn; |
| 5092 | const char *name; |
| 5093 | size_t len; |
| 5094 | struct sample smp; |
| 5095 | |
| 5096 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5097 | |
| 5098 | /* It is useles to retrieve the stream, but this function |
| 5099 | * runs only in a stream context. |
| 5100 | */ |
| 5101 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5102 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5103 | |
| 5104 | /* Converts the third argument in a sample. */ |
| 5105 | hlua_lua2smp(L, 3, &smp); |
| 5106 | |
| 5107 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5108 | 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] | 5109 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5110 | return 0; |
| 5111 | } |
| 5112 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5113 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5114 | { |
| 5115 | struct hlua_txn *htxn; |
| 5116 | const char *name; |
| 5117 | size_t len; |
| 5118 | struct sample smp; |
| 5119 | |
| 5120 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5121 | |
| 5122 | /* It is useles to retrieve the stream, but this function |
| 5123 | * runs only in a stream context. |
| 5124 | */ |
| 5125 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5126 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5127 | |
| 5128 | /* Unset the variable. */ |
| 5129 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5130 | vars_unset_by_name(name, len, &smp); |
| 5131 | return 0; |
| 5132 | } |
| 5133 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5134 | __LJMP static int hlua_get_var(lua_State *L) |
| 5135 | { |
| 5136 | struct hlua_txn *htxn; |
| 5137 | const char *name; |
| 5138 | size_t len; |
| 5139 | struct sample smp; |
| 5140 | |
| 5141 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5142 | |
| 5143 | /* It is useles to retrieve the stream, but this function |
| 5144 | * runs only in a stream context. |
| 5145 | */ |
| 5146 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5147 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5148 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5149 | 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] | 5150 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5151 | lua_pushnil(L); |
| 5152 | return 1; |
| 5153 | } |
| 5154 | |
| 5155 | return hlua_smp2lua(L, &smp); |
| 5156 | } |
| 5157 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5158 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5159 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5160 | struct hlua *hlua; |
| 5161 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5162 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5163 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5164 | /* It is useles to retrieve the stream, but this function |
| 5165 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5166 | */ |
| 5167 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5168 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5169 | |
| 5170 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5171 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5172 | |
| 5173 | /* Get and store new value. */ |
| 5174 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5175 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5176 | |
| 5177 | return 0; |
| 5178 | } |
| 5179 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5180 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5181 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5182 | struct hlua *hlua; |
| 5183 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5184 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5185 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5186 | /* It is useles to retrieve the stream, but this function |
| 5187 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5188 | */ |
| 5189 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5190 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5191 | |
| 5192 | /* Push configuration index in the stack. */ |
| 5193 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5194 | |
| 5195 | return 1; |
| 5196 | } |
| 5197 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5198 | /* Create stack entry containing a class TXN. This function |
| 5199 | * return 0 if the stack does not contains free slots, |
| 5200 | * otherwise it returns 1. |
| 5201 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5202 | 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] | 5203 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5204 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5205 | |
| 5206 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5207 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5208 | return 0; |
| 5209 | |
| 5210 | /* NOTE: The allocation never fails. The failure |
| 5211 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5212 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5213 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5214 | /* Create the object: obj[0] = userdata. */ |
| 5215 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5216 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5217 | lua_rawseti(L, -2, 0); |
| 5218 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5219 | htxn->s = s; |
| 5220 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5221 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5222 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5223 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5224 | /* Create the "f" field that contains a list of fetches. */ |
| 5225 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5226 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5227 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5228 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5229 | |
| 5230 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5231 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5232 | 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] | 5233 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5234 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5235 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5236 | /* Create the "c" field that contains a list of converters. */ |
| 5237 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5238 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5239 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5240 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5241 | |
| 5242 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5243 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5244 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5245 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5246 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5247 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5248 | /* Create the "req" field that contains the request channel object. */ |
| 5249 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5250 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5251 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5252 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5253 | |
| 5254 | /* Create the "res" field that contains the response channel object. */ |
| 5255 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5256 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5257 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5258 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5259 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5260 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5261 | lua_pushstring(L, "http"); |
| 5262 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5263 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5264 | return 0; |
| 5265 | } |
| 5266 | else |
| 5267 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5268 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5269 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5270 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5271 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5272 | lua_setmetatable(L, -2); |
| 5273 | |
| 5274 | return 1; |
| 5275 | } |
| 5276 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5277 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5278 | { |
| 5279 | const char *msg; |
| 5280 | struct hlua_txn *htxn; |
| 5281 | |
| 5282 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5283 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5284 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5285 | |
| 5286 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5287 | return 0; |
| 5288 | } |
| 5289 | |
| 5290 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5291 | { |
| 5292 | int level; |
| 5293 | const char *msg; |
| 5294 | struct hlua_txn *htxn; |
| 5295 | |
| 5296 | MAY_LJMP(check_args(L, 3, "log")); |
| 5297 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5298 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5299 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5300 | |
| 5301 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5302 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5303 | |
| 5304 | hlua_sendlog(htxn->s->be, level, msg); |
| 5305 | return 0; |
| 5306 | } |
| 5307 | |
| 5308 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5309 | { |
| 5310 | const char *msg; |
| 5311 | struct hlua_txn *htxn; |
| 5312 | |
| 5313 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5314 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5315 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5316 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5317 | return 0; |
| 5318 | } |
| 5319 | |
| 5320 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5321 | { |
| 5322 | const char *msg; |
| 5323 | struct hlua_txn *htxn; |
| 5324 | |
| 5325 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5326 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5327 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5328 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5329 | return 0; |
| 5330 | } |
| 5331 | |
| 5332 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5333 | { |
| 5334 | const char *msg; |
| 5335 | struct hlua_txn *htxn; |
| 5336 | |
| 5337 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5338 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5339 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5340 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5341 | return 0; |
| 5342 | } |
| 5343 | |
| 5344 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5345 | { |
| 5346 | const char *msg; |
| 5347 | struct hlua_txn *htxn; |
| 5348 | |
| 5349 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5350 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5351 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5352 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5353 | return 0; |
| 5354 | } |
| 5355 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5356 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5357 | { |
| 5358 | struct hlua_txn *htxn; |
| 5359 | int ll; |
| 5360 | |
| 5361 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5362 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5363 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5364 | |
| 5365 | if (ll < 0 || ll > 7) |
| 5366 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5367 | |
| 5368 | htxn->s->logs.level = ll; |
| 5369 | return 0; |
| 5370 | } |
| 5371 | |
| 5372 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5373 | { |
| 5374 | struct hlua_txn *htxn; |
| 5375 | struct connection *cli_conn; |
| 5376 | int tos; |
| 5377 | |
| 5378 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5379 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5380 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5381 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5382 | 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] | 5383 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5384 | |
| 5385 | return 0; |
| 5386 | } |
| 5387 | |
| 5388 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5389 | { |
| 5390 | #ifdef SO_MARK |
| 5391 | struct hlua_txn *htxn; |
| 5392 | struct connection *cli_conn; |
| 5393 | int mark; |
| 5394 | |
| 5395 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5396 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5397 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5398 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5399 | 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] | 5400 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5401 | #endif |
| 5402 | return 0; |
| 5403 | } |
| 5404 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5405 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5406 | { |
| 5407 | struct hlua_txn *htxn; |
| 5408 | |
| 5409 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5410 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5411 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5412 | return 0; |
| 5413 | } |
| 5414 | |
| 5415 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5416 | { |
| 5417 | struct hlua_txn *htxn; |
| 5418 | |
| 5419 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5420 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5421 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5422 | return 0; |
| 5423 | } |
| 5424 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5425 | /* This function is an Lua binding that send pending data |
| 5426 | * to the client, and close the stream interface. |
| 5427 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5428 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5429 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5430 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5431 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5432 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5433 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5434 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5435 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5436 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5437 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5438 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5439 | * session, so we just end the execution of the current |
| 5440 | * lua code. |
| 5441 | */ |
| 5442 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5443 | WILL_LJMP(hlua_done(L)); |
| 5444 | return 0; |
| 5445 | } |
| 5446 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5447 | ic = &htxn->s->req; |
| 5448 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5449 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5450 | if (htxn->s->txn) { |
| 5451 | /* HTTP mode, let's stay in sync with the stream */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5452 | b_del(&ic->buf, htxn->s->txn->req.sov); |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5453 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 5454 | htxn->s->txn->req.sov = 0; |
| 5455 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 5456 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 5457 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5458 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 5459 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5460 | /* Note that if we want to support keep-alive, we need |
| 5461 | * to bypass the close/shutr_now calls below, but that |
| 5462 | * may only be done if the HTTP request was already |
| 5463 | * processed and the connection header is known (ie |
| 5464 | * not during TCP rules). |
| 5465 | */ |
| 5466 | } |
| 5467 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5468 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5469 | channel_abort(ic); |
| 5470 | channel_auto_close(ic); |
| 5471 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5472 | |
| 5473 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5474 | channel_auto_read(oc); |
| 5475 | channel_auto_close(oc); |
| 5476 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5477 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 5478 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5479 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5480 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5481 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5482 | return 0; |
| 5483 | } |
| 5484 | |
| 5485 | __LJMP static int hlua_log(lua_State *L) |
| 5486 | { |
| 5487 | int level; |
| 5488 | const char *msg; |
| 5489 | |
| 5490 | MAY_LJMP(check_args(L, 2, "log")); |
| 5491 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5492 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5493 | |
| 5494 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5495 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5496 | |
| 5497 | hlua_sendlog(NULL, level, msg); |
| 5498 | return 0; |
| 5499 | } |
| 5500 | |
| 5501 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5502 | { |
| 5503 | const char *msg; |
| 5504 | |
| 5505 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5506 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5507 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5508 | return 0; |
| 5509 | } |
| 5510 | |
| 5511 | __LJMP static int hlua_log_info(lua_State *L) |
| 5512 | { |
| 5513 | const char *msg; |
| 5514 | |
| 5515 | MAY_LJMP(check_args(L, 1, "info")); |
| 5516 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5517 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5518 | return 0; |
| 5519 | } |
| 5520 | |
| 5521 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5522 | { |
| 5523 | const char *msg; |
| 5524 | |
| 5525 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5526 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5527 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5528 | return 0; |
| 5529 | } |
| 5530 | |
| 5531 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5532 | { |
| 5533 | const char *msg; |
| 5534 | |
| 5535 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5536 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5537 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5538 | return 0; |
| 5539 | } |
| 5540 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5541 | __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] | 5542 | { |
| 5543 | int wakeup_ms = lua_tointeger(L, -1); |
| 5544 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5545 | 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] | 5546 | return 0; |
| 5547 | } |
| 5548 | |
| 5549 | __LJMP static int hlua_sleep(lua_State *L) |
| 5550 | { |
| 5551 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5552 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5553 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5554 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5555 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5556 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5557 | wakeup_ms = tick_add(now_ms, delay); |
| 5558 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5559 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5560 | 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] | 5561 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5562 | } |
| 5563 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5564 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5565 | { |
| 5566 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5567 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5568 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5569 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5570 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5571 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5572 | wakeup_ms = tick_add(now_ms, delay); |
| 5573 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5574 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5575 | 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] | 5576 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5577 | } |
| 5578 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5579 | /* This functionis an LUA binding. it permits to give back |
| 5580 | * the hand at the HAProxy scheduler. It is used when the |
| 5581 | * LUA processing consumes a lot of time. |
| 5582 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5583 | __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] | 5584 | { |
| 5585 | return 0; |
| 5586 | } |
| 5587 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5588 | __LJMP static int hlua_yield(lua_State *L) |
| 5589 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5590 | 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] | 5591 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5592 | } |
| 5593 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5594 | /* This function change the nice of the currently executed |
| 5595 | * task. It is used set low or high priority at the current |
| 5596 | * task. |
| 5597 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5598 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5599 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5600 | struct hlua *hlua; |
| 5601 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5602 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5603 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5604 | hlua = hlua_gethlua(L); |
| 5605 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5606 | |
| 5607 | /* If he task is not set, I'm in a start mode. */ |
| 5608 | if (!hlua || !hlua->task) |
| 5609 | return 0; |
| 5610 | |
| 5611 | if (nice < -1024) |
| 5612 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5613 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5614 | nice = 1024; |
| 5615 | |
| 5616 | hlua->task->nice = nice; |
| 5617 | return 0; |
| 5618 | } |
| 5619 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5620 | /* 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] | 5621 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5622 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5623 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5624 | * |
| 5625 | * Task wrapper are longjmp safe because the only one Lua code |
| 5626 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5627 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5628 | 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] | 5629 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5630 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5631 | enum hlua_exec status; |
| 5632 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 5633 | if (task->thread_mask == MAX_THREADS_MASK) |
| 5634 | task_set_affinity(task, tid_bit); |
| 5635 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5636 | /* If it is the first call to the task, we must initialize the |
| 5637 | * execution timeouts. |
| 5638 | */ |
| 5639 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5640 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5641 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5642 | /* Execute the Lua code. */ |
| 5643 | status = hlua_ctx_resume(hlua, 1); |
| 5644 | |
| 5645 | switch (status) { |
| 5646 | /* finished or yield */ |
| 5647 | case HLUA_E_OK: |
| 5648 | hlua_ctx_destroy(hlua); |
| 5649 | task_delete(task); |
| 5650 | task_free(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 5651 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5652 | break; |
| 5653 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5654 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 5655 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 5656 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5657 | break; |
| 5658 | |
| 5659 | /* finished with error. */ |
| 5660 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5661 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5662 | hlua_ctx_destroy(hlua); |
| 5663 | task_delete(task); |
| 5664 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5665 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5666 | break; |
| 5667 | |
| 5668 | case HLUA_E_ERR: |
| 5669 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5670 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5671 | hlua_ctx_destroy(hlua); |
| 5672 | task_delete(task); |
| 5673 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5674 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5675 | break; |
| 5676 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5677 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5678 | } |
| 5679 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5680 | /* This function is an LUA binding that register LUA function to be |
| 5681 | * executed after the HAProxy configuration parsing and before the |
| 5682 | * HAProxy scheduler starts. This function expect only one LUA |
| 5683 | * argument that is a function. This function returns nothing, but |
| 5684 | * throws if an error is encountered. |
| 5685 | */ |
| 5686 | __LJMP static int hlua_register_init(lua_State *L) |
| 5687 | { |
| 5688 | struct hlua_init_function *init; |
| 5689 | int ref; |
| 5690 | |
| 5691 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5692 | |
| 5693 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5694 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5695 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5696 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5697 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5698 | |
| 5699 | init->function_ref = ref; |
| 5700 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5701 | return 0; |
| 5702 | } |
| 5703 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5704 | /* This functio is an LUA binding. It permits to register a task |
| 5705 | * executed in parallel of the main HAroxy activity. The task is |
| 5706 | * created and it is set in the HAProxy scheduler. It can be called |
| 5707 | * from the "init" section, "post init" or during the runtime. |
| 5708 | * |
| 5709 | * Lua prototype: |
| 5710 | * |
| 5711 | * <none> core.register_task(<function>) |
| 5712 | */ |
| 5713 | static int hlua_register_task(lua_State *L) |
| 5714 | { |
| 5715 | struct hlua *hlua; |
| 5716 | struct task *task; |
| 5717 | int ref; |
| 5718 | |
| 5719 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5720 | |
| 5721 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5722 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5723 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5724 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5725 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5726 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 5727 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 5728 | if (!task) |
| 5729 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 5730 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5731 | task->context = hlua; |
| 5732 | task->process = hlua_process_task; |
| 5733 | |
| 5734 | if (!hlua_ctx_init(hlua, task)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5735 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5736 | |
| 5737 | /* Restore the function in the stack. */ |
| 5738 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5739 | hlua->nargs = 0; |
| 5740 | |
| 5741 | /* Schedule task. */ |
| 5742 | task_schedule(task, now_ms); |
| 5743 | |
| 5744 | return 0; |
| 5745 | } |
| 5746 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5747 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5748 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5749 | * resume converters. |
| 5750 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5751 | 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] | 5752 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5753 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5754 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5755 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5756 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5757 | if (!stream) |
| 5758 | return 0; |
| 5759 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 5760 | if (IS_HTX_STRM(stream)) { |
| 5761 | SEND_ERR(stream->be, "Lua converter '%s': Lua fetches cannot be used when the" |
| 5762 | " HTX internal representation is enabled.\n", fcn->name); |
| 5763 | return 0; |
| 5764 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5765 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5766 | * Lua context can be not initialized. This behavior |
| 5767 | * permits to save performances because a systematic |
| 5768 | * Lua initialization cause 5% performances loss. |
| 5769 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5770 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5771 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5772 | if (!stream->hlua) { |
| 5773 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5774 | return 0; |
| 5775 | } |
| 5776 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5777 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5778 | return 0; |
| 5779 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5780 | } |
| 5781 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5782 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5783 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5784 | |
| 5785 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5786 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5787 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5788 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5789 | else |
| 5790 | error = "critical error"; |
| 5791 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5792 | return 0; |
| 5793 | } |
| 5794 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5795 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5796 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5797 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5798 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5799 | return 0; |
| 5800 | } |
| 5801 | |
| 5802 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5803 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5804 | |
| 5805 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5806 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5807 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5808 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5809 | return 0; |
| 5810 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5811 | hlua_smp2lua(stream->hlua->T, smp); |
| 5812 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5813 | |
| 5814 | /* push keywords in the stack. */ |
| 5815 | if (arg_p) { |
| 5816 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5817 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5818 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5819 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5820 | return 0; |
| 5821 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5822 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5823 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5824 | } |
| 5825 | } |
| 5826 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5827 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5828 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5829 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5830 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5831 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5832 | } |
| 5833 | |
| 5834 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5835 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5836 | /* finished. */ |
| 5837 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5838 | /* If the stack is empty, the function fails. */ |
| 5839 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5840 | return 0; |
| 5841 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5842 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5843 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5844 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5845 | return 1; |
| 5846 | |
| 5847 | /* yield. */ |
| 5848 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5849 | 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] | 5850 | return 0; |
| 5851 | |
| 5852 | /* finished with error. */ |
| 5853 | case HLUA_E_ERRMSG: |
| 5854 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5855 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5856 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5857 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5858 | return 0; |
| 5859 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5860 | case HLUA_E_ETMOUT: |
| 5861 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 5862 | return 0; |
| 5863 | |
| 5864 | case HLUA_E_NOMEM: |
| 5865 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 5866 | return 0; |
| 5867 | |
| 5868 | case HLUA_E_YIELD: |
| 5869 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 5870 | return 0; |
| 5871 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5872 | case HLUA_E_ERR: |
| 5873 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5874 | 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] | 5875 | |
| 5876 | default: |
| 5877 | return 0; |
| 5878 | } |
| 5879 | } |
| 5880 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5881 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5882 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5883 | * resume sample-fetches. This function will be called by the sample |
| 5884 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5885 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5886 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5887 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5888 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5889 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5890 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5891 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5892 | const struct buffer msg = { }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5893 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5894 | if (!stream) |
| 5895 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 5896 | |
| 5897 | if (IS_HTX_STRM(stream)) { |
| 5898 | SEND_ERR(stream->be, "Lua sample-fetch '%s': Lua fetches cannot be used when the" |
| 5899 | " HTX internal representation is enabled.\n", fcn->name); |
| 5900 | return 0; |
| 5901 | } |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5902 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5903 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5904 | * Lua context can be not initialized. This behavior |
| 5905 | * permits to save performances because a systematic |
| 5906 | * Lua initialization cause 5% performances loss. |
| 5907 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5908 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5909 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5910 | if (!stream->hlua) { |
| 5911 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5912 | return 0; |
| 5913 | } |
| 5914 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5915 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5916 | return 0; |
| 5917 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5918 | } |
| 5919 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5920 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5921 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5922 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5923 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5924 | |
| 5925 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5926 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5927 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5928 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5929 | else |
| 5930 | error = "critical error"; |
| 5931 | 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] | 5932 | return 0; |
| 5933 | } |
| 5934 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5935 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5936 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5937 | 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] | 5938 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5939 | return 0; |
| 5940 | } |
| 5941 | |
| 5942 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5943 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5944 | |
| 5945 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5946 | 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] | 5947 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5948 | 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] | 5949 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5950 | return 0; |
| 5951 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5952 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5953 | |
| 5954 | /* push keywords in the stack. */ |
| 5955 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5956 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5957 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5958 | 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] | 5959 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5960 | return 0; |
| 5961 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5962 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5963 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5964 | } |
| 5965 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5966 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5967 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5968 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5969 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5970 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5971 | } |
| 5972 | |
| 5973 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5974 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5975 | /* finished. */ |
| 5976 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5977 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5978 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5979 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5980 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5981 | /* If the stack is empty, the function fails. */ |
| 5982 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5983 | return 0; |
| 5984 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5985 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5986 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5987 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5988 | |
| 5989 | /* Set the end of execution flag. */ |
| 5990 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5991 | return 1; |
| 5992 | |
| 5993 | /* yield. */ |
| 5994 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5995 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5996 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5997 | 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] | 5998 | return 0; |
| 5999 | |
| 6000 | /* finished with error. */ |
| 6001 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6002 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6003 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6004 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6005 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6006 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6007 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6008 | return 0; |
| 6009 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6010 | case HLUA_E_ETMOUT: |
| 6011 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 6012 | stream_int_retnclose(&stream->si[0], &msg); |
| 6013 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6014 | return 0; |
| 6015 | |
| 6016 | case HLUA_E_NOMEM: |
| 6017 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 6018 | stream_int_retnclose(&stream->si[0], &msg); |
| 6019 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6020 | return 0; |
| 6021 | |
| 6022 | case HLUA_E_YIELD: |
| 6023 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 6024 | stream_int_retnclose(&stream->si[0], &msg); |
| 6025 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6026 | return 0; |
| 6027 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6028 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6029 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6030 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6031 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6032 | 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] | 6033 | |
| 6034 | default: |
| 6035 | return 0; |
| 6036 | } |
| 6037 | } |
| 6038 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6039 | /* This function is an LUA binding used for registering |
| 6040 | * "sample-conv" functions. It expects a converter name used |
| 6041 | * in the haproxy configuration file, and an LUA function. |
| 6042 | */ |
| 6043 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6044 | { |
| 6045 | struct sample_conv_kw_list *sck; |
| 6046 | const char *name; |
| 6047 | int ref; |
| 6048 | int len; |
| 6049 | struct hlua_function *fcn; |
| 6050 | |
| 6051 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6052 | |
| 6053 | /* First argument : converter name. */ |
| 6054 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6055 | |
| 6056 | /* Second argument : lua function. */ |
| 6057 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6058 | |
| 6059 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6060 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6061 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6062 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6063 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6064 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6065 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6066 | |
| 6067 | /* Fill fcn. */ |
| 6068 | fcn->name = strdup(name); |
| 6069 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6070 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6071 | fcn->function_ref = ref; |
| 6072 | |
| 6073 | /* List head */ |
| 6074 | sck->list.n = sck->list.p = NULL; |
| 6075 | |
| 6076 | /* converter keyword. */ |
| 6077 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6078 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6079 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6080 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6081 | |
| 6082 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6083 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6084 | 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] | 6085 | sck->kw[0].val_args = NULL; |
| 6086 | sck->kw[0].in_type = SMP_T_STR; |
| 6087 | sck->kw[0].out_type = SMP_T_STR; |
| 6088 | sck->kw[0].private = fcn; |
| 6089 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6090 | /* Register this new converter */ |
| 6091 | sample_register_convs(sck); |
| 6092 | |
| 6093 | return 0; |
| 6094 | } |
| 6095 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6096 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6097 | * "sample-fetch" functions. It expects a converter name used |
| 6098 | * in the haproxy configuration file, and an LUA function. |
| 6099 | */ |
| 6100 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6101 | { |
| 6102 | const char *name; |
| 6103 | int ref; |
| 6104 | int len; |
| 6105 | struct sample_fetch_kw_list *sfk; |
| 6106 | struct hlua_function *fcn; |
| 6107 | |
| 6108 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6109 | |
| 6110 | /* First argument : sample-fetch name. */ |
| 6111 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6112 | |
| 6113 | /* Second argument : lua function. */ |
| 6114 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6115 | |
| 6116 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6117 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6118 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6119 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6120 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6121 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6122 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6123 | |
| 6124 | /* Fill fcn. */ |
| 6125 | fcn->name = strdup(name); |
| 6126 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6127 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6128 | fcn->function_ref = ref; |
| 6129 | |
| 6130 | /* List head */ |
| 6131 | sfk->list.n = sfk->list.p = NULL; |
| 6132 | |
| 6133 | /* sample-fetch keyword. */ |
| 6134 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6135 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6136 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6137 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6138 | |
| 6139 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6140 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6141 | 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] | 6142 | sfk->kw[0].val_args = NULL; |
| 6143 | sfk->kw[0].out_type = SMP_T_STR; |
| 6144 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6145 | sfk->kw[0].val = 0; |
| 6146 | sfk->kw[0].private = fcn; |
| 6147 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6148 | /* Register this new fetch. */ |
| 6149 | sample_register_fetches(sfk); |
| 6150 | |
| 6151 | return 0; |
| 6152 | } |
| 6153 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6154 | /* This function is a wrapper to execute each LUA function declared |
| 6155 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6156 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6157 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6158 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6159 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6160 | 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] | 6161 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6162 | { |
| 6163 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6164 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6165 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6166 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6167 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6168 | |
| 6169 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 6170 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 6171 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 6172 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 6173 | 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] | 6174 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6175 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6176 | return ACT_RET_CONT; |
| 6177 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6178 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6179 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6180 | * Lua context can be not initialized. This behavior |
| 6181 | * permits to save performances because a systematic |
| 6182 | * Lua initialization cause 5% performances loss. |
| 6183 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6184 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6185 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6186 | if (!s->hlua) { |
| 6187 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6188 | rule->arg.hlua_rule->fcn.name); |
| 6189 | return ACT_RET_CONT; |
| 6190 | } |
| 6191 | if (!hlua_ctx_init(s->hlua, s->task)) { |
| 6192 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6193 | rule->arg.hlua_rule->fcn.name); |
| 6194 | return ACT_RET_CONT; |
| 6195 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6196 | } |
| 6197 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6198 | consistency_set(s, dir, &s->hlua->cons); |
| 6199 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6200 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6201 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6202 | |
| 6203 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6204 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6205 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6206 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6207 | else |
| 6208 | error = "critical error"; |
| 6209 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6210 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6211 | return ACT_RET_CONT; |
| 6212 | } |
| 6213 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6214 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6215 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6216 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6217 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6218 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6219 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6220 | } |
| 6221 | |
| 6222 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6223 | 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] | 6224 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6225 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6226 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6227 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6228 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6229 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6230 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6231 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6232 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6233 | |
| 6234 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6235 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6236 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6237 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6238 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6239 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6240 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6241 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6242 | lua_pushstring(s->hlua->T, *arg); |
| 6243 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6244 | } |
| 6245 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6246 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6247 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6248 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6249 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6250 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6251 | } |
| 6252 | |
| 6253 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6254 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6255 | /* finished. */ |
| 6256 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6257 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6258 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6259 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6260 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6261 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6262 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6263 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6264 | |
| 6265 | /* yield. */ |
| 6266 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6267 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6268 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6269 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6270 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6271 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6272 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6273 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6274 | /* Some actions can be wake up when a "write" event |
| 6275 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6276 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6277 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6278 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6279 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6280 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6281 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6282 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6283 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6284 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6285 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6286 | * because HAProxy is not able to manipulate data, it |
| 6287 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6288 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6289 | |
| 6290 | /* finished with error. */ |
| 6291 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6292 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6293 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6294 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6295 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6296 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6297 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6298 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6299 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6300 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6301 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6302 | case HLUA_E_ETMOUT: |
| 6303 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6304 | stream_int_retnclose(&s->si[0], &msg); |
| 6305 | return ACT_RET_ERR; |
| 6306 | } |
| 6307 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6308 | return 0; |
| 6309 | |
| 6310 | case HLUA_E_NOMEM: |
| 6311 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6312 | stream_int_retnclose(&s->si[0], &msg); |
| 6313 | return ACT_RET_ERR; |
| 6314 | } |
| 6315 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6316 | return 0; |
| 6317 | |
| 6318 | case HLUA_E_YIELD: |
| 6319 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6320 | stream_int_retnclose(&s->si[0], &msg); |
| 6321 | return ACT_RET_ERR; |
| 6322 | } |
| 6323 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6324 | rule->arg.hlua_rule->fcn.name); |
| 6325 | return 0; |
| 6326 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6327 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6328 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6329 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6330 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6331 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6332 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6333 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6334 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6335 | |
| 6336 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6337 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6338 | } |
| 6339 | } |
| 6340 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6341 | 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] | 6342 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6343 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6344 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6345 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6346 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6347 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6348 | } |
| 6349 | |
| 6350 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6351 | { |
| 6352 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6353 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6354 | struct task *task; |
| 6355 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6356 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6357 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6358 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6359 | if (!hlua) { |
| 6360 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6361 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6362 | return 0; |
| 6363 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6364 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6365 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6366 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6367 | |
| 6368 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6369 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6370 | if (!task) { |
| 6371 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6372 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6373 | return 0; |
| 6374 | } |
| 6375 | task->nice = 0; |
| 6376 | task->context = ctx; |
| 6377 | task->process = hlua_applet_wakeup; |
| 6378 | ctx->ctx.hlua_apptcp.task = task; |
| 6379 | |
| 6380 | /* In the execution wrappers linked with a stream, the |
| 6381 | * Lua context can be not initialized. This behavior |
| 6382 | * permits to save performances because a systematic |
| 6383 | * Lua initialization cause 5% performances loss. |
| 6384 | */ |
| 6385 | if (!hlua_ctx_init(hlua, task)) { |
| 6386 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6387 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6388 | return 0; |
| 6389 | } |
| 6390 | |
| 6391 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6392 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6393 | |
| 6394 | /* The following Lua calls can fail. */ |
| 6395 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6396 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6397 | error = lua_tostring(hlua->T, -1); |
| 6398 | else |
| 6399 | error = "critical error"; |
| 6400 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6401 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6402 | return 0; |
| 6403 | } |
| 6404 | |
| 6405 | /* Check stack available size. */ |
| 6406 | if (!lua_checkstack(hlua->T, 1)) { |
| 6407 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6408 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6409 | RESET_SAFE_LJMP(hlua->T); |
| 6410 | return 0; |
| 6411 | } |
| 6412 | |
| 6413 | /* Restore the function in the stack. */ |
| 6414 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6415 | |
| 6416 | /* Create and and push object stream in the stack. */ |
| 6417 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6418 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6419 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6420 | RESET_SAFE_LJMP(hlua->T); |
| 6421 | return 0; |
| 6422 | } |
| 6423 | hlua->nargs = 1; |
| 6424 | |
| 6425 | /* push keywords in the stack. */ |
| 6426 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6427 | if (!lua_checkstack(hlua->T, 1)) { |
| 6428 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6429 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6430 | RESET_SAFE_LJMP(hlua->T); |
| 6431 | return 0; |
| 6432 | } |
| 6433 | lua_pushstring(hlua->T, *arg); |
| 6434 | hlua->nargs++; |
| 6435 | } |
| 6436 | |
| 6437 | RESET_SAFE_LJMP(hlua->T); |
| 6438 | |
| 6439 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6440 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6441 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6442 | |
| 6443 | return 1; |
| 6444 | } |
| 6445 | |
| 6446 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6447 | { |
| 6448 | struct stream_interface *si = ctx->owner; |
| 6449 | struct stream *strm = si_strm(si); |
| 6450 | struct channel *res = si_ic(si); |
| 6451 | struct act_rule *rule = ctx->rule; |
| 6452 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6453 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6454 | |
| 6455 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6456 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6457 | /* eat the whole request */ |
| 6458 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6459 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6460 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6461 | |
| 6462 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6463 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6464 | return; |
| 6465 | |
| 6466 | /* Execute the function. */ |
| 6467 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6468 | /* finished. */ |
| 6469 | case HLUA_E_OK: |
| 6470 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6471 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6472 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6473 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6474 | res->flags |= CF_READ_NULL; |
| 6475 | si_shutr(si); |
| 6476 | return; |
| 6477 | |
| 6478 | /* yield. */ |
| 6479 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6480 | if (hlua->wake_time != TICK_ETERNITY) |
| 6481 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6482 | return; |
| 6483 | |
| 6484 | /* finished with error. */ |
| 6485 | case HLUA_E_ERRMSG: |
| 6486 | /* Display log. */ |
| 6487 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6488 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6489 | lua_pop(hlua->T, 1); |
| 6490 | goto error; |
| 6491 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6492 | case HLUA_E_ETMOUT: |
| 6493 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6494 | rule->arg.hlua_rule->fcn.name); |
| 6495 | goto error; |
| 6496 | |
| 6497 | case HLUA_E_NOMEM: |
| 6498 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6499 | rule->arg.hlua_rule->fcn.name); |
| 6500 | goto error; |
| 6501 | |
| 6502 | case HLUA_E_YIELD: /* unexpected */ |
| 6503 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6504 | rule->arg.hlua_rule->fcn.name); |
| 6505 | goto error; |
| 6506 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6507 | case HLUA_E_ERR: |
| 6508 | /* Display log. */ |
| 6509 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6510 | rule->arg.hlua_rule->fcn.name); |
| 6511 | goto error; |
| 6512 | |
| 6513 | default: |
| 6514 | goto error; |
| 6515 | } |
| 6516 | |
| 6517 | error: |
| 6518 | |
| 6519 | /* For all other cases, just close the stream. */ |
| 6520 | si_shutw(si); |
| 6521 | si_shutr(si); |
| 6522 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6523 | } |
| 6524 | |
| 6525 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6526 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6527 | task_delete(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6528 | task_free(ctx->ctx.hlua_apptcp.task); |
| 6529 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6530 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6531 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6532 | } |
| 6533 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6534 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6535 | * an errors occurs and -1 if more data are required for initializing |
| 6536 | * the applet. |
| 6537 | */ |
| 6538 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6539 | { |
| 6540 | struct stream_interface *si = ctx->owner; |
| 6541 | struct channel *req = si_oc(si); |
| 6542 | struct http_msg *msg; |
| 6543 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6544 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6545 | char **arg; |
| 6546 | struct hdr_ctx hdr; |
| 6547 | struct task *task; |
| 6548 | 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] | 6549 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6550 | |
| 6551 | /* Wait for a full HTTP request. */ |
| 6552 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 6553 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 6554 | return -1; |
| 6555 | return 0; |
| 6556 | } |
| 6557 | txn = strm->txn; |
| 6558 | msg = &txn->req; |
| 6559 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6560 | /* We want two things in HTTP mode : |
| 6561 | * - enforce server-close mode if we were in keep-alive, so that the |
| 6562 | * applet is released after each response ; |
| 6563 | * - enable request body transfer to the applet in order to resync |
| 6564 | * with the response body. |
| 6565 | */ |
| 6566 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 6567 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6568 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6569 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6570 | if (!hlua) { |
| 6571 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6572 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6573 | return 0; |
| 6574 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6575 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6576 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6577 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6578 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6579 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6580 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6581 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6582 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6583 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6584 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6585 | if (!task) { |
| 6586 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6587 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6588 | return 0; |
| 6589 | } |
| 6590 | task->nice = 0; |
| 6591 | task->context = ctx; |
| 6592 | task->process = hlua_applet_wakeup; |
| 6593 | ctx->ctx.hlua_apphttp.task = task; |
| 6594 | |
| 6595 | /* In the execution wrappers linked with a stream, the |
| 6596 | * Lua context can be not initialized. This behavior |
| 6597 | * permits to save performances because a systematic |
| 6598 | * Lua initialization cause 5% performances loss. |
| 6599 | */ |
| 6600 | if (!hlua_ctx_init(hlua, task)) { |
| 6601 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6602 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6603 | return 0; |
| 6604 | } |
| 6605 | |
| 6606 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6607 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6608 | |
| 6609 | /* The following Lua calls can fail. */ |
| 6610 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6611 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6612 | error = lua_tostring(hlua->T, -1); |
| 6613 | else |
| 6614 | error = "critical error"; |
| 6615 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6616 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6617 | return 0; |
| 6618 | } |
| 6619 | |
| 6620 | /* Check stack available size. */ |
| 6621 | if (!lua_checkstack(hlua->T, 1)) { |
| 6622 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6623 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6624 | RESET_SAFE_LJMP(hlua->T); |
| 6625 | return 0; |
| 6626 | } |
| 6627 | |
| 6628 | /* Restore the function in the stack. */ |
| 6629 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6630 | |
| 6631 | /* Create and and push object stream in the stack. */ |
| 6632 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6633 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6634 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6635 | RESET_SAFE_LJMP(hlua->T); |
| 6636 | return 0; |
| 6637 | } |
| 6638 | hlua->nargs = 1; |
| 6639 | |
| 6640 | /* Look for a 100-continue expected. */ |
| 6641 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 6642 | hdr.idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6643 | 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] | 6644 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 6645 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 6646 | } |
| 6647 | |
| 6648 | /* push keywords in the stack. */ |
| 6649 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6650 | if (!lua_checkstack(hlua->T, 1)) { |
| 6651 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6652 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6653 | RESET_SAFE_LJMP(hlua->T); |
| 6654 | return 0; |
| 6655 | } |
| 6656 | lua_pushstring(hlua->T, *arg); |
| 6657 | hlua->nargs++; |
| 6658 | } |
| 6659 | |
| 6660 | RESET_SAFE_LJMP(hlua->T); |
| 6661 | |
| 6662 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6663 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6664 | |
| 6665 | return 1; |
| 6666 | } |
| 6667 | |
| 6668 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 6669 | { |
| 6670 | struct stream_interface *si = ctx->owner; |
| 6671 | struct stream *strm = si_strm(si); |
| 6672 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6673 | struct act_rule *rule = ctx->rule; |
| 6674 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6675 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 6676 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 6677 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 6678 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 6679 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6680 | int ret; |
| 6681 | |
| 6682 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6683 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6684 | return; |
| 6685 | |
| 6686 | /* Set the currently running flag. */ |
| 6687 | if (!HLUA_IS_RUNNING(hlua) && |
| 6688 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6689 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6690 | /* Wait for full HTTP analysys. */ |
| 6691 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6692 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6693 | return; |
| 6694 | } |
| 6695 | |
| 6696 | /* Store the max amount of bytes that we can read. */ |
| 6697 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 6698 | |
| 6699 | /* We need to flush the request header. This left the body |
| 6700 | * for the Lua. |
| 6701 | */ |
| 6702 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6703 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6704 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6705 | if (ret == -1) |
| 6706 | return; |
| 6707 | |
| 6708 | /* No data available, ask for more data. */ |
| 6709 | if (ret == 1) |
| 6710 | len2 = 0; |
| 6711 | if (ret == 0) |
| 6712 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 6713 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6714 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6715 | return; |
| 6716 | } |
| 6717 | |
| 6718 | /* skip the requests bytes. */ |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 6719 | 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] | 6720 | } |
| 6721 | |
| 6722 | /* Executes The applet if it is not done. */ |
| 6723 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6724 | |
| 6725 | /* Execute the function. */ |
| 6726 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6727 | /* finished. */ |
| 6728 | case HLUA_E_OK: |
| 6729 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6730 | break; |
| 6731 | |
| 6732 | /* yield. */ |
| 6733 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6734 | if (hlua->wake_time != TICK_ETERNITY) |
| 6735 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6736 | return; |
| 6737 | |
| 6738 | /* finished with error. */ |
| 6739 | case HLUA_E_ERRMSG: |
| 6740 | /* Display log. */ |
| 6741 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6742 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6743 | lua_pop(hlua->T, 1); |
| 6744 | goto error; |
| 6745 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6746 | case HLUA_E_ETMOUT: |
| 6747 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 6748 | rule->arg.hlua_rule->fcn.name); |
| 6749 | goto error; |
| 6750 | |
| 6751 | case HLUA_E_NOMEM: |
| 6752 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 6753 | rule->arg.hlua_rule->fcn.name); |
| 6754 | goto error; |
| 6755 | |
| 6756 | case HLUA_E_YIELD: /* unexpected */ |
| 6757 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 6758 | rule->arg.hlua_rule->fcn.name); |
| 6759 | goto error; |
| 6760 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6761 | case HLUA_E_ERR: |
| 6762 | /* Display log. */ |
| 6763 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6764 | rule->arg.hlua_rule->fcn.name); |
| 6765 | goto error; |
| 6766 | |
| 6767 | default: |
| 6768 | goto error; |
| 6769 | } |
| 6770 | } |
| 6771 | |
| 6772 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 6773 | |
| 6774 | /* We must send the final chunk. */ |
| 6775 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 6776 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 6777 | |
| 6778 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6779 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6780 | |
| 6781 | /* critical error. */ |
| 6782 | if (ret == -2 || ret == -3) { |
| 6783 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 6784 | rule->arg.hlua_rule->fcn.name); |
| 6785 | goto error; |
| 6786 | } |
| 6787 | |
| 6788 | /* no enough space error. */ |
| 6789 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6790 | si_rx_room_blk(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6791 | return; |
| 6792 | } |
| 6793 | |
| 6794 | /* set the last chunk sent. */ |
| 6795 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6796 | } |
| 6797 | |
| 6798 | /* close the connection. */ |
| 6799 | |
Frédéric Lécaille | 83ed5d5 | 2018-07-18 14:25:26 +0200 | [diff] [blame] | 6800 | /* status */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6801 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6802 | |
| 6803 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6804 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6805 | res->flags |= CF_READ_NULL; |
| 6806 | si_shutr(si); |
| 6807 | |
| 6808 | return; |
| 6809 | } |
| 6810 | |
| 6811 | error: |
| 6812 | |
| 6813 | /* If we are in HTTP mode, and we are not send any |
| 6814 | * data, return a 500 server error in best effort: |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6815 | * if there is no room available in the buffer, |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6816 | * just close the connection. |
| 6817 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6818 | ci_putblk(res, error_500, strlen(error_500)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6819 | if (!(strm->flags & SF_ERR_MASK)) |
| 6820 | strm->flags |= SF_ERR_RESOURCE; |
| 6821 | si_shutw(si); |
| 6822 | si_shutr(si); |
| 6823 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6824 | } |
| 6825 | |
| 6826 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6827 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6828 | task_delete(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6829 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6830 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6831 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6832 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6833 | } |
| 6834 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6835 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6836 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6837 | * |
| 6838 | * This function can fail with an abort() due to an Lua critical error. |
| 6839 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6840 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6841 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6842 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6843 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6844 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6845 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6846 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6847 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6848 | if (px->options2 & PR_O2_USE_HTX) { |
| 6849 | memprintf(err, "Lua actions cannot be used when the HTX internal representation is enabled"); |
| 6850 | return ACT_RET_PRS_ERR; |
| 6851 | } |
| 6852 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6853 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6854 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6855 | if (!rule->arg.hlua_rule) { |
| 6856 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6857 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6858 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6859 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6860 | /* Memory for arguments. */ |
| 6861 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6862 | if (!rule->arg.hlua_rule->args) { |
| 6863 | memprintf(err, "out of memory error"); |
| 6864 | return ACT_RET_PRS_ERR; |
| 6865 | } |
| 6866 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6867 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6868 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6869 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6870 | /* Expect some arguments */ |
| 6871 | for (i = 0; i < fcn->nargs; i++) { |
| 6872 | if (*args[i+1] == '\0') { |
| 6873 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6874 | return ACT_RET_PRS_ERR; |
| 6875 | } |
| 6876 | rule->arg.hlua_rule->args[i] = strdup(args[i + 1]); |
| 6877 | if (!rule->arg.hlua_rule->args[i]) { |
| 6878 | memprintf(err, "out of memory error"); |
| 6879 | return ACT_RET_PRS_ERR; |
| 6880 | } |
| 6881 | (*cur_arg)++; |
| 6882 | } |
| 6883 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6884 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6885 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6886 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6887 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6888 | } |
| 6889 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6890 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6891 | struct act_rule *rule, char **err) |
| 6892 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6893 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6894 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6895 | if (px->options2 & PR_O2_USE_HTX) { |
| 6896 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 6897 | return ACT_RET_PRS_ERR; |
| 6898 | } |
| 6899 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6900 | /* HTTP applets are forbidden in tcp-request rules. |
| 6901 | * HTTP applet request requires everything initilized by |
| 6902 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6903 | * The applet will be immediately initilized, but its before |
| 6904 | * the call of this analyzer. |
| 6905 | */ |
| 6906 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6907 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6908 | return ACT_RET_PRS_ERR; |
| 6909 | } |
| 6910 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6911 | /* Memory for the rule. */ |
| 6912 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6913 | if (!rule->arg.hlua_rule) { |
| 6914 | memprintf(err, "out of memory error"); |
| 6915 | return ACT_RET_PRS_ERR; |
| 6916 | } |
| 6917 | |
| 6918 | /* Reference the Lua function and store the reference. */ |
| 6919 | rule->arg.hlua_rule->fcn = *fcn; |
| 6920 | |
| 6921 | /* TODO: later accept arguments. */ |
| 6922 | rule->arg.hlua_rule->args = NULL; |
| 6923 | |
| 6924 | /* Add applet pointer in the rule. */ |
| 6925 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6926 | rule->applet.name = fcn->name; |
| 6927 | rule->applet.init = hlua_applet_http_init; |
| 6928 | rule->applet.fct = hlua_applet_http_fct; |
| 6929 | rule->applet.release = hlua_applet_http_release; |
| 6930 | rule->applet.timeout = hlua_timeout_applet; |
| 6931 | |
| 6932 | return ACT_RET_PRS_OK; |
| 6933 | } |
| 6934 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6935 | /* This function is an LUA binding used for registering |
| 6936 | * "sample-conv" functions. It expects a converter name used |
| 6937 | * in the haproxy configuration file, and an LUA function. |
| 6938 | */ |
| 6939 | __LJMP static int hlua_register_action(lua_State *L) |
| 6940 | { |
| 6941 | struct action_kw_list *akl; |
| 6942 | const char *name; |
| 6943 | int ref; |
| 6944 | int len; |
| 6945 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6946 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6947 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6948 | /* Initialise the number of expected arguments at 0. */ |
| 6949 | nargs = 0; |
| 6950 | |
| 6951 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6952 | 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] | 6953 | |
| 6954 | /* First argument : converter name. */ |
| 6955 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6956 | |
| 6957 | /* Second argument : environment. */ |
| 6958 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6959 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6960 | |
| 6961 | /* Third argument : lua function. */ |
| 6962 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6963 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6964 | /* 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] | 6965 | if (lua_gettop(L) >= 4) |
| 6966 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6967 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6968 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6969 | lua_pushnil(L); |
| 6970 | while (lua_next(L, 2) != 0) { |
| 6971 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6972 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6973 | |
| 6974 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6975 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6976 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6977 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6978 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6979 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6980 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6981 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6982 | |
| 6983 | /* Fill fcn. */ |
| 6984 | fcn->name = strdup(name); |
| 6985 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6986 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6987 | fcn->function_ref = ref; |
| 6988 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6989 | /* Set the expected number od arguments. */ |
| 6990 | fcn->nargs = nargs; |
| 6991 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6992 | /* List head */ |
| 6993 | akl->list.n = akl->list.p = NULL; |
| 6994 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6995 | /* action keyword. */ |
| 6996 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6997 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6998 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6999 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7000 | |
| 7001 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7002 | |
| 7003 | akl->kw[0].match_pfx = 0; |
| 7004 | akl->kw[0].private = fcn; |
| 7005 | akl->kw[0].parse = action_register_lua; |
| 7006 | |
| 7007 | /* select the action registering point. */ |
| 7008 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7009 | tcp_req_cont_keywords_register(akl); |
| 7010 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7011 | tcp_res_cont_keywords_register(akl); |
| 7012 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7013 | http_req_keywords_register(akl); |
| 7014 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7015 | http_res_keywords_register(akl); |
| 7016 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7017 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7018 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7019 | "are expected.", lua_tostring(L, -1))); |
| 7020 | |
| 7021 | /* pop the environment string. */ |
| 7022 | lua_pop(L, 1); |
| 7023 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7024 | return ACT_RET_PRS_OK; |
| 7025 | } |
| 7026 | |
| 7027 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7028 | struct act_rule *rule, char **err) |
| 7029 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7030 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7031 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7032 | if (px->options2 & PR_O2_USE_HTX) { |
| 7033 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 7034 | return ACT_RET_PRS_ERR; |
| 7035 | } |
| 7036 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7037 | /* Memory for the rule. */ |
| 7038 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7039 | if (!rule->arg.hlua_rule) { |
| 7040 | memprintf(err, "out of memory error"); |
| 7041 | return ACT_RET_PRS_ERR; |
| 7042 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7043 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7044 | /* Reference the Lua function and store the reference. */ |
| 7045 | rule->arg.hlua_rule->fcn = *fcn; |
| 7046 | |
| 7047 | /* TODO: later accept arguments. */ |
| 7048 | rule->arg.hlua_rule->args = NULL; |
| 7049 | |
| 7050 | /* Add applet pointer in the rule. */ |
| 7051 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7052 | rule->applet.name = fcn->name; |
| 7053 | rule->applet.init = hlua_applet_tcp_init; |
| 7054 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7055 | rule->applet.release = hlua_applet_tcp_release; |
| 7056 | rule->applet.timeout = hlua_timeout_applet; |
| 7057 | |
| 7058 | return 0; |
| 7059 | } |
| 7060 | |
| 7061 | /* This function is an LUA binding used for registering |
| 7062 | * "sample-conv" functions. It expects a converter name used |
| 7063 | * in the haproxy configuration file, and an LUA function. |
| 7064 | */ |
| 7065 | __LJMP static int hlua_register_service(lua_State *L) |
| 7066 | { |
| 7067 | struct action_kw_list *akl; |
| 7068 | const char *name; |
| 7069 | const char *env; |
| 7070 | int ref; |
| 7071 | int len; |
| 7072 | struct hlua_function *fcn; |
| 7073 | |
| 7074 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7075 | |
| 7076 | /* First argument : converter name. */ |
| 7077 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7078 | |
| 7079 | /* Second argument : environment. */ |
| 7080 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7081 | |
| 7082 | /* Third argument : lua function. */ |
| 7083 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7084 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7085 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7086 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7087 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7088 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7089 | fcn = calloc(1, sizeof(*fcn)); |
| 7090 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7091 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7092 | |
| 7093 | /* Fill fcn. */ |
| 7094 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7095 | fcn->name = calloc(1, len); |
| 7096 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7097 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7098 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7099 | fcn->function_ref = ref; |
| 7100 | |
| 7101 | /* List head */ |
| 7102 | akl->list.n = akl->list.p = NULL; |
| 7103 | |
| 7104 | /* converter keyword. */ |
| 7105 | len = strlen("lua.") + strlen(name) + 1; |
| 7106 | akl->kw[0].kw = calloc(1, len); |
| 7107 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7108 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7109 | |
| 7110 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7111 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7112 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7113 | if (strcmp(env, "tcp") == 0) |
| 7114 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7115 | else if (strcmp(env, "http") == 0) |
| 7116 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7117 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7118 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7119 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7120 | |
| 7121 | akl->kw[0].match_pfx = 0; |
| 7122 | akl->kw[0].private = fcn; |
| 7123 | |
| 7124 | /* End of array. */ |
| 7125 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7126 | |
| 7127 | /* Register this new converter */ |
| 7128 | service_keywords_register(akl); |
| 7129 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7130 | return 0; |
| 7131 | } |
| 7132 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7133 | /* This function initialises Lua cli handler. It copies the |
| 7134 | * arguments in the Lua stack and create channel IO objects. |
| 7135 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7136 | 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] | 7137 | { |
| 7138 | struct hlua *hlua; |
| 7139 | struct hlua_function *fcn; |
| 7140 | int i; |
| 7141 | const char *error; |
| 7142 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7143 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7144 | appctx->ctx.hlua_cli.fcn = private; |
| 7145 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7146 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7147 | if (!hlua) { |
| 7148 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7149 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7150 | } |
| 7151 | HLUA_INIT(hlua); |
| 7152 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7153 | |
| 7154 | /* Create task used by signal to wakeup applets. |
| 7155 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7156 | * applet_http. It is absolutely compatible. |
| 7157 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7158 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7159 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7160 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7161 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7162 | } |
| 7163 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7164 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7165 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7166 | |
| 7167 | /* Initialises the Lua context */ |
| 7168 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 7169 | 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] | 7170 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7171 | } |
| 7172 | |
| 7173 | /* The following Lua calls can fail. */ |
| 7174 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7175 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7176 | error = lua_tostring(hlua->T, -1); |
| 7177 | else |
| 7178 | error = "critical error"; |
| 7179 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7180 | goto error; |
| 7181 | } |
| 7182 | |
| 7183 | /* Check stack available size. */ |
| 7184 | if (!lua_checkstack(hlua->T, 2)) { |
| 7185 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7186 | goto error; |
| 7187 | } |
| 7188 | |
| 7189 | /* Restore the function in the stack. */ |
| 7190 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7191 | |
| 7192 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7193 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7194 | */ |
| 7195 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7196 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7197 | goto error; |
| 7198 | } |
| 7199 | hlua->nargs = 1; |
| 7200 | |
| 7201 | /* push keywords in the stack. */ |
| 7202 | for (i = 0; *args[i]; i++) { |
| 7203 | /* Check stack available size. */ |
| 7204 | if (!lua_checkstack(hlua->T, 1)) { |
| 7205 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7206 | goto error; |
| 7207 | } |
| 7208 | lua_pushstring(hlua->T, args[i]); |
| 7209 | hlua->nargs++; |
| 7210 | } |
| 7211 | |
| 7212 | /* We must initialize the execution timeouts. */ |
| 7213 | hlua->max_time = hlua_timeout_session; |
| 7214 | |
| 7215 | /* At this point the execution is safe. */ |
| 7216 | RESET_SAFE_LJMP(hlua->T); |
| 7217 | |
| 7218 | /* It's ok */ |
| 7219 | return 0; |
| 7220 | |
| 7221 | /* It's not ok. */ |
| 7222 | error: |
| 7223 | RESET_SAFE_LJMP(hlua->T); |
| 7224 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7225 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7226 | return 1; |
| 7227 | } |
| 7228 | |
| 7229 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7230 | { |
| 7231 | struct hlua *hlua; |
| 7232 | struct stream_interface *si; |
| 7233 | struct hlua_function *fcn; |
| 7234 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7235 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7236 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7237 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7238 | |
| 7239 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7240 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7241 | return 1; |
| 7242 | |
| 7243 | /* Execute the function. */ |
| 7244 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7245 | |
| 7246 | /* finished. */ |
| 7247 | case HLUA_E_OK: |
| 7248 | return 1; |
| 7249 | |
| 7250 | /* yield. */ |
| 7251 | case HLUA_E_AGAIN: |
| 7252 | /* We want write. */ |
| 7253 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7254 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7255 | /* Set the timeout. */ |
| 7256 | if (hlua->wake_time != TICK_ETERNITY) |
| 7257 | task_schedule(hlua->task, hlua->wake_time); |
| 7258 | return 0; |
| 7259 | |
| 7260 | /* finished with error. */ |
| 7261 | case HLUA_E_ERRMSG: |
| 7262 | /* Display log. */ |
| 7263 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7264 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7265 | lua_pop(hlua->T, 1); |
| 7266 | return 1; |
| 7267 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7268 | case HLUA_E_ETMOUT: |
| 7269 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7270 | fcn->name); |
| 7271 | return 1; |
| 7272 | |
| 7273 | case HLUA_E_NOMEM: |
| 7274 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7275 | fcn->name); |
| 7276 | return 1; |
| 7277 | |
| 7278 | case HLUA_E_YIELD: /* unexpected */ |
| 7279 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7280 | fcn->name); |
| 7281 | return 1; |
| 7282 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7283 | case HLUA_E_ERR: |
| 7284 | /* Display log. */ |
| 7285 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7286 | fcn->name); |
| 7287 | return 1; |
| 7288 | |
| 7289 | default: |
| 7290 | return 1; |
| 7291 | } |
| 7292 | |
| 7293 | return 1; |
| 7294 | } |
| 7295 | |
| 7296 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7297 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7298 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7299 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7300 | } |
| 7301 | |
| 7302 | /* This function is an LUA binding used for registering |
| 7303 | * new keywords in the cli. It expects a list of keywords |
| 7304 | * which are the "path". It is limited to 5 keywords. A |
| 7305 | * description of the command, a function to be executed |
| 7306 | * for the parsing and a function for io handlers. |
| 7307 | */ |
| 7308 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7309 | { |
| 7310 | struct cli_kw_list *cli_kws; |
| 7311 | const char *message; |
| 7312 | int ref_io; |
| 7313 | int len; |
| 7314 | struct hlua_function *fcn; |
| 7315 | int index; |
| 7316 | int i; |
| 7317 | |
| 7318 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7319 | |
| 7320 | /* First argument : an array of maximum 5 keywords. */ |
| 7321 | if (!lua_istable(L, 1)) |
| 7322 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7323 | |
| 7324 | /* Second argument : string with contextual message. */ |
| 7325 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7326 | |
| 7327 | /* Third and fourth argument : lua function. */ |
| 7328 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7329 | |
| 7330 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7331 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7332 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7333 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7334 | fcn = calloc(1, sizeof(*fcn)); |
| 7335 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7336 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7337 | |
| 7338 | /* Fill path. */ |
| 7339 | index = 0; |
| 7340 | lua_pushnil(L); |
| 7341 | while(lua_next(L, 1) != 0) { |
| 7342 | if (index >= 5) |
| 7343 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7344 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7345 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7346 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7347 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7348 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7349 | index++; |
| 7350 | lua_pop(L, 1); |
| 7351 | } |
| 7352 | |
| 7353 | /* Copy help message. */ |
| 7354 | cli_kws->kw[0].usage = strdup(message); |
| 7355 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7356 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7357 | |
| 7358 | /* Fill fcn io handler. */ |
| 7359 | len = strlen("<lua.cli>") + 1; |
| 7360 | for (i = 0; i < index; i++) |
| 7361 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7362 | fcn->name = calloc(1, len); |
| 7363 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7364 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7365 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7366 | for (i = 0; i < index; i++) { |
| 7367 | strncat((char *)fcn->name, ".", len); |
| 7368 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7369 | } |
| 7370 | strncat((char *)fcn->name, ">", len); |
| 7371 | fcn->function_ref = ref_io; |
| 7372 | |
| 7373 | /* Fill last entries. */ |
| 7374 | cli_kws->kw[0].private = fcn; |
| 7375 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7376 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7377 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7378 | |
| 7379 | /* Register this new converter */ |
| 7380 | cli_register_kw(cli_kws); |
| 7381 | |
| 7382 | return 0; |
| 7383 | } |
| 7384 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7385 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7386 | struct proxy *defpx, const char *file, int line, |
| 7387 | char **err, unsigned int *timeout) |
| 7388 | { |
| 7389 | const char *error; |
| 7390 | |
| 7391 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 7392 | if (error && *error != '\0') { |
| 7393 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7394 | return -1; |
| 7395 | } |
| 7396 | return 0; |
| 7397 | } |
| 7398 | |
| 7399 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7400 | struct proxy *defpx, const char *file, int line, |
| 7401 | char **err) |
| 7402 | { |
| 7403 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7404 | file, line, err, &hlua_timeout_session); |
| 7405 | } |
| 7406 | |
| 7407 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7408 | struct proxy *defpx, const char *file, int line, |
| 7409 | char **err) |
| 7410 | { |
| 7411 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7412 | file, line, err, &hlua_timeout_task); |
| 7413 | } |
| 7414 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7415 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7416 | struct proxy *defpx, const char *file, int line, |
| 7417 | char **err) |
| 7418 | { |
| 7419 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7420 | file, line, err, &hlua_timeout_applet); |
| 7421 | } |
| 7422 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7423 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7424 | struct proxy *defpx, const char *file, int line, |
| 7425 | char **err) |
| 7426 | { |
| 7427 | char *error; |
| 7428 | |
| 7429 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7430 | if (*error != '\0') { |
| 7431 | memprintf(err, "%s: invalid number", args[0]); |
| 7432 | return -1; |
| 7433 | } |
| 7434 | return 0; |
| 7435 | } |
| 7436 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7437 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7438 | struct proxy *defpx, const char *file, int line, |
| 7439 | char **err) |
| 7440 | { |
| 7441 | char *error; |
| 7442 | |
| 7443 | if (*(args[1]) == 0) { |
| 7444 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7445 | return -1; |
| 7446 | } |
| 7447 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7448 | if (*error != '\0') { |
| 7449 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7450 | return -1; |
| 7451 | } |
| 7452 | return 0; |
| 7453 | } |
| 7454 | |
| 7455 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7456 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7457 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7458 | * the main lua entry point. |
| 7459 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7460 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7461 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7462 | * |
| 7463 | * In some error case, LUA set an error message in top of the stack. This function |
| 7464 | * 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] | 7465 | * |
| 7466 | * This function can fail with an abort() due to an Lua critical error. |
| 7467 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7468 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7469 | */ |
| 7470 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7471 | struct proxy *defpx, const char *file, int line, |
| 7472 | char **err) |
| 7473 | { |
| 7474 | int error; |
| 7475 | |
| 7476 | /* Just load and compile the file. */ |
| 7477 | error = luaL_loadfile(gL.T, args[1]); |
| 7478 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7479 | 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] | 7480 | lua_pop(gL.T, 1); |
| 7481 | return -1; |
| 7482 | } |
| 7483 | |
| 7484 | /* If no syntax error where detected, execute the code. */ |
| 7485 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7486 | switch (error) { |
| 7487 | case LUA_OK: |
| 7488 | break; |
| 7489 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7490 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7491 | lua_pop(gL.T, 1); |
| 7492 | return -1; |
| 7493 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7494 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7495 | return -1; |
| 7496 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7497 | 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] | 7498 | lua_pop(gL.T, 1); |
| 7499 | return -1; |
| 7500 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7501 | 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] | 7502 | lua_pop(gL.T, 1); |
| 7503 | return -1; |
| 7504 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7505 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7506 | lua_pop(gL.T, 1); |
| 7507 | return -1; |
| 7508 | } |
| 7509 | |
| 7510 | return 0; |
| 7511 | } |
| 7512 | |
| 7513 | /* configuration keywords declaration */ |
| 7514 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7515 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7516 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7517 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7518 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7519 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7520 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7521 | { 0, NULL, NULL }, |
| 7522 | }}; |
| 7523 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7524 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 7525 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7526 | static int hlua_check_config() |
| 7527 | { |
| 7528 | struct proxy *px; |
| 7529 | struct acl *acl; |
| 7530 | struct acl_expr *expr; |
| 7531 | struct sample_fetch *fetch; |
| 7532 | int err = 0; |
| 7533 | |
| 7534 | for (px = proxies_list; px; px = px->next) { |
| 7535 | if (!(px->options2 & PR_O2_USE_HTX)) |
| 7536 | continue; |
| 7537 | |
| 7538 | list_for_each_entry(acl, &px->acl, list) { |
| 7539 | list_for_each_entry(expr, &acl->expr, list) { |
| 7540 | fetch = expr->smp->fetch; |
| 7541 | if (fetch->process != hlua_sample_fetch_wrapper) |
| 7542 | continue; |
| 7543 | |
| 7544 | ha_alert("config: %s '%s': sample-fetch '%s' cannot be used used " |
| 7545 | "when the HTX internal representation is enabled.\n", |
| 7546 | proxy_type_str(px), px->id, fetch->kw); |
| 7547 | err++; |
| 7548 | } |
| 7549 | } |
| 7550 | } |
| 7551 | return err; |
| 7552 | } |
| 7553 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7554 | /* This function can fail with an abort() due to an Lua critical error. |
| 7555 | * We are in the initialisation process of HAProxy, this abort() is |
| 7556 | * tolerated. |
| 7557 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7558 | int hlua_post_init() |
| 7559 | { |
| 7560 | struct hlua_init_function *init; |
| 7561 | const char *msg; |
| 7562 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7563 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7564 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7565 | /* Call post initialisation function in safe environement. */ |
| 7566 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7567 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7568 | error = lua_tostring(gL.T, -1); |
| 7569 | else |
| 7570 | error = "critical error"; |
| 7571 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7572 | exit(1); |
| 7573 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7574 | |
| 7575 | #if USE_OPENSSL |
| 7576 | /* Initialize SSL server. */ |
| 7577 | if (socket_ssl.xprt->prepare_srv) { |
| 7578 | int saved_used_backed = global.ssl_used_backend; |
| 7579 | // don't affect maxconn automatic computation |
| 7580 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7581 | global.ssl_used_backend = saved_used_backed; |
| 7582 | } |
| 7583 | #endif |
| 7584 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7585 | hlua_fcn_post_init(gL.T); |
| 7586 | RESET_SAFE_LJMP(gL.T); |
| 7587 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7588 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7589 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7590 | ret = hlua_ctx_resume(&gL, 0); |
| 7591 | switch (ret) { |
| 7592 | case HLUA_E_OK: |
| 7593 | lua_pop(gL.T, -1); |
| 7594 | return 1; |
| 7595 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7596 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7597 | return 0; |
| 7598 | case HLUA_E_ERRMSG: |
| 7599 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7600 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7601 | return 0; |
| 7602 | case HLUA_E_ERR: |
| 7603 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7604 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7605 | return 0; |
| 7606 | } |
| 7607 | } |
| 7608 | return 1; |
| 7609 | } |
| 7610 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7611 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7612 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7613 | * is the previously allocated size or the kind of object in case of a new |
| 7614 | * allocation. <nsize> is the requested new size. |
| 7615 | */ |
| 7616 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7617 | { |
| 7618 | struct hlua_mem_allocator *zone = ud; |
| 7619 | |
| 7620 | if (nsize == 0) { |
| 7621 | /* it's a free */ |
| 7622 | if (ptr) |
| 7623 | zone->allocated -= osize; |
| 7624 | free(ptr); |
| 7625 | return NULL; |
| 7626 | } |
| 7627 | |
| 7628 | if (!ptr) { |
| 7629 | /* it's a new allocation */ |
| 7630 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7631 | return NULL; |
| 7632 | |
| 7633 | ptr = malloc(nsize); |
| 7634 | if (ptr) |
| 7635 | zone->allocated += nsize; |
| 7636 | return ptr; |
| 7637 | } |
| 7638 | |
| 7639 | /* it's a realloc */ |
| 7640 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7641 | return NULL; |
| 7642 | |
| 7643 | ptr = realloc(ptr, nsize); |
| 7644 | if (ptr) |
| 7645 | zone->allocated += nsize - osize; |
| 7646 | return ptr; |
| 7647 | } |
| 7648 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7649 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7650 | * We are in the initialisation process of HAProxy, this abort() is |
| 7651 | * tolerated. |
| 7652 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7653 | void hlua_init(void) |
| 7654 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7655 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7656 | int idx; |
| 7657 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7658 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7659 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7660 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7661 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7662 | struct srv_kw *kw; |
| 7663 | int tmp_error; |
| 7664 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7665 | char *args[] = { /* SSL client configuration. */ |
| 7666 | "ssl", |
| 7667 | "verify", |
| 7668 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7669 | NULL |
| 7670 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7671 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7672 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7673 | /* Init main lua stack. */ |
| 7674 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7675 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7676 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7677 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7678 | hlua_sethlua(&gL); |
| 7679 | gL.Tref = LUA_REFNIL; |
| 7680 | gL.task = NULL; |
| 7681 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7682 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7683 | * the Lua function can fail with an abort. We are in the initialisation |
| 7684 | * process of HAProxy, this abort() is tolerated. |
| 7685 | */ |
| 7686 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7687 | /* Initialise lua. */ |
| 7688 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7689 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7690 | /* Set safe environment for the initialisation. */ |
| 7691 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7692 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7693 | error_msg = lua_tostring(gL.T, -1); |
| 7694 | else |
| 7695 | error_msg = "critical error"; |
| 7696 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7697 | exit(1); |
| 7698 | } |
| 7699 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7700 | /* |
| 7701 | * |
| 7702 | * Create "core" object. |
| 7703 | * |
| 7704 | */ |
| 7705 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7706 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7707 | lua_newtable(gL.T); |
| 7708 | |
| 7709 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7710 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7711 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7712 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7713 | /* Register special functions. */ |
| 7714 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7715 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7716 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7717 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7718 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7719 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7720 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7721 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7722 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7723 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7724 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7725 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7726 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7727 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7728 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7729 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7730 | hlua_class_function(gL.T, "log", hlua_log); |
| 7731 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7732 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7733 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7734 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7735 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7736 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7737 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7738 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7739 | |
| 7740 | /* |
| 7741 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7742 | * Register class Map |
| 7743 | * |
| 7744 | */ |
| 7745 | |
| 7746 | /* This table entry is the object "Map" base. */ |
| 7747 | lua_newtable(gL.T); |
| 7748 | |
| 7749 | /* register pattern types. */ |
| 7750 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7751 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7752 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7753 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 7754 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7755 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7756 | |
| 7757 | /* register constructor. */ |
| 7758 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7759 | |
| 7760 | /* Create and fill the metatable. */ |
| 7761 | lua_newtable(gL.T); |
| 7762 | |
| 7763 | /* Create and fille the __index entry. */ |
| 7764 | lua_pushstring(gL.T, "__index"); |
| 7765 | lua_newtable(gL.T); |
| 7766 | |
| 7767 | /* Register . */ |
| 7768 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7769 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7770 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7771 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7772 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7773 | /* Register previous table in the registry with reference and named entry. |
| 7774 | * The function hlua_register_metatable() pops the stack, so we |
| 7775 | * previously create a copy of the table. |
| 7776 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7777 | 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] | 7778 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7779 | |
| 7780 | /* Assign the metatable to the mai Map object. */ |
| 7781 | lua_setmetatable(gL.T, -2); |
| 7782 | |
| 7783 | /* Set a name to the table. */ |
| 7784 | lua_setglobal(gL.T, "Map"); |
| 7785 | |
| 7786 | /* |
| 7787 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7788 | * Register class Channel |
| 7789 | * |
| 7790 | */ |
| 7791 | |
| 7792 | /* Create and fill the metatable. */ |
| 7793 | lua_newtable(gL.T); |
| 7794 | |
| 7795 | /* Create and fille the __index entry. */ |
| 7796 | lua_pushstring(gL.T, "__index"); |
| 7797 | lua_newtable(gL.T); |
| 7798 | |
| 7799 | /* Register . */ |
| 7800 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7801 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7802 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7803 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7804 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7805 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7806 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7807 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7808 | 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] | 7809 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7810 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7811 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7812 | |
| 7813 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7814 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7815 | |
| 7816 | /* |
| 7817 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7818 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7819 | * |
| 7820 | */ |
| 7821 | |
| 7822 | /* Create and fill the metatable. */ |
| 7823 | lua_newtable(gL.T); |
| 7824 | |
| 7825 | /* Create and fille the __index entry. */ |
| 7826 | lua_pushstring(gL.T, "__index"); |
| 7827 | lua_newtable(gL.T); |
| 7828 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7829 | /* Browse existing fetches and create the associated |
| 7830 | * object method. |
| 7831 | */ |
| 7832 | sf = NULL; |
| 7833 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7834 | |
| 7835 | /* Dont register the keywork if the arguments check function are |
| 7836 | * not safe during the runtime. |
| 7837 | */ |
| 7838 | if ((sf->val_args != NULL) && |
| 7839 | (sf->val_args != val_payload_lv) && |
| 7840 | (sf->val_args != val_hdr)) |
| 7841 | continue; |
| 7842 | |
| 7843 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7844 | * by an underscore. |
| 7845 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7846 | strncpy(trash.area, sf->kw, trash.size); |
| 7847 | trash.area[trash.size - 1] = '\0'; |
| 7848 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7849 | if (*p == '.' || *p == '-' || *p == '+') |
| 7850 | *p = '_'; |
| 7851 | |
| 7852 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7853 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7854 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7855 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7856 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7857 | } |
| 7858 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7859 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7860 | |
| 7861 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7862 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7863 | |
| 7864 | /* |
| 7865 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7866 | * Register class Converters |
| 7867 | * |
| 7868 | */ |
| 7869 | |
| 7870 | /* Create and fill the metatable. */ |
| 7871 | lua_newtable(gL.T); |
| 7872 | |
| 7873 | /* Create and fill the __index entry. */ |
| 7874 | lua_pushstring(gL.T, "__index"); |
| 7875 | lua_newtable(gL.T); |
| 7876 | |
| 7877 | /* Browse existing converters and create the associated |
| 7878 | * object method. |
| 7879 | */ |
| 7880 | sc = NULL; |
| 7881 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7882 | /* Dont register the keywork if the arguments check function are |
| 7883 | * not safe during the runtime. |
| 7884 | */ |
| 7885 | if (sc->val_args != NULL) |
| 7886 | continue; |
| 7887 | |
| 7888 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7889 | * by an underscore. |
| 7890 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7891 | strncpy(trash.area, sc->kw, trash.size); |
| 7892 | trash.area[trash.size - 1] = '\0'; |
| 7893 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7894 | if (*p == '.' || *p == '-' || *p == '+') |
| 7895 | *p = '_'; |
| 7896 | |
| 7897 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7898 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7899 | lua_pushlightuserdata(gL.T, sc); |
| 7900 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7901 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7902 | } |
| 7903 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7904 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7905 | |
| 7906 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7907 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7908 | |
| 7909 | /* |
| 7910 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7911 | * Register class HTTP |
| 7912 | * |
| 7913 | */ |
| 7914 | |
| 7915 | /* Create and fill the metatable. */ |
| 7916 | lua_newtable(gL.T); |
| 7917 | |
| 7918 | /* Create and fille the __index entry. */ |
| 7919 | lua_pushstring(gL.T, "__index"); |
| 7920 | lua_newtable(gL.T); |
| 7921 | |
| 7922 | /* Register Lua functions. */ |
| 7923 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7924 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7925 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7926 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7927 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7928 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7929 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7930 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7931 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7932 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7933 | |
| 7934 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7935 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7936 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7937 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7938 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7939 | 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] | 7940 | 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] | 7941 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7942 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7943 | |
| 7944 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7945 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7946 | |
| 7947 | /* |
| 7948 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7949 | * Register class AppletTCP |
| 7950 | * |
| 7951 | */ |
| 7952 | |
| 7953 | /* Create and fill the metatable. */ |
| 7954 | lua_newtable(gL.T); |
| 7955 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7956 | /* Create and fille the __index entry. */ |
| 7957 | lua_pushstring(gL.T, "__index"); |
| 7958 | lua_newtable(gL.T); |
| 7959 | |
| 7960 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7961 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7962 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7963 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7964 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7965 | 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] | 7966 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7967 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7968 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7969 | |
| 7970 | lua_settable(gL.T, -3); |
| 7971 | |
| 7972 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7973 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7974 | |
| 7975 | /* |
| 7976 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7977 | * Register class AppletHTTP |
| 7978 | * |
| 7979 | */ |
| 7980 | |
| 7981 | /* Create and fill the metatable. */ |
| 7982 | lua_newtable(gL.T); |
| 7983 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7984 | /* Create and fille the __index entry. */ |
| 7985 | lua_pushstring(gL.T, "__index"); |
| 7986 | lua_newtable(gL.T); |
| 7987 | |
| 7988 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7989 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7990 | 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] | 7991 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7992 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7993 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7994 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7995 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7996 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7997 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7998 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7999 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8000 | |
| 8001 | lua_settable(gL.T, -3); |
| 8002 | |
| 8003 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8004 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8005 | |
| 8006 | /* |
| 8007 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8008 | * Register class TXN |
| 8009 | * |
| 8010 | */ |
| 8011 | |
| 8012 | /* Create and fill the metatable. */ |
| 8013 | lua_newtable(gL.T); |
| 8014 | |
| 8015 | /* Create and fille the __index entry. */ |
| 8016 | lua_pushstring(gL.T, "__index"); |
| 8017 | lua_newtable(gL.T); |
| 8018 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8019 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8020 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8021 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8022 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8023 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8024 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8025 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 8026 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8027 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8028 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8029 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8030 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8031 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8032 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8033 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8034 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8035 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8036 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8037 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8038 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8039 | |
| 8040 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8041 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8042 | |
| 8043 | /* |
| 8044 | * |
| 8045 | * Register class Socket |
| 8046 | * |
| 8047 | */ |
| 8048 | |
| 8049 | /* Create and fill the metatable. */ |
| 8050 | lua_newtable(gL.T); |
| 8051 | |
| 8052 | /* Create and fille the __index entry. */ |
| 8053 | lua_pushstring(gL.T, "__index"); |
| 8054 | lua_newtable(gL.T); |
| 8055 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8056 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8057 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8058 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8059 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8060 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8061 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8062 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8063 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8064 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8065 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8066 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8067 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8068 | 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] | 8069 | |
| 8070 | /* Register the garbage collector entry. */ |
| 8071 | lua_pushstring(gL.T, "__gc"); |
| 8072 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8073 | 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] | 8074 | |
| 8075 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8076 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8077 | |
| 8078 | /* Proxy and server configuration initialisation. */ |
| 8079 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8080 | init_new_proxy(&socket_proxy); |
| 8081 | socket_proxy.parent = NULL; |
| 8082 | socket_proxy.last_change = now.tv_sec; |
| 8083 | socket_proxy.id = "LUA-SOCKET"; |
| 8084 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8085 | socket_proxy.maxconn = 0; |
| 8086 | socket_proxy.accept = NULL; |
| 8087 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8088 | socket_proxy.srv = NULL; |
| 8089 | socket_proxy.conn_retries = 0; |
| 8090 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8091 | |
| 8092 | /* Init TCP server: unchanged parameters */ |
| 8093 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8094 | socket_tcp.next = NULL; |
| 8095 | socket_tcp.proxy = &socket_proxy; |
| 8096 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8097 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8098 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8099 | socket_tcp.priv_conns = NULL; |
| 8100 | socket_tcp.idle_conns = NULL; |
| 8101 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8102 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8103 | socket_tcp.last_change = 0; |
| 8104 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8105 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8106 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8107 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8108 | |
| 8109 | /* XXX: Copy default parameter from default server, |
| 8110 | * but the default server is not initialized. |
| 8111 | */ |
| 8112 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8113 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8114 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8115 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8116 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8117 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8118 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8119 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8120 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8121 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8122 | |
| 8123 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8124 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8125 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8126 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8127 | socket_tcp.check.server = &socket_tcp; |
| 8128 | |
| 8129 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8130 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8131 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8132 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8133 | socket_tcp.agent.server = &socket_tcp; |
| 8134 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8135 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8136 | |
| 8137 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8138 | /* Init TCP server: unchanged parameters */ |
| 8139 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8140 | socket_ssl.next = NULL; |
| 8141 | socket_ssl.proxy = &socket_proxy; |
| 8142 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8143 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8144 | socket_ssl.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8145 | socket_tcp.priv_conns = NULL; |
| 8146 | socket_tcp.idle_conns = NULL; |
| 8147 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8148 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8149 | socket_ssl.last_change = 0; |
| 8150 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8151 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8152 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8153 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8154 | |
| 8155 | /* XXX: Copy default parameter from default server, |
| 8156 | * but the default server is not initialized. |
| 8157 | */ |
| 8158 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8159 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8160 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8161 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8162 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8163 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8164 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8165 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8166 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8167 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8168 | |
| 8169 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8170 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8171 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8172 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8173 | socket_ssl.check.server = &socket_ssl; |
| 8174 | |
| 8175 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8176 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8177 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8178 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8179 | socket_ssl.agent.server = &socket_ssl; |
| 8180 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8181 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8182 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8183 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8184 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8185 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8186 | /* |
| 8187 | * |
| 8188 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8189 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8190 | * features like client certificates and ssl_verify. |
| 8191 | * |
| 8192 | */ |
| 8193 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8194 | if (tmp_error != 0) { |
| 8195 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8196 | abort(); /* This must be never arrives because the command line |
| 8197 | not editable by the user. */ |
| 8198 | } |
| 8199 | idx += kw->skip; |
| 8200 | } |
| 8201 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8202 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8203 | |
| 8204 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8205 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8206 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8207 | static void hlua_register_build_options(void) |
| 8208 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8209 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8210 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8211 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8212 | hap_register_build_opts(ptr, 1); |
| 8213 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8214 | |
| 8215 | INITCALL0(STG_REGISTER, hlua_register_build_options); |
Willy Tarreau | e655251 | 2018-11-26 11:33:13 +0100 | [diff] [blame] | 8216 | REGISTER_CONFIG_POSTPARSER("hlua", hlua_check_config); |