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 | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 29 | #include <common/xref.h> |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 30 | #include <common/hathreads.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 31 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 32 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 33 | #include <types/hlua.h> |
| 34 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 35 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 36 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 37 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 38 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 39 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 40 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 41 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 42 | #include <proto/stats.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 43 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 44 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 45 | #include <proto/hlua_fcn.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 46 | #include <proto/http_fetch.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 47 | #include <proto/http_rules.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 48 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 49 | #include <proto/obj_type.h> |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 50 | #include <proto/queue.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 51 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 52 | #include <proto/payload.h> |
| 53 | #include <proto/proto_http.h> |
| 54 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 55 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 56 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 57 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 58 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 59 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 60 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 61 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 62 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 63 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 64 | * macro is used only for identifying the function that can |
| 65 | * not return because a longjmp is executed. |
| 66 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 67 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 68 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 69 | */ |
| 70 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 71 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 72 | #define MAY_LJMP(func) func |
| 73 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 74 | /* This couple of function executes securely some Lua calls outside of |
| 75 | * the lua runtime environment. Each Lua call can return a longjmp |
| 76 | * if it encounter a memory error. |
| 77 | * |
| 78 | * Lua documentation extract: |
| 79 | * |
| 80 | * If an error happens outside any protected environment, Lua calls |
| 81 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 82 | * exiting the host application. Your panic function can avoid this |
| 83 | * exit by never returning (e.g., doing a long jump to your own |
| 84 | * recovery point outside Lua). |
| 85 | * |
| 86 | * The panic function runs as if it were a message handler (see |
| 87 | * §2.3); in particular, the error message is at the top of the |
| 88 | * stack. However, there is no guarantee about stack space. To push |
| 89 | * anything on the stack, the panic function must first check the |
| 90 | * available space (see §4.2). |
| 91 | * |
| 92 | * We must check all the Lua entry point. This includes: |
| 93 | * - The include/proto/hlua.h exported functions |
| 94 | * - the task wrapper function |
| 95 | * - The action wrapper function |
| 96 | * - The converters wrapper function |
| 97 | * - The sample-fetch wrapper functions |
| 98 | * |
| 99 | * It is tolerated that the initilisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 100 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 101 | * |
| 102 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 103 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 104 | * because they must be exists in the program stack when the longjmp |
| 105 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 106 | * |
| 107 | * Note that the Lua processing is not really thread safe. It provides |
| 108 | * heavy system which consists to add our own lock function in the Lua |
| 109 | * code and recompile the library. This system will probably not accepted |
| 110 | * by maintainers of various distribs. |
| 111 | * |
| 112 | * Our main excution point of the Lua is the function lua_resume(). A |
| 113 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 114 | * of function and a lua_unlock() at the end of the function. So I |
| 115 | * conclude that the Lua thread safe mode just perform a mutex around |
| 116 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 117 | * easier for distro maintainers. |
| 118 | * |
| 119 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 120 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 121 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 122 | */ |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 123 | __decl_hathreads(HA_SPINLOCK_T hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 124 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 125 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 126 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 127 | |
| 128 | #define SET_SAFE_LJMP(__L) \ |
| 129 | ({ \ |
| 130 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 131 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 132 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 133 | lua_atpanic(__L, hlua_panic_safe); \ |
| 134 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 135 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 136 | } else { \ |
| 137 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 138 | ret = 1; \ |
| 139 | } \ |
| 140 | ret; \ |
| 141 | }) |
| 142 | |
| 143 | /* If we are the last function catching Lua errors, we |
| 144 | * must reset the panic function. |
| 145 | */ |
| 146 | #define RESET_SAFE_LJMP(__L) \ |
| 147 | do { \ |
| 148 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 149 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 150 | } while(0) |
| 151 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 152 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 153 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
| 154 | #define APPLET_100C 0x02 /* 100 continue expected. */ |
| 155 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 156 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 157 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 158 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 159 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 160 | /* The main Lua execution context. */ |
| 161 | struct hlua gL; |
| 162 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 163 | /* This is the memory pool containing struct lua for applets |
| 164 | * (including cli). |
| 165 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 166 | struct pool_head *pool_head_hlua; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 167 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 168 | /* Used for Socket connection. */ |
| 169 | static struct proxy socket_proxy; |
| 170 | static struct server socket_tcp; |
| 171 | #ifdef USE_OPENSSL |
| 172 | static struct server socket_ssl; |
| 173 | #endif |
| 174 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 175 | /* List head of the function called at the initialisation time. */ |
| 176 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 177 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 178 | /* The following variables contains the reference of the different |
| 179 | * Lua classes. These references are useful for identify metadata |
| 180 | * associated with an object. |
| 181 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 182 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 183 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 184 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 185 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 186 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 187 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 188 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 189 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 190 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 191 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 192 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 193 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 194 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 195 | * because a task may remain alive during all the haproxy execution. |
| 196 | */ |
| 197 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 198 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 199 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 200 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 201 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 202 | * it is used for preventing infinite loops. |
| 203 | * |
| 204 | * I test the scheer with an infinite loop containing one incrementation |
| 205 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 206 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 207 | * to one interrupt each 10 000 instructions. |
| 208 | * |
| 209 | * configured | Number of |
| 210 | * instructions | loops executed |
| 211 | * between two | in milions |
| 212 | * forced yields | |
| 213 | * ---------------+--------------- |
| 214 | * 10 | 160 |
| 215 | * 500 | 670 |
| 216 | * 1000 | 680 |
| 217 | * 5000 | 700 |
| 218 | * 7000 | 700 |
| 219 | * 8000 | 700 |
| 220 | * 9000 | 710 <- ceil |
| 221 | * 10000 | 710 |
| 222 | * 100000 | 710 |
| 223 | * 1000000 | 710 |
| 224 | * |
| 225 | */ |
| 226 | static unsigned int hlua_nb_instruction = 10000; |
| 227 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 228 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 229 | * be enforced on any memory allocation. |
| 230 | */ |
| 231 | struct hlua_mem_allocator { |
| 232 | size_t allocated; |
| 233 | size_t limit; |
| 234 | }; |
| 235 | |
| 236 | static struct hlua_mem_allocator hlua_global_allocator; |
| 237 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 238 | static const char error_500[] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 239 | "HTTP/1.0 500 Internal Server Error\r\n" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 240 | "Cache-Control: no-cache\r\n" |
| 241 | "Connection: close\r\n" |
| 242 | "Content-Type: text/html\r\n" |
| 243 | "\r\n" |
Joseph Herlant | 7fe1577 | 2018-11-15 10:07:32 -0800 | [diff] [blame] | 244 | "<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] | 245 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 246 | /* These functions converts types between HAProxy internal args or |
| 247 | * sample and LUA types. Another function permits to check if the |
| 248 | * LUA stack contains arguments according with an required ARG_T |
| 249 | * format. |
| 250 | */ |
| 251 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 252 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 253 | __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] | 254 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 255 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 256 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 257 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 258 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 259 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 260 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 261 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 262 | do { \ |
| 263 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 264 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 265 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 266 | } while (0) |
| 267 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 268 | /* Used to check an Lua function type in the stack. It creates and |
| 269 | * returns a reference of the function. This function throws an |
| 270 | * error if the rgument is not a "function". |
| 271 | */ |
| 272 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 273 | { |
| 274 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 275 | 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] | 276 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 277 | } |
| 278 | lua_pushvalue(L, argno); |
| 279 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 280 | } |
| 281 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 282 | /* Return the string that is of the top of the stack. */ |
| 283 | const char *hlua_get_top_error_string(lua_State *L) |
| 284 | { |
| 285 | if (lua_gettop(L) < 1) |
| 286 | return "unknown error"; |
| 287 | if (lua_type(L, -1) != LUA_TSTRING) |
| 288 | return "unknown error"; |
| 289 | return lua_tostring(L, -1); |
| 290 | } |
| 291 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 292 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 293 | { |
| 294 | lua_Debug ar; |
| 295 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 296 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 297 | int filled = 0; |
| 298 | |
| 299 | while (lua_getstack(L, level++, &ar)) { |
| 300 | |
| 301 | /* Add separator */ |
| 302 | if (filled) |
| 303 | chunk_appendf(msg, ", "); |
| 304 | filled = 1; |
| 305 | |
| 306 | /* Fill fields: |
| 307 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 308 | * 'l': fills in the field currentline; |
| 309 | * 'n': fills in the field name and namewhat; |
| 310 | * 't': fills in the field istailcall; |
| 311 | */ |
| 312 | lua_getinfo(L, "Slnt", &ar); |
| 313 | |
| 314 | /* Append code localisation */ |
| 315 | if (ar.currentline > 0) |
| 316 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 317 | else |
| 318 | chunk_appendf(msg, "%s ", ar.short_src); |
| 319 | |
| 320 | /* |
| 321 | * Get function name |
| 322 | * |
| 323 | * if namewhat is no empty, name is defined. |
| 324 | * what contains "Lua" for Lua function, "C" for C function, |
| 325 | * or "main" for main code. |
| 326 | */ |
| 327 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 328 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 329 | |
| 330 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 331 | chunk_appendf(msg, "main chunk"); |
| 332 | |
| 333 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 334 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 335 | |
| 336 | else /* nothing left... */ |
| 337 | chunk_appendf(msg, "?"); |
| 338 | |
| 339 | |
| 340 | /* Display tailed call */ |
| 341 | if (ar.istailcall) |
| 342 | chunk_appendf(msg, " ..."); |
| 343 | } |
| 344 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 345 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 349 | /* This function check the number of arguments available in the |
| 350 | * stack. If the number of arguments available is not the same |
| 351 | * then <nb> an error is throwed. |
| 352 | */ |
| 353 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 354 | { |
| 355 | if (lua_gettop(L) == nb) |
| 356 | return; |
| 357 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 358 | } |
| 359 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 360 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 361 | * and the line number where the error is encountered. |
| 362 | */ |
| 363 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 364 | { |
| 365 | va_list argp; |
| 366 | va_start(argp, fmt); |
| 367 | luaL_where(L, 1); |
| 368 | lua_pushvfstring(L, fmt, argp); |
| 369 | va_end(argp); |
| 370 | lua_concat(L, 2); |
| 371 | return 1; |
| 372 | } |
| 373 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 374 | /* This functions is used with sample fetch and converters. It |
| 375 | * converts the HAProxy configuration argument in a lua stack |
| 376 | * values. |
| 377 | * |
| 378 | * It takes an array of "arg", and each entry of the array is |
| 379 | * converted and pushed in the LUA stack. |
| 380 | */ |
| 381 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 382 | { |
| 383 | switch (arg->type) { |
| 384 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 385 | case ARGT_TIME: |
| 386 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 387 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 388 | break; |
| 389 | |
| 390 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 391 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 392 | break; |
| 393 | |
| 394 | case ARGT_IPV4: |
| 395 | case ARGT_IPV6: |
| 396 | case ARGT_MSK4: |
| 397 | case ARGT_MSK6: |
| 398 | case ARGT_FE: |
| 399 | case ARGT_BE: |
| 400 | case ARGT_TAB: |
| 401 | case ARGT_SRV: |
| 402 | case ARGT_USR: |
| 403 | case ARGT_MAP: |
| 404 | default: |
| 405 | lua_pushnil(L); |
| 406 | break; |
| 407 | } |
| 408 | return 1; |
| 409 | } |
| 410 | |
| 411 | /* This function take one entrie in an LUA stack at the index "ud", |
| 412 | * and try to convert it in an HAProxy argument entry. This is useful |
| 413 | * with sample fetch wrappers. The input arguments are gived to the |
| 414 | * lua wrapper and converted as arg list by thi function. |
| 415 | */ |
| 416 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 417 | { |
| 418 | switch (lua_type(L, ud)) { |
| 419 | |
| 420 | case LUA_TNUMBER: |
| 421 | case LUA_TBOOLEAN: |
| 422 | arg->type = ARGT_SINT; |
| 423 | arg->data.sint = lua_tointeger(L, ud); |
| 424 | break; |
| 425 | |
| 426 | case LUA_TSTRING: |
| 427 | arg->type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 428 | 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] | 429 | break; |
| 430 | |
| 431 | case LUA_TUSERDATA: |
| 432 | case LUA_TNIL: |
| 433 | case LUA_TTABLE: |
| 434 | case LUA_TFUNCTION: |
| 435 | case LUA_TTHREAD: |
| 436 | case LUA_TLIGHTUSERDATA: |
| 437 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 438 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 439 | break; |
| 440 | } |
| 441 | return 1; |
| 442 | } |
| 443 | |
| 444 | /* the following functions are used to convert a struct sample |
| 445 | * in Lua type. This useful to convert the return of the |
| 446 | * fetchs or converters. |
| 447 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 448 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 449 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 450 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 451 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 452 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 453 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 454 | break; |
| 455 | |
| 456 | case SMP_T_BIN: |
| 457 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 458 | 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] | 459 | break; |
| 460 | |
| 461 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 462 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 463 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 464 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 465 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 466 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 467 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 468 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 469 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 470 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 471 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 472 | 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] | 473 | break; |
| 474 | default: |
| 475 | lua_pushnil(L); |
| 476 | break; |
| 477 | } |
| 478 | break; |
| 479 | |
| 480 | case SMP_T_IPV4: |
| 481 | case SMP_T_IPV6: |
| 482 | 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] | 483 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 484 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 485 | 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] | 486 | else |
| 487 | lua_pushnil(L); |
| 488 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 489 | default: |
| 490 | lua_pushnil(L); |
| 491 | break; |
| 492 | } |
| 493 | return 1; |
| 494 | } |
| 495 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 496 | /* the following functions are used to convert a struct sample |
| 497 | * in Lua strings. This is useful to convert the return of the |
| 498 | * fetchs or converters. |
| 499 | */ |
| 500 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 501 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 502 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 503 | |
| 504 | case SMP_T_BIN: |
| 505 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 506 | 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] | 507 | break; |
| 508 | |
| 509 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 510 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 511 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 512 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 513 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 514 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 515 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 516 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 517 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 518 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 519 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 520 | 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] | 521 | break; |
| 522 | default: |
| 523 | lua_pushstring(L, ""); |
| 524 | break; |
| 525 | } |
| 526 | break; |
| 527 | |
| 528 | case SMP_T_SINT: |
| 529 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 530 | case SMP_T_IPV4: |
| 531 | case SMP_T_IPV6: |
| 532 | 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] | 533 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 534 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 535 | 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] | 536 | else |
| 537 | lua_pushstring(L, ""); |
| 538 | break; |
| 539 | default: |
| 540 | lua_pushstring(L, ""); |
| 541 | break; |
| 542 | } |
| 543 | return 1; |
| 544 | } |
| 545 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 546 | /* the following functions are used to convert an Lua type in a |
| 547 | * struct sample. This is useful to provide data from a converter |
| 548 | * to the LUA code. |
| 549 | */ |
| 550 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 551 | { |
| 552 | switch (lua_type(L, ud)) { |
| 553 | |
| 554 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 555 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 556 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 557 | break; |
| 558 | |
| 559 | |
| 560 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 561 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 562 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 563 | break; |
| 564 | |
| 565 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 566 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 567 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 568 | 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] | 569 | break; |
| 570 | |
| 571 | case LUA_TUSERDATA: |
| 572 | case LUA_TNIL: |
| 573 | case LUA_TTABLE: |
| 574 | case LUA_TFUNCTION: |
| 575 | case LUA_TTHREAD: |
| 576 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 577 | case LUA_TNONE: |
| 578 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 579 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 580 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 581 | break; |
| 582 | } |
| 583 | return 1; |
| 584 | } |
| 585 | |
| 586 | /* This function check the "argp" builded by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 587 | * 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] | 588 | * 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] | 589 | * |
| 590 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 591 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 592 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 593 | __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] | 594 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 595 | { |
| 596 | int min_arg; |
| 597 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 598 | struct proxy *px; |
| 599 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 600 | |
| 601 | idx = 0; |
| 602 | min_arg = ARGM(mask); |
| 603 | mask >>= ARGM_BITS; |
| 604 | |
| 605 | while (1) { |
| 606 | |
| 607 | /* Check oversize. */ |
| 608 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 609 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /* Check for mandatory arguments. */ |
| 613 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 614 | if (idx < min_arg) { |
| 615 | |
| 616 | /* If miss other argument than the first one, we return an error. */ |
| 617 | if (idx > 0) |
| 618 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 619 | |
| 620 | /* If first argument have a certain type, some default values |
| 621 | * may be used. See the function smp_resolve_args(). |
| 622 | */ |
| 623 | switch (mask & ARGT_MASK) { |
| 624 | |
| 625 | case ARGT_FE: |
| 626 | if (!(p->cap & PR_CAP_FE)) |
| 627 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 628 | argp[idx].data.prx = p; |
| 629 | argp[idx].type = ARGT_FE; |
| 630 | argp[idx+1].type = ARGT_STOP; |
| 631 | break; |
| 632 | |
| 633 | case ARGT_BE: |
| 634 | if (!(p->cap & PR_CAP_BE)) |
| 635 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 636 | argp[idx].data.prx = p; |
| 637 | argp[idx].type = ARGT_BE; |
| 638 | argp[idx+1].type = ARGT_STOP; |
| 639 | break; |
| 640 | |
| 641 | case ARGT_TAB: |
| 642 | argp[idx].data.prx = p; |
| 643 | argp[idx].type = ARGT_TAB; |
| 644 | argp[idx+1].type = ARGT_STOP; |
| 645 | break; |
| 646 | |
| 647 | default: |
| 648 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 649 | break; |
| 650 | } |
| 651 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | /* Check for exceed the number of requiered argument. */ |
| 656 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 657 | argp[idx].type != ARGT_STOP) { |
| 658 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 659 | } |
| 660 | |
| 661 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 662 | argp[idx].type == ARGT_STOP) { |
| 663 | return 0; |
| 664 | } |
| 665 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 666 | /* Convert some argument types. */ |
| 667 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 668 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 669 | if (argp[idx].type != ARGT_SINT) |
| 670 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 671 | argp[idx].type = ARGT_SINT; |
| 672 | break; |
| 673 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 674 | case ARGT_TIME: |
| 675 | if (argp[idx].type != ARGT_SINT) |
| 676 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 677 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 678 | break; |
| 679 | |
| 680 | case ARGT_SIZE: |
| 681 | if (argp[idx].type != ARGT_SINT) |
| 682 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 683 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 684 | break; |
| 685 | |
| 686 | case ARGT_FE: |
| 687 | if (argp[idx].type != ARGT_STR) |
| 688 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 689 | memcpy(trash.area, argp[idx].data.str.area, |
| 690 | argp[idx].data.str.data); |
| 691 | trash.area[argp[idx].data.str.data] = 0; |
| 692 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 693 | if (!argp[idx].data.prx) |
| 694 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 695 | argp[idx].type = ARGT_FE; |
| 696 | break; |
| 697 | |
| 698 | case ARGT_BE: |
| 699 | if (argp[idx].type != ARGT_STR) |
| 700 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 701 | memcpy(trash.area, argp[idx].data.str.area, |
| 702 | argp[idx].data.str.data); |
| 703 | trash.area[argp[idx].data.str.data] = 0; |
| 704 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 705 | if (!argp[idx].data.prx) |
| 706 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 707 | argp[idx].type = ARGT_BE; |
| 708 | break; |
| 709 | |
| 710 | case ARGT_TAB: |
| 711 | if (argp[idx].type != ARGT_STR) |
| 712 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 713 | memcpy(trash.area, argp[idx].data.str.area, |
| 714 | argp[idx].data.str.data); |
| 715 | trash.area[argp[idx].data.str.data] = 0; |
| 716 | argp[idx].data.prx = proxy_tbl_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 717 | if (!argp[idx].data.prx) |
| 718 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 719 | argp[idx].type = ARGT_TAB; |
| 720 | break; |
| 721 | |
| 722 | case ARGT_SRV: |
| 723 | if (argp[idx].type != ARGT_STR) |
| 724 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 725 | memcpy(trash.area, argp[idx].data.str.area, |
| 726 | argp[idx].data.str.data); |
| 727 | trash.area[argp[idx].data.str.data] = 0; |
| 728 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 729 | if (sname) { |
| 730 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 731 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 732 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 733 | if (!px) |
| 734 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 735 | } |
| 736 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 737 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 738 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 739 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 740 | argp[idx].data.srv = findserver(px, sname); |
| 741 | if (!argp[idx].data.srv) |
| 742 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 743 | argp[idx].type = ARGT_SRV; |
| 744 | break; |
| 745 | |
| 746 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 747 | memcpy(trash.area, argp[idx].data.str.area, |
| 748 | argp[idx].data.str.data); |
| 749 | trash.area[argp[idx].data.str.data] = 0; |
| 750 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 751 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 752 | argp[idx].type = ARGT_IPV4; |
| 753 | break; |
| 754 | |
| 755 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 756 | memcpy(trash.area, argp[idx].data.str.area, |
| 757 | argp[idx].data.str.data); |
| 758 | trash.area[argp[idx].data.str.data] = 0; |
| 759 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 760 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 761 | argp[idx].type = ARGT_MSK4; |
| 762 | break; |
| 763 | |
| 764 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 765 | memcpy(trash.area, argp[idx].data.str.area, |
| 766 | argp[idx].data.str.data); |
| 767 | trash.area[argp[idx].data.str.data] = 0; |
| 768 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 769 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 770 | argp[idx].type = ARGT_IPV6; |
| 771 | break; |
| 772 | |
| 773 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 774 | memcpy(trash.area, argp[idx].data.str.area, |
| 775 | argp[idx].data.str.data); |
| 776 | trash.area[argp[idx].data.str.data] = 0; |
| 777 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 778 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 779 | argp[idx].type = ARGT_MSK6; |
| 780 | break; |
| 781 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 782 | case ARGT_MAP: |
| 783 | case ARGT_REG: |
| 784 | case ARGT_USR: |
| 785 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 786 | break; |
| 787 | } |
| 788 | |
| 789 | /* Check for type of argument. */ |
| 790 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 791 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 792 | arg_type_names[(mask & ARGT_MASK)], |
| 793 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 794 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 795 | } |
| 796 | |
| 797 | /* Next argument. */ |
| 798 | mask >>= ARGT_BITS; |
| 799 | idx++; |
| 800 | } |
| 801 | } |
| 802 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 803 | /* |
| 804 | * The following functions are used to make correspondance between the the |
| 805 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 806 | * |
| 807 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 808 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 809 | */ |
| 810 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 811 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 812 | struct hlua **hlua = lua_getextraspace(L); |
| 813 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 814 | } |
| 815 | static inline void hlua_sethlua(struct hlua *hlua) |
| 816 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 817 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 818 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 819 | } |
| 820 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 821 | /* This function is used to send logs. It try to send on screen (stderr) |
| 822 | * and on the default syslog server. |
| 823 | */ |
| 824 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 825 | { |
| 826 | struct tm tm; |
| 827 | char *p; |
| 828 | |
| 829 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 830 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 831 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 832 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 833 | /* Break the message if exceed the buffer size. */ |
| 834 | *(p-4) = ' '; |
| 835 | *(p-3) = '.'; |
| 836 | *(p-2) = '.'; |
| 837 | *(p-1) = '.'; |
| 838 | break; |
| 839 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 840 | if (isprint(*msg)) |
| 841 | *p = *msg; |
| 842 | else |
| 843 | *p = '.'; |
| 844 | } |
| 845 | *p = '\0'; |
| 846 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 847 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 848 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 849 | get_localtime(date.tv_sec, &tm); |
| 850 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 851 | 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] | 852 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 853 | fflush(stderr); |
| 854 | } |
| 855 | } |
| 856 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 857 | /* This function just ensure that the yield will be always |
| 858 | * returned with a timeout and permit to set some flags |
| 859 | */ |
| 860 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 861 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 862 | { |
| 863 | struct hlua *hlua = hlua_gethlua(L); |
| 864 | |
| 865 | /* Set the wake timeout. If timeout is required, we set |
| 866 | * the expiration time. |
| 867 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 868 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 869 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 870 | hlua->flags |= flags; |
| 871 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 872 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 873 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 874 | } |
| 875 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 876 | /* This function initialises the Lua environment stored in the stream. |
| 877 | * 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] | 878 | * 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] | 879 | * |
| 880 | * This function is particular. it initialises a new Lua thread. If the |
| 881 | * initialisation fails (example: out of memory error), the lua function |
| 882 | * throws an error (longjmp). |
| 883 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 884 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 885 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 886 | * MUST NOT manipulate the created thread stack state, because it is not |
| 887 | * proctected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 888 | */ |
| 889 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 890 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 891 | if (!SET_SAFE_LJMP(gL.T)) { |
| 892 | lua->Tref = LUA_REFNIL; |
| 893 | return 0; |
| 894 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 895 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 896 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 897 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 898 | lua->T = lua_newthread(gL.T); |
| 899 | if (!lua->T) { |
| 900 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | 0a97620 | 2017-07-12 11:18:00 +0200 | [diff] [blame] | 901 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 902 | return 0; |
| 903 | } |
| 904 | hlua_sethlua(lua); |
| 905 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 906 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 907 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 908 | return 1; |
| 909 | } |
| 910 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 911 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 912 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 913 | * is not freed. |
| 914 | */ |
| 915 | void hlua_ctx_destroy(struct hlua *lua) |
| 916 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 917 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 918 | return; |
| 919 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 920 | if (!lua->T) |
| 921 | goto end; |
| 922 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 923 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 924 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 925 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 926 | if (!SET_SAFE_LJMP(lua->T)) |
| 927 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 928 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 929 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 930 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 931 | if (!SET_SAFE_LJMP(gL.T)) |
| 932 | return; |
| 933 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 934 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 935 | /* Forces a garbage collecting process. If the Lua program is finished |
| 936 | * without error, we run the GC on the thread pointer. Its freed all |
| 937 | * the unused memory. |
| 938 | * If the thread is finnish with an error or is currently yielded, |
| 939 | * it seems that the GC applied on the thread doesn't clean anything, |
| 940 | * so e run the GC on the main thread. |
| 941 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 942 | * the garbage collection. |
| 943 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 944 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 945 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 946 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 947 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 948 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 949 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 950 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 951 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 952 | |
| 953 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 954 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | /* This function is used to restore the Lua context when a coroutine |
| 958 | * fails. This function copy the common memory between old coroutine |
| 959 | * and the new coroutine. The old coroutine is destroyed, and its |
| 960 | * replaced by the new coroutine. |
| 961 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 962 | * as string error message and it is copied in the new stack. |
| 963 | */ |
| 964 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 965 | { |
| 966 | lua_State *T; |
| 967 | int new_ref; |
| 968 | |
| 969 | /* Renew the main LUA stack doesn't have sense. */ |
| 970 | if (lua == &gL) |
| 971 | return 0; |
| 972 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 973 | /* New Lua coroutine. */ |
| 974 | T = lua_newthread(gL.T); |
| 975 | if (!T) |
| 976 | return 0; |
| 977 | |
| 978 | /* Copy last error message. */ |
| 979 | if (keep_msg) |
| 980 | lua_xmove(lua->T, T, 1); |
| 981 | |
| 982 | /* Copy data between the coroutines. */ |
| 983 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 984 | lua_xmove(lua->T, T, 1); |
| 985 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 986 | |
| 987 | /* Destroy old data. */ |
| 988 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 989 | |
| 990 | /* The thread is garbage collected by Lua. */ |
| 991 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 992 | |
| 993 | /* Fill the struct with the new coroutine values. */ |
| 994 | lua->Mref = new_ref; |
| 995 | lua->T = T; |
| 996 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 997 | |
| 998 | /* Set context. */ |
| 999 | hlua_sethlua(lua); |
| 1000 | |
| 1001 | return 1; |
| 1002 | } |
| 1003 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1004 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1005 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1006 | struct hlua *hlua = hlua_gethlua(L); |
| 1007 | |
| 1008 | /* Lua cannot yield when its returning from a function, |
| 1009 | * so, we can fix the interrupt hook to 1 instruction, |
| 1010 | * expecting that the function is finnished. |
| 1011 | */ |
| 1012 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1013 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1014 | return; |
| 1015 | } |
| 1016 | |
| 1017 | /* restore the interrupt condition. */ |
| 1018 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1019 | |
| 1020 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1021 | * If the state is not yieldable, trying yield causes an error. |
| 1022 | */ |
| 1023 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1024 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1025 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1026 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1027 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1028 | hlua->run_time += now_ms - hlua->start_time; |
| 1029 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1030 | lua_pushfstring(L, "execution timeout"); |
| 1031 | WILL_LJMP(lua_error(L)); |
| 1032 | } |
| 1033 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1034 | /* Update the start time. */ |
| 1035 | hlua->start_time = now_ms; |
| 1036 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1037 | /* Try to interrupt the process at the end of the current |
| 1038 | * unyieldable function. |
| 1039 | */ |
| 1040 | 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] | 1041 | } |
| 1042 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1043 | /* This function start or resumes the Lua stack execution. If the flag |
| 1044 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1045 | * The function return an error. |
| 1046 | * |
| 1047 | * The function can returns 4 values: |
| 1048 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1049 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1050 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1051 | * - 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] | 1052 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1053 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1054 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1055 | * 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] | 1056 | * LUA code. |
| 1057 | */ |
| 1058 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1059 | { |
| 1060 | int ret; |
| 1061 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1062 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1063 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1064 | /* Initialise run time counter. */ |
| 1065 | if (!HLUA_IS_RUNNING(lua)) |
| 1066 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1067 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1068 | /* Lock the whole Lua execution. This lock must be before the |
| 1069 | * label "resume_execution". |
| 1070 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1071 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1072 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1073 | resume_execution: |
| 1074 | |
| 1075 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1076 | * instructions. it is used for preventing infinite loops. |
| 1077 | */ |
| 1078 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1079 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1080 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1081 | HLUA_SET_RUN(lua); |
| 1082 | HLUA_CLR_CTRLYIELD(lua); |
| 1083 | HLUA_CLR_WAKERESWR(lua); |
| 1084 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1085 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1086 | /* Update the start time. */ |
| 1087 | lua->start_time = now_ms; |
| 1088 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1089 | /* Call the function. */ |
| 1090 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1091 | switch (ret) { |
| 1092 | |
| 1093 | case LUA_OK: |
| 1094 | ret = HLUA_E_OK; |
| 1095 | break; |
| 1096 | |
| 1097 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1098 | /* Check if the execution timeout is expired. It it is the case, we |
| 1099 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1100 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1101 | tv_update_date(0, 1); |
| 1102 | lua->run_time += now_ms - lua->start_time; |
| 1103 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1104 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1105 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1106 | break; |
| 1107 | } |
| 1108 | /* Process the forced yield. if the general yield is not allowed or |
| 1109 | * if no task were associated this the current Lua execution |
| 1110 | * coroutine, we resume the execution. Else we want to return in the |
| 1111 | * scheduler and we want to be waked up again, to continue the |
| 1112 | * current Lua execution. So we schedule our own task. |
| 1113 | */ |
| 1114 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1115 | if (!yield_allowed || !lua->task) |
| 1116 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1117 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1118 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1119 | if (!yield_allowed) { |
| 1120 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1121 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1122 | break; |
| 1123 | } |
| 1124 | ret = HLUA_E_AGAIN; |
| 1125 | break; |
| 1126 | |
| 1127 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1128 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1129 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1130 | * because the errors ares the only one mean to return immediately |
| 1131 | * from and lua execution. |
| 1132 | */ |
| 1133 | if (lua->flags & HLUA_EXIT) { |
| 1134 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1135 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1136 | break; |
| 1137 | } |
| 1138 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1139 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1140 | if (!lua_checkstack(lua->T, 1)) { |
| 1141 | ret = HLUA_E_ERR; |
| 1142 | break; |
| 1143 | } |
| 1144 | msg = lua_tostring(lua->T, -1); |
| 1145 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1146 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1147 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1148 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1149 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1150 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1151 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1152 | ret = HLUA_E_ERRMSG; |
| 1153 | break; |
| 1154 | |
| 1155 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1156 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1157 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1158 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1159 | break; |
| 1160 | |
| 1161 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1162 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1163 | if (!lua_checkstack(lua->T, 1)) { |
| 1164 | ret = HLUA_E_ERR; |
| 1165 | break; |
| 1166 | } |
| 1167 | msg = lua_tostring(lua->T, -1); |
| 1168 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1169 | lua_pop(lua->T, 1); |
| 1170 | if (msg) |
| 1171 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1172 | else |
| 1173 | lua_pushfstring(lua->T, "message handler error"); |
| 1174 | ret = HLUA_E_ERRMSG; |
| 1175 | break; |
| 1176 | |
| 1177 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1178 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1179 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1180 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1181 | break; |
| 1182 | } |
| 1183 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1184 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1185 | if (lua->flags & HLUA_MUST_GC && |
| 1186 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1187 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1188 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1189 | switch (ret) { |
| 1190 | case HLUA_E_AGAIN: |
| 1191 | break; |
| 1192 | |
| 1193 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1194 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1195 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1196 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1197 | break; |
| 1198 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1199 | case HLUA_E_ETMOUT: |
| 1200 | case HLUA_E_NOMEM: |
| 1201 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1202 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1203 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1204 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1205 | hlua_ctx_renew(lua, 0); |
| 1206 | break; |
| 1207 | |
| 1208 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1209 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1210 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1211 | break; |
| 1212 | } |
| 1213 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1214 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1215 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1216 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1217 | return ret; |
| 1218 | } |
| 1219 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1220 | /* This function exit the current code. */ |
| 1221 | __LJMP static int hlua_done(lua_State *L) |
| 1222 | { |
| 1223 | struct hlua *hlua = hlua_gethlua(L); |
| 1224 | |
| 1225 | hlua->flags |= HLUA_EXIT; |
| 1226 | WILL_LJMP(lua_error(L)); |
| 1227 | |
| 1228 | return 0; |
| 1229 | } |
| 1230 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1231 | /* This function is an LUA binding. It provides a function |
| 1232 | * for deleting ACL from a referenced ACL file. |
| 1233 | */ |
| 1234 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1235 | { |
| 1236 | const char *name; |
| 1237 | const char *key; |
| 1238 | struct pat_ref *ref; |
| 1239 | |
| 1240 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1241 | |
| 1242 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1243 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1244 | |
| 1245 | ref = pat_ref_lookup(name); |
| 1246 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1247 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1248 | |
| 1249 | pat_ref_delete(ref, key); |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
| 1253 | /* This function is an LUA binding. It provides a function |
| 1254 | * for deleting map entry from a referenced map file. |
| 1255 | */ |
| 1256 | static int hlua_del_map(lua_State *L) |
| 1257 | { |
| 1258 | const char *name; |
| 1259 | const char *key; |
| 1260 | struct pat_ref *ref; |
| 1261 | |
| 1262 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1263 | |
| 1264 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1265 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1266 | |
| 1267 | ref = pat_ref_lookup(name); |
| 1268 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1269 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1270 | |
| 1271 | pat_ref_delete(ref, key); |
| 1272 | return 0; |
| 1273 | } |
| 1274 | |
| 1275 | /* This function is an LUA binding. It provides a function |
| 1276 | * for adding ACL pattern from a referenced ACL file. |
| 1277 | */ |
| 1278 | static int hlua_add_acl(lua_State *L) |
| 1279 | { |
| 1280 | const char *name; |
| 1281 | const char *key; |
| 1282 | struct pat_ref *ref; |
| 1283 | |
| 1284 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1285 | |
| 1286 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1287 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1288 | |
| 1289 | ref = pat_ref_lookup(name); |
| 1290 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1291 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1292 | |
| 1293 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1294 | pat_ref_add(ref, key, NULL, NULL); |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | /* This function is an LUA binding. It provides a function |
| 1299 | * for setting map pattern and sample from a referenced map |
| 1300 | * file. |
| 1301 | */ |
| 1302 | static int hlua_set_map(lua_State *L) |
| 1303 | { |
| 1304 | const char *name; |
| 1305 | const char *key; |
| 1306 | const char *value; |
| 1307 | struct pat_ref *ref; |
| 1308 | |
| 1309 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1310 | |
| 1311 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1312 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1313 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1314 | |
| 1315 | ref = pat_ref_lookup(name); |
| 1316 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1317 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1318 | |
| 1319 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1320 | pat_ref_set(ref, key, value, NULL); |
| 1321 | else |
| 1322 | pat_ref_add(ref, key, value, NULL); |
| 1323 | return 0; |
| 1324 | } |
| 1325 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1326 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1327 | * an integer or user data. This data is associated with a metatable. This |
| 1328 | * metatable have an original version registred in the global context with |
| 1329 | * the name of the object (_G[<name>] = <metable> ). |
| 1330 | * |
| 1331 | * A metable is a table that modify the standard behavior of a standard |
| 1332 | * access to the associated data. The entries of this new metatable are |
| 1333 | * defined as is: |
| 1334 | * |
| 1335 | * http://lua-users.org/wiki/MetatableEvents |
| 1336 | * |
| 1337 | * __index |
| 1338 | * |
| 1339 | * we access an absent field in a table, the result is nil. This is |
| 1340 | * true, but it is not the whole truth. Actually, such access triggers |
| 1341 | * the interpreter to look for an __index metamethod: If there is no |
| 1342 | * such method, as usually happens, then the access results in nil; |
| 1343 | * otherwise, the metamethod will provide the result. |
| 1344 | * |
| 1345 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1346 | * the key does not appear in the table, but the metatable has an __index |
| 1347 | * property: |
| 1348 | * |
| 1349 | * - if the value is a function, the function is called, passing in the |
| 1350 | * table and the key; the return value of that function is returned as |
| 1351 | * the result. |
| 1352 | * |
| 1353 | * - if the value is another table, the value of the key in that table is |
| 1354 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1355 | * table's metatable has an __index property, then it continues on up) |
| 1356 | * |
| 1357 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1358 | * |
| 1359 | * http://www.lua.org/pil/13.4.1.html |
| 1360 | * |
| 1361 | * __newindex |
| 1362 | * |
| 1363 | * Like __index, but control property assignment. |
| 1364 | * |
| 1365 | * __mode - Control weak references. A string value with one or both |
| 1366 | * of the characters 'k' and 'v' which specifies that the the |
| 1367 | * keys and/or values in the table are weak references. |
| 1368 | * |
| 1369 | * __call - Treat a table like a function. When a table is followed by |
| 1370 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1371 | * a __call key pointing to a function, that function is invoked |
| 1372 | * (passing any specified arguments) and the return value is |
| 1373 | * returned. |
| 1374 | * |
| 1375 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1376 | * called, if the metatable for myTable has a __metatable |
| 1377 | * key, the value of that key is returned instead of the |
| 1378 | * actual metatable. |
| 1379 | * |
| 1380 | * __tostring - Control string representation. When the builtin |
| 1381 | * "tostring( myTable )" function is called, if the metatable |
| 1382 | * for myTable has a __tostring property set to a function, |
| 1383 | * that function is invoked (passing myTable to it) and the |
| 1384 | * return value is used as the string representation. |
| 1385 | * |
| 1386 | * __len - Control table length. When the table length is requested using |
| 1387 | * the length operator ( '#' ), if the metatable for myTable has |
| 1388 | * a __len key pointing to a function, that function is invoked |
| 1389 | * (passing myTable to it) and the return value used as the value |
| 1390 | * of "#myTable". |
| 1391 | * |
| 1392 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1393 | * collected, if the metatable has a __gc field pointing to a |
| 1394 | * function, that function is first invoked, passing the userdata |
| 1395 | * to it. The __gc metamethod is not called for tables. |
| 1396 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1397 | * |
| 1398 | * Special metamethods for redefining standard operators: |
| 1399 | * http://www.lua.org/pil/13.1.html |
| 1400 | * |
| 1401 | * __add "+" |
| 1402 | * __sub "-" |
| 1403 | * __mul "*" |
| 1404 | * __div "/" |
| 1405 | * __unm "!" |
| 1406 | * __pow "^" |
| 1407 | * __concat ".." |
| 1408 | * |
| 1409 | * Special methods for redfining standar relations |
| 1410 | * http://www.lua.org/pil/13.2.html |
| 1411 | * |
| 1412 | * __eq "==" |
| 1413 | * __lt "<" |
| 1414 | * __le "<=" |
| 1415 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1416 | |
| 1417 | /* |
| 1418 | * |
| 1419 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1420 | * Class Map |
| 1421 | * |
| 1422 | * |
| 1423 | */ |
| 1424 | |
| 1425 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1426 | * a class session, otherwise it throws an error. |
| 1427 | */ |
| 1428 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1429 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1430 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | /* This function is the map constructor. It don't need |
| 1434 | * the class Map object. It creates and return a new Map |
| 1435 | * object. It must be called only during "body" or "init" |
| 1436 | * context because it process some filesystem accesses. |
| 1437 | */ |
| 1438 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1439 | { |
| 1440 | const char *fn; |
| 1441 | int match = PAT_MATCH_STR; |
| 1442 | struct sample_conv conv; |
| 1443 | const char *file = ""; |
| 1444 | int line = 0; |
| 1445 | lua_Debug ar; |
| 1446 | char *err = NULL; |
| 1447 | struct arg args[2]; |
| 1448 | |
| 1449 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1450 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1451 | |
| 1452 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1453 | |
| 1454 | if (lua_gettop(L) >= 2) { |
| 1455 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1456 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1457 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1458 | } |
| 1459 | |
| 1460 | /* Get Lua filename and line number. */ |
| 1461 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1462 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1463 | if (ar.currentline > 0) { /* is there info? */ |
| 1464 | file = ar.short_src; |
| 1465 | line = ar.currentline; |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | /* fill fake sample_conv struct. */ |
| 1470 | conv.kw = ""; /* unused. */ |
| 1471 | conv.process = NULL; /* unused. */ |
| 1472 | conv.arg_mask = 0; /* unused. */ |
| 1473 | conv.val_args = NULL; /* unused. */ |
| 1474 | conv.out_type = SMP_T_STR; |
| 1475 | conv.private = (void *)(long)match; |
| 1476 | switch (match) { |
| 1477 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1478 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1479 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1480 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1481 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1482 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1483 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1484 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1485 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1486 | default: |
| 1487 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1488 | } |
| 1489 | |
| 1490 | /* fill fake args. */ |
| 1491 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1492 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1493 | args[1].type = ARGT_STOP; |
| 1494 | |
| 1495 | /* load the map. */ |
| 1496 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1497 | /* error case: we cant use luaL_error because we must |
| 1498 | * free the err variable. |
| 1499 | */ |
| 1500 | luaL_where(L, 1); |
| 1501 | lua_pushfstring(L, "'new': %s.", err); |
| 1502 | lua_concat(L, 2); |
| 1503 | free(err); |
| 1504 | WILL_LJMP(lua_error(L)); |
| 1505 | } |
| 1506 | |
| 1507 | /* create the lua object. */ |
| 1508 | lua_newtable(L); |
| 1509 | lua_pushlightuserdata(L, args[0].data.map); |
| 1510 | lua_rawseti(L, -2, 0); |
| 1511 | |
| 1512 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1513 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1514 | lua_setmetatable(L, -2); |
| 1515 | |
| 1516 | |
| 1517 | return 1; |
| 1518 | } |
| 1519 | |
| 1520 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1521 | { |
| 1522 | struct map_descriptor *desc; |
| 1523 | struct pattern *pat; |
| 1524 | struct sample smp; |
| 1525 | |
| 1526 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1527 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1528 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1529 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1530 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1531 | } |
| 1532 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1533 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1534 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1535 | 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] | 1536 | } |
| 1537 | |
| 1538 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1539 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1540 | if (str) |
| 1541 | lua_pushstring(L, ""); |
| 1542 | else |
| 1543 | lua_pushnil(L); |
| 1544 | return 1; |
| 1545 | } |
| 1546 | |
| 1547 | /* 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] | 1548 | 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] | 1549 | return 1; |
| 1550 | } |
| 1551 | |
| 1552 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1553 | { |
| 1554 | return _hlua_map_lookup(L, 0); |
| 1555 | } |
| 1556 | |
| 1557 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1558 | { |
| 1559 | return _hlua_map_lookup(L, 1); |
| 1560 | } |
| 1561 | |
| 1562 | /* |
| 1563 | * |
| 1564 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1565 | * Class Socket |
| 1566 | * |
| 1567 | * |
| 1568 | */ |
| 1569 | |
| 1570 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1571 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1572 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | /* 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] | 1576 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1577 | * received. |
| 1578 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1579 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1580 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1581 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1582 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1583 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1584 | if (appctx->ctx.hlua_cosocket.die) { |
| 1585 | si_shutw(si); |
| 1586 | si_shutr(si); |
| 1587 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1588 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1589 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1590 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1591 | } |
| 1592 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1593 | /* If the connection object is not available, close all the |
| 1594 | * streams and wakeup everything waiting for. |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1595 | */ |
| 1596 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1597 | si_shutw(si); |
| 1598 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1599 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1600 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1601 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1602 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1603 | } |
| 1604 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1605 | /* If we cant write, wakeup the pending write signals. */ |
| 1606 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1607 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1608 | |
| 1609 | /* If we cant read, wakeup the pending read signals. */ |
| 1610 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1611 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1612 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1613 | /* if the connection is not estabkished, inform the stream that we want |
| 1614 | * to be notified whenever the connection completes. |
| 1615 | */ |
| 1616 | if (!(c->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1617 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1618 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1619 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1620 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1621 | |
| 1622 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1623 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1624 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1625 | /* 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] | 1626 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1627 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1628 | |
| 1629 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1630 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1631 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1632 | |
| 1633 | /* Some data were injected in the buffer, notify the stream |
| 1634 | * interface. |
| 1635 | */ |
| 1636 | if (!channel_is_empty(si_ic(si))) |
| 1637 | stream_int_update(si); |
| 1638 | |
| 1639 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1640 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1641 | */ |
| 1642 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1643 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1644 | } |
| 1645 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1646 | /* This function is called when the "struct stream" is destroyed. |
| 1647 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1648 | * Wake all the pending signals. |
| 1649 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1650 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1651 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1652 | struct xref *peer; |
| 1653 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1654 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1655 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1656 | if (peer) |
| 1657 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1658 | |
| 1659 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1660 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1661 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1665 | * uses this object. If the stream does not exists, just quit. |
| 1666 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1667 | * pending signal can rest in the read and write lists. destroy |
| 1668 | * it. |
| 1669 | */ |
| 1670 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1671 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1672 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1673 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1674 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1675 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1676 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1677 | |
| 1678 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1679 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1680 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1681 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1682 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1683 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1684 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1685 | appctx->ctx.hlua_cosocket.die = 1; |
| 1686 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1687 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1688 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1689 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1690 | return 0; |
| 1691 | } |
| 1692 | |
| 1693 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1694 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1695 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1696 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1697 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1698 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1699 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1700 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1701 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1702 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1703 | |
| 1704 | /* Check if we run on the same thread than the xreator thread. |
| 1705 | * We cannot access to the socket if the thread is different. |
| 1706 | */ |
| 1707 | if (socket->tid != tid) |
| 1708 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1709 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1710 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1711 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1712 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1713 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1714 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1715 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1716 | appctx->ctx.hlua_cosocket.die = 1; |
| 1717 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1718 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1719 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1720 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1721 | return 0; |
| 1722 | } |
| 1723 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1724 | /* The close function calls close_helper. |
| 1725 | */ |
| 1726 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1727 | { |
| 1728 | MAY_LJMP(check_args(L, 1, "close")); |
| 1729 | return hlua_socket_close_helper(L); |
| 1730 | } |
| 1731 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1732 | /* This Lua function assumes that the stack contain three parameters. |
| 1733 | * 1 - USERDATA containing a struct socket |
| 1734 | * 2 - INTEGER with values of the macro defined below |
| 1735 | * If the integer is -1, we must read at most one line. |
| 1736 | * If the integer is -2, we ust read all the data until the |
| 1737 | * end of the stream. |
| 1738 | * If the integer is positive value, we must read a number of |
| 1739 | * bytes corresponding to this value. |
| 1740 | */ |
| 1741 | #define HLSR_READ_LINE (-1) |
| 1742 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1743 | __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] | 1744 | { |
| 1745 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1746 | int wanted = lua_tointeger(L, 2); |
| 1747 | struct hlua *hlua = hlua_gethlua(L); |
| 1748 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1749 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1750 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1751 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1752 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1753 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1754 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1755 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1756 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1757 | struct stream_interface *si; |
| 1758 | struct stream *s; |
| 1759 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1760 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1761 | |
| 1762 | /* Check if this lua stack is schedulable. */ |
| 1763 | if (!hlua || !hlua->task) |
| 1764 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1765 | "'frontend', 'backend' or 'task'")); |
| 1766 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1767 | /* Check if we run on the same thread than the xreator thread. |
| 1768 | * We cannot access to the socket if the thread is different. |
| 1769 | */ |
| 1770 | if (socket->tid != tid) |
| 1771 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1772 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1773 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1774 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1775 | if (!peer) |
| 1776 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1777 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1778 | si = appctx->owner; |
| 1779 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1780 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1781 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1782 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1783 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1784 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1785 | if (nblk < 0) /* Connection close. */ |
| 1786 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1787 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1788 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1789 | |
| 1790 | /* remove final \r\n. */ |
| 1791 | if (nblk == 1) { |
| 1792 | if (blk1[len1-1] == '\n') { |
| 1793 | len1--; |
| 1794 | skip_at_end++; |
| 1795 | if (blk1[len1-1] == '\r') { |
| 1796 | len1--; |
| 1797 | skip_at_end++; |
| 1798 | } |
| 1799 | } |
| 1800 | } |
| 1801 | else { |
| 1802 | if (blk2[len2-1] == '\n') { |
| 1803 | len2--; |
| 1804 | skip_at_end++; |
| 1805 | if (blk2[len2-1] == '\r') { |
| 1806 | len2--; |
| 1807 | skip_at_end++; |
| 1808 | } |
| 1809 | } |
| 1810 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1814 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1815 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1816 | if (nblk < 0) /* Connection close. */ |
| 1817 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1818 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1819 | goto connection_empty; |
| 1820 | } |
| 1821 | |
| 1822 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1823 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1824 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1825 | if (nblk < 0) /* Connection close. */ |
| 1826 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1827 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1828 | goto connection_empty; |
| 1829 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1830 | missing_bytes = wanted - socket->b.n; |
| 1831 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1832 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1833 | len1 = missing_bytes; |
| 1834 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1835 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | len = len1; |
| 1839 | |
| 1840 | luaL_addlstring(&socket->b, blk1, len1); |
| 1841 | if (nblk == 2) { |
| 1842 | len += len2; |
| 1843 | luaL_addlstring(&socket->b, blk2, len2); |
| 1844 | } |
| 1845 | |
| 1846 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1847 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1848 | |
| 1849 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1850 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1851 | |
| 1852 | /* If the pattern reclaim to read all the data |
| 1853 | * in the connection, got out. |
| 1854 | */ |
| 1855 | if (wanted == HLSR_READ_ALL) |
| 1856 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1857 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1858 | goto connection_empty; |
| 1859 | |
| 1860 | /* Return result. */ |
| 1861 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1862 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1863 | return 1; |
| 1864 | |
| 1865 | connection_closed: |
| 1866 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1867 | xref_unlock(&socket->xref, peer); |
| 1868 | |
| 1869 | no_peer: |
| 1870 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1871 | /* If the buffer containds data. */ |
| 1872 | if (socket->b.n > 0) { |
| 1873 | luaL_pushresult(&socket->b); |
| 1874 | return 1; |
| 1875 | } |
| 1876 | lua_pushnil(L); |
| 1877 | lua_pushstring(L, "connection closed."); |
| 1878 | return 2; |
| 1879 | |
| 1880 | connection_empty: |
| 1881 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1882 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1883 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1884 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1885 | } |
| 1886 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1887 | 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] | 1888 | return 0; |
| 1889 | } |
| 1890 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1891 | /* This Lua function gets two parameters. The first one can be string |
| 1892 | * or a number. If the string is "*l", the user requires one line. If |
| 1893 | * 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] | 1894 | * If the value is a number, the user require a number of bytes equal |
| 1895 | * to the value. The default value is "*l" (a line). |
| 1896 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1897 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1898 | * integer takes this values: |
| 1899 | * -1 : read a line |
| 1900 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1901 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1902 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1903 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1904 | * concatenated with the read data. |
| 1905 | */ |
| 1906 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1907 | { |
| 1908 | int wanted = HLSR_READ_LINE; |
| 1909 | const char *pattern; |
| 1910 | int type; |
| 1911 | char *error; |
| 1912 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1913 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1914 | |
| 1915 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1916 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1917 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1918 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1919 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1920 | /* Check if we run on the same thread than the xreator thread. |
| 1921 | * We cannot access to the socket if the thread is different. |
| 1922 | */ |
| 1923 | if (socket->tid != tid) |
| 1924 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1925 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1926 | /* check for pattern. */ |
| 1927 | if (lua_gettop(L) >= 2) { |
| 1928 | type = lua_type(L, 2); |
| 1929 | if (type == LUA_TSTRING) { |
| 1930 | pattern = lua_tostring(L, 2); |
| 1931 | if (strcmp(pattern, "*a") == 0) |
| 1932 | wanted = HLSR_READ_ALL; |
| 1933 | else if (strcmp(pattern, "*l") == 0) |
| 1934 | wanted = HLSR_READ_LINE; |
| 1935 | else { |
| 1936 | wanted = strtoll(pattern, &error, 10); |
| 1937 | if (*error != '\0') |
| 1938 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1939 | } |
| 1940 | } |
| 1941 | else if (type == LUA_TNUMBER) { |
| 1942 | wanted = lua_tointeger(L, 2); |
| 1943 | if (wanted < 0) |
| 1944 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | /* Set pattern. */ |
| 1949 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1950 | |
| 1951 | /* Check if we would replace the top by itself. */ |
| 1952 | if (lua_gettop(L) != 2) |
| 1953 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1954 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1955 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1956 | luaL_buffinit(L, &socket->b); |
| 1957 | |
| 1958 | /* Check prefix. */ |
| 1959 | if (lua_gettop(L) >= 3) { |
| 1960 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1961 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1962 | pattern = lua_tolstring(L, 3, &len); |
| 1963 | luaL_addlstring(&socket->b, pattern, len); |
| 1964 | } |
| 1965 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1966 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1967 | } |
| 1968 | |
| 1969 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1970 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1971 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1972 | 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] | 1973 | { |
| 1974 | struct hlua_socket *socket; |
| 1975 | struct hlua *hlua = hlua_gethlua(L); |
| 1976 | struct appctx *appctx; |
| 1977 | size_t buf_len; |
| 1978 | const char *buf; |
| 1979 | int len; |
| 1980 | int send_len; |
| 1981 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1982 | struct xref *peer; |
| 1983 | struct stream_interface *si; |
| 1984 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1985 | |
| 1986 | /* Check if this lua stack is schedulable. */ |
| 1987 | if (!hlua || !hlua->task) |
| 1988 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1989 | "'frontend', 'backend' or 'task'")); |
| 1990 | |
| 1991 | /* Get object */ |
| 1992 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1993 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1994 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1995 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1996 | /* Check if we run on the same thread than the xreator thread. |
| 1997 | * We cannot access to the socket if the thread is different. |
| 1998 | */ |
| 1999 | if (socket->tid != tid) |
| 2000 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2001 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2002 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2003 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2004 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2005 | lua_pushinteger(L, -1); |
| 2006 | return 1; |
| 2007 | } |
| 2008 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2009 | si = appctx->owner; |
| 2010 | s = si_strm(si); |
| 2011 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2012 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2013 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2014 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | lua_pushinteger(L, -1); |
| 2016 | return 1; |
| 2017 | } |
| 2018 | |
| 2019 | /* Update the input buffer data. */ |
| 2020 | buf += sent; |
| 2021 | send_len = buf_len - sent; |
| 2022 | |
| 2023 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2024 | if (sent >= buf_len) { |
| 2025 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2026 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2027 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2028 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2029 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2030 | * the request buffer if its not required. |
| 2031 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2032 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2033 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2034 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2035 | } |
| 2036 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2037 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2038 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2039 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2040 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2041 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2042 | |
| 2043 | /* send data */ |
| 2044 | if (len < send_len) |
| 2045 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2046 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2047 | |
| 2048 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2049 | * the data" (-2) are not expected because the available length |
| 2050 | * is tested. |
| 2051 | * Other unknown error are also not expected. |
| 2052 | */ |
| 2053 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2054 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2055 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2056 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2057 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2058 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2059 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2060 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2061 | return 1; |
| 2062 | } |
| 2063 | |
| 2064 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2065 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2066 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2067 | s->req.rex = TICK_ETERNITY; |
| 2068 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2069 | |
| 2070 | /* Update length sent. */ |
| 2071 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2072 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2073 | |
| 2074 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2075 | if (sent + len >= buf_len) { |
| 2076 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2077 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2078 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2079 | |
| 2080 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2081 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2082 | xref_unlock(&socket->xref, peer); |
| 2083 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2084 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2085 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2086 | 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] | 2087 | return 0; |
| 2088 | } |
| 2089 | |
| 2090 | /* This function initiate the send of data. It just check the input |
| 2091 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2092 | * 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] | 2093 | * "hlua_socket_write_yield" that can yield. |
| 2094 | * |
| 2095 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2096 | * the associated object. The second is a string buffer. The third is |
| 2097 | * a facultative integer that represents where is the buffer position |
| 2098 | * of the start of the data that can send. The first byte is the |
| 2099 | * position "1". The default value is "1". The fourth argument is a |
| 2100 | * facultative integer that represents where is the buffer position |
| 2101 | * of the end of the data that can send. The default is the last byte. |
| 2102 | */ |
| 2103 | static int hlua_socket_send(struct lua_State *L) |
| 2104 | { |
| 2105 | int i; |
| 2106 | int j; |
| 2107 | const char *buf; |
| 2108 | size_t buf_len; |
| 2109 | |
| 2110 | /* Check number of arguments. */ |
| 2111 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2112 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2113 | |
| 2114 | /* Get the string. */ |
| 2115 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2116 | |
| 2117 | /* Get and check j. */ |
| 2118 | if (lua_gettop(L) == 4) { |
| 2119 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2120 | if (j < 0) |
| 2121 | j = buf_len + j + 1; |
| 2122 | if (j > buf_len) |
| 2123 | j = buf_len + 1; |
| 2124 | lua_pop(L, 1); |
| 2125 | } |
| 2126 | else |
| 2127 | j = buf_len; |
| 2128 | |
| 2129 | /* Get and check i. */ |
| 2130 | if (lua_gettop(L) == 3) { |
| 2131 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2132 | if (i < 0) |
| 2133 | i = buf_len + i + 1; |
| 2134 | if (i > buf_len) |
| 2135 | i = buf_len + 1; |
| 2136 | lua_pop(L, 1); |
| 2137 | } else |
| 2138 | i = 1; |
| 2139 | |
| 2140 | /* Check bth i and j. */ |
| 2141 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2142 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2143 | return 1; |
| 2144 | } |
| 2145 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2146 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2147 | return 1; |
| 2148 | } |
| 2149 | if (i == 0) |
| 2150 | i = 1; |
| 2151 | if (j == 0) |
| 2152 | j = 1; |
| 2153 | |
| 2154 | /* Pop the string. */ |
| 2155 | lua_pop(L, 1); |
| 2156 | |
| 2157 | /* Update the buffer length. */ |
| 2158 | buf += i - 1; |
| 2159 | buf_len = j - i + 1; |
| 2160 | lua_pushlstring(L, buf, buf_len); |
| 2161 | |
| 2162 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2163 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2164 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2165 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2166 | } |
| 2167 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2168 | #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] | 2169 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2170 | { |
| 2171 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2172 | int ret; |
| 2173 | int len; |
| 2174 | char *p; |
| 2175 | |
| 2176 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2177 | if (ret <= 0) { |
| 2178 | lua_pushnil(L); |
| 2179 | return 1; |
| 2180 | } |
| 2181 | |
| 2182 | if (ret == AF_UNIX) { |
| 2183 | lua_pushstring(L, buffer+1); |
| 2184 | return 1; |
| 2185 | } |
| 2186 | else if (ret == AF_INET6) { |
| 2187 | buffer[0] = '['; |
| 2188 | len = strlen(buffer); |
| 2189 | buffer[len] = ']'; |
| 2190 | len++; |
| 2191 | buffer[len] = ':'; |
| 2192 | len++; |
| 2193 | p = buffer; |
| 2194 | } |
| 2195 | else if (ret == AF_INET) { |
| 2196 | p = buffer + 1; |
| 2197 | len = strlen(p); |
| 2198 | p[len] = ':'; |
| 2199 | len++; |
| 2200 | } |
| 2201 | else { |
| 2202 | lua_pushnil(L); |
| 2203 | return 1; |
| 2204 | } |
| 2205 | |
| 2206 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2207 | lua_pushnil(L); |
| 2208 | return 1; |
| 2209 | } |
| 2210 | |
| 2211 | lua_pushstring(L, p); |
| 2212 | return 1; |
| 2213 | } |
| 2214 | |
| 2215 | /* Returns information about the peer of the connection. */ |
| 2216 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2217 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2218 | struct hlua_socket *socket; |
| 2219 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2220 | struct xref *peer; |
| 2221 | struct appctx *appctx; |
| 2222 | struct stream_interface *si; |
| 2223 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2224 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2225 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2226 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2227 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2228 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2229 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2230 | /* Check if we run on the same thread than the xreator thread. |
| 2231 | * We cannot access to the socket if the thread is different. |
| 2232 | */ |
| 2233 | if (socket->tid != tid) |
| 2234 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2235 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2236 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2237 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2238 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2239 | lua_pushnil(L); |
| 2240 | return 1; |
| 2241 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2242 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2243 | si = appctx->owner; |
| 2244 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2245 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2246 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2247 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2248 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2249 | lua_pushnil(L); |
| 2250 | return 1; |
| 2251 | } |
| 2252 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2253 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2254 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2255 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2256 | lua_pushnil(L); |
| 2257 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2258 | } |
| 2259 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2260 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2261 | xref_unlock(&socket->xref, peer); |
| 2262 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | /* Returns information about my connection side. */ |
| 2266 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2267 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2268 | struct hlua_socket *socket; |
| 2269 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2270 | struct appctx *appctx; |
| 2271 | struct xref *peer; |
| 2272 | struct stream_interface *si; |
| 2273 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2274 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2275 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2276 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2277 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2278 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2279 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2280 | /* Check if we run on the same thread than the xreator thread. |
| 2281 | * We cannot access to the socket if the thread is different. |
| 2282 | */ |
| 2283 | if (socket->tid != tid) |
| 2284 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2285 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2286 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2287 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2288 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2289 | lua_pushnil(L); |
| 2290 | return 1; |
| 2291 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2292 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2293 | si = appctx->owner; |
| 2294 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2295 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2296 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2298 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2299 | lua_pushnil(L); |
| 2300 | return 1; |
| 2301 | } |
| 2302 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2303 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2304 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2305 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2306 | lua_pushnil(L); |
| 2307 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2308 | } |
| 2309 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2310 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2311 | xref_unlock(&socket->xref, peer); |
| 2312 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2316 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2317 | .obj_type = OBJ_TYPE_APPLET, |
| 2318 | .name = "<LUA_TCP>", |
| 2319 | .fct = hlua_socket_handler, |
| 2320 | .release = hlua_socket_release, |
| 2321 | }; |
| 2322 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2323 | __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] | 2324 | { |
| 2325 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2326 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2327 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2328 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2329 | struct stream_interface *si; |
| 2330 | struct stream *s; |
| 2331 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2332 | /* Check if we run on the same thread than the xreator thread. |
| 2333 | * We cannot access to the socket if the thread is different. |
| 2334 | */ |
| 2335 | if (socket->tid != tid) |
| 2336 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2337 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2338 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2339 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2340 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2341 | lua_pushnil(L); |
| 2342 | lua_pushstring(L, "Can't connect"); |
| 2343 | return 2; |
| 2344 | } |
| 2345 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2346 | si = appctx->owner; |
| 2347 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2348 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2349 | /* Check if we run on the same thread than the xreator thread. |
| 2350 | * We cannot access to the socket if the thread is different. |
| 2351 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2352 | if (socket->tid != tid) { |
| 2353 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2354 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2355 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2356 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2357 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2358 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2359 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2360 | lua_pushnil(L); |
| 2361 | lua_pushstring(L, "Can't connect"); |
| 2362 | return 2; |
| 2363 | } |
| 2364 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2365 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2366 | |
| 2367 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2368 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2369 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2370 | lua_pushinteger(L, 1); |
| 2371 | return 1; |
| 2372 | } |
| 2373 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2374 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2375 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2376 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2377 | } |
| 2378 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2379 | 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] | 2380 | return 0; |
| 2381 | } |
| 2382 | |
| 2383 | /* This function fail or initite the connection. */ |
| 2384 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2385 | { |
| 2386 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2387 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2388 | const char *ip; |
| 2389 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2390 | struct hlua *hlua; |
| 2391 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2392 | int low, high; |
| 2393 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2394 | struct xref *peer; |
| 2395 | struct stream_interface *si; |
| 2396 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2397 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2398 | if (lua_gettop(L) < 2) |
| 2399 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2400 | |
| 2401 | /* Get args. */ |
| 2402 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2403 | |
| 2404 | /* Check if we run on the same thread than the xreator thread. |
| 2405 | * We cannot access to the socket if the thread is different. |
| 2406 | */ |
| 2407 | if (socket->tid != tid) |
| 2408 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2409 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2410 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2411 | if (lua_gettop(L) >= 3) { |
| 2412 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2413 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2414 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2415 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2416 | * that are not enclosed within square brackets. |
| 2417 | */ |
| 2418 | if (port > 0) { |
| 2419 | luaL_buffinit(L, &b); |
| 2420 | luaL_addstring(&b, ip); |
| 2421 | luaL_addchar(&b, ':'); |
| 2422 | luaL_pushresult(&b); |
| 2423 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2424 | } |
| 2425 | } |
| 2426 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2427 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2428 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2429 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2430 | lua_pushnil(L); |
| 2431 | return 1; |
| 2432 | } |
| 2433 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2434 | si = appctx->owner; |
| 2435 | s = si_strm(si); |
| 2436 | |
| 2437 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2438 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2439 | if (!conn) { |
| 2440 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2441 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2442 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2443 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2444 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2445 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2446 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2447 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2448 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2449 | if (!addr) { |
| 2450 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2451 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2452 | } |
| 2453 | if (low != high) { |
| 2454 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2455 | WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2456 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2457 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2458 | |
| 2459 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2460 | if (low == 0) { |
| 2461 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2462 | if (port == -1) { |
| 2463 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2464 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2465 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2466 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2467 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2468 | if (port == -1) { |
| 2469 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2470 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2471 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2472 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2473 | } |
| 2474 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2475 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2476 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2477 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2478 | |
| 2479 | /* inform the stream that we want to be notified whenever the |
| 2480 | * connection completes. |
| 2481 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2482 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2483 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2484 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2485 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2486 | hlua->flags |= HLUA_MUST_GC; |
| 2487 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2488 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2489 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2490 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2491 | } |
| 2492 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2493 | |
| 2494 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2495 | /* Return yield waiting for connection. */ |
| 2496 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2497 | 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] | 2498 | |
| 2499 | return 0; |
| 2500 | } |
| 2501 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2502 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2503 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2504 | { |
| 2505 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2506 | struct xref *peer; |
| 2507 | struct appctx *appctx; |
| 2508 | struct stream_interface *si; |
| 2509 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2510 | |
| 2511 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2512 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2513 | |
| 2514 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2515 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2516 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2517 | lua_pushnil(L); |
| 2518 | return 1; |
| 2519 | } |
| 2520 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2521 | si = appctx->owner; |
| 2522 | s = si_strm(si); |
| 2523 | |
| 2524 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2525 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2526 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2527 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2528 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2529 | |
| 2530 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2531 | { |
| 2532 | return 0; |
| 2533 | } |
| 2534 | |
| 2535 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2536 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2537 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2538 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2539 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2540 | struct xref *peer; |
| 2541 | struct appctx *appctx; |
| 2542 | struct stream_interface *si; |
| 2543 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2544 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2545 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2546 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2547 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2548 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2549 | /* convert the timeout to millis */ |
| 2550 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2551 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2552 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2553 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2554 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2555 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2556 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2557 | 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] | 2558 | |
| 2559 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2560 | if (tmout == 0) |
| 2561 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2562 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2563 | /* Check if we run on the same thread than the xreator thread. |
| 2564 | * We cannot access to the socket if the thread is different. |
| 2565 | */ |
| 2566 | if (socket->tid != tid) |
| 2567 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2568 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2569 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2570 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2571 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2572 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2573 | WILL_LJMP(lua_error(L)); |
| 2574 | return 0; |
| 2575 | } |
| 2576 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2577 | si = appctx->owner; |
| 2578 | s = si_strm(si); |
| 2579 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2580 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2581 | s->req.rto = tmout; |
| 2582 | s->req.wto = tmout; |
| 2583 | s->res.rto = tmout; |
| 2584 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2585 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2586 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2587 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2588 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2589 | |
| 2590 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2591 | task_queue(s->task); |
| 2592 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2593 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2594 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2595 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2596 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2600 | { |
| 2601 | struct hlua_socket *socket; |
| 2602 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2603 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2604 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2605 | |
| 2606 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2607 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2608 | hlua_pusherror(L, "socket: full stack"); |
| 2609 | goto out_fail_conf; |
| 2610 | } |
| 2611 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2612 | /* Create the object: obj[0] = userdata. */ |
| 2613 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2614 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2615 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2616 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2617 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2618 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2619 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2620 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2621 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2622 | goto out_fail_conf; |
| 2623 | } |
| 2624 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2625 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2626 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2627 | lua_setmetatable(L, -2); |
| 2628 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2629 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2630 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2631 | if (!appctx) { |
| 2632 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2633 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2634 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2635 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2636 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2637 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2638 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2639 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2640 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2641 | /* Now create a session, task and stream for this applet */ |
| 2642 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2643 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2644 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2645 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2646 | } |
| 2647 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2648 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2649 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2650 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2651 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2652 | } |
| 2653 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2654 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2655 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2656 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2657 | /* Configure "right" stream interface. this "si" is used to connect |
| 2658 | * and retrieve data from the server. The connection is initialized |
| 2659 | * with the "struct server". |
| 2660 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2661 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2662 | |
| 2663 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2664 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2665 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2666 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2667 | return 1; |
| 2668 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2669 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2670 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2671 | out_fail_sess: |
| 2672 | appctx_free(appctx); |
| 2673 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2674 | WILL_LJMP(lua_error(L)); |
| 2675 | return 0; |
| 2676 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2677 | |
| 2678 | /* |
| 2679 | * |
| 2680 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2681 | * Class Channel |
| 2682 | * |
| 2683 | * |
| 2684 | */ |
| 2685 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2686 | /* 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] | 2687 | * inconsistent, so reset the parser and call it again. Warning, this |
| 2688 | * action does not revalidate the request and does not send a 400 if the modified |
| 2689 | * request is not valid. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2690 | * |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2691 | * This function never fails. The direction is set using dir, which equals |
| 2692 | * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2693 | */ |
| 2694 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2695 | { |
| 2696 | /* Protocol HTTP. */ |
| 2697 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2698 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2699 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2700 | http_txn_reset_req(stream->txn); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2701 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2702 | http_txn_reset_res(stream->txn); |
| 2703 | |
| 2704 | if (stream->txn->hdr_idx.v) |
| 2705 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2706 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2707 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2708 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2709 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2710 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2711 | } |
| 2712 | } |
| 2713 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2714 | /* This function is called before the Lua execution. It stores |
| 2715 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2716 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2717 | 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] | 2718 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2719 | c->mode = stream->be->mode; |
| 2720 | switch (c->mode) { |
| 2721 | case PR_MODE_HTTP: |
| 2722 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2723 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2724 | c->data.http.state = stream->txn->req.msg_state; |
| 2725 | else |
| 2726 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2727 | break; |
| 2728 | default: |
| 2729 | break; |
| 2730 | } |
| 2731 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2732 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2733 | /* This function is called after the Lua execution. it |
| 2734 | * returns true if the parser state is consistent, otherwise, |
| 2735 | * it return false. |
| 2736 | * |
| 2737 | * In HTTP mode, the parser state must be in the same state |
| 2738 | * or greater when we exit the function. Even if we do a |
| 2739 | * control yield. This prevent to break the HTTP message |
| 2740 | * from the Lua code. |
| 2741 | */ |
| 2742 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2743 | { |
| 2744 | if (c->mode != stream->be->mode) |
| 2745 | return 0; |
| 2746 | |
| 2747 | switch (c->mode) { |
| 2748 | case PR_MODE_HTTP: |
| 2749 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2750 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2751 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2752 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2753 | else |
| 2754 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2755 | default: |
| 2756 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2757 | } |
| 2758 | return 1; |
| 2759 | } |
| 2760 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2761 | /* Returns the struct hlua_channel join to the class channel in the |
| 2762 | * stack entry "ud" or throws an argument error. |
| 2763 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2764 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2765 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2766 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2767 | } |
| 2768 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2769 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2770 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2771 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2772 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2773 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2774 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2775 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2776 | return 0; |
| 2777 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2778 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2779 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2780 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2781 | |
| 2782 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2783 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2784 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2785 | return 1; |
| 2786 | } |
| 2787 | |
| 2788 | /* Duplicate all the data present in the input channel and put it |
| 2789 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2790 | * the stack if the channel is closed and all the data are consumed, |
| 2791 | * returns 0 if no data are available, otherwise it returns the length |
| 2792 | * of the builded string. |
| 2793 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2794 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2795 | { |
| 2796 | char *blk1; |
| 2797 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2798 | size_t len1; |
| 2799 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2800 | int ret; |
| 2801 | luaL_Buffer b; |
| 2802 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2803 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2804 | if (unlikely(ret == 0)) |
| 2805 | return 0; |
| 2806 | |
| 2807 | if (unlikely(ret < 0)) { |
| 2808 | lua_pushnil(L); |
| 2809 | return -1; |
| 2810 | } |
| 2811 | |
| 2812 | luaL_buffinit(L, &b); |
| 2813 | luaL_addlstring(&b, blk1, len1); |
| 2814 | if (unlikely(ret == 2)) |
| 2815 | luaL_addlstring(&b, blk2, len2); |
| 2816 | luaL_pushresult(&b); |
| 2817 | |
| 2818 | if (unlikely(ret == 2)) |
| 2819 | return len1 + len2; |
| 2820 | return len1; |
| 2821 | } |
| 2822 | |
| 2823 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2824 | * a yield. This function keep the data in the buffer. |
| 2825 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2826 | __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] | 2827 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2828 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2829 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2830 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2831 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2832 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2833 | 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] | 2834 | return 1; |
| 2835 | } |
| 2836 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2837 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2838 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2839 | { |
| 2840 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2841 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2842 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2843 | } |
| 2844 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2845 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2846 | * a yield. This function consumes the data in the buffer. It returns |
| 2847 | * a string containing the data or a nil pointer if no data are available |
| 2848 | * and the channel is closed. |
| 2849 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2850 | __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] | 2851 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2852 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2853 | int ret; |
| 2854 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2855 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2856 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2857 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2858 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2859 | 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] | 2860 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2861 | if (unlikely(ret == -1)) |
| 2862 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2863 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2864 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2865 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2866 | return 1; |
| 2867 | } |
| 2868 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2869 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2870 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2871 | { |
| 2872 | MAY_LJMP(check_args(L, 1, "get")); |
| 2873 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2874 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2875 | } |
| 2876 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2877 | /* This functions consumes and returns one line. If the channel is closed, |
| 2878 | * 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] | 2879 | * 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] | 2880 | * value. |
| 2881 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2882 | __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] | 2883 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2884 | char *blk1; |
| 2885 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2886 | size_t len1; |
| 2887 | size_t len2; |
| 2888 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2889 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2890 | int ret; |
| 2891 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2892 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2893 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2894 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2895 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2896 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2897 | 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] | 2898 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2899 | if (ret == -1) { |
| 2900 | lua_pushnil(L); |
| 2901 | return 1; |
| 2902 | } |
| 2903 | |
| 2904 | luaL_buffinit(L, &b); |
| 2905 | luaL_addlstring(&b, blk1, len1); |
| 2906 | len = len1; |
| 2907 | if (unlikely(ret == 2)) { |
| 2908 | luaL_addlstring(&b, blk2, len2); |
| 2909 | len += len2; |
| 2910 | } |
| 2911 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2912 | 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] | 2913 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2914 | return 1; |
| 2915 | } |
| 2916 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2917 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2918 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2919 | { |
| 2920 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2921 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2922 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2923 | } |
| 2924 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2925 | /* This function takes a string as input, and append it at the |
| 2926 | * input side of channel. If the data is too big, but a space |
| 2927 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2928 | * yields. If the data is bigger than the buffer, or if the |
| 2929 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2930 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2931 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2932 | __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] | 2933 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2934 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2935 | size_t len; |
| 2936 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2937 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2938 | int ret; |
| 2939 | int max; |
| 2940 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2941 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2942 | * the request buffer if its not required. |
| 2943 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2944 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2945 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2946 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2947 | } |
| 2948 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2949 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2950 | if (max > len - l) |
| 2951 | max = len - l; |
| 2952 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2953 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2954 | if (ret == -2 || ret == -3) { |
| 2955 | lua_pushinteger(L, -1); |
| 2956 | return 1; |
| 2957 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2958 | if (ret == -1) { |
| 2959 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2960 | 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] | 2961 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2962 | l += ret; |
| 2963 | lua_pop(L, 1); |
| 2964 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2965 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2966 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2967 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2968 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2969 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2970 | * in this case, we cannot add more data, so we cannot yield, |
| 2971 | * we return the amount of copyied data. |
| 2972 | */ |
| 2973 | return 1; |
| 2974 | } |
| 2975 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2976 | 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] | 2977 | return 1; |
| 2978 | } |
| 2979 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2980 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2981 | * 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] | 2982 | * buffer size is too little for the data. |
| 2983 | */ |
| 2984 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2985 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2986 | size_t len; |
| 2987 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2988 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2989 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2990 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2991 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2992 | lua_pushinteger(L, 0); |
| 2993 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2994 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2995 | } |
| 2996 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2997 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2998 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2999 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3000 | * or -1 if the channel is closed or if the buffer size is too |
| 3001 | * little for the data. |
| 3002 | */ |
| 3003 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3004 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3005 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3006 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3007 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3008 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3009 | lua_pushinteger(L, 0); |
| 3010 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3011 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3012 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3013 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3014 | } |
| 3015 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3016 | /* Append data in the output side of the buffer. This data is immediately |
| 3017 | * sent. The function returns the amount of data written. If the buffer |
| 3018 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3019 | * if the channel is closed. |
| 3020 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3021 | __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] | 3022 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3023 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3024 | size_t len; |
| 3025 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3026 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3027 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3028 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3029 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3030 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3031 | lua_pushinteger(L, -1); |
| 3032 | return 1; |
| 3033 | } |
| 3034 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3035 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3036 | * the request buffer if its not required. |
| 3037 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3038 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3039 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3040 | 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] | 3041 | } |
| 3042 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3043 | /* The written data will be immediately sent, so we can check |
| 3044 | * the available space without taking in account the reserve. |
| 3045 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3046 | * data, because the buffer will be flushed. |
| 3047 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3048 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3049 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3050 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3051 | * in this case, we cannot add more data, so we cannot yield, |
| 3052 | * we return the amount of copyied data. |
| 3053 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3054 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3055 | return 1; |
| 3056 | |
| 3057 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3058 | if (max > len - l) |
| 3059 | max = len - l; |
| 3060 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3061 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3062 | * detects a non contiguous buffer and realign it. |
| 3063 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3064 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3065 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3066 | |
| 3067 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3068 | 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] | 3069 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3070 | /* buffer replace considers that the input part is filled. |
| 3071 | * so, I must forward these new data in the output part. |
| 3072 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3073 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3074 | |
| 3075 | l += max; |
| 3076 | lua_pop(L, 1); |
| 3077 | lua_pushinteger(L, l); |
| 3078 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3079 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3080 | * in this case, we cannot add more data, so we cannot yield, |
| 3081 | * we return the amount of copyied data. |
| 3082 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3083 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3084 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3085 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3086 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3087 | if (l < len) { |
| 3088 | /* If we are waiting for space in the response buffer, we |
| 3089 | * must set the flag WAKERESWR. This flag required the task |
| 3090 | * wake up if any activity is detected on the response buffer. |
| 3091 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3092 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3093 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3094 | else |
| 3095 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3096 | 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] | 3097 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3098 | |
| 3099 | return 1; |
| 3100 | } |
| 3101 | |
| 3102 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3103 | * yield the LUA process, and resume it without checking the |
| 3104 | * input arguments. |
| 3105 | */ |
| 3106 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3107 | { |
| 3108 | MAY_LJMP(check_args(L, 2, "send")); |
| 3109 | lua_pushinteger(L, 0); |
| 3110 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3111 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3112 | } |
| 3113 | |
| 3114 | /* This function forward and amount of butes. The data pass from |
| 3115 | * the input side of the buffer to the output side, and can be |
| 3116 | * forwarded. This function never fails. |
| 3117 | * |
| 3118 | * The Lua function takes an amount of bytes to be forwarded in |
| 3119 | * imput. It returns the number of bytes forwarded. |
| 3120 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3121 | __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] | 3122 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3123 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3124 | int len; |
| 3125 | int l; |
| 3126 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3127 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3128 | |
| 3129 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3130 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3131 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3132 | |
| 3133 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3134 | if (max > ci_data(chn)) |
| 3135 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3136 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3137 | l += max; |
| 3138 | |
| 3139 | lua_pop(L, 1); |
| 3140 | lua_pushinteger(L, l); |
| 3141 | |
| 3142 | /* Check if it miss bytes to forward. */ |
| 3143 | if (l < len) { |
| 3144 | /* The the input channel or the output channel are closed, we |
| 3145 | * must return the amount of data forwarded. |
| 3146 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3147 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3148 | return 1; |
| 3149 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3150 | /* If we are waiting for space data in the response buffer, we |
| 3151 | * must set the flag WAKERESWR. This flag required the task |
| 3152 | * wake up if any activity is detected on the response buffer. |
| 3153 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3154 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3155 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3156 | else |
| 3157 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3158 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3159 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3160 | 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] | 3161 | } |
| 3162 | |
| 3163 | return 1; |
| 3164 | } |
| 3165 | |
| 3166 | /* Just check the input and prepare the stack for the previous |
| 3167 | * function "hlua_channel_forward_yield" |
| 3168 | */ |
| 3169 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3170 | { |
| 3171 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3172 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3173 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3174 | |
| 3175 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3176 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3177 | } |
| 3178 | |
| 3179 | /* Just returns the number of bytes available in the input |
| 3180 | * side of the buffer. This function never fails. |
| 3181 | */ |
| 3182 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3183 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3184 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3185 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3186 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3187 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3188 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3189 | return 1; |
| 3190 | } |
| 3191 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3192 | /* Returns true if the channel is full. */ |
| 3193 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3194 | { |
| 3195 | struct channel *chn; |
| 3196 | int rem; |
| 3197 | |
| 3198 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3199 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3200 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3201 | rem = b_room(&chn->buf); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3202 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3203 | |
| 3204 | lua_pushboolean(L, rem <= 0); |
| 3205 | return 1; |
| 3206 | } |
| 3207 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3208 | /* Just returns the number of bytes available in the output |
| 3209 | * side of the buffer. This function never fails. |
| 3210 | */ |
| 3211 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3212 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3213 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3214 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3215 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3216 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3217 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3218 | return 1; |
| 3219 | } |
| 3220 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3221 | /* |
| 3222 | * |
| 3223 | * |
| 3224 | * Class Fetches |
| 3225 | * |
| 3226 | * |
| 3227 | */ |
| 3228 | |
| 3229 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3230 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3231 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3232 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3233 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3234 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3235 | } |
| 3236 | |
| 3237 | /* This function creates and push in the stack a fetch object according |
| 3238 | * with a current TXN. |
| 3239 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3240 | 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] | 3241 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3242 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3243 | |
| 3244 | /* Check stack size. */ |
| 3245 | if (!lua_checkstack(L, 3)) |
| 3246 | return 0; |
| 3247 | |
| 3248 | /* Create the object: obj[0] = userdata. |
| 3249 | * Note that the base of the Fetches object is the |
| 3250 | * transaction object. |
| 3251 | */ |
| 3252 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3253 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3254 | lua_rawseti(L, -2, 0); |
| 3255 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3256 | hsmp->s = txn->s; |
| 3257 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3258 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3259 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3260 | |
| 3261 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3262 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3263 | lua_setmetatable(L, -2); |
| 3264 | |
| 3265 | return 1; |
| 3266 | } |
| 3267 | |
| 3268 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3269 | * It uses closure argument to store the associated sample-fetch. It |
| 3270 | * returns only one argument or throws an error. An error is thrown |
| 3271 | * only if an error is encountered during the argument parsing. If |
| 3272 | * the "sample-fetch" function fails, nil is returned. |
| 3273 | */ |
| 3274 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3275 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3276 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3277 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3278 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3279 | int i; |
| 3280 | struct sample smp; |
| 3281 | |
| 3282 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3283 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3284 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3285 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3286 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3287 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3288 | /* Check execution authorization. */ |
| 3289 | if (f->use & SMP_USE_HTTP_ANY && |
| 3290 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3291 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3292 | "is not available in Lua services", f->kw); |
| 3293 | WILL_LJMP(lua_error(L)); |
| 3294 | } |
| 3295 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3296 | /* Get extra arguments. */ |
| 3297 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3298 | if (i >= ARGM_NBARGS) |
| 3299 | break; |
| 3300 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3301 | } |
| 3302 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3303 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3304 | |
| 3305 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3306 | 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] | 3307 | |
| 3308 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3309 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3310 | lua_pushfstring(L, "error in arguments"); |
| 3311 | WILL_LJMP(lua_error(L)); |
| 3312 | } |
| 3313 | |
| 3314 | /* Initialise the sample. */ |
| 3315 | memset(&smp, 0, sizeof(smp)); |
| 3316 | |
| 3317 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3318 | 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] | 3319 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3320 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3321 | lua_pushstring(L, ""); |
| 3322 | else |
| 3323 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3324 | return 1; |
| 3325 | } |
| 3326 | |
| 3327 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3328 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3329 | hlua_smp2lua_str(L, &smp); |
| 3330 | else |
| 3331 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3332 | return 1; |
| 3333 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3334 | |
| 3335 | /* |
| 3336 | * |
| 3337 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3338 | * Class Converters |
| 3339 | * |
| 3340 | * |
| 3341 | */ |
| 3342 | |
| 3343 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3344 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3345 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3346 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3347 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3348 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3349 | } |
| 3350 | |
| 3351 | /* This function creates and push in the stack a Converters object |
| 3352 | * according with a current TXN. |
| 3353 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3354 | 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] | 3355 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3356 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3357 | |
| 3358 | /* Check stack size. */ |
| 3359 | if (!lua_checkstack(L, 3)) |
| 3360 | return 0; |
| 3361 | |
| 3362 | /* Create the object: obj[0] = userdata. |
| 3363 | * Note that the base of the Converters object is the |
| 3364 | * same than the TXN object. |
| 3365 | */ |
| 3366 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3367 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3368 | lua_rawseti(L, -2, 0); |
| 3369 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3370 | hsmp->s = txn->s; |
| 3371 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3372 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3373 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3374 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3375 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3376 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3377 | lua_setmetatable(L, -2); |
| 3378 | |
| 3379 | return 1; |
| 3380 | } |
| 3381 | |
| 3382 | /* This function is an LUA binding. It is called with each converter. |
| 3383 | * It uses closure argument to store the associated converter. It |
| 3384 | * returns only one argument or throws an error. An error is thrown |
| 3385 | * only if an error is encountered during the argument parsing. If |
| 3386 | * the converter function function fails, nil is returned. |
| 3387 | */ |
| 3388 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3389 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3390 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3391 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3392 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3393 | int i; |
| 3394 | struct sample smp; |
| 3395 | |
| 3396 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3397 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3398 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3399 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3400 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3401 | |
| 3402 | /* Get extra arguments. */ |
| 3403 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3404 | if (i >= ARGM_NBARGS) |
| 3405 | break; |
| 3406 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3407 | } |
| 3408 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3409 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3410 | |
| 3411 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3412 | 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] | 3413 | |
| 3414 | /* Run the special args checker. */ |
| 3415 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3416 | hlua_pusherror(L, "error in arguments"); |
| 3417 | WILL_LJMP(lua_error(L)); |
| 3418 | } |
| 3419 | |
| 3420 | /* Initialise the sample. */ |
| 3421 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3422 | hlua_pusherror(L, "error in the input argument"); |
| 3423 | WILL_LJMP(lua_error(L)); |
| 3424 | } |
| 3425 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3426 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3427 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3428 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3429 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3430 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3431 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3432 | WILL_LJMP(lua_error(L)); |
| 3433 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3434 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3435 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3436 | hlua_pusherror(L, "error during the input argument casting"); |
| 3437 | WILL_LJMP(lua_error(L)); |
| 3438 | } |
| 3439 | |
| 3440 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3441 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3442 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3443 | lua_pushstring(L, ""); |
| 3444 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3445 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3446 | return 1; |
| 3447 | } |
| 3448 | |
| 3449 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3450 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3451 | hlua_smp2lua_str(L, &smp); |
| 3452 | else |
| 3453 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3454 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3455 | } |
| 3456 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3457 | /* |
| 3458 | * |
| 3459 | * |
| 3460 | * Class AppletTCP |
| 3461 | * |
| 3462 | * |
| 3463 | */ |
| 3464 | |
| 3465 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3466 | * a class stream, otherwise it throws an error. |
| 3467 | */ |
| 3468 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3469 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3470 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3471 | } |
| 3472 | |
| 3473 | /* This function creates and push in the stack an Applet object |
| 3474 | * according with a current TXN. |
| 3475 | */ |
| 3476 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3477 | { |
| 3478 | struct hlua_appctx *appctx; |
| 3479 | struct stream_interface *si = ctx->owner; |
| 3480 | struct stream *s = si_strm(si); |
| 3481 | struct proxy *p = s->be; |
| 3482 | |
| 3483 | /* Check stack size. */ |
| 3484 | if (!lua_checkstack(L, 3)) |
| 3485 | return 0; |
| 3486 | |
| 3487 | /* Create the object: obj[0] = userdata. |
| 3488 | * Note that the base of the Converters object is the |
| 3489 | * same than the TXN object. |
| 3490 | */ |
| 3491 | lua_newtable(L); |
| 3492 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3493 | lua_rawseti(L, -2, 0); |
| 3494 | appctx->appctx = ctx; |
| 3495 | appctx->htxn.s = s; |
| 3496 | appctx->htxn.p = p; |
| 3497 | |
| 3498 | /* Create the "f" field that contains a list of fetches. */ |
| 3499 | lua_pushstring(L, "f"); |
| 3500 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3501 | return 0; |
| 3502 | lua_settable(L, -3); |
| 3503 | |
| 3504 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3505 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3506 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3507 | return 0; |
| 3508 | lua_settable(L, -3); |
| 3509 | |
| 3510 | /* Create the "c" field that contains a list of converters. */ |
| 3511 | lua_pushstring(L, "c"); |
| 3512 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3513 | return 0; |
| 3514 | lua_settable(L, -3); |
| 3515 | |
| 3516 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3517 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3518 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3519 | return 0; |
| 3520 | lua_settable(L, -3); |
| 3521 | |
| 3522 | /* Pop a class stream metatable and affect it to the table. */ |
| 3523 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3524 | lua_setmetatable(L, -2); |
| 3525 | |
| 3526 | return 1; |
| 3527 | } |
| 3528 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3529 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3530 | { |
| 3531 | struct hlua_appctx *appctx; |
| 3532 | struct stream *s; |
| 3533 | const char *name; |
| 3534 | size_t len; |
| 3535 | struct sample smp; |
| 3536 | |
| 3537 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3538 | |
| 3539 | /* It is useles to retrieve the stream, but this function |
| 3540 | * runs only in a stream context. |
| 3541 | */ |
| 3542 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3543 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3544 | s = appctx->htxn.s; |
| 3545 | |
| 3546 | /* Converts the third argument in a sample. */ |
| 3547 | hlua_lua2smp(L, 3, &smp); |
| 3548 | |
| 3549 | /* Store the sample in a variable. */ |
| 3550 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3551 | vars_set_by_name(name, len, &smp); |
| 3552 | return 0; |
| 3553 | } |
| 3554 | |
| 3555 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3556 | { |
| 3557 | struct hlua_appctx *appctx; |
| 3558 | struct stream *s; |
| 3559 | const char *name; |
| 3560 | size_t len; |
| 3561 | struct sample smp; |
| 3562 | |
| 3563 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3564 | |
| 3565 | /* It is useles to retrieve the stream, but this function |
| 3566 | * runs only in a stream context. |
| 3567 | */ |
| 3568 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3569 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3570 | s = appctx->htxn.s; |
| 3571 | |
| 3572 | /* Unset the variable. */ |
| 3573 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3574 | vars_unset_by_name(name, len, &smp); |
| 3575 | return 0; |
| 3576 | } |
| 3577 | |
| 3578 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3579 | { |
| 3580 | struct hlua_appctx *appctx; |
| 3581 | struct stream *s; |
| 3582 | const char *name; |
| 3583 | size_t len; |
| 3584 | struct sample smp; |
| 3585 | |
| 3586 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3587 | |
| 3588 | /* It is useles to retrieve the stream, but this function |
| 3589 | * runs only in a stream context. |
| 3590 | */ |
| 3591 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3592 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3593 | s = appctx->htxn.s; |
| 3594 | |
| 3595 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3596 | if (!vars_get_by_name(name, len, &smp)) { |
| 3597 | lua_pushnil(L); |
| 3598 | return 1; |
| 3599 | } |
| 3600 | |
| 3601 | return hlua_smp2lua(L, &smp); |
| 3602 | } |
| 3603 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3604 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3605 | { |
| 3606 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3607 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3608 | struct hlua *hlua; |
| 3609 | |
| 3610 | /* 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] | 3611 | if (!s->hlua) |
| 3612 | return 0; |
| 3613 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3614 | |
| 3615 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3616 | |
| 3617 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3618 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3619 | |
| 3620 | /* Get and store new value. */ |
| 3621 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3622 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3623 | |
| 3624 | return 0; |
| 3625 | } |
| 3626 | |
| 3627 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3628 | { |
| 3629 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3630 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3631 | struct hlua *hlua; |
| 3632 | |
| 3633 | /* 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] | 3634 | if (!s->hlua) { |
| 3635 | lua_pushnil(L); |
| 3636 | return 1; |
| 3637 | } |
| 3638 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3639 | |
| 3640 | /* Push configuration index in the stack. */ |
| 3641 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3642 | |
| 3643 | return 1; |
| 3644 | } |
| 3645 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3646 | /* If expected data not yet available, it returns a yield. This function |
| 3647 | * consumes the data in the buffer. It returns a string containing the |
| 3648 | * data. This string can be empty. |
| 3649 | */ |
| 3650 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3651 | { |
| 3652 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3653 | struct stream_interface *si = appctx->appctx->owner; |
| 3654 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3655 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3656 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3657 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3658 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3659 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3660 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3661 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3662 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3663 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3664 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3665 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3666 | 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] | 3667 | } |
| 3668 | |
| 3669 | /* End of data: commit the total strings and return. */ |
| 3670 | if (ret < 0) { |
| 3671 | luaL_pushresult(&appctx->b); |
| 3672 | return 1; |
| 3673 | } |
| 3674 | |
| 3675 | /* Ensure that the block 2 length is usable. */ |
| 3676 | if (ret == 1) |
| 3677 | len2 = 0; |
| 3678 | |
| 3679 | /* dont check the max length read and dont check. */ |
| 3680 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3681 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3682 | |
| 3683 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3684 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3685 | luaL_pushresult(&appctx->b); |
| 3686 | return 1; |
| 3687 | } |
| 3688 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3689 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3690 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3691 | { |
| 3692 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3693 | |
| 3694 | /* Initialise the string catenation. */ |
| 3695 | luaL_buffinit(L, &appctx->b); |
| 3696 | |
| 3697 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3698 | } |
| 3699 | |
| 3700 | /* If expected data not yet available, it returns a yield. This function |
| 3701 | * consumes the data in the buffer. It returns a string containing the |
| 3702 | * data. This string can be empty. |
| 3703 | */ |
| 3704 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3705 | { |
| 3706 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3707 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3708 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3709 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3710 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3711 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3712 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3713 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3714 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3715 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3716 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3717 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3718 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3719 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3720 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3721 | 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] | 3722 | } |
| 3723 | |
| 3724 | /* End of data: commit the total strings and return. */ |
| 3725 | if (ret < 0) { |
| 3726 | luaL_pushresult(&appctx->b); |
| 3727 | return 1; |
| 3728 | } |
| 3729 | |
| 3730 | /* Ensure that the block 2 length is usable. */ |
| 3731 | if (ret == 1) |
| 3732 | len2 = 0; |
| 3733 | |
| 3734 | if (len == -1) { |
| 3735 | |
| 3736 | /* If len == -1, catenate all the data avalaile and |
| 3737 | * yield because we want to get all the data until |
| 3738 | * the end of data stream. |
| 3739 | */ |
| 3740 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3741 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3742 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3743 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3744 | 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] | 3745 | |
| 3746 | } else { |
| 3747 | |
| 3748 | /* Copy the fisrt block caping to the length required. */ |
| 3749 | if (len1 > len) |
| 3750 | len1 = len; |
| 3751 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3752 | len -= len1; |
| 3753 | |
| 3754 | /* Copy the second block. */ |
| 3755 | if (len2 > len) |
| 3756 | len2 = len; |
| 3757 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3758 | len -= len2; |
| 3759 | |
| 3760 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3761 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3762 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3763 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3764 | if (len > 0) { |
| 3765 | lua_pushinteger(L, len); |
| 3766 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3767 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3768 | 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] | 3769 | } |
| 3770 | |
| 3771 | /* return the result. */ |
| 3772 | luaL_pushresult(&appctx->b); |
| 3773 | return 1; |
| 3774 | } |
| 3775 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3776 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3777 | hlua_pusherror(L, "Lua: internal error"); |
| 3778 | WILL_LJMP(lua_error(L)); |
| 3779 | return 0; |
| 3780 | } |
| 3781 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3782 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3783 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3784 | { |
| 3785 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3786 | int len = -1; |
| 3787 | |
| 3788 | if (lua_gettop(L) > 2) |
| 3789 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3790 | if (lua_gettop(L) >= 2) { |
| 3791 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3792 | lua_pop(L, 1); |
| 3793 | } |
| 3794 | |
| 3795 | /* Confirm or set the required length */ |
| 3796 | lua_pushinteger(L, len); |
| 3797 | |
| 3798 | /* Initialise the string catenation. */ |
| 3799 | luaL_buffinit(L, &appctx->b); |
| 3800 | |
| 3801 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3802 | } |
| 3803 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3804 | /* Append data in the output side of the buffer. This data is immediately |
| 3805 | * sent. The function returns the amount of data written. If the buffer |
| 3806 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3807 | * if the channel is closed. |
| 3808 | */ |
| 3809 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3810 | { |
| 3811 | size_t len; |
| 3812 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3813 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3814 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3815 | struct stream_interface *si = appctx->appctx->owner; |
| 3816 | struct channel *chn = si_ic(si); |
| 3817 | int max; |
| 3818 | |
| 3819 | /* Get the max amount of data which can write as input in the channel. */ |
| 3820 | max = channel_recv_max(chn); |
| 3821 | if (max > (len - l)) |
| 3822 | max = len - l; |
| 3823 | |
| 3824 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3825 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3826 | |
| 3827 | /* update counters. */ |
| 3828 | l += max; |
| 3829 | lua_pop(L, 1); |
| 3830 | lua_pushinteger(L, l); |
| 3831 | |
| 3832 | /* If some data is not send, declares the situation to the |
| 3833 | * applet, and returns a yield. |
| 3834 | */ |
| 3835 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3836 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3837 | 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] | 3838 | } |
| 3839 | |
| 3840 | return 1; |
| 3841 | } |
| 3842 | |
| 3843 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3844 | * yield the LUA process, and resume it without checking the |
| 3845 | * input arguments. |
| 3846 | */ |
| 3847 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3848 | { |
| 3849 | MAY_LJMP(check_args(L, 2, "send")); |
| 3850 | lua_pushinteger(L, 0); |
| 3851 | |
| 3852 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3853 | } |
| 3854 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3855 | /* |
| 3856 | * |
| 3857 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3858 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3859 | * |
| 3860 | * |
| 3861 | */ |
| 3862 | |
| 3863 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3864 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3865 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3866 | __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] | 3867 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3868 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3869 | } |
| 3870 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3871 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3872 | * according with a current TXN. |
| 3873 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3874 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3875 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3876 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3877 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3878 | struct stream_interface *si = ctx->owner; |
| 3879 | struct stream *s = si_strm(si); |
| 3880 | struct proxy *px = s->be; |
| 3881 | struct http_txn *txn = s->txn; |
| 3882 | const char *path; |
| 3883 | const char *end; |
| 3884 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3885 | |
| 3886 | /* Check stack size. */ |
| 3887 | if (!lua_checkstack(L, 3)) |
| 3888 | return 0; |
| 3889 | |
| 3890 | /* Create the object: obj[0] = userdata. |
| 3891 | * Note that the base of the Converters object is the |
| 3892 | * same than the TXN object. |
| 3893 | */ |
| 3894 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3895 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3896 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3897 | appctx->appctx = ctx; |
| 3898 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3899 | 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] | 3900 | appctx->htxn.s = s; |
| 3901 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3902 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3903 | /* Create the "f" field that contains a list of fetches. */ |
| 3904 | lua_pushstring(L, "f"); |
| 3905 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3906 | return 0; |
| 3907 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3908 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3909 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3910 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3911 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3912 | return 0; |
| 3913 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3914 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3915 | /* Create the "c" field that contains a list of converters. */ |
| 3916 | lua_pushstring(L, "c"); |
| 3917 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3918 | return 0; |
| 3919 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3920 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3921 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3922 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3923 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3924 | return 0; |
| 3925 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3926 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3927 | /* Stores the request method. */ |
| 3928 | lua_pushstring(L, "method"); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3929 | lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3930 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3931 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3932 | /* Stores the http version. */ |
| 3933 | lua_pushstring(L, "version"); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3934 | 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] | 3935 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3936 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3937 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3938 | * the array on the top of the stack. |
| 3939 | */ |
| 3940 | lua_pushstring(L, "headers"); |
| 3941 | htxn.s = s; |
| 3942 | htxn.p = px; |
| 3943 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3944 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3945 | return 0; |
| 3946 | lua_settable(L, -3); |
| 3947 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3948 | /* Get path and qs */ |
Willy Tarreau | 6b952c8 | 2018-09-10 17:45:34 +0200 | [diff] [blame] | 3949 | path = http_txn_get_path(txn); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3950 | if (path) { |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3951 | 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] | 3952 | p = path; |
| 3953 | while (p < end && *p != '?') |
| 3954 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3955 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3956 | /* Stores the request path. */ |
| 3957 | lua_pushstring(L, "path"); |
| 3958 | lua_pushlstring(L, path, p - path); |
| 3959 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3960 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3961 | /* Stores the query string. */ |
| 3962 | lua_pushstring(L, "qs"); |
| 3963 | if (*p == '?') |
| 3964 | p++; |
| 3965 | lua_pushlstring(L, p, end - p); |
| 3966 | lua_settable(L, -3); |
| 3967 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3968 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3969 | /* Stores the request path. */ |
| 3970 | lua_pushstring(L, "length"); |
| 3971 | lua_pushinteger(L, txn->req.body_len); |
| 3972 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3973 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3974 | /* Create an array of HTTP request headers. */ |
| 3975 | lua_pushstring(L, "headers"); |
| 3976 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3977 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3978 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3979 | /* Create an empty array of HTTP request headers. */ |
| 3980 | lua_pushstring(L, "response"); |
| 3981 | lua_newtable(L); |
| 3982 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3983 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3984 | /* Pop a class stream metatable and affect it to the table. */ |
| 3985 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3986 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3987 | |
| 3988 | return 1; |
| 3989 | } |
| 3990 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3991 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3992 | { |
| 3993 | struct hlua_appctx *appctx; |
| 3994 | struct stream *s; |
| 3995 | const char *name; |
| 3996 | size_t len; |
| 3997 | struct sample smp; |
| 3998 | |
| 3999 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4000 | |
| 4001 | /* It is useles to retrieve the stream, but this function |
| 4002 | * runs only in a stream context. |
| 4003 | */ |
| 4004 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4005 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4006 | s = appctx->htxn.s; |
| 4007 | |
| 4008 | /* Converts the third argument in a sample. */ |
| 4009 | hlua_lua2smp(L, 3, &smp); |
| 4010 | |
| 4011 | /* Store the sample in a variable. */ |
| 4012 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4013 | vars_set_by_name(name, len, &smp); |
| 4014 | return 0; |
| 4015 | } |
| 4016 | |
| 4017 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4018 | { |
| 4019 | struct hlua_appctx *appctx; |
| 4020 | struct stream *s; |
| 4021 | const char *name; |
| 4022 | size_t len; |
| 4023 | struct sample smp; |
| 4024 | |
| 4025 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4026 | |
| 4027 | /* It is useles to retrieve the stream, but this function |
| 4028 | * runs only in a stream context. |
| 4029 | */ |
| 4030 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4031 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4032 | s = appctx->htxn.s; |
| 4033 | |
| 4034 | /* Unset the variable. */ |
| 4035 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4036 | vars_unset_by_name(name, len, &smp); |
| 4037 | return 0; |
| 4038 | } |
| 4039 | |
| 4040 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4041 | { |
| 4042 | struct hlua_appctx *appctx; |
| 4043 | struct stream *s; |
| 4044 | const char *name; |
| 4045 | size_t len; |
| 4046 | struct sample smp; |
| 4047 | |
| 4048 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4049 | |
| 4050 | /* It is useles to retrieve the stream, but this function |
| 4051 | * runs only in a stream context. |
| 4052 | */ |
| 4053 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4054 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4055 | s = appctx->htxn.s; |
| 4056 | |
| 4057 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4058 | if (!vars_get_by_name(name, len, &smp)) { |
| 4059 | lua_pushnil(L); |
| 4060 | return 1; |
| 4061 | } |
| 4062 | |
| 4063 | return hlua_smp2lua(L, &smp); |
| 4064 | } |
| 4065 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4066 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4067 | { |
| 4068 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4069 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4070 | struct hlua *hlua; |
| 4071 | |
| 4072 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4073 | if (!s->hlua) |
| 4074 | return 0; |
| 4075 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4076 | |
| 4077 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4078 | |
| 4079 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4080 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4081 | |
| 4082 | /* Get and store new value. */ |
| 4083 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4084 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4085 | |
| 4086 | return 0; |
| 4087 | } |
| 4088 | |
| 4089 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4090 | { |
| 4091 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4092 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4093 | struct hlua *hlua; |
| 4094 | |
| 4095 | /* 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] | 4096 | if (!s->hlua) { |
| 4097 | lua_pushnil(L); |
| 4098 | return 1; |
| 4099 | } |
| 4100 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4101 | |
| 4102 | /* Push configuration index in the stack. */ |
| 4103 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4104 | |
| 4105 | return 1; |
| 4106 | } |
| 4107 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4108 | /* If expected data not yet available, it returns a yield. This function |
| 4109 | * consumes the data in the buffer. It returns a string containing the |
| 4110 | * data. This string can be empty. |
| 4111 | */ |
| 4112 | __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] | 4113 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4114 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4115 | struct stream_interface *si = appctx->appctx->owner; |
| 4116 | struct channel *chn = si_ic(si); |
| 4117 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4118 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4119 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4120 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4121 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4122 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4123 | /* Maybe we cant send a 100-continue ? */ |
| 4124 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4125 | ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4126 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4127 | * big for the buffers. We cant send anything. So, we ignoring |
| 4128 | * the error, considers that the 100-continue is sent, and try |
| 4129 | * to receive. |
| 4130 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4131 | */ |
| 4132 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4133 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4134 | 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] | 4135 | } |
| 4136 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4137 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4138 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4139 | /* Check for the end of the data. */ |
| 4140 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4141 | luaL_pushresult(&appctx->b); |
| 4142 | return 1; |
| 4143 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4144 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4145 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4146 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4147 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4148 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4149 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4150 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4151 | 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] | 4152 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4153 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4154 | /* End of data: commit the total strings and return. */ |
| 4155 | if (ret < 0) { |
| 4156 | luaL_pushresult(&appctx->b); |
| 4157 | return 1; |
| 4158 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4159 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4160 | /* Ensure that the block 2 length is usable. */ |
| 4161 | if (ret == 1) |
| 4162 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4163 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4164 | /* Copy the fisrt block caping to the length required. */ |
| 4165 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4166 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4167 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4168 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4169 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4170 | /* Copy the second block. */ |
| 4171 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4172 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4173 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4174 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4175 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4176 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4177 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4178 | luaL_pushresult(&appctx->b); |
| 4179 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4180 | } |
| 4181 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4182 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4183 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4184 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4185 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4186 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4187 | /* Initialise the string catenation. */ |
| 4188 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4189 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4190 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4191 | } |
| 4192 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4193 | /* If expected data not yet available, it returns a yield. This function |
| 4194 | * consumes the data in the buffer. It returns a string containing the |
| 4195 | * data. This string can be empty. |
| 4196 | */ |
| 4197 | __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] | 4198 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4199 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4200 | struct stream_interface *si = appctx->appctx->owner; |
| 4201 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4202 | struct channel *chn = si_ic(si); |
| 4203 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4204 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4205 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4206 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4207 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4208 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4209 | /* Maybe we cant send a 100-continue ? */ |
| 4210 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4211 | ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4212 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4213 | * big for the buffers. We cant send anything. So, we ignoring |
| 4214 | * the error, considers that the 100-continue is sent, and try |
| 4215 | * to receive. |
| 4216 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4217 | */ |
| 4218 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4219 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4220 | 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] | 4221 | } |
| 4222 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4223 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4224 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4225 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4226 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4227 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4228 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4229 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4230 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4231 | 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] | 4232 | } |
| 4233 | |
| 4234 | /* End of data: commit the total strings and return. */ |
| 4235 | if (ret < 0) { |
| 4236 | luaL_pushresult(&appctx->b); |
| 4237 | return 1; |
| 4238 | } |
| 4239 | |
| 4240 | /* Ensure that the block 2 length is usable. */ |
| 4241 | if (ret == 1) |
| 4242 | len2 = 0; |
| 4243 | |
| 4244 | /* Copy the fisrt block caping to the length required. */ |
| 4245 | if (len1 > len) |
| 4246 | len1 = len; |
| 4247 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4248 | len -= len1; |
| 4249 | |
| 4250 | /* Copy the second block. */ |
| 4251 | if (len2 > len) |
| 4252 | len2 = len; |
| 4253 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4254 | len -= len2; |
| 4255 | |
| 4256 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4257 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4258 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4259 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4260 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4261 | /* If we are no other data available, yield waiting for new data. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4262 | if (len > 0) { |
| 4263 | lua_pushinteger(L, len); |
| 4264 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4265 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4266 | 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] | 4267 | } |
| 4268 | |
| 4269 | /* return the result. */ |
| 4270 | luaL_pushresult(&appctx->b); |
| 4271 | return 1; |
| 4272 | } |
| 4273 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4274 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4275 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4276 | { |
| 4277 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4278 | int len = -1; |
| 4279 | |
| 4280 | /* Check arguments. */ |
| 4281 | if (lua_gettop(L) > 2) |
| 4282 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4283 | if (lua_gettop(L) >= 2) { |
| 4284 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4285 | lua_pop(L, 1); |
| 4286 | } |
| 4287 | |
| 4288 | /* Check the required length */ |
| 4289 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4290 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4291 | lua_pushinteger(L, len); |
| 4292 | |
| 4293 | /* Initialise the string catenation. */ |
| 4294 | luaL_buffinit(L, &appctx->b); |
| 4295 | |
| 4296 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4297 | } |
| 4298 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4299 | /* Append data in the output side of the buffer. This data is immediately |
| 4300 | * sent. The function returns the amount of data written. If the buffer |
| 4301 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4302 | * if the channel is closed. |
| 4303 | */ |
| 4304 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4305 | { |
| 4306 | size_t len; |
| 4307 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4308 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4309 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4310 | struct stream_interface *si = appctx->appctx->owner; |
| 4311 | struct channel *chn = si_ic(si); |
| 4312 | int max; |
| 4313 | |
| 4314 | /* Get the max amount of data which can write as input in the channel. */ |
| 4315 | max = channel_recv_max(chn); |
| 4316 | if (max > (len - l)) |
| 4317 | max = len - l; |
| 4318 | |
| 4319 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4320 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4321 | |
| 4322 | /* update counters. */ |
| 4323 | l += max; |
| 4324 | lua_pop(L, 1); |
| 4325 | lua_pushinteger(L, l); |
| 4326 | |
| 4327 | /* If some data is not send, declares the situation to the |
| 4328 | * applet, and returns a yield. |
| 4329 | */ |
| 4330 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4331 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4332 | 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] | 4333 | } |
| 4334 | |
| 4335 | return 1; |
| 4336 | } |
| 4337 | |
| 4338 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4339 | * yield the LUA process, and resume it without checking the |
| 4340 | * input arguments. |
| 4341 | */ |
| 4342 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4343 | { |
| 4344 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4345 | size_t len; |
| 4346 | char hex[10]; |
| 4347 | |
| 4348 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4349 | |
| 4350 | /* If transfer encoding chunked is selected, we surround the data |
| 4351 | * by chunk data. |
| 4352 | */ |
| 4353 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4354 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4355 | lua_pushfstring(L, "%s\r\n", hex); |
| 4356 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4357 | lua_pushstring(L, "\r\n"); |
| 4358 | lua_concat(L, 3); |
| 4359 | } |
| 4360 | |
| 4361 | /* This interger is used for followinf the amount of data sent. */ |
| 4362 | lua_pushinteger(L, 0); |
| 4363 | |
| 4364 | /* We want to send some data. Headers must be sent. */ |
| 4365 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4366 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4367 | WILL_LJMP(lua_error(L)); |
| 4368 | } |
| 4369 | |
| 4370 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4371 | } |
| 4372 | |
| 4373 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4374 | { |
| 4375 | const char *name; |
| 4376 | int ret; |
| 4377 | |
| 4378 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4379 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4380 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4381 | |
| 4382 | /* Push in the stack the "response" entry. */ |
| 4383 | ret = lua_getfield(L, 1, "response"); |
| 4384 | if (ret != LUA_TTABLE) { |
| 4385 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4386 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4387 | WILL_LJMP(lua_error(L)); |
| 4388 | } |
| 4389 | |
| 4390 | /* check if the header is already registered if it is not |
| 4391 | * the case, register it. |
| 4392 | */ |
| 4393 | ret = lua_getfield(L, -1, name); |
| 4394 | if (ret == LUA_TNIL) { |
| 4395 | |
| 4396 | /* Entry not found. */ |
| 4397 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4398 | |
| 4399 | /* Insert the new header name in the array in the top of the stack. |
| 4400 | * It left the new array in the top of the stack. |
| 4401 | */ |
| 4402 | lua_newtable(L); |
| 4403 | lua_pushvalue(L, 2); |
| 4404 | lua_pushvalue(L, -2); |
| 4405 | lua_settable(L, -4); |
| 4406 | |
| 4407 | } else if (ret != LUA_TTABLE) { |
| 4408 | |
| 4409 | /* corruption error. */ |
| 4410 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4411 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4412 | WILL_LJMP(lua_error(L)); |
| 4413 | } |
| 4414 | |
| 4415 | /* Now the top od thestack is an array of values. We push |
| 4416 | * the header value as new entry. |
| 4417 | */ |
| 4418 | lua_pushvalue(L, 3); |
| 4419 | ret = lua_rawlen(L, -2); |
| 4420 | lua_rawseti(L, -2, ret + 1); |
| 4421 | lua_pushboolean(L, 1); |
| 4422 | return 1; |
| 4423 | } |
| 4424 | |
| 4425 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4426 | { |
| 4427 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4428 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4429 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4430 | |
| 4431 | if (status < 100 || status > 599) { |
| 4432 | lua_pushboolean(L, 0); |
| 4433 | return 1; |
| 4434 | } |
| 4435 | |
| 4436 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4437 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4438 | lua_pushboolean(L, 1); |
| 4439 | return 1; |
| 4440 | } |
| 4441 | |
| 4442 | /* We will build the status line and the headers of the HTTP response. |
| 4443 | * We will try send at once if its not possible, we give back the hand |
| 4444 | * waiting for more room. |
| 4445 | */ |
| 4446 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4447 | { |
| 4448 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4449 | struct stream_interface *si = appctx->appctx->owner; |
| 4450 | struct channel *chn = si_ic(si); |
| 4451 | int ret; |
| 4452 | size_t len; |
| 4453 | const char *msg; |
| 4454 | |
| 4455 | /* Get the message as the first argument on the stack. */ |
| 4456 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4457 | |
| 4458 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4459 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4460 | |
| 4461 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4462 | * big for the buffers. |
| 4463 | */ |
| 4464 | if (ret == -2 || ret == -3) { |
| 4465 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4466 | WILL_LJMP(lua_error(L)); |
| 4467 | } |
| 4468 | |
| 4469 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4470 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4471 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4472 | 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] | 4473 | } |
| 4474 | |
| 4475 | /* Headers sent, set the flag. */ |
| 4476 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4477 | return 0; |
| 4478 | } |
| 4479 | |
| 4480 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4481 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4482 | struct buffer *tmp = get_trash_chunk(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4483 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4484 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4485 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4486 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4487 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4488 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4489 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4490 | int hdr_chunked = 0; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4491 | const char *reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4492 | |
| 4493 | if (reason == NULL) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4494 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4495 | |
| 4496 | /* Use the same http version than the request. */ |
| 4497 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4498 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4499 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4500 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4501 | |
| 4502 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4503 | lua_pushvalue(L, 0); |
| 4504 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4505 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4506 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4507 | WILL_LJMP(lua_error(L)); |
| 4508 | } |
| 4509 | |
| 4510 | /* Browse the list of headers. */ |
| 4511 | lua_pushnil(L); |
| 4512 | while(lua_next(L, -2) != 0) { |
| 4513 | |
| 4514 | /* We expect a string as -2. */ |
| 4515 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4516 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4517 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4518 | lua_typename(L, lua_type(L, -2))); |
| 4519 | WILL_LJMP(lua_error(L)); |
| 4520 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4521 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4522 | |
| 4523 | /* We expect an array as -1. */ |
| 4524 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4525 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4526 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4527 | name, |
| 4528 | lua_typename(L, lua_type(L, -1))); |
| 4529 | WILL_LJMP(lua_error(L)); |
| 4530 | } |
| 4531 | |
| 4532 | /* Browse the table who is on the top of the stack. */ |
| 4533 | lua_pushnil(L); |
| 4534 | while(lua_next(L, -2) != 0) { |
| 4535 | |
| 4536 | /* We expect a number as -2. */ |
| 4537 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4538 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4539 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4540 | name, |
| 4541 | lua_typename(L, lua_type(L, -2))); |
| 4542 | WILL_LJMP(lua_error(L)); |
| 4543 | } |
| 4544 | id = lua_tointeger(L, -2); |
| 4545 | |
| 4546 | /* We expect a string as -2. */ |
| 4547 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4548 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4549 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4550 | name, id, |
| 4551 | lua_typename(L, lua_type(L, -1))); |
| 4552 | WILL_LJMP(lua_error(L)); |
| 4553 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4554 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4555 | |
| 4556 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4557 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 4558 | memcpy(tmp->area + tmp->data, name, name_len); |
| 4559 | tmp->data += name_len; |
| 4560 | tmp->area[tmp->data++] = ':'; |
| 4561 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4562 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4563 | memcpy(tmp->area + tmp->data, value, |
| 4564 | value_len); |
| 4565 | tmp->data += value_len; |
| 4566 | tmp->area[tmp->data++] = '\r'; |
| 4567 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4568 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4569 | |
| 4570 | /* Protocol checks. */ |
| 4571 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4572 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4573 | * is done without control. If it contains a bad value, |
| 4574 | * the content-length remains negative so that we can |
| 4575 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4576 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4577 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4578 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4579 | |
| 4580 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4581 | if (name_len == 17 && value_len == 7 && |
| 4582 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4583 | strcasecmp("chunked", value) == 0) |
| 4584 | hdr_chunked = 1; |
| 4585 | |
| 4586 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4587 | lua_pop(L, 1); |
| 4588 | } |
| 4589 | |
| 4590 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4591 | lua_pop(L, 1); |
| 4592 | } |
| 4593 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4594 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4595 | * and the status code implies the presence of a message body, we must |
| 4596 | * announce a transfer encoding chunked. This is required by haproxy |
| 4597 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4598 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4599 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4600 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4601 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 4602 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4603 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4604 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4605 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4606 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4607 | } |
| 4608 | |
| 4609 | /* Finalize headers. */ |
| 4610 | chunk_appendf(tmp, "\r\n"); |
| 4611 | |
| 4612 | /* Remove the last entry and the array of headers */ |
| 4613 | lua_pop(L, 2); |
| 4614 | |
| 4615 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4616 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4617 | |
| 4618 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4619 | } |
| 4620 | |
| 4621 | /* |
| 4622 | * |
| 4623 | * |
| 4624 | * Class HTTP |
| 4625 | * |
| 4626 | * |
| 4627 | */ |
| 4628 | |
| 4629 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4630 | * a class stream, otherwise it throws an error. |
| 4631 | */ |
| 4632 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4633 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4634 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | /* This function creates and push in the stack a HTTP object |
| 4638 | * according with a current TXN. |
| 4639 | */ |
| 4640 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4641 | { |
| 4642 | struct hlua_txn *htxn; |
| 4643 | |
| 4644 | /* Check stack size. */ |
| 4645 | if (!lua_checkstack(L, 3)) |
| 4646 | return 0; |
| 4647 | |
| 4648 | /* Create the object: obj[0] = userdata. |
| 4649 | * Note that the base of the Converters object is the |
| 4650 | * same than the TXN object. |
| 4651 | */ |
| 4652 | lua_newtable(L); |
| 4653 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4654 | lua_rawseti(L, -2, 0); |
| 4655 | |
| 4656 | htxn->s = txn->s; |
| 4657 | htxn->p = txn->p; |
| 4658 | |
| 4659 | /* Pop a class stream metatable and affect it to the table. */ |
| 4660 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4661 | lua_setmetatable(L, -2); |
| 4662 | |
| 4663 | return 1; |
| 4664 | } |
| 4665 | |
| 4666 | /* This function creates ans returns an array of HTTP headers. |
| 4667 | * This function does not fails. It is used as wrapper with the |
| 4668 | * 2 following functions. |
| 4669 | */ |
| 4670 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4671 | { |
| 4672 | const char *cur_ptr, *cur_next, *p; |
| 4673 | int old_idx, cur_idx; |
| 4674 | struct hdr_idx_elem *cur_hdr; |
| 4675 | const char *hn, *hv; |
| 4676 | int hnl, hvl; |
| 4677 | int type; |
| 4678 | const char *in; |
| 4679 | char *out; |
| 4680 | int len; |
| 4681 | |
| 4682 | /* Create the table. */ |
| 4683 | lua_newtable(L); |
| 4684 | |
| 4685 | if (!htxn->s->txn) |
| 4686 | return 1; |
| 4687 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4688 | /* Check if a valid response is parsed */ |
| 4689 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4690 | return 1; |
| 4691 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4692 | /* Build array of headers. */ |
| 4693 | old_idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 4694 | 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] | 4695 | |
| 4696 | while (1) { |
| 4697 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4698 | if (!cur_idx) |
| 4699 | break; |
| 4700 | old_idx = cur_idx; |
| 4701 | |
| 4702 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4703 | cur_ptr = cur_next; |
| 4704 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4705 | |
| 4706 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4707 | * and the next header starts at cur_next. We'll check |
| 4708 | * this header in the list as well as against the default |
| 4709 | * rule. |
| 4710 | */ |
| 4711 | |
| 4712 | /* look for ': *'. */ |
| 4713 | hn = cur_ptr; |
| 4714 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4715 | if (p >= cur_ptr+cur_hdr->len) |
| 4716 | continue; |
| 4717 | hnl = p - hn; |
| 4718 | p++; |
| 4719 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4720 | p++; |
| 4721 | if (p >= cur_ptr+cur_hdr->len) |
| 4722 | continue; |
| 4723 | hv = p; |
| 4724 | hvl = cur_ptr+cur_hdr->len-p; |
| 4725 | |
| 4726 | /* Lowercase the key. Don't check the size of trash, it have |
| 4727 | * the size of one buffer and the input data contains in one |
| 4728 | * buffer. |
| 4729 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4730 | out = trash.area; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4731 | for (in=hn; in<hn+hnl; in++, out++) |
| 4732 | *out = tolower(*in); |
| 4733 | *out = '\0'; |
| 4734 | |
| 4735 | /* Check for existing entry: |
| 4736 | * assume that the table is on the top of the stack, and |
| 4737 | * push the key in the stack, the function lua_gettable() |
| 4738 | * perform the lookup. |
| 4739 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4740 | lua_pushlstring(L, trash.area, hnl); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4741 | lua_gettable(L, -2); |
| 4742 | type = lua_type(L, -1); |
| 4743 | |
| 4744 | switch (type) { |
| 4745 | case LUA_TNIL: |
| 4746 | /* Table not found, create it. */ |
| 4747 | lua_pop(L, 1); /* remove the nil value. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4748 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4749 | lua_newtable(L); /* create and push empty table. */ |
| 4750 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4751 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4752 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4753 | break; |
| 4754 | |
| 4755 | case LUA_TTABLE: |
| 4756 | /* Entry found: push the value in the table. */ |
| 4757 | len = lua_rawlen(L, -1); |
| 4758 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4759 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4760 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4761 | break; |
| 4762 | |
| 4763 | default: |
| 4764 | /* Other cases are errors. */ |
| 4765 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4766 | WILL_LJMP(lua_error(L)); |
| 4767 | } |
| 4768 | } |
| 4769 | |
| 4770 | return 1; |
| 4771 | } |
| 4772 | |
| 4773 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4774 | { |
| 4775 | struct hlua_txn *htxn; |
| 4776 | |
| 4777 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4778 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4779 | |
| 4780 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4781 | } |
| 4782 | |
| 4783 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4784 | { |
| 4785 | struct hlua_txn *htxn; |
| 4786 | |
| 4787 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4788 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4789 | |
| 4790 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4791 | } |
| 4792 | |
| 4793 | /* This function replace full header, or just a value in |
| 4794 | * the request or in the response. It is a wrapper fir the |
| 4795 | * 4 following functions. |
| 4796 | */ |
| 4797 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4798 | struct http_msg *msg, int action) |
| 4799 | { |
| 4800 | size_t name_len; |
| 4801 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4802 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4803 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4804 | struct my_regex re; |
| 4805 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4806 | /* Check if a valid response is parsed */ |
| 4807 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4808 | return 0; |
| 4809 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4810 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4811 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4812 | |
| 4813 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4814 | regex_free(&re); |
| 4815 | return 0; |
| 4816 | } |
| 4817 | |
| 4818 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4819 | { |
| 4820 | struct hlua_txn *htxn; |
| 4821 | |
| 4822 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4823 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4824 | |
| 4825 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4826 | } |
| 4827 | |
| 4828 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4829 | { |
| 4830 | struct hlua_txn *htxn; |
| 4831 | |
| 4832 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4833 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4834 | |
| 4835 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4836 | } |
| 4837 | |
| 4838 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4839 | { |
| 4840 | struct hlua_txn *htxn; |
| 4841 | |
| 4842 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4843 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4844 | |
| 4845 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4846 | } |
| 4847 | |
| 4848 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4849 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4850 | struct hlua_txn *htxn; |
| 4851 | |
| 4852 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4853 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4854 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4855 | 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] | 4856 | } |
| 4857 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4858 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4859 | * It is a wrapper for the 2 following functions. |
| 4860 | */ |
| 4861 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4862 | { |
| 4863 | size_t len; |
| 4864 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4865 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4866 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4867 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4868 | /* Check if a valid response is parsed */ |
| 4869 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4870 | return 0; |
| 4871 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4872 | ctx.idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 4873 | 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] | 4874 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4875 | return 0; |
| 4876 | } |
| 4877 | |
| 4878 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4879 | { |
| 4880 | struct hlua_txn *htxn; |
| 4881 | |
| 4882 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4883 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4884 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4885 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4886 | } |
| 4887 | |
| 4888 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4889 | { |
| 4890 | struct hlua_txn *htxn; |
| 4891 | |
| 4892 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4893 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4894 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4895 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4896 | } |
| 4897 | |
| 4898 | /* This function adds an header. It is a wrapper used by |
| 4899 | * the 2 following functions. |
| 4900 | */ |
| 4901 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4902 | { |
| 4903 | size_t name_len; |
| 4904 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4905 | size_t value_len; |
| 4906 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4907 | char *p; |
| 4908 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4909 | /* Check if a valid message is parsed */ |
| 4910 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4911 | return 0; |
| 4912 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4913 | /* Check length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4914 | trash.data = value_len + name_len + 2; |
| 4915 | if (trash.data > trash.size) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4916 | return 0; |
| 4917 | |
| 4918 | /* Creates the header string. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4919 | p = trash.area; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4920 | memcpy(p, name, name_len); |
| 4921 | p += name_len; |
| 4922 | *p = ':'; |
| 4923 | p++; |
| 4924 | *p = ' '; |
| 4925 | p++; |
| 4926 | memcpy(p, value, value_len); |
| 4927 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4928 | 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] | 4929 | trash.area, trash.data) != 0); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4930 | |
| 4931 | return 0; |
| 4932 | } |
| 4933 | |
| 4934 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4935 | { |
| 4936 | struct hlua_txn *htxn; |
| 4937 | |
| 4938 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4939 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4940 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4941 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4942 | } |
| 4943 | |
| 4944 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4945 | { |
| 4946 | struct hlua_txn *htxn; |
| 4947 | |
| 4948 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4949 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4950 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4951 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4952 | } |
| 4953 | |
| 4954 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4955 | { |
| 4956 | struct hlua_txn *htxn; |
| 4957 | |
| 4958 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4959 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4960 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4961 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4962 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4966 | { |
| 4967 | struct hlua_txn *htxn; |
| 4968 | |
| 4969 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4970 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4971 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4972 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4973 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4974 | } |
| 4975 | |
| 4976 | /* This function set the method. */ |
| 4977 | static int hlua_http_req_set_meth(lua_State *L) |
| 4978 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4979 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4980 | size_t name_len; |
| 4981 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4982 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4983 | /* Check if a valid request is parsed */ |
| 4984 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4985 | lua_pushboolean(L, 0); |
| 4986 | return 1; |
| 4987 | } |
| 4988 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4989 | 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] | 4990 | return 1; |
| 4991 | } |
| 4992 | |
| 4993 | /* This function set the method. */ |
| 4994 | static int hlua_http_req_set_path(lua_State *L) |
| 4995 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4996 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4997 | size_t name_len; |
| 4998 | 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] | 4999 | |
| 5000 | /* Check if a valid request is parsed */ |
| 5001 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5002 | lua_pushboolean(L, 0); |
| 5003 | return 1; |
| 5004 | } |
| 5005 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5006 | 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] | 5007 | return 1; |
| 5008 | } |
| 5009 | |
| 5010 | /* This function set the query-string. */ |
| 5011 | static int hlua_http_req_set_query(lua_State *L) |
| 5012 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5013 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5014 | size_t name_len; |
| 5015 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5016 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5017 | /* Check if a valid request is parsed */ |
| 5018 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5019 | lua_pushboolean(L, 0); |
| 5020 | return 1; |
| 5021 | } |
| 5022 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5023 | /* Check length. */ |
| 5024 | if (name_len > trash.size - 1) { |
| 5025 | lua_pushboolean(L, 0); |
| 5026 | return 1; |
| 5027 | } |
| 5028 | |
| 5029 | /* Add the mark question as prefix. */ |
| 5030 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5031 | trash.area[trash.data++] = '?'; |
| 5032 | memcpy(trash.area + trash.data, name, name_len); |
| 5033 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5034 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5035 | lua_pushboolean(L, |
| 5036 | 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] | 5037 | return 1; |
| 5038 | } |
| 5039 | |
| 5040 | /* This function set the uri. */ |
| 5041 | static int hlua_http_req_set_uri(lua_State *L) |
| 5042 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5043 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5044 | size_t name_len; |
| 5045 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5046 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5047 | /* Check if a valid request is parsed */ |
| 5048 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5049 | lua_pushboolean(L, 0); |
| 5050 | return 1; |
| 5051 | } |
| 5052 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5053 | 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] | 5054 | return 1; |
| 5055 | } |
| 5056 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5057 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5058 | static int hlua_http_res_set_status(lua_State *L) |
| 5059 | { |
| 5060 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5061 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5062 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5063 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5064 | /* Check if a valid response is parsed */ |
| 5065 | if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5066 | return 0; |
| 5067 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5068 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5069 | return 0; |
| 5070 | } |
| 5071 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5072 | /* |
| 5073 | * |
| 5074 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5075 | * Class TXN |
| 5076 | * |
| 5077 | * |
| 5078 | */ |
| 5079 | |
| 5080 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5081 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5082 | */ |
| 5083 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5084 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5085 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5086 | } |
| 5087 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5088 | __LJMP static int hlua_set_var(lua_State *L) |
| 5089 | { |
| 5090 | struct hlua_txn *htxn; |
| 5091 | const char *name; |
| 5092 | size_t len; |
| 5093 | struct sample smp; |
| 5094 | |
| 5095 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5096 | |
| 5097 | /* It is useles to retrieve the stream, but this function |
| 5098 | * runs only in a stream context. |
| 5099 | */ |
| 5100 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5101 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5102 | |
| 5103 | /* Converts the third argument in a sample. */ |
| 5104 | hlua_lua2smp(L, 3, &smp); |
| 5105 | |
| 5106 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5107 | 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] | 5108 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5109 | return 0; |
| 5110 | } |
| 5111 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5112 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5113 | { |
| 5114 | struct hlua_txn *htxn; |
| 5115 | const char *name; |
| 5116 | size_t len; |
| 5117 | struct sample smp; |
| 5118 | |
| 5119 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5120 | |
| 5121 | /* It is useles to retrieve the stream, but this function |
| 5122 | * runs only in a stream context. |
| 5123 | */ |
| 5124 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5125 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5126 | |
| 5127 | /* Unset the variable. */ |
| 5128 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5129 | vars_unset_by_name(name, len, &smp); |
| 5130 | return 0; |
| 5131 | } |
| 5132 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5133 | __LJMP static int hlua_get_var(lua_State *L) |
| 5134 | { |
| 5135 | struct hlua_txn *htxn; |
| 5136 | const char *name; |
| 5137 | size_t len; |
| 5138 | struct sample smp; |
| 5139 | |
| 5140 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5141 | |
| 5142 | /* It is useles to retrieve the stream, but this function |
| 5143 | * runs only in a stream context. |
| 5144 | */ |
| 5145 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5146 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5147 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5148 | 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] | 5149 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5150 | lua_pushnil(L); |
| 5151 | return 1; |
| 5152 | } |
| 5153 | |
| 5154 | return hlua_smp2lua(L, &smp); |
| 5155 | } |
| 5156 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5157 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5158 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5159 | struct hlua *hlua; |
| 5160 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5161 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5162 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5163 | /* It is useles to retrieve the stream, but this function |
| 5164 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5165 | */ |
| 5166 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5167 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5168 | |
| 5169 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5170 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5171 | |
| 5172 | /* Get and store new value. */ |
| 5173 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5174 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5175 | |
| 5176 | return 0; |
| 5177 | } |
| 5178 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5179 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5180 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5181 | struct hlua *hlua; |
| 5182 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5183 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5184 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5185 | /* It is useles to retrieve the stream, but this function |
| 5186 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5187 | */ |
| 5188 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5189 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5190 | |
| 5191 | /* Push configuration index in the stack. */ |
| 5192 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5193 | |
| 5194 | return 1; |
| 5195 | } |
| 5196 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5197 | /* Create stack entry containing a class TXN. This function |
| 5198 | * return 0 if the stack does not contains free slots, |
| 5199 | * otherwise it returns 1. |
| 5200 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5201 | 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] | 5202 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5203 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5204 | |
| 5205 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5206 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5207 | return 0; |
| 5208 | |
| 5209 | /* NOTE: The allocation never fails. The failure |
| 5210 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5211 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5212 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5213 | /* Create the object: obj[0] = userdata. */ |
| 5214 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5215 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5216 | lua_rawseti(L, -2, 0); |
| 5217 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5218 | htxn->s = s; |
| 5219 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5220 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5221 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5222 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5223 | /* Create the "f" field that contains a list of fetches. */ |
| 5224 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5225 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5226 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5227 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5228 | |
| 5229 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5230 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5231 | 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] | 5232 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5233 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5234 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5235 | /* Create the "c" field that contains a list of converters. */ |
| 5236 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5237 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5238 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5239 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5240 | |
| 5241 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5242 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5243 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5244 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5245 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5246 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5247 | /* Create the "req" field that contains the request channel object. */ |
| 5248 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5249 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5250 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5251 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5252 | |
| 5253 | /* Create the "res" field that contains the response channel object. */ |
| 5254 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5255 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5256 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5257 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5258 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5259 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5260 | lua_pushstring(L, "http"); |
| 5261 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5262 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5263 | return 0; |
| 5264 | } |
| 5265 | else |
| 5266 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5267 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5268 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5269 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5270 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5271 | lua_setmetatable(L, -2); |
| 5272 | |
| 5273 | return 1; |
| 5274 | } |
| 5275 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5276 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5277 | { |
| 5278 | const char *msg; |
| 5279 | struct hlua_txn *htxn; |
| 5280 | |
| 5281 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5282 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5283 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5284 | |
| 5285 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5286 | return 0; |
| 5287 | } |
| 5288 | |
| 5289 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5290 | { |
| 5291 | int level; |
| 5292 | const char *msg; |
| 5293 | struct hlua_txn *htxn; |
| 5294 | |
| 5295 | MAY_LJMP(check_args(L, 3, "log")); |
| 5296 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5297 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5298 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5299 | |
| 5300 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5301 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5302 | |
| 5303 | hlua_sendlog(htxn->s->be, level, msg); |
| 5304 | return 0; |
| 5305 | } |
| 5306 | |
| 5307 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5308 | { |
| 5309 | const char *msg; |
| 5310 | struct hlua_txn *htxn; |
| 5311 | |
| 5312 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5313 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5314 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5315 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5316 | return 0; |
| 5317 | } |
| 5318 | |
| 5319 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5320 | { |
| 5321 | const char *msg; |
| 5322 | struct hlua_txn *htxn; |
| 5323 | |
| 5324 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5325 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5326 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5327 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5328 | return 0; |
| 5329 | } |
| 5330 | |
| 5331 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5332 | { |
| 5333 | const char *msg; |
| 5334 | struct hlua_txn *htxn; |
| 5335 | |
| 5336 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5337 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5338 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5339 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5340 | return 0; |
| 5341 | } |
| 5342 | |
| 5343 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5344 | { |
| 5345 | const char *msg; |
| 5346 | struct hlua_txn *htxn; |
| 5347 | |
| 5348 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5349 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5350 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5351 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5352 | return 0; |
| 5353 | } |
| 5354 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5355 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5356 | { |
| 5357 | struct hlua_txn *htxn; |
| 5358 | int ll; |
| 5359 | |
| 5360 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5361 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5362 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5363 | |
| 5364 | if (ll < 0 || ll > 7) |
| 5365 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5366 | |
| 5367 | htxn->s->logs.level = ll; |
| 5368 | return 0; |
| 5369 | } |
| 5370 | |
| 5371 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5372 | { |
| 5373 | struct hlua_txn *htxn; |
| 5374 | struct connection *cli_conn; |
| 5375 | int tos; |
| 5376 | |
| 5377 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5378 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5379 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5380 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5381 | 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] | 5382 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5383 | |
| 5384 | return 0; |
| 5385 | } |
| 5386 | |
| 5387 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5388 | { |
| 5389 | #ifdef SO_MARK |
| 5390 | struct hlua_txn *htxn; |
| 5391 | struct connection *cli_conn; |
| 5392 | int mark; |
| 5393 | |
| 5394 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5395 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5396 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5397 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5398 | 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] | 5399 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5400 | #endif |
| 5401 | return 0; |
| 5402 | } |
| 5403 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5404 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5405 | { |
| 5406 | struct hlua_txn *htxn; |
| 5407 | |
| 5408 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5409 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5410 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5411 | return 0; |
| 5412 | } |
| 5413 | |
| 5414 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5415 | { |
| 5416 | struct hlua_txn *htxn; |
| 5417 | |
| 5418 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5419 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5420 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5421 | return 0; |
| 5422 | } |
| 5423 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5424 | /* This function is an Lua binding that send pending data |
| 5425 | * to the client, and close the stream interface. |
| 5426 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5427 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5428 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5429 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5430 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5431 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5432 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5433 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5434 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5435 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5436 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5437 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5438 | * session, so we just end the execution of the current |
| 5439 | * lua code. |
| 5440 | */ |
| 5441 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5442 | WILL_LJMP(hlua_done(L)); |
| 5443 | return 0; |
| 5444 | } |
| 5445 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5446 | ic = &htxn->s->req; |
| 5447 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5448 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5449 | if (htxn->s->txn) { |
| 5450 | /* HTTP mode, let's stay in sync with the stream */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5451 | b_del(&ic->buf, htxn->s->txn->req.sov); |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5452 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 5453 | htxn->s->txn->req.sov = 0; |
| 5454 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 5455 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 5456 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5457 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 5458 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5459 | /* Note that if we want to support keep-alive, we need |
| 5460 | * to bypass the close/shutr_now calls below, but that |
| 5461 | * may only be done if the HTTP request was already |
| 5462 | * processed and the connection header is known (ie |
| 5463 | * not during TCP rules). |
| 5464 | */ |
| 5465 | } |
| 5466 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5467 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5468 | channel_abort(ic); |
| 5469 | channel_auto_close(ic); |
| 5470 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5471 | |
| 5472 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5473 | channel_auto_read(oc); |
| 5474 | channel_auto_close(oc); |
| 5475 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5476 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 5477 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5478 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5479 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5480 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5481 | return 0; |
| 5482 | } |
| 5483 | |
| 5484 | __LJMP static int hlua_log(lua_State *L) |
| 5485 | { |
| 5486 | int level; |
| 5487 | const char *msg; |
| 5488 | |
| 5489 | MAY_LJMP(check_args(L, 2, "log")); |
| 5490 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5491 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5492 | |
| 5493 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5494 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5495 | |
| 5496 | hlua_sendlog(NULL, level, msg); |
| 5497 | return 0; |
| 5498 | } |
| 5499 | |
| 5500 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5501 | { |
| 5502 | const char *msg; |
| 5503 | |
| 5504 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5505 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5506 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5507 | return 0; |
| 5508 | } |
| 5509 | |
| 5510 | __LJMP static int hlua_log_info(lua_State *L) |
| 5511 | { |
| 5512 | const char *msg; |
| 5513 | |
| 5514 | MAY_LJMP(check_args(L, 1, "info")); |
| 5515 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5516 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5517 | return 0; |
| 5518 | } |
| 5519 | |
| 5520 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5521 | { |
| 5522 | const char *msg; |
| 5523 | |
| 5524 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5525 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5526 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5527 | return 0; |
| 5528 | } |
| 5529 | |
| 5530 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5531 | { |
| 5532 | const char *msg; |
| 5533 | |
| 5534 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5535 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5536 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5537 | return 0; |
| 5538 | } |
| 5539 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5540 | __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] | 5541 | { |
| 5542 | int wakeup_ms = lua_tointeger(L, -1); |
| 5543 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5544 | 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] | 5545 | return 0; |
| 5546 | } |
| 5547 | |
| 5548 | __LJMP static int hlua_sleep(lua_State *L) |
| 5549 | { |
| 5550 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5551 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5552 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5553 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5554 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5555 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5556 | wakeup_ms = tick_add(now_ms, delay); |
| 5557 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5558 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5559 | 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] | 5560 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5561 | } |
| 5562 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5563 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5564 | { |
| 5565 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5566 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5567 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5568 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5569 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5570 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5571 | wakeup_ms = tick_add(now_ms, delay); |
| 5572 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5573 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5574 | 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] | 5575 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5576 | } |
| 5577 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5578 | /* This functionis an LUA binding. it permits to give back |
| 5579 | * the hand at the HAProxy scheduler. It is used when the |
| 5580 | * LUA processing consumes a lot of time. |
| 5581 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5582 | __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] | 5583 | { |
| 5584 | return 0; |
| 5585 | } |
| 5586 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5587 | __LJMP static int hlua_yield(lua_State *L) |
| 5588 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5589 | 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] | 5590 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5591 | } |
| 5592 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5593 | /* This function change the nice of the currently executed |
| 5594 | * task. It is used set low or high priority at the current |
| 5595 | * task. |
| 5596 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5597 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5598 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5599 | struct hlua *hlua; |
| 5600 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5601 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5602 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5603 | hlua = hlua_gethlua(L); |
| 5604 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5605 | |
| 5606 | /* If he task is not set, I'm in a start mode. */ |
| 5607 | if (!hlua || !hlua->task) |
| 5608 | return 0; |
| 5609 | |
| 5610 | if (nice < -1024) |
| 5611 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5612 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5613 | nice = 1024; |
| 5614 | |
| 5615 | hlua->task->nice = nice; |
| 5616 | return 0; |
| 5617 | } |
| 5618 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5619 | /* 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] | 5620 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5621 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5622 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5623 | * |
| 5624 | * Task wrapper are longjmp safe because the only one Lua code |
| 5625 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5626 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5627 | 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] | 5628 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5629 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5630 | enum hlua_exec status; |
| 5631 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 5632 | if (task->thread_mask == MAX_THREADS_MASK) |
| 5633 | task_set_affinity(task, tid_bit); |
| 5634 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5635 | /* If it is the first call to the task, we must initialize the |
| 5636 | * execution timeouts. |
| 5637 | */ |
| 5638 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5639 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5640 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5641 | /* Execute the Lua code. */ |
| 5642 | status = hlua_ctx_resume(hlua, 1); |
| 5643 | |
| 5644 | switch (status) { |
| 5645 | /* finished or yield */ |
| 5646 | case HLUA_E_OK: |
| 5647 | hlua_ctx_destroy(hlua); |
| 5648 | task_delete(task); |
| 5649 | task_free(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 5650 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5651 | break; |
| 5652 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5653 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 5654 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 5655 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5656 | break; |
| 5657 | |
| 5658 | /* finished with error. */ |
| 5659 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5660 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5661 | hlua_ctx_destroy(hlua); |
| 5662 | task_delete(task); |
| 5663 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5664 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5665 | break; |
| 5666 | |
| 5667 | case HLUA_E_ERR: |
| 5668 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5669 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5670 | hlua_ctx_destroy(hlua); |
| 5671 | task_delete(task); |
| 5672 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5673 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5674 | break; |
| 5675 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5676 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5677 | } |
| 5678 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5679 | /* This function is an LUA binding that register LUA function to be |
| 5680 | * executed after the HAProxy configuration parsing and before the |
| 5681 | * HAProxy scheduler starts. This function expect only one LUA |
| 5682 | * argument that is a function. This function returns nothing, but |
| 5683 | * throws if an error is encountered. |
| 5684 | */ |
| 5685 | __LJMP static int hlua_register_init(lua_State *L) |
| 5686 | { |
| 5687 | struct hlua_init_function *init; |
| 5688 | int ref; |
| 5689 | |
| 5690 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5691 | |
| 5692 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5693 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5694 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5695 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5696 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5697 | |
| 5698 | init->function_ref = ref; |
| 5699 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5700 | return 0; |
| 5701 | } |
| 5702 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5703 | /* This functio is an LUA binding. It permits to register a task |
| 5704 | * executed in parallel of the main HAroxy activity. The task is |
| 5705 | * created and it is set in the HAProxy scheduler. It can be called |
| 5706 | * from the "init" section, "post init" or during the runtime. |
| 5707 | * |
| 5708 | * Lua prototype: |
| 5709 | * |
| 5710 | * <none> core.register_task(<function>) |
| 5711 | */ |
| 5712 | static int hlua_register_task(lua_State *L) |
| 5713 | { |
| 5714 | struct hlua *hlua; |
| 5715 | struct task *task; |
| 5716 | int ref; |
| 5717 | |
| 5718 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5719 | |
| 5720 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5721 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5722 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5723 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5724 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5725 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 5726 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 5727 | if (!task) |
| 5728 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 5729 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5730 | task->context = hlua; |
| 5731 | task->process = hlua_process_task; |
| 5732 | |
| 5733 | if (!hlua_ctx_init(hlua, task)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5734 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5735 | |
| 5736 | /* Restore the function in the stack. */ |
| 5737 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5738 | hlua->nargs = 0; |
| 5739 | |
| 5740 | /* Schedule task. */ |
| 5741 | task_schedule(task, now_ms); |
| 5742 | |
| 5743 | return 0; |
| 5744 | } |
| 5745 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5746 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5747 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5748 | * resume converters. |
| 5749 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5750 | 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] | 5751 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5752 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5753 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5754 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5755 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5756 | if (!stream) |
| 5757 | return 0; |
| 5758 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 5759 | if (IS_HTX_STRM(stream)) { |
| 5760 | SEND_ERR(stream->be, "Lua converter '%s': Lua fetches cannot be used when the" |
| 5761 | " HTX internal representation is enabled.\n", fcn->name); |
| 5762 | return 0; |
| 5763 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5764 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5765 | * Lua context can be not initialized. This behavior |
| 5766 | * permits to save performances because a systematic |
| 5767 | * Lua initialization cause 5% performances loss. |
| 5768 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5769 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5770 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5771 | if (!stream->hlua) { |
| 5772 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5773 | return 0; |
| 5774 | } |
| 5775 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5776 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5777 | return 0; |
| 5778 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5779 | } |
| 5780 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5781 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5782 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5783 | |
| 5784 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5785 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5786 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5787 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5788 | else |
| 5789 | error = "critical error"; |
| 5790 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5791 | return 0; |
| 5792 | } |
| 5793 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5794 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5795 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5796 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5797 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5798 | return 0; |
| 5799 | } |
| 5800 | |
| 5801 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5802 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5803 | |
| 5804 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5805 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5806 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5807 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5808 | return 0; |
| 5809 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5810 | hlua_smp2lua(stream->hlua->T, smp); |
| 5811 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5812 | |
| 5813 | /* push keywords in the stack. */ |
| 5814 | if (arg_p) { |
| 5815 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5816 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5817 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5818 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5819 | return 0; |
| 5820 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5821 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5822 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5823 | } |
| 5824 | } |
| 5825 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5826 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5827 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5828 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5829 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5830 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5831 | } |
| 5832 | |
| 5833 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5834 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5835 | /* finished. */ |
| 5836 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5837 | /* If the stack is empty, the function fails. */ |
| 5838 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5839 | return 0; |
| 5840 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5841 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5842 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5843 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5844 | return 1; |
| 5845 | |
| 5846 | /* yield. */ |
| 5847 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5848 | 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] | 5849 | return 0; |
| 5850 | |
| 5851 | /* finished with error. */ |
| 5852 | case HLUA_E_ERRMSG: |
| 5853 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5854 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5855 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5856 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5857 | return 0; |
| 5858 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5859 | case HLUA_E_ETMOUT: |
| 5860 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 5861 | return 0; |
| 5862 | |
| 5863 | case HLUA_E_NOMEM: |
| 5864 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 5865 | return 0; |
| 5866 | |
| 5867 | case HLUA_E_YIELD: |
| 5868 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 5869 | return 0; |
| 5870 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5871 | case HLUA_E_ERR: |
| 5872 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5873 | 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] | 5874 | |
| 5875 | default: |
| 5876 | return 0; |
| 5877 | } |
| 5878 | } |
| 5879 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5880 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5881 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5882 | * resume sample-fetches. This function will be called by the sample |
| 5883 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5884 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5885 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5886 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5887 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5888 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5889 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5890 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5891 | const struct buffer msg = { }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5892 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5893 | if (!stream) |
| 5894 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 5895 | |
| 5896 | if (IS_HTX_STRM(stream)) { |
| 5897 | SEND_ERR(stream->be, "Lua sample-fetch '%s': Lua fetches cannot be used when the" |
| 5898 | " HTX internal representation is enabled.\n", fcn->name); |
| 5899 | return 0; |
| 5900 | } |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5901 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5902 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5903 | * Lua context can be not initialized. This behavior |
| 5904 | * permits to save performances because a systematic |
| 5905 | * Lua initialization cause 5% performances loss. |
| 5906 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5907 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5908 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5909 | if (!stream->hlua) { |
| 5910 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5911 | return 0; |
| 5912 | } |
| 5913 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5914 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5915 | return 0; |
| 5916 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5917 | } |
| 5918 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5919 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5920 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5921 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5922 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5923 | |
| 5924 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5925 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5926 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5927 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5928 | else |
| 5929 | error = "critical error"; |
| 5930 | 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] | 5931 | return 0; |
| 5932 | } |
| 5933 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5934 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5935 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5936 | 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] | 5937 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5938 | return 0; |
| 5939 | } |
| 5940 | |
| 5941 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5942 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5943 | |
| 5944 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5945 | 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] | 5946 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5947 | 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] | 5948 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5949 | return 0; |
| 5950 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5951 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5952 | |
| 5953 | /* push keywords in the stack. */ |
| 5954 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5955 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5956 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5957 | 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] | 5958 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5959 | return 0; |
| 5960 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5961 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5962 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5963 | } |
| 5964 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5965 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5966 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5967 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5968 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5969 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5970 | } |
| 5971 | |
| 5972 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5973 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5974 | /* finished. */ |
| 5975 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5976 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5977 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5978 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5979 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5980 | /* If the stack is empty, the function fails. */ |
| 5981 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5982 | return 0; |
| 5983 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5984 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5985 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5986 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5987 | |
| 5988 | /* Set the end of execution flag. */ |
| 5989 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5990 | return 1; |
| 5991 | |
| 5992 | /* yield. */ |
| 5993 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5994 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5995 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5996 | 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] | 5997 | return 0; |
| 5998 | |
| 5999 | /* finished with error. */ |
| 6000 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6001 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6002 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6003 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6004 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6005 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6006 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6007 | return 0; |
| 6008 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6009 | case HLUA_E_ETMOUT: |
| 6010 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 6011 | stream_int_retnclose(&stream->si[0], &msg); |
| 6012 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6013 | return 0; |
| 6014 | |
| 6015 | case HLUA_E_NOMEM: |
| 6016 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 6017 | stream_int_retnclose(&stream->si[0], &msg); |
| 6018 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6019 | return 0; |
| 6020 | |
| 6021 | case HLUA_E_YIELD: |
| 6022 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 6023 | stream_int_retnclose(&stream->si[0], &msg); |
| 6024 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6025 | return 0; |
| 6026 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6027 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6028 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6029 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6030 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6031 | 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] | 6032 | |
| 6033 | default: |
| 6034 | return 0; |
| 6035 | } |
| 6036 | } |
| 6037 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6038 | /* This function is an LUA binding used for registering |
| 6039 | * "sample-conv" functions. It expects a converter name used |
| 6040 | * in the haproxy configuration file, and an LUA function. |
| 6041 | */ |
| 6042 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6043 | { |
| 6044 | struct sample_conv_kw_list *sck; |
| 6045 | const char *name; |
| 6046 | int ref; |
| 6047 | int len; |
| 6048 | struct hlua_function *fcn; |
| 6049 | |
| 6050 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6051 | |
| 6052 | /* First argument : converter name. */ |
| 6053 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6054 | |
| 6055 | /* Second argument : lua function. */ |
| 6056 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6057 | |
| 6058 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6059 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6060 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6061 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6062 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6063 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6064 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6065 | |
| 6066 | /* Fill fcn. */ |
| 6067 | fcn->name = strdup(name); |
| 6068 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6069 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6070 | fcn->function_ref = ref; |
| 6071 | |
| 6072 | /* List head */ |
| 6073 | sck->list.n = sck->list.p = NULL; |
| 6074 | |
| 6075 | /* converter keyword. */ |
| 6076 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6077 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6078 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6079 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6080 | |
| 6081 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6082 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6083 | 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] | 6084 | sck->kw[0].val_args = NULL; |
| 6085 | sck->kw[0].in_type = SMP_T_STR; |
| 6086 | sck->kw[0].out_type = SMP_T_STR; |
| 6087 | sck->kw[0].private = fcn; |
| 6088 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6089 | /* Register this new converter */ |
| 6090 | sample_register_convs(sck); |
| 6091 | |
| 6092 | return 0; |
| 6093 | } |
| 6094 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6095 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6096 | * "sample-fetch" functions. It expects a converter name used |
| 6097 | * in the haproxy configuration file, and an LUA function. |
| 6098 | */ |
| 6099 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6100 | { |
| 6101 | const char *name; |
| 6102 | int ref; |
| 6103 | int len; |
| 6104 | struct sample_fetch_kw_list *sfk; |
| 6105 | struct hlua_function *fcn; |
| 6106 | |
| 6107 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6108 | |
| 6109 | /* First argument : sample-fetch name. */ |
| 6110 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6111 | |
| 6112 | /* Second argument : lua function. */ |
| 6113 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6114 | |
| 6115 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6116 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6117 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6118 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6119 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6120 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6121 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6122 | |
| 6123 | /* Fill fcn. */ |
| 6124 | fcn->name = strdup(name); |
| 6125 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6126 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6127 | fcn->function_ref = ref; |
| 6128 | |
| 6129 | /* List head */ |
| 6130 | sfk->list.n = sfk->list.p = NULL; |
| 6131 | |
| 6132 | /* sample-fetch keyword. */ |
| 6133 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6134 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6135 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6136 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6137 | |
| 6138 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6139 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6140 | 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] | 6141 | sfk->kw[0].val_args = NULL; |
| 6142 | sfk->kw[0].out_type = SMP_T_STR; |
| 6143 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6144 | sfk->kw[0].val = 0; |
| 6145 | sfk->kw[0].private = fcn; |
| 6146 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6147 | /* Register this new fetch. */ |
| 6148 | sample_register_fetches(sfk); |
| 6149 | |
| 6150 | return 0; |
| 6151 | } |
| 6152 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6153 | /* This function is a wrapper to execute each LUA function declared |
| 6154 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6155 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6156 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6157 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6158 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6159 | 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] | 6160 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6161 | { |
| 6162 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6163 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6164 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6165 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6166 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6167 | |
| 6168 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 6169 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 6170 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 6171 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 6172 | 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] | 6173 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6174 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6175 | return ACT_RET_CONT; |
| 6176 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6177 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6178 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6179 | * Lua context can be not initialized. This behavior |
| 6180 | * permits to save performances because a systematic |
| 6181 | * Lua initialization cause 5% performances loss. |
| 6182 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6183 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6184 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6185 | if (!s->hlua) { |
| 6186 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6187 | rule->arg.hlua_rule->fcn.name); |
| 6188 | return ACT_RET_CONT; |
| 6189 | } |
| 6190 | if (!hlua_ctx_init(s->hlua, s->task)) { |
| 6191 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6192 | rule->arg.hlua_rule->fcn.name); |
| 6193 | return ACT_RET_CONT; |
| 6194 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6195 | } |
| 6196 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6197 | consistency_set(s, dir, &s->hlua->cons); |
| 6198 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6199 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6200 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6201 | |
| 6202 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6203 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6204 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6205 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6206 | else |
| 6207 | error = "critical error"; |
| 6208 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6209 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6210 | return ACT_RET_CONT; |
| 6211 | } |
| 6212 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6213 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6214 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6215 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6216 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6217 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6218 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6219 | } |
| 6220 | |
| 6221 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6222 | 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] | 6223 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6224 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6225 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6226 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6227 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6228 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6229 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6230 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6231 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6232 | |
| 6233 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6234 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6235 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6236 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6237 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6238 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6239 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6240 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6241 | lua_pushstring(s->hlua->T, *arg); |
| 6242 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6243 | } |
| 6244 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6245 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6246 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6247 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6248 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6249 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6250 | } |
| 6251 | |
| 6252 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6253 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6254 | /* finished. */ |
| 6255 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6256 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6257 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6258 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6259 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6260 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6261 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6262 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6263 | |
| 6264 | /* yield. */ |
| 6265 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6266 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6267 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6268 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6269 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6270 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6271 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6272 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6273 | /* Some actions can be wake up when a "write" event |
| 6274 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6275 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6276 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6277 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6278 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6279 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6280 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6281 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6282 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6283 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6284 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6285 | * because HAProxy is not able to manipulate data, it |
| 6286 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6287 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6288 | |
| 6289 | /* finished with error. */ |
| 6290 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6291 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6292 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6293 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6294 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6295 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6296 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6297 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6298 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6299 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6300 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6301 | case HLUA_E_ETMOUT: |
| 6302 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6303 | stream_int_retnclose(&s->si[0], &msg); |
| 6304 | return ACT_RET_ERR; |
| 6305 | } |
| 6306 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6307 | return 0; |
| 6308 | |
| 6309 | case HLUA_E_NOMEM: |
| 6310 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6311 | stream_int_retnclose(&s->si[0], &msg); |
| 6312 | return ACT_RET_ERR; |
| 6313 | } |
| 6314 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6315 | return 0; |
| 6316 | |
| 6317 | case HLUA_E_YIELD: |
| 6318 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6319 | stream_int_retnclose(&s->si[0], &msg); |
| 6320 | return ACT_RET_ERR; |
| 6321 | } |
| 6322 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6323 | rule->arg.hlua_rule->fcn.name); |
| 6324 | return 0; |
| 6325 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6326 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6327 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6328 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6329 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6330 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6331 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6332 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6333 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6334 | |
| 6335 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6336 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6337 | } |
| 6338 | } |
| 6339 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6340 | 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] | 6341 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6342 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6343 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6344 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6345 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6346 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6347 | } |
| 6348 | |
| 6349 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6350 | { |
| 6351 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6352 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6353 | struct task *task; |
| 6354 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6355 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6356 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6357 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6358 | if (!hlua) { |
| 6359 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6360 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6361 | return 0; |
| 6362 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6363 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6364 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6365 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6366 | |
| 6367 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6368 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6369 | if (!task) { |
| 6370 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6371 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6372 | return 0; |
| 6373 | } |
| 6374 | task->nice = 0; |
| 6375 | task->context = ctx; |
| 6376 | task->process = hlua_applet_wakeup; |
| 6377 | ctx->ctx.hlua_apptcp.task = task; |
| 6378 | |
| 6379 | /* In the execution wrappers linked with a stream, the |
| 6380 | * Lua context can be not initialized. This behavior |
| 6381 | * permits to save performances because a systematic |
| 6382 | * Lua initialization cause 5% performances loss. |
| 6383 | */ |
| 6384 | if (!hlua_ctx_init(hlua, task)) { |
| 6385 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6386 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6387 | return 0; |
| 6388 | } |
| 6389 | |
| 6390 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6391 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6392 | |
| 6393 | /* The following Lua calls can fail. */ |
| 6394 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6395 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6396 | error = lua_tostring(hlua->T, -1); |
| 6397 | else |
| 6398 | error = "critical error"; |
| 6399 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6400 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6401 | return 0; |
| 6402 | } |
| 6403 | |
| 6404 | /* Check stack available size. */ |
| 6405 | if (!lua_checkstack(hlua->T, 1)) { |
| 6406 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6407 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6408 | RESET_SAFE_LJMP(hlua->T); |
| 6409 | return 0; |
| 6410 | } |
| 6411 | |
| 6412 | /* Restore the function in the stack. */ |
| 6413 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6414 | |
| 6415 | /* Create and and push object stream in the stack. */ |
| 6416 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6417 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6418 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6419 | RESET_SAFE_LJMP(hlua->T); |
| 6420 | return 0; |
| 6421 | } |
| 6422 | hlua->nargs = 1; |
| 6423 | |
| 6424 | /* push keywords in the stack. */ |
| 6425 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6426 | if (!lua_checkstack(hlua->T, 1)) { |
| 6427 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6428 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6429 | RESET_SAFE_LJMP(hlua->T); |
| 6430 | return 0; |
| 6431 | } |
| 6432 | lua_pushstring(hlua->T, *arg); |
| 6433 | hlua->nargs++; |
| 6434 | } |
| 6435 | |
| 6436 | RESET_SAFE_LJMP(hlua->T); |
| 6437 | |
| 6438 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6439 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6440 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6441 | |
| 6442 | return 1; |
| 6443 | } |
| 6444 | |
| 6445 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6446 | { |
| 6447 | struct stream_interface *si = ctx->owner; |
| 6448 | struct stream *strm = si_strm(si); |
| 6449 | struct channel *res = si_ic(si); |
| 6450 | struct act_rule *rule = ctx->rule; |
| 6451 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6452 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6453 | |
| 6454 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6455 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6456 | /* eat the whole request */ |
| 6457 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6458 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6459 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6460 | |
| 6461 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6462 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6463 | return; |
| 6464 | |
| 6465 | /* Execute the function. */ |
| 6466 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6467 | /* finished. */ |
| 6468 | case HLUA_E_OK: |
| 6469 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6470 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6471 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6472 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6473 | res->flags |= CF_READ_NULL; |
| 6474 | si_shutr(si); |
| 6475 | return; |
| 6476 | |
| 6477 | /* yield. */ |
| 6478 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6479 | if (hlua->wake_time != TICK_ETERNITY) |
| 6480 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6481 | return; |
| 6482 | |
| 6483 | /* finished with error. */ |
| 6484 | case HLUA_E_ERRMSG: |
| 6485 | /* Display log. */ |
| 6486 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6487 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6488 | lua_pop(hlua->T, 1); |
| 6489 | goto error; |
| 6490 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6491 | case HLUA_E_ETMOUT: |
| 6492 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6493 | rule->arg.hlua_rule->fcn.name); |
| 6494 | goto error; |
| 6495 | |
| 6496 | case HLUA_E_NOMEM: |
| 6497 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6498 | rule->arg.hlua_rule->fcn.name); |
| 6499 | goto error; |
| 6500 | |
| 6501 | case HLUA_E_YIELD: /* unexpected */ |
| 6502 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6503 | rule->arg.hlua_rule->fcn.name); |
| 6504 | goto error; |
| 6505 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6506 | case HLUA_E_ERR: |
| 6507 | /* Display log. */ |
| 6508 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6509 | rule->arg.hlua_rule->fcn.name); |
| 6510 | goto error; |
| 6511 | |
| 6512 | default: |
| 6513 | goto error; |
| 6514 | } |
| 6515 | |
| 6516 | error: |
| 6517 | |
| 6518 | /* For all other cases, just close the stream. */ |
| 6519 | si_shutw(si); |
| 6520 | si_shutr(si); |
| 6521 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6522 | } |
| 6523 | |
| 6524 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6525 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6526 | task_delete(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6527 | task_free(ctx->ctx.hlua_apptcp.task); |
| 6528 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6529 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6530 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6531 | } |
| 6532 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6533 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6534 | * an errors occurs and -1 if more data are required for initializing |
| 6535 | * the applet. |
| 6536 | */ |
| 6537 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6538 | { |
| 6539 | struct stream_interface *si = ctx->owner; |
| 6540 | struct channel *req = si_oc(si); |
| 6541 | struct http_msg *msg; |
| 6542 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6543 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6544 | char **arg; |
| 6545 | struct hdr_ctx hdr; |
| 6546 | struct task *task; |
| 6547 | 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] | 6548 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6549 | |
| 6550 | /* Wait for a full HTTP request. */ |
| 6551 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 6552 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 6553 | return -1; |
| 6554 | return 0; |
| 6555 | } |
| 6556 | txn = strm->txn; |
| 6557 | msg = &txn->req; |
| 6558 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6559 | /* We want two things in HTTP mode : |
| 6560 | * - enforce server-close mode if we were in keep-alive, so that the |
| 6561 | * applet is released after each response ; |
| 6562 | * - enable request body transfer to the applet in order to resync |
| 6563 | * with the response body. |
| 6564 | */ |
| 6565 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 6566 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6567 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6568 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6569 | if (!hlua) { |
| 6570 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6571 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6572 | return 0; |
| 6573 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6574 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6575 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6576 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6577 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6578 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6579 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6580 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6581 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6582 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6583 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6584 | if (!task) { |
| 6585 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6586 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6587 | return 0; |
| 6588 | } |
| 6589 | task->nice = 0; |
| 6590 | task->context = ctx; |
| 6591 | task->process = hlua_applet_wakeup; |
| 6592 | ctx->ctx.hlua_apphttp.task = task; |
| 6593 | |
| 6594 | /* In the execution wrappers linked with a stream, the |
| 6595 | * Lua context can be not initialized. This behavior |
| 6596 | * permits to save performances because a systematic |
| 6597 | * Lua initialization cause 5% performances loss. |
| 6598 | */ |
| 6599 | if (!hlua_ctx_init(hlua, task)) { |
| 6600 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6601 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6602 | return 0; |
| 6603 | } |
| 6604 | |
| 6605 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6606 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6607 | |
| 6608 | /* The following Lua calls can fail. */ |
| 6609 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6610 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6611 | error = lua_tostring(hlua->T, -1); |
| 6612 | else |
| 6613 | error = "critical error"; |
| 6614 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6615 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6616 | return 0; |
| 6617 | } |
| 6618 | |
| 6619 | /* Check stack available size. */ |
| 6620 | if (!lua_checkstack(hlua->T, 1)) { |
| 6621 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6622 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6623 | RESET_SAFE_LJMP(hlua->T); |
| 6624 | return 0; |
| 6625 | } |
| 6626 | |
| 6627 | /* Restore the function in the stack. */ |
| 6628 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6629 | |
| 6630 | /* Create and and push object stream in the stack. */ |
| 6631 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6632 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6633 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6634 | RESET_SAFE_LJMP(hlua->T); |
| 6635 | return 0; |
| 6636 | } |
| 6637 | hlua->nargs = 1; |
| 6638 | |
| 6639 | /* Look for a 100-continue expected. */ |
| 6640 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 6641 | hdr.idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6642 | 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] | 6643 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 6644 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 6645 | } |
| 6646 | |
| 6647 | /* push keywords in the stack. */ |
| 6648 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6649 | if (!lua_checkstack(hlua->T, 1)) { |
| 6650 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6651 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6652 | RESET_SAFE_LJMP(hlua->T); |
| 6653 | return 0; |
| 6654 | } |
| 6655 | lua_pushstring(hlua->T, *arg); |
| 6656 | hlua->nargs++; |
| 6657 | } |
| 6658 | |
| 6659 | RESET_SAFE_LJMP(hlua->T); |
| 6660 | |
| 6661 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6662 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6663 | |
| 6664 | return 1; |
| 6665 | } |
| 6666 | |
| 6667 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 6668 | { |
| 6669 | struct stream_interface *si = ctx->owner; |
| 6670 | struct stream *strm = si_strm(si); |
| 6671 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6672 | struct act_rule *rule = ctx->rule; |
| 6673 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6674 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 6675 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 6676 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 6677 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 6678 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6679 | int ret; |
| 6680 | |
| 6681 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6682 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6683 | return; |
| 6684 | |
| 6685 | /* Set the currently running flag. */ |
| 6686 | if (!HLUA_IS_RUNNING(hlua) && |
| 6687 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6688 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6689 | /* Wait for full HTTP analysys. */ |
| 6690 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6691 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6692 | return; |
| 6693 | } |
| 6694 | |
| 6695 | /* Store the max amount of bytes that we can read. */ |
| 6696 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 6697 | |
| 6698 | /* We need to flush the request header. This left the body |
| 6699 | * for the Lua. |
| 6700 | */ |
| 6701 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6702 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6703 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6704 | if (ret == -1) |
| 6705 | return; |
| 6706 | |
| 6707 | /* No data available, ask for more data. */ |
| 6708 | if (ret == 1) |
| 6709 | len2 = 0; |
| 6710 | if (ret == 0) |
| 6711 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 6712 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6713 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6714 | return; |
| 6715 | } |
| 6716 | |
| 6717 | /* skip the requests bytes. */ |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 6718 | 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] | 6719 | } |
| 6720 | |
| 6721 | /* Executes The applet if it is not done. */ |
| 6722 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6723 | |
| 6724 | /* Execute the function. */ |
| 6725 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6726 | /* finished. */ |
| 6727 | case HLUA_E_OK: |
| 6728 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6729 | break; |
| 6730 | |
| 6731 | /* yield. */ |
| 6732 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6733 | if (hlua->wake_time != TICK_ETERNITY) |
| 6734 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6735 | return; |
| 6736 | |
| 6737 | /* finished with error. */ |
| 6738 | case HLUA_E_ERRMSG: |
| 6739 | /* Display log. */ |
| 6740 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6741 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6742 | lua_pop(hlua->T, 1); |
| 6743 | goto error; |
| 6744 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6745 | case HLUA_E_ETMOUT: |
| 6746 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 6747 | rule->arg.hlua_rule->fcn.name); |
| 6748 | goto error; |
| 6749 | |
| 6750 | case HLUA_E_NOMEM: |
| 6751 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 6752 | rule->arg.hlua_rule->fcn.name); |
| 6753 | goto error; |
| 6754 | |
| 6755 | case HLUA_E_YIELD: /* unexpected */ |
| 6756 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 6757 | rule->arg.hlua_rule->fcn.name); |
| 6758 | goto error; |
| 6759 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6760 | case HLUA_E_ERR: |
| 6761 | /* Display log. */ |
| 6762 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6763 | rule->arg.hlua_rule->fcn.name); |
| 6764 | goto error; |
| 6765 | |
| 6766 | default: |
| 6767 | goto error; |
| 6768 | } |
| 6769 | } |
| 6770 | |
| 6771 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 6772 | |
| 6773 | /* We must send the final chunk. */ |
| 6774 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 6775 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 6776 | |
| 6777 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6778 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6779 | |
| 6780 | /* critical error. */ |
| 6781 | if (ret == -2 || ret == -3) { |
| 6782 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 6783 | rule->arg.hlua_rule->fcn.name); |
| 6784 | goto error; |
| 6785 | } |
| 6786 | |
| 6787 | /* no enough space error. */ |
| 6788 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6789 | si_rx_room_blk(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6790 | return; |
| 6791 | } |
| 6792 | |
| 6793 | /* set the last chunk sent. */ |
| 6794 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6795 | } |
| 6796 | |
| 6797 | /* close the connection. */ |
| 6798 | |
Frédéric Lécaille | 83ed5d5 | 2018-07-18 14:25:26 +0200 | [diff] [blame] | 6799 | /* status */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6800 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6801 | |
| 6802 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6803 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6804 | res->flags |= CF_READ_NULL; |
| 6805 | si_shutr(si); |
| 6806 | |
| 6807 | return; |
| 6808 | } |
| 6809 | |
| 6810 | error: |
| 6811 | |
| 6812 | /* If we are in HTTP mode, and we are not send any |
| 6813 | * data, return a 500 server error in best effort: |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6814 | * if there is no room available in the buffer, |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6815 | * just close the connection. |
| 6816 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6817 | ci_putblk(res, error_500, strlen(error_500)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6818 | if (!(strm->flags & SF_ERR_MASK)) |
| 6819 | strm->flags |= SF_ERR_RESOURCE; |
| 6820 | si_shutw(si); |
| 6821 | si_shutr(si); |
| 6822 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6823 | } |
| 6824 | |
| 6825 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6826 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6827 | task_delete(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6828 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6829 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6830 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6831 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6832 | } |
| 6833 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6834 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6835 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6836 | * |
| 6837 | * This function can fail with an abort() due to an Lua critical error. |
| 6838 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6839 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6840 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6841 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6842 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6843 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6844 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6845 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6846 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6847 | if (px->options2 & PR_O2_USE_HTX) { |
| 6848 | memprintf(err, "Lua actions cannot be used when the HTX internal representation is enabled"); |
| 6849 | return ACT_RET_PRS_ERR; |
| 6850 | } |
| 6851 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6852 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6853 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6854 | if (!rule->arg.hlua_rule) { |
| 6855 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6856 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6857 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6858 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6859 | /* Memory for arguments. */ |
| 6860 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6861 | if (!rule->arg.hlua_rule->args) { |
| 6862 | memprintf(err, "out of memory error"); |
| 6863 | return ACT_RET_PRS_ERR; |
| 6864 | } |
| 6865 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6866 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6867 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6868 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6869 | /* Expect some arguments */ |
| 6870 | for (i = 0; i < fcn->nargs; i++) { |
| 6871 | if (*args[i+1] == '\0') { |
| 6872 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6873 | return ACT_RET_PRS_ERR; |
| 6874 | } |
| 6875 | rule->arg.hlua_rule->args[i] = strdup(args[i + 1]); |
| 6876 | if (!rule->arg.hlua_rule->args[i]) { |
| 6877 | memprintf(err, "out of memory error"); |
| 6878 | return ACT_RET_PRS_ERR; |
| 6879 | } |
| 6880 | (*cur_arg)++; |
| 6881 | } |
| 6882 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6883 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6884 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6885 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6886 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6887 | } |
| 6888 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6889 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6890 | struct act_rule *rule, char **err) |
| 6891 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6892 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6893 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6894 | if (px->options2 & PR_O2_USE_HTX) { |
| 6895 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 6896 | return ACT_RET_PRS_ERR; |
| 6897 | } |
| 6898 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6899 | /* HTTP applets are forbidden in tcp-request rules. |
| 6900 | * HTTP applet request requires everything initilized by |
| 6901 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6902 | * The applet will be immediately initilized, but its before |
| 6903 | * the call of this analyzer. |
| 6904 | */ |
| 6905 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6906 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6907 | return ACT_RET_PRS_ERR; |
| 6908 | } |
| 6909 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6910 | /* Memory for the rule. */ |
| 6911 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6912 | if (!rule->arg.hlua_rule) { |
| 6913 | memprintf(err, "out of memory error"); |
| 6914 | return ACT_RET_PRS_ERR; |
| 6915 | } |
| 6916 | |
| 6917 | /* Reference the Lua function and store the reference. */ |
| 6918 | rule->arg.hlua_rule->fcn = *fcn; |
| 6919 | |
| 6920 | /* TODO: later accept arguments. */ |
| 6921 | rule->arg.hlua_rule->args = NULL; |
| 6922 | |
| 6923 | /* Add applet pointer in the rule. */ |
| 6924 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6925 | rule->applet.name = fcn->name; |
| 6926 | rule->applet.init = hlua_applet_http_init; |
| 6927 | rule->applet.fct = hlua_applet_http_fct; |
| 6928 | rule->applet.release = hlua_applet_http_release; |
| 6929 | rule->applet.timeout = hlua_timeout_applet; |
| 6930 | |
| 6931 | return ACT_RET_PRS_OK; |
| 6932 | } |
| 6933 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6934 | /* This function is an LUA binding used for registering |
| 6935 | * "sample-conv" functions. It expects a converter name used |
| 6936 | * in the haproxy configuration file, and an LUA function. |
| 6937 | */ |
| 6938 | __LJMP static int hlua_register_action(lua_State *L) |
| 6939 | { |
| 6940 | struct action_kw_list *akl; |
| 6941 | const char *name; |
| 6942 | int ref; |
| 6943 | int len; |
| 6944 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6945 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6946 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6947 | /* Initialise the number of expected arguments at 0. */ |
| 6948 | nargs = 0; |
| 6949 | |
| 6950 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6951 | 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] | 6952 | |
| 6953 | /* First argument : converter name. */ |
| 6954 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6955 | |
| 6956 | /* Second argument : environment. */ |
| 6957 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6958 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6959 | |
| 6960 | /* Third argument : lua function. */ |
| 6961 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6962 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6963 | /* 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] | 6964 | if (lua_gettop(L) >= 4) |
| 6965 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6966 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6967 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6968 | lua_pushnil(L); |
| 6969 | while (lua_next(L, 2) != 0) { |
| 6970 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6971 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6972 | |
| 6973 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6974 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6975 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6976 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6977 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6978 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6979 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6980 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6981 | |
| 6982 | /* Fill fcn. */ |
| 6983 | fcn->name = strdup(name); |
| 6984 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6985 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6986 | fcn->function_ref = ref; |
| 6987 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6988 | /* Set the expected number od arguments. */ |
| 6989 | fcn->nargs = nargs; |
| 6990 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6991 | /* List head */ |
| 6992 | akl->list.n = akl->list.p = NULL; |
| 6993 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6994 | /* action keyword. */ |
| 6995 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6996 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6997 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6998 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6999 | |
| 7000 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7001 | |
| 7002 | akl->kw[0].match_pfx = 0; |
| 7003 | akl->kw[0].private = fcn; |
| 7004 | akl->kw[0].parse = action_register_lua; |
| 7005 | |
| 7006 | /* select the action registering point. */ |
| 7007 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7008 | tcp_req_cont_keywords_register(akl); |
| 7009 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7010 | tcp_res_cont_keywords_register(akl); |
| 7011 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7012 | http_req_keywords_register(akl); |
| 7013 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7014 | http_res_keywords_register(akl); |
| 7015 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7016 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7017 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7018 | "are expected.", lua_tostring(L, -1))); |
| 7019 | |
| 7020 | /* pop the environment string. */ |
| 7021 | lua_pop(L, 1); |
| 7022 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7023 | return ACT_RET_PRS_OK; |
| 7024 | } |
| 7025 | |
| 7026 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7027 | struct act_rule *rule, char **err) |
| 7028 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7029 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7030 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7031 | if (px->options2 & PR_O2_USE_HTX) { |
| 7032 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 7033 | return ACT_RET_PRS_ERR; |
| 7034 | } |
| 7035 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7036 | /* Memory for the rule. */ |
| 7037 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7038 | if (!rule->arg.hlua_rule) { |
| 7039 | memprintf(err, "out of memory error"); |
| 7040 | return ACT_RET_PRS_ERR; |
| 7041 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7042 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7043 | /* Reference the Lua function and store the reference. */ |
| 7044 | rule->arg.hlua_rule->fcn = *fcn; |
| 7045 | |
| 7046 | /* TODO: later accept arguments. */ |
| 7047 | rule->arg.hlua_rule->args = NULL; |
| 7048 | |
| 7049 | /* Add applet pointer in the rule. */ |
| 7050 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7051 | rule->applet.name = fcn->name; |
| 7052 | rule->applet.init = hlua_applet_tcp_init; |
| 7053 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7054 | rule->applet.release = hlua_applet_tcp_release; |
| 7055 | rule->applet.timeout = hlua_timeout_applet; |
| 7056 | |
| 7057 | return 0; |
| 7058 | } |
| 7059 | |
| 7060 | /* This function is an LUA binding used for registering |
| 7061 | * "sample-conv" functions. It expects a converter name used |
| 7062 | * in the haproxy configuration file, and an LUA function. |
| 7063 | */ |
| 7064 | __LJMP static int hlua_register_service(lua_State *L) |
| 7065 | { |
| 7066 | struct action_kw_list *akl; |
| 7067 | const char *name; |
| 7068 | const char *env; |
| 7069 | int ref; |
| 7070 | int len; |
| 7071 | struct hlua_function *fcn; |
| 7072 | |
| 7073 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7074 | |
| 7075 | /* First argument : converter name. */ |
| 7076 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7077 | |
| 7078 | /* Second argument : environment. */ |
| 7079 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7080 | |
| 7081 | /* Third argument : lua function. */ |
| 7082 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7083 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7084 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7085 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7086 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7087 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7088 | fcn = calloc(1, sizeof(*fcn)); |
| 7089 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7090 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7091 | |
| 7092 | /* Fill fcn. */ |
| 7093 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7094 | fcn->name = calloc(1, len); |
| 7095 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7096 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7097 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7098 | fcn->function_ref = ref; |
| 7099 | |
| 7100 | /* List head */ |
| 7101 | akl->list.n = akl->list.p = NULL; |
| 7102 | |
| 7103 | /* converter keyword. */ |
| 7104 | len = strlen("lua.") + strlen(name) + 1; |
| 7105 | akl->kw[0].kw = calloc(1, len); |
| 7106 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7107 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7108 | |
| 7109 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7110 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7111 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7112 | if (strcmp(env, "tcp") == 0) |
| 7113 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7114 | else if (strcmp(env, "http") == 0) |
| 7115 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7116 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7117 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7118 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7119 | |
| 7120 | akl->kw[0].match_pfx = 0; |
| 7121 | akl->kw[0].private = fcn; |
| 7122 | |
| 7123 | /* End of array. */ |
| 7124 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7125 | |
| 7126 | /* Register this new converter */ |
| 7127 | service_keywords_register(akl); |
| 7128 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7129 | return 0; |
| 7130 | } |
| 7131 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7132 | /* This function initialises Lua cli handler. It copies the |
| 7133 | * arguments in the Lua stack and create channel IO objects. |
| 7134 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7135 | 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] | 7136 | { |
| 7137 | struct hlua *hlua; |
| 7138 | struct hlua_function *fcn; |
| 7139 | int i; |
| 7140 | const char *error; |
| 7141 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7142 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7143 | appctx->ctx.hlua_cli.fcn = private; |
| 7144 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7145 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7146 | if (!hlua) { |
| 7147 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7148 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7149 | } |
| 7150 | HLUA_INIT(hlua); |
| 7151 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7152 | |
| 7153 | /* Create task used by signal to wakeup applets. |
| 7154 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7155 | * applet_http. It is absolutely compatible. |
| 7156 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7157 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7158 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7159 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7160 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7161 | } |
| 7162 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7163 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7164 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7165 | |
| 7166 | /* Initialises the Lua context */ |
| 7167 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 7168 | 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] | 7169 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7170 | } |
| 7171 | |
| 7172 | /* The following Lua calls can fail. */ |
| 7173 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7174 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7175 | error = lua_tostring(hlua->T, -1); |
| 7176 | else |
| 7177 | error = "critical error"; |
| 7178 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7179 | goto error; |
| 7180 | } |
| 7181 | |
| 7182 | /* Check stack available size. */ |
| 7183 | if (!lua_checkstack(hlua->T, 2)) { |
| 7184 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7185 | goto error; |
| 7186 | } |
| 7187 | |
| 7188 | /* Restore the function in the stack. */ |
| 7189 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7190 | |
| 7191 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7192 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7193 | */ |
| 7194 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7195 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7196 | goto error; |
| 7197 | } |
| 7198 | hlua->nargs = 1; |
| 7199 | |
| 7200 | /* push keywords in the stack. */ |
| 7201 | for (i = 0; *args[i]; i++) { |
| 7202 | /* Check stack available size. */ |
| 7203 | if (!lua_checkstack(hlua->T, 1)) { |
| 7204 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7205 | goto error; |
| 7206 | } |
| 7207 | lua_pushstring(hlua->T, args[i]); |
| 7208 | hlua->nargs++; |
| 7209 | } |
| 7210 | |
| 7211 | /* We must initialize the execution timeouts. */ |
| 7212 | hlua->max_time = hlua_timeout_session; |
| 7213 | |
| 7214 | /* At this point the execution is safe. */ |
| 7215 | RESET_SAFE_LJMP(hlua->T); |
| 7216 | |
| 7217 | /* It's ok */ |
| 7218 | return 0; |
| 7219 | |
| 7220 | /* It's not ok. */ |
| 7221 | error: |
| 7222 | RESET_SAFE_LJMP(hlua->T); |
| 7223 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7224 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7225 | return 1; |
| 7226 | } |
| 7227 | |
| 7228 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7229 | { |
| 7230 | struct hlua *hlua; |
| 7231 | struct stream_interface *si; |
| 7232 | struct hlua_function *fcn; |
| 7233 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7234 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7235 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7236 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7237 | |
| 7238 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7239 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7240 | return 1; |
| 7241 | |
| 7242 | /* Execute the function. */ |
| 7243 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7244 | |
| 7245 | /* finished. */ |
| 7246 | case HLUA_E_OK: |
| 7247 | return 1; |
| 7248 | |
| 7249 | /* yield. */ |
| 7250 | case HLUA_E_AGAIN: |
| 7251 | /* We want write. */ |
| 7252 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7253 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7254 | /* Set the timeout. */ |
| 7255 | if (hlua->wake_time != TICK_ETERNITY) |
| 7256 | task_schedule(hlua->task, hlua->wake_time); |
| 7257 | return 0; |
| 7258 | |
| 7259 | /* finished with error. */ |
| 7260 | case HLUA_E_ERRMSG: |
| 7261 | /* Display log. */ |
| 7262 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7263 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7264 | lua_pop(hlua->T, 1); |
| 7265 | return 1; |
| 7266 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7267 | case HLUA_E_ETMOUT: |
| 7268 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7269 | fcn->name); |
| 7270 | return 1; |
| 7271 | |
| 7272 | case HLUA_E_NOMEM: |
| 7273 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7274 | fcn->name); |
| 7275 | return 1; |
| 7276 | |
| 7277 | case HLUA_E_YIELD: /* unexpected */ |
| 7278 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7279 | fcn->name); |
| 7280 | return 1; |
| 7281 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7282 | case HLUA_E_ERR: |
| 7283 | /* Display log. */ |
| 7284 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7285 | fcn->name); |
| 7286 | return 1; |
| 7287 | |
| 7288 | default: |
| 7289 | return 1; |
| 7290 | } |
| 7291 | |
| 7292 | return 1; |
| 7293 | } |
| 7294 | |
| 7295 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7296 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7297 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7298 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7299 | } |
| 7300 | |
| 7301 | /* This function is an LUA binding used for registering |
| 7302 | * new keywords in the cli. It expects a list of keywords |
| 7303 | * which are the "path". It is limited to 5 keywords. A |
| 7304 | * description of the command, a function to be executed |
| 7305 | * for the parsing and a function for io handlers. |
| 7306 | */ |
| 7307 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7308 | { |
| 7309 | struct cli_kw_list *cli_kws; |
| 7310 | const char *message; |
| 7311 | int ref_io; |
| 7312 | int len; |
| 7313 | struct hlua_function *fcn; |
| 7314 | int index; |
| 7315 | int i; |
| 7316 | |
| 7317 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7318 | |
| 7319 | /* First argument : an array of maximum 5 keywords. */ |
| 7320 | if (!lua_istable(L, 1)) |
| 7321 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7322 | |
| 7323 | /* Second argument : string with contextual message. */ |
| 7324 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7325 | |
| 7326 | /* Third and fourth argument : lua function. */ |
| 7327 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7328 | |
| 7329 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7330 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7331 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7332 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7333 | fcn = calloc(1, sizeof(*fcn)); |
| 7334 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7335 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7336 | |
| 7337 | /* Fill path. */ |
| 7338 | index = 0; |
| 7339 | lua_pushnil(L); |
| 7340 | while(lua_next(L, 1) != 0) { |
| 7341 | if (index >= 5) |
| 7342 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7343 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7344 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7345 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7346 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7347 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7348 | index++; |
| 7349 | lua_pop(L, 1); |
| 7350 | } |
| 7351 | |
| 7352 | /* Copy help message. */ |
| 7353 | cli_kws->kw[0].usage = strdup(message); |
| 7354 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7355 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7356 | |
| 7357 | /* Fill fcn io handler. */ |
| 7358 | len = strlen("<lua.cli>") + 1; |
| 7359 | for (i = 0; i < index; i++) |
| 7360 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7361 | fcn->name = calloc(1, len); |
| 7362 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7363 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7364 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7365 | for (i = 0; i < index; i++) { |
| 7366 | strncat((char *)fcn->name, ".", len); |
| 7367 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7368 | } |
| 7369 | strncat((char *)fcn->name, ">", len); |
| 7370 | fcn->function_ref = ref_io; |
| 7371 | |
| 7372 | /* Fill last entries. */ |
| 7373 | cli_kws->kw[0].private = fcn; |
| 7374 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7375 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7376 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7377 | |
| 7378 | /* Register this new converter */ |
| 7379 | cli_register_kw(cli_kws); |
| 7380 | |
| 7381 | return 0; |
| 7382 | } |
| 7383 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7384 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7385 | struct proxy *defpx, const char *file, int line, |
| 7386 | char **err, unsigned int *timeout) |
| 7387 | { |
| 7388 | const char *error; |
| 7389 | |
| 7390 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 7391 | if (error && *error != '\0') { |
| 7392 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7393 | return -1; |
| 7394 | } |
| 7395 | return 0; |
| 7396 | } |
| 7397 | |
| 7398 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7399 | struct proxy *defpx, const char *file, int line, |
| 7400 | char **err) |
| 7401 | { |
| 7402 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7403 | file, line, err, &hlua_timeout_session); |
| 7404 | } |
| 7405 | |
| 7406 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7407 | struct proxy *defpx, const char *file, int line, |
| 7408 | char **err) |
| 7409 | { |
| 7410 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7411 | file, line, err, &hlua_timeout_task); |
| 7412 | } |
| 7413 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7414 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7415 | struct proxy *defpx, const char *file, int line, |
| 7416 | char **err) |
| 7417 | { |
| 7418 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7419 | file, line, err, &hlua_timeout_applet); |
| 7420 | } |
| 7421 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7422 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7423 | struct proxy *defpx, const char *file, int line, |
| 7424 | char **err) |
| 7425 | { |
| 7426 | char *error; |
| 7427 | |
| 7428 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7429 | if (*error != '\0') { |
| 7430 | memprintf(err, "%s: invalid number", args[0]); |
| 7431 | return -1; |
| 7432 | } |
| 7433 | return 0; |
| 7434 | } |
| 7435 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7436 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7437 | struct proxy *defpx, const char *file, int line, |
| 7438 | char **err) |
| 7439 | { |
| 7440 | char *error; |
| 7441 | |
| 7442 | if (*(args[1]) == 0) { |
| 7443 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7444 | return -1; |
| 7445 | } |
| 7446 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7447 | if (*error != '\0') { |
| 7448 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7449 | return -1; |
| 7450 | } |
| 7451 | return 0; |
| 7452 | } |
| 7453 | |
| 7454 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7455 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7456 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7457 | * the main lua entry point. |
| 7458 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7459 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7460 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7461 | * |
| 7462 | * In some error case, LUA set an error message in top of the stack. This function |
| 7463 | * 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] | 7464 | * |
| 7465 | * This function can fail with an abort() due to an Lua critical error. |
| 7466 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7467 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7468 | */ |
| 7469 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7470 | struct proxy *defpx, const char *file, int line, |
| 7471 | char **err) |
| 7472 | { |
| 7473 | int error; |
| 7474 | |
| 7475 | /* Just load and compile the file. */ |
| 7476 | error = luaL_loadfile(gL.T, args[1]); |
| 7477 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7478 | 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] | 7479 | lua_pop(gL.T, 1); |
| 7480 | return -1; |
| 7481 | } |
| 7482 | |
| 7483 | /* If no syntax error where detected, execute the code. */ |
| 7484 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7485 | switch (error) { |
| 7486 | case LUA_OK: |
| 7487 | break; |
| 7488 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7489 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7490 | lua_pop(gL.T, 1); |
| 7491 | return -1; |
| 7492 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7493 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7494 | return -1; |
| 7495 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7496 | 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] | 7497 | lua_pop(gL.T, 1); |
| 7498 | return -1; |
| 7499 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7500 | 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] | 7501 | lua_pop(gL.T, 1); |
| 7502 | return -1; |
| 7503 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7504 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7505 | lua_pop(gL.T, 1); |
| 7506 | return -1; |
| 7507 | } |
| 7508 | |
| 7509 | return 0; |
| 7510 | } |
| 7511 | |
| 7512 | /* configuration keywords declaration */ |
| 7513 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7514 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7515 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7516 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7517 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7518 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7519 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7520 | { 0, NULL, NULL }, |
| 7521 | }}; |
| 7522 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7523 | static int hlua_check_config() |
| 7524 | { |
| 7525 | struct proxy *px; |
| 7526 | struct acl *acl; |
| 7527 | struct acl_expr *expr; |
| 7528 | struct sample_fetch *fetch; |
| 7529 | int err = 0; |
| 7530 | |
| 7531 | for (px = proxies_list; px; px = px->next) { |
| 7532 | if (!(px->options2 & PR_O2_USE_HTX)) |
| 7533 | continue; |
| 7534 | |
| 7535 | list_for_each_entry(acl, &px->acl, list) { |
| 7536 | list_for_each_entry(expr, &acl->expr, list) { |
| 7537 | fetch = expr->smp->fetch; |
| 7538 | if (fetch->process != hlua_sample_fetch_wrapper) |
| 7539 | continue; |
| 7540 | |
| 7541 | ha_alert("config: %s '%s': sample-fetch '%s' cannot be used used " |
| 7542 | "when the HTX internal representation is enabled.\n", |
| 7543 | proxy_type_str(px), px->id, fetch->kw); |
| 7544 | err++; |
| 7545 | } |
| 7546 | } |
| 7547 | } |
| 7548 | return err; |
| 7549 | } |
| 7550 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7551 | /* This function can fail with an abort() due to an Lua critical error. |
| 7552 | * We are in the initialisation process of HAProxy, this abort() is |
| 7553 | * tolerated. |
| 7554 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7555 | int hlua_post_init() |
| 7556 | { |
| 7557 | struct hlua_init_function *init; |
| 7558 | const char *msg; |
| 7559 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7560 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7561 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7562 | /* Call post initialisation function in safe environement. */ |
| 7563 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7564 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7565 | error = lua_tostring(gL.T, -1); |
| 7566 | else |
| 7567 | error = "critical error"; |
| 7568 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7569 | exit(1); |
| 7570 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7571 | |
| 7572 | #if USE_OPENSSL |
| 7573 | /* Initialize SSL server. */ |
| 7574 | if (socket_ssl.xprt->prepare_srv) { |
| 7575 | int saved_used_backed = global.ssl_used_backend; |
| 7576 | // don't affect maxconn automatic computation |
| 7577 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7578 | global.ssl_used_backend = saved_used_backed; |
| 7579 | } |
| 7580 | #endif |
| 7581 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7582 | hlua_fcn_post_init(gL.T); |
| 7583 | RESET_SAFE_LJMP(gL.T); |
| 7584 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7585 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7586 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7587 | ret = hlua_ctx_resume(&gL, 0); |
| 7588 | switch (ret) { |
| 7589 | case HLUA_E_OK: |
| 7590 | lua_pop(gL.T, -1); |
| 7591 | return 1; |
| 7592 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7593 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7594 | return 0; |
| 7595 | case HLUA_E_ERRMSG: |
| 7596 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7597 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7598 | return 0; |
| 7599 | case HLUA_E_ERR: |
| 7600 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7601 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7602 | return 0; |
| 7603 | } |
| 7604 | } |
| 7605 | return 1; |
| 7606 | } |
| 7607 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7608 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7609 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7610 | * is the previously allocated size or the kind of object in case of a new |
| 7611 | * allocation. <nsize> is the requested new size. |
| 7612 | */ |
| 7613 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7614 | { |
| 7615 | struct hlua_mem_allocator *zone = ud; |
| 7616 | |
| 7617 | if (nsize == 0) { |
| 7618 | /* it's a free */ |
| 7619 | if (ptr) |
| 7620 | zone->allocated -= osize; |
| 7621 | free(ptr); |
| 7622 | return NULL; |
| 7623 | } |
| 7624 | |
| 7625 | if (!ptr) { |
| 7626 | /* it's a new allocation */ |
| 7627 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7628 | return NULL; |
| 7629 | |
| 7630 | ptr = malloc(nsize); |
| 7631 | if (ptr) |
| 7632 | zone->allocated += nsize; |
| 7633 | return ptr; |
| 7634 | } |
| 7635 | |
| 7636 | /* it's a realloc */ |
| 7637 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7638 | return NULL; |
| 7639 | |
| 7640 | ptr = realloc(ptr, nsize); |
| 7641 | if (ptr) |
| 7642 | zone->allocated += nsize - osize; |
| 7643 | return ptr; |
| 7644 | } |
| 7645 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7646 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7647 | * We are in the initialisation process of HAProxy, this abort() is |
| 7648 | * tolerated. |
| 7649 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7650 | void hlua_init(void) |
| 7651 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7652 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7653 | int idx; |
| 7654 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7655 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7656 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7657 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7658 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7659 | struct srv_kw *kw; |
| 7660 | int tmp_error; |
| 7661 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7662 | char *args[] = { /* SSL client configuration. */ |
| 7663 | "ssl", |
| 7664 | "verify", |
| 7665 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7666 | NULL |
| 7667 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7668 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7669 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7670 | HA_SPIN_INIT(&hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 7671 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7672 | /* Initialise struct hlua and com signals pool */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7673 | pool_head_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7674 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7675 | /* Register configuration keywords. */ |
| 7676 | cfg_register_keywords(&cfg_kws); |
| 7677 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7678 | /* Init main lua stack. */ |
| 7679 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7680 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7681 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7682 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7683 | hlua_sethlua(&gL); |
| 7684 | gL.Tref = LUA_REFNIL; |
| 7685 | gL.task = NULL; |
| 7686 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7687 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7688 | * the Lua function can fail with an abort. We are in the initialisation |
| 7689 | * process of HAProxy, this abort() is tolerated. |
| 7690 | */ |
| 7691 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7692 | /* Initialise lua. */ |
| 7693 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7694 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7695 | /* Set safe environment for the initialisation. */ |
| 7696 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7697 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7698 | error_msg = lua_tostring(gL.T, -1); |
| 7699 | else |
| 7700 | error_msg = "critical error"; |
| 7701 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7702 | exit(1); |
| 7703 | } |
| 7704 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7705 | /* |
| 7706 | * |
| 7707 | * Create "core" object. |
| 7708 | * |
| 7709 | */ |
| 7710 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7711 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7712 | lua_newtable(gL.T); |
| 7713 | |
| 7714 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7715 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7716 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7717 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7718 | /* Register special functions. */ |
| 7719 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7720 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7721 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7722 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7723 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7724 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7725 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7726 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7727 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7728 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7729 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7730 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7731 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7732 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7733 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7734 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7735 | hlua_class_function(gL.T, "log", hlua_log); |
| 7736 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7737 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7738 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7739 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7740 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7741 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7742 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7743 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7744 | |
| 7745 | /* |
| 7746 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7747 | * Register class Map |
| 7748 | * |
| 7749 | */ |
| 7750 | |
| 7751 | /* This table entry is the object "Map" base. */ |
| 7752 | lua_newtable(gL.T); |
| 7753 | |
| 7754 | /* register pattern types. */ |
| 7755 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7756 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7757 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7758 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 7759 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7760 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7761 | |
| 7762 | /* register constructor. */ |
| 7763 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7764 | |
| 7765 | /* Create and fill the metatable. */ |
| 7766 | lua_newtable(gL.T); |
| 7767 | |
| 7768 | /* Create and fille the __index entry. */ |
| 7769 | lua_pushstring(gL.T, "__index"); |
| 7770 | lua_newtable(gL.T); |
| 7771 | |
| 7772 | /* Register . */ |
| 7773 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7774 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7775 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7776 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7777 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7778 | /* Register previous table in the registry with reference and named entry. |
| 7779 | * The function hlua_register_metatable() pops the stack, so we |
| 7780 | * previously create a copy of the table. |
| 7781 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7782 | 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] | 7783 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7784 | |
| 7785 | /* Assign the metatable to the mai Map object. */ |
| 7786 | lua_setmetatable(gL.T, -2); |
| 7787 | |
| 7788 | /* Set a name to the table. */ |
| 7789 | lua_setglobal(gL.T, "Map"); |
| 7790 | |
| 7791 | /* |
| 7792 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7793 | * Register class Channel |
| 7794 | * |
| 7795 | */ |
| 7796 | |
| 7797 | /* Create and fill the metatable. */ |
| 7798 | lua_newtable(gL.T); |
| 7799 | |
| 7800 | /* Create and fille the __index entry. */ |
| 7801 | lua_pushstring(gL.T, "__index"); |
| 7802 | lua_newtable(gL.T); |
| 7803 | |
| 7804 | /* Register . */ |
| 7805 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7806 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7807 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7808 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7809 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7810 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7811 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7812 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7813 | 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] | 7814 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7815 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7816 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7817 | |
| 7818 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7819 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7820 | |
| 7821 | /* |
| 7822 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7823 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7824 | * |
| 7825 | */ |
| 7826 | |
| 7827 | /* Create and fill the metatable. */ |
| 7828 | lua_newtable(gL.T); |
| 7829 | |
| 7830 | /* Create and fille the __index entry. */ |
| 7831 | lua_pushstring(gL.T, "__index"); |
| 7832 | lua_newtable(gL.T); |
| 7833 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7834 | /* Browse existing fetches and create the associated |
| 7835 | * object method. |
| 7836 | */ |
| 7837 | sf = NULL; |
| 7838 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7839 | |
| 7840 | /* Dont register the keywork if the arguments check function are |
| 7841 | * not safe during the runtime. |
| 7842 | */ |
| 7843 | if ((sf->val_args != NULL) && |
| 7844 | (sf->val_args != val_payload_lv) && |
| 7845 | (sf->val_args != val_hdr)) |
| 7846 | continue; |
| 7847 | |
| 7848 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7849 | * by an underscore. |
| 7850 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7851 | strncpy(trash.area, sf->kw, trash.size); |
| 7852 | trash.area[trash.size - 1] = '\0'; |
| 7853 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7854 | if (*p == '.' || *p == '-' || *p == '+') |
| 7855 | *p = '_'; |
| 7856 | |
| 7857 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7858 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7859 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7860 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7861 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7862 | } |
| 7863 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7864 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7865 | |
| 7866 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7867 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7868 | |
| 7869 | /* |
| 7870 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7871 | * Register class Converters |
| 7872 | * |
| 7873 | */ |
| 7874 | |
| 7875 | /* Create and fill the metatable. */ |
| 7876 | lua_newtable(gL.T); |
| 7877 | |
| 7878 | /* Create and fill the __index entry. */ |
| 7879 | lua_pushstring(gL.T, "__index"); |
| 7880 | lua_newtable(gL.T); |
| 7881 | |
| 7882 | /* Browse existing converters and create the associated |
| 7883 | * object method. |
| 7884 | */ |
| 7885 | sc = NULL; |
| 7886 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7887 | /* Dont register the keywork if the arguments check function are |
| 7888 | * not safe during the runtime. |
| 7889 | */ |
| 7890 | if (sc->val_args != NULL) |
| 7891 | continue; |
| 7892 | |
| 7893 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7894 | * by an underscore. |
| 7895 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7896 | strncpy(trash.area, sc->kw, trash.size); |
| 7897 | trash.area[trash.size - 1] = '\0'; |
| 7898 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7899 | if (*p == '.' || *p == '-' || *p == '+') |
| 7900 | *p = '_'; |
| 7901 | |
| 7902 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7903 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7904 | lua_pushlightuserdata(gL.T, sc); |
| 7905 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7906 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7907 | } |
| 7908 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7909 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7910 | |
| 7911 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7912 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7913 | |
| 7914 | /* |
| 7915 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7916 | * Register class HTTP |
| 7917 | * |
| 7918 | */ |
| 7919 | |
| 7920 | /* Create and fill the metatable. */ |
| 7921 | lua_newtable(gL.T); |
| 7922 | |
| 7923 | /* Create and fille the __index entry. */ |
| 7924 | lua_pushstring(gL.T, "__index"); |
| 7925 | lua_newtable(gL.T); |
| 7926 | |
| 7927 | /* Register Lua functions. */ |
| 7928 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7929 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7930 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7931 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7932 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7933 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7934 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7935 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7936 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7937 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7938 | |
| 7939 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7940 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7941 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7942 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7943 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7944 | 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] | 7945 | 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] | 7946 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7947 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7948 | |
| 7949 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7950 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7951 | |
| 7952 | /* |
| 7953 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7954 | * Register class AppletTCP |
| 7955 | * |
| 7956 | */ |
| 7957 | |
| 7958 | /* Create and fill the metatable. */ |
| 7959 | lua_newtable(gL.T); |
| 7960 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7961 | /* Create and fille the __index entry. */ |
| 7962 | lua_pushstring(gL.T, "__index"); |
| 7963 | lua_newtable(gL.T); |
| 7964 | |
| 7965 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7966 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7967 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7968 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7969 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7970 | 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] | 7971 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7972 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7973 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7974 | |
| 7975 | lua_settable(gL.T, -3); |
| 7976 | |
| 7977 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7978 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7979 | |
| 7980 | /* |
| 7981 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7982 | * Register class AppletHTTP |
| 7983 | * |
| 7984 | */ |
| 7985 | |
| 7986 | /* Create and fill the metatable. */ |
| 7987 | lua_newtable(gL.T); |
| 7988 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7989 | /* Create and fille the __index entry. */ |
| 7990 | lua_pushstring(gL.T, "__index"); |
| 7991 | lua_newtable(gL.T); |
| 7992 | |
| 7993 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7994 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7995 | 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] | 7996 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7997 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7998 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7999 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8000 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8001 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8002 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8003 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8004 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8005 | |
| 8006 | lua_settable(gL.T, -3); |
| 8007 | |
| 8008 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8009 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8010 | |
| 8011 | /* |
| 8012 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8013 | * Register class TXN |
| 8014 | * |
| 8015 | */ |
| 8016 | |
| 8017 | /* Create and fill the metatable. */ |
| 8018 | lua_newtable(gL.T); |
| 8019 | |
| 8020 | /* Create and fille the __index entry. */ |
| 8021 | lua_pushstring(gL.T, "__index"); |
| 8022 | lua_newtable(gL.T); |
| 8023 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8024 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8025 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8026 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8027 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8028 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8029 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8030 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 8031 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8032 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8033 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8034 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8035 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8036 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8037 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8038 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8039 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8040 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8041 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8042 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8043 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8044 | |
| 8045 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8046 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8047 | |
| 8048 | /* |
| 8049 | * |
| 8050 | * Register class Socket |
| 8051 | * |
| 8052 | */ |
| 8053 | |
| 8054 | /* Create and fill the metatable. */ |
| 8055 | lua_newtable(gL.T); |
| 8056 | |
| 8057 | /* Create and fille the __index entry. */ |
| 8058 | lua_pushstring(gL.T, "__index"); |
| 8059 | lua_newtable(gL.T); |
| 8060 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8061 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8062 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8063 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8064 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8065 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8066 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8067 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8068 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8069 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8070 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8071 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8072 | |
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 the garbage collector entry. */ |
| 8076 | lua_pushstring(gL.T, "__gc"); |
| 8077 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8078 | 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] | 8079 | |
| 8080 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8081 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8082 | |
| 8083 | /* Proxy and server configuration initialisation. */ |
| 8084 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8085 | init_new_proxy(&socket_proxy); |
| 8086 | socket_proxy.parent = NULL; |
| 8087 | socket_proxy.last_change = now.tv_sec; |
| 8088 | socket_proxy.id = "LUA-SOCKET"; |
| 8089 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8090 | socket_proxy.maxconn = 0; |
| 8091 | socket_proxy.accept = NULL; |
| 8092 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8093 | socket_proxy.srv = NULL; |
| 8094 | socket_proxy.conn_retries = 0; |
| 8095 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8096 | |
| 8097 | /* Init TCP server: unchanged parameters */ |
| 8098 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8099 | socket_tcp.next = NULL; |
| 8100 | socket_tcp.proxy = &socket_proxy; |
| 8101 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8102 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8103 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8104 | socket_tcp.priv_conns = NULL; |
| 8105 | socket_tcp.idle_conns = NULL; |
| 8106 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8107 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8108 | socket_tcp.last_change = 0; |
| 8109 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8110 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8111 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8112 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8113 | |
| 8114 | /* XXX: Copy default parameter from default server, |
| 8115 | * but the default server is not initialized. |
| 8116 | */ |
| 8117 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8118 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8119 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8120 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8121 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8122 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8123 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8124 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8125 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8126 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8127 | |
| 8128 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8129 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8130 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8131 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8132 | socket_tcp.check.server = &socket_tcp; |
| 8133 | |
| 8134 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8135 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8136 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8137 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8138 | socket_tcp.agent.server = &socket_tcp; |
| 8139 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8140 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8141 | |
| 8142 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8143 | /* Init TCP server: unchanged parameters */ |
| 8144 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8145 | socket_ssl.next = NULL; |
| 8146 | socket_ssl.proxy = &socket_proxy; |
| 8147 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8148 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8149 | socket_ssl.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8150 | socket_tcp.priv_conns = NULL; |
| 8151 | socket_tcp.idle_conns = NULL; |
| 8152 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8153 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8154 | socket_ssl.last_change = 0; |
| 8155 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8156 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8157 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8158 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8159 | |
| 8160 | /* XXX: Copy default parameter from default server, |
| 8161 | * but the default server is not initialized. |
| 8162 | */ |
| 8163 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8164 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8165 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8166 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8167 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8168 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8169 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8170 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8171 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8172 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8173 | |
| 8174 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8175 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8176 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8177 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8178 | socket_ssl.check.server = &socket_ssl; |
| 8179 | |
| 8180 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8181 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8182 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8183 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8184 | socket_ssl.agent.server = &socket_ssl; |
| 8185 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8186 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8187 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8188 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8189 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8190 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8191 | /* |
| 8192 | * |
| 8193 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8194 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8195 | * features like client certificates and ssl_verify. |
| 8196 | * |
| 8197 | */ |
| 8198 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8199 | if (tmp_error != 0) { |
| 8200 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8201 | abort(); /* This must be never arrives because the command line |
| 8202 | not editable by the user. */ |
| 8203 | } |
| 8204 | idx += kw->skip; |
| 8205 | } |
| 8206 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8207 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8208 | |
| 8209 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8210 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8211 | |
| 8212 | __attribute__((constructor)) |
| 8213 | static void __hlua_init(void) |
| 8214 | { |
| 8215 | char *ptr = NULL; |
| 8216 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8217 | hap_register_build_opts(ptr, 1); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8218 | cfg_register_postparser("hlua", hlua_check_config); |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8219 | } |