Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 14 | #include <limits.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 15 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 16 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 17 | #include <lauxlib.h> |
| 18 | #include <lua.h> |
| 19 | #include <lualib.h> |
| 20 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 21 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 22 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 23 | #endif |
| 24 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 25 | #include <ebpttree.h> |
| 26 | |
| 27 | #include <common/cfgparse.h> |
Willy Tarreau | b059b89 | 2018-10-16 17:57:36 +0200 | [diff] [blame] | 28 | #include <common/compiler.h> |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 29 | #include <common/hathreads.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 30 | #include <common/initcall.h> |
| 31 | #include <common/xref.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 32 | #include <common/h1.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 33 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 34 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 35 | #include <types/hlua.h> |
| 36 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 37 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 38 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 39 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 40 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 41 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 42 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 43 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 44 | #include <proto/stats.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 45 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 46 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 47 | #include <proto/hlua_fcn.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 48 | #include <proto/http_fetch.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 49 | #include <proto/http_htx.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 50 | #include <proto/http_rules.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 51 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 52 | #include <proto/obj_type.h> |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 53 | #include <proto/queue.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 54 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 55 | #include <proto/payload.h> |
| 56 | #include <proto/proto_http.h> |
| 57 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 58 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 59 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 60 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 61 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 62 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 63 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 64 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 65 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 66 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 67 | * macro is used only for identifying the function that can |
| 68 | * not return because a longjmp is executed. |
| 69 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 70 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 71 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 72 | */ |
| 73 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 74 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 75 | #define MAY_LJMP(func) func |
| 76 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 77 | /* This couple of function executes securely some Lua calls outside of |
| 78 | * the lua runtime environment. Each Lua call can return a longjmp |
| 79 | * if it encounter a memory error. |
| 80 | * |
| 81 | * Lua documentation extract: |
| 82 | * |
| 83 | * If an error happens outside any protected environment, Lua calls |
| 84 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 85 | * exiting the host application. Your panic function can avoid this |
| 86 | * exit by never returning (e.g., doing a long jump to your own |
| 87 | * recovery point outside Lua). |
| 88 | * |
| 89 | * The panic function runs as if it were a message handler (see |
| 90 | * §2.3); in particular, the error message is at the top of the |
| 91 | * stack. However, there is no guarantee about stack space. To push |
| 92 | * anything on the stack, the panic function must first check the |
| 93 | * available space (see §4.2). |
| 94 | * |
| 95 | * We must check all the Lua entry point. This includes: |
| 96 | * - The include/proto/hlua.h exported functions |
| 97 | * - the task wrapper function |
| 98 | * - The action wrapper function |
| 99 | * - The converters wrapper function |
| 100 | * - The sample-fetch wrapper functions |
| 101 | * |
| 102 | * It is tolerated that the initilisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 103 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 104 | * |
| 105 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 106 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 107 | * because they must be exists in the program stack when the longjmp |
| 108 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 109 | * |
| 110 | * Note that the Lua processing is not really thread safe. It provides |
| 111 | * heavy system which consists to add our own lock function in the Lua |
| 112 | * code and recompile the library. This system will probably not accepted |
| 113 | * by maintainers of various distribs. |
| 114 | * |
| 115 | * Our main excution point of the Lua is the function lua_resume(). A |
| 116 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 117 | * of function and a lua_unlock() at the end of the function. So I |
| 118 | * conclude that the Lua thread safe mode just perform a mutex around |
| 119 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 120 | * easier for distro maintainers. |
| 121 | * |
| 122 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 123 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 124 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 125 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 126 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 127 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 128 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 129 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 130 | |
| 131 | #define SET_SAFE_LJMP(__L) \ |
| 132 | ({ \ |
| 133 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 134 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 135 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 136 | lua_atpanic(__L, hlua_panic_safe); \ |
| 137 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 138 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 139 | } else { \ |
| 140 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 141 | ret = 1; \ |
| 142 | } \ |
| 143 | ret; \ |
| 144 | }) |
| 145 | |
| 146 | /* If we are the last function catching Lua errors, we |
| 147 | * must reset the panic function. |
| 148 | */ |
| 149 | #define RESET_SAFE_LJMP(__L) \ |
| 150 | do { \ |
| 151 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 152 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 153 | } while(0) |
| 154 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 155 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 156 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 157 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 158 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 159 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 160 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 161 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 162 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 163 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 164 | /* The main Lua execution context. */ |
| 165 | struct hlua gL; |
| 166 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 167 | /* This is the memory pool containing struct lua for applets |
| 168 | * (including cli). |
| 169 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 170 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 171 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 172 | /* Used for Socket connection. */ |
| 173 | static struct proxy socket_proxy; |
| 174 | static struct server socket_tcp; |
| 175 | #ifdef USE_OPENSSL |
| 176 | static struct server socket_ssl; |
| 177 | #endif |
| 178 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 179 | /* List head of the function called at the initialisation time. */ |
| 180 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 181 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 182 | /* The following variables contains the reference of the different |
| 183 | * Lua classes. These references are useful for identify metadata |
| 184 | * associated with an object. |
| 185 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 186 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 187 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 188 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 189 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 190 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 191 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 192 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 193 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 194 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 195 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 196 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 197 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 198 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 199 | * because a task may remain alive during all the haproxy execution. |
| 200 | */ |
| 201 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 202 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 203 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 204 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 205 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 206 | * it is used for preventing infinite loops. |
| 207 | * |
| 208 | * I test the scheer with an infinite loop containing one incrementation |
| 209 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 210 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 211 | * to one interrupt each 10 000 instructions. |
| 212 | * |
| 213 | * configured | Number of |
| 214 | * instructions | loops executed |
| 215 | * between two | in milions |
| 216 | * forced yields | |
| 217 | * ---------------+--------------- |
| 218 | * 10 | 160 |
| 219 | * 500 | 670 |
| 220 | * 1000 | 680 |
| 221 | * 5000 | 700 |
| 222 | * 7000 | 700 |
| 223 | * 8000 | 700 |
| 224 | * 9000 | 710 <- ceil |
| 225 | * 10000 | 710 |
| 226 | * 100000 | 710 |
| 227 | * 1000000 | 710 |
| 228 | * |
| 229 | */ |
| 230 | static unsigned int hlua_nb_instruction = 10000; |
| 231 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 232 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 233 | * be enforced on any memory allocation. |
| 234 | */ |
| 235 | struct hlua_mem_allocator { |
| 236 | size_t allocated; |
| 237 | size_t limit; |
| 238 | }; |
| 239 | |
| 240 | static struct hlua_mem_allocator hlua_global_allocator; |
| 241 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 242 | static const char error_500[] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 243 | "HTTP/1.0 500 Internal Server Error\r\n" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 244 | "Cache-Control: no-cache\r\n" |
| 245 | "Connection: close\r\n" |
| 246 | "Content-Type: text/html\r\n" |
| 247 | "\r\n" |
Joseph Herlant | 7fe1577 | 2018-11-15 10:07:32 -0800 | [diff] [blame] | 248 | "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n"; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 249 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 250 | /* These functions converts types between HAProxy internal args or |
| 251 | * sample and LUA types. Another function permits to check if the |
| 252 | * LUA stack contains arguments according with an required ARG_T |
| 253 | * format. |
| 254 | */ |
| 255 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 256 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 257 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 258 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 259 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 260 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 261 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 262 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 263 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 264 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 265 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 266 | do { \ |
| 267 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 268 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 269 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 270 | } while (0) |
| 271 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 272 | /* Used to check an Lua function type in the stack. It creates and |
| 273 | * returns a reference of the function. This function throws an |
| 274 | * error if the rgument is not a "function". |
| 275 | */ |
| 276 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 277 | { |
| 278 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 279 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno)); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 280 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 281 | } |
| 282 | lua_pushvalue(L, argno); |
| 283 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 284 | } |
| 285 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 286 | /* Return the string that is of the top of the stack. */ |
| 287 | const char *hlua_get_top_error_string(lua_State *L) |
| 288 | { |
| 289 | if (lua_gettop(L) < 1) |
| 290 | return "unknown error"; |
| 291 | if (lua_type(L, -1) != LUA_TSTRING) |
| 292 | return "unknown error"; |
| 293 | return lua_tostring(L, -1); |
| 294 | } |
| 295 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 296 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 297 | { |
| 298 | lua_Debug ar; |
| 299 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 300 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 301 | int filled = 0; |
| 302 | |
| 303 | while (lua_getstack(L, level++, &ar)) { |
| 304 | |
| 305 | /* Add separator */ |
| 306 | if (filled) |
| 307 | chunk_appendf(msg, ", "); |
| 308 | filled = 1; |
| 309 | |
| 310 | /* Fill fields: |
| 311 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 312 | * 'l': fills in the field currentline; |
| 313 | * 'n': fills in the field name and namewhat; |
| 314 | * 't': fills in the field istailcall; |
| 315 | */ |
| 316 | lua_getinfo(L, "Slnt", &ar); |
| 317 | |
| 318 | /* Append code localisation */ |
| 319 | if (ar.currentline > 0) |
| 320 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 321 | else |
| 322 | chunk_appendf(msg, "%s ", ar.short_src); |
| 323 | |
| 324 | /* |
| 325 | * Get function name |
| 326 | * |
| 327 | * if namewhat is no empty, name is defined. |
| 328 | * what contains "Lua" for Lua function, "C" for C function, |
| 329 | * or "main" for main code. |
| 330 | */ |
| 331 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 332 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 333 | |
| 334 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 335 | chunk_appendf(msg, "main chunk"); |
| 336 | |
| 337 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 338 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 339 | |
| 340 | else /* nothing left... */ |
| 341 | chunk_appendf(msg, "?"); |
| 342 | |
| 343 | |
| 344 | /* Display tailed call */ |
| 345 | if (ar.istailcall) |
| 346 | chunk_appendf(msg, " ..."); |
| 347 | } |
| 348 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 349 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 353 | /* This function check the number of arguments available in the |
| 354 | * stack. If the number of arguments available is not the same |
| 355 | * then <nb> an error is throwed. |
| 356 | */ |
| 357 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 358 | { |
| 359 | if (lua_gettop(L) == nb) |
| 360 | return; |
| 361 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 362 | } |
| 363 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 364 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 365 | * and the line number where the error is encountered. |
| 366 | */ |
| 367 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 368 | { |
| 369 | va_list argp; |
| 370 | va_start(argp, fmt); |
| 371 | luaL_where(L, 1); |
| 372 | lua_pushvfstring(L, fmt, argp); |
| 373 | va_end(argp); |
| 374 | lua_concat(L, 2); |
| 375 | return 1; |
| 376 | } |
| 377 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 378 | /* This functions is used with sample fetch and converters. It |
| 379 | * converts the HAProxy configuration argument in a lua stack |
| 380 | * values. |
| 381 | * |
| 382 | * It takes an array of "arg", and each entry of the array is |
| 383 | * converted and pushed in the LUA stack. |
| 384 | */ |
| 385 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 386 | { |
| 387 | switch (arg->type) { |
| 388 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 389 | case ARGT_TIME: |
| 390 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 391 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 392 | break; |
| 393 | |
| 394 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 395 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 396 | break; |
| 397 | |
| 398 | case ARGT_IPV4: |
| 399 | case ARGT_IPV6: |
| 400 | case ARGT_MSK4: |
| 401 | case ARGT_MSK6: |
| 402 | case ARGT_FE: |
| 403 | case ARGT_BE: |
| 404 | case ARGT_TAB: |
| 405 | case ARGT_SRV: |
| 406 | case ARGT_USR: |
| 407 | case ARGT_MAP: |
| 408 | default: |
| 409 | lua_pushnil(L); |
| 410 | break; |
| 411 | } |
| 412 | return 1; |
| 413 | } |
| 414 | |
| 415 | /* This function take one entrie in an LUA stack at the index "ud", |
| 416 | * and try to convert it in an HAProxy argument entry. This is useful |
| 417 | * with sample fetch wrappers. The input arguments are gived to the |
| 418 | * lua wrapper and converted as arg list by thi function. |
| 419 | */ |
| 420 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 421 | { |
| 422 | switch (lua_type(L, ud)) { |
| 423 | |
| 424 | case LUA_TNUMBER: |
| 425 | case LUA_TBOOLEAN: |
| 426 | arg->type = ARGT_SINT; |
| 427 | arg->data.sint = lua_tointeger(L, ud); |
| 428 | break; |
| 429 | |
| 430 | case LUA_TSTRING: |
| 431 | arg->type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 432 | arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data); |
Tim Duesterhus | f371b83 | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 433 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 434 | arg->data.str.size = arg->data.str.data; |
| 435 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 436 | break; |
| 437 | |
| 438 | case LUA_TUSERDATA: |
| 439 | case LUA_TNIL: |
| 440 | case LUA_TTABLE: |
| 441 | case LUA_TFUNCTION: |
| 442 | case LUA_TTHREAD: |
| 443 | case LUA_TLIGHTUSERDATA: |
| 444 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 445 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | return 1; |
| 449 | } |
| 450 | |
| 451 | /* the following functions are used to convert a struct sample |
| 452 | * in Lua type. This useful to convert the return of the |
| 453 | * fetchs or converters. |
| 454 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 455 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 456 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 457 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 458 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 459 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 460 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 461 | break; |
| 462 | |
| 463 | case SMP_T_BIN: |
| 464 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 465 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 466 | break; |
| 467 | |
| 468 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 469 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 470 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 471 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 472 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 473 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 474 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 475 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 476 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 477 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 478 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 479 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 480 | break; |
| 481 | default: |
| 482 | lua_pushnil(L); |
| 483 | break; |
| 484 | } |
| 485 | break; |
| 486 | |
| 487 | case SMP_T_IPV4: |
| 488 | case SMP_T_IPV6: |
| 489 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 490 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 491 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 492 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 493 | else |
| 494 | lua_pushnil(L); |
| 495 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 496 | default: |
| 497 | lua_pushnil(L); |
| 498 | break; |
| 499 | } |
| 500 | return 1; |
| 501 | } |
| 502 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 503 | /* the following functions are used to convert a struct sample |
| 504 | * in Lua strings. This is useful to convert the return of the |
| 505 | * fetchs or converters. |
| 506 | */ |
| 507 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 508 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 509 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 510 | |
| 511 | case SMP_T_BIN: |
| 512 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 513 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 514 | break; |
| 515 | |
| 516 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 517 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 518 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 519 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 520 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 521 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 522 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 523 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 524 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 525 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 526 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 527 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 528 | break; |
| 529 | default: |
| 530 | lua_pushstring(L, ""); |
| 531 | break; |
| 532 | } |
| 533 | break; |
| 534 | |
| 535 | case SMP_T_SINT: |
| 536 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 537 | case SMP_T_IPV4: |
| 538 | case SMP_T_IPV6: |
| 539 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 540 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 541 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 542 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 543 | else |
| 544 | lua_pushstring(L, ""); |
| 545 | break; |
| 546 | default: |
| 547 | lua_pushstring(L, ""); |
| 548 | break; |
| 549 | } |
| 550 | return 1; |
| 551 | } |
| 552 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 553 | /* the following functions are used to convert an Lua type in a |
| 554 | * struct sample. This is useful to provide data from a converter |
| 555 | * to the LUA code. |
| 556 | */ |
| 557 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 558 | { |
| 559 | switch (lua_type(L, ud)) { |
| 560 | |
| 561 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 562 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 563 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 564 | break; |
| 565 | |
| 566 | |
| 567 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 568 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 569 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 570 | break; |
| 571 | |
| 572 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 573 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 574 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 575 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data); |
Tim Duesterhus | f371b83 | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 576 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 577 | smp->data.u.str.size = smp->data.u.str.data; |
| 578 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 579 | break; |
| 580 | |
| 581 | case LUA_TUSERDATA: |
| 582 | case LUA_TNIL: |
| 583 | case LUA_TTABLE: |
| 584 | case LUA_TFUNCTION: |
| 585 | case LUA_TTHREAD: |
| 586 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 587 | case LUA_TNONE: |
| 588 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 589 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 590 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | return 1; |
| 594 | } |
| 595 | |
| 596 | /* This function check the "argp" builded by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 597 | * is in accord with the expected argp defined by the "mask". The function |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 598 | * returns true or false. It can be adjust the types if there compatibles. |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 599 | * |
| 600 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 601 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 602 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 603 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 604 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 605 | { |
| 606 | int min_arg; |
| 607 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 608 | struct proxy *px; |
| 609 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 610 | |
| 611 | idx = 0; |
| 612 | min_arg = ARGM(mask); |
| 613 | mask >>= ARGM_BITS; |
| 614 | |
| 615 | while (1) { |
| 616 | |
| 617 | /* Check oversize. */ |
| 618 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 619 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /* Check for mandatory arguments. */ |
| 623 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 624 | if (idx < min_arg) { |
| 625 | |
| 626 | /* If miss other argument than the first one, we return an error. */ |
| 627 | if (idx > 0) |
| 628 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 629 | |
| 630 | /* If first argument have a certain type, some default values |
| 631 | * may be used. See the function smp_resolve_args(). |
| 632 | */ |
| 633 | switch (mask & ARGT_MASK) { |
| 634 | |
| 635 | case ARGT_FE: |
| 636 | if (!(p->cap & PR_CAP_FE)) |
| 637 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 638 | argp[idx].data.prx = p; |
| 639 | argp[idx].type = ARGT_FE; |
| 640 | argp[idx+1].type = ARGT_STOP; |
| 641 | break; |
| 642 | |
| 643 | case ARGT_BE: |
| 644 | if (!(p->cap & PR_CAP_BE)) |
| 645 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 646 | argp[idx].data.prx = p; |
| 647 | argp[idx].type = ARGT_BE; |
| 648 | argp[idx+1].type = ARGT_STOP; |
| 649 | break; |
| 650 | |
| 651 | case ARGT_TAB: |
| 652 | argp[idx].data.prx = p; |
| 653 | argp[idx].type = ARGT_TAB; |
| 654 | argp[idx+1].type = ARGT_STOP; |
| 655 | break; |
| 656 | |
| 657 | default: |
| 658 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 659 | break; |
| 660 | } |
| 661 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | /* Check for exceed the number of requiered argument. */ |
| 666 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 667 | argp[idx].type != ARGT_STOP) { |
| 668 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 669 | } |
| 670 | |
| 671 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 672 | argp[idx].type == ARGT_STOP) { |
| 673 | return 0; |
| 674 | } |
| 675 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 676 | /* Convert some argument types. */ |
| 677 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 678 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 679 | if (argp[idx].type != ARGT_SINT) |
| 680 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 681 | argp[idx].type = ARGT_SINT; |
| 682 | break; |
| 683 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 684 | case ARGT_TIME: |
| 685 | if (argp[idx].type != ARGT_SINT) |
| 686 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 687 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 688 | break; |
| 689 | |
| 690 | case ARGT_SIZE: |
| 691 | if (argp[idx].type != ARGT_SINT) |
| 692 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 693 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 694 | break; |
| 695 | |
| 696 | case ARGT_FE: |
| 697 | if (argp[idx].type != ARGT_STR) |
| 698 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 699 | memcpy(trash.area, argp[idx].data.str.area, |
| 700 | argp[idx].data.str.data); |
| 701 | trash.area[argp[idx].data.str.data] = 0; |
| 702 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 703 | if (!argp[idx].data.prx) |
| 704 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 705 | argp[idx].type = ARGT_FE; |
| 706 | break; |
| 707 | |
| 708 | case ARGT_BE: |
| 709 | if (argp[idx].type != ARGT_STR) |
| 710 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 711 | memcpy(trash.area, argp[idx].data.str.area, |
| 712 | argp[idx].data.str.data); |
| 713 | trash.area[argp[idx].data.str.data] = 0; |
| 714 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 715 | if (!argp[idx].data.prx) |
| 716 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 717 | argp[idx].type = ARGT_BE; |
| 718 | break; |
| 719 | |
| 720 | case ARGT_TAB: |
| 721 | if (argp[idx].type != ARGT_STR) |
| 722 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 723 | memcpy(trash.area, argp[idx].data.str.area, |
| 724 | argp[idx].data.str.data); |
| 725 | trash.area[argp[idx].data.str.data] = 0; |
Tim Duesterhus | e351e76 | 2019-09-29 23:03:09 +0200 | [diff] [blame] | 726 | argp[idx].data.t = stktable_find_by_name(trash.area); |
| 727 | if (!argp[idx].data.t) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 728 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 729 | argp[idx].type = ARGT_TAB; |
| 730 | break; |
| 731 | |
| 732 | case ARGT_SRV: |
| 733 | if (argp[idx].type != ARGT_STR) |
| 734 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 735 | memcpy(trash.area, argp[idx].data.str.area, |
| 736 | argp[idx].data.str.data); |
| 737 | trash.area[argp[idx].data.str.data] = 0; |
| 738 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 739 | if (sname) { |
| 740 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 741 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 742 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 743 | if (!px) |
| 744 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 745 | } |
| 746 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 747 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 748 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 749 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 750 | argp[idx].data.srv = findserver(px, sname); |
| 751 | if (!argp[idx].data.srv) |
| 752 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 753 | argp[idx].type = ARGT_SRV; |
| 754 | break; |
| 755 | |
| 756 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 757 | memcpy(trash.area, argp[idx].data.str.area, |
| 758 | argp[idx].data.str.data); |
| 759 | trash.area[argp[idx].data.str.data] = 0; |
| 760 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 761 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 762 | argp[idx].type = ARGT_IPV4; |
| 763 | break; |
| 764 | |
| 765 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 766 | memcpy(trash.area, argp[idx].data.str.area, |
| 767 | argp[idx].data.str.data); |
| 768 | trash.area[argp[idx].data.str.data] = 0; |
| 769 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 770 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 771 | argp[idx].type = ARGT_MSK4; |
| 772 | break; |
| 773 | |
| 774 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 775 | memcpy(trash.area, argp[idx].data.str.area, |
| 776 | argp[idx].data.str.data); |
| 777 | trash.area[argp[idx].data.str.data] = 0; |
| 778 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 779 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 780 | argp[idx].type = ARGT_IPV6; |
| 781 | break; |
| 782 | |
| 783 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 784 | memcpy(trash.area, argp[idx].data.str.area, |
| 785 | argp[idx].data.str.data); |
| 786 | trash.area[argp[idx].data.str.data] = 0; |
| 787 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 788 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 789 | argp[idx].type = ARGT_MSK6; |
| 790 | break; |
| 791 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 792 | case ARGT_MAP: |
| 793 | case ARGT_REG: |
| 794 | case ARGT_USR: |
| 795 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 796 | break; |
| 797 | } |
| 798 | |
| 799 | /* Check for type of argument. */ |
| 800 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 801 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 802 | arg_type_names[(mask & ARGT_MASK)], |
| 803 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 804 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 805 | } |
| 806 | |
| 807 | /* Next argument. */ |
| 808 | mask >>= ARGT_BITS; |
| 809 | idx++; |
| 810 | } |
| 811 | } |
| 812 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 813 | /* |
| 814 | * The following functions are used to make correspondance between the the |
| 815 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 816 | * |
| 817 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 818 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 819 | */ |
| 820 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 821 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 822 | struct hlua **hlua = lua_getextraspace(L); |
| 823 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 824 | } |
| 825 | static inline void hlua_sethlua(struct hlua *hlua) |
| 826 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 827 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 828 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 829 | } |
| 830 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 831 | /* This function is used to send logs. It try to send on screen (stderr) |
| 832 | * and on the default syslog server. |
| 833 | */ |
| 834 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 835 | { |
| 836 | struct tm tm; |
| 837 | char *p; |
| 838 | |
| 839 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 840 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 841 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 842 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 843 | /* Break the message if exceed the buffer size. */ |
| 844 | *(p-4) = ' '; |
| 845 | *(p-3) = '.'; |
| 846 | *(p-2) = '.'; |
| 847 | *(p-1) = '.'; |
| 848 | break; |
| 849 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 850 | if (isprint(*msg)) |
| 851 | *p = *msg; |
| 852 | else |
| 853 | *p = '.'; |
| 854 | } |
| 855 | *p = '\0'; |
| 856 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 857 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 858 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 859 | get_localtime(date.tv_sec, &tm); |
| 860 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 861 | 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] | 862 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 863 | fflush(stderr); |
| 864 | } |
| 865 | } |
| 866 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 867 | /* This function just ensure that the yield will be always |
| 868 | * returned with a timeout and permit to set some flags |
| 869 | */ |
| 870 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 871 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 872 | { |
| 873 | struct hlua *hlua = hlua_gethlua(L); |
| 874 | |
| 875 | /* Set the wake timeout. If timeout is required, we set |
| 876 | * the expiration time. |
| 877 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 878 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 879 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 880 | hlua->flags |= flags; |
| 881 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 882 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 883 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 884 | } |
| 885 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 886 | /* This function initialises the Lua environment stored in the stream. |
| 887 | * 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] | 888 | * 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] | 889 | * |
| 890 | * This function is particular. it initialises a new Lua thread. If the |
| 891 | * initialisation fails (example: out of memory error), the lua function |
| 892 | * throws an error (longjmp). |
| 893 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 894 | * In some case (at least one), this function can be called from safe |
| 895 | * environement, so we must not initialise it. While the support of |
| 896 | * threads appear, the safe environment set a lock to ensure only one |
| 897 | * Lua execution at a time. If we initialize safe environment in another |
| 898 | * safe environmenet, we have a dead lock. |
| 899 | * |
| 900 | * set "already_safe" true if the context is initialized form safe |
| 901 | * Lua fonction. |
| 902 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 903 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 904 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 905 | * MUST NOT manipulate the created thread stack state, because it is not |
| 906 | * proctected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 907 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 908 | int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 909 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 910 | if (!already_safe) { |
| 911 | if (!SET_SAFE_LJMP(gL.T)) { |
| 912 | lua->Tref = LUA_REFNIL; |
| 913 | return 0; |
| 914 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 915 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 916 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 917 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 918 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 919 | lua->T = lua_newthread(gL.T); |
| 920 | if (!lua->T) { |
| 921 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 922 | if (!already_safe) |
| 923 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 924 | return 0; |
| 925 | } |
| 926 | hlua_sethlua(lua); |
| 927 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 928 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 929 | if (!already_safe) |
| 930 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 931 | return 1; |
| 932 | } |
| 933 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 934 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 935 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 936 | * is not freed. |
| 937 | */ |
| 938 | void hlua_ctx_destroy(struct hlua *lua) |
| 939 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 940 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 941 | return; |
| 942 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 943 | if (!lua->T) |
| 944 | goto end; |
| 945 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 946 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 947 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 948 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 949 | if (!SET_SAFE_LJMP(lua->T)) |
| 950 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 951 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 952 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 953 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 954 | if (!SET_SAFE_LJMP(gL.T)) |
| 955 | return; |
| 956 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 957 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 958 | /* Forces a garbage collecting process. If the Lua program is finished |
| 959 | * without error, we run the GC on the thread pointer. Its freed all |
| 960 | * the unused memory. |
| 961 | * If the thread is finnish with an error or is currently yielded, |
| 962 | * it seems that the GC applied on the thread doesn't clean anything, |
| 963 | * so e run the GC on the main thread. |
| 964 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 965 | * the garbage collection. |
| 966 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 967 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 968 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 969 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 970 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 971 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 972 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 973 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 974 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 975 | |
| 976 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 977 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /* This function is used to restore the Lua context when a coroutine |
| 981 | * fails. This function copy the common memory between old coroutine |
| 982 | * and the new coroutine. The old coroutine is destroyed, and its |
| 983 | * replaced by the new coroutine. |
| 984 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 985 | * as string error message and it is copied in the new stack. |
| 986 | */ |
| 987 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 988 | { |
| 989 | lua_State *T; |
| 990 | int new_ref; |
| 991 | |
| 992 | /* Renew the main LUA stack doesn't have sense. */ |
| 993 | if (lua == &gL) |
| 994 | return 0; |
| 995 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 996 | /* New Lua coroutine. */ |
| 997 | T = lua_newthread(gL.T); |
| 998 | if (!T) |
| 999 | return 0; |
| 1000 | |
| 1001 | /* Copy last error message. */ |
| 1002 | if (keep_msg) |
| 1003 | lua_xmove(lua->T, T, 1); |
| 1004 | |
| 1005 | /* Copy data between the coroutines. */ |
| 1006 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1007 | lua_xmove(lua->T, T, 1); |
| 1008 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 1009 | |
| 1010 | /* Destroy old data. */ |
| 1011 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1012 | |
| 1013 | /* The thread is garbage collected by Lua. */ |
| 1014 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1015 | |
| 1016 | /* Fill the struct with the new coroutine values. */ |
| 1017 | lua->Mref = new_ref; |
| 1018 | lua->T = T; |
| 1019 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1020 | |
| 1021 | /* Set context. */ |
| 1022 | hlua_sethlua(lua); |
| 1023 | |
| 1024 | return 1; |
| 1025 | } |
| 1026 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1027 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1028 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1029 | struct hlua *hlua = hlua_gethlua(L); |
| 1030 | |
| 1031 | /* Lua cannot yield when its returning from a function, |
| 1032 | * so, we can fix the interrupt hook to 1 instruction, |
| 1033 | * expecting that the function is finnished. |
| 1034 | */ |
| 1035 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1036 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1037 | return; |
| 1038 | } |
| 1039 | |
| 1040 | /* restore the interrupt condition. */ |
| 1041 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1042 | |
| 1043 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1044 | * If the state is not yieldable, trying yield causes an error. |
| 1045 | */ |
| 1046 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1047 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1048 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1049 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1050 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1051 | hlua->run_time += now_ms - hlua->start_time; |
| 1052 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1053 | lua_pushfstring(L, "execution timeout"); |
| 1054 | WILL_LJMP(lua_error(L)); |
| 1055 | } |
| 1056 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1057 | /* Update the start time. */ |
| 1058 | hlua->start_time = now_ms; |
| 1059 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1060 | /* Try to interrupt the process at the end of the current |
| 1061 | * unyieldable function. |
| 1062 | */ |
| 1063 | 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] | 1064 | } |
| 1065 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1066 | /* This function start or resumes the Lua stack execution. If the flag |
| 1067 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1068 | * The function return an error. |
| 1069 | * |
| 1070 | * The function can returns 4 values: |
| 1071 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1072 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1073 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1074 | * - 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] | 1075 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1076 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1077 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1078 | * 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] | 1079 | * LUA code. |
| 1080 | */ |
| 1081 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1082 | { |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1083 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1084 | int nres; |
| 1085 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1086 | int ret; |
| 1087 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1088 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1089 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1090 | /* Initialise run time counter. */ |
| 1091 | if (!HLUA_IS_RUNNING(lua)) |
| 1092 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1093 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1094 | /* Lock the whole Lua execution. This lock must be before the |
| 1095 | * label "resume_execution". |
| 1096 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1097 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1098 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1099 | resume_execution: |
| 1100 | |
| 1101 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1102 | * instructions. it is used for preventing infinite loops. |
| 1103 | */ |
| 1104 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1105 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1106 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1107 | HLUA_SET_RUN(lua); |
| 1108 | HLUA_CLR_CTRLYIELD(lua); |
| 1109 | HLUA_CLR_WAKERESWR(lua); |
| 1110 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1111 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1112 | /* Update the start time. */ |
| 1113 | lua->start_time = now_ms; |
| 1114 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1115 | /* Call the function. */ |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1116 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1117 | ret = lua_resume(lua->T, gL.T, lua->nargs, &nres); |
| 1118 | #else |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1119 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1120 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1121 | switch (ret) { |
| 1122 | |
| 1123 | case LUA_OK: |
| 1124 | ret = HLUA_E_OK; |
| 1125 | break; |
| 1126 | |
| 1127 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1128 | /* Check if the execution timeout is expired. It it is the case, we |
| 1129 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1130 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1131 | tv_update_date(0, 1); |
| 1132 | lua->run_time += now_ms - lua->start_time; |
| 1133 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1134 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1135 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1136 | break; |
| 1137 | } |
| 1138 | /* Process the forced yield. if the general yield is not allowed or |
| 1139 | * if no task were associated this the current Lua execution |
| 1140 | * coroutine, we resume the execution. Else we want to return in the |
| 1141 | * scheduler and we want to be waked up again, to continue the |
| 1142 | * current Lua execution. So we schedule our own task. |
| 1143 | */ |
| 1144 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1145 | if (!yield_allowed || !lua->task) |
| 1146 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1147 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1148 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1149 | if (!yield_allowed) { |
| 1150 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1151 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1152 | break; |
| 1153 | } |
| 1154 | ret = HLUA_E_AGAIN; |
| 1155 | break; |
| 1156 | |
| 1157 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1158 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1159 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1160 | * because the errors ares the only one mean to return immediately |
| 1161 | * from and lua execution. |
| 1162 | */ |
| 1163 | if (lua->flags & HLUA_EXIT) { |
| 1164 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1165 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1166 | break; |
| 1167 | } |
| 1168 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1169 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1170 | if (!lua_checkstack(lua->T, 1)) { |
| 1171 | ret = HLUA_E_ERR; |
| 1172 | break; |
| 1173 | } |
| 1174 | msg = lua_tostring(lua->T, -1); |
| 1175 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1176 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1177 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1178 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1179 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1180 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1181 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1182 | ret = HLUA_E_ERRMSG; |
| 1183 | break; |
| 1184 | |
| 1185 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1186 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1187 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1188 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1189 | break; |
| 1190 | |
| 1191 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1192 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1193 | if (!lua_checkstack(lua->T, 1)) { |
| 1194 | ret = HLUA_E_ERR; |
| 1195 | break; |
| 1196 | } |
| 1197 | msg = lua_tostring(lua->T, -1); |
| 1198 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1199 | lua_pop(lua->T, 1); |
| 1200 | if (msg) |
| 1201 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1202 | else |
| 1203 | lua_pushfstring(lua->T, "message handler error"); |
| 1204 | ret = HLUA_E_ERRMSG; |
| 1205 | break; |
| 1206 | |
| 1207 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1208 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1209 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1210 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1211 | break; |
| 1212 | } |
| 1213 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1214 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1215 | if (lua->flags & HLUA_MUST_GC && |
| 1216 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1217 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1218 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1219 | switch (ret) { |
| 1220 | case HLUA_E_AGAIN: |
| 1221 | break; |
| 1222 | |
| 1223 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1224 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1225 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1226 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1227 | break; |
| 1228 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1229 | case HLUA_E_ETMOUT: |
| 1230 | case HLUA_E_NOMEM: |
| 1231 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1232 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1233 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1234 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1235 | hlua_ctx_renew(lua, 0); |
| 1236 | break; |
| 1237 | |
| 1238 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1239 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1240 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1241 | break; |
| 1242 | } |
| 1243 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1244 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1245 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1246 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1247 | return ret; |
| 1248 | } |
| 1249 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1250 | /* This function exit the current code. */ |
| 1251 | __LJMP static int hlua_done(lua_State *L) |
| 1252 | { |
| 1253 | struct hlua *hlua = hlua_gethlua(L); |
| 1254 | |
| 1255 | hlua->flags |= HLUA_EXIT; |
| 1256 | WILL_LJMP(lua_error(L)); |
| 1257 | |
| 1258 | return 0; |
| 1259 | } |
| 1260 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1261 | /* This function is an LUA binding. It provides a function |
| 1262 | * for deleting ACL from a referenced ACL file. |
| 1263 | */ |
| 1264 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1265 | { |
| 1266 | const char *name; |
| 1267 | const char *key; |
| 1268 | struct pat_ref *ref; |
| 1269 | |
| 1270 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1271 | |
| 1272 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1273 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1274 | |
| 1275 | ref = pat_ref_lookup(name); |
| 1276 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1277 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1278 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1279 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1280 | pat_ref_delete(ref, key); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1281 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1282 | return 0; |
| 1283 | } |
| 1284 | |
| 1285 | /* This function is an LUA binding. It provides a function |
| 1286 | * for deleting map entry from a referenced map file. |
| 1287 | */ |
| 1288 | static int hlua_del_map(lua_State *L) |
| 1289 | { |
| 1290 | const char *name; |
| 1291 | const char *key; |
| 1292 | struct pat_ref *ref; |
| 1293 | |
| 1294 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1295 | |
| 1296 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1297 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1298 | |
| 1299 | ref = pat_ref_lookup(name); |
| 1300 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1301 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1302 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1303 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1304 | pat_ref_delete(ref, key); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1305 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1306 | return 0; |
| 1307 | } |
| 1308 | |
| 1309 | /* This function is an LUA binding. It provides a function |
| 1310 | * for adding ACL pattern from a referenced ACL file. |
| 1311 | */ |
| 1312 | static int hlua_add_acl(lua_State *L) |
| 1313 | { |
| 1314 | const char *name; |
| 1315 | const char *key; |
| 1316 | struct pat_ref *ref; |
| 1317 | |
| 1318 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1319 | |
| 1320 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1321 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1322 | |
| 1323 | ref = pat_ref_lookup(name); |
| 1324 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1325 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1326 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1327 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1328 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1329 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1330 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1331 | return 0; |
| 1332 | } |
| 1333 | |
| 1334 | /* This function is an LUA binding. It provides a function |
| 1335 | * for setting map pattern and sample from a referenced map |
| 1336 | * file. |
| 1337 | */ |
| 1338 | static int hlua_set_map(lua_State *L) |
| 1339 | { |
| 1340 | const char *name; |
| 1341 | const char *key; |
| 1342 | const char *value; |
| 1343 | struct pat_ref *ref; |
| 1344 | |
| 1345 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1346 | |
| 1347 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1348 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1349 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1350 | |
| 1351 | ref = pat_ref_lookup(name); |
| 1352 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1353 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1354 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1355 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1356 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1357 | pat_ref_set(ref, key, value, NULL); |
| 1358 | else |
| 1359 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1360 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1361 | return 0; |
| 1362 | } |
| 1363 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1364 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1365 | * an integer or user data. This data is associated with a metatable. This |
| 1366 | * metatable have an original version registred in the global context with |
| 1367 | * the name of the object (_G[<name>] = <metable> ). |
| 1368 | * |
| 1369 | * A metable is a table that modify the standard behavior of a standard |
| 1370 | * access to the associated data. The entries of this new metatable are |
| 1371 | * defined as is: |
| 1372 | * |
| 1373 | * http://lua-users.org/wiki/MetatableEvents |
| 1374 | * |
| 1375 | * __index |
| 1376 | * |
| 1377 | * we access an absent field in a table, the result is nil. This is |
| 1378 | * true, but it is not the whole truth. Actually, such access triggers |
| 1379 | * the interpreter to look for an __index metamethod: If there is no |
| 1380 | * such method, as usually happens, then the access results in nil; |
| 1381 | * otherwise, the metamethod will provide the result. |
| 1382 | * |
| 1383 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1384 | * the key does not appear in the table, but the metatable has an __index |
| 1385 | * property: |
| 1386 | * |
| 1387 | * - if the value is a function, the function is called, passing in the |
| 1388 | * table and the key; the return value of that function is returned as |
| 1389 | * the result. |
| 1390 | * |
| 1391 | * - if the value is another table, the value of the key in that table is |
| 1392 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1393 | * table's metatable has an __index property, then it continues on up) |
| 1394 | * |
| 1395 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1396 | * |
| 1397 | * http://www.lua.org/pil/13.4.1.html |
| 1398 | * |
| 1399 | * __newindex |
| 1400 | * |
| 1401 | * Like __index, but control property assignment. |
| 1402 | * |
| 1403 | * __mode - Control weak references. A string value with one or both |
| 1404 | * of the characters 'k' and 'v' which specifies that the the |
| 1405 | * keys and/or values in the table are weak references. |
| 1406 | * |
| 1407 | * __call - Treat a table like a function. When a table is followed by |
| 1408 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1409 | * a __call key pointing to a function, that function is invoked |
| 1410 | * (passing any specified arguments) and the return value is |
| 1411 | * returned. |
| 1412 | * |
| 1413 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1414 | * called, if the metatable for myTable has a __metatable |
| 1415 | * key, the value of that key is returned instead of the |
| 1416 | * actual metatable. |
| 1417 | * |
| 1418 | * __tostring - Control string representation. When the builtin |
| 1419 | * "tostring( myTable )" function is called, if the metatable |
| 1420 | * for myTable has a __tostring property set to a function, |
| 1421 | * that function is invoked (passing myTable to it) and the |
| 1422 | * return value is used as the string representation. |
| 1423 | * |
| 1424 | * __len - Control table length. When the table length is requested using |
| 1425 | * the length operator ( '#' ), if the metatable for myTable has |
| 1426 | * a __len key pointing to a function, that function is invoked |
| 1427 | * (passing myTable to it) and the return value used as the value |
| 1428 | * of "#myTable". |
| 1429 | * |
| 1430 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1431 | * collected, if the metatable has a __gc field pointing to a |
| 1432 | * function, that function is first invoked, passing the userdata |
| 1433 | * to it. The __gc metamethod is not called for tables. |
| 1434 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1435 | * |
| 1436 | * Special metamethods for redefining standard operators: |
| 1437 | * http://www.lua.org/pil/13.1.html |
| 1438 | * |
| 1439 | * __add "+" |
| 1440 | * __sub "-" |
| 1441 | * __mul "*" |
| 1442 | * __div "/" |
| 1443 | * __unm "!" |
| 1444 | * __pow "^" |
| 1445 | * __concat ".." |
| 1446 | * |
| 1447 | * Special methods for redfining standar relations |
| 1448 | * http://www.lua.org/pil/13.2.html |
| 1449 | * |
| 1450 | * __eq "==" |
| 1451 | * __lt "<" |
| 1452 | * __le "<=" |
| 1453 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1454 | |
| 1455 | /* |
| 1456 | * |
| 1457 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1458 | * Class Map |
| 1459 | * |
| 1460 | * |
| 1461 | */ |
| 1462 | |
| 1463 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1464 | * a class session, otherwise it throws an error. |
| 1465 | */ |
| 1466 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1467 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1468 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1469 | } |
| 1470 | |
| 1471 | /* This function is the map constructor. It don't need |
| 1472 | * the class Map object. It creates and return a new Map |
| 1473 | * object. It must be called only during "body" or "init" |
| 1474 | * context because it process some filesystem accesses. |
| 1475 | */ |
| 1476 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1477 | { |
| 1478 | const char *fn; |
| 1479 | int match = PAT_MATCH_STR; |
| 1480 | struct sample_conv conv; |
| 1481 | const char *file = ""; |
| 1482 | int line = 0; |
| 1483 | lua_Debug ar; |
| 1484 | char *err = NULL; |
| 1485 | struct arg args[2]; |
| 1486 | |
| 1487 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1488 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1489 | |
| 1490 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1491 | |
| 1492 | if (lua_gettop(L) >= 2) { |
| 1493 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1494 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1495 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1496 | } |
| 1497 | |
| 1498 | /* Get Lua filename and line number. */ |
| 1499 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1500 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1501 | if (ar.currentline > 0) { /* is there info? */ |
| 1502 | file = ar.short_src; |
| 1503 | line = ar.currentline; |
| 1504 | } |
| 1505 | } |
| 1506 | |
| 1507 | /* fill fake sample_conv struct. */ |
| 1508 | conv.kw = ""; /* unused. */ |
| 1509 | conv.process = NULL; /* unused. */ |
| 1510 | conv.arg_mask = 0; /* unused. */ |
| 1511 | conv.val_args = NULL; /* unused. */ |
| 1512 | conv.out_type = SMP_T_STR; |
| 1513 | conv.private = (void *)(long)match; |
| 1514 | switch (match) { |
| 1515 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1516 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1517 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1518 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1519 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1520 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1521 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1522 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1523 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1524 | default: |
| 1525 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1526 | } |
| 1527 | |
| 1528 | /* fill fake args. */ |
| 1529 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1530 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1531 | args[1].type = ARGT_STOP; |
| 1532 | |
| 1533 | /* load the map. */ |
| 1534 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1535 | /* error case: we cant use luaL_error because we must |
| 1536 | * free the err variable. |
| 1537 | */ |
| 1538 | luaL_where(L, 1); |
| 1539 | lua_pushfstring(L, "'new': %s.", err); |
| 1540 | lua_concat(L, 2); |
| 1541 | free(err); |
| 1542 | WILL_LJMP(lua_error(L)); |
| 1543 | } |
| 1544 | |
| 1545 | /* create the lua object. */ |
| 1546 | lua_newtable(L); |
| 1547 | lua_pushlightuserdata(L, args[0].data.map); |
| 1548 | lua_rawseti(L, -2, 0); |
| 1549 | |
| 1550 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1551 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1552 | lua_setmetatable(L, -2); |
| 1553 | |
| 1554 | |
| 1555 | return 1; |
| 1556 | } |
| 1557 | |
| 1558 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1559 | { |
| 1560 | struct map_descriptor *desc; |
| 1561 | struct pattern *pat; |
| 1562 | struct sample smp; |
| 1563 | |
| 1564 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1565 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1566 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1567 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1568 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1569 | } |
| 1570 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1571 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1572 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1573 | 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] | 1574 | } |
| 1575 | |
| 1576 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1577 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1578 | if (str) |
| 1579 | lua_pushstring(L, ""); |
| 1580 | else |
| 1581 | lua_pushnil(L); |
| 1582 | return 1; |
| 1583 | } |
| 1584 | |
| 1585 | /* 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] | 1586 | 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] | 1587 | return 1; |
| 1588 | } |
| 1589 | |
| 1590 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1591 | { |
| 1592 | return _hlua_map_lookup(L, 0); |
| 1593 | } |
| 1594 | |
| 1595 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1596 | { |
| 1597 | return _hlua_map_lookup(L, 1); |
| 1598 | } |
| 1599 | |
| 1600 | /* |
| 1601 | * |
| 1602 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1603 | * Class Socket |
| 1604 | * |
| 1605 | * |
| 1606 | */ |
| 1607 | |
| 1608 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1609 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1610 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | /* 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] | 1614 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1615 | * received. |
| 1616 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1617 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1618 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1619 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1620 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1621 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1622 | if (appctx->ctx.hlua_cosocket.die) { |
| 1623 | si_shutw(si); |
| 1624 | si_shutr(si); |
| 1625 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1626 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1627 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1628 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1629 | } |
| 1630 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1631 | /* If the connection object is not available, close all the |
| 1632 | * streams and wakeup everything waiting for. |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1633 | */ |
| 1634 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1635 | si_shutw(si); |
| 1636 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1637 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1638 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1639 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1640 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1641 | } |
| 1642 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1643 | /* If we cant write, wakeup the pending write signals. */ |
| 1644 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1645 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1646 | |
| 1647 | /* If we cant read, wakeup the pending read signals. */ |
| 1648 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1649 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1650 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1651 | /* if the connection is not estabkished, inform the stream that we want |
| 1652 | * to be notified whenever the connection completes. |
| 1653 | */ |
| 1654 | if (!(c->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1655 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1656 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1657 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1658 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1659 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1660 | |
| 1661 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1662 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1663 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1664 | /* 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] | 1665 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1666 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1667 | |
| 1668 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1669 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1670 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1671 | |
| 1672 | /* Some data were injected in the buffer, notify the stream |
| 1673 | * interface. |
| 1674 | */ |
| 1675 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1676 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1677 | |
| 1678 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1679 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1680 | */ |
| 1681 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1682 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1683 | } |
| 1684 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1685 | /* This function is called when the "struct stream" is destroyed. |
| 1686 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1687 | * Wake all the pending signals. |
| 1688 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1689 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1690 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1691 | struct xref *peer; |
| 1692 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1693 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1694 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1695 | if (peer) |
| 1696 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1697 | |
| 1698 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1699 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1700 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1701 | } |
| 1702 | |
| 1703 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1704 | * uses this object. If the stream does not exists, just quit. |
| 1705 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1706 | * pending signal can rest in the read and write lists. destroy |
| 1707 | * it. |
| 1708 | */ |
| 1709 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1710 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1711 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1712 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1713 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1714 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1715 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1716 | |
| 1717 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1718 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1719 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1720 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1721 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1722 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1723 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1724 | appctx->ctx.hlua_cosocket.die = 1; |
| 1725 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1726 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1727 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1728 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1729 | return 0; |
| 1730 | } |
| 1731 | |
| 1732 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1733 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1734 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1735 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1736 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1737 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1738 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1739 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1740 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1741 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1742 | |
| 1743 | /* Check if we run on the same thread than the xreator thread. |
| 1744 | * We cannot access to the socket if the thread is different. |
| 1745 | */ |
| 1746 | if (socket->tid != tid) |
| 1747 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1748 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1749 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1750 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1751 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1752 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1753 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1754 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1755 | appctx->ctx.hlua_cosocket.die = 1; |
| 1756 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1757 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1758 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1759 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1760 | return 0; |
| 1761 | } |
| 1762 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1763 | /* The close function calls close_helper. |
| 1764 | */ |
| 1765 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1766 | { |
| 1767 | MAY_LJMP(check_args(L, 1, "close")); |
| 1768 | return hlua_socket_close_helper(L); |
| 1769 | } |
| 1770 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1771 | /* This Lua function assumes that the stack contain three parameters. |
| 1772 | * 1 - USERDATA containing a struct socket |
| 1773 | * 2 - INTEGER with values of the macro defined below |
| 1774 | * If the integer is -1, we must read at most one line. |
| 1775 | * If the integer is -2, we ust read all the data until the |
| 1776 | * end of the stream. |
| 1777 | * If the integer is positive value, we must read a number of |
| 1778 | * bytes corresponding to this value. |
| 1779 | */ |
| 1780 | #define HLSR_READ_LINE (-1) |
| 1781 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1782 | __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] | 1783 | { |
| 1784 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1785 | int wanted = lua_tointeger(L, 2); |
| 1786 | struct hlua *hlua = hlua_gethlua(L); |
| 1787 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1788 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1789 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1790 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1791 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1792 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1793 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1794 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1795 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1796 | struct stream_interface *si; |
| 1797 | struct stream *s; |
| 1798 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1799 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1800 | |
| 1801 | /* Check if this lua stack is schedulable. */ |
| 1802 | if (!hlua || !hlua->task) |
| 1803 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1804 | "'frontend', 'backend' or 'task'")); |
| 1805 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1806 | /* Check if we run on the same thread than the xreator thread. |
| 1807 | * We cannot access to the socket if the thread is different. |
| 1808 | */ |
| 1809 | if (socket->tid != tid) |
| 1810 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1811 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1812 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1813 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1814 | if (!peer) |
| 1815 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1816 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1817 | si = appctx->owner; |
| 1818 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1819 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1820 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1821 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1822 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1823 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1824 | if (nblk < 0) /* Connection close. */ |
| 1825 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1826 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1827 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1828 | |
| 1829 | /* remove final \r\n. */ |
| 1830 | if (nblk == 1) { |
| 1831 | if (blk1[len1-1] == '\n') { |
| 1832 | len1--; |
| 1833 | skip_at_end++; |
| 1834 | if (blk1[len1-1] == '\r') { |
| 1835 | len1--; |
| 1836 | skip_at_end++; |
| 1837 | } |
| 1838 | } |
| 1839 | } |
| 1840 | else { |
| 1841 | if (blk2[len2-1] == '\n') { |
| 1842 | len2--; |
| 1843 | skip_at_end++; |
| 1844 | if (blk2[len2-1] == '\r') { |
| 1845 | len2--; |
| 1846 | skip_at_end++; |
| 1847 | } |
| 1848 | } |
| 1849 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1853 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1854 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1855 | if (nblk < 0) /* Connection close. */ |
| 1856 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1857 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1858 | goto connection_empty; |
| 1859 | } |
| 1860 | |
| 1861 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1862 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1863 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1864 | if (nblk < 0) /* Connection close. */ |
| 1865 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1866 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1867 | goto connection_empty; |
| 1868 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1869 | missing_bytes = wanted - socket->b.n; |
| 1870 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1871 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1872 | len1 = missing_bytes; |
| 1873 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1874 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | len = len1; |
| 1878 | |
| 1879 | luaL_addlstring(&socket->b, blk1, len1); |
| 1880 | if (nblk == 2) { |
| 1881 | len += len2; |
| 1882 | luaL_addlstring(&socket->b, blk2, len2); |
| 1883 | } |
| 1884 | |
| 1885 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1886 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1887 | |
| 1888 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1889 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1890 | |
| 1891 | /* If the pattern reclaim to read all the data |
| 1892 | * in the connection, got out. |
| 1893 | */ |
| 1894 | if (wanted == HLSR_READ_ALL) |
| 1895 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1896 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1897 | goto connection_empty; |
| 1898 | |
| 1899 | /* Return result. */ |
| 1900 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1901 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1902 | return 1; |
| 1903 | |
| 1904 | connection_closed: |
| 1905 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1906 | xref_unlock(&socket->xref, peer); |
| 1907 | |
| 1908 | no_peer: |
| 1909 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1910 | /* If the buffer containds data. */ |
| 1911 | if (socket->b.n > 0) { |
| 1912 | luaL_pushresult(&socket->b); |
| 1913 | return 1; |
| 1914 | } |
| 1915 | lua_pushnil(L); |
| 1916 | lua_pushstring(L, "connection closed."); |
| 1917 | return 2; |
| 1918 | |
| 1919 | connection_empty: |
| 1920 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1921 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1922 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1923 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1924 | } |
| 1925 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1926 | 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] | 1927 | return 0; |
| 1928 | } |
| 1929 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1930 | /* This Lua function gets two parameters. The first one can be string |
| 1931 | * or a number. If the string is "*l", the user requires one line. If |
| 1932 | * 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] | 1933 | * If the value is a number, the user require a number of bytes equal |
| 1934 | * to the value. The default value is "*l" (a line). |
| 1935 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1936 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1937 | * integer takes this values: |
| 1938 | * -1 : read a line |
| 1939 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1940 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1941 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1942 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1943 | * concatenated with the read data. |
| 1944 | */ |
| 1945 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1946 | { |
| 1947 | int wanted = HLSR_READ_LINE; |
| 1948 | const char *pattern; |
| 1949 | int type; |
| 1950 | char *error; |
| 1951 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1952 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1953 | |
| 1954 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1955 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1956 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1957 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1958 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1959 | /* Check if we run on the same thread than the xreator thread. |
| 1960 | * We cannot access to the socket if the thread is different. |
| 1961 | */ |
| 1962 | if (socket->tid != tid) |
| 1963 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1964 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1965 | /* check for pattern. */ |
| 1966 | if (lua_gettop(L) >= 2) { |
| 1967 | type = lua_type(L, 2); |
| 1968 | if (type == LUA_TSTRING) { |
| 1969 | pattern = lua_tostring(L, 2); |
| 1970 | if (strcmp(pattern, "*a") == 0) |
| 1971 | wanted = HLSR_READ_ALL; |
| 1972 | else if (strcmp(pattern, "*l") == 0) |
| 1973 | wanted = HLSR_READ_LINE; |
| 1974 | else { |
| 1975 | wanted = strtoll(pattern, &error, 10); |
| 1976 | if (*error != '\0') |
| 1977 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1978 | } |
| 1979 | } |
| 1980 | else if (type == LUA_TNUMBER) { |
| 1981 | wanted = lua_tointeger(L, 2); |
| 1982 | if (wanted < 0) |
| 1983 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | /* Set pattern. */ |
| 1988 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1989 | |
| 1990 | /* Check if we would replace the top by itself. */ |
| 1991 | if (lua_gettop(L) != 2) |
| 1992 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1993 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1994 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1995 | luaL_buffinit(L, &socket->b); |
| 1996 | |
| 1997 | /* Check prefix. */ |
| 1998 | if (lua_gettop(L) >= 3) { |
| 1999 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2000 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2001 | pattern = lua_tolstring(L, 3, &len); |
| 2002 | luaL_addlstring(&socket->b, pattern, len); |
| 2003 | } |
| 2004 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2005 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2006 | } |
| 2007 | |
| 2008 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2009 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2010 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2011 | 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] | 2012 | { |
| 2013 | struct hlua_socket *socket; |
| 2014 | struct hlua *hlua = hlua_gethlua(L); |
| 2015 | struct appctx *appctx; |
| 2016 | size_t buf_len; |
| 2017 | const char *buf; |
| 2018 | int len; |
| 2019 | int send_len; |
| 2020 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2021 | struct xref *peer; |
| 2022 | struct stream_interface *si; |
| 2023 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2024 | |
| 2025 | /* Check if this lua stack is schedulable. */ |
| 2026 | if (!hlua || !hlua->task) |
| 2027 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2028 | "'frontend', 'backend' or 'task'")); |
| 2029 | |
| 2030 | /* Get object */ |
| 2031 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2032 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2033 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2034 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2035 | /* Check if we run on the same thread than the xreator thread. |
| 2036 | * We cannot access to the socket if the thread is different. |
| 2037 | */ |
| 2038 | if (socket->tid != tid) |
| 2039 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2040 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2041 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2042 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2043 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2044 | lua_pushinteger(L, -1); |
| 2045 | return 1; |
| 2046 | } |
| 2047 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2048 | si = appctx->owner; |
| 2049 | s = si_strm(si); |
| 2050 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2051 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2052 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2053 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2054 | lua_pushinteger(L, -1); |
| 2055 | return 1; |
| 2056 | } |
| 2057 | |
| 2058 | /* Update the input buffer data. */ |
| 2059 | buf += sent; |
| 2060 | send_len = buf_len - sent; |
| 2061 | |
| 2062 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2063 | if (sent >= buf_len) { |
| 2064 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2065 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2066 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2067 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2068 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2069 | * the request buffer if its not required. |
| 2070 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2071 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2072 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2073 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2074 | } |
| 2075 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2076 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2077 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2078 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2079 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2080 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2081 | |
| 2082 | /* send data */ |
| 2083 | if (len < send_len) |
| 2084 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2085 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2086 | |
| 2087 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2088 | * the data" (-2) are not expected because the available length |
| 2089 | * is tested. |
| 2090 | * Other unknown error are also not expected. |
| 2091 | */ |
| 2092 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2093 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2094 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2095 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2096 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2097 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2098 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2099 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2100 | return 1; |
| 2101 | } |
| 2102 | |
| 2103 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2104 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2105 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2106 | s->req.rex = TICK_ETERNITY; |
| 2107 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2108 | |
| 2109 | /* Update length sent. */ |
| 2110 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2111 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2112 | |
| 2113 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2114 | if (sent + len >= buf_len) { |
| 2115 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2116 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2117 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2118 | |
| 2119 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2120 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2121 | xref_unlock(&socket->xref, peer); |
| 2122 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2123 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2124 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2125 | 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] | 2126 | return 0; |
| 2127 | } |
| 2128 | |
| 2129 | /* This function initiate the send of data. It just check the input |
| 2130 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2131 | * 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] | 2132 | * "hlua_socket_write_yield" that can yield. |
| 2133 | * |
| 2134 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2135 | * the associated object. The second is a string buffer. The third is |
| 2136 | * a facultative integer that represents where is the buffer position |
| 2137 | * of the start of the data that can send. The first byte is the |
| 2138 | * position "1". The default value is "1". The fourth argument is a |
| 2139 | * facultative integer that represents where is the buffer position |
| 2140 | * of the end of the data that can send. The default is the last byte. |
| 2141 | */ |
| 2142 | static int hlua_socket_send(struct lua_State *L) |
| 2143 | { |
| 2144 | int i; |
| 2145 | int j; |
| 2146 | const char *buf; |
| 2147 | size_t buf_len; |
| 2148 | |
| 2149 | /* Check number of arguments. */ |
| 2150 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2151 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2152 | |
| 2153 | /* Get the string. */ |
| 2154 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2155 | |
| 2156 | /* Get and check j. */ |
| 2157 | if (lua_gettop(L) == 4) { |
| 2158 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2159 | if (j < 0) |
| 2160 | j = buf_len + j + 1; |
| 2161 | if (j > buf_len) |
| 2162 | j = buf_len + 1; |
| 2163 | lua_pop(L, 1); |
| 2164 | } |
| 2165 | else |
| 2166 | j = buf_len; |
| 2167 | |
| 2168 | /* Get and check i. */ |
| 2169 | if (lua_gettop(L) == 3) { |
| 2170 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2171 | if (i < 0) |
| 2172 | i = buf_len + i + 1; |
| 2173 | if (i > buf_len) |
| 2174 | i = buf_len + 1; |
| 2175 | lua_pop(L, 1); |
| 2176 | } else |
| 2177 | i = 1; |
| 2178 | |
| 2179 | /* Check bth i and j. */ |
| 2180 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2181 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2182 | return 1; |
| 2183 | } |
| 2184 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2185 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2186 | return 1; |
| 2187 | } |
| 2188 | if (i == 0) |
| 2189 | i = 1; |
| 2190 | if (j == 0) |
| 2191 | j = 1; |
| 2192 | |
| 2193 | /* Pop the string. */ |
| 2194 | lua_pop(L, 1); |
| 2195 | |
| 2196 | /* Update the buffer length. */ |
| 2197 | buf += i - 1; |
| 2198 | buf_len = j - i + 1; |
| 2199 | lua_pushlstring(L, buf, buf_len); |
| 2200 | |
| 2201 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2202 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2203 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2204 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2205 | } |
| 2206 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2207 | #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] | 2208 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2209 | { |
| 2210 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2211 | int ret; |
| 2212 | int len; |
| 2213 | char *p; |
| 2214 | |
| 2215 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2216 | if (ret <= 0) { |
| 2217 | lua_pushnil(L); |
| 2218 | return 1; |
| 2219 | } |
| 2220 | |
| 2221 | if (ret == AF_UNIX) { |
| 2222 | lua_pushstring(L, buffer+1); |
| 2223 | return 1; |
| 2224 | } |
| 2225 | else if (ret == AF_INET6) { |
| 2226 | buffer[0] = '['; |
| 2227 | len = strlen(buffer); |
| 2228 | buffer[len] = ']'; |
| 2229 | len++; |
| 2230 | buffer[len] = ':'; |
| 2231 | len++; |
| 2232 | p = buffer; |
| 2233 | } |
| 2234 | else if (ret == AF_INET) { |
| 2235 | p = buffer + 1; |
| 2236 | len = strlen(p); |
| 2237 | p[len] = ':'; |
| 2238 | len++; |
| 2239 | } |
| 2240 | else { |
| 2241 | lua_pushnil(L); |
| 2242 | return 1; |
| 2243 | } |
| 2244 | |
| 2245 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2246 | lua_pushnil(L); |
| 2247 | return 1; |
| 2248 | } |
| 2249 | |
| 2250 | lua_pushstring(L, p); |
| 2251 | return 1; |
| 2252 | } |
| 2253 | |
| 2254 | /* Returns information about the peer of the connection. */ |
| 2255 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2256 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2257 | struct hlua_socket *socket; |
| 2258 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2259 | struct xref *peer; |
| 2260 | struct appctx *appctx; |
| 2261 | struct stream_interface *si; |
| 2262 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2263 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2264 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2265 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2266 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2267 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2268 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2269 | /* Check if we run on the same thread than the xreator thread. |
| 2270 | * We cannot access to the socket if the thread is different. |
| 2271 | */ |
| 2272 | if (socket->tid != tid) |
| 2273 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2274 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2275 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2276 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2277 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2278 | lua_pushnil(L); |
| 2279 | return 1; |
| 2280 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2281 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2282 | si = appctx->owner; |
| 2283 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2284 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2285 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2286 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2287 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | lua_pushnil(L); |
| 2289 | return 1; |
| 2290 | } |
| 2291 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2292 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2294 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2295 | lua_pushnil(L); |
| 2296 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | } |
| 2298 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2299 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2300 | xref_unlock(&socket->xref, peer); |
| 2301 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | /* Returns information about my connection side. */ |
| 2305 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2306 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2307 | struct hlua_socket *socket; |
| 2308 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2309 | struct appctx *appctx; |
| 2310 | struct xref *peer; |
| 2311 | struct stream_interface *si; |
| 2312 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2313 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2314 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2315 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2316 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2317 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2318 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2319 | /* Check if we run on the same thread than the xreator thread. |
| 2320 | * We cannot access to the socket if the thread is different. |
| 2321 | */ |
| 2322 | if (socket->tid != tid) |
| 2323 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2324 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2325 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2326 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2327 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2328 | lua_pushnil(L); |
| 2329 | return 1; |
| 2330 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2331 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2332 | si = appctx->owner; |
| 2333 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2334 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2335 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2336 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2337 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2338 | lua_pushnil(L); |
| 2339 | return 1; |
| 2340 | } |
| 2341 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2342 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2343 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2344 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2345 | lua_pushnil(L); |
| 2346 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2347 | } |
| 2348 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2349 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2350 | xref_unlock(&socket->xref, peer); |
| 2351 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2352 | } |
| 2353 | |
| 2354 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2355 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2356 | .obj_type = OBJ_TYPE_APPLET, |
| 2357 | .name = "<LUA_TCP>", |
| 2358 | .fct = hlua_socket_handler, |
| 2359 | .release = hlua_socket_release, |
| 2360 | }; |
| 2361 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2362 | __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] | 2363 | { |
| 2364 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2365 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2366 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2367 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2368 | struct stream_interface *si; |
| 2369 | struct stream *s; |
| 2370 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2371 | /* Check if we run on the same thread than the xreator thread. |
| 2372 | * We cannot access to the socket if the thread is different. |
| 2373 | */ |
| 2374 | if (socket->tid != tid) |
| 2375 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2376 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2377 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2378 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2379 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2380 | lua_pushnil(L); |
| 2381 | lua_pushstring(L, "Can't connect"); |
| 2382 | return 2; |
| 2383 | } |
| 2384 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2385 | si = appctx->owner; |
| 2386 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2387 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2388 | /* Check if we run on the same thread than the xreator thread. |
| 2389 | * We cannot access to the socket if the thread is different. |
| 2390 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2391 | if (socket->tid != tid) { |
| 2392 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2393 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2394 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2395 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2396 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2397 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2398 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2399 | lua_pushnil(L); |
| 2400 | lua_pushstring(L, "Can't connect"); |
| 2401 | return 2; |
| 2402 | } |
| 2403 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2404 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2405 | |
| 2406 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2407 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2408 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2409 | lua_pushinteger(L, 1); |
| 2410 | return 1; |
| 2411 | } |
| 2412 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2413 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2414 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2415 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2416 | } |
| 2417 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2418 | 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] | 2419 | return 0; |
| 2420 | } |
| 2421 | |
| 2422 | /* This function fail or initite the connection. */ |
| 2423 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2424 | { |
| 2425 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2426 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2427 | const char *ip; |
| 2428 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2429 | struct hlua *hlua; |
| 2430 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2431 | int low, high; |
| 2432 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2433 | struct xref *peer; |
| 2434 | struct stream_interface *si; |
| 2435 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2436 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2437 | if (lua_gettop(L) < 2) |
| 2438 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2439 | |
| 2440 | /* Get args. */ |
| 2441 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2442 | |
| 2443 | /* Check if we run on the same thread than the xreator thread. |
| 2444 | * We cannot access to the socket if the thread is different. |
| 2445 | */ |
| 2446 | if (socket->tid != tid) |
| 2447 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2448 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2449 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2450 | if (lua_gettop(L) >= 3) { |
| 2451 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2452 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2453 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2454 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2455 | * that are not enclosed within square brackets. |
| 2456 | */ |
| 2457 | if (port > 0) { |
| 2458 | luaL_buffinit(L, &b); |
| 2459 | luaL_addstring(&b, ip); |
| 2460 | luaL_addchar(&b, ':'); |
| 2461 | luaL_pushresult(&b); |
| 2462 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2463 | } |
| 2464 | } |
| 2465 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2466 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2467 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2468 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2469 | lua_pushnil(L); |
| 2470 | return 1; |
| 2471 | } |
| 2472 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2473 | si = appctx->owner; |
| 2474 | s = si_strm(si); |
| 2475 | |
| 2476 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2477 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2478 | if (!conn) { |
| 2479 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2480 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2481 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2482 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2483 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2484 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2485 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2486 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2487 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2488 | if (!addr) { |
| 2489 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2490 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2491 | } |
| 2492 | if (low != high) { |
| 2493 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2494 | WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2495 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2496 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2497 | |
| 2498 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2499 | if (low == 0) { |
| 2500 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2501 | if (port == -1) { |
| 2502 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2503 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2504 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2505 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2506 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2507 | if (port == -1) { |
| 2508 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2509 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2510 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2511 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2512 | } |
| 2513 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2514 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2515 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2516 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2517 | |
| 2518 | /* inform the stream that we want to be notified whenever the |
| 2519 | * connection completes. |
| 2520 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2521 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2522 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2523 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2524 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2525 | hlua->flags |= HLUA_MUST_GC; |
| 2526 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2527 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2528 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2529 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2530 | } |
| 2531 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2532 | |
| 2533 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2534 | /* Return yield waiting for connection. */ |
| 2535 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2536 | 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] | 2537 | |
| 2538 | return 0; |
| 2539 | } |
| 2540 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2541 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2542 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2543 | { |
| 2544 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2545 | struct xref *peer; |
| 2546 | struct appctx *appctx; |
| 2547 | struct stream_interface *si; |
| 2548 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2549 | |
| 2550 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2551 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2552 | |
| 2553 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2554 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2555 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2556 | lua_pushnil(L); |
| 2557 | return 1; |
| 2558 | } |
| 2559 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2560 | si = appctx->owner; |
| 2561 | s = si_strm(si); |
| 2562 | |
| 2563 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2564 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2565 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2566 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2567 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2568 | |
| 2569 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2570 | { |
| 2571 | return 0; |
| 2572 | } |
| 2573 | |
| 2574 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2575 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2576 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2577 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2578 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2579 | struct xref *peer; |
| 2580 | struct appctx *appctx; |
| 2581 | struct stream_interface *si; |
| 2582 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2583 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2584 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2585 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2586 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2587 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2588 | /* convert the timeout to millis */ |
| 2589 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2590 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2591 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2592 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2593 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2594 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2595 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2596 | 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] | 2597 | |
| 2598 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2599 | if (tmout == 0) |
| 2600 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2601 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2602 | /* Check if we run on the same thread than the xreator thread. |
| 2603 | * We cannot access to the socket if the thread is different. |
| 2604 | */ |
| 2605 | if (socket->tid != tid) |
| 2606 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2607 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2608 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2609 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2610 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2611 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2612 | WILL_LJMP(lua_error(L)); |
| 2613 | return 0; |
| 2614 | } |
| 2615 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2616 | si = appctx->owner; |
| 2617 | s = si_strm(si); |
| 2618 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2619 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2620 | s->req.rto = tmout; |
| 2621 | s->req.wto = tmout; |
| 2622 | s->res.rto = tmout; |
| 2623 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2624 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2625 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2626 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2627 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2628 | |
| 2629 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2630 | task_queue(s->task); |
| 2631 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2632 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2633 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2634 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2635 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2636 | } |
| 2637 | |
| 2638 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2639 | { |
| 2640 | struct hlua_socket *socket; |
| 2641 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2642 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2643 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2644 | |
| 2645 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2646 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2647 | hlua_pusherror(L, "socket: full stack"); |
| 2648 | goto out_fail_conf; |
| 2649 | } |
| 2650 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2651 | /* Create the object: obj[0] = userdata. */ |
| 2652 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2653 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2654 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2655 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2656 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2657 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2658 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2659 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2660 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2661 | goto out_fail_conf; |
| 2662 | } |
| 2663 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2664 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2665 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2666 | lua_setmetatable(L, -2); |
| 2667 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2668 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2669 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2670 | if (!appctx) { |
| 2671 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2672 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2673 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2674 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2675 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2676 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2677 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2678 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2679 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2680 | /* Now create a session, task and stream for this applet */ |
| 2681 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2682 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2683 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2684 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2685 | } |
| 2686 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2687 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2688 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2689 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2690 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2691 | } |
| 2692 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2693 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2694 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2695 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2696 | /* Configure "right" stream interface. this "si" is used to connect |
| 2697 | * and retrieve data from the server. The connection is initialized |
| 2698 | * with the "struct server". |
| 2699 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2700 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2701 | |
| 2702 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2703 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2704 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2705 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2706 | return 1; |
| 2707 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2708 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2709 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2710 | out_fail_sess: |
| 2711 | appctx_free(appctx); |
| 2712 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2713 | WILL_LJMP(lua_error(L)); |
| 2714 | return 0; |
| 2715 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2716 | |
| 2717 | /* |
| 2718 | * |
| 2719 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2720 | * Class Channel |
| 2721 | * |
| 2722 | * |
| 2723 | */ |
| 2724 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2725 | /* This function is called before the Lua execution. It stores |
| 2726 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2727 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2728 | 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] | 2729 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2730 | c->mode = stream->be->mode; |
| 2731 | switch (c->mode) { |
| 2732 | case PR_MODE_HTTP: |
| 2733 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2734 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2735 | c->data.http.state = stream->txn->req.msg_state; |
| 2736 | else |
| 2737 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2738 | break; |
| 2739 | default: |
| 2740 | break; |
| 2741 | } |
| 2742 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2743 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2744 | /* This function is called after the Lua execution. it |
| 2745 | * returns true if the parser state is consistent, otherwise, |
| 2746 | * it return false. |
| 2747 | * |
| 2748 | * In HTTP mode, the parser state must be in the same state |
| 2749 | * or greater when we exit the function. Even if we do a |
| 2750 | * control yield. This prevent to break the HTTP message |
| 2751 | * from the Lua code. |
| 2752 | */ |
| 2753 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2754 | { |
| 2755 | if (c->mode != stream->be->mode) |
| 2756 | return 0; |
| 2757 | |
| 2758 | switch (c->mode) { |
| 2759 | case PR_MODE_HTTP: |
| 2760 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2761 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2762 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2763 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2764 | else |
| 2765 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2766 | default: |
| 2767 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2768 | } |
| 2769 | return 1; |
| 2770 | } |
| 2771 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2772 | /* Returns the struct hlua_channel join to the class channel in the |
| 2773 | * stack entry "ud" or throws an argument error. |
| 2774 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2775 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2776 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2777 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2778 | } |
| 2779 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2780 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2781 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2782 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2783 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2784 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2785 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2786 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2787 | return 0; |
| 2788 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2789 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2790 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2791 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2792 | |
| 2793 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2794 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2795 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2796 | return 1; |
| 2797 | } |
| 2798 | |
| 2799 | /* Duplicate all the data present in the input channel and put it |
| 2800 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2801 | * the stack if the channel is closed and all the data are consumed, |
| 2802 | * returns 0 if no data are available, otherwise it returns the length |
| 2803 | * of the builded string. |
| 2804 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2805 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2806 | { |
| 2807 | char *blk1; |
| 2808 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2809 | size_t len1; |
| 2810 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2811 | int ret; |
| 2812 | luaL_Buffer b; |
| 2813 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2814 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2815 | if (unlikely(ret == 0)) |
| 2816 | return 0; |
| 2817 | |
| 2818 | if (unlikely(ret < 0)) { |
| 2819 | lua_pushnil(L); |
| 2820 | return -1; |
| 2821 | } |
| 2822 | |
| 2823 | luaL_buffinit(L, &b); |
| 2824 | luaL_addlstring(&b, blk1, len1); |
| 2825 | if (unlikely(ret == 2)) |
| 2826 | luaL_addlstring(&b, blk2, len2); |
| 2827 | luaL_pushresult(&b); |
| 2828 | |
| 2829 | if (unlikely(ret == 2)) |
| 2830 | return len1 + len2; |
| 2831 | return len1; |
| 2832 | } |
| 2833 | |
| 2834 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2835 | * a yield. This function keep the data in the buffer. |
| 2836 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2837 | __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] | 2838 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2839 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2840 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2841 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2842 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2843 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2844 | WILL_LJMP(lua_error(L)); |
| 2845 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2846 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2847 | 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] | 2848 | return 1; |
| 2849 | } |
| 2850 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2851 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2852 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2853 | { |
| 2854 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2855 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2856 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2857 | } |
| 2858 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2859 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2860 | * a yield. This function consumes the data in the buffer. It returns |
| 2861 | * a string containing the data or a nil pointer if no data are available |
| 2862 | * and the channel is closed. |
| 2863 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2864 | __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] | 2865 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2866 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2867 | int ret; |
| 2868 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2869 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2870 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2871 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2872 | WILL_LJMP(lua_error(L)); |
| 2873 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2874 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2875 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2876 | 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] | 2877 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2878 | if (unlikely(ret == -1)) |
| 2879 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2880 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2881 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2882 | return 1; |
| 2883 | } |
| 2884 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2885 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2886 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2887 | { |
| 2888 | MAY_LJMP(check_args(L, 1, "get")); |
| 2889 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2890 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2891 | } |
| 2892 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2893 | /* This functions consumes and returns one line. If the channel is closed, |
| 2894 | * 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] | 2895 | * 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] | 2896 | * value. |
| 2897 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2898 | __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] | 2899 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2900 | char *blk1; |
| 2901 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2902 | size_t len1; |
| 2903 | size_t len2; |
| 2904 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2905 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2906 | int ret; |
| 2907 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2908 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2909 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2910 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2911 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2912 | WILL_LJMP(lua_error(L)); |
| 2913 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2914 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2915 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2916 | 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] | 2917 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2918 | if (ret == -1) { |
| 2919 | lua_pushnil(L); |
| 2920 | return 1; |
| 2921 | } |
| 2922 | |
| 2923 | luaL_buffinit(L, &b); |
| 2924 | luaL_addlstring(&b, blk1, len1); |
| 2925 | len = len1; |
| 2926 | if (unlikely(ret == 2)) { |
| 2927 | luaL_addlstring(&b, blk2, len2); |
| 2928 | len += len2; |
| 2929 | } |
| 2930 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2931 | b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2932 | return 1; |
| 2933 | } |
| 2934 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2935 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2936 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2937 | { |
| 2938 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2939 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2940 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2941 | } |
| 2942 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2943 | /* This function takes a string as input, and append it at the |
| 2944 | * input side of channel. If the data is too big, but a space |
| 2945 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2946 | * yields. If the data is bigger than the buffer, or if the |
| 2947 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2948 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2949 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2950 | __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] | 2951 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2952 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2953 | size_t len; |
| 2954 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2955 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2956 | int ret; |
| 2957 | int max; |
| 2958 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2959 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2960 | WILL_LJMP(lua_error(L)); |
| 2961 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2962 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2963 | * the request buffer if its not required. |
| 2964 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2965 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2966 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2967 | 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] | 2968 | } |
| 2969 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2970 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2971 | if (max > len - l) |
| 2972 | max = len - l; |
| 2973 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2974 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2975 | if (ret == -2 || ret == -3) { |
| 2976 | lua_pushinteger(L, -1); |
| 2977 | return 1; |
| 2978 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2979 | if (ret == -1) { |
| 2980 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2981 | 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] | 2982 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2983 | l += ret; |
| 2984 | lua_pop(L, 1); |
| 2985 | lua_pushinteger(L, l); |
| 2986 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2987 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2988 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2989 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2990 | * in this case, we cannot add more data, so we cannot yield, |
| 2991 | * we return the amount of copyied data. |
| 2992 | */ |
| 2993 | return 1; |
| 2994 | } |
| 2995 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2996 | 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] | 2997 | return 1; |
| 2998 | } |
| 2999 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3000 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3001 | * 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] | 3002 | * buffer size is too little for the data. |
| 3003 | */ |
| 3004 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3005 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3006 | size_t len; |
| 3007 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3008 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3009 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3010 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3011 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3012 | lua_pushinteger(L, 0); |
| 3013 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3014 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3015 | } |
| 3016 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3017 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3018 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3019 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3020 | * or -1 if the channel is closed or if the buffer size is too |
| 3021 | * little for the data. |
| 3022 | */ |
| 3023 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3024 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3025 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3026 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3027 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3028 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3029 | lua_pushinteger(L, 0); |
| 3030 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3031 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3032 | WILL_LJMP(lua_error(L)); |
| 3033 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3034 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3035 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3036 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3037 | } |
| 3038 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3039 | /* Append data in the output side of the buffer. This data is immediately |
| 3040 | * sent. The function returns the amount of data written. If the buffer |
| 3041 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3042 | * if the channel is closed. |
| 3043 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3044 | __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] | 3045 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3046 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3047 | size_t len; |
| 3048 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3049 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3050 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3051 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3052 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3053 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3054 | WILL_LJMP(lua_error(L)); |
| 3055 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3056 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3057 | lua_pushinteger(L, -1); |
| 3058 | return 1; |
| 3059 | } |
| 3060 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3061 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3062 | * the request buffer if its not required. |
| 3063 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3064 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3065 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3066 | 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] | 3067 | } |
| 3068 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3069 | /* The written data will be immediately sent, so we can check |
| 3070 | * the available space without taking in account the reserve. |
| 3071 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3072 | * data, because the buffer will be flushed. |
| 3073 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3074 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3075 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3076 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3077 | * in this case, we cannot add more data, so we cannot yield, |
| 3078 | * we return the amount of copyied data. |
| 3079 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3080 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3081 | return 1; |
| 3082 | |
| 3083 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3084 | if (max > len - l) |
| 3085 | max = len - l; |
| 3086 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3087 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3088 | * detects a non contiguous buffer and realign it. |
| 3089 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3090 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3091 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3092 | |
| 3093 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3094 | 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] | 3095 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3096 | /* buffer replace considers that the input part is filled. |
| 3097 | * so, I must forward these new data in the output part. |
| 3098 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3099 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3100 | |
| 3101 | l += max; |
| 3102 | lua_pop(L, 1); |
| 3103 | lua_pushinteger(L, l); |
| 3104 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3105 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3106 | * in this case, we cannot add more data, so we cannot yield, |
| 3107 | * we return the amount of copyied data. |
| 3108 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3109 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3110 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3111 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3112 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3113 | if (l < len) { |
| 3114 | /* If we are waiting for space in the response buffer, we |
| 3115 | * must set the flag WAKERESWR. This flag required the task |
| 3116 | * wake up if any activity is detected on the response buffer. |
| 3117 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3118 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3119 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3120 | else |
| 3121 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3122 | 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] | 3123 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3124 | |
| 3125 | return 1; |
| 3126 | } |
| 3127 | |
| 3128 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3129 | * yield the LUA process, and resume it without checking the |
| 3130 | * input arguments. |
| 3131 | */ |
| 3132 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3133 | { |
| 3134 | MAY_LJMP(check_args(L, 2, "send")); |
| 3135 | lua_pushinteger(L, 0); |
| 3136 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3137 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | /* This function forward and amount of butes. The data pass from |
| 3141 | * the input side of the buffer to the output side, and can be |
| 3142 | * forwarded. This function never fails. |
| 3143 | * |
| 3144 | * The Lua function takes an amount of bytes to be forwarded in |
| 3145 | * imput. It returns the number of bytes forwarded. |
| 3146 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3147 | __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] | 3148 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3149 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3150 | int len; |
| 3151 | int l; |
| 3152 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3153 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3154 | |
| 3155 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3156 | |
| 3157 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3158 | WILL_LJMP(lua_error(L)); |
| 3159 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3160 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3161 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3162 | |
| 3163 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3164 | if (max > ci_data(chn)) |
| 3165 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3166 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3167 | l += max; |
| 3168 | |
| 3169 | lua_pop(L, 1); |
| 3170 | lua_pushinteger(L, l); |
| 3171 | |
| 3172 | /* Check if it miss bytes to forward. */ |
| 3173 | if (l < len) { |
| 3174 | /* The the input channel or the output channel are closed, we |
| 3175 | * must return the amount of data forwarded. |
| 3176 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3177 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3178 | return 1; |
| 3179 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3180 | /* If we are waiting for space data in the response buffer, we |
| 3181 | * must set the flag WAKERESWR. This flag required the task |
| 3182 | * wake up if any activity is detected on the response buffer. |
| 3183 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3184 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3185 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3186 | else |
| 3187 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3188 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3189 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3190 | 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] | 3191 | } |
| 3192 | |
| 3193 | return 1; |
| 3194 | } |
| 3195 | |
| 3196 | /* Just check the input and prepare the stack for the previous |
| 3197 | * function "hlua_channel_forward_yield" |
| 3198 | */ |
| 3199 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3200 | { |
| 3201 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3202 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3203 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3204 | |
| 3205 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3206 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3207 | } |
| 3208 | |
| 3209 | /* Just returns the number of bytes available in the input |
| 3210 | * side of the buffer. This function never fails. |
| 3211 | */ |
| 3212 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3213 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3214 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3215 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3216 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3217 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3218 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3219 | struct htx *htx = htxbuf(&chn->buf); |
| 3220 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3221 | } |
| 3222 | else |
| 3223 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3224 | return 1; |
| 3225 | } |
| 3226 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3227 | /* Returns true if the channel is full. */ |
| 3228 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3229 | { |
| 3230 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3231 | |
| 3232 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3233 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0136ebb | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3234 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3235 | * applet). |
| 3236 | */ |
| 3237 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3238 | return 1; |
| 3239 | } |
| 3240 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3241 | /* Just returns the number of bytes available in the output |
| 3242 | * side of the buffer. This function never fails. |
| 3243 | */ |
| 3244 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3245 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3246 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3247 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3248 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3249 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3250 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3251 | return 1; |
| 3252 | } |
| 3253 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3254 | /* |
| 3255 | * |
| 3256 | * |
| 3257 | * Class Fetches |
| 3258 | * |
| 3259 | * |
| 3260 | */ |
| 3261 | |
| 3262 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3263 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3264 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3265 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3266 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3267 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3268 | } |
| 3269 | |
| 3270 | /* This function creates and push in the stack a fetch object according |
| 3271 | * with a current TXN. |
| 3272 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3273 | 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] | 3274 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3275 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3276 | |
| 3277 | /* Check stack size. */ |
| 3278 | if (!lua_checkstack(L, 3)) |
| 3279 | return 0; |
| 3280 | |
| 3281 | /* Create the object: obj[0] = userdata. |
| 3282 | * Note that the base of the Fetches object is the |
| 3283 | * transaction object. |
| 3284 | */ |
| 3285 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3286 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3287 | lua_rawseti(L, -2, 0); |
| 3288 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3289 | hsmp->s = txn->s; |
| 3290 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3291 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3292 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3293 | |
| 3294 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3295 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3296 | lua_setmetatable(L, -2); |
| 3297 | |
| 3298 | return 1; |
| 3299 | } |
| 3300 | |
| 3301 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3302 | * It uses closure argument to store the associated sample-fetch. It |
| 3303 | * returns only one argument or throws an error. An error is thrown |
| 3304 | * only if an error is encountered during the argument parsing. If |
| 3305 | * the "sample-fetch" function fails, nil is returned. |
| 3306 | */ |
| 3307 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3308 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3309 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3310 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3311 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3312 | int i; |
| 3313 | struct sample smp; |
| 3314 | |
| 3315 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3316 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3317 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3318 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3319 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3320 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3321 | /* Check execution authorization. */ |
| 3322 | if (f->use & SMP_USE_HTTP_ANY && |
| 3323 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3324 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3325 | "is not available in Lua services", f->kw); |
| 3326 | WILL_LJMP(lua_error(L)); |
| 3327 | } |
| 3328 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3329 | /* Get extra arguments. */ |
| 3330 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3331 | if (i >= ARGM_NBARGS) |
| 3332 | break; |
| 3333 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3334 | } |
| 3335 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3336 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3337 | |
| 3338 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3339 | 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] | 3340 | |
| 3341 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3342 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3343 | lua_pushfstring(L, "error in arguments"); |
| 3344 | WILL_LJMP(lua_error(L)); |
| 3345 | } |
| 3346 | |
| 3347 | /* Initialise the sample. */ |
| 3348 | memset(&smp, 0, sizeof(smp)); |
| 3349 | |
| 3350 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3351 | 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] | 3352 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3353 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3354 | lua_pushstring(L, ""); |
| 3355 | else |
| 3356 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3357 | return 1; |
| 3358 | } |
| 3359 | |
| 3360 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3361 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3362 | hlua_smp2lua_str(L, &smp); |
| 3363 | else |
| 3364 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3365 | return 1; |
| 3366 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3367 | |
| 3368 | /* |
| 3369 | * |
| 3370 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3371 | * Class Converters |
| 3372 | * |
| 3373 | * |
| 3374 | */ |
| 3375 | |
| 3376 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3377 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3378 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3379 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3380 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3381 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3382 | } |
| 3383 | |
| 3384 | /* This function creates and push in the stack a Converters object |
| 3385 | * according with a current TXN. |
| 3386 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3387 | 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] | 3388 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3389 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3390 | |
| 3391 | /* Check stack size. */ |
| 3392 | if (!lua_checkstack(L, 3)) |
| 3393 | return 0; |
| 3394 | |
| 3395 | /* Create the object: obj[0] = userdata. |
| 3396 | * Note that the base of the Converters object is the |
| 3397 | * same than the TXN object. |
| 3398 | */ |
| 3399 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3400 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3401 | lua_rawseti(L, -2, 0); |
| 3402 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3403 | hsmp->s = txn->s; |
| 3404 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3405 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3406 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3407 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3408 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3409 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3410 | lua_setmetatable(L, -2); |
| 3411 | |
| 3412 | return 1; |
| 3413 | } |
| 3414 | |
| 3415 | /* This function is an LUA binding. It is called with each converter. |
| 3416 | * It uses closure argument to store the associated converter. It |
| 3417 | * returns only one argument or throws an error. An error is thrown |
| 3418 | * only if an error is encountered during the argument parsing. If |
| 3419 | * the converter function function fails, nil is returned. |
| 3420 | */ |
| 3421 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3422 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3423 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3424 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3425 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3426 | int i; |
| 3427 | struct sample smp; |
| 3428 | |
| 3429 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3430 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3431 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3432 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3433 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3434 | |
| 3435 | /* Get extra arguments. */ |
| 3436 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3437 | if (i >= ARGM_NBARGS) |
| 3438 | break; |
| 3439 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3440 | } |
| 3441 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3442 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3443 | |
| 3444 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3445 | 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] | 3446 | |
| 3447 | /* Run the special args checker. */ |
| 3448 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3449 | hlua_pusherror(L, "error in arguments"); |
| 3450 | WILL_LJMP(lua_error(L)); |
| 3451 | } |
| 3452 | |
| 3453 | /* Initialise the sample. */ |
| 3454 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3455 | hlua_pusherror(L, "error in the input argument"); |
| 3456 | WILL_LJMP(lua_error(L)); |
| 3457 | } |
| 3458 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3459 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3460 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3461 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3462 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3463 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3464 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3465 | WILL_LJMP(lua_error(L)); |
| 3466 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3467 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3468 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3469 | hlua_pusherror(L, "error during the input argument casting"); |
| 3470 | WILL_LJMP(lua_error(L)); |
| 3471 | } |
| 3472 | |
| 3473 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3474 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3475 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3476 | lua_pushstring(L, ""); |
| 3477 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3478 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3479 | return 1; |
| 3480 | } |
| 3481 | |
| 3482 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3483 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3484 | hlua_smp2lua_str(L, &smp); |
| 3485 | else |
| 3486 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3487 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3488 | } |
| 3489 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3490 | /* |
| 3491 | * |
| 3492 | * |
| 3493 | * Class AppletTCP |
| 3494 | * |
| 3495 | * |
| 3496 | */ |
| 3497 | |
| 3498 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3499 | * a class stream, otherwise it throws an error. |
| 3500 | */ |
| 3501 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3502 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3503 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3504 | } |
| 3505 | |
| 3506 | /* This function creates and push in the stack an Applet object |
| 3507 | * according with a current TXN. |
| 3508 | */ |
| 3509 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3510 | { |
| 3511 | struct hlua_appctx *appctx; |
| 3512 | struct stream_interface *si = ctx->owner; |
| 3513 | struct stream *s = si_strm(si); |
| 3514 | struct proxy *p = s->be; |
| 3515 | |
| 3516 | /* Check stack size. */ |
| 3517 | if (!lua_checkstack(L, 3)) |
| 3518 | return 0; |
| 3519 | |
| 3520 | /* Create the object: obj[0] = userdata. |
| 3521 | * Note that the base of the Converters object is the |
| 3522 | * same than the TXN object. |
| 3523 | */ |
| 3524 | lua_newtable(L); |
| 3525 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3526 | lua_rawseti(L, -2, 0); |
| 3527 | appctx->appctx = ctx; |
| 3528 | appctx->htxn.s = s; |
| 3529 | appctx->htxn.p = p; |
| 3530 | |
| 3531 | /* Create the "f" field that contains a list of fetches. */ |
| 3532 | lua_pushstring(L, "f"); |
| 3533 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3534 | return 0; |
| 3535 | lua_settable(L, -3); |
| 3536 | |
| 3537 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3538 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3539 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3540 | return 0; |
| 3541 | lua_settable(L, -3); |
| 3542 | |
| 3543 | /* Create the "c" field that contains a list of converters. */ |
| 3544 | lua_pushstring(L, "c"); |
| 3545 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3546 | return 0; |
| 3547 | lua_settable(L, -3); |
| 3548 | |
| 3549 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3550 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3551 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3552 | return 0; |
| 3553 | lua_settable(L, -3); |
| 3554 | |
| 3555 | /* Pop a class stream metatable and affect it to the table. */ |
| 3556 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3557 | lua_setmetatable(L, -2); |
| 3558 | |
| 3559 | return 1; |
| 3560 | } |
| 3561 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3562 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3563 | { |
| 3564 | struct hlua_appctx *appctx; |
| 3565 | struct stream *s; |
| 3566 | const char *name; |
| 3567 | size_t len; |
| 3568 | struct sample smp; |
| 3569 | |
| 3570 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3571 | |
| 3572 | /* It is useles to retrieve the stream, but this function |
| 3573 | * runs only in a stream context. |
| 3574 | */ |
| 3575 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3576 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3577 | s = appctx->htxn.s; |
| 3578 | |
| 3579 | /* Converts the third argument in a sample. */ |
| 3580 | hlua_lua2smp(L, 3, &smp); |
| 3581 | |
| 3582 | /* Store the sample in a variable. */ |
| 3583 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3584 | vars_set_by_name(name, len, &smp); |
| 3585 | return 0; |
| 3586 | } |
| 3587 | |
| 3588 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3589 | { |
| 3590 | struct hlua_appctx *appctx; |
| 3591 | struct stream *s; |
| 3592 | const char *name; |
| 3593 | size_t len; |
| 3594 | struct sample smp; |
| 3595 | |
| 3596 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3597 | |
| 3598 | /* It is useles to retrieve the stream, but this function |
| 3599 | * runs only in a stream context. |
| 3600 | */ |
| 3601 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3602 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3603 | s = appctx->htxn.s; |
| 3604 | |
| 3605 | /* Unset the variable. */ |
| 3606 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3607 | vars_unset_by_name(name, len, &smp); |
| 3608 | return 0; |
| 3609 | } |
| 3610 | |
| 3611 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3612 | { |
| 3613 | struct hlua_appctx *appctx; |
| 3614 | struct stream *s; |
| 3615 | const char *name; |
| 3616 | size_t len; |
| 3617 | struct sample smp; |
| 3618 | |
| 3619 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3620 | |
| 3621 | /* It is useles to retrieve the stream, but this function |
| 3622 | * runs only in a stream context. |
| 3623 | */ |
| 3624 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3625 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3626 | s = appctx->htxn.s; |
| 3627 | |
| 3628 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3629 | if (!vars_get_by_name(name, len, &smp)) { |
| 3630 | lua_pushnil(L); |
| 3631 | return 1; |
| 3632 | } |
| 3633 | |
| 3634 | return hlua_smp2lua(L, &smp); |
| 3635 | } |
| 3636 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3637 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3638 | { |
| 3639 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3640 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3641 | struct hlua *hlua; |
| 3642 | |
| 3643 | /* 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] | 3644 | if (!s->hlua) |
| 3645 | return 0; |
| 3646 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3647 | |
| 3648 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3649 | |
| 3650 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3651 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3652 | |
| 3653 | /* Get and store new value. */ |
| 3654 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3655 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3656 | |
| 3657 | return 0; |
| 3658 | } |
| 3659 | |
| 3660 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3661 | { |
| 3662 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3663 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3664 | struct hlua *hlua; |
| 3665 | |
| 3666 | /* 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] | 3667 | if (!s->hlua) { |
| 3668 | lua_pushnil(L); |
| 3669 | return 1; |
| 3670 | } |
| 3671 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3672 | |
| 3673 | /* Push configuration index in the stack. */ |
| 3674 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3675 | |
| 3676 | return 1; |
| 3677 | } |
| 3678 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3679 | /* If expected data not yet available, it returns a yield. This function |
| 3680 | * consumes the data in the buffer. It returns a string containing the |
| 3681 | * data. This string can be empty. |
| 3682 | */ |
| 3683 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3684 | { |
| 3685 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3686 | struct stream_interface *si = appctx->appctx->owner; |
| 3687 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3688 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3689 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3690 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3691 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3692 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3693 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3694 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3695 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3696 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3697 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3698 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3699 | 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] | 3700 | } |
| 3701 | |
| 3702 | /* End of data: commit the total strings and return. */ |
| 3703 | if (ret < 0) { |
| 3704 | luaL_pushresult(&appctx->b); |
| 3705 | return 1; |
| 3706 | } |
| 3707 | |
| 3708 | /* Ensure that the block 2 length is usable. */ |
| 3709 | if (ret == 1) |
| 3710 | len2 = 0; |
| 3711 | |
| 3712 | /* dont check the max length read and dont check. */ |
| 3713 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3714 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3715 | |
| 3716 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3717 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3718 | luaL_pushresult(&appctx->b); |
| 3719 | return 1; |
| 3720 | } |
| 3721 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3722 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3723 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3724 | { |
| 3725 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3726 | |
| 3727 | /* Initialise the string catenation. */ |
| 3728 | luaL_buffinit(L, &appctx->b); |
| 3729 | |
| 3730 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3731 | } |
| 3732 | |
| 3733 | /* If expected data not yet available, it returns a yield. This function |
| 3734 | * consumes the data in the buffer. It returns a string containing the |
| 3735 | * data. This string can be empty. |
| 3736 | */ |
| 3737 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3738 | { |
| 3739 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3740 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3741 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3742 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3743 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3744 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3745 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3746 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3747 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3748 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3749 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3750 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3751 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3752 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3753 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3754 | 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] | 3755 | } |
| 3756 | |
| 3757 | /* End of data: commit the total strings and return. */ |
| 3758 | if (ret < 0) { |
| 3759 | luaL_pushresult(&appctx->b); |
| 3760 | return 1; |
| 3761 | } |
| 3762 | |
| 3763 | /* Ensure that the block 2 length is usable. */ |
| 3764 | if (ret == 1) |
| 3765 | len2 = 0; |
| 3766 | |
| 3767 | if (len == -1) { |
| 3768 | |
| 3769 | /* If len == -1, catenate all the data avalaile and |
| 3770 | * yield because we want to get all the data until |
| 3771 | * the end of data stream. |
| 3772 | */ |
| 3773 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3774 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3775 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3776 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3777 | 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] | 3778 | |
| 3779 | } else { |
| 3780 | |
| 3781 | /* Copy the fisrt block caping to the length required. */ |
| 3782 | if (len1 > len) |
| 3783 | len1 = len; |
| 3784 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3785 | len -= len1; |
| 3786 | |
| 3787 | /* Copy the second block. */ |
| 3788 | if (len2 > len) |
| 3789 | len2 = len; |
| 3790 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3791 | len -= len2; |
| 3792 | |
| 3793 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3794 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3795 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3796 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3797 | if (len > 0) { |
| 3798 | lua_pushinteger(L, len); |
| 3799 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3800 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3801 | 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] | 3802 | } |
| 3803 | |
| 3804 | /* return the result. */ |
| 3805 | luaL_pushresult(&appctx->b); |
| 3806 | return 1; |
| 3807 | } |
| 3808 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3809 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3810 | hlua_pusherror(L, "Lua: internal error"); |
| 3811 | WILL_LJMP(lua_error(L)); |
| 3812 | return 0; |
| 3813 | } |
| 3814 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3815 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3816 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3817 | { |
| 3818 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3819 | int len = -1; |
| 3820 | |
| 3821 | if (lua_gettop(L) > 2) |
| 3822 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3823 | if (lua_gettop(L) >= 2) { |
| 3824 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3825 | lua_pop(L, 1); |
| 3826 | } |
| 3827 | |
| 3828 | /* Confirm or set the required length */ |
| 3829 | lua_pushinteger(L, len); |
| 3830 | |
| 3831 | /* Initialise the string catenation. */ |
| 3832 | luaL_buffinit(L, &appctx->b); |
| 3833 | |
| 3834 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3835 | } |
| 3836 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3837 | /* Append data in the output side of the buffer. This data is immediately |
| 3838 | * sent. The function returns the amount of data written. If the buffer |
| 3839 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3840 | * if the channel is closed. |
| 3841 | */ |
| 3842 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3843 | { |
| 3844 | size_t len; |
| 3845 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3846 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3847 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3848 | struct stream_interface *si = appctx->appctx->owner; |
| 3849 | struct channel *chn = si_ic(si); |
| 3850 | int max; |
| 3851 | |
| 3852 | /* Get the max amount of data which can write as input in the channel. */ |
| 3853 | max = channel_recv_max(chn); |
| 3854 | if (max > (len - l)) |
| 3855 | max = len - l; |
| 3856 | |
| 3857 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3858 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3859 | |
| 3860 | /* update counters. */ |
| 3861 | l += max; |
| 3862 | lua_pop(L, 1); |
| 3863 | lua_pushinteger(L, l); |
| 3864 | |
| 3865 | /* If some data is not send, declares the situation to the |
| 3866 | * applet, and returns a yield. |
| 3867 | */ |
| 3868 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3869 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3870 | 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] | 3871 | } |
| 3872 | |
| 3873 | return 1; |
| 3874 | } |
| 3875 | |
| 3876 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3877 | * yield the LUA process, and resume it without checking the |
| 3878 | * input arguments. |
| 3879 | */ |
| 3880 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3881 | { |
| 3882 | MAY_LJMP(check_args(L, 2, "send")); |
| 3883 | lua_pushinteger(L, 0); |
| 3884 | |
| 3885 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3886 | } |
| 3887 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3888 | /* |
| 3889 | * |
| 3890 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3891 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3892 | * |
| 3893 | * |
| 3894 | */ |
| 3895 | |
| 3896 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3897 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3898 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3899 | __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] | 3900 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3901 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3902 | } |
| 3903 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3904 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3905 | * according with a current TXN. |
| 3906 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3907 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
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 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3910 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3911 | struct stream_interface *si = ctx->owner; |
| 3912 | struct stream *s = si_strm(si); |
| 3913 | struct proxy *px = s->be; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3914 | |
| 3915 | /* Check stack size. */ |
| 3916 | if (!lua_checkstack(L, 3)) |
| 3917 | return 0; |
| 3918 | |
| 3919 | /* Create the object: obj[0] = userdata. |
| 3920 | * Note that the base of the Converters object is the |
| 3921 | * same than the TXN object. |
| 3922 | */ |
| 3923 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3924 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3925 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3926 | appctx->appctx = ctx; |
| 3927 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3928 | 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] | 3929 | appctx->htxn.s = s; |
| 3930 | appctx->htxn.p = px; |
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 | /* Create the "f" field that contains a list of fetches. */ |
| 3933 | lua_pushstring(L, "f"); |
| 3934 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3935 | return 0; |
| 3936 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3937 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3938 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3939 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3940 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3941 | return 0; |
| 3942 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3943 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3944 | /* Create the "c" field that contains a list of converters. */ |
| 3945 | lua_pushstring(L, "c"); |
| 3946 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3947 | return 0; |
| 3948 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3949 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3950 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3951 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3952 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3953 | return 0; |
| 3954 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3955 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3956 | if (IS_HTX_STRM(s)) { |
| 3957 | /* HTX version */ |
| 3958 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3959 | struct htx_blk *blk; |
| 3960 | struct htx_sl *sl; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3961 | struct ist path; |
| 3962 | unsigned long long len = 0; |
| 3963 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3964 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3965 | blk = htx_get_first_blk(htx); |
Christopher Faulet | 43a686d | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 3966 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3967 | sl = htx_get_blk_ptr(htx, blk); |
| 3968 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3969 | /* Stores the request method. */ |
| 3970 | lua_pushstring(L, "method"); |
| 3971 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3972 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3973 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3974 | /* Stores the http version. */ |
| 3975 | lua_pushstring(L, "version"); |
| 3976 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3977 | lua_settable(L, -3); |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3978 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3979 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3980 | * the array on the top of the stack. |
| 3981 | */ |
| 3982 | lua_pushstring(L, "headers"); |
| 3983 | htxn.s = s; |
| 3984 | htxn.p = px; |
| 3985 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3986 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3987 | return 0; |
| 3988 | lua_settable(L, -3); |
| 3989 | |
| 3990 | path = http_get_path(htx_sl_req_uri(sl)); |
| 3991 | if (path.ptr) { |
| 3992 | char *p, *q, *end; |
| 3993 | |
| 3994 | p = path.ptr; |
| 3995 | end = path.ptr + path.len; |
| 3996 | q = p; |
| 3997 | while (q < end && *q != '?') |
| 3998 | q++; |
| 3999 | |
| 4000 | /* Stores the request path. */ |
| 4001 | lua_pushstring(L, "path"); |
| 4002 | lua_pushlstring(L, p, q - p); |
| 4003 | lua_settable(L, -3); |
| 4004 | |
| 4005 | /* Stores the query string. */ |
| 4006 | lua_pushstring(L, "qs"); |
| 4007 | if (*q == '?') |
| 4008 | q++; |
| 4009 | lua_pushlstring(L, q, end - q); |
| 4010 | lua_settable(L, -3); |
| 4011 | } |
| 4012 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4013 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4014 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4015 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4016 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4017 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4018 | break; |
| 4019 | if (type == HTX_BLK_DATA) |
| 4020 | len += htx_get_blksz(blk); |
| 4021 | } |
| 4022 | if (htx->extra != ULLONG_MAX) |
| 4023 | len += htx->extra; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4024 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4025 | /* Stores the request path. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4026 | lua_pushstring(L, "length"); |
| 4027 | lua_pushinteger(L, len); |
| 4028 | lua_settable(L, -3); |
| 4029 | } |
| 4030 | else { |
| 4031 | /* Legacy HTTP version */ |
| 4032 | struct http_txn *txn = s->txn; |
| 4033 | const char *path; |
| 4034 | const char *end; |
| 4035 | const char *p; |
| 4036 | |
| 4037 | /* Stores the request method. */ |
| 4038 | lua_pushstring(L, "method"); |
| 4039 | lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4040 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4041 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4042 | /* Stores the http version. */ |
| 4043 | lua_pushstring(L, "version"); |
| 4044 | lua_pushlstring(L, ci_head(txn->req.chn) + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4045 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4046 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4047 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4048 | * the array on the top of the stack. |
| 4049 | */ |
| 4050 | lua_pushstring(L, "headers"); |
| 4051 | htxn.s = s; |
| 4052 | htxn.p = px; |
| 4053 | htxn.dir = SMP_OPT_DIR_REQ; |
| 4054 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 4055 | return 0; |
| 4056 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4057 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4058 | /* Get path and qs */ |
| 4059 | path = http_txn_get_path(txn); |
| 4060 | if (path) { |
| 4061 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 4062 | p = path; |
| 4063 | while (p < end && *p != '?') |
| 4064 | p++; |
| 4065 | |
| 4066 | /* Stores the request path. */ |
| 4067 | lua_pushstring(L, "path"); |
| 4068 | lua_pushlstring(L, path, p - path); |
| 4069 | lua_settable(L, -3); |
| 4070 | |
| 4071 | /* Stores the query string. */ |
| 4072 | lua_pushstring(L, "qs"); |
| 4073 | if (*p == '?') |
| 4074 | p++; |
| 4075 | lua_pushlstring(L, p, end - p); |
| 4076 | lua_settable(L, -3); |
| 4077 | } |
| 4078 | |
| 4079 | /* Stores the request path. */ |
| 4080 | lua_pushstring(L, "length"); |
| 4081 | lua_pushinteger(L, txn->req.body_len); |
| 4082 | lua_settable(L, -3); |
| 4083 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4084 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4085 | /* Create an empty array of HTTP request headers. */ |
| 4086 | lua_pushstring(L, "response"); |
| 4087 | lua_newtable(L); |
| 4088 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4089 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4090 | /* Pop a class stream metatable and affect it to the table. */ |
| 4091 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4092 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4093 | |
| 4094 | return 1; |
| 4095 | } |
| 4096 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4097 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4098 | { |
| 4099 | struct hlua_appctx *appctx; |
| 4100 | struct stream *s; |
| 4101 | const char *name; |
| 4102 | size_t len; |
| 4103 | struct sample smp; |
| 4104 | |
| 4105 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4106 | |
| 4107 | /* It is useles to retrieve the stream, but this function |
| 4108 | * runs only in a stream context. |
| 4109 | */ |
| 4110 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4111 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4112 | s = appctx->htxn.s; |
| 4113 | |
| 4114 | /* Converts the third argument in a sample. */ |
| 4115 | hlua_lua2smp(L, 3, &smp); |
| 4116 | |
| 4117 | /* Store the sample in a variable. */ |
| 4118 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4119 | vars_set_by_name(name, len, &smp); |
| 4120 | return 0; |
| 4121 | } |
| 4122 | |
| 4123 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4124 | { |
| 4125 | struct hlua_appctx *appctx; |
| 4126 | struct stream *s; |
| 4127 | const char *name; |
| 4128 | size_t len; |
| 4129 | struct sample smp; |
| 4130 | |
| 4131 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4132 | |
| 4133 | /* It is useles to retrieve the stream, but this function |
| 4134 | * runs only in a stream context. |
| 4135 | */ |
| 4136 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4137 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4138 | s = appctx->htxn.s; |
| 4139 | |
| 4140 | /* Unset the variable. */ |
| 4141 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4142 | vars_unset_by_name(name, len, &smp); |
| 4143 | return 0; |
| 4144 | } |
| 4145 | |
| 4146 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4147 | { |
| 4148 | struct hlua_appctx *appctx; |
| 4149 | struct stream *s; |
| 4150 | const char *name; |
| 4151 | size_t len; |
| 4152 | struct sample smp; |
| 4153 | |
| 4154 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4155 | |
| 4156 | /* It is useles to retrieve the stream, but this function |
| 4157 | * runs only in a stream context. |
| 4158 | */ |
| 4159 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4160 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4161 | s = appctx->htxn.s; |
| 4162 | |
| 4163 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4164 | if (!vars_get_by_name(name, len, &smp)) { |
| 4165 | lua_pushnil(L); |
| 4166 | return 1; |
| 4167 | } |
| 4168 | |
| 4169 | return hlua_smp2lua(L, &smp); |
| 4170 | } |
| 4171 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4172 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4173 | { |
| 4174 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4175 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4176 | struct hlua *hlua; |
| 4177 | |
| 4178 | /* 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] | 4179 | if (!s->hlua) |
| 4180 | return 0; |
| 4181 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4182 | |
| 4183 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4184 | |
| 4185 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4186 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4187 | |
| 4188 | /* Get and store new value. */ |
| 4189 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4190 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4191 | |
| 4192 | return 0; |
| 4193 | } |
| 4194 | |
| 4195 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4196 | { |
| 4197 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4198 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4199 | struct hlua *hlua; |
| 4200 | |
| 4201 | /* 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] | 4202 | if (!s->hlua) { |
| 4203 | lua_pushnil(L); |
| 4204 | return 1; |
| 4205 | } |
| 4206 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4207 | |
| 4208 | /* Push configuration index in the stack. */ |
| 4209 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4210 | |
| 4211 | return 1; |
| 4212 | } |
| 4213 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4214 | /* If expected data not yet available, it returns a yield. This function |
| 4215 | * consumes the data in the buffer. It returns a string containing the |
| 4216 | * data. This string can be empty. |
| 4217 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4218 | __LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4219 | { |
| 4220 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4221 | struct stream_interface *si = appctx->appctx->owner; |
| 4222 | struct channel *req = si_oc(si); |
| 4223 | struct htx *htx; |
| 4224 | struct htx_blk *blk; |
| 4225 | size_t count; |
| 4226 | int stop = 0; |
| 4227 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4228 | htx = htx_from_buf(&req->buf); |
| 4229 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4230 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4231 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4232 | while (count && !stop && blk) { |
| 4233 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4234 | uint32_t sz = htx_get_blksz(blk); |
| 4235 | struct ist v; |
| 4236 | uint32_t vlen; |
| 4237 | char *nl; |
| 4238 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4239 | if (type == HTX_BLK_EOM) { |
| 4240 | stop = 1; |
| 4241 | break; |
| 4242 | } |
| 4243 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4244 | vlen = sz; |
| 4245 | if (vlen > count) { |
| 4246 | if (type != HTX_BLK_DATA) |
| 4247 | break; |
| 4248 | vlen = count; |
| 4249 | } |
| 4250 | |
| 4251 | switch (type) { |
| 4252 | case HTX_BLK_UNUSED: |
| 4253 | break; |
| 4254 | |
| 4255 | case HTX_BLK_DATA: |
| 4256 | v = htx_get_blk_value(htx, blk); |
| 4257 | v.len = vlen; |
| 4258 | nl = istchr(v, '\n'); |
| 4259 | if (nl != NULL) { |
| 4260 | stop = 1; |
| 4261 | vlen = nl - v.ptr + 1; |
| 4262 | } |
| 4263 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4264 | break; |
| 4265 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4266 | case HTX_BLK_TLR: |
| 4267 | case HTX_BLK_EOM: |
| 4268 | stop = 1; |
| 4269 | break; |
| 4270 | |
| 4271 | default: |
| 4272 | break; |
| 4273 | } |
| 4274 | |
| 4275 | co_set_data(req, co_data(req) - vlen); |
| 4276 | count -= vlen; |
| 4277 | if (sz == vlen) |
| 4278 | blk = htx_remove_blk(htx, blk); |
| 4279 | else { |
| 4280 | htx_cut_data_blk(htx, blk, vlen); |
| 4281 | break; |
| 4282 | } |
| 4283 | } |
| 4284 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4285 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4286 | if (!stop) { |
| 4287 | si_cant_get(si); |
| 4288 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0)); |
| 4289 | } |
| 4290 | |
| 4291 | /* return the result. */ |
| 4292 | luaL_pushresult(&appctx->b); |
| 4293 | return 1; |
| 4294 | } |
| 4295 | |
| 4296 | |
| 4297 | /* If expected data not yet available, it returns a yield. This function |
| 4298 | * consumes the data in the buffer. It returns a string containing the |
| 4299 | * data. This string can be empty. |
| 4300 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4301 | __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] | 4302 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4303 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4304 | struct stream_interface *si = appctx->appctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4305 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4306 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4307 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4308 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4309 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4310 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4311 | /* Check for the end of the data. */ |
| 4312 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4313 | luaL_pushresult(&appctx->b); |
| 4314 | return 1; |
| 4315 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4316 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4317 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4318 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4319 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4320 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4321 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4322 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4323 | 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] | 4324 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4325 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4326 | /* End of data: commit the total strings and return. */ |
| 4327 | if (ret < 0) { |
| 4328 | luaL_pushresult(&appctx->b); |
| 4329 | return 1; |
| 4330 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4331 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4332 | /* Ensure that the block 2 length is usable. */ |
| 4333 | if (ret == 1) |
| 4334 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4335 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4336 | /* Copy the fisrt block caping to the length required. */ |
| 4337 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4338 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4339 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4340 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4341 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4342 | /* Copy the second block. */ |
| 4343 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4344 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4345 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4346 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4347 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4348 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4349 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4350 | luaL_pushresult(&appctx->b); |
| 4351 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4352 | } |
| 4353 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4354 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4355 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4356 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4357 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4358 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4359 | /* Initialise the string catenation. */ |
| 4360 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4361 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4362 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 4363 | return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0)); |
| 4364 | else |
| 4365 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4366 | } |
| 4367 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4368 | /* If expected data not yet available, it returns a yield. This function |
| 4369 | * consumes the data in the buffer. It returns a string containing the |
| 4370 | * data. This string can be empty. |
| 4371 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4372 | __LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4373 | { |
| 4374 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4375 | struct stream_interface *si = appctx->appctx->owner; |
| 4376 | struct channel *req = si_oc(si); |
| 4377 | struct htx *htx; |
| 4378 | struct htx_blk *blk; |
| 4379 | size_t count; |
| 4380 | int len; |
| 4381 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4382 | htx = htx_from_buf(&req->buf); |
| 4383 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4384 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4385 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4386 | while (count && len && blk) { |
| 4387 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4388 | uint32_t sz = htx_get_blksz(blk); |
| 4389 | struct ist v; |
| 4390 | uint32_t vlen; |
| 4391 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4392 | if (type == HTX_BLK_EOM) { |
| 4393 | len = 0; |
| 4394 | break; |
| 4395 | } |
| 4396 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4397 | vlen = sz; |
| 4398 | if (len > 0 && vlen > len) |
| 4399 | vlen = len; |
| 4400 | if (vlen > count) { |
| 4401 | if (type != HTX_BLK_DATA) |
| 4402 | break; |
| 4403 | vlen = count; |
| 4404 | } |
| 4405 | |
| 4406 | switch (type) { |
| 4407 | case HTX_BLK_UNUSED: |
| 4408 | break; |
| 4409 | |
| 4410 | case HTX_BLK_DATA: |
| 4411 | v = htx_get_blk_value(htx, blk); |
| 4412 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4413 | break; |
| 4414 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4415 | case HTX_BLK_TLR: |
| 4416 | case HTX_BLK_EOM: |
| 4417 | len = 0; |
| 4418 | break; |
| 4419 | |
| 4420 | default: |
| 4421 | break; |
| 4422 | } |
| 4423 | |
| 4424 | co_set_data(req, co_data(req) - vlen); |
| 4425 | count -= vlen; |
| 4426 | if (len > 0) |
| 4427 | len -= vlen; |
| 4428 | if (sz == vlen) |
| 4429 | blk = htx_remove_blk(htx, blk); |
| 4430 | else { |
| 4431 | htx_cut_data_blk(htx, blk, vlen); |
| 4432 | break; |
| 4433 | } |
| 4434 | } |
| 4435 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4436 | htx_to_buf(htx, &req->buf); |
| 4437 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4438 | /* If we are no other data available, yield waiting for new data. */ |
| 4439 | if (len) { |
| 4440 | if (len > 0) { |
| 4441 | lua_pushinteger(L, len); |
| 4442 | lua_replace(L, 2); |
| 4443 | } |
| 4444 | si_cant_get(si); |
| 4445 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0)); |
| 4446 | } |
| 4447 | |
| 4448 | /* return the result. */ |
| 4449 | luaL_pushresult(&appctx->b); |
| 4450 | return 1; |
| 4451 | } |
| 4452 | |
| 4453 | /* If expected data not yet available, it returns a yield. This function |
| 4454 | * consumes the data in the buffer. It returns a string containing the |
| 4455 | * data. This string can be empty. |
| 4456 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4457 | __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] | 4458 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4459 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4460 | struct stream_interface *si = appctx->appctx->owner; |
| 4461 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4462 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4463 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4464 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4465 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4466 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4467 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4468 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4469 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4470 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4471 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4472 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4473 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4474 | 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] | 4475 | } |
| 4476 | |
| 4477 | /* End of data: commit the total strings and return. */ |
| 4478 | if (ret < 0) { |
| 4479 | luaL_pushresult(&appctx->b); |
| 4480 | return 1; |
| 4481 | } |
| 4482 | |
| 4483 | /* Ensure that the block 2 length is usable. */ |
| 4484 | if (ret == 1) |
| 4485 | len2 = 0; |
| 4486 | |
| 4487 | /* Copy the fisrt block caping to the length required. */ |
| 4488 | if (len1 > len) |
| 4489 | len1 = len; |
| 4490 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4491 | len -= len1; |
| 4492 | |
| 4493 | /* Copy the second block. */ |
| 4494 | if (len2 > len) |
| 4495 | len2 = len; |
| 4496 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4497 | len -= len2; |
| 4498 | |
| 4499 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4500 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4501 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4502 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4503 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4504 | /* If we are no other data available, yield waiting for new data. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4505 | if (len > 0) { |
| 4506 | lua_pushinteger(L, len); |
| 4507 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4508 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4509 | 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] | 4510 | } |
| 4511 | |
| 4512 | /* return the result. */ |
| 4513 | luaL_pushresult(&appctx->b); |
| 4514 | return 1; |
| 4515 | } |
| 4516 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4517 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4518 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4519 | { |
| 4520 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4521 | int len = -1; |
| 4522 | |
| 4523 | /* Check arguments. */ |
| 4524 | if (lua_gettop(L) > 2) |
| 4525 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4526 | if (lua_gettop(L) >= 2) { |
| 4527 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4528 | lua_pop(L, 1); |
| 4529 | } |
| 4530 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4531 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4532 | /* HTX version */ |
| 4533 | lua_pushinteger(L, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4534 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4535 | /* Initialise the string catenation. */ |
| 4536 | luaL_buffinit(L, &appctx->b); |
| 4537 | |
| 4538 | return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0)); |
| 4539 | } |
| 4540 | else { |
| 4541 | /* Legacy HTTP version */ |
| 4542 | /* Check the required length */ |
| 4543 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4544 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4545 | lua_pushinteger(L, len); |
| 4546 | |
| 4547 | /* Initialise the string catenation. */ |
| 4548 | luaL_buffinit(L, &appctx->b); |
| 4549 | |
| 4550 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4551 | } |
| 4552 | } |
| 4553 | |
| 4554 | /* Append data in the output side of the buffer. This data is immediately |
| 4555 | * sent. The function returns the amount of data written. If the buffer |
| 4556 | * cannot contain the data, the function yields. The function returns -1 |
| 4557 | * if the channel is closed. |
| 4558 | */ |
| 4559 | __LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4560 | { |
| 4561 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4562 | struct stream_interface *si = appctx->appctx->owner; |
| 4563 | struct channel *res = si_ic(si); |
| 4564 | struct htx *htx = htx_from_buf(&res->buf); |
| 4565 | const char *data; |
| 4566 | size_t len; |
| 4567 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4568 | int max; |
| 4569 | |
Christopher Faulet | 4e96301 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4570 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4571 | if (!max) |
| 4572 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4573 | |
| 4574 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4575 | |
| 4576 | /* Get the max amount of data which can write as input in the channel. */ |
| 4577 | if (max > (len - l)) |
| 4578 | max = len - l; |
| 4579 | |
| 4580 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4581 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4582 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4583 | |
| 4584 | /* update counters. */ |
| 4585 | l += max; |
| 4586 | lua_pop(L, 1); |
| 4587 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4588 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4589 | /* If some data is not send, declares the situation to the |
| 4590 | * applet, and returns a yield. |
| 4591 | */ |
| 4592 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4593 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4594 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4595 | si_rx_room_blk(si); |
| 4596 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0)); |
| 4597 | } |
| 4598 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4599 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4600 | return 1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4601 | } |
| 4602 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4603 | /* Append data in the output side of the buffer. This data is immediately |
| 4604 | * sent. The function returns the amount of data written. If the buffer |
| 4605 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4606 | * if the channel is closed. |
| 4607 | */ |
| 4608 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4609 | { |
| 4610 | size_t len; |
| 4611 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4612 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4613 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4614 | struct stream_interface *si = appctx->appctx->owner; |
| 4615 | struct channel *chn = si_ic(si); |
| 4616 | int max; |
| 4617 | |
| 4618 | /* Get the max amount of data which can write as input in the channel. */ |
| 4619 | max = channel_recv_max(chn); |
| 4620 | if (max > (len - l)) |
| 4621 | max = len - l; |
| 4622 | |
| 4623 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4624 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4625 | |
| 4626 | /* update counters. */ |
| 4627 | l += max; |
| 4628 | lua_pop(L, 1); |
| 4629 | lua_pushinteger(L, l); |
| 4630 | |
| 4631 | /* If some data is not send, declares the situation to the |
| 4632 | * applet, and returns a yield. |
| 4633 | */ |
| 4634 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4635 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4636 | 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] | 4637 | } |
| 4638 | |
| 4639 | return 1; |
| 4640 | } |
| 4641 | |
| 4642 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4643 | * yield the LUA process, and resume it without checking the |
| 4644 | * input arguments. |
| 4645 | */ |
| 4646 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4647 | { |
| 4648 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4649 | |
| 4650 | /* We want to send some data. Headers must be sent. */ |
| 4651 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4652 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4653 | WILL_LJMP(lua_error(L)); |
| 4654 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4655 | |
| 4656 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4657 | /* HTX version */ |
| 4658 | /* This interger is used for followinf the amount of data sent. */ |
| 4659 | lua_pushinteger(L, 0); |
| 4660 | |
| 4661 | return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0)); |
| 4662 | } |
| 4663 | else { |
| 4664 | /* Legacy HTTP version */ |
| 4665 | size_t len; |
| 4666 | char hex[10]; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4667 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4668 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4669 | |
| 4670 | /* If transfer encoding chunked is selected, we surround the data |
| 4671 | * by chunk data. |
| 4672 | */ |
| 4673 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4674 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4675 | lua_pushfstring(L, "%s\r\n", hex); |
| 4676 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4677 | lua_pushstring(L, "\r\n"); |
| 4678 | lua_concat(L, 3); |
| 4679 | } |
| 4680 | |
| 4681 | /* This interger is used for followinf the amount of data sent. */ |
| 4682 | lua_pushinteger(L, 0); |
| 4683 | |
| 4684 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4685 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4686 | } |
| 4687 | |
| 4688 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4689 | { |
| 4690 | const char *name; |
| 4691 | int ret; |
| 4692 | |
| 4693 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4694 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4695 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4696 | |
| 4697 | /* Push in the stack the "response" entry. */ |
| 4698 | ret = lua_getfield(L, 1, "response"); |
| 4699 | if (ret != LUA_TTABLE) { |
| 4700 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4701 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4702 | WILL_LJMP(lua_error(L)); |
| 4703 | } |
| 4704 | |
| 4705 | /* check if the header is already registered if it is not |
| 4706 | * the case, register it. |
| 4707 | */ |
| 4708 | ret = lua_getfield(L, -1, name); |
| 4709 | if (ret == LUA_TNIL) { |
| 4710 | |
| 4711 | /* Entry not found. */ |
| 4712 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4713 | |
| 4714 | /* Insert the new header name in the array in the top of the stack. |
| 4715 | * It left the new array in the top of the stack. |
| 4716 | */ |
| 4717 | lua_newtable(L); |
| 4718 | lua_pushvalue(L, 2); |
| 4719 | lua_pushvalue(L, -2); |
| 4720 | lua_settable(L, -4); |
| 4721 | |
| 4722 | } else if (ret != LUA_TTABLE) { |
| 4723 | |
| 4724 | /* corruption error. */ |
| 4725 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4726 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4727 | WILL_LJMP(lua_error(L)); |
| 4728 | } |
| 4729 | |
| 4730 | /* Now the top od thestack is an array of values. We push |
| 4731 | * the header value as new entry. |
| 4732 | */ |
| 4733 | lua_pushvalue(L, 3); |
| 4734 | ret = lua_rawlen(L, -2); |
| 4735 | lua_rawseti(L, -2, ret + 1); |
| 4736 | lua_pushboolean(L, 1); |
| 4737 | return 1; |
| 4738 | } |
| 4739 | |
| 4740 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4741 | { |
| 4742 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4743 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4744 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4745 | |
| 4746 | if (status < 100 || status > 599) { |
| 4747 | lua_pushboolean(L, 0); |
| 4748 | return 1; |
| 4749 | } |
| 4750 | |
| 4751 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4752 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4753 | lua_pushboolean(L, 1); |
| 4754 | return 1; |
| 4755 | } |
| 4756 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4757 | |
| 4758 | __LJMP static int hlua_applet_htx_send_response(lua_State *L) |
| 4759 | { |
| 4760 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4761 | struct stream_interface *si = appctx->appctx->owner; |
| 4762 | struct channel *res = si_ic(si); |
| 4763 | struct htx *htx; |
| 4764 | struct htx_sl *sl; |
| 4765 | struct h1m h1m; |
| 4766 | const char *status, *reason; |
| 4767 | const char *name, *value; |
| 4768 | size_t nlen, vlen; |
| 4769 | unsigned int flags; |
| 4770 | |
| 4771 | /* Send the message at once. */ |
| 4772 | htx = htx_from_buf(&res->buf); |
| 4773 | h1m_init_res(&h1m); |
| 4774 | |
| 4775 | /* Use the same http version than the request. */ |
| 4776 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4777 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4778 | if (reason == NULL) |
| 4779 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4780 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4781 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4782 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4783 | } |
| 4784 | else { |
| 4785 | flags = HTX_SL_F_IS_RESP; |
| 4786 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4787 | } |
| 4788 | if (!sl) { |
| 4789 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4790 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4791 | WILL_LJMP(lua_error(L)); |
| 4792 | } |
| 4793 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4794 | |
| 4795 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4796 | lua_pushvalue(L, 0); |
| 4797 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4798 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4799 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4800 | WILL_LJMP(lua_error(L)); |
| 4801 | } |
| 4802 | |
| 4803 | /* Browse the list of headers. */ |
| 4804 | lua_pushnil(L); |
| 4805 | while(lua_next(L, -2) != 0) { |
| 4806 | /* We expect a string as -2. */ |
| 4807 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4808 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4809 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4810 | lua_typename(L, lua_type(L, -2))); |
| 4811 | WILL_LJMP(lua_error(L)); |
| 4812 | } |
| 4813 | name = lua_tolstring(L, -2, &nlen); |
| 4814 | |
| 4815 | /* We expect an array as -1. */ |
| 4816 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4817 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4818 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4819 | name, |
| 4820 | lua_typename(L, lua_type(L, -1))); |
| 4821 | WILL_LJMP(lua_error(L)); |
| 4822 | } |
| 4823 | |
| 4824 | /* Browse the table who is on the top of the stack. */ |
| 4825 | lua_pushnil(L); |
| 4826 | while(lua_next(L, -2) != 0) { |
| 4827 | int id; |
| 4828 | |
| 4829 | /* We expect a number as -2. */ |
| 4830 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4831 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4832 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4833 | name, |
| 4834 | lua_typename(L, lua_type(L, -2))); |
| 4835 | WILL_LJMP(lua_error(L)); |
| 4836 | } |
| 4837 | id = lua_tointeger(L, -2); |
| 4838 | |
| 4839 | /* We expect a string as -2. */ |
| 4840 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4841 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4842 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4843 | name, id, |
| 4844 | lua_typename(L, lua_type(L, -1))); |
| 4845 | WILL_LJMP(lua_error(L)); |
| 4846 | } |
| 4847 | value = lua_tolstring(L, -1, &vlen); |
| 4848 | |
| 4849 | /* Simple Protocol checks. */ |
| 4850 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
| 4851 | h1_parse_xfer_enc_header(&h1m, ist2(name, nlen)); |
| 4852 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4853 | struct ist v = ist2(value, vlen); |
| 4854 | int ret; |
| 4855 | |
| 4856 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4857 | if (ret < 0) { |
| 4858 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4859 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4860 | name); |
| 4861 | WILL_LJMP(lua_error(L)); |
| 4862 | } |
| 4863 | else if (ret == 0) |
| 4864 | goto next; /* Skip it */ |
| 4865 | } |
| 4866 | |
| 4867 | /* Add a new header */ |
| 4868 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4869 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4870 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4871 | name); |
| 4872 | WILL_LJMP(lua_error(L)); |
| 4873 | } |
| 4874 | next: |
| 4875 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4876 | lua_pop(L, 1); |
| 4877 | } |
| 4878 | |
| 4879 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4880 | lua_pop(L, 1); |
| 4881 | } |
| 4882 | |
| 4883 | if (h1m.flags & H1_MF_CHNK) |
| 4884 | h1m.flags &= ~H1_MF_CLEN; |
| 4885 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4886 | h1m.flags |= H1_MF_XFER_LEN; |
| 4887 | |
| 4888 | /* Uset HTX start-line flags */ |
| 4889 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4890 | flags |= HTX_SL_F_XFER_ENC; |
| 4891 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4892 | flags |= HTX_SL_F_XFER_LEN; |
| 4893 | if (h1m.flags & H1_MF_CHNK) |
| 4894 | flags |= HTX_SL_F_CHNK; |
| 4895 | else if (h1m.flags & H1_MF_CLEN) |
| 4896 | flags |= HTX_SL_F_CLEN; |
| 4897 | if (h1m.body_len == 0) |
| 4898 | flags |= HTX_SL_F_BODYLESS; |
| 4899 | } |
| 4900 | sl->flags |= flags; |
| 4901 | |
| 4902 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4903 | * and the status code implies the presence of a message body, we must |
| 4904 | * announce a transfer encoding chunked. This is required by haproxy |
| 4905 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4906 | * chunked itslef, don't do anything. |
| 4907 | */ |
| 4908 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4909 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4910 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4911 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4912 | /* Add a new header */ |
| 4913 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4914 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4915 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4916 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4917 | WILL_LJMP(lua_error(L)); |
| 4918 | } |
| 4919 | } |
| 4920 | |
| 4921 | /* Finalize headers. */ |
| 4922 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4923 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4924 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4925 | WILL_LJMP(lua_error(L)); |
| 4926 | } |
| 4927 | |
| 4928 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4929 | b_reset(&res->buf); |
| 4930 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4931 | WILL_LJMP(lua_error(L)); |
| 4932 | } |
| 4933 | |
| 4934 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4935 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4936 | |
| 4937 | /* Headers sent, set the flag. */ |
| 4938 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4939 | return 0; |
| 4940 | |
| 4941 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4942 | /* We will build the status line and the headers of the HTTP response. |
| 4943 | * We will try send at once if its not possible, we give back the hand |
| 4944 | * waiting for more room. |
| 4945 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4946 | __LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4947 | { |
| 4948 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4949 | struct stream_interface *si = appctx->appctx->owner; |
| 4950 | struct channel *res = si_ic(si); |
| 4951 | |
| 4952 | if (co_data(res)) { |
| 4953 | si_rx_room_blk(si); |
| 4954 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0)); |
| 4955 | } |
| 4956 | return MAY_LJMP(hlua_applet_htx_send_response(L)); |
| 4957 | } |
| 4958 | |
| 4959 | |
| 4960 | __LJMP static int hlua_applet_htx_start_response(lua_State *L) |
| 4961 | { |
| 4962 | return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0)); |
| 4963 | } |
| 4964 | |
| 4965 | /* We will build the status line and the headers of the HTTP response. |
| 4966 | * We will try send at once if its not possible, we give back the hand |
| 4967 | * waiting for more room. |
| 4968 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4969 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4970 | { |
| 4971 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4972 | struct stream_interface *si = appctx->appctx->owner; |
| 4973 | struct channel *chn = si_ic(si); |
| 4974 | int ret; |
| 4975 | size_t len; |
| 4976 | const char *msg; |
| 4977 | |
| 4978 | /* Get the message as the first argument on the stack. */ |
| 4979 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4980 | |
| 4981 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4982 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4983 | |
| 4984 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4985 | * big for the buffers. |
| 4986 | */ |
| 4987 | if (ret == -2 || ret == -3) { |
| 4988 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4989 | WILL_LJMP(lua_error(L)); |
| 4990 | } |
| 4991 | |
| 4992 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4993 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4994 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4995 | 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] | 4996 | } |
| 4997 | |
| 4998 | /* Headers sent, set the flag. */ |
| 4999 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 5000 | return 0; |
| 5001 | } |
| 5002 | |
| 5003 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 5004 | { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5005 | struct buffer *tmp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5006 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5007 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5008 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5009 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5010 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5011 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5012 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5013 | int hdr_chunked = 0; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5014 | const char *reason; |
| 5015 | |
| 5016 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 5017 | return MAY_LJMP(hlua_applet_htx_start_response(L)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5018 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5019 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5020 | if (reason == NULL) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 5021 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5022 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5023 | tmp = get_trash_chunk(); |
| 5024 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5025 | /* Use the same http version than the request. */ |
| 5026 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 5027 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5028 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5029 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5030 | |
| 5031 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5032 | lua_pushvalue(L, 0); |
| 5033 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5034 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 5035 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 5036 | WILL_LJMP(lua_error(L)); |
| 5037 | } |
| 5038 | |
| 5039 | /* Browse the list of headers. */ |
| 5040 | lua_pushnil(L); |
| 5041 | while(lua_next(L, -2) != 0) { |
| 5042 | |
| 5043 | /* We expect a string as -2. */ |
| 5044 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5045 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 5046 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5047 | lua_typename(L, lua_type(L, -2))); |
| 5048 | WILL_LJMP(lua_error(L)); |
| 5049 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5050 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5051 | |
| 5052 | /* We expect an array as -1. */ |
| 5053 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5054 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 5055 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5056 | name, |
| 5057 | lua_typename(L, lua_type(L, -1))); |
| 5058 | WILL_LJMP(lua_error(L)); |
| 5059 | } |
| 5060 | |
| 5061 | /* Browse the table who is on the top of the stack. */ |
| 5062 | lua_pushnil(L); |
| 5063 | while(lua_next(L, -2) != 0) { |
| 5064 | |
| 5065 | /* We expect a number as -2. */ |
| 5066 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5067 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 5068 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5069 | name, |
| 5070 | lua_typename(L, lua_type(L, -2))); |
| 5071 | WILL_LJMP(lua_error(L)); |
| 5072 | } |
| 5073 | id = lua_tointeger(L, -2); |
| 5074 | |
| 5075 | /* We expect a string as -2. */ |
| 5076 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5077 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 5078 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5079 | name, id, |
| 5080 | lua_typename(L, lua_type(L, -1))); |
| 5081 | WILL_LJMP(lua_error(L)); |
| 5082 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5083 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5084 | |
| 5085 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5086 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 5087 | memcpy(tmp->area + tmp->data, name, name_len); |
| 5088 | tmp->data += name_len; |
| 5089 | tmp->area[tmp->data++] = ':'; |
| 5090 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5091 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5092 | memcpy(tmp->area + tmp->data, value, |
| 5093 | value_len); |
| 5094 | tmp->data += value_len; |
| 5095 | tmp->area[tmp->data++] = '\r'; |
| 5096 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5097 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5098 | |
| 5099 | /* Protocol checks. */ |
| 5100 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5101 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5102 | * is done without control. If it contains a bad value, |
| 5103 | * the content-length remains negative so that we can |
| 5104 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5105 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5106 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5107 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5108 | |
| 5109 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5110 | if (name_len == 17 && value_len == 7 && |
| 5111 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5112 | strcasecmp("chunked", value) == 0) |
| 5113 | hdr_chunked = 1; |
| 5114 | |
| 5115 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5116 | lua_pop(L, 1); |
| 5117 | } |
| 5118 | |
| 5119 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5120 | lua_pop(L, 1); |
| 5121 | } |
| 5122 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5123 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 5124 | * and the status code implies the presence of a message body, we must |
| 5125 | * announce a transfer encoding chunked. This is required by haproxy |
| 5126 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 5127 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5128 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5129 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5130 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 5131 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5132 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5133 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5134 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 5135 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 5136 | } |
| 5137 | |
| 5138 | /* Finalize headers. */ |
| 5139 | chunk_appendf(tmp, "\r\n"); |
| 5140 | |
| 5141 | /* Remove the last entry and the array of headers */ |
| 5142 | lua_pop(L, 2); |
| 5143 | |
| 5144 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5145 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5146 | |
| 5147 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 5148 | } |
| 5149 | |
| 5150 | /* |
| 5151 | * |
| 5152 | * |
| 5153 | * Class HTTP |
| 5154 | * |
| 5155 | * |
| 5156 | */ |
| 5157 | |
| 5158 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5159 | * a class stream, otherwise it throws an error. |
| 5160 | */ |
| 5161 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 5162 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5163 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5164 | } |
| 5165 | |
| 5166 | /* This function creates and push in the stack a HTTP object |
| 5167 | * according with a current TXN. |
| 5168 | */ |
| 5169 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5170 | { |
| 5171 | struct hlua_txn *htxn; |
| 5172 | |
| 5173 | /* Check stack size. */ |
| 5174 | if (!lua_checkstack(L, 3)) |
| 5175 | return 0; |
| 5176 | |
| 5177 | /* Create the object: obj[0] = userdata. |
| 5178 | * Note that the base of the Converters object is the |
| 5179 | * same than the TXN object. |
| 5180 | */ |
| 5181 | lua_newtable(L); |
| 5182 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5183 | lua_rawseti(L, -2, 0); |
| 5184 | |
| 5185 | htxn->s = txn->s; |
| 5186 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5187 | htxn->dir = txn->dir; |
| 5188 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5189 | |
| 5190 | /* Pop a class stream metatable and affect it to the table. */ |
| 5191 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5192 | lua_setmetatable(L, -2); |
| 5193 | |
| 5194 | return 1; |
| 5195 | } |
| 5196 | |
| 5197 | /* This function creates ans returns an array of HTTP headers. |
| 5198 | * This function does not fails. It is used as wrapper with the |
| 5199 | * 2 following functions. |
| 5200 | */ |
| 5201 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5202 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5203 | /* Create the table. */ |
| 5204 | lua_newtable(L); |
| 5205 | |
| 5206 | if (!htxn->s->txn) |
| 5207 | return 1; |
| 5208 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5209 | if (IS_HTX_STRM(htxn->s)) { |
| 5210 | /* HTX version */ |
| 5211 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5212 | int32_t pos; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5213 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5214 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5215 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5216 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5217 | struct ist n, v; |
| 5218 | int len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5219 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5220 | if (type == HTX_BLK_HDR) { |
| 5221 | n = htx_get_blk_name(htx,blk); |
| 5222 | v = htx_get_blk_value(htx, blk); |
| 5223 | } |
| 5224 | else if (type == HTX_BLK_EOH) |
| 5225 | break; |
| 5226 | else |
| 5227 | continue; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5228 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5229 | /* Check for existing entry: |
| 5230 | * assume that the table is on the top of the stack, and |
| 5231 | * push the key in the stack, the function lua_gettable() |
| 5232 | * perform the lookup. |
| 5233 | */ |
| 5234 | lua_pushlstring(L, n.ptr, n.len); |
| 5235 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5236 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5237 | switch (lua_type(L, -1)) { |
| 5238 | case LUA_TNIL: |
| 5239 | /* Table not found, create it. */ |
| 5240 | lua_pop(L, 1); /* remove the nil value. */ |
| 5241 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5242 | lua_newtable(L); /* create and push empty table. */ |
| 5243 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5244 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5245 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5246 | break; |
| 5247 | |
| 5248 | case LUA_TTABLE: |
| 5249 | /* Entry found: push the value in the table. */ |
| 5250 | len = lua_rawlen(L, -1); |
| 5251 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5252 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5253 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5254 | break; |
| 5255 | |
| 5256 | default: |
| 5257 | /* Other cases are errors. */ |
| 5258 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5259 | WILL_LJMP(lua_error(L)); |
| 5260 | } |
| 5261 | } |
| 5262 | } |
| 5263 | else { |
| 5264 | /* Legacy HTTP version */ |
| 5265 | const char *cur_ptr, *cur_next, *p; |
| 5266 | int old_idx, cur_idx; |
| 5267 | struct hdr_idx_elem *cur_hdr; |
| 5268 | const char *hn, *hv; |
| 5269 | int hnl, hvl; |
| 5270 | const char *in; |
| 5271 | char *out; |
| 5272 | int len; |
| 5273 | |
| 5274 | /* Build array of headers. */ |
| 5275 | old_idx = 0; |
| 5276 | cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 5277 | |
| 5278 | while (1) { |
| 5279 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 5280 | if (!cur_idx) |
| 5281 | break; |
| 5282 | old_idx = cur_idx; |
| 5283 | |
| 5284 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 5285 | cur_ptr = cur_next; |
| 5286 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 5287 | |
| 5288 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 5289 | * and the next header starts at cur_next. We'll check |
| 5290 | * this header in the list as well as against the default |
| 5291 | * rule. |
| 5292 | */ |
| 5293 | |
| 5294 | /* look for ': *'. */ |
| 5295 | hn = cur_ptr; |
| 5296 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 5297 | if (p >= cur_ptr+cur_hdr->len) |
| 5298 | continue; |
| 5299 | hnl = p - hn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5300 | p++; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5301 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 5302 | p++; |
| 5303 | if (p >= cur_ptr+cur_hdr->len) |
| 5304 | continue; |
| 5305 | hv = p; |
| 5306 | hvl = cur_ptr+cur_hdr->len-p; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5307 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5308 | /* Lowercase the key. Don't check the size of trash, it have |
| 5309 | * the size of one buffer and the input data contains in one |
| 5310 | * buffer. |
| 5311 | */ |
| 5312 | out = trash.area; |
| 5313 | for (in=hn; in<hn+hnl; in++, out++) |
| 5314 | *out = tolower(*in); |
| 5315 | *out = '\0'; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5316 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5317 | /* Check for existing entry: |
| 5318 | * assume that the table is on the top of the stack, and |
| 5319 | * push the key in the stack, the function lua_gettable() |
| 5320 | * perform the lookup. |
| 5321 | */ |
| 5322 | lua_pushlstring(L, trash.area, hnl); |
| 5323 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5324 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5325 | switch (lua_type(L, -1)) { |
| 5326 | case LUA_TNIL: |
| 5327 | /* Table not found, create it. */ |
| 5328 | lua_pop(L, 1); /* remove the nil value. */ |
| 5329 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
| 5330 | lua_newtable(L); /* create and push empty table. */ |
| 5331 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5332 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5333 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5334 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5335 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5336 | case LUA_TTABLE: |
| 5337 | /* Entry found: push the value in the table. */ |
| 5338 | len = lua_rawlen(L, -1); |
| 5339 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5340 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5341 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5342 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5343 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5344 | default: |
| 5345 | /* Other cases are errors. */ |
| 5346 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5347 | WILL_LJMP(lua_error(L)); |
| 5348 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5349 | } |
| 5350 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5351 | return 1; |
| 5352 | } |
| 5353 | |
| 5354 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5355 | { |
| 5356 | struct hlua_txn *htxn; |
| 5357 | |
| 5358 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5359 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5360 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5361 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5362 | WILL_LJMP(lua_error(L)); |
| 5363 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5364 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 5365 | } |
| 5366 | |
| 5367 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5368 | { |
| 5369 | struct hlua_txn *htxn; |
| 5370 | |
| 5371 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5372 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5373 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5374 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5375 | WILL_LJMP(lua_error(L)); |
| 5376 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5377 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 5378 | } |
| 5379 | |
| 5380 | /* This function replace full header, or just a value in |
| 5381 | * the request or in the response. It is a wrapper fir the |
| 5382 | * 4 following functions. |
| 5383 | */ |
| 5384 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 5385 | struct http_msg *msg, int action) |
| 5386 | { |
| 5387 | size_t name_len; |
| 5388 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5389 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5390 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5391 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5392 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5393 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5394 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5395 | |
Christopher Faulet | 5a8549e | 2019-06-17 13:36:06 +0200 | [diff] [blame] | 5396 | if (IS_HTX_STRM(htxn->s)) { |
| 5397 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5398 | |
| 5399 | htx_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action); |
| 5400 | } |
| 5401 | else |
| 5402 | http_transform_header_str(htxn->s, msg, name, name_len, value, re, action); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5403 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5404 | return 0; |
| 5405 | } |
| 5406 | |
| 5407 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5408 | { |
| 5409 | struct hlua_txn *htxn; |
| 5410 | |
| 5411 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5412 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5413 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5414 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5415 | WILL_LJMP(lua_error(L)); |
| 5416 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5417 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 5418 | } |
| 5419 | |
| 5420 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5421 | { |
| 5422 | struct hlua_txn *htxn; |
| 5423 | |
| 5424 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5425 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5426 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5427 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5428 | WILL_LJMP(lua_error(L)); |
| 5429 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5430 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 5431 | } |
| 5432 | |
| 5433 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5434 | { |
| 5435 | struct hlua_txn *htxn; |
| 5436 | |
| 5437 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5438 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5439 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5440 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5441 | WILL_LJMP(lua_error(L)); |
| 5442 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5443 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 5444 | } |
| 5445 | |
| 5446 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5447 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5448 | struct hlua_txn *htxn; |
| 5449 | |
| 5450 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5451 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5452 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5453 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5454 | WILL_LJMP(lua_error(L)); |
| 5455 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 5456 | 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] | 5457 | } |
| 5458 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5459 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5460 | * It is a wrapper for the 2 following functions. |
| 5461 | */ |
| 5462 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5463 | { |
| 5464 | size_t len; |
| 5465 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5466 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5467 | if (IS_HTX_STRM(htxn->s)) { |
| 5468 | /* HTX version */ |
| 5469 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5470 | struct http_hdr_ctx ctx; |
| 5471 | |
| 5472 | ctx.blk = NULL; |
| 5473 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5474 | http_remove_header(htx, &ctx); |
| 5475 | } |
| 5476 | else { |
| 5477 | /* Legacy HTTP version */ |
| 5478 | struct hdr_ctx ctx; |
| 5479 | struct http_txn *txn = htxn->s->txn; |
| 5480 | |
| 5481 | ctx.idx = 0; |
| 5482 | while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) |
| 5483 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5484 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5485 | return 0; |
| 5486 | } |
| 5487 | |
| 5488 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5489 | { |
| 5490 | struct hlua_txn *htxn; |
| 5491 | |
| 5492 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5493 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5494 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5495 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5496 | WILL_LJMP(lua_error(L)); |
| 5497 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5498 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5499 | } |
| 5500 | |
| 5501 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5502 | { |
| 5503 | struct hlua_txn *htxn; |
| 5504 | |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5505 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5506 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5507 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5508 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5509 | WILL_LJMP(lua_error(L)); |
| 5510 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5511 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5512 | } |
| 5513 | |
| 5514 | /* This function adds an header. It is a wrapper used by |
| 5515 | * the 2 following functions. |
| 5516 | */ |
| 5517 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5518 | { |
| 5519 | size_t name_len; |
| 5520 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5521 | size_t value_len; |
| 5522 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 5523 | char *p; |
| 5524 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5525 | if (IS_HTX_STRM(htxn->s)) { |
| 5526 | /* HTX version */ |
| 5527 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5528 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5529 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5530 | ist2(value, value_len))); |
| 5531 | } |
| 5532 | else { |
| 5533 | /* Legacy HTTP version */ |
| 5534 | /* Check length. */ |
| 5535 | trash.data = value_len + name_len + 2; |
| 5536 | if (trash.data > trash.size) |
| 5537 | return 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5538 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5539 | /* Creates the header string. */ |
| 5540 | p = trash.area; |
| 5541 | memcpy(p, name, name_len); |
| 5542 | p += name_len; |
| 5543 | *p = ':'; |
| 5544 | p++; |
| 5545 | *p = ' '; |
| 5546 | p++; |
| 5547 | memcpy(p, value, value_len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5548 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5549 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
| 5550 | trash.area, trash.data) != 0); |
| 5551 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5552 | return 0; |
| 5553 | } |
| 5554 | |
| 5555 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5556 | { |
| 5557 | struct hlua_txn *htxn; |
| 5558 | |
| 5559 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5560 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5561 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5562 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5563 | WILL_LJMP(lua_error(L)); |
| 5564 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5565 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5566 | } |
| 5567 | |
| 5568 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5569 | { |
| 5570 | struct hlua_txn *htxn; |
| 5571 | |
| 5572 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5573 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5574 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5575 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5576 | WILL_LJMP(lua_error(L)); |
| 5577 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5578 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5579 | } |
| 5580 | |
| 5581 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5582 | { |
| 5583 | struct hlua_txn *htxn; |
| 5584 | |
| 5585 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5586 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5587 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5588 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5589 | WILL_LJMP(lua_error(L)); |
| 5590 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5591 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 5592 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5593 | } |
| 5594 | |
| 5595 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5596 | { |
| 5597 | struct hlua_txn *htxn; |
| 5598 | |
| 5599 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5600 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5601 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5602 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5603 | WILL_LJMP(lua_error(L)); |
| 5604 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5605 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 5606 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5607 | } |
| 5608 | |
| 5609 | /* This function set the method. */ |
| 5610 | static int hlua_http_req_set_meth(lua_State *L) |
| 5611 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5612 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5613 | size_t name_len; |
| 5614 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5615 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5616 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5617 | WILL_LJMP(lua_error(L)); |
| 5618 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5619 | 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] | 5620 | return 1; |
| 5621 | } |
| 5622 | |
| 5623 | /* This function set the method. */ |
| 5624 | static int hlua_http_req_set_path(lua_State *L) |
| 5625 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5626 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5627 | size_t name_len; |
| 5628 | 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] | 5629 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5630 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5631 | WILL_LJMP(lua_error(L)); |
| 5632 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5633 | 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] | 5634 | return 1; |
| 5635 | } |
| 5636 | |
| 5637 | /* This function set the query-string. */ |
| 5638 | static int hlua_http_req_set_query(lua_State *L) |
| 5639 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5640 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5641 | size_t name_len; |
| 5642 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5643 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5644 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5645 | WILL_LJMP(lua_error(L)); |
| 5646 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5647 | /* Check length. */ |
| 5648 | if (name_len > trash.size - 1) { |
| 5649 | lua_pushboolean(L, 0); |
| 5650 | return 1; |
| 5651 | } |
| 5652 | |
| 5653 | /* Add the mark question as prefix. */ |
| 5654 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5655 | trash.area[trash.data++] = '?'; |
| 5656 | memcpy(trash.area + trash.data, name, name_len); |
| 5657 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5658 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5659 | lua_pushboolean(L, |
| 5660 | 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] | 5661 | return 1; |
| 5662 | } |
| 5663 | |
| 5664 | /* This function set the uri. */ |
| 5665 | static int hlua_http_req_set_uri(lua_State *L) |
| 5666 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5667 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5668 | size_t name_len; |
| 5669 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5670 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5671 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5672 | WILL_LJMP(lua_error(L)); |
| 5673 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5674 | 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] | 5675 | return 1; |
| 5676 | } |
| 5677 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5678 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5679 | static int hlua_http_res_set_status(lua_State *L) |
| 5680 | { |
| 5681 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5682 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5683 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5684 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5685 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5686 | WILL_LJMP(lua_error(L)); |
| 5687 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5688 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5689 | return 0; |
| 5690 | } |
| 5691 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5692 | /* |
| 5693 | * |
| 5694 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5695 | * Class TXN |
| 5696 | * |
| 5697 | * |
| 5698 | */ |
| 5699 | |
| 5700 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5701 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5702 | */ |
| 5703 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5704 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5705 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5706 | } |
| 5707 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5708 | __LJMP static int hlua_set_var(lua_State *L) |
| 5709 | { |
| 5710 | struct hlua_txn *htxn; |
| 5711 | const char *name; |
| 5712 | size_t len; |
| 5713 | struct sample smp; |
| 5714 | |
| 5715 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5716 | |
| 5717 | /* It is useles to retrieve the stream, but this function |
| 5718 | * runs only in a stream context. |
| 5719 | */ |
| 5720 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5721 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5722 | |
| 5723 | /* Converts the third argument in a sample. */ |
| 5724 | hlua_lua2smp(L, 3, &smp); |
| 5725 | |
| 5726 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5727 | 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] | 5728 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5729 | return 0; |
| 5730 | } |
| 5731 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5732 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5733 | { |
| 5734 | struct hlua_txn *htxn; |
| 5735 | const char *name; |
| 5736 | size_t len; |
| 5737 | struct sample smp; |
| 5738 | |
| 5739 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5740 | |
| 5741 | /* It is useles to retrieve the stream, but this function |
| 5742 | * runs only in a stream context. |
| 5743 | */ |
| 5744 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5745 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5746 | |
| 5747 | /* Unset the variable. */ |
| 5748 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5749 | vars_unset_by_name(name, len, &smp); |
| 5750 | return 0; |
| 5751 | } |
| 5752 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5753 | __LJMP static int hlua_get_var(lua_State *L) |
| 5754 | { |
| 5755 | struct hlua_txn *htxn; |
| 5756 | const char *name; |
| 5757 | size_t len; |
| 5758 | struct sample smp; |
| 5759 | |
| 5760 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5761 | |
| 5762 | /* It is useles to retrieve the stream, but this function |
| 5763 | * runs only in a stream context. |
| 5764 | */ |
| 5765 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5766 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5767 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5768 | 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] | 5769 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5770 | lua_pushnil(L); |
| 5771 | return 1; |
| 5772 | } |
| 5773 | |
| 5774 | return hlua_smp2lua(L, &smp); |
| 5775 | } |
| 5776 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5777 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5778 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5779 | struct hlua *hlua; |
| 5780 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5781 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5782 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5783 | /* It is useles to retrieve the stream, but this function |
| 5784 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5785 | */ |
| 5786 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5787 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5788 | |
| 5789 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5790 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5791 | |
| 5792 | /* Get and store new value. */ |
| 5793 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5794 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5795 | |
| 5796 | return 0; |
| 5797 | } |
| 5798 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5799 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5800 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5801 | struct hlua *hlua; |
| 5802 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5803 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5804 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5805 | /* It is useles to retrieve the stream, but this function |
| 5806 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5807 | */ |
| 5808 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5809 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5810 | |
| 5811 | /* Push configuration index in the stack. */ |
| 5812 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5813 | |
| 5814 | return 1; |
| 5815 | } |
| 5816 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5817 | /* Create stack entry containing a class TXN. This function |
| 5818 | * return 0 if the stack does not contains free slots, |
| 5819 | * otherwise it returns 1. |
| 5820 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5821 | 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] | 5822 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5823 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5824 | |
| 5825 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5826 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5827 | return 0; |
| 5828 | |
| 5829 | /* NOTE: The allocation never fails. The failure |
| 5830 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5831 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5832 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5833 | /* Create the object: obj[0] = userdata. */ |
| 5834 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5835 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5836 | lua_rawseti(L, -2, 0); |
| 5837 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5838 | htxn->s = s; |
| 5839 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5840 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5841 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5842 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5843 | /* Create the "f" field that contains a list of fetches. */ |
| 5844 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5845 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5846 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5847 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5848 | |
| 5849 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5850 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5851 | 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] | 5852 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5853 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5854 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5855 | /* Create the "c" field that contains a list of converters. */ |
| 5856 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5857 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5858 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5859 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5860 | |
| 5861 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5862 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5863 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5864 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5865 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5866 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5867 | /* Create the "req" field that contains the request channel object. */ |
| 5868 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5869 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5870 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5871 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5872 | |
| 5873 | /* Create the "res" field that contains the response channel object. */ |
| 5874 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5875 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5876 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5877 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5878 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5879 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5880 | lua_pushstring(L, "http"); |
| 5881 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5882 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5883 | return 0; |
| 5884 | } |
| 5885 | else |
| 5886 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5887 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5888 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5889 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5890 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5891 | lua_setmetatable(L, -2); |
| 5892 | |
| 5893 | return 1; |
| 5894 | } |
| 5895 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5896 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5897 | { |
| 5898 | const char *msg; |
| 5899 | struct hlua_txn *htxn; |
| 5900 | |
| 5901 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5902 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5903 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5904 | |
| 5905 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5906 | return 0; |
| 5907 | } |
| 5908 | |
| 5909 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5910 | { |
| 5911 | int level; |
| 5912 | const char *msg; |
| 5913 | struct hlua_txn *htxn; |
| 5914 | |
| 5915 | MAY_LJMP(check_args(L, 3, "log")); |
| 5916 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5917 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5918 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5919 | |
| 5920 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5921 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5922 | |
| 5923 | hlua_sendlog(htxn->s->be, level, msg); |
| 5924 | return 0; |
| 5925 | } |
| 5926 | |
| 5927 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5928 | { |
| 5929 | const char *msg; |
| 5930 | struct hlua_txn *htxn; |
| 5931 | |
| 5932 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5933 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5934 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5935 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5936 | return 0; |
| 5937 | } |
| 5938 | |
| 5939 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5940 | { |
| 5941 | const char *msg; |
| 5942 | struct hlua_txn *htxn; |
| 5943 | |
| 5944 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5945 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5946 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5947 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5948 | return 0; |
| 5949 | } |
| 5950 | |
| 5951 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5952 | { |
| 5953 | const char *msg; |
| 5954 | struct hlua_txn *htxn; |
| 5955 | |
| 5956 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5957 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5958 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5959 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5960 | return 0; |
| 5961 | } |
| 5962 | |
| 5963 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5964 | { |
| 5965 | const char *msg; |
| 5966 | struct hlua_txn *htxn; |
| 5967 | |
| 5968 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5969 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5970 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5971 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5972 | return 0; |
| 5973 | } |
| 5974 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5975 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5976 | { |
| 5977 | struct hlua_txn *htxn; |
| 5978 | int ll; |
| 5979 | |
| 5980 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5981 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5982 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5983 | |
| 5984 | if (ll < 0 || ll > 7) |
| 5985 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5986 | |
| 5987 | htxn->s->logs.level = ll; |
| 5988 | return 0; |
| 5989 | } |
| 5990 | |
| 5991 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5992 | { |
| 5993 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5994 | int tos; |
| 5995 | |
| 5996 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5997 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5998 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5999 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 6000 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6001 | return 0; |
| 6002 | } |
| 6003 | |
| 6004 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 6005 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6006 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6007 | int mark; |
| 6008 | |
| 6009 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 6010 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6011 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6012 | |
Lukas Tribus | b59291a | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 6013 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6014 | return 0; |
| 6015 | } |
| 6016 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 6017 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 6018 | { |
| 6019 | struct hlua_txn *htxn; |
| 6020 | |
| 6021 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 6022 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6023 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6024 | return 0; |
| 6025 | } |
| 6026 | |
| 6027 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 6028 | { |
| 6029 | struct hlua_txn *htxn; |
| 6030 | |
| 6031 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 6032 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6033 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6034 | return 0; |
| 6035 | } |
| 6036 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6037 | /* This function is an Lua binding that send pending data |
| 6038 | * to the client, and close the stream interface. |
| 6039 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6040 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6041 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6042 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6043 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6044 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6045 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6046 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6047 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6048 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6049 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6050 | /* If the flags NOTERM is set, we cannot terminate the http |
| 6051 | * session, so we just end the execution of the current |
| 6052 | * lua code. |
| 6053 | */ |
| 6054 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 6055 | WILL_LJMP(hlua_done(L)); |
| 6056 | return 0; |
| 6057 | } |
| 6058 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6059 | ic = &htxn->s->req; |
| 6060 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6061 | |
Christopher Faulet | 72c6927 | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6062 | if (IS_HTX_STRM(htxn->s)) { |
| 6063 | htxn->s->txn->status = 0; |
| 6064 | http_reply_and_close(htxn->s, 0, NULL); |
| 6065 | ic->analysers &= AN_REQ_FLT_END; |
| 6066 | oc->analysers &= AN_RES_FLT_END; |
| 6067 | } |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6068 | else { |
| 6069 | if (htxn->s->txn) { |
| 6070 | /* HTTP mode, let's stay in sync with the stream */ |
| 6071 | b_del(&ic->buf, htxn->s->txn->req.sov); |
| 6072 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 6073 | htxn->s->txn->req.sov = 0; |
| 6074 | |
| 6075 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 6076 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 6077 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 6078 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6079 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6080 | /* Note that if we want to support keep-alive, we need |
| 6081 | * to bypass the close/shutr_now calls below, but that |
| 6082 | * may only be done if the HTTP request was already |
| 6083 | * processed and the connection header is known (ie |
| 6084 | * not during TCP rules). |
| 6085 | */ |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6086 | } |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6087 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6088 | channel_auto_read(ic); |
| 6089 | channel_abort(ic); |
| 6090 | channel_auto_close(ic); |
| 6091 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6092 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6093 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 6094 | channel_auto_read(oc); |
| 6095 | channel_auto_close(oc); |
| 6096 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6097 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6098 | ic->analysers = 0; |
| 6099 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6100 | |
Christopher Faulet | 72c6927 | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6101 | if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 6102 | htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 6103 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6104 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6105 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6106 | return 0; |
| 6107 | } |
| 6108 | |
| 6109 | __LJMP static int hlua_log(lua_State *L) |
| 6110 | { |
| 6111 | int level; |
| 6112 | const char *msg; |
| 6113 | |
| 6114 | MAY_LJMP(check_args(L, 2, "log")); |
| 6115 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6116 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6117 | |
| 6118 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6119 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6120 | |
| 6121 | hlua_sendlog(NULL, level, msg); |
| 6122 | return 0; |
| 6123 | } |
| 6124 | |
| 6125 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6126 | { |
| 6127 | const char *msg; |
| 6128 | |
| 6129 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6130 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6131 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6132 | return 0; |
| 6133 | } |
| 6134 | |
| 6135 | __LJMP static int hlua_log_info(lua_State *L) |
| 6136 | { |
| 6137 | const char *msg; |
| 6138 | |
| 6139 | MAY_LJMP(check_args(L, 1, "info")); |
| 6140 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6141 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6142 | return 0; |
| 6143 | } |
| 6144 | |
| 6145 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6146 | { |
| 6147 | const char *msg; |
| 6148 | |
| 6149 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6150 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6151 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6152 | return 0; |
| 6153 | } |
| 6154 | |
| 6155 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6156 | { |
| 6157 | const char *msg; |
| 6158 | |
| 6159 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6160 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6161 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6162 | return 0; |
| 6163 | } |
| 6164 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6165 | __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] | 6166 | { |
| 6167 | int wakeup_ms = lua_tointeger(L, -1); |
| 6168 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6169 | 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] | 6170 | return 0; |
| 6171 | } |
| 6172 | |
| 6173 | __LJMP static int hlua_sleep(lua_State *L) |
| 6174 | { |
| 6175 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6176 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6177 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6178 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6179 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6180 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6181 | wakeup_ms = tick_add(now_ms, delay); |
| 6182 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6183 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6184 | 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] | 6185 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6186 | } |
| 6187 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6188 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6189 | { |
| 6190 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6191 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6192 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6193 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6194 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6195 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6196 | wakeup_ms = tick_add(now_ms, delay); |
| 6197 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6198 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6199 | 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] | 6200 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6201 | } |
| 6202 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6203 | /* This functionis an LUA binding. it permits to give back |
| 6204 | * the hand at the HAProxy scheduler. It is used when the |
| 6205 | * LUA processing consumes a lot of time. |
| 6206 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6207 | __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] | 6208 | { |
| 6209 | return 0; |
| 6210 | } |
| 6211 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6212 | __LJMP static int hlua_yield(lua_State *L) |
| 6213 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6214 | 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] | 6215 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6216 | } |
| 6217 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6218 | /* This function change the nice of the currently executed |
| 6219 | * task. It is used set low or high priority at the current |
| 6220 | * task. |
| 6221 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6222 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6223 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6224 | struct hlua *hlua; |
| 6225 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6226 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6227 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6228 | hlua = hlua_gethlua(L); |
| 6229 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6230 | |
| 6231 | /* If he task is not set, I'm in a start mode. */ |
| 6232 | if (!hlua || !hlua->task) |
| 6233 | return 0; |
| 6234 | |
| 6235 | if (nice < -1024) |
| 6236 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6237 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6238 | nice = 1024; |
| 6239 | |
| 6240 | hlua->task->nice = nice; |
| 6241 | return 0; |
| 6242 | } |
| 6243 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6244 | /* 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] | 6245 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6246 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6247 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6248 | * |
| 6249 | * Task wrapper are longjmp safe because the only one Lua code |
| 6250 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6251 | */ |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6252 | 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] | 6253 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6254 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6255 | enum hlua_exec status; |
| 6256 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6257 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6258 | task_set_affinity(task, tid_bit); |
| 6259 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6260 | /* If it is the first call to the task, we must initialize the |
| 6261 | * execution timeouts. |
| 6262 | */ |
| 6263 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6264 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6265 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6266 | /* Execute the Lua code. */ |
| 6267 | status = hlua_ctx_resume(hlua, 1); |
| 6268 | |
| 6269 | switch (status) { |
| 6270 | /* finished or yield */ |
| 6271 | case HLUA_E_OK: |
| 6272 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6273 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6274 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6275 | break; |
| 6276 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6277 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6278 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6279 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6280 | break; |
| 6281 | |
| 6282 | /* finished with error. */ |
| 6283 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6284 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6285 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6286 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6287 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6288 | break; |
| 6289 | |
| 6290 | case HLUA_E_ERR: |
| 6291 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6292 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6293 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6294 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6295 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6296 | break; |
| 6297 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6298 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6299 | } |
| 6300 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6301 | /* This function is an LUA binding that register LUA function to be |
| 6302 | * executed after the HAProxy configuration parsing and before the |
| 6303 | * HAProxy scheduler starts. This function expect only one LUA |
| 6304 | * argument that is a function. This function returns nothing, but |
| 6305 | * throws if an error is encountered. |
| 6306 | */ |
| 6307 | __LJMP static int hlua_register_init(lua_State *L) |
| 6308 | { |
| 6309 | struct hlua_init_function *init; |
| 6310 | int ref; |
| 6311 | |
| 6312 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6313 | |
| 6314 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6315 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6316 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6317 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6318 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6319 | |
| 6320 | init->function_ref = ref; |
| 6321 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6322 | return 0; |
| 6323 | } |
| 6324 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6325 | /* This functio is an LUA binding. It permits to register a task |
| 6326 | * executed in parallel of the main HAroxy activity. The task is |
| 6327 | * created and it is set in the HAProxy scheduler. It can be called |
| 6328 | * from the "init" section, "post init" or during the runtime. |
| 6329 | * |
| 6330 | * Lua prototype: |
| 6331 | * |
| 6332 | * <none> core.register_task(<function>) |
| 6333 | */ |
| 6334 | static int hlua_register_task(lua_State *L) |
| 6335 | { |
| 6336 | struct hlua *hlua; |
| 6337 | struct task *task; |
| 6338 | int ref; |
| 6339 | |
| 6340 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6341 | |
| 6342 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6343 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6344 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6345 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6346 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6347 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6348 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6349 | if (!task) |
| 6350 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6351 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6352 | task->context = hlua; |
| 6353 | task->process = hlua_process_task; |
| 6354 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6355 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6356 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6357 | |
| 6358 | /* Restore the function in the stack. */ |
| 6359 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6360 | hlua->nargs = 0; |
| 6361 | |
| 6362 | /* Schedule task. */ |
| 6363 | task_schedule(task, now_ms); |
| 6364 | |
| 6365 | return 0; |
| 6366 | } |
| 6367 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6368 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6369 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6370 | * resume converters. |
| 6371 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6372 | 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] | 6373 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6374 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6375 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6376 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6377 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6378 | if (!stream) |
| 6379 | return 0; |
| 6380 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6381 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6382 | * Lua context can be not initialized. This behavior |
| 6383 | * permits to save performances because a systematic |
| 6384 | * Lua initialization cause 5% performances loss. |
| 6385 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6386 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6387 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6388 | if (!stream->hlua) { |
| 6389 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6390 | return 0; |
| 6391 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6392 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6393 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6394 | return 0; |
| 6395 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6396 | } |
| 6397 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6398 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6399 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6400 | |
| 6401 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6402 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6403 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6404 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6405 | else |
| 6406 | error = "critical error"; |
| 6407 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6408 | return 0; |
| 6409 | } |
| 6410 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6411 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6412 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6413 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6414 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6415 | return 0; |
| 6416 | } |
| 6417 | |
| 6418 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6419 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6420 | |
| 6421 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6422 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6423 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6424 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6425 | return 0; |
| 6426 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6427 | hlua_smp2lua(stream->hlua->T, smp); |
| 6428 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6429 | |
| 6430 | /* push keywords in the stack. */ |
| 6431 | if (arg_p) { |
| 6432 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6433 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6434 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6435 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6436 | return 0; |
| 6437 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6438 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6439 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6440 | } |
| 6441 | } |
| 6442 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6443 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6444 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6445 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6446 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6447 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6448 | } |
| 6449 | |
| 6450 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6451 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6452 | /* finished. */ |
| 6453 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6454 | /* If the stack is empty, the function fails. */ |
| 6455 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6456 | return 0; |
| 6457 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6458 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6459 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6460 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6461 | return 1; |
| 6462 | |
| 6463 | /* yield. */ |
| 6464 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6465 | 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] | 6466 | return 0; |
| 6467 | |
| 6468 | /* finished with error. */ |
| 6469 | case HLUA_E_ERRMSG: |
| 6470 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6471 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6472 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6473 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6474 | return 0; |
| 6475 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6476 | case HLUA_E_ETMOUT: |
| 6477 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6478 | return 0; |
| 6479 | |
| 6480 | case HLUA_E_NOMEM: |
| 6481 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6482 | return 0; |
| 6483 | |
| 6484 | case HLUA_E_YIELD: |
| 6485 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6486 | return 0; |
| 6487 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6488 | case HLUA_E_ERR: |
| 6489 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6490 | 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] | 6491 | |
| 6492 | default: |
| 6493 | return 0; |
| 6494 | } |
| 6495 | } |
| 6496 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6497 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6498 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6499 | * resume sample-fetches. This function will be called by the sample |
| 6500 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6501 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6502 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6503 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6504 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6505 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6506 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6507 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6508 | const struct buffer msg = { }; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6509 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6510 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6511 | if (!stream) |
| 6512 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6513 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6514 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6515 | * Lua context can be not initialized. This behavior |
| 6516 | * permits to save performances because a systematic |
| 6517 | * Lua initialization cause 5% performances loss. |
| 6518 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6519 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6520 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6521 | if (!stream->hlua) { |
| 6522 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6523 | return 0; |
| 6524 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6525 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6526 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6527 | return 0; |
| 6528 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6529 | } |
| 6530 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6531 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6532 | |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6533 | if (stream->be->mode == PR_MODE_HTTP) { |
| 6534 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
| 6535 | hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 6536 | else |
| 6537 | hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 6538 | } |
| 6539 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6540 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6541 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6542 | |
| 6543 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6544 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6545 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6546 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6547 | else |
| 6548 | error = "critical error"; |
| 6549 | 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] | 6550 | return 0; |
| 6551 | } |
| 6552 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6553 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6554 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6555 | 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] | 6556 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6557 | return 0; |
| 6558 | } |
| 6559 | |
| 6560 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6561 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6562 | |
| 6563 | /* push arguments in the stack. */ |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6564 | if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6565 | 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] | 6566 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6567 | return 0; |
| 6568 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6569 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6570 | |
| 6571 | /* push keywords in the stack. */ |
| 6572 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6573 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6574 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6575 | 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] | 6576 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6577 | return 0; |
| 6578 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6579 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6580 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6581 | } |
| 6582 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6583 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6584 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6585 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6586 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6587 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6588 | } |
| 6589 | |
| 6590 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6591 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6592 | /* finished. */ |
| 6593 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6594 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6595 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6596 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6597 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6598 | /* If the stack is empty, the function fails. */ |
| 6599 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6600 | return 0; |
| 6601 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6602 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6603 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6604 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6605 | |
| 6606 | /* Set the end of execution flag. */ |
| 6607 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6608 | return 1; |
| 6609 | |
| 6610 | /* yield. */ |
| 6611 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6612 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6613 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6614 | 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] | 6615 | return 0; |
| 6616 | |
| 6617 | /* finished with error. */ |
| 6618 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6619 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6620 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6621 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6622 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6623 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6624 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6625 | return 0; |
| 6626 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6627 | case HLUA_E_ETMOUT: |
| 6628 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6629 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6630 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6631 | return 0; |
| 6632 | |
| 6633 | case HLUA_E_NOMEM: |
| 6634 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6635 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6636 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6637 | return 0; |
| 6638 | |
| 6639 | case HLUA_E_YIELD: |
| 6640 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6641 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6642 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6643 | return 0; |
| 6644 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6645 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6646 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6647 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6648 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6649 | 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] | 6650 | |
| 6651 | default: |
| 6652 | return 0; |
| 6653 | } |
| 6654 | } |
| 6655 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6656 | /* This function is an LUA binding used for registering |
| 6657 | * "sample-conv" functions. It expects a converter name used |
| 6658 | * in the haproxy configuration file, and an LUA function. |
| 6659 | */ |
| 6660 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6661 | { |
| 6662 | struct sample_conv_kw_list *sck; |
| 6663 | const char *name; |
| 6664 | int ref; |
| 6665 | int len; |
| 6666 | struct hlua_function *fcn; |
| 6667 | |
| 6668 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6669 | |
| 6670 | /* First argument : converter name. */ |
| 6671 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6672 | |
| 6673 | /* Second argument : lua function. */ |
| 6674 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6675 | |
| 6676 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6677 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6678 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6679 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6680 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6681 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6682 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6683 | |
| 6684 | /* Fill fcn. */ |
| 6685 | fcn->name = strdup(name); |
| 6686 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6687 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6688 | fcn->function_ref = ref; |
| 6689 | |
| 6690 | /* List head */ |
| 6691 | sck->list.n = sck->list.p = NULL; |
| 6692 | |
| 6693 | /* converter keyword. */ |
| 6694 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6695 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6696 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6697 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6698 | |
| 6699 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6700 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6701 | 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] | 6702 | sck->kw[0].val_args = NULL; |
| 6703 | sck->kw[0].in_type = SMP_T_STR; |
| 6704 | sck->kw[0].out_type = SMP_T_STR; |
| 6705 | sck->kw[0].private = fcn; |
| 6706 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6707 | /* Register this new converter */ |
| 6708 | sample_register_convs(sck); |
| 6709 | |
| 6710 | return 0; |
| 6711 | } |
| 6712 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6713 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6714 | * "sample-fetch" functions. It expects a converter name used |
| 6715 | * in the haproxy configuration file, and an LUA function. |
| 6716 | */ |
| 6717 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6718 | { |
| 6719 | const char *name; |
| 6720 | int ref; |
| 6721 | int len; |
| 6722 | struct sample_fetch_kw_list *sfk; |
| 6723 | struct hlua_function *fcn; |
| 6724 | |
| 6725 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6726 | |
| 6727 | /* First argument : sample-fetch name. */ |
| 6728 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6729 | |
| 6730 | /* Second argument : lua function. */ |
| 6731 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6732 | |
| 6733 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6734 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6735 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6736 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6737 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6738 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6739 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6740 | |
| 6741 | /* Fill fcn. */ |
| 6742 | fcn->name = strdup(name); |
| 6743 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6744 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6745 | fcn->function_ref = ref; |
| 6746 | |
| 6747 | /* List head */ |
| 6748 | sfk->list.n = sfk->list.p = NULL; |
| 6749 | |
| 6750 | /* sample-fetch keyword. */ |
| 6751 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6752 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6753 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6754 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6755 | |
| 6756 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6757 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6758 | 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] | 6759 | sfk->kw[0].val_args = NULL; |
| 6760 | sfk->kw[0].out_type = SMP_T_STR; |
| 6761 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6762 | sfk->kw[0].val = 0; |
| 6763 | sfk->kw[0].private = fcn; |
| 6764 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6765 | /* Register this new fetch. */ |
| 6766 | sample_register_fetches(sfk); |
| 6767 | |
| 6768 | return 0; |
| 6769 | } |
| 6770 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6771 | /* This function is a wrapper to execute each LUA function declared |
| 6772 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6773 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6774 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6775 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6776 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6777 | 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] | 6778 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6779 | { |
| 6780 | char **arg; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6781 | unsigned int hflags = 0; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6782 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6783 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6784 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6785 | |
| 6786 | switch (rule->from) { |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6787 | case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break; |
| 6788 | case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break; |
| 6789 | case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break; |
| 6790 | case ACT_F_HTTP_RES: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6791 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6792 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6793 | return ACT_RET_CONT; |
| 6794 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6795 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6796 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6797 | * Lua context can be not initialized. This behavior |
| 6798 | * permits to save performances because a systematic |
| 6799 | * Lua initialization cause 5% performances loss. |
| 6800 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6801 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6802 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6803 | if (!s->hlua) { |
| 6804 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6805 | rule->arg.hlua_rule->fcn.name); |
| 6806 | return ACT_RET_CONT; |
| 6807 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6808 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6809 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6810 | rule->arg.hlua_rule->fcn.name); |
| 6811 | return ACT_RET_CONT; |
| 6812 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6813 | } |
| 6814 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6815 | consistency_set(s, dir, &s->hlua->cons); |
| 6816 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6817 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6818 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6819 | |
| 6820 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6821 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6822 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6823 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6824 | else |
| 6825 | error = "critical error"; |
| 6826 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6827 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6828 | return ACT_RET_CONT; |
| 6829 | } |
| 6830 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6831 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6832 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6833 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6834 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6835 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6836 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6837 | } |
| 6838 | |
| 6839 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6840 | 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] | 6841 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6842 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6843 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6844 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6845 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6846 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6847 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6848 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6849 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6850 | |
| 6851 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6852 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6853 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6854 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6855 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6856 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6857 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6858 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6859 | lua_pushstring(s->hlua->T, *arg); |
| 6860 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6861 | } |
| 6862 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6863 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6864 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6865 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6866 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6867 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6868 | } |
| 6869 | |
Christopher Faulet | 719ddc8 | 2020-06-02 18:46:07 +0200 | [diff] [blame] | 6870 | /* Always reset the analyse expiration timeout for the corresponding |
| 6871 | * channel in case the lua script yield, to be sure to not keep an |
| 6872 | * expired timeout. |
| 6873 | */ |
| 6874 | if (dir == SMP_OPT_DIR_REQ) |
| 6875 | s->req.analyse_exp = TICK_ETERNITY; |
| 6876 | else |
| 6877 | s->res.analyse_exp = TICK_ETERNITY; |
| 6878 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6879 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6880 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6881 | /* finished. */ |
| 6882 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6883 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6884 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6885 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6886 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6887 | if (s->hlua->flags & HLUA_STOP) |
Christopher Faulet | 4629d08 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 6888 | return ACT_RET_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6889 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6890 | |
| 6891 | /* yield. */ |
| 6892 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6893 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6894 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Christopher Faulet | ea0b0e2 | 2019-08-14 23:19:45 +0200 | [diff] [blame] | 6895 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6896 | s->req.analyse_exp = s->hlua->wake_time; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6897 | else |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6898 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6899 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6900 | /* Some actions can be wake up when a "write" event |
| 6901 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6902 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6903 | */ |
Christopher Faulet | b22f650 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6904 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6905 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6906 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6907 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6908 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6909 | * because HAProxy is not able to manipulate data, it |
| 6910 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6911 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6912 | |
| 6913 | /* finished with error. */ |
| 6914 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6915 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6916 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6917 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6918 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6919 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6920 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6921 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6922 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6923 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6924 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6925 | case HLUA_E_ETMOUT: |
| 6926 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6927 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6928 | return ACT_RET_ERR; |
| 6929 | } |
| 6930 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6931 | return 0; |
| 6932 | |
| 6933 | case HLUA_E_NOMEM: |
| 6934 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6935 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6936 | return ACT_RET_ERR; |
| 6937 | } |
| 6938 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6939 | return 0; |
| 6940 | |
| 6941 | case HLUA_E_YIELD: |
| 6942 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6943 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6944 | return ACT_RET_ERR; |
| 6945 | } |
| 6946 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6947 | rule->arg.hlua_rule->fcn.name); |
| 6948 | return 0; |
| 6949 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6950 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6951 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6952 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6953 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6954 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6955 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6956 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6957 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6958 | |
| 6959 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6960 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6961 | } |
| 6962 | } |
| 6963 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6964 | 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] | 6965 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6966 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6967 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6968 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6969 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6970 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6971 | } |
| 6972 | |
| 6973 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6974 | { |
| 6975 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6976 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6977 | struct task *task; |
| 6978 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6979 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6980 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6981 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6982 | if (!hlua) { |
| 6983 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6984 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6985 | return 0; |
| 6986 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6987 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6988 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6989 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6990 | |
| 6991 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6992 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6993 | if (!task) { |
| 6994 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6995 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6996 | return 0; |
| 6997 | } |
| 6998 | task->nice = 0; |
| 6999 | task->context = ctx; |
| 7000 | task->process = hlua_applet_wakeup; |
| 7001 | ctx->ctx.hlua_apptcp.task = task; |
| 7002 | |
| 7003 | /* In the execution wrappers linked with a stream, the |
| 7004 | * Lua context can be not initialized. This behavior |
| 7005 | * permits to save performances because a systematic |
| 7006 | * Lua initialization cause 5% performances loss. |
| 7007 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7008 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7009 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 7010 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7011 | return 0; |
| 7012 | } |
| 7013 | |
| 7014 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7015 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7016 | |
| 7017 | /* The following Lua calls can fail. */ |
| 7018 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7019 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7020 | error = lua_tostring(hlua->T, -1); |
| 7021 | else |
| 7022 | error = "critical error"; |
| 7023 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7024 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7025 | return 0; |
| 7026 | } |
| 7027 | |
| 7028 | /* Check stack available size. */ |
| 7029 | if (!lua_checkstack(hlua->T, 1)) { |
| 7030 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7031 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7032 | RESET_SAFE_LJMP(hlua->T); |
| 7033 | return 0; |
| 7034 | } |
| 7035 | |
| 7036 | /* Restore the function in the stack. */ |
| 7037 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7038 | |
| 7039 | /* Create and and push object stream in the stack. */ |
| 7040 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7041 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7042 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7043 | RESET_SAFE_LJMP(hlua->T); |
| 7044 | return 0; |
| 7045 | } |
| 7046 | hlua->nargs = 1; |
| 7047 | |
| 7048 | /* push keywords in the stack. */ |
| 7049 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7050 | if (!lua_checkstack(hlua->T, 1)) { |
| 7051 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7052 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7053 | RESET_SAFE_LJMP(hlua->T); |
| 7054 | return 0; |
| 7055 | } |
| 7056 | lua_pushstring(hlua->T, *arg); |
| 7057 | hlua->nargs++; |
| 7058 | } |
| 7059 | |
| 7060 | RESET_SAFE_LJMP(hlua->T); |
| 7061 | |
| 7062 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7063 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7064 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7065 | |
| 7066 | return 1; |
| 7067 | } |
| 7068 | |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7069 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7070 | { |
| 7071 | struct stream_interface *si = ctx->owner; |
| 7072 | struct stream *strm = si_strm(si); |
| 7073 | struct channel *res = si_ic(si); |
| 7074 | struct act_rule *rule = ctx->rule; |
| 7075 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7076 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7077 | |
| 7078 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7079 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7080 | /* eat the whole request */ |
| 7081 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7082 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7083 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7084 | |
| 7085 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7086 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7087 | return; |
| 7088 | |
| 7089 | /* Execute the function. */ |
| 7090 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7091 | /* finished. */ |
| 7092 | case HLUA_E_OK: |
| 7093 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7094 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7095 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7096 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7097 | res->flags |= CF_READ_NULL; |
| 7098 | si_shutr(si); |
| 7099 | return; |
| 7100 | |
| 7101 | /* yield. */ |
| 7102 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7103 | if (hlua->wake_time != TICK_ETERNITY) |
| 7104 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7105 | return; |
| 7106 | |
| 7107 | /* finished with error. */ |
| 7108 | case HLUA_E_ERRMSG: |
| 7109 | /* Display log. */ |
| 7110 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7111 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7112 | lua_pop(hlua->T, 1); |
| 7113 | goto error; |
| 7114 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7115 | case HLUA_E_ETMOUT: |
| 7116 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 7117 | rule->arg.hlua_rule->fcn.name); |
| 7118 | goto error; |
| 7119 | |
| 7120 | case HLUA_E_NOMEM: |
| 7121 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 7122 | rule->arg.hlua_rule->fcn.name); |
| 7123 | goto error; |
| 7124 | |
| 7125 | case HLUA_E_YIELD: /* unexpected */ |
| 7126 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 7127 | rule->arg.hlua_rule->fcn.name); |
| 7128 | goto error; |
| 7129 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7130 | case HLUA_E_ERR: |
| 7131 | /* Display log. */ |
| 7132 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 7133 | rule->arg.hlua_rule->fcn.name); |
| 7134 | goto error; |
| 7135 | |
| 7136 | default: |
| 7137 | goto error; |
| 7138 | } |
| 7139 | |
| 7140 | error: |
| 7141 | |
| 7142 | /* For all other cases, just close the stream. */ |
| 7143 | si_shutw(si); |
| 7144 | si_shutr(si); |
| 7145 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7146 | } |
| 7147 | |
| 7148 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7149 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7150 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7151 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7152 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7153 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7154 | } |
| 7155 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7156 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7157 | * an errors occurs and -1 if more data are required for initializing |
| 7158 | * the applet. |
| 7159 | */ |
| 7160 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7161 | { |
| 7162 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7163 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7164 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7165 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7166 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7167 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7168 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7169 | txn = strm->txn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7170 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7171 | /* We want two things in HTTP mode : |
| 7172 | * - enforce server-close mode if we were in keep-alive, so that the |
| 7173 | * applet is released after each response ; |
| 7174 | * - enable request body transfer to the applet in order to resync |
| 7175 | * with the response body. |
| 7176 | */ |
| 7177 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 7178 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7179 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7180 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7181 | if (!hlua) { |
| 7182 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7183 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7184 | return 0; |
| 7185 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7186 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7187 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7188 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7189 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7190 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7191 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7192 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7193 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7194 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7195 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7196 | if (!task) { |
| 7197 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7198 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7199 | return 0; |
| 7200 | } |
| 7201 | task->nice = 0; |
| 7202 | task->context = ctx; |
| 7203 | task->process = hlua_applet_wakeup; |
| 7204 | ctx->ctx.hlua_apphttp.task = task; |
| 7205 | |
| 7206 | /* In the execution wrappers linked with a stream, the |
| 7207 | * Lua context can be not initialized. This behavior |
| 7208 | * permits to save performances because a systematic |
| 7209 | * Lua initialization cause 5% performances loss. |
| 7210 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7211 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7212 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 7213 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7214 | return 0; |
| 7215 | } |
| 7216 | |
| 7217 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7218 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7219 | |
| 7220 | /* The following Lua calls can fail. */ |
| 7221 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7222 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7223 | error = lua_tostring(hlua->T, -1); |
| 7224 | else |
| 7225 | error = "critical error"; |
| 7226 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7227 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7228 | return 0; |
| 7229 | } |
| 7230 | |
| 7231 | /* Check stack available size. */ |
| 7232 | if (!lua_checkstack(hlua->T, 1)) { |
| 7233 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7234 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7235 | RESET_SAFE_LJMP(hlua->T); |
| 7236 | return 0; |
| 7237 | } |
| 7238 | |
| 7239 | /* Restore the function in the stack. */ |
| 7240 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7241 | |
| 7242 | /* Create and and push object stream in the stack. */ |
| 7243 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7244 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7245 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7246 | RESET_SAFE_LJMP(hlua->T); |
| 7247 | return 0; |
| 7248 | } |
| 7249 | hlua->nargs = 1; |
| 7250 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7251 | /* push keywords in the stack. */ |
| 7252 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7253 | if (!lua_checkstack(hlua->T, 1)) { |
| 7254 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7255 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7256 | RESET_SAFE_LJMP(hlua->T); |
| 7257 | return 0; |
| 7258 | } |
| 7259 | lua_pushstring(hlua->T, *arg); |
| 7260 | hlua->nargs++; |
| 7261 | } |
| 7262 | |
| 7263 | RESET_SAFE_LJMP(hlua->T); |
| 7264 | |
| 7265 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7266 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7267 | |
| 7268 | return 1; |
| 7269 | } |
| 7270 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7271 | static void hlua_applet_htx_fct(struct appctx *ctx) |
| 7272 | { |
| 7273 | struct stream_interface *si = ctx->owner; |
| 7274 | struct stream *strm = si_strm(si); |
| 7275 | struct channel *req = si_oc(si); |
| 7276 | struct channel *res = si_ic(si); |
| 7277 | struct act_rule *rule = ctx->rule; |
| 7278 | struct proxy *px = strm->be; |
| 7279 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7280 | struct htx *req_htx, *res_htx; |
| 7281 | |
| 7282 | res_htx = htx_from_buf(&res->buf); |
| 7283 | |
| 7284 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7285 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7286 | goto out; |
| 7287 | |
| 7288 | /* Check if the input buffer is avalaible. */ |
| 7289 | if (!b_size(&res->buf)) { |
| 7290 | si_rx_room_blk(si); |
| 7291 | goto out; |
| 7292 | } |
| 7293 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7294 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7295 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7296 | |
| 7297 | /* Set the currently running flag. */ |
| 7298 | if (!HLUA_IS_RUNNING(hlua) && |
| 7299 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7300 | struct htx_blk *blk; |
| 7301 | size_t count = co_data(req); |
| 7302 | |
| 7303 | if (!count) { |
| 7304 | si_cant_get(si); |
| 7305 | goto out; |
| 7306 | } |
| 7307 | |
| 7308 | /* We need to flush the request header. This left the body for |
| 7309 | * the Lua. |
| 7310 | */ |
| 7311 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7312 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7313 | while (count && blk) { |
| 7314 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7315 | uint32_t sz = htx_get_blksz(blk); |
| 7316 | |
| 7317 | if (sz > count) { |
| 7318 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7319 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7320 | goto out; |
| 7321 | } |
| 7322 | |
| 7323 | count -= sz; |
| 7324 | co_set_data(req, co_data(req) - sz); |
| 7325 | blk = htx_remove_blk(req_htx, blk); |
| 7326 | |
| 7327 | if (type == HTX_BLK_EOH) |
| 7328 | break; |
| 7329 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7330 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7331 | } |
| 7332 | |
| 7333 | /* Executes The applet if it is not done. */ |
| 7334 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7335 | |
| 7336 | /* Execute the function. */ |
| 7337 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7338 | /* finished. */ |
| 7339 | case HLUA_E_OK: |
| 7340 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7341 | break; |
| 7342 | |
| 7343 | /* yield. */ |
| 7344 | case HLUA_E_AGAIN: |
| 7345 | if (hlua->wake_time != TICK_ETERNITY) |
| 7346 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7347 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7348 | |
| 7349 | /* finished with error. */ |
| 7350 | case HLUA_E_ERRMSG: |
| 7351 | /* Display log. */ |
| 7352 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7353 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7354 | lua_pop(hlua->T, 1); |
| 7355 | goto error; |
| 7356 | |
| 7357 | case HLUA_E_ETMOUT: |
| 7358 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7359 | rule->arg.hlua_rule->fcn.name); |
| 7360 | goto error; |
| 7361 | |
| 7362 | case HLUA_E_NOMEM: |
| 7363 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7364 | rule->arg.hlua_rule->fcn.name); |
| 7365 | goto error; |
| 7366 | |
| 7367 | case HLUA_E_YIELD: /* unexpected */ |
| 7368 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7369 | rule->arg.hlua_rule->fcn.name); |
| 7370 | goto error; |
| 7371 | |
| 7372 | case HLUA_E_ERR: |
| 7373 | /* Display log. */ |
| 7374 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7375 | rule->arg.hlua_rule->fcn.name); |
| 7376 | goto error; |
| 7377 | |
| 7378 | default: |
| 7379 | goto error; |
| 7380 | } |
| 7381 | } |
| 7382 | |
| 7383 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7384 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7385 | goto done; |
| 7386 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7387 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7388 | goto error; |
| 7389 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7390 | /* Don't add TLR because mux-h1 will take care of it */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7391 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7392 | si_rx_room_blk(si); |
| 7393 | goto out; |
| 7394 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7395 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7396 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7397 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7398 | } |
| 7399 | |
| 7400 | done: |
| 7401 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7402 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7403 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7404 | si_shutr(si); |
| 7405 | } |
| 7406 | |
| 7407 | /* eat the whole request */ |
| 7408 | if (co_data(req)) { |
| 7409 | req_htx = htx_from_buf(&req->buf); |
| 7410 | co_htx_skip(req, req_htx, co_data(req)); |
| 7411 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7412 | } |
| 7413 | } |
| 7414 | |
| 7415 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7416 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7417 | return; |
| 7418 | |
| 7419 | error: |
| 7420 | |
| 7421 | /* If we are in HTTP mode, and we are not send any |
| 7422 | * data, return a 500 server error in best effort: |
| 7423 | * if there is no room available in the buffer, |
| 7424 | * just close the connection. |
| 7425 | */ |
| 7426 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7427 | struct buffer *err = &htx_err_chunks[HTTP_ERR_500]; |
| 7428 | |
| 7429 | channel_erase(res); |
| 7430 | res->buf.data = b_data(err); |
| 7431 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7432 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7433 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7434 | } |
| 7435 | if (!(strm->flags & SF_ERR_MASK)) |
| 7436 | strm->flags |= SF_ERR_RESOURCE; |
| 7437 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7438 | goto done; |
| 7439 | } |
| 7440 | |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7441 | void hlua_applet_http_fct(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7442 | { |
| 7443 | struct stream_interface *si = ctx->owner; |
| 7444 | struct stream *strm = si_strm(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7445 | struct channel *req = si_oc(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7446 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7447 | struct act_rule *rule = ctx->rule; |
| 7448 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7449 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7450 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7451 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7452 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7453 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7454 | int ret; |
| 7455 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7456 | if (IS_HTX_STRM(strm)) |
| 7457 | return hlua_applet_htx_fct(ctx); |
| 7458 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7459 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7460 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7461 | goto out; |
| 7462 | |
| 7463 | /* Check if the input buffer is avalaible. */ |
| 7464 | if (!b_size(&res->buf)) { |
| 7465 | si_rx_room_blk(si); |
| 7466 | goto out; |
| 7467 | } |
| 7468 | /* check that the output is not closed */ |
| 7469 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
| 7470 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7471 | |
| 7472 | /* Set the currently running flag. */ |
| 7473 | if (!HLUA_IS_RUNNING(hlua) && |
| 7474 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7475 | /* Store the max amount of bytes that we can read. */ |
| 7476 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 7477 | |
| 7478 | /* We need to flush the request header. This left the body |
| 7479 | * for the Lua. |
| 7480 | */ |
| 7481 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7482 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7483 | ret = co_getblk_nc(req, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7484 | if (ret == -1) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7485 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7486 | |
| 7487 | /* No data available, ask for more data. */ |
| 7488 | if (ret == 1) |
| 7489 | len2 = 0; |
| 7490 | if (ret == 0) |
| 7491 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 7492 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7493 | si_cant_get(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7494 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7495 | } |
| 7496 | |
| 7497 | /* skip the requests bytes. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7498 | co_skip(req, strm->txn->req.eoh + strm->txn->req.eol); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7499 | } |
| 7500 | |
| 7501 | /* Executes The applet if it is not done. */ |
| 7502 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7503 | |
| 7504 | /* Execute the function. */ |
| 7505 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7506 | /* finished. */ |
| 7507 | case HLUA_E_OK: |
| 7508 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7509 | break; |
| 7510 | |
| 7511 | /* yield. */ |
| 7512 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7513 | if (hlua->wake_time != TICK_ETERNITY) |
| 7514 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7515 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7516 | |
| 7517 | /* finished with error. */ |
| 7518 | case HLUA_E_ERRMSG: |
| 7519 | /* Display log. */ |
| 7520 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7521 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7522 | lua_pop(hlua->T, 1); |
| 7523 | goto error; |
| 7524 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7525 | case HLUA_E_ETMOUT: |
| 7526 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7527 | rule->arg.hlua_rule->fcn.name); |
| 7528 | goto error; |
| 7529 | |
| 7530 | case HLUA_E_NOMEM: |
| 7531 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7532 | rule->arg.hlua_rule->fcn.name); |
| 7533 | goto error; |
| 7534 | |
| 7535 | case HLUA_E_YIELD: /* unexpected */ |
| 7536 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7537 | rule->arg.hlua_rule->fcn.name); |
| 7538 | goto error; |
| 7539 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7540 | case HLUA_E_ERR: |
| 7541 | /* Display log. */ |
| 7542 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7543 | rule->arg.hlua_rule->fcn.name); |
| 7544 | goto error; |
| 7545 | |
| 7546 | default: |
| 7547 | goto error; |
| 7548 | } |
| 7549 | } |
| 7550 | |
| 7551 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7552 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7553 | goto done; |
| 7554 | |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 7555 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7556 | goto error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7557 | |
| 7558 | /* We must send the final chunk. */ |
| 7559 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 7560 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 7561 | |
| 7562 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 7563 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7564 | |
| 7565 | /* critical error. */ |
| 7566 | if (ret == -2 || ret == -3) { |
| 7567 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 7568 | rule->arg.hlua_rule->fcn.name); |
| 7569 | goto error; |
| 7570 | } |
| 7571 | |
| 7572 | /* no enough space error. */ |
| 7573 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7574 | si_rx_room_blk(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7575 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7576 | } |
| 7577 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7578 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7579 | ctx->ctx.hlua_apphttp.flags |= (APPLET_LAST_CHK|APPLET_RSP_SENT); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7580 | } |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7581 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7582 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7583 | done: |
| 7584 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 7585 | if (!(res->flags & CF_SHUTR)) { |
| 7586 | res->flags |= CF_READ_NULL; |
| 7587 | si_shutr(si); |
| 7588 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7589 | |
| 7590 | /* eat the whole request */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7591 | if (co_data(req)) |
| 7592 | co_skip(req, co_data(req)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7593 | } |
| 7594 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7595 | out: |
| 7596 | return; |
| 7597 | |
| 7598 | error: |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7599 | |
| 7600 | /* If we are in HTTP mode, and we are not send any |
| 7601 | * data, return a 500 server error in best effort: |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7602 | * if there is no room available in the buffer, |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7603 | * just close the connection. |
| 7604 | */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7605 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7606 | channel_erase(res); |
| 7607 | ci_putblk(res, error_500, strlen(error_500)); |
| 7608 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7609 | if (!(strm->flags & SF_ERR_MASK)) |
| 7610 | strm->flags |= SF_ERR_RESOURCE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7611 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7612 | goto done; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7613 | } |
| 7614 | |
| 7615 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7616 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7617 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7618 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7619 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7620 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7621 | } |
| 7622 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7623 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 7624 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7625 | * |
| 7626 | * This function can fail with an abort() due to an Lua critical error. |
| 7627 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7628 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7629 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7630 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7631 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7632 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7633 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7634 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7635 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7636 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7637 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7638 | if (!rule->arg.hlua_rule) { |
| 7639 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7640 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7641 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7642 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7643 | /* Memory for arguments. */ |
| 7644 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7645 | if (!rule->arg.hlua_rule->args) { |
| 7646 | memprintf(err, "out of memory error"); |
| 7647 | return ACT_RET_PRS_ERR; |
| 7648 | } |
| 7649 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7650 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7651 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7652 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7653 | /* Expect some arguments */ |
| 7654 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7655 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7656 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7657 | return ACT_RET_PRS_ERR; |
| 7658 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7659 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7660 | if (!rule->arg.hlua_rule->args[i]) { |
| 7661 | memprintf(err, "out of memory error"); |
| 7662 | return ACT_RET_PRS_ERR; |
| 7663 | } |
| 7664 | (*cur_arg)++; |
| 7665 | } |
| 7666 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7667 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7668 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7669 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7670 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7671 | } |
| 7672 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7673 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7674 | struct act_rule *rule, char **err) |
| 7675 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7676 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7677 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7678 | /* HTTP applets are forbidden in tcp-request rules. |
| 7679 | * HTTP applet request requires everything initilized by |
| 7680 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 7681 | * The applet will be immediately initilized, but its before |
| 7682 | * the call of this analyzer. |
| 7683 | */ |
| 7684 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7685 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7686 | return ACT_RET_PRS_ERR; |
| 7687 | } |
| 7688 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7689 | /* Memory for the rule. */ |
| 7690 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7691 | if (!rule->arg.hlua_rule) { |
| 7692 | memprintf(err, "out of memory error"); |
| 7693 | return ACT_RET_PRS_ERR; |
| 7694 | } |
| 7695 | |
| 7696 | /* Reference the Lua function and store the reference. */ |
| 7697 | rule->arg.hlua_rule->fcn = *fcn; |
| 7698 | |
| 7699 | /* TODO: later accept arguments. */ |
| 7700 | rule->arg.hlua_rule->args = NULL; |
| 7701 | |
| 7702 | /* Add applet pointer in the rule. */ |
| 7703 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7704 | rule->applet.name = fcn->name; |
| 7705 | rule->applet.init = hlua_applet_http_init; |
| 7706 | rule->applet.fct = hlua_applet_http_fct; |
| 7707 | rule->applet.release = hlua_applet_http_release; |
| 7708 | rule->applet.timeout = hlua_timeout_applet; |
| 7709 | |
| 7710 | return ACT_RET_PRS_OK; |
| 7711 | } |
| 7712 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7713 | /* This function is an LUA binding used for registering |
| 7714 | * "sample-conv" functions. It expects a converter name used |
| 7715 | * in the haproxy configuration file, and an LUA function. |
| 7716 | */ |
| 7717 | __LJMP static int hlua_register_action(lua_State *L) |
| 7718 | { |
| 7719 | struct action_kw_list *akl; |
| 7720 | const char *name; |
| 7721 | int ref; |
| 7722 | int len; |
| 7723 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7724 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7725 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7726 | /* Initialise the number of expected arguments at 0. */ |
| 7727 | nargs = 0; |
| 7728 | |
| 7729 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7730 | 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] | 7731 | |
| 7732 | /* First argument : converter name. */ |
| 7733 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7734 | |
| 7735 | /* Second argument : environment. */ |
| 7736 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7737 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7738 | |
| 7739 | /* Third argument : lua function. */ |
| 7740 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7741 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7742 | /* 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] | 7743 | if (lua_gettop(L) >= 4) |
| 7744 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7745 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7746 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7747 | lua_pushnil(L); |
| 7748 | while (lua_next(L, 2) != 0) { |
| 7749 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7750 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7751 | |
| 7752 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7753 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7754 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7755 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7756 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7757 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7758 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7759 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7760 | |
| 7761 | /* Fill fcn. */ |
| 7762 | fcn->name = strdup(name); |
| 7763 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7764 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7765 | fcn->function_ref = ref; |
| 7766 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7767 | /* Set the expected number od arguments. */ |
| 7768 | fcn->nargs = nargs; |
| 7769 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7770 | /* List head */ |
| 7771 | akl->list.n = akl->list.p = NULL; |
| 7772 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7773 | /* action keyword. */ |
| 7774 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7775 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7776 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7777 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7778 | |
| 7779 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7780 | |
| 7781 | akl->kw[0].match_pfx = 0; |
| 7782 | akl->kw[0].private = fcn; |
| 7783 | akl->kw[0].parse = action_register_lua; |
| 7784 | |
| 7785 | /* select the action registering point. */ |
| 7786 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7787 | tcp_req_cont_keywords_register(akl); |
| 7788 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7789 | tcp_res_cont_keywords_register(akl); |
| 7790 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7791 | http_req_keywords_register(akl); |
| 7792 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7793 | http_res_keywords_register(akl); |
| 7794 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7795 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7796 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7797 | "are expected.", lua_tostring(L, -1))); |
| 7798 | |
| 7799 | /* pop the environment string. */ |
| 7800 | lua_pop(L, 1); |
| 7801 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7802 | return ACT_RET_PRS_OK; |
| 7803 | } |
| 7804 | |
| 7805 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7806 | struct act_rule *rule, char **err) |
| 7807 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7808 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7809 | |
Christopher Faulet | eb70980 | 2019-04-11 22:04:08 +0200 | [diff] [blame] | 7810 | if (px->mode == PR_MODE_HTTP && (px->options2 & PR_O2_USE_HTX)) { |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7811 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 7812 | return ACT_RET_PRS_ERR; |
| 7813 | } |
| 7814 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7815 | /* Memory for the rule. */ |
| 7816 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7817 | if (!rule->arg.hlua_rule) { |
| 7818 | memprintf(err, "out of memory error"); |
| 7819 | return ACT_RET_PRS_ERR; |
| 7820 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7821 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7822 | /* Reference the Lua function and store the reference. */ |
| 7823 | rule->arg.hlua_rule->fcn = *fcn; |
| 7824 | |
| 7825 | /* TODO: later accept arguments. */ |
| 7826 | rule->arg.hlua_rule->args = NULL; |
| 7827 | |
| 7828 | /* Add applet pointer in the rule. */ |
| 7829 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7830 | rule->applet.name = fcn->name; |
| 7831 | rule->applet.init = hlua_applet_tcp_init; |
| 7832 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7833 | rule->applet.release = hlua_applet_tcp_release; |
| 7834 | rule->applet.timeout = hlua_timeout_applet; |
| 7835 | |
| 7836 | return 0; |
| 7837 | } |
| 7838 | |
| 7839 | /* This function is an LUA binding used for registering |
| 7840 | * "sample-conv" functions. It expects a converter name used |
| 7841 | * in the haproxy configuration file, and an LUA function. |
| 7842 | */ |
| 7843 | __LJMP static int hlua_register_service(lua_State *L) |
| 7844 | { |
| 7845 | struct action_kw_list *akl; |
| 7846 | const char *name; |
| 7847 | const char *env; |
| 7848 | int ref; |
| 7849 | int len; |
| 7850 | struct hlua_function *fcn; |
| 7851 | |
| 7852 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7853 | |
| 7854 | /* First argument : converter name. */ |
| 7855 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7856 | |
| 7857 | /* Second argument : environment. */ |
| 7858 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7859 | |
| 7860 | /* Third argument : lua function. */ |
| 7861 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7862 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7863 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7864 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7865 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7866 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7867 | fcn = calloc(1, sizeof(*fcn)); |
| 7868 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7869 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7870 | |
| 7871 | /* Fill fcn. */ |
| 7872 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7873 | fcn->name = calloc(1, len); |
| 7874 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7875 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7876 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7877 | fcn->function_ref = ref; |
| 7878 | |
| 7879 | /* List head */ |
| 7880 | akl->list.n = akl->list.p = NULL; |
| 7881 | |
| 7882 | /* converter keyword. */ |
| 7883 | len = strlen("lua.") + strlen(name) + 1; |
| 7884 | akl->kw[0].kw = calloc(1, len); |
| 7885 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7886 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7887 | |
| 7888 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7889 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7890 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7891 | if (strcmp(env, "tcp") == 0) |
| 7892 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7893 | else if (strcmp(env, "http") == 0) |
| 7894 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7895 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7896 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7897 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7898 | |
| 7899 | akl->kw[0].match_pfx = 0; |
| 7900 | akl->kw[0].private = fcn; |
| 7901 | |
| 7902 | /* End of array. */ |
| 7903 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7904 | |
| 7905 | /* Register this new converter */ |
| 7906 | service_keywords_register(akl); |
| 7907 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7908 | return 0; |
| 7909 | } |
| 7910 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7911 | /* This function initialises Lua cli handler. It copies the |
| 7912 | * arguments in the Lua stack and create channel IO objects. |
| 7913 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7914 | 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] | 7915 | { |
| 7916 | struct hlua *hlua; |
| 7917 | struct hlua_function *fcn; |
| 7918 | int i; |
| 7919 | const char *error; |
| 7920 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7921 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7922 | appctx->ctx.hlua_cli.fcn = private; |
| 7923 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7924 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7925 | if (!hlua) { |
| 7926 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7927 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7928 | } |
| 7929 | HLUA_INIT(hlua); |
| 7930 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7931 | |
| 7932 | /* Create task used by signal to wakeup applets. |
| 7933 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7934 | * applet_http. It is absolutely compatible. |
| 7935 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7936 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7937 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7938 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7939 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7940 | } |
| 7941 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7942 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7943 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7944 | |
| 7945 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7946 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7947 | 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] | 7948 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7949 | } |
| 7950 | |
| 7951 | /* The following Lua calls can fail. */ |
| 7952 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7953 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7954 | error = lua_tostring(hlua->T, -1); |
| 7955 | else |
| 7956 | error = "critical error"; |
| 7957 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7958 | goto error; |
| 7959 | } |
| 7960 | |
| 7961 | /* Check stack available size. */ |
| 7962 | if (!lua_checkstack(hlua->T, 2)) { |
| 7963 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7964 | goto error; |
| 7965 | } |
| 7966 | |
| 7967 | /* Restore the function in the stack. */ |
| 7968 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7969 | |
| 7970 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7971 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7972 | */ |
| 7973 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7974 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7975 | goto error; |
| 7976 | } |
| 7977 | hlua->nargs = 1; |
| 7978 | |
| 7979 | /* push keywords in the stack. */ |
| 7980 | for (i = 0; *args[i]; i++) { |
| 7981 | /* Check stack available size. */ |
| 7982 | if (!lua_checkstack(hlua->T, 1)) { |
| 7983 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7984 | goto error; |
| 7985 | } |
| 7986 | lua_pushstring(hlua->T, args[i]); |
| 7987 | hlua->nargs++; |
| 7988 | } |
| 7989 | |
| 7990 | /* We must initialize the execution timeouts. */ |
| 7991 | hlua->max_time = hlua_timeout_session; |
| 7992 | |
| 7993 | /* At this point the execution is safe. */ |
| 7994 | RESET_SAFE_LJMP(hlua->T); |
| 7995 | |
| 7996 | /* It's ok */ |
| 7997 | return 0; |
| 7998 | |
| 7999 | /* It's not ok. */ |
| 8000 | error: |
| 8001 | RESET_SAFE_LJMP(hlua->T); |
| 8002 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8003 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8004 | return 1; |
| 8005 | } |
| 8006 | |
| 8007 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8008 | { |
| 8009 | struct hlua *hlua; |
| 8010 | struct stream_interface *si; |
| 8011 | struct hlua_function *fcn; |
| 8012 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8013 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8014 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8015 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8016 | |
| 8017 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8018 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8019 | return 1; |
| 8020 | |
| 8021 | /* Execute the function. */ |
| 8022 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8023 | |
| 8024 | /* finished. */ |
| 8025 | case HLUA_E_OK: |
| 8026 | return 1; |
| 8027 | |
| 8028 | /* yield. */ |
| 8029 | case HLUA_E_AGAIN: |
| 8030 | /* We want write. */ |
| 8031 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8032 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8033 | /* Set the timeout. */ |
| 8034 | if (hlua->wake_time != TICK_ETERNITY) |
| 8035 | task_schedule(hlua->task, hlua->wake_time); |
| 8036 | return 0; |
| 8037 | |
| 8038 | /* finished with error. */ |
| 8039 | case HLUA_E_ERRMSG: |
| 8040 | /* Display log. */ |
| 8041 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8042 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8043 | lua_pop(hlua->T, 1); |
| 8044 | return 1; |
| 8045 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8046 | case HLUA_E_ETMOUT: |
| 8047 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8048 | fcn->name); |
| 8049 | return 1; |
| 8050 | |
| 8051 | case HLUA_E_NOMEM: |
| 8052 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8053 | fcn->name); |
| 8054 | return 1; |
| 8055 | |
| 8056 | case HLUA_E_YIELD: /* unexpected */ |
| 8057 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8058 | fcn->name); |
| 8059 | return 1; |
| 8060 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8061 | case HLUA_E_ERR: |
| 8062 | /* Display log. */ |
| 8063 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8064 | fcn->name); |
| 8065 | return 1; |
| 8066 | |
| 8067 | default: |
| 8068 | return 1; |
| 8069 | } |
| 8070 | |
| 8071 | return 1; |
| 8072 | } |
| 8073 | |
| 8074 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8075 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8076 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8077 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8078 | } |
| 8079 | |
| 8080 | /* This function is an LUA binding used for registering |
| 8081 | * new keywords in the cli. It expects a list of keywords |
| 8082 | * which are the "path". It is limited to 5 keywords. A |
| 8083 | * description of the command, a function to be executed |
| 8084 | * for the parsing and a function for io handlers. |
| 8085 | */ |
| 8086 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8087 | { |
| 8088 | struct cli_kw_list *cli_kws; |
| 8089 | const char *message; |
| 8090 | int ref_io; |
| 8091 | int len; |
| 8092 | struct hlua_function *fcn; |
| 8093 | int index; |
| 8094 | int i; |
| 8095 | |
| 8096 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8097 | |
| 8098 | /* First argument : an array of maximum 5 keywords. */ |
| 8099 | if (!lua_istable(L, 1)) |
| 8100 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8101 | |
| 8102 | /* Second argument : string with contextual message. */ |
| 8103 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8104 | |
| 8105 | /* Third and fourth argument : lua function. */ |
| 8106 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8107 | |
| 8108 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8109 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 8110 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8111 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8112 | fcn = calloc(1, sizeof(*fcn)); |
| 8113 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8114 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8115 | |
| 8116 | /* Fill path. */ |
| 8117 | index = 0; |
| 8118 | lua_pushnil(L); |
| 8119 | while(lua_next(L, 1) != 0) { |
| 8120 | if (index >= 5) |
| 8121 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8122 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8123 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8124 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 8125 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8126 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8127 | index++; |
| 8128 | lua_pop(L, 1); |
| 8129 | } |
| 8130 | |
| 8131 | /* Copy help message. */ |
| 8132 | cli_kws->kw[0].usage = strdup(message); |
| 8133 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8134 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8135 | |
| 8136 | /* Fill fcn io handler. */ |
| 8137 | len = strlen("<lua.cli>") + 1; |
| 8138 | for (i = 0; i < index; i++) |
| 8139 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8140 | fcn->name = calloc(1, len); |
| 8141 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8142 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8143 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8144 | for (i = 0; i < index; i++) { |
| 8145 | strncat((char *)fcn->name, ".", len); |
| 8146 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8147 | } |
| 8148 | strncat((char *)fcn->name, ">", len); |
| 8149 | fcn->function_ref = ref_io; |
| 8150 | |
| 8151 | /* Fill last entries. */ |
| 8152 | cli_kws->kw[0].private = fcn; |
| 8153 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8154 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8155 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8156 | |
| 8157 | /* Register this new converter */ |
| 8158 | cli_register_kw(cli_kws); |
| 8159 | |
| 8160 | return 0; |
| 8161 | } |
| 8162 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8163 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 8164 | struct proxy *defpx, const char *file, int line, |
| 8165 | char **err, unsigned int *timeout) |
| 8166 | { |
| 8167 | const char *error; |
| 8168 | |
| 8169 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8170 | if (error == PARSE_TIME_OVER) { |
| 8171 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8172 | args[1], args[0]); |
| 8173 | return -1; |
| 8174 | } |
| 8175 | else if (error == PARSE_TIME_UNDER) { |
| 8176 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8177 | args[1], args[0]); |
| 8178 | return -1; |
| 8179 | } |
| 8180 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8181 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8182 | return -1; |
| 8183 | } |
| 8184 | return 0; |
| 8185 | } |
| 8186 | |
| 8187 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 8188 | struct proxy *defpx, const char *file, int line, |
| 8189 | char **err) |
| 8190 | { |
| 8191 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8192 | file, line, err, &hlua_timeout_session); |
| 8193 | } |
| 8194 | |
| 8195 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 8196 | struct proxy *defpx, const char *file, int line, |
| 8197 | char **err) |
| 8198 | { |
| 8199 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8200 | file, line, err, &hlua_timeout_task); |
| 8201 | } |
| 8202 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8203 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 8204 | struct proxy *defpx, const char *file, int line, |
| 8205 | char **err) |
| 8206 | { |
| 8207 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8208 | file, line, err, &hlua_timeout_applet); |
| 8209 | } |
| 8210 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8211 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 8212 | struct proxy *defpx, const char *file, int line, |
| 8213 | char **err) |
| 8214 | { |
| 8215 | char *error; |
| 8216 | |
| 8217 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8218 | if (*error != '\0') { |
| 8219 | memprintf(err, "%s: invalid number", args[0]); |
| 8220 | return -1; |
| 8221 | } |
| 8222 | return 0; |
| 8223 | } |
| 8224 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8225 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 8226 | struct proxy *defpx, const char *file, int line, |
| 8227 | char **err) |
| 8228 | { |
| 8229 | char *error; |
| 8230 | |
| 8231 | if (*(args[1]) == 0) { |
| 8232 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8233 | return -1; |
| 8234 | } |
| 8235 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8236 | if (*error != '\0') { |
| 8237 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8238 | return -1; |
| 8239 | } |
| 8240 | return 0; |
| 8241 | } |
| 8242 | |
| 8243 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8244 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8245 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8246 | * the main lua entry point. |
| 8247 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8248 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8249 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8250 | * |
| 8251 | * In some error case, LUA set an error message in top of the stack. This function |
| 8252 | * 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] | 8253 | * |
| 8254 | * This function can fail with an abort() due to an Lua critical error. |
| 8255 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8256 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8257 | */ |
| 8258 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 8259 | struct proxy *defpx, const char *file, int line, |
| 8260 | char **err) |
| 8261 | { |
| 8262 | int error; |
| 8263 | |
| 8264 | /* Just load and compile the file. */ |
| 8265 | error = luaL_loadfile(gL.T, args[1]); |
| 8266 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8267 | 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] | 8268 | lua_pop(gL.T, 1); |
| 8269 | return -1; |
| 8270 | } |
| 8271 | |
| 8272 | /* If no syntax error where detected, execute the code. */ |
| 8273 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 8274 | switch (error) { |
| 8275 | case LUA_OK: |
| 8276 | break; |
| 8277 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8278 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8279 | lua_pop(gL.T, 1); |
| 8280 | return -1; |
| 8281 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8282 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8283 | return -1; |
| 8284 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8285 | 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] | 8286 | lua_pop(gL.T, 1); |
| 8287 | return -1; |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8288 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8289 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8290 | 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] | 8291 | lua_pop(gL.T, 1); |
| 8292 | return -1; |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8293 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8294 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8295 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8296 | lua_pop(gL.T, 1); |
| 8297 | return -1; |
| 8298 | } |
| 8299 | |
| 8300 | return 0; |
| 8301 | } |
| 8302 | |
| 8303 | /* configuration keywords declaration */ |
| 8304 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8305 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8306 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8307 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8308 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8309 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8310 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8311 | { 0, NULL, NULL }, |
| 8312 | }}; |
| 8313 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8314 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8315 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8316 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8317 | /* This function can fail with an abort() due to an Lua critical error. |
| 8318 | * We are in the initialisation process of HAProxy, this abort() is |
| 8319 | * tolerated. |
| 8320 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8321 | int hlua_post_init() |
| 8322 | { |
| 8323 | struct hlua_init_function *init; |
| 8324 | const char *msg; |
| 8325 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8326 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8327 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8328 | /* Call post initialisation function in safe environement. */ |
| 8329 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8330 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8331 | error = lua_tostring(gL.T, -1); |
| 8332 | else |
| 8333 | error = "critical error"; |
| 8334 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8335 | exit(1); |
| 8336 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8337 | |
| 8338 | #if USE_OPENSSL |
| 8339 | /* Initialize SSL server. */ |
| 8340 | if (socket_ssl.xprt->prepare_srv) { |
| 8341 | int saved_used_backed = global.ssl_used_backend; |
| 8342 | // don't affect maxconn automatic computation |
| 8343 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8344 | global.ssl_used_backend = saved_used_backed; |
| 8345 | } |
| 8346 | #endif |
| 8347 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8348 | hlua_fcn_post_init(gL.T); |
| 8349 | RESET_SAFE_LJMP(gL.T); |
| 8350 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8351 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8352 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 8353 | ret = hlua_ctx_resume(&gL, 0); |
| 8354 | switch (ret) { |
| 8355 | case HLUA_E_OK: |
| 8356 | lua_pop(gL.T, -1); |
| 8357 | return 1; |
| 8358 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8359 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8360 | return 0; |
| 8361 | case HLUA_E_ERRMSG: |
| 8362 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8363 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8364 | return 0; |
| 8365 | case HLUA_E_ERR: |
| 8366 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8367 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8368 | return 0; |
| 8369 | } |
| 8370 | } |
| 8371 | return 1; |
| 8372 | } |
| 8373 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8374 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8375 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8376 | * is the previously allocated size or the kind of object in case of a new |
| 8377 | * allocation. <nsize> is the requested new size. |
| 8378 | */ |
| 8379 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8380 | { |
| 8381 | struct hlua_mem_allocator *zone = ud; |
| 8382 | |
| 8383 | if (nsize == 0) { |
| 8384 | /* it's a free */ |
| 8385 | if (ptr) |
| 8386 | zone->allocated -= osize; |
| 8387 | free(ptr); |
| 8388 | return NULL; |
| 8389 | } |
| 8390 | |
| 8391 | if (!ptr) { |
| 8392 | /* it's a new allocation */ |
| 8393 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8394 | return NULL; |
| 8395 | |
| 8396 | ptr = malloc(nsize); |
| 8397 | if (ptr) |
| 8398 | zone->allocated += nsize; |
| 8399 | return ptr; |
| 8400 | } |
| 8401 | |
| 8402 | /* it's a realloc */ |
| 8403 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8404 | return NULL; |
| 8405 | |
| 8406 | ptr = realloc(ptr, nsize); |
| 8407 | if (ptr) |
| 8408 | zone->allocated += nsize - osize; |
| 8409 | return ptr; |
| 8410 | } |
| 8411 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8412 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8413 | * We are in the initialisation process of HAProxy, this abort() is |
| 8414 | * tolerated. |
| 8415 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8416 | void hlua_init(void) |
| 8417 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8418 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8419 | int idx; |
| 8420 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8421 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8422 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8423 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8424 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8425 | struct srv_kw *kw; |
| 8426 | int tmp_error; |
| 8427 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8428 | char *args[] = { /* SSL client configuration. */ |
| 8429 | "ssl", |
| 8430 | "verify", |
| 8431 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8432 | NULL |
| 8433 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8434 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8435 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8436 | /* Init main lua stack. */ |
| 8437 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8438 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8439 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8440 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8441 | hlua_sethlua(&gL); |
| 8442 | gL.Tref = LUA_REFNIL; |
| 8443 | gL.task = NULL; |
| 8444 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8445 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8446 | * the Lua function can fail with an abort. We are in the initialisation |
| 8447 | * process of HAProxy, this abort() is tolerated. |
| 8448 | */ |
| 8449 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8450 | /* Initialise lua. */ |
| 8451 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8452 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8453 | /* Set safe environment for the initialisation. */ |
| 8454 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8455 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8456 | error_msg = lua_tostring(gL.T, -1); |
| 8457 | else |
| 8458 | error_msg = "critical error"; |
| 8459 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8460 | exit(1); |
| 8461 | } |
| 8462 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8463 | /* |
| 8464 | * |
| 8465 | * Create "core" object. |
| 8466 | * |
| 8467 | */ |
| 8468 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8469 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8470 | lua_newtable(gL.T); |
| 8471 | |
| 8472 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8473 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8474 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8475 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8476 | /* Register special functions. */ |
| 8477 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8478 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8479 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8480 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8481 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8482 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8483 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8484 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8485 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8486 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8487 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8488 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8489 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8490 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8491 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8492 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8493 | hlua_class_function(gL.T, "log", hlua_log); |
| 8494 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8495 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8496 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8497 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8498 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8499 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8500 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8501 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8502 | |
| 8503 | /* |
| 8504 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8505 | * Register class Map |
| 8506 | * |
| 8507 | */ |
| 8508 | |
| 8509 | /* This table entry is the object "Map" base. */ |
| 8510 | lua_newtable(gL.T); |
| 8511 | |
| 8512 | /* register pattern types. */ |
| 8513 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8514 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8515 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8516 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8517 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8518 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8519 | |
| 8520 | /* register constructor. */ |
| 8521 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8522 | |
| 8523 | /* Create and fill the metatable. */ |
| 8524 | lua_newtable(gL.T); |
| 8525 | |
| 8526 | /* Create and fille the __index entry. */ |
| 8527 | lua_pushstring(gL.T, "__index"); |
| 8528 | lua_newtable(gL.T); |
| 8529 | |
| 8530 | /* Register . */ |
| 8531 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8532 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8533 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8534 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8535 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8536 | /* Register previous table in the registry with reference and named entry. |
| 8537 | * The function hlua_register_metatable() pops the stack, so we |
| 8538 | * previously create a copy of the table. |
| 8539 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8540 | 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] | 8541 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8542 | |
| 8543 | /* Assign the metatable to the mai Map object. */ |
| 8544 | lua_setmetatable(gL.T, -2); |
| 8545 | |
| 8546 | /* Set a name to the table. */ |
| 8547 | lua_setglobal(gL.T, "Map"); |
| 8548 | |
| 8549 | /* |
| 8550 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8551 | * Register class Channel |
| 8552 | * |
| 8553 | */ |
| 8554 | |
| 8555 | /* Create and fill the metatable. */ |
| 8556 | lua_newtable(gL.T); |
| 8557 | |
| 8558 | /* Create and fille the __index entry. */ |
| 8559 | lua_pushstring(gL.T, "__index"); |
| 8560 | lua_newtable(gL.T); |
| 8561 | |
| 8562 | /* Register . */ |
| 8563 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8564 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8565 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8566 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8567 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8568 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8569 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8570 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8571 | 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] | 8572 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8573 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8574 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8575 | |
| 8576 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8577 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8578 | |
| 8579 | /* |
| 8580 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8581 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8582 | * |
| 8583 | */ |
| 8584 | |
| 8585 | /* Create and fill the metatable. */ |
| 8586 | lua_newtable(gL.T); |
| 8587 | |
| 8588 | /* Create and fille the __index entry. */ |
| 8589 | lua_pushstring(gL.T, "__index"); |
| 8590 | lua_newtable(gL.T); |
| 8591 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8592 | /* Browse existing fetches and create the associated |
| 8593 | * object method. |
| 8594 | */ |
| 8595 | sf = NULL; |
| 8596 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8597 | |
| 8598 | /* Dont register the keywork if the arguments check function are |
| 8599 | * not safe during the runtime. |
| 8600 | */ |
| 8601 | if ((sf->val_args != NULL) && |
| 8602 | (sf->val_args != val_payload_lv) && |
| 8603 | (sf->val_args != val_hdr)) |
| 8604 | continue; |
| 8605 | |
| 8606 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8607 | * by an underscore. |
| 8608 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8609 | strncpy(trash.area, sf->kw, trash.size); |
| 8610 | trash.area[trash.size - 1] = '\0'; |
| 8611 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8612 | if (*p == '.' || *p == '-' || *p == '+') |
| 8613 | *p = '_'; |
| 8614 | |
| 8615 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8616 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8617 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8618 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8619 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8620 | } |
| 8621 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8622 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8623 | |
| 8624 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8625 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8626 | |
| 8627 | /* |
| 8628 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8629 | * Register class Converters |
| 8630 | * |
| 8631 | */ |
| 8632 | |
| 8633 | /* Create and fill the metatable. */ |
| 8634 | lua_newtable(gL.T); |
| 8635 | |
| 8636 | /* Create and fill the __index entry. */ |
| 8637 | lua_pushstring(gL.T, "__index"); |
| 8638 | lua_newtable(gL.T); |
| 8639 | |
| 8640 | /* Browse existing converters and create the associated |
| 8641 | * object method. |
| 8642 | */ |
| 8643 | sc = NULL; |
| 8644 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8645 | /* Dont register the keywork if the arguments check function are |
| 8646 | * not safe during the runtime. |
| 8647 | */ |
| 8648 | if (sc->val_args != NULL) |
| 8649 | continue; |
| 8650 | |
| 8651 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8652 | * by an underscore. |
| 8653 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8654 | strncpy(trash.area, sc->kw, trash.size); |
| 8655 | trash.area[trash.size - 1] = '\0'; |
| 8656 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8657 | if (*p == '.' || *p == '-' || *p == '+') |
| 8658 | *p = '_'; |
| 8659 | |
| 8660 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8661 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8662 | lua_pushlightuserdata(gL.T, sc); |
| 8663 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8664 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8665 | } |
| 8666 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8667 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8668 | |
| 8669 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8670 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8671 | |
| 8672 | /* |
| 8673 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8674 | * Register class HTTP |
| 8675 | * |
| 8676 | */ |
| 8677 | |
| 8678 | /* Create and fill the metatable. */ |
| 8679 | lua_newtable(gL.T); |
| 8680 | |
| 8681 | /* Create and fille the __index entry. */ |
| 8682 | lua_pushstring(gL.T, "__index"); |
| 8683 | lua_newtable(gL.T); |
| 8684 | |
| 8685 | /* Register Lua functions. */ |
| 8686 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8687 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8688 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8689 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8690 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8691 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8692 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8693 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8694 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8695 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8696 | |
| 8697 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8698 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8699 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8700 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8701 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8702 | 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] | 8703 | 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] | 8704 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8705 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8706 | |
| 8707 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8708 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8709 | |
| 8710 | /* |
| 8711 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8712 | * Register class AppletTCP |
| 8713 | * |
| 8714 | */ |
| 8715 | |
| 8716 | /* Create and fill the metatable. */ |
| 8717 | lua_newtable(gL.T); |
| 8718 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8719 | /* Create and fille the __index entry. */ |
| 8720 | lua_pushstring(gL.T, "__index"); |
| 8721 | lua_newtable(gL.T); |
| 8722 | |
| 8723 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8724 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8725 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8726 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8727 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8728 | 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] | 8729 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8730 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8731 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8732 | |
| 8733 | lua_settable(gL.T, -3); |
| 8734 | |
| 8735 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8736 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8737 | |
| 8738 | /* |
| 8739 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8740 | * Register class AppletHTTP |
| 8741 | * |
| 8742 | */ |
| 8743 | |
| 8744 | /* Create and fill the metatable. */ |
| 8745 | lua_newtable(gL.T); |
| 8746 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8747 | /* Create and fille the __index entry. */ |
| 8748 | lua_pushstring(gL.T, "__index"); |
| 8749 | lua_newtable(gL.T); |
| 8750 | |
| 8751 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8752 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8753 | 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] | 8754 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8755 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8756 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8757 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8758 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8759 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8760 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8761 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8762 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8763 | |
| 8764 | lua_settable(gL.T, -3); |
| 8765 | |
| 8766 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8767 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8768 | |
| 8769 | /* |
| 8770 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8771 | * Register class TXN |
| 8772 | * |
| 8773 | */ |
| 8774 | |
| 8775 | /* Create and fill the metatable. */ |
| 8776 | lua_newtable(gL.T); |
| 8777 | |
| 8778 | /* Create and fille the __index entry. */ |
| 8779 | lua_pushstring(gL.T, "__index"); |
| 8780 | lua_newtable(gL.T); |
| 8781 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8782 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8783 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8784 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8785 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8786 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8787 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8788 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 8789 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8790 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8791 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8792 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8793 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8794 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8795 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8796 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8797 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8798 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8799 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8800 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8801 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8802 | |
| 8803 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8804 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8805 | |
| 8806 | /* |
| 8807 | * |
| 8808 | * Register class Socket |
| 8809 | * |
| 8810 | */ |
| 8811 | |
| 8812 | /* Create and fill the metatable. */ |
| 8813 | lua_newtable(gL.T); |
| 8814 | |
| 8815 | /* Create and fille the __index entry. */ |
| 8816 | lua_pushstring(gL.T, "__index"); |
| 8817 | lua_newtable(gL.T); |
| 8818 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8819 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8820 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8821 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8822 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8823 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8824 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8825 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8826 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8827 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8828 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8829 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8830 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8831 | 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] | 8832 | |
| 8833 | /* Register the garbage collector entry. */ |
| 8834 | lua_pushstring(gL.T, "__gc"); |
| 8835 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8836 | 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] | 8837 | |
| 8838 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8839 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8840 | |
| 8841 | /* Proxy and server configuration initialisation. */ |
| 8842 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8843 | init_new_proxy(&socket_proxy); |
| 8844 | socket_proxy.parent = NULL; |
| 8845 | socket_proxy.last_change = now.tv_sec; |
| 8846 | socket_proxy.id = "LUA-SOCKET"; |
| 8847 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8848 | socket_proxy.maxconn = 0; |
| 8849 | socket_proxy.accept = NULL; |
| 8850 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8851 | socket_proxy.srv = NULL; |
| 8852 | socket_proxy.conn_retries = 0; |
| 8853 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8854 | |
| 8855 | /* Init TCP server: unchanged parameters */ |
| 8856 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8857 | socket_tcp.next = NULL; |
| 8858 | socket_tcp.proxy = &socket_proxy; |
| 8859 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8860 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8861 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8862 | socket_tcp.priv_conns = NULL; |
| 8863 | socket_tcp.idle_conns = NULL; |
| 8864 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8865 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8866 | socket_tcp.last_change = 0; |
| 8867 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8868 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8869 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8870 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8871 | |
| 8872 | /* XXX: Copy default parameter from default server, |
| 8873 | * but the default server is not initialized. |
| 8874 | */ |
| 8875 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8876 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8877 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8878 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8879 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8880 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8881 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8882 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8883 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8884 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8885 | |
| 8886 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8887 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8888 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8889 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8890 | socket_tcp.check.server = &socket_tcp; |
| 8891 | |
| 8892 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8893 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8894 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8895 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8896 | socket_tcp.agent.server = &socket_tcp; |
| 8897 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8898 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8899 | |
| 8900 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8901 | /* Init TCP server: unchanged parameters */ |
| 8902 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8903 | socket_ssl.next = NULL; |
| 8904 | socket_ssl.proxy = &socket_proxy; |
| 8905 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8906 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8907 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8908 | socket_ssl.priv_conns = NULL; |
| 8909 | socket_ssl.idle_conns = NULL; |
| 8910 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8911 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8912 | socket_ssl.last_change = 0; |
| 8913 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8914 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8915 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8916 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8917 | |
| 8918 | /* XXX: Copy default parameter from default server, |
| 8919 | * but the default server is not initialized. |
| 8920 | */ |
| 8921 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8922 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8923 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8924 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8925 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8926 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8927 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8928 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8929 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8930 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8931 | |
| 8932 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8933 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8934 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8935 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8936 | socket_ssl.check.server = &socket_ssl; |
| 8937 | |
| 8938 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8939 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8940 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8941 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8942 | socket_ssl.agent.server = &socket_ssl; |
| 8943 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8944 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8945 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8946 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8947 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8948 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8949 | /* |
| 8950 | * |
| 8951 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8952 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8953 | * features like client certificates and ssl_verify. |
| 8954 | * |
| 8955 | */ |
| 8956 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8957 | if (tmp_error != 0) { |
| 8958 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8959 | abort(); /* This must be never arrives because the command line |
| 8960 | not editable by the user. */ |
| 8961 | } |
| 8962 | idx += kw->skip; |
| 8963 | } |
| 8964 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8965 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8966 | |
| 8967 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8968 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8969 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8970 | static void hlua_register_build_options(void) |
| 8971 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8972 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8973 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8974 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8975 | hap_register_build_opts(ptr, 1); |
| 8976 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8977 | |
| 8978 | INITCALL0(STG_REGISTER, hlua_register_build_options); |