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: |
Christopher Faulet | e7f1fcf | 2020-08-07 09:07:26 +0200 | [diff] [blame] | 757 | if (argp[idx].type != ARGT_STR) |
| 758 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 759 | memcpy(trash.area, argp[idx].data.str.area, |
| 760 | argp[idx].data.str.data); |
| 761 | trash.area[argp[idx].data.str.data] = 0; |
| 762 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 763 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 764 | argp[idx].type = ARGT_IPV4; |
| 765 | break; |
| 766 | |
| 767 | case ARGT_MSK4: |
Christopher Faulet | ff8de2c | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 768 | if (argp[idx].type == ARGT_SINT) |
| 769 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 770 | else if (argp[idx].type == ARGT_STR) { |
| 771 | memcpy(trash.area, argp[idx].data.str.area, |
| 772 | argp[idx].data.str.data); |
| 773 | trash.area[argp[idx].data.str.data] = 0; |
| 774 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
| 775 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 776 | } |
| 777 | else |
| 778 | WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected")); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 779 | argp[idx].type = ARGT_MSK4; |
| 780 | break; |
| 781 | |
| 782 | case ARGT_IPV6: |
Christopher Faulet | e7f1fcf | 2020-08-07 09:07:26 +0200 | [diff] [blame] | 783 | if (argp[idx].type != ARGT_STR) |
| 784 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 785 | memcpy(trash.area, argp[idx].data.str.area, |
| 786 | argp[idx].data.str.data); |
| 787 | trash.area[argp[idx].data.str.data] = 0; |
| 788 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 789 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 790 | argp[idx].type = ARGT_IPV6; |
| 791 | break; |
| 792 | |
| 793 | case ARGT_MSK6: |
Christopher Faulet | ff8de2c | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 794 | if (argp[idx].type == ARGT_SINT) |
| 795 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 796 | else if (argp[idx].type == ARGT_STR) { |
| 797 | memcpy(trash.area, argp[idx].data.str.area, |
| 798 | argp[idx].data.str.data); |
| 799 | trash.area[argp[idx].data.str.data] = 0; |
| 800 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
| 801 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 802 | } |
| 803 | else |
| 804 | WILL_LJMP(luaL_argerror(L, first + idx, "integer or string expected")); |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 805 | argp[idx].type = ARGT_MSK6; |
| 806 | break; |
| 807 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 808 | case ARGT_MAP: |
| 809 | case ARGT_REG: |
| 810 | case ARGT_USR: |
| 811 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 812 | break; |
| 813 | } |
| 814 | |
| 815 | /* Check for type of argument. */ |
| 816 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 817 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 818 | arg_type_names[(mask & ARGT_MASK)], |
| 819 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 820 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 821 | } |
| 822 | |
| 823 | /* Next argument. */ |
| 824 | mask >>= ARGT_BITS; |
| 825 | idx++; |
| 826 | } |
| 827 | } |
| 828 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 829 | /* |
| 830 | * The following functions are used to make correspondance between the the |
| 831 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 832 | * |
| 833 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 834 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 835 | */ |
| 836 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 837 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 838 | struct hlua **hlua = lua_getextraspace(L); |
| 839 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 840 | } |
| 841 | static inline void hlua_sethlua(struct hlua *hlua) |
| 842 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 843 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 844 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 845 | } |
| 846 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 847 | /* This function is used to send logs. It try to send on screen (stderr) |
| 848 | * and on the default syslog server. |
| 849 | */ |
| 850 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 851 | { |
| 852 | struct tm tm; |
| 853 | char *p; |
| 854 | |
| 855 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 856 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 857 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 858 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 859 | /* Break the message if exceed the buffer size. */ |
| 860 | *(p-4) = ' '; |
| 861 | *(p-3) = '.'; |
| 862 | *(p-2) = '.'; |
| 863 | *(p-1) = '.'; |
| 864 | break; |
| 865 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 866 | if (isprint(*msg)) |
| 867 | *p = *msg; |
| 868 | else |
| 869 | *p = '.'; |
| 870 | } |
| 871 | *p = '\0'; |
| 872 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 873 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 874 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | b290edf | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 875 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 876 | return; |
| 877 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 878 | get_localtime(date.tv_sec, &tm); |
| 879 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 880 | 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] | 881 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 882 | fflush(stderr); |
| 883 | } |
| 884 | } |
| 885 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 886 | /* This function just ensure that the yield will be always |
| 887 | * returned with a timeout and permit to set some flags |
| 888 | */ |
| 889 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 890 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 891 | { |
| 892 | struct hlua *hlua = hlua_gethlua(L); |
| 893 | |
| 894 | /* Set the wake timeout. If timeout is required, we set |
| 895 | * the expiration time. |
| 896 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 897 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 898 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 899 | hlua->flags |= flags; |
| 900 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 901 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 902 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 903 | } |
| 904 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 905 | /* This function initialises the Lua environment stored in the stream. |
| 906 | * 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] | 907 | * 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] | 908 | * |
| 909 | * This function is particular. it initialises a new Lua thread. If the |
| 910 | * initialisation fails (example: out of memory error), the lua function |
| 911 | * throws an error (longjmp). |
| 912 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 913 | * In some case (at least one), this function can be called from safe |
| 914 | * environement, so we must not initialise it. While the support of |
| 915 | * threads appear, the safe environment set a lock to ensure only one |
| 916 | * Lua execution at a time. If we initialize safe environment in another |
| 917 | * safe environmenet, we have a dead lock. |
| 918 | * |
| 919 | * set "already_safe" true if the context is initialized form safe |
| 920 | * Lua fonction. |
| 921 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 922 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 923 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 924 | * MUST NOT manipulate the created thread stack state, because it is not |
| 925 | * proctected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 926 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 927 | 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] | 928 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 929 | if (!already_safe) { |
| 930 | if (!SET_SAFE_LJMP(gL.T)) { |
| 931 | lua->Tref = LUA_REFNIL; |
| 932 | return 0; |
| 933 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 934 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 935 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 936 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 937 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 938 | lua->T = lua_newthread(gL.T); |
| 939 | if (!lua->T) { |
| 940 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 941 | if (!already_safe) |
| 942 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 943 | return 0; |
| 944 | } |
| 945 | hlua_sethlua(lua); |
| 946 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 947 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 948 | if (!already_safe) |
| 949 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 950 | return 1; |
| 951 | } |
| 952 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 953 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 954 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 955 | * is not freed. |
| 956 | */ |
| 957 | void hlua_ctx_destroy(struct hlua *lua) |
| 958 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 959 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 960 | return; |
| 961 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 962 | if (!lua->T) |
| 963 | goto end; |
| 964 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 965 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 966 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 967 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 968 | if (!SET_SAFE_LJMP(lua->T)) |
| 969 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 970 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 971 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 972 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 973 | if (!SET_SAFE_LJMP(gL.T)) |
| 974 | return; |
| 975 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 976 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 977 | /* Forces a garbage collecting process. If the Lua program is finished |
| 978 | * without error, we run the GC on the thread pointer. Its freed all |
| 979 | * the unused memory. |
| 980 | * If the thread is finnish with an error or is currently yielded, |
| 981 | * it seems that the GC applied on the thread doesn't clean anything, |
| 982 | * so e run the GC on the main thread. |
| 983 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 984 | * the garbage collection. |
| 985 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 986 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 987 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 988 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 989 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 990 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 991 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 992 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 993 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 994 | |
| 995 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 996 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | /* This function is used to restore the Lua context when a coroutine |
| 1000 | * fails. This function copy the common memory between old coroutine |
| 1001 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1002 | * replaced by the new coroutine. |
| 1003 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1004 | * as string error message and it is copied in the new stack. |
| 1005 | */ |
| 1006 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1007 | { |
| 1008 | lua_State *T; |
| 1009 | int new_ref; |
| 1010 | |
| 1011 | /* Renew the main LUA stack doesn't have sense. */ |
| 1012 | if (lua == &gL) |
| 1013 | return 0; |
| 1014 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1015 | /* New Lua coroutine. */ |
| 1016 | T = lua_newthread(gL.T); |
| 1017 | if (!T) |
| 1018 | return 0; |
| 1019 | |
| 1020 | /* Copy last error message. */ |
| 1021 | if (keep_msg) |
| 1022 | lua_xmove(lua->T, T, 1); |
| 1023 | |
| 1024 | /* Copy data between the coroutines. */ |
| 1025 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1026 | lua_xmove(lua->T, T, 1); |
| 1027 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 1028 | |
| 1029 | /* Destroy old data. */ |
| 1030 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1031 | |
| 1032 | /* The thread is garbage collected by Lua. */ |
| 1033 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1034 | |
| 1035 | /* Fill the struct with the new coroutine values. */ |
| 1036 | lua->Mref = new_ref; |
| 1037 | lua->T = T; |
| 1038 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1039 | |
| 1040 | /* Set context. */ |
| 1041 | hlua_sethlua(lua); |
| 1042 | |
| 1043 | return 1; |
| 1044 | } |
| 1045 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1046 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1047 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1048 | struct hlua *hlua = hlua_gethlua(L); |
| 1049 | |
| 1050 | /* Lua cannot yield when its returning from a function, |
| 1051 | * so, we can fix the interrupt hook to 1 instruction, |
| 1052 | * expecting that the function is finnished. |
| 1053 | */ |
| 1054 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1055 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1056 | return; |
| 1057 | } |
| 1058 | |
| 1059 | /* restore the interrupt condition. */ |
| 1060 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1061 | |
| 1062 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1063 | * If the state is not yieldable, trying yield causes an error. |
| 1064 | */ |
| 1065 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1066 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1067 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1068 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1069 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1070 | hlua->run_time += now_ms - hlua->start_time; |
| 1071 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1072 | lua_pushfstring(L, "execution timeout"); |
| 1073 | WILL_LJMP(lua_error(L)); |
| 1074 | } |
| 1075 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1076 | /* Update the start time. */ |
| 1077 | hlua->start_time = now_ms; |
| 1078 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1079 | /* Try to interrupt the process at the end of the current |
| 1080 | * unyieldable function. |
| 1081 | */ |
| 1082 | 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] | 1083 | } |
| 1084 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1085 | /* This function start or resumes the Lua stack execution. If the flag |
| 1086 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1087 | * The function return an error. |
| 1088 | * |
| 1089 | * The function can returns 4 values: |
| 1090 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1091 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1092 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1093 | * - 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] | 1094 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1095 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1096 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1097 | * 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] | 1098 | * LUA code. |
| 1099 | */ |
| 1100 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1101 | { |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1102 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1103 | int nres; |
| 1104 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1105 | int ret; |
| 1106 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1107 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1108 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1109 | /* Initialise run time counter. */ |
| 1110 | if (!HLUA_IS_RUNNING(lua)) |
| 1111 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1112 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1113 | /* Lock the whole Lua execution. This lock must be before the |
| 1114 | * label "resume_execution". |
| 1115 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1116 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1117 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1118 | resume_execution: |
| 1119 | |
| 1120 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1121 | * instructions. it is used for preventing infinite loops. |
| 1122 | */ |
| 1123 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1124 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1125 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1126 | HLUA_SET_RUN(lua); |
| 1127 | HLUA_CLR_CTRLYIELD(lua); |
| 1128 | HLUA_CLR_WAKERESWR(lua); |
| 1129 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1130 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1131 | /* Update the start time. */ |
| 1132 | lua->start_time = now_ms; |
| 1133 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1134 | /* Call the function. */ |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1135 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1136 | ret = lua_resume(lua->T, gL.T, lua->nargs, &nres); |
| 1137 | #else |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1138 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1139 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1140 | switch (ret) { |
| 1141 | |
| 1142 | case LUA_OK: |
| 1143 | ret = HLUA_E_OK; |
| 1144 | break; |
| 1145 | |
| 1146 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1147 | /* Check if the execution timeout is expired. It it is the case, we |
| 1148 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1149 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1150 | tv_update_date(0, 1); |
| 1151 | lua->run_time += now_ms - lua->start_time; |
| 1152 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1153 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1154 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1155 | break; |
| 1156 | } |
| 1157 | /* Process the forced yield. if the general yield is not allowed or |
| 1158 | * if no task were associated this the current Lua execution |
| 1159 | * coroutine, we resume the execution. Else we want to return in the |
| 1160 | * scheduler and we want to be waked up again, to continue the |
| 1161 | * current Lua execution. So we schedule our own task. |
| 1162 | */ |
| 1163 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1164 | if (!yield_allowed || !lua->task) |
| 1165 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1166 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1167 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1168 | if (!yield_allowed) { |
| 1169 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1170 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1171 | break; |
| 1172 | } |
| 1173 | ret = HLUA_E_AGAIN; |
| 1174 | break; |
| 1175 | |
| 1176 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1177 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1178 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1179 | * because the errors ares the only one mean to return immediately |
| 1180 | * from and lua execution. |
| 1181 | */ |
| 1182 | if (lua->flags & HLUA_EXIT) { |
| 1183 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1184 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1185 | break; |
| 1186 | } |
| 1187 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1188 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1189 | if (!lua_checkstack(lua->T, 1)) { |
| 1190 | ret = HLUA_E_ERR; |
| 1191 | break; |
| 1192 | } |
| 1193 | msg = lua_tostring(lua->T, -1); |
| 1194 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1195 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1196 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1197 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1198 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1199 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1200 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1201 | ret = HLUA_E_ERRMSG; |
| 1202 | break; |
| 1203 | |
| 1204 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1205 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1206 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1207 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1208 | break; |
| 1209 | |
| 1210 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1211 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1212 | if (!lua_checkstack(lua->T, 1)) { |
| 1213 | ret = HLUA_E_ERR; |
| 1214 | break; |
| 1215 | } |
| 1216 | msg = lua_tostring(lua->T, -1); |
| 1217 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1218 | lua_pop(lua->T, 1); |
| 1219 | if (msg) |
| 1220 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1221 | else |
| 1222 | lua_pushfstring(lua->T, "message handler error"); |
| 1223 | ret = HLUA_E_ERRMSG; |
| 1224 | break; |
| 1225 | |
| 1226 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1227 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1228 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1229 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1230 | break; |
| 1231 | } |
| 1232 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1233 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1234 | if (lua->flags & HLUA_MUST_GC && |
| 1235 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1236 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1237 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1238 | switch (ret) { |
| 1239 | case HLUA_E_AGAIN: |
| 1240 | break; |
| 1241 | |
| 1242 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1243 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1244 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1245 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1246 | break; |
| 1247 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1248 | case HLUA_E_ETMOUT: |
| 1249 | case HLUA_E_NOMEM: |
| 1250 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1251 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1252 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1253 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1254 | hlua_ctx_renew(lua, 0); |
| 1255 | break; |
| 1256 | |
| 1257 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1258 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1259 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1260 | break; |
| 1261 | } |
| 1262 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1263 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1264 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1265 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1266 | return ret; |
| 1267 | } |
| 1268 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1269 | /* This function exit the current code. */ |
| 1270 | __LJMP static int hlua_done(lua_State *L) |
| 1271 | { |
| 1272 | struct hlua *hlua = hlua_gethlua(L); |
| 1273 | |
| 1274 | hlua->flags |= HLUA_EXIT; |
| 1275 | WILL_LJMP(lua_error(L)); |
| 1276 | |
| 1277 | return 0; |
| 1278 | } |
| 1279 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1280 | /* This function is an LUA binding. It provides a function |
| 1281 | * for deleting ACL from a referenced ACL file. |
| 1282 | */ |
| 1283 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1284 | { |
| 1285 | const char *name; |
| 1286 | const char *key; |
| 1287 | struct pat_ref *ref; |
| 1288 | |
| 1289 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1290 | |
| 1291 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1292 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1293 | |
| 1294 | ref = pat_ref_lookup(name); |
| 1295 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1296 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1297 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1298 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1299 | pat_ref_delete(ref, key); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1300 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1301 | return 0; |
| 1302 | } |
| 1303 | |
| 1304 | /* This function is an LUA binding. It provides a function |
| 1305 | * for deleting map entry from a referenced map file. |
| 1306 | */ |
| 1307 | static int hlua_del_map(lua_State *L) |
| 1308 | { |
| 1309 | const char *name; |
| 1310 | const char *key; |
| 1311 | struct pat_ref *ref; |
| 1312 | |
| 1313 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1314 | |
| 1315 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1316 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1317 | |
| 1318 | ref = pat_ref_lookup(name); |
| 1319 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1320 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1321 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1322 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1323 | pat_ref_delete(ref, key); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1324 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | /* This function is an LUA binding. It provides a function |
| 1329 | * for adding ACL pattern from a referenced ACL file. |
| 1330 | */ |
| 1331 | static int hlua_add_acl(lua_State *L) |
| 1332 | { |
| 1333 | const char *name; |
| 1334 | const char *key; |
| 1335 | struct pat_ref *ref; |
| 1336 | |
| 1337 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1338 | |
| 1339 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1340 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1341 | |
| 1342 | ref = pat_ref_lookup(name); |
| 1343 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1344 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1345 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1346 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1347 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1348 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1349 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1350 | return 0; |
| 1351 | } |
| 1352 | |
| 1353 | /* This function is an LUA binding. It provides a function |
| 1354 | * for setting map pattern and sample from a referenced map |
| 1355 | * file. |
| 1356 | */ |
| 1357 | static int hlua_set_map(lua_State *L) |
| 1358 | { |
| 1359 | const char *name; |
| 1360 | const char *key; |
| 1361 | const char *value; |
| 1362 | struct pat_ref *ref; |
| 1363 | |
| 1364 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1365 | |
| 1366 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1367 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1368 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1369 | |
| 1370 | ref = pat_ref_lookup(name); |
| 1371 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1372 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1373 | |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1374 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1375 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1376 | pat_ref_set(ref, key, value, NULL); |
| 1377 | else |
| 1378 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 9fa1f4e | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1379 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1380 | return 0; |
| 1381 | } |
| 1382 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1383 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1384 | * an integer or user data. This data is associated with a metatable. This |
| 1385 | * metatable have an original version registred in the global context with |
| 1386 | * the name of the object (_G[<name>] = <metable> ). |
| 1387 | * |
| 1388 | * A metable is a table that modify the standard behavior of a standard |
| 1389 | * access to the associated data. The entries of this new metatable are |
| 1390 | * defined as is: |
| 1391 | * |
| 1392 | * http://lua-users.org/wiki/MetatableEvents |
| 1393 | * |
| 1394 | * __index |
| 1395 | * |
| 1396 | * we access an absent field in a table, the result is nil. This is |
| 1397 | * true, but it is not the whole truth. Actually, such access triggers |
| 1398 | * the interpreter to look for an __index metamethod: If there is no |
| 1399 | * such method, as usually happens, then the access results in nil; |
| 1400 | * otherwise, the metamethod will provide the result. |
| 1401 | * |
| 1402 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1403 | * the key does not appear in the table, but the metatable has an __index |
| 1404 | * property: |
| 1405 | * |
| 1406 | * - if the value is a function, the function is called, passing in the |
| 1407 | * table and the key; the return value of that function is returned as |
| 1408 | * the result. |
| 1409 | * |
| 1410 | * - if the value is another table, the value of the key in that table is |
| 1411 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1412 | * table's metatable has an __index property, then it continues on up) |
| 1413 | * |
| 1414 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1415 | * |
| 1416 | * http://www.lua.org/pil/13.4.1.html |
| 1417 | * |
| 1418 | * __newindex |
| 1419 | * |
| 1420 | * Like __index, but control property assignment. |
| 1421 | * |
| 1422 | * __mode - Control weak references. A string value with one or both |
| 1423 | * of the characters 'k' and 'v' which specifies that the the |
| 1424 | * keys and/or values in the table are weak references. |
| 1425 | * |
| 1426 | * __call - Treat a table like a function. When a table is followed by |
| 1427 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1428 | * a __call key pointing to a function, that function is invoked |
| 1429 | * (passing any specified arguments) and the return value is |
| 1430 | * returned. |
| 1431 | * |
| 1432 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1433 | * called, if the metatable for myTable has a __metatable |
| 1434 | * key, the value of that key is returned instead of the |
| 1435 | * actual metatable. |
| 1436 | * |
| 1437 | * __tostring - Control string representation. When the builtin |
| 1438 | * "tostring( myTable )" function is called, if the metatable |
| 1439 | * for myTable has a __tostring property set to a function, |
| 1440 | * that function is invoked (passing myTable to it) and the |
| 1441 | * return value is used as the string representation. |
| 1442 | * |
| 1443 | * __len - Control table length. When the table length is requested using |
| 1444 | * the length operator ( '#' ), if the metatable for myTable has |
| 1445 | * a __len key pointing to a function, that function is invoked |
| 1446 | * (passing myTable to it) and the return value used as the value |
| 1447 | * of "#myTable". |
| 1448 | * |
| 1449 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1450 | * collected, if the metatable has a __gc field pointing to a |
| 1451 | * function, that function is first invoked, passing the userdata |
| 1452 | * to it. The __gc metamethod is not called for tables. |
| 1453 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1454 | * |
| 1455 | * Special metamethods for redefining standard operators: |
| 1456 | * http://www.lua.org/pil/13.1.html |
| 1457 | * |
| 1458 | * __add "+" |
| 1459 | * __sub "-" |
| 1460 | * __mul "*" |
| 1461 | * __div "/" |
| 1462 | * __unm "!" |
| 1463 | * __pow "^" |
| 1464 | * __concat ".." |
| 1465 | * |
| 1466 | * Special methods for redfining standar relations |
| 1467 | * http://www.lua.org/pil/13.2.html |
| 1468 | * |
| 1469 | * __eq "==" |
| 1470 | * __lt "<" |
| 1471 | * __le "<=" |
| 1472 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1473 | |
| 1474 | /* |
| 1475 | * |
| 1476 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1477 | * Class Map |
| 1478 | * |
| 1479 | * |
| 1480 | */ |
| 1481 | |
| 1482 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1483 | * a class session, otherwise it throws an error. |
| 1484 | */ |
| 1485 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1486 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1487 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | /* This function is the map constructor. It don't need |
| 1491 | * the class Map object. It creates and return a new Map |
| 1492 | * object. It must be called only during "body" or "init" |
| 1493 | * context because it process some filesystem accesses. |
| 1494 | */ |
| 1495 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1496 | { |
| 1497 | const char *fn; |
| 1498 | int match = PAT_MATCH_STR; |
| 1499 | struct sample_conv conv; |
| 1500 | const char *file = ""; |
| 1501 | int line = 0; |
| 1502 | lua_Debug ar; |
| 1503 | char *err = NULL; |
| 1504 | struct arg args[2]; |
| 1505 | |
| 1506 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1507 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1508 | |
| 1509 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1510 | |
| 1511 | if (lua_gettop(L) >= 2) { |
| 1512 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1513 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1514 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1515 | } |
| 1516 | |
| 1517 | /* Get Lua filename and line number. */ |
| 1518 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1519 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1520 | if (ar.currentline > 0) { /* is there info? */ |
| 1521 | file = ar.short_src; |
| 1522 | line = ar.currentline; |
| 1523 | } |
| 1524 | } |
| 1525 | |
| 1526 | /* fill fake sample_conv struct. */ |
| 1527 | conv.kw = ""; /* unused. */ |
| 1528 | conv.process = NULL; /* unused. */ |
| 1529 | conv.arg_mask = 0; /* unused. */ |
| 1530 | conv.val_args = NULL; /* unused. */ |
| 1531 | conv.out_type = SMP_T_STR; |
| 1532 | conv.private = (void *)(long)match; |
| 1533 | switch (match) { |
| 1534 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1535 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1536 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1537 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1538 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1539 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1540 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1541 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1542 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1543 | default: |
| 1544 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1545 | } |
| 1546 | |
| 1547 | /* fill fake args. */ |
| 1548 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1549 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1550 | args[1].type = ARGT_STOP; |
| 1551 | |
| 1552 | /* load the map. */ |
| 1553 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1554 | /* error case: we cant use luaL_error because we must |
| 1555 | * free the err variable. |
| 1556 | */ |
| 1557 | luaL_where(L, 1); |
| 1558 | lua_pushfstring(L, "'new': %s.", err); |
| 1559 | lua_concat(L, 2); |
| 1560 | free(err); |
| 1561 | WILL_LJMP(lua_error(L)); |
| 1562 | } |
| 1563 | |
| 1564 | /* create the lua object. */ |
| 1565 | lua_newtable(L); |
| 1566 | lua_pushlightuserdata(L, args[0].data.map); |
| 1567 | lua_rawseti(L, -2, 0); |
| 1568 | |
| 1569 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1570 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1571 | lua_setmetatable(L, -2); |
| 1572 | |
| 1573 | |
| 1574 | return 1; |
| 1575 | } |
| 1576 | |
| 1577 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1578 | { |
| 1579 | struct map_descriptor *desc; |
| 1580 | struct pattern *pat; |
| 1581 | struct sample smp; |
| 1582 | |
| 1583 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1584 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1585 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1586 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1587 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1588 | } |
| 1589 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1590 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1591 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1592 | smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data)); |
Thierry Fournier | 8e55384 | 2020-11-10 20:38:20 +0100 | [diff] [blame] | 1593 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1594 | } |
| 1595 | |
| 1596 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1597 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1598 | if (str) |
| 1599 | lua_pushstring(L, ""); |
| 1600 | else |
| 1601 | lua_pushnil(L); |
| 1602 | return 1; |
| 1603 | } |
| 1604 | |
| 1605 | /* 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] | 1606 | 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] | 1607 | return 1; |
| 1608 | } |
| 1609 | |
| 1610 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1611 | { |
| 1612 | return _hlua_map_lookup(L, 0); |
| 1613 | } |
| 1614 | |
| 1615 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1616 | { |
| 1617 | return _hlua_map_lookup(L, 1); |
| 1618 | } |
| 1619 | |
| 1620 | /* |
| 1621 | * |
| 1622 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1623 | * Class Socket |
| 1624 | * |
| 1625 | * |
| 1626 | */ |
| 1627 | |
| 1628 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1629 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1630 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | /* 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] | 1634 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1635 | * received. |
| 1636 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1637 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1638 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1639 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1640 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1641 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1642 | if (appctx->ctx.hlua_cosocket.die) { |
| 1643 | si_shutw(si); |
| 1644 | si_shutr(si); |
| 1645 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1646 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1647 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1648 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1649 | } |
| 1650 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1651 | /* If the connection object is not available, close all the |
| 1652 | * streams and wakeup everything waiting for. |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1653 | */ |
| 1654 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1655 | si_shutw(si); |
| 1656 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1657 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1658 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1659 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1660 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1661 | } |
| 1662 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1663 | /* If we cant write, wakeup the pending write signals. */ |
| 1664 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1665 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1666 | |
| 1667 | /* If we cant read, wakeup the pending read signals. */ |
| 1668 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1669 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1670 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1671 | /* if the connection is not estabkished, inform the stream that we want |
| 1672 | * to be notified whenever the connection completes. |
| 1673 | */ |
| 1674 | if (!(c->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1675 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1676 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1677 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1678 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1679 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1680 | |
| 1681 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1682 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1683 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1684 | /* 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] | 1685 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1686 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1687 | |
| 1688 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1689 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1690 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1691 | |
| 1692 | /* Some data were injected in the buffer, notify the stream |
| 1693 | * interface. |
| 1694 | */ |
| 1695 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1696 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1697 | |
| 1698 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1699 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1700 | */ |
| 1701 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1702 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1703 | } |
| 1704 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1705 | /* This function is called when the "struct stream" is destroyed. |
| 1706 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1707 | * Wake all the pending signals. |
| 1708 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1709 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1710 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1711 | struct xref *peer; |
| 1712 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1713 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1714 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1715 | if (peer) |
| 1716 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1717 | |
| 1718 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1719 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1720 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1721 | } |
| 1722 | |
| 1723 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1724 | * uses this object. If the stream does not exists, just quit. |
| 1725 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1726 | * pending signal can rest in the read and write lists. destroy |
| 1727 | * it. |
| 1728 | */ |
| 1729 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1730 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1731 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1732 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1733 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1734 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1735 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1736 | |
| 1737 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1738 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1739 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1740 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1741 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1742 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1743 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1744 | appctx->ctx.hlua_cosocket.die = 1; |
| 1745 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1746 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1747 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1748 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1749 | return 0; |
| 1750 | } |
| 1751 | |
| 1752 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1753 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1754 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1755 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1756 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1757 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1758 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1759 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1760 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1761 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1762 | |
| 1763 | /* Check if we run on the same thread than the xreator thread. |
| 1764 | * We cannot access to the socket if the thread is different. |
| 1765 | */ |
| 1766 | if (socket->tid != tid) |
| 1767 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1768 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1769 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1770 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1771 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1772 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1773 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1774 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1775 | appctx->ctx.hlua_cosocket.die = 1; |
| 1776 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1777 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1778 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1779 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1780 | return 0; |
| 1781 | } |
| 1782 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1783 | /* The close function calls close_helper. |
| 1784 | */ |
| 1785 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1786 | { |
| 1787 | MAY_LJMP(check_args(L, 1, "close")); |
| 1788 | return hlua_socket_close_helper(L); |
| 1789 | } |
| 1790 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1791 | /* This Lua function assumes that the stack contain three parameters. |
| 1792 | * 1 - USERDATA containing a struct socket |
| 1793 | * 2 - INTEGER with values of the macro defined below |
| 1794 | * If the integer is -1, we must read at most one line. |
| 1795 | * If the integer is -2, we ust read all the data until the |
| 1796 | * end of the stream. |
| 1797 | * If the integer is positive value, we must read a number of |
| 1798 | * bytes corresponding to this value. |
| 1799 | */ |
| 1800 | #define HLSR_READ_LINE (-1) |
| 1801 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1802 | __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] | 1803 | { |
| 1804 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1805 | int wanted = lua_tointeger(L, 2); |
| 1806 | struct hlua *hlua = hlua_gethlua(L); |
| 1807 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1808 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1809 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1810 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1811 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1812 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1813 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1814 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1815 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1816 | struct stream_interface *si; |
| 1817 | struct stream *s; |
| 1818 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1819 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1820 | |
| 1821 | /* Check if this lua stack is schedulable. */ |
| 1822 | if (!hlua || !hlua->task) |
| 1823 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1824 | "'frontend', 'backend' or 'task'")); |
| 1825 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1826 | /* Check if we run on the same thread than the xreator thread. |
| 1827 | * We cannot access to the socket if the thread is different. |
| 1828 | */ |
| 1829 | if (socket->tid != tid) |
| 1830 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1831 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1832 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1833 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1834 | if (!peer) |
| 1835 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1836 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1837 | si = appctx->owner; |
| 1838 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1839 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1840 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1841 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1842 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1843 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1844 | if (nblk < 0) /* Connection close. */ |
| 1845 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1846 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1847 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1848 | |
| 1849 | /* remove final \r\n. */ |
| 1850 | if (nblk == 1) { |
| 1851 | if (blk1[len1-1] == '\n') { |
| 1852 | len1--; |
| 1853 | skip_at_end++; |
| 1854 | if (blk1[len1-1] == '\r') { |
| 1855 | len1--; |
| 1856 | skip_at_end++; |
| 1857 | } |
| 1858 | } |
| 1859 | } |
| 1860 | else { |
| 1861 | if (blk2[len2-1] == '\n') { |
| 1862 | len2--; |
| 1863 | skip_at_end++; |
| 1864 | if (blk2[len2-1] == '\r') { |
| 1865 | len2--; |
| 1866 | skip_at_end++; |
| 1867 | } |
| 1868 | } |
| 1869 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1873 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1874 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1875 | if (nblk < 0) /* Connection close. */ |
| 1876 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1877 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1878 | goto connection_empty; |
| 1879 | } |
| 1880 | |
| 1881 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1882 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1883 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1884 | if (nblk < 0) /* Connection close. */ |
| 1885 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1886 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1887 | goto connection_empty; |
| 1888 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1889 | missing_bytes = wanted - socket->b.n; |
| 1890 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1891 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1892 | len1 = missing_bytes; |
| 1893 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1894 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | len = len1; |
| 1898 | |
| 1899 | luaL_addlstring(&socket->b, blk1, len1); |
| 1900 | if (nblk == 2) { |
| 1901 | len += len2; |
| 1902 | luaL_addlstring(&socket->b, blk2, len2); |
| 1903 | } |
| 1904 | |
| 1905 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1906 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1907 | |
| 1908 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1909 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1910 | |
| 1911 | /* If the pattern reclaim to read all the data |
| 1912 | * in the connection, got out. |
| 1913 | */ |
| 1914 | if (wanted == HLSR_READ_ALL) |
| 1915 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1916 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1917 | goto connection_empty; |
| 1918 | |
| 1919 | /* Return result. */ |
| 1920 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1921 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1922 | return 1; |
| 1923 | |
| 1924 | connection_closed: |
| 1925 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1926 | xref_unlock(&socket->xref, peer); |
| 1927 | |
| 1928 | no_peer: |
| 1929 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1930 | /* If the buffer containds data. */ |
| 1931 | if (socket->b.n > 0) { |
| 1932 | luaL_pushresult(&socket->b); |
| 1933 | return 1; |
| 1934 | } |
| 1935 | lua_pushnil(L); |
| 1936 | lua_pushstring(L, "connection closed."); |
| 1937 | return 2; |
| 1938 | |
| 1939 | connection_empty: |
| 1940 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1941 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1942 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1943 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1944 | } |
| 1945 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1946 | 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] | 1947 | return 0; |
| 1948 | } |
| 1949 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1950 | /* This Lua function gets two parameters. The first one can be string |
| 1951 | * or a number. If the string is "*l", the user requires one line. If |
| 1952 | * 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] | 1953 | * If the value is a number, the user require a number of bytes equal |
| 1954 | * to the value. The default value is "*l" (a line). |
| 1955 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1956 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1957 | * integer takes this values: |
| 1958 | * -1 : read a line |
| 1959 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1960 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1961 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1962 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1963 | * concatenated with the read data. |
| 1964 | */ |
| 1965 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1966 | { |
| 1967 | int wanted = HLSR_READ_LINE; |
| 1968 | const char *pattern; |
| 1969 | int type; |
| 1970 | char *error; |
| 1971 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1972 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | |
| 1974 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1975 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1976 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1977 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1978 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1979 | /* Check if we run on the same thread than the xreator thread. |
| 1980 | * We cannot access to the socket if the thread is different. |
| 1981 | */ |
| 1982 | if (socket->tid != tid) |
| 1983 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1984 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1985 | /* check for pattern. */ |
| 1986 | if (lua_gettop(L) >= 2) { |
| 1987 | type = lua_type(L, 2); |
| 1988 | if (type == LUA_TSTRING) { |
| 1989 | pattern = lua_tostring(L, 2); |
| 1990 | if (strcmp(pattern, "*a") == 0) |
| 1991 | wanted = HLSR_READ_ALL; |
| 1992 | else if (strcmp(pattern, "*l") == 0) |
| 1993 | wanted = HLSR_READ_LINE; |
| 1994 | else { |
| 1995 | wanted = strtoll(pattern, &error, 10); |
| 1996 | if (*error != '\0') |
| 1997 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1998 | } |
| 1999 | } |
| 2000 | else if (type == LUA_TNUMBER) { |
| 2001 | wanted = lua_tointeger(L, 2); |
| 2002 | if (wanted < 0) |
| 2003 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2004 | } |
| 2005 | } |
| 2006 | |
| 2007 | /* Set pattern. */ |
| 2008 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2009 | |
| 2010 | /* Check if we would replace the top by itself. */ |
| 2011 | if (lua_gettop(L) != 2) |
| 2012 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2013 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2014 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | luaL_buffinit(L, &socket->b); |
| 2016 | |
| 2017 | /* Check prefix. */ |
| 2018 | if (lua_gettop(L) >= 3) { |
| 2019 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2020 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2021 | pattern = lua_tolstring(L, 3, &len); |
| 2022 | luaL_addlstring(&socket->b, pattern, len); |
| 2023 | } |
| 2024 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2025 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2026 | } |
| 2027 | |
| 2028 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2029 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2030 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2031 | 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] | 2032 | { |
| 2033 | struct hlua_socket *socket; |
| 2034 | struct hlua *hlua = hlua_gethlua(L); |
| 2035 | struct appctx *appctx; |
| 2036 | size_t buf_len; |
| 2037 | const char *buf; |
| 2038 | int len; |
| 2039 | int send_len; |
| 2040 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2041 | struct xref *peer; |
| 2042 | struct stream_interface *si; |
| 2043 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2044 | |
| 2045 | /* Check if this lua stack is schedulable. */ |
| 2046 | if (!hlua || !hlua->task) |
| 2047 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2048 | "'frontend', 'backend' or 'task'")); |
| 2049 | |
| 2050 | /* Get object */ |
| 2051 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2052 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2053 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2054 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2055 | /* Check if we run on the same thread than the xreator thread. |
| 2056 | * We cannot access to the socket if the thread is different. |
| 2057 | */ |
| 2058 | if (socket->tid != tid) |
| 2059 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2060 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2061 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2062 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2063 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2064 | lua_pushinteger(L, -1); |
| 2065 | return 1; |
| 2066 | } |
| 2067 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2068 | si = appctx->owner; |
| 2069 | s = si_strm(si); |
| 2070 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2071 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2072 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2073 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2074 | lua_pushinteger(L, -1); |
| 2075 | return 1; |
| 2076 | } |
| 2077 | |
| 2078 | /* Update the input buffer data. */ |
| 2079 | buf += sent; |
| 2080 | send_len = buf_len - sent; |
| 2081 | |
| 2082 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2083 | if (sent >= buf_len) { |
| 2084 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2085 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2086 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2087 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2088 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2089 | * the request buffer if its not required. |
| 2090 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2091 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2092 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2093 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2094 | } |
| 2095 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2096 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2097 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2098 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2099 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2100 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2101 | |
| 2102 | /* send data */ |
| 2103 | if (len < send_len) |
| 2104 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2105 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2106 | |
| 2107 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2108 | * the data" (-2) are not expected because the available length |
| 2109 | * is tested. |
| 2110 | * Other unknown error are also not expected. |
| 2111 | */ |
| 2112 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2113 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2114 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2115 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2116 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2117 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2118 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2119 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2120 | return 1; |
| 2121 | } |
| 2122 | |
| 2123 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2124 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2125 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2126 | s->req.rex = TICK_ETERNITY; |
| 2127 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2128 | |
| 2129 | /* Update length sent. */ |
| 2130 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2131 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2132 | |
| 2133 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2134 | if (sent + len >= buf_len) { |
| 2135 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2136 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2137 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2138 | |
| 2139 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2140 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2141 | xref_unlock(&socket->xref, peer); |
| 2142 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2143 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2144 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2145 | 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] | 2146 | return 0; |
| 2147 | } |
| 2148 | |
| 2149 | /* This function initiate the send of data. It just check the input |
| 2150 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2151 | * 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] | 2152 | * "hlua_socket_write_yield" that can yield. |
| 2153 | * |
| 2154 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2155 | * the associated object. The second is a string buffer. The third is |
| 2156 | * a facultative integer that represents where is the buffer position |
| 2157 | * of the start of the data that can send. The first byte is the |
| 2158 | * position "1". The default value is "1". The fourth argument is a |
| 2159 | * facultative integer that represents where is the buffer position |
| 2160 | * of the end of the data that can send. The default is the last byte. |
| 2161 | */ |
| 2162 | static int hlua_socket_send(struct lua_State *L) |
| 2163 | { |
| 2164 | int i; |
| 2165 | int j; |
| 2166 | const char *buf; |
| 2167 | size_t buf_len; |
| 2168 | |
| 2169 | /* Check number of arguments. */ |
| 2170 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2171 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2172 | |
| 2173 | /* Get the string. */ |
| 2174 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2175 | |
| 2176 | /* Get and check j. */ |
| 2177 | if (lua_gettop(L) == 4) { |
| 2178 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2179 | if (j < 0) |
| 2180 | j = buf_len + j + 1; |
| 2181 | if (j > buf_len) |
| 2182 | j = buf_len + 1; |
| 2183 | lua_pop(L, 1); |
| 2184 | } |
| 2185 | else |
| 2186 | j = buf_len; |
| 2187 | |
| 2188 | /* Get and check i. */ |
| 2189 | if (lua_gettop(L) == 3) { |
| 2190 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2191 | if (i < 0) |
| 2192 | i = buf_len + i + 1; |
| 2193 | if (i > buf_len) |
| 2194 | i = buf_len + 1; |
| 2195 | lua_pop(L, 1); |
| 2196 | } else |
| 2197 | i = 1; |
| 2198 | |
| 2199 | /* Check bth i and j. */ |
| 2200 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2201 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2202 | return 1; |
| 2203 | } |
| 2204 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2205 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2206 | return 1; |
| 2207 | } |
| 2208 | if (i == 0) |
| 2209 | i = 1; |
| 2210 | if (j == 0) |
| 2211 | j = 1; |
| 2212 | |
| 2213 | /* Pop the string. */ |
| 2214 | lua_pop(L, 1); |
| 2215 | |
| 2216 | /* Update the buffer length. */ |
| 2217 | buf += i - 1; |
| 2218 | buf_len = j - i + 1; |
| 2219 | lua_pushlstring(L, buf, buf_len); |
| 2220 | |
| 2221 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2222 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2223 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2224 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2225 | } |
| 2226 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2227 | #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] | 2228 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2229 | { |
| 2230 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2231 | int ret; |
| 2232 | int len; |
| 2233 | char *p; |
| 2234 | |
| 2235 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2236 | if (ret <= 0) { |
| 2237 | lua_pushnil(L); |
| 2238 | return 1; |
| 2239 | } |
| 2240 | |
| 2241 | if (ret == AF_UNIX) { |
| 2242 | lua_pushstring(L, buffer+1); |
| 2243 | return 1; |
| 2244 | } |
| 2245 | else if (ret == AF_INET6) { |
| 2246 | buffer[0] = '['; |
| 2247 | len = strlen(buffer); |
| 2248 | buffer[len] = ']'; |
| 2249 | len++; |
| 2250 | buffer[len] = ':'; |
| 2251 | len++; |
| 2252 | p = buffer; |
| 2253 | } |
| 2254 | else if (ret == AF_INET) { |
| 2255 | p = buffer + 1; |
| 2256 | len = strlen(p); |
| 2257 | p[len] = ':'; |
| 2258 | len++; |
| 2259 | } |
| 2260 | else { |
| 2261 | lua_pushnil(L); |
| 2262 | return 1; |
| 2263 | } |
| 2264 | |
| 2265 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2266 | lua_pushnil(L); |
| 2267 | return 1; |
| 2268 | } |
| 2269 | |
| 2270 | lua_pushstring(L, p); |
| 2271 | return 1; |
| 2272 | } |
| 2273 | |
| 2274 | /* Returns information about the peer of the connection. */ |
| 2275 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2276 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2277 | struct hlua_socket *socket; |
| 2278 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2279 | struct xref *peer; |
| 2280 | struct appctx *appctx; |
| 2281 | struct stream_interface *si; |
| 2282 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2283 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2284 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2285 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2286 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2287 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2289 | /* Check if we run on the same thread than the xreator thread. |
| 2290 | * We cannot access to the socket if the thread is different. |
| 2291 | */ |
| 2292 | if (socket->tid != tid) |
| 2293 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2294 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2295 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2296 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2297 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2298 | lua_pushnil(L); |
| 2299 | return 1; |
| 2300 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2301 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2302 | si = appctx->owner; |
| 2303 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2304 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2305 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2307 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2308 | lua_pushnil(L); |
| 2309 | return 1; |
| 2310 | } |
| 2311 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2312 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2313 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2314 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2315 | lua_pushnil(L); |
| 2316 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2317 | } |
| 2318 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2319 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2320 | xref_unlock(&socket->xref, peer); |
| 2321 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | /* Returns information about my connection side. */ |
| 2325 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2326 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2327 | struct hlua_socket *socket; |
| 2328 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2329 | struct appctx *appctx; |
| 2330 | struct xref *peer; |
| 2331 | struct stream_interface *si; |
| 2332 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2333 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2334 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2335 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2336 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2337 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2338 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2339 | /* Check if we run on the same thread than the xreator thread. |
| 2340 | * We cannot access to the socket if the thread is different. |
| 2341 | */ |
| 2342 | if (socket->tid != tid) |
| 2343 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2344 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2345 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2346 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2347 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2348 | lua_pushnil(L); |
| 2349 | return 1; |
| 2350 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2351 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2352 | si = appctx->owner; |
| 2353 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2354 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2355 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2356 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2357 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2358 | lua_pushnil(L); |
| 2359 | return 1; |
| 2360 | } |
| 2361 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2362 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2363 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2364 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2365 | lua_pushnil(L); |
| 2366 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2367 | } |
| 2368 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2369 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2370 | xref_unlock(&socket->xref, peer); |
| 2371 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2375 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2376 | .obj_type = OBJ_TYPE_APPLET, |
| 2377 | .name = "<LUA_TCP>", |
| 2378 | .fct = hlua_socket_handler, |
| 2379 | .release = hlua_socket_release, |
| 2380 | }; |
| 2381 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2382 | __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] | 2383 | { |
| 2384 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2385 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2386 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2387 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2388 | struct stream_interface *si; |
| 2389 | struct stream *s; |
| 2390 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2391 | /* Check if we run on the same thread than the xreator thread. |
| 2392 | * We cannot access to the socket if the thread is different. |
| 2393 | */ |
| 2394 | if (socket->tid != tid) |
| 2395 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2396 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2397 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2398 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2399 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2400 | lua_pushnil(L); |
| 2401 | lua_pushstring(L, "Can't connect"); |
| 2402 | return 2; |
| 2403 | } |
| 2404 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2405 | si = appctx->owner; |
| 2406 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2407 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2408 | /* Check if we run on the same thread than the xreator thread. |
| 2409 | * We cannot access to the socket if the thread is different. |
| 2410 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2411 | if (socket->tid != tid) { |
| 2412 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2413 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2414 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2415 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2416 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2417 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2418 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2419 | lua_pushnil(L); |
| 2420 | lua_pushstring(L, "Can't connect"); |
| 2421 | return 2; |
| 2422 | } |
| 2423 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2424 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2425 | |
| 2426 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2427 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2428 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2429 | lua_pushinteger(L, 1); |
| 2430 | return 1; |
| 2431 | } |
| 2432 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2433 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2434 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2435 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2436 | } |
| 2437 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2438 | 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] | 2439 | return 0; |
| 2440 | } |
| 2441 | |
| 2442 | /* This function fail or initite the connection. */ |
| 2443 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2444 | { |
| 2445 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2446 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2447 | const char *ip; |
| 2448 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2449 | struct hlua *hlua; |
| 2450 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2451 | int low, high; |
| 2452 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2453 | struct xref *peer; |
| 2454 | struct stream_interface *si; |
| 2455 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2456 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2457 | if (lua_gettop(L) < 2) |
| 2458 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2459 | |
| 2460 | /* Get args. */ |
| 2461 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2462 | |
| 2463 | /* Check if we run on the same thread than the xreator thread. |
| 2464 | * We cannot access to the socket if the thread is different. |
| 2465 | */ |
| 2466 | if (socket->tid != tid) |
| 2467 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2468 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2469 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2470 | if (lua_gettop(L) >= 3) { |
| 2471 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2472 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2473 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2474 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2475 | * that are not enclosed within square brackets. |
| 2476 | */ |
| 2477 | if (port > 0) { |
| 2478 | luaL_buffinit(L, &b); |
| 2479 | luaL_addstring(&b, ip); |
| 2480 | luaL_addchar(&b, ':'); |
| 2481 | luaL_pushresult(&b); |
| 2482 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2483 | } |
| 2484 | } |
| 2485 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2486 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2487 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2488 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2489 | lua_pushnil(L); |
| 2490 | return 1; |
| 2491 | } |
| 2492 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2493 | si = appctx->owner; |
| 2494 | s = si_strm(si); |
| 2495 | |
| 2496 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2497 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2498 | if (!conn) { |
| 2499 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2500 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2501 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2502 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2503 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2504 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2505 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2506 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2507 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2508 | if (!addr) { |
| 2509 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2510 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2511 | } |
| 2512 | if (low != high) { |
| 2513 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2514 | 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] | 2515 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2516 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2517 | |
| 2518 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2519 | if (low == 0) { |
| 2520 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2521 | if (port == -1) { |
| 2522 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2523 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2524 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2525 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2526 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2527 | if (port == -1) { |
| 2528 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2529 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2530 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2531 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2532 | } |
| 2533 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2534 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2535 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2536 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2537 | |
| 2538 | /* inform the stream that we want to be notified whenever the |
| 2539 | * connection completes. |
| 2540 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2541 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2542 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2543 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2544 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2545 | hlua->flags |= HLUA_MUST_GC; |
| 2546 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2547 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2548 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2549 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2550 | } |
| 2551 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2552 | |
| 2553 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2554 | /* Return yield waiting for connection. */ |
| 2555 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2556 | 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] | 2557 | |
| 2558 | return 0; |
| 2559 | } |
| 2560 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2561 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2562 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2563 | { |
| 2564 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2565 | struct xref *peer; |
| 2566 | struct appctx *appctx; |
| 2567 | struct stream_interface *si; |
| 2568 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2569 | |
| 2570 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2571 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2572 | |
| 2573 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2574 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2575 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2576 | lua_pushnil(L); |
| 2577 | return 1; |
| 2578 | } |
| 2579 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2580 | si = appctx->owner; |
| 2581 | s = si_strm(si); |
| 2582 | |
| 2583 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2584 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2585 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2586 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2587 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2588 | |
| 2589 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2590 | { |
| 2591 | return 0; |
| 2592 | } |
| 2593 | |
| 2594 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2595 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2596 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2597 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2598 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2599 | struct xref *peer; |
| 2600 | struct appctx *appctx; |
| 2601 | struct stream_interface *si; |
| 2602 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2603 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2604 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2605 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2606 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2607 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2608 | /* convert the timeout to millis */ |
| 2609 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2610 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2611 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2612 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2613 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2614 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2615 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2616 | 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] | 2617 | |
| 2618 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2619 | if (tmout == 0) |
| 2620 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2621 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2622 | /* Check if we run on the same thread than the xreator thread. |
| 2623 | * We cannot access to the socket if the thread is different. |
| 2624 | */ |
| 2625 | if (socket->tid != tid) |
| 2626 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2627 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2628 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2629 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2630 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2631 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2632 | WILL_LJMP(lua_error(L)); |
| 2633 | return 0; |
| 2634 | } |
| 2635 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2636 | si = appctx->owner; |
| 2637 | s = si_strm(si); |
| 2638 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2639 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2640 | s->req.rto = tmout; |
| 2641 | s->req.wto = tmout; |
| 2642 | s->res.rto = tmout; |
| 2643 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2644 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2645 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2646 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2647 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2648 | |
| 2649 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2650 | task_queue(s->task); |
| 2651 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2652 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2653 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2654 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2655 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2659 | { |
| 2660 | struct hlua_socket *socket; |
| 2661 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2662 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2663 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2664 | |
| 2665 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2666 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2667 | hlua_pusherror(L, "socket: full stack"); |
| 2668 | goto out_fail_conf; |
| 2669 | } |
| 2670 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2671 | /* Create the object: obj[0] = userdata. */ |
| 2672 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2673 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2674 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2675 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2676 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2677 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2678 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2679 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2680 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2681 | goto out_fail_conf; |
| 2682 | } |
| 2683 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2684 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2685 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2686 | lua_setmetatable(L, -2); |
| 2687 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2688 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2689 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2690 | if (!appctx) { |
| 2691 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2692 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2693 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2694 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2695 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2696 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2697 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2698 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2699 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2700 | /* Now create a session, task and stream for this applet */ |
| 2701 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2702 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2703 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2704 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2705 | } |
| 2706 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2707 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2708 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2709 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2710 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2711 | } |
| 2712 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2713 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2714 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2715 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2716 | /* Configure "right" stream interface. this "si" is used to connect |
| 2717 | * and retrieve data from the server. The connection is initialized |
| 2718 | * with the "struct server". |
| 2719 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2720 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2721 | |
| 2722 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2723 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2724 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2725 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2726 | return 1; |
| 2727 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2728 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2729 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2730 | out_fail_sess: |
| 2731 | appctx_free(appctx); |
| 2732 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2733 | WILL_LJMP(lua_error(L)); |
| 2734 | return 0; |
| 2735 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2736 | |
| 2737 | /* |
| 2738 | * |
| 2739 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2740 | * Class Channel |
| 2741 | * |
| 2742 | * |
| 2743 | */ |
| 2744 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2745 | /* This function is called before the Lua execution. It stores |
| 2746 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2747 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2748 | 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] | 2749 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2750 | c->mode = stream->be->mode; |
| 2751 | switch (c->mode) { |
| 2752 | case PR_MODE_HTTP: |
| 2753 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2754 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2755 | c->data.http.state = stream->txn->req.msg_state; |
| 2756 | else |
| 2757 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2758 | break; |
| 2759 | default: |
| 2760 | break; |
| 2761 | } |
| 2762 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2763 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2764 | /* This function is called after the Lua execution. it |
| 2765 | * returns true if the parser state is consistent, otherwise, |
| 2766 | * it return false. |
| 2767 | * |
| 2768 | * In HTTP mode, the parser state must be in the same state |
| 2769 | * or greater when we exit the function. Even if we do a |
| 2770 | * control yield. This prevent to break the HTTP message |
| 2771 | * from the Lua code. |
| 2772 | */ |
| 2773 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2774 | { |
| 2775 | if (c->mode != stream->be->mode) |
| 2776 | return 0; |
| 2777 | |
| 2778 | switch (c->mode) { |
| 2779 | case PR_MODE_HTTP: |
| 2780 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2781 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2782 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2783 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2784 | else |
| 2785 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2786 | default: |
| 2787 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2788 | } |
| 2789 | return 1; |
| 2790 | } |
| 2791 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2792 | /* Returns the struct hlua_channel join to the class channel in the |
| 2793 | * stack entry "ud" or throws an argument error. |
| 2794 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2795 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2796 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2797 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2798 | } |
| 2799 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2800 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2801 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2802 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2803 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2804 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2805 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2806 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2807 | return 0; |
| 2808 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2809 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2810 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2811 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2812 | |
| 2813 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2814 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2815 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2816 | return 1; |
| 2817 | } |
| 2818 | |
| 2819 | /* Duplicate all the data present in the input channel and put it |
| 2820 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2821 | * the stack if the channel is closed and all the data are consumed, |
| 2822 | * returns 0 if no data are available, otherwise it returns the length |
| 2823 | * of the builded string. |
| 2824 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2825 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2826 | { |
| 2827 | char *blk1; |
| 2828 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2829 | size_t len1; |
| 2830 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2831 | int ret; |
| 2832 | luaL_Buffer b; |
| 2833 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2834 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2835 | if (unlikely(ret == 0)) |
| 2836 | return 0; |
| 2837 | |
| 2838 | if (unlikely(ret < 0)) { |
| 2839 | lua_pushnil(L); |
| 2840 | return -1; |
| 2841 | } |
| 2842 | |
| 2843 | luaL_buffinit(L, &b); |
| 2844 | luaL_addlstring(&b, blk1, len1); |
| 2845 | if (unlikely(ret == 2)) |
| 2846 | luaL_addlstring(&b, blk2, len2); |
| 2847 | luaL_pushresult(&b); |
| 2848 | |
| 2849 | if (unlikely(ret == 2)) |
| 2850 | return len1 + len2; |
| 2851 | return len1; |
| 2852 | } |
| 2853 | |
| 2854 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2855 | * a yield. This function keep the data in the buffer. |
| 2856 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2857 | __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] | 2858 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2859 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2860 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2861 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2862 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2863 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2864 | WILL_LJMP(lua_error(L)); |
| 2865 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2866 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2867 | 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] | 2868 | return 1; |
| 2869 | } |
| 2870 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2871 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2872 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2873 | { |
| 2874 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2875 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2876 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2877 | } |
| 2878 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2879 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2880 | * a yield. This function consumes the data in the buffer. It returns |
| 2881 | * a string containing the data or a nil pointer if no data are available |
| 2882 | * and the channel is closed. |
| 2883 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2884 | __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] | 2885 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2886 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2887 | int ret; |
| 2888 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2889 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2890 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2891 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2892 | WILL_LJMP(lua_error(L)); |
| 2893 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2894 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2895 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2896 | 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] | 2897 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2898 | if (unlikely(ret == -1)) |
| 2899 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2900 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2901 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2902 | return 1; |
| 2903 | } |
| 2904 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2905 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2906 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2907 | { |
| 2908 | MAY_LJMP(check_args(L, 1, "get")); |
| 2909 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2910 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2911 | } |
| 2912 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2913 | /* This functions consumes and returns one line. If the channel is closed, |
| 2914 | * 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] | 2915 | * 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] | 2916 | * value. |
| 2917 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2918 | __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] | 2919 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2920 | char *blk1; |
| 2921 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2922 | size_t len1; |
| 2923 | size_t len2; |
| 2924 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2925 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2926 | int ret; |
| 2927 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2928 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2929 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2930 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2931 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2932 | WILL_LJMP(lua_error(L)); |
| 2933 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2934 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2935 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2936 | 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] | 2937 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2938 | if (ret == -1) { |
| 2939 | lua_pushnil(L); |
| 2940 | return 1; |
| 2941 | } |
| 2942 | |
| 2943 | luaL_buffinit(L, &b); |
| 2944 | luaL_addlstring(&b, blk1, len1); |
| 2945 | len = len1; |
| 2946 | if (unlikely(ret == 2)) { |
| 2947 | luaL_addlstring(&b, blk2, len2); |
| 2948 | len += len2; |
| 2949 | } |
| 2950 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2951 | 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] | 2952 | return 1; |
| 2953 | } |
| 2954 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2955 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2956 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2957 | { |
| 2958 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2959 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2960 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2961 | } |
| 2962 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2963 | /* This function takes a string as input, and append it at the |
| 2964 | * input side of channel. If the data is too big, but a space |
| 2965 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2966 | * yields. If the data is bigger than the buffer, or if the |
| 2967 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2968 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2969 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2970 | __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] | 2971 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2972 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2973 | size_t len; |
| 2974 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2975 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2976 | int ret; |
| 2977 | int max; |
| 2978 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2979 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2980 | WILL_LJMP(lua_error(L)); |
| 2981 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2982 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2983 | * the request buffer if its not required. |
| 2984 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2985 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2986 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2987 | 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] | 2988 | } |
| 2989 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2990 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2991 | if (max > len - l) |
| 2992 | max = len - l; |
| 2993 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2994 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2995 | if (ret == -2 || ret == -3) { |
| 2996 | lua_pushinteger(L, -1); |
| 2997 | return 1; |
| 2998 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2999 | if (ret == -1) { |
| 3000 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3001 | 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] | 3002 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3003 | l += ret; |
| 3004 | lua_pop(L, 1); |
| 3005 | lua_pushinteger(L, l); |
| 3006 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3007 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3008 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3009 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3010 | * in this case, we cannot add more data, so we cannot yield, |
| 3011 | * we return the amount of copyied data. |
| 3012 | */ |
| 3013 | return 1; |
| 3014 | } |
| 3015 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3016 | 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] | 3017 | return 1; |
| 3018 | } |
| 3019 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3020 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3021 | * 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] | 3022 | * buffer size is too little for the data. |
| 3023 | */ |
| 3024 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3025 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3026 | size_t len; |
| 3027 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3028 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3029 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3030 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3031 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3032 | lua_pushinteger(L, 0); |
| 3033 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3034 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3035 | } |
| 3036 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3037 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3038 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3039 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3040 | * or -1 if the channel is closed or if the buffer size is too |
| 3041 | * little for the data. |
| 3042 | */ |
| 3043 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3044 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3045 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3046 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3047 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3048 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3049 | lua_pushinteger(L, 0); |
| 3050 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3051 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3052 | WILL_LJMP(lua_error(L)); |
| 3053 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3054 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3055 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3056 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3057 | } |
| 3058 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3059 | /* Append data in the output side of the buffer. This data is immediately |
| 3060 | * sent. The function returns the amount of data written. If the buffer |
| 3061 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3062 | * if the channel is closed. |
| 3063 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3064 | __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] | 3065 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3066 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3067 | size_t len; |
| 3068 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3069 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3070 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3071 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3072 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3073 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3074 | WILL_LJMP(lua_error(L)); |
| 3075 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3076 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3077 | lua_pushinteger(L, -1); |
| 3078 | return 1; |
| 3079 | } |
| 3080 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3081 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3082 | * the request buffer if its not required. |
| 3083 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3084 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3085 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3086 | 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] | 3087 | } |
| 3088 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3089 | /* The written data will be immediately sent, so we can check |
| 3090 | * the available space without taking in account the reserve. |
| 3091 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3092 | * data, because the buffer will be flushed. |
| 3093 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3094 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3095 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3096 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3097 | * in this case, we cannot add more data, so we cannot yield, |
| 3098 | * we return the amount of copyied data. |
| 3099 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3100 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3101 | return 1; |
| 3102 | |
| 3103 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3104 | if (max > len - l) |
| 3105 | max = len - l; |
| 3106 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3107 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3108 | * detects a non contiguous buffer and realign it. |
| 3109 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3110 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3111 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3112 | |
| 3113 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3114 | 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] | 3115 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3116 | /* buffer replace considers that the input part is filled. |
| 3117 | * so, I must forward these new data in the output part. |
| 3118 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3119 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3120 | |
| 3121 | l += max; |
| 3122 | lua_pop(L, 1); |
| 3123 | lua_pushinteger(L, l); |
| 3124 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3125 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3126 | * in this case, we cannot add more data, so we cannot yield, |
| 3127 | * we return the amount of copyied data. |
| 3128 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3129 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3130 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3131 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3132 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3133 | if (l < len) { |
| 3134 | /* If we are waiting for space in the response buffer, we |
| 3135 | * must set the flag WAKERESWR. This flag required the task |
| 3136 | * wake up if any activity is detected on the response buffer. |
| 3137 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3138 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3139 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3140 | else |
| 3141 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3142 | 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] | 3143 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3144 | |
| 3145 | return 1; |
| 3146 | } |
| 3147 | |
| 3148 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3149 | * yield the LUA process, and resume it without checking the |
| 3150 | * input arguments. |
| 3151 | */ |
| 3152 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3153 | { |
| 3154 | MAY_LJMP(check_args(L, 2, "send")); |
| 3155 | lua_pushinteger(L, 0); |
| 3156 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3157 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3158 | } |
| 3159 | |
| 3160 | /* This function forward and amount of butes. The data pass from |
| 3161 | * the input side of the buffer to the output side, and can be |
| 3162 | * forwarded. This function never fails. |
| 3163 | * |
| 3164 | * The Lua function takes an amount of bytes to be forwarded in |
| 3165 | * imput. It returns the number of bytes forwarded. |
| 3166 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3167 | __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] | 3168 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3169 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3170 | int len; |
| 3171 | int l; |
| 3172 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3173 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3174 | |
| 3175 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3176 | |
| 3177 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3178 | WILL_LJMP(lua_error(L)); |
| 3179 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3180 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3181 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3182 | |
| 3183 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3184 | if (max > ci_data(chn)) |
| 3185 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3186 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3187 | l += max; |
| 3188 | |
| 3189 | lua_pop(L, 1); |
| 3190 | lua_pushinteger(L, l); |
| 3191 | |
| 3192 | /* Check if it miss bytes to forward. */ |
| 3193 | if (l < len) { |
| 3194 | /* The the input channel or the output channel are closed, we |
| 3195 | * must return the amount of data forwarded. |
| 3196 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3197 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3198 | return 1; |
| 3199 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3200 | /* If we are waiting for space data in the response buffer, we |
| 3201 | * must set the flag WAKERESWR. This flag required the task |
| 3202 | * wake up if any activity is detected on the response buffer. |
| 3203 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3204 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3205 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3206 | else |
| 3207 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3208 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3209 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3210 | 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] | 3211 | } |
| 3212 | |
| 3213 | return 1; |
| 3214 | } |
| 3215 | |
| 3216 | /* Just check the input and prepare the stack for the previous |
| 3217 | * function "hlua_channel_forward_yield" |
| 3218 | */ |
| 3219 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3220 | { |
| 3221 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3222 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3223 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3224 | |
| 3225 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3226 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3227 | } |
| 3228 | |
| 3229 | /* Just returns the number of bytes available in the input |
| 3230 | * side of the buffer. This function never fails. |
| 3231 | */ |
| 3232 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3233 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3234 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3235 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3236 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3237 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3238 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3239 | struct htx *htx = htxbuf(&chn->buf); |
| 3240 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3241 | } |
| 3242 | else |
| 3243 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3244 | return 1; |
| 3245 | } |
| 3246 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3247 | /* Returns true if the channel is full. */ |
| 3248 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3249 | { |
| 3250 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3251 | |
| 3252 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3253 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0136ebb | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3254 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3255 | * applet). |
| 3256 | */ |
| 3257 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3258 | return 1; |
| 3259 | } |
| 3260 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3261 | /* Just returns the number of bytes available in the output |
| 3262 | * side of the buffer. This function never fails. |
| 3263 | */ |
| 3264 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3265 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3266 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3267 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3268 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3269 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3270 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3271 | return 1; |
| 3272 | } |
| 3273 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3274 | /* |
| 3275 | * |
| 3276 | * |
| 3277 | * Class Fetches |
| 3278 | * |
| 3279 | * |
| 3280 | */ |
| 3281 | |
| 3282 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3283 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3284 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3285 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3286 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3287 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3288 | } |
| 3289 | |
| 3290 | /* This function creates and push in the stack a fetch object according |
| 3291 | * with a current TXN. |
| 3292 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3293 | 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] | 3294 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3295 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3296 | |
| 3297 | /* Check stack size. */ |
| 3298 | if (!lua_checkstack(L, 3)) |
| 3299 | return 0; |
| 3300 | |
| 3301 | /* Create the object: obj[0] = userdata. |
| 3302 | * Note that the base of the Fetches object is the |
| 3303 | * transaction object. |
| 3304 | */ |
| 3305 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3306 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3307 | lua_rawseti(L, -2, 0); |
| 3308 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3309 | hsmp->s = txn->s; |
| 3310 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3311 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3312 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3313 | |
| 3314 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3315 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3316 | lua_setmetatable(L, -2); |
| 3317 | |
| 3318 | return 1; |
| 3319 | } |
| 3320 | |
| 3321 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3322 | * It uses closure argument to store the associated sample-fetch. It |
| 3323 | * returns only one argument or throws an error. An error is thrown |
| 3324 | * only if an error is encountered during the argument parsing. If |
| 3325 | * the "sample-fetch" function fails, nil is returned. |
| 3326 | */ |
| 3327 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3328 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3329 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3330 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3331 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3332 | int i; |
| 3333 | struct sample smp; |
| 3334 | |
| 3335 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3336 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3337 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3338 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3339 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3340 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3341 | /* Check execution authorization. */ |
| 3342 | if (f->use & SMP_USE_HTTP_ANY && |
| 3343 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3344 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3345 | "is not available in Lua services", f->kw); |
| 3346 | WILL_LJMP(lua_error(L)); |
| 3347 | } |
| 3348 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3349 | /* Get extra arguments. */ |
| 3350 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3351 | if (i >= ARGM_NBARGS) |
| 3352 | break; |
| 3353 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3354 | } |
| 3355 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3356 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3357 | |
| 3358 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3359 | 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] | 3360 | |
| 3361 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3362 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3363 | lua_pushfstring(L, "error in arguments"); |
| 3364 | WILL_LJMP(lua_error(L)); |
| 3365 | } |
| 3366 | |
| 3367 | /* Initialise the sample. */ |
| 3368 | memset(&smp, 0, sizeof(smp)); |
| 3369 | |
| 3370 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3371 | 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] | 3372 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3373 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3374 | lua_pushstring(L, ""); |
| 3375 | else |
| 3376 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3377 | return 1; |
| 3378 | } |
| 3379 | |
| 3380 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3381 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3382 | hlua_smp2lua_str(L, &smp); |
| 3383 | else |
| 3384 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3385 | return 1; |
| 3386 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3387 | |
| 3388 | /* |
| 3389 | * |
| 3390 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3391 | * Class Converters |
| 3392 | * |
| 3393 | * |
| 3394 | */ |
| 3395 | |
| 3396 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3397 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3398 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3399 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3400 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3401 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3402 | } |
| 3403 | |
| 3404 | /* This function creates and push in the stack a Converters object |
| 3405 | * according with a current TXN. |
| 3406 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3407 | 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] | 3408 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3409 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3410 | |
| 3411 | /* Check stack size. */ |
| 3412 | if (!lua_checkstack(L, 3)) |
| 3413 | return 0; |
| 3414 | |
| 3415 | /* Create the object: obj[0] = userdata. |
| 3416 | * Note that the base of the Converters object is the |
| 3417 | * same than the TXN object. |
| 3418 | */ |
| 3419 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3420 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3421 | lua_rawseti(L, -2, 0); |
| 3422 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3423 | hsmp->s = txn->s; |
| 3424 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3425 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3426 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3427 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3428 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3429 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3430 | lua_setmetatable(L, -2); |
| 3431 | |
| 3432 | return 1; |
| 3433 | } |
| 3434 | |
| 3435 | /* This function is an LUA binding. It is called with each converter. |
| 3436 | * It uses closure argument to store the associated converter. It |
| 3437 | * returns only one argument or throws an error. An error is thrown |
| 3438 | * only if an error is encountered during the argument parsing. If |
| 3439 | * the converter function function fails, nil is returned. |
| 3440 | */ |
| 3441 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3442 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3443 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3444 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3445 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3446 | int i; |
| 3447 | struct sample smp; |
| 3448 | |
| 3449 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3450 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3451 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3452 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3453 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3454 | |
| 3455 | /* Get extra arguments. */ |
| 3456 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3457 | if (i >= ARGM_NBARGS) |
| 3458 | break; |
| 3459 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3460 | } |
| 3461 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3462 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3463 | |
| 3464 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3465 | 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] | 3466 | |
| 3467 | /* Run the special args checker. */ |
| 3468 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3469 | hlua_pusherror(L, "error in arguments"); |
| 3470 | WILL_LJMP(lua_error(L)); |
| 3471 | } |
| 3472 | |
| 3473 | /* Initialise the sample. */ |
Amaury Denoyelle | ca492f3 | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3474 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3475 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3476 | hlua_pusherror(L, "error in the input argument"); |
| 3477 | WILL_LJMP(lua_error(L)); |
| 3478 | } |
| 3479 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3480 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3481 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3482 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3483 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3484 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3485 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3486 | WILL_LJMP(lua_error(L)); |
| 3487 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3488 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3489 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3490 | hlua_pusherror(L, "error during the input argument casting"); |
| 3491 | WILL_LJMP(lua_error(L)); |
| 3492 | } |
| 3493 | |
| 3494 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3495 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3496 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3497 | lua_pushstring(L, ""); |
| 3498 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3499 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3500 | return 1; |
| 3501 | } |
| 3502 | |
| 3503 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3504 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3505 | hlua_smp2lua_str(L, &smp); |
| 3506 | else |
| 3507 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3508 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3509 | } |
| 3510 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3511 | /* |
| 3512 | * |
| 3513 | * |
| 3514 | * Class AppletTCP |
| 3515 | * |
| 3516 | * |
| 3517 | */ |
| 3518 | |
| 3519 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3520 | * a class stream, otherwise it throws an error. |
| 3521 | */ |
| 3522 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3523 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3524 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | /* This function creates and push in the stack an Applet object |
| 3528 | * according with a current TXN. |
| 3529 | */ |
| 3530 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3531 | { |
| 3532 | struct hlua_appctx *appctx; |
| 3533 | struct stream_interface *si = ctx->owner; |
| 3534 | struct stream *s = si_strm(si); |
| 3535 | struct proxy *p = s->be; |
| 3536 | |
| 3537 | /* Check stack size. */ |
| 3538 | if (!lua_checkstack(L, 3)) |
| 3539 | return 0; |
| 3540 | |
| 3541 | /* Create the object: obj[0] = userdata. |
| 3542 | * Note that the base of the Converters object is the |
| 3543 | * same than the TXN object. |
| 3544 | */ |
| 3545 | lua_newtable(L); |
| 3546 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3547 | lua_rawseti(L, -2, 0); |
| 3548 | appctx->appctx = ctx; |
| 3549 | appctx->htxn.s = s; |
| 3550 | appctx->htxn.p = p; |
| 3551 | |
| 3552 | /* Create the "f" field that contains a list of fetches. */ |
| 3553 | lua_pushstring(L, "f"); |
| 3554 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3555 | return 0; |
| 3556 | lua_settable(L, -3); |
| 3557 | |
| 3558 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3559 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3560 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3561 | return 0; |
| 3562 | lua_settable(L, -3); |
| 3563 | |
| 3564 | /* Create the "c" field that contains a list of converters. */ |
| 3565 | lua_pushstring(L, "c"); |
| 3566 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3567 | return 0; |
| 3568 | lua_settable(L, -3); |
| 3569 | |
| 3570 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3571 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3572 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3573 | return 0; |
| 3574 | lua_settable(L, -3); |
| 3575 | |
| 3576 | /* Pop a class stream metatable and affect it to the table. */ |
| 3577 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3578 | lua_setmetatable(L, -2); |
| 3579 | |
| 3580 | return 1; |
| 3581 | } |
| 3582 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3583 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3584 | { |
| 3585 | struct hlua_appctx *appctx; |
| 3586 | struct stream *s; |
| 3587 | const char *name; |
| 3588 | size_t len; |
| 3589 | struct sample smp; |
| 3590 | |
| 3591 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3592 | |
| 3593 | /* It is useles to retrieve the stream, but this function |
| 3594 | * runs only in a stream context. |
| 3595 | */ |
| 3596 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3597 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3598 | s = appctx->htxn.s; |
| 3599 | |
| 3600 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | ca492f3 | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3601 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3602 | hlua_lua2smp(L, 3, &smp); |
| 3603 | |
| 3604 | /* Store the sample in a variable. */ |
| 3605 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3606 | vars_set_by_name(name, len, &smp); |
| 3607 | return 0; |
| 3608 | } |
| 3609 | |
| 3610 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3611 | { |
| 3612 | struct hlua_appctx *appctx; |
| 3613 | struct stream *s; |
| 3614 | const char *name; |
| 3615 | size_t len; |
| 3616 | struct sample smp; |
| 3617 | |
| 3618 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3619 | |
| 3620 | /* It is useles to retrieve the stream, but this function |
| 3621 | * runs only in a stream context. |
| 3622 | */ |
| 3623 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3624 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3625 | s = appctx->htxn.s; |
| 3626 | |
| 3627 | /* Unset the variable. */ |
| 3628 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3629 | vars_unset_by_name(name, len, &smp); |
| 3630 | return 0; |
| 3631 | } |
| 3632 | |
| 3633 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3634 | { |
| 3635 | struct hlua_appctx *appctx; |
| 3636 | struct stream *s; |
| 3637 | const char *name; |
| 3638 | size_t len; |
| 3639 | struct sample smp; |
| 3640 | |
| 3641 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3642 | |
| 3643 | /* It is useles to retrieve the stream, but this function |
| 3644 | * runs only in a stream context. |
| 3645 | */ |
| 3646 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3647 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3648 | s = appctx->htxn.s; |
| 3649 | |
| 3650 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3651 | if (!vars_get_by_name(name, len, &smp)) { |
| 3652 | lua_pushnil(L); |
| 3653 | return 1; |
| 3654 | } |
| 3655 | |
| 3656 | return hlua_smp2lua(L, &smp); |
| 3657 | } |
| 3658 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3659 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3660 | { |
| 3661 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3662 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3663 | struct hlua *hlua; |
| 3664 | |
| 3665 | /* 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] | 3666 | if (!s->hlua) |
| 3667 | return 0; |
| 3668 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3669 | |
| 3670 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3671 | |
| 3672 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3673 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3674 | |
| 3675 | /* Get and store new value. */ |
| 3676 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3677 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3678 | |
| 3679 | return 0; |
| 3680 | } |
| 3681 | |
| 3682 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3683 | { |
| 3684 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3685 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3686 | struct hlua *hlua; |
| 3687 | |
| 3688 | /* 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] | 3689 | if (!s->hlua) { |
| 3690 | lua_pushnil(L); |
| 3691 | return 1; |
| 3692 | } |
| 3693 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3694 | |
| 3695 | /* Push configuration index in the stack. */ |
| 3696 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3697 | |
| 3698 | return 1; |
| 3699 | } |
| 3700 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3701 | /* If expected data not yet available, it returns a yield. This function |
| 3702 | * consumes the data in the buffer. It returns a string containing the |
| 3703 | * data. This string can be empty. |
| 3704 | */ |
| 3705 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3706 | { |
| 3707 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3708 | struct stream_interface *si = appctx->appctx->owner; |
| 3709 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3710 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3711 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3712 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3713 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3714 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3715 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3716 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3717 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3718 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3719 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3720 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3721 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3722 | } |
| 3723 | |
| 3724 | /* End of data: commit the total strings and return. */ |
| 3725 | if (ret < 0) { |
| 3726 | luaL_pushresult(&appctx->b); |
| 3727 | return 1; |
| 3728 | } |
| 3729 | |
| 3730 | /* Ensure that the block 2 length is usable. */ |
| 3731 | if (ret == 1) |
| 3732 | len2 = 0; |
| 3733 | |
| 3734 | /* dont check the max length read and dont check. */ |
| 3735 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3736 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3737 | |
| 3738 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3739 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3740 | luaL_pushresult(&appctx->b); |
| 3741 | return 1; |
| 3742 | } |
| 3743 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3744 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3745 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3746 | { |
| 3747 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3748 | |
| 3749 | /* Initialise the string catenation. */ |
| 3750 | luaL_buffinit(L, &appctx->b); |
| 3751 | |
| 3752 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3753 | } |
| 3754 | |
| 3755 | /* If expected data not yet available, it returns a yield. This function |
| 3756 | * consumes the data in the buffer. It returns a string containing the |
| 3757 | * data. This string can be empty. |
| 3758 | */ |
| 3759 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3760 | { |
| 3761 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3762 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3763 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3764 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3765 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3766 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3767 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3768 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3769 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3770 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3771 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3772 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3773 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3774 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3775 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3776 | 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] | 3777 | } |
| 3778 | |
| 3779 | /* End of data: commit the total strings and return. */ |
| 3780 | if (ret < 0) { |
| 3781 | luaL_pushresult(&appctx->b); |
| 3782 | return 1; |
| 3783 | } |
| 3784 | |
| 3785 | /* Ensure that the block 2 length is usable. */ |
| 3786 | if (ret == 1) |
| 3787 | len2 = 0; |
| 3788 | |
| 3789 | if (len == -1) { |
| 3790 | |
| 3791 | /* If len == -1, catenate all the data avalaile and |
| 3792 | * yield because we want to get all the data until |
| 3793 | * the end of data stream. |
| 3794 | */ |
| 3795 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3796 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3797 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3798 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3799 | 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] | 3800 | |
| 3801 | } else { |
| 3802 | |
| 3803 | /* Copy the fisrt block caping to the length required. */ |
| 3804 | if (len1 > len) |
| 3805 | len1 = len; |
| 3806 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3807 | len -= len1; |
| 3808 | |
| 3809 | /* Copy the second block. */ |
| 3810 | if (len2 > len) |
| 3811 | len2 = len; |
| 3812 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3813 | len -= len2; |
| 3814 | |
| 3815 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3816 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3817 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3818 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3819 | if (len > 0) { |
| 3820 | lua_pushinteger(L, len); |
| 3821 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3822 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3823 | 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] | 3824 | } |
| 3825 | |
| 3826 | /* return the result. */ |
| 3827 | luaL_pushresult(&appctx->b); |
| 3828 | return 1; |
| 3829 | } |
| 3830 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3831 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3832 | hlua_pusherror(L, "Lua: internal error"); |
| 3833 | WILL_LJMP(lua_error(L)); |
| 3834 | return 0; |
| 3835 | } |
| 3836 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3837 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3838 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3839 | { |
| 3840 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3841 | int len = -1; |
| 3842 | |
| 3843 | if (lua_gettop(L) > 2) |
| 3844 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3845 | if (lua_gettop(L) >= 2) { |
| 3846 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3847 | lua_pop(L, 1); |
| 3848 | } |
| 3849 | |
| 3850 | /* Confirm or set the required length */ |
| 3851 | lua_pushinteger(L, len); |
| 3852 | |
| 3853 | /* Initialise the string catenation. */ |
| 3854 | luaL_buffinit(L, &appctx->b); |
| 3855 | |
| 3856 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3857 | } |
| 3858 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3859 | /* Append data in the output side of the buffer. This data is immediately |
| 3860 | * sent. The function returns the amount of data written. If the buffer |
| 3861 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3862 | * if the channel is closed. |
| 3863 | */ |
| 3864 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3865 | { |
| 3866 | size_t len; |
| 3867 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3868 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3869 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3870 | struct stream_interface *si = appctx->appctx->owner; |
| 3871 | struct channel *chn = si_ic(si); |
| 3872 | int max; |
| 3873 | |
| 3874 | /* Get the max amount of data which can write as input in the channel. */ |
| 3875 | max = channel_recv_max(chn); |
| 3876 | if (max > (len - l)) |
| 3877 | max = len - l; |
| 3878 | |
| 3879 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3880 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3881 | |
| 3882 | /* update counters. */ |
| 3883 | l += max; |
| 3884 | lua_pop(L, 1); |
| 3885 | lua_pushinteger(L, l); |
| 3886 | |
| 3887 | /* If some data is not send, declares the situation to the |
| 3888 | * applet, and returns a yield. |
| 3889 | */ |
| 3890 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3891 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3892 | 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] | 3893 | } |
| 3894 | |
| 3895 | return 1; |
| 3896 | } |
| 3897 | |
| 3898 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3899 | * yield the LUA process, and resume it without checking the |
| 3900 | * input arguments. |
| 3901 | */ |
| 3902 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3903 | { |
| 3904 | MAY_LJMP(check_args(L, 2, "send")); |
| 3905 | lua_pushinteger(L, 0); |
| 3906 | |
| 3907 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3908 | } |
| 3909 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3910 | /* |
| 3911 | * |
| 3912 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3913 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3914 | * |
| 3915 | * |
| 3916 | */ |
| 3917 | |
| 3918 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3919 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3920 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3921 | __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] | 3922 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3923 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3924 | } |
| 3925 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3926 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3927 | * according with a current TXN. |
| 3928 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3929 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3930 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3931 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3932 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3933 | struct stream_interface *si = ctx->owner; |
| 3934 | struct stream *s = si_strm(si); |
| 3935 | struct proxy *px = s->be; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3936 | |
| 3937 | /* Check stack size. */ |
| 3938 | if (!lua_checkstack(L, 3)) |
| 3939 | return 0; |
| 3940 | |
| 3941 | /* Create the object: obj[0] = userdata. |
| 3942 | * Note that the base of the Converters object is the |
| 3943 | * same than the TXN object. |
| 3944 | */ |
| 3945 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3946 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3947 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3948 | appctx->appctx = ctx; |
| 3949 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3950 | 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] | 3951 | appctx->htxn.s = s; |
| 3952 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3953 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3954 | /* Create the "f" field that contains a list of fetches. */ |
| 3955 | lua_pushstring(L, "f"); |
| 3956 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3957 | return 0; |
| 3958 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3959 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3960 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3961 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3962 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3963 | return 0; |
| 3964 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3965 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3966 | /* Create the "c" field that contains a list of converters. */ |
| 3967 | lua_pushstring(L, "c"); |
| 3968 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3969 | return 0; |
| 3970 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3971 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3972 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3973 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3974 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3975 | return 0; |
| 3976 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3977 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3978 | if (IS_HTX_STRM(s)) { |
| 3979 | /* HTX version */ |
| 3980 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3981 | struct htx_blk *blk; |
| 3982 | struct htx_sl *sl; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3983 | struct ist path; |
| 3984 | unsigned long long len = 0; |
| 3985 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3986 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3987 | blk = htx_get_first_blk(htx); |
Christopher Faulet | 43a686d | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 3988 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3989 | sl = htx_get_blk_ptr(htx, blk); |
| 3990 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3991 | /* Stores the request method. */ |
| 3992 | lua_pushstring(L, "method"); |
| 3993 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3994 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3995 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3996 | /* Stores the http version. */ |
| 3997 | lua_pushstring(L, "version"); |
| 3998 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3999 | lua_settable(L, -3); |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4000 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4001 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4002 | * the array on the top of the stack. |
| 4003 | */ |
| 4004 | lua_pushstring(L, "headers"); |
| 4005 | htxn.s = s; |
| 4006 | htxn.p = px; |
| 4007 | htxn.dir = SMP_OPT_DIR_REQ; |
| 4008 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 4009 | return 0; |
| 4010 | lua_settable(L, -3); |
| 4011 | |
| 4012 | path = http_get_path(htx_sl_req_uri(sl)); |
| 4013 | if (path.ptr) { |
| 4014 | char *p, *q, *end; |
| 4015 | |
| 4016 | p = path.ptr; |
| 4017 | end = path.ptr + path.len; |
| 4018 | q = p; |
| 4019 | while (q < end && *q != '?') |
| 4020 | q++; |
| 4021 | |
| 4022 | /* Stores the request path. */ |
| 4023 | lua_pushstring(L, "path"); |
| 4024 | lua_pushlstring(L, p, q - p); |
| 4025 | lua_settable(L, -3); |
| 4026 | |
| 4027 | /* Stores the query string. */ |
| 4028 | lua_pushstring(L, "qs"); |
| 4029 | if (*q == '?') |
| 4030 | q++; |
| 4031 | lua_pushlstring(L, q, end - q); |
| 4032 | lua_settable(L, -3); |
| 4033 | } |
| 4034 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4035 | 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] | 4036 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4037 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4038 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4039 | 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] | 4040 | break; |
| 4041 | if (type == HTX_BLK_DATA) |
| 4042 | len += htx_get_blksz(blk); |
| 4043 | } |
| 4044 | if (htx->extra != ULLONG_MAX) |
| 4045 | len += htx->extra; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4046 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4047 | /* Stores the request path. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4048 | lua_pushstring(L, "length"); |
| 4049 | lua_pushinteger(L, len); |
| 4050 | lua_settable(L, -3); |
| 4051 | } |
| 4052 | else { |
| 4053 | /* Legacy HTTP version */ |
| 4054 | struct http_txn *txn = s->txn; |
| 4055 | const char *path; |
| 4056 | const char *end; |
| 4057 | const char *p; |
| 4058 | |
| 4059 | /* Stores the request method. */ |
| 4060 | lua_pushstring(L, "method"); |
| 4061 | 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] | 4062 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4063 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4064 | /* Stores the http version. */ |
| 4065 | lua_pushstring(L, "version"); |
| 4066 | 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] | 4067 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4068 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4069 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4070 | * the array on the top of the stack. |
| 4071 | */ |
| 4072 | lua_pushstring(L, "headers"); |
| 4073 | htxn.s = s; |
| 4074 | htxn.p = px; |
| 4075 | htxn.dir = SMP_OPT_DIR_REQ; |
| 4076 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 4077 | return 0; |
| 4078 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4079 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4080 | /* Get path and qs */ |
| 4081 | path = http_txn_get_path(txn); |
| 4082 | if (path) { |
| 4083 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 4084 | p = path; |
| 4085 | while (p < end && *p != '?') |
| 4086 | p++; |
| 4087 | |
| 4088 | /* Stores the request path. */ |
| 4089 | lua_pushstring(L, "path"); |
| 4090 | lua_pushlstring(L, path, p - path); |
| 4091 | lua_settable(L, -3); |
| 4092 | |
| 4093 | /* Stores the query string. */ |
| 4094 | lua_pushstring(L, "qs"); |
| 4095 | if (*p == '?') |
| 4096 | p++; |
| 4097 | lua_pushlstring(L, p, end - p); |
| 4098 | lua_settable(L, -3); |
| 4099 | } |
| 4100 | |
| 4101 | /* Stores the request path. */ |
| 4102 | lua_pushstring(L, "length"); |
| 4103 | lua_pushinteger(L, txn->req.body_len); |
| 4104 | lua_settable(L, -3); |
| 4105 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4106 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4107 | /* Create an empty array of HTTP request headers. */ |
| 4108 | lua_pushstring(L, "response"); |
| 4109 | lua_newtable(L); |
| 4110 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4111 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4112 | /* Pop a class stream metatable and affect it to the table. */ |
| 4113 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4114 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4115 | |
| 4116 | return 1; |
| 4117 | } |
| 4118 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4119 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4120 | { |
| 4121 | struct hlua_appctx *appctx; |
| 4122 | struct stream *s; |
| 4123 | const char *name; |
| 4124 | size_t len; |
| 4125 | struct sample smp; |
| 4126 | |
| 4127 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4128 | |
| 4129 | /* It is useles to retrieve the stream, but this function |
| 4130 | * runs only in a stream context. |
| 4131 | */ |
| 4132 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4133 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4134 | s = appctx->htxn.s; |
| 4135 | |
| 4136 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | ca492f3 | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4137 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4138 | hlua_lua2smp(L, 3, &smp); |
| 4139 | |
| 4140 | /* Store the sample in a variable. */ |
| 4141 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4142 | vars_set_by_name(name, len, &smp); |
| 4143 | return 0; |
| 4144 | } |
| 4145 | |
| 4146 | __LJMP static int hlua_applet_http_unset_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, "unset_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 | /* Unset the variable. */ |
| 4164 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4165 | vars_unset_by_name(name, len, &smp); |
| 4166 | return 0; |
| 4167 | } |
| 4168 | |
| 4169 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4170 | { |
| 4171 | struct hlua_appctx *appctx; |
| 4172 | struct stream *s; |
| 4173 | const char *name; |
| 4174 | size_t len; |
| 4175 | struct sample smp; |
| 4176 | |
| 4177 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4178 | |
| 4179 | /* It is useles to retrieve the stream, but this function |
| 4180 | * runs only in a stream context. |
| 4181 | */ |
| 4182 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4183 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4184 | s = appctx->htxn.s; |
| 4185 | |
| 4186 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4187 | if (!vars_get_by_name(name, len, &smp)) { |
| 4188 | lua_pushnil(L); |
| 4189 | return 1; |
| 4190 | } |
| 4191 | |
| 4192 | return hlua_smp2lua(L, &smp); |
| 4193 | } |
| 4194 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4195 | __LJMP static int hlua_applet_http_set_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 | return 0; |
| 4204 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4205 | |
| 4206 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4207 | |
| 4208 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4209 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4210 | |
| 4211 | /* Get and store new value. */ |
| 4212 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4213 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4214 | |
| 4215 | return 0; |
| 4216 | } |
| 4217 | |
| 4218 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4219 | { |
| 4220 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4221 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4222 | struct hlua *hlua; |
| 4223 | |
| 4224 | /* 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] | 4225 | if (!s->hlua) { |
| 4226 | lua_pushnil(L); |
| 4227 | return 1; |
| 4228 | } |
| 4229 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4230 | |
| 4231 | /* Push configuration index in the stack. */ |
| 4232 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4233 | |
| 4234 | return 1; |
| 4235 | } |
| 4236 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4237 | /* If expected data not yet available, it returns a yield. This function |
| 4238 | * consumes the data in the buffer. It returns a string containing the |
| 4239 | * data. This string can be empty. |
| 4240 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4241 | __LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4242 | { |
| 4243 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4244 | struct stream_interface *si = appctx->appctx->owner; |
| 4245 | struct channel *req = si_oc(si); |
| 4246 | struct htx *htx; |
| 4247 | struct htx_blk *blk; |
| 4248 | size_t count; |
| 4249 | int stop = 0; |
| 4250 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4251 | htx = htx_from_buf(&req->buf); |
| 4252 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4253 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4254 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4255 | while (count && !stop && blk) { |
| 4256 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4257 | uint32_t sz = htx_get_blksz(blk); |
| 4258 | struct ist v; |
| 4259 | uint32_t vlen; |
| 4260 | char *nl; |
| 4261 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4262 | if (type == HTX_BLK_EOM) { |
| 4263 | stop = 1; |
| 4264 | break; |
| 4265 | } |
| 4266 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4267 | vlen = sz; |
| 4268 | if (vlen > count) { |
| 4269 | if (type != HTX_BLK_DATA) |
| 4270 | break; |
| 4271 | vlen = count; |
| 4272 | } |
| 4273 | |
| 4274 | switch (type) { |
| 4275 | case HTX_BLK_UNUSED: |
| 4276 | break; |
| 4277 | |
| 4278 | case HTX_BLK_DATA: |
| 4279 | v = htx_get_blk_value(htx, blk); |
| 4280 | v.len = vlen; |
| 4281 | nl = istchr(v, '\n'); |
| 4282 | if (nl != NULL) { |
| 4283 | stop = 1; |
| 4284 | vlen = nl - v.ptr + 1; |
| 4285 | } |
| 4286 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4287 | break; |
| 4288 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4289 | case HTX_BLK_TLR: |
| 4290 | case HTX_BLK_EOM: |
| 4291 | stop = 1; |
| 4292 | break; |
| 4293 | |
| 4294 | default: |
| 4295 | break; |
| 4296 | } |
| 4297 | |
| 4298 | co_set_data(req, co_data(req) - vlen); |
| 4299 | count -= vlen; |
| 4300 | if (sz == vlen) |
| 4301 | blk = htx_remove_blk(htx, blk); |
| 4302 | else { |
| 4303 | htx_cut_data_blk(htx, blk, vlen); |
| 4304 | break; |
| 4305 | } |
| 4306 | } |
| 4307 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4308 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4309 | if (!stop) { |
| 4310 | si_cant_get(si); |
| 4311 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0)); |
| 4312 | } |
| 4313 | |
| 4314 | /* return the result. */ |
| 4315 | luaL_pushresult(&appctx->b); |
| 4316 | return 1; |
| 4317 | } |
| 4318 | |
| 4319 | |
| 4320 | /* If expected data not yet available, it returns a yield. This function |
| 4321 | * consumes the data in the buffer. It returns a string containing the |
| 4322 | * data. This string can be empty. |
| 4323 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4324 | __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] | 4325 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4326 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4327 | struct stream_interface *si = appctx->appctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4328 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4329 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4330 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4331 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4332 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4333 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4334 | /* Check for the end of the data. */ |
| 4335 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4336 | luaL_pushresult(&appctx->b); |
| 4337 | return 1; |
| 4338 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4339 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4340 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4341 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4342 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4343 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4344 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4345 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4346 | 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] | 4347 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4348 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4349 | /* End of data: commit the total strings and return. */ |
| 4350 | if (ret < 0) { |
| 4351 | luaL_pushresult(&appctx->b); |
| 4352 | return 1; |
| 4353 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4354 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4355 | /* Ensure that the block 2 length is usable. */ |
| 4356 | if (ret == 1) |
| 4357 | len2 = 0; |
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 | /* Copy the fisrt block caping to the length required. */ |
| 4360 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4361 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4362 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4363 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4364 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4365 | /* Copy the second block. */ |
| 4366 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4367 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4368 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4369 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4370 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4371 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4372 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4373 | luaL_pushresult(&appctx->b); |
| 4374 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4375 | } |
| 4376 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4377 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4378 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4379 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4380 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4381 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4382 | /* Initialise the string catenation. */ |
| 4383 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4384 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4385 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 4386 | return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0)); |
| 4387 | else |
| 4388 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4389 | } |
| 4390 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4391 | /* If expected data not yet available, it returns a yield. This function |
| 4392 | * consumes the data in the buffer. It returns a string containing the |
| 4393 | * data. This string can be empty. |
| 4394 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4395 | __LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4396 | { |
| 4397 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4398 | struct stream_interface *si = appctx->appctx->owner; |
| 4399 | struct channel *req = si_oc(si); |
| 4400 | struct htx *htx; |
| 4401 | struct htx_blk *blk; |
| 4402 | size_t count; |
| 4403 | int len; |
| 4404 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4405 | htx = htx_from_buf(&req->buf); |
| 4406 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4407 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4408 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4409 | while (count && len && blk) { |
| 4410 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4411 | uint32_t sz = htx_get_blksz(blk); |
| 4412 | struct ist v; |
| 4413 | uint32_t vlen; |
| 4414 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4415 | if (type == HTX_BLK_EOM) { |
| 4416 | len = 0; |
| 4417 | break; |
| 4418 | } |
| 4419 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4420 | vlen = sz; |
| 4421 | if (len > 0 && vlen > len) |
| 4422 | vlen = len; |
| 4423 | if (vlen > count) { |
| 4424 | if (type != HTX_BLK_DATA) |
| 4425 | break; |
| 4426 | vlen = count; |
| 4427 | } |
| 4428 | |
| 4429 | switch (type) { |
| 4430 | case HTX_BLK_UNUSED: |
| 4431 | break; |
| 4432 | |
| 4433 | case HTX_BLK_DATA: |
| 4434 | v = htx_get_blk_value(htx, blk); |
| 4435 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4436 | break; |
| 4437 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4438 | case HTX_BLK_TLR: |
| 4439 | case HTX_BLK_EOM: |
| 4440 | len = 0; |
| 4441 | break; |
| 4442 | |
| 4443 | default: |
| 4444 | break; |
| 4445 | } |
| 4446 | |
| 4447 | co_set_data(req, co_data(req) - vlen); |
| 4448 | count -= vlen; |
| 4449 | if (len > 0) |
| 4450 | len -= vlen; |
| 4451 | if (sz == vlen) |
| 4452 | blk = htx_remove_blk(htx, blk); |
| 4453 | else { |
| 4454 | htx_cut_data_blk(htx, blk, vlen); |
| 4455 | break; |
| 4456 | } |
| 4457 | } |
| 4458 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4459 | htx_to_buf(htx, &req->buf); |
| 4460 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4461 | /* If we are no other data available, yield waiting for new data. */ |
| 4462 | if (len) { |
| 4463 | if (len > 0) { |
| 4464 | lua_pushinteger(L, len); |
| 4465 | lua_replace(L, 2); |
| 4466 | } |
| 4467 | si_cant_get(si); |
| 4468 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0)); |
| 4469 | } |
| 4470 | |
| 4471 | /* return the result. */ |
| 4472 | luaL_pushresult(&appctx->b); |
| 4473 | return 1; |
| 4474 | } |
| 4475 | |
| 4476 | /* If expected data not yet available, it returns a yield. This function |
| 4477 | * consumes the data in the buffer. It returns a string containing the |
| 4478 | * data. This string can be empty. |
| 4479 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4480 | __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] | 4481 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4482 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4483 | struct stream_interface *si = appctx->appctx->owner; |
| 4484 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4485 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4486 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4487 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4488 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4489 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4490 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4491 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4492 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4493 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4494 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4495 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4496 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4497 | 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] | 4498 | } |
| 4499 | |
| 4500 | /* End of data: commit the total strings and return. */ |
| 4501 | if (ret < 0) { |
| 4502 | luaL_pushresult(&appctx->b); |
| 4503 | return 1; |
| 4504 | } |
| 4505 | |
| 4506 | /* Ensure that the block 2 length is usable. */ |
| 4507 | if (ret == 1) |
| 4508 | len2 = 0; |
| 4509 | |
| 4510 | /* Copy the fisrt block caping to the length required. */ |
| 4511 | if (len1 > len) |
| 4512 | len1 = len; |
| 4513 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4514 | len -= len1; |
| 4515 | |
| 4516 | /* Copy the second block. */ |
| 4517 | if (len2 > len) |
| 4518 | len2 = len; |
| 4519 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4520 | len -= len2; |
| 4521 | |
| 4522 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4523 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4524 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4525 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4526 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4527 | /* If we are no other data available, yield waiting for new data. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4528 | if (len > 0) { |
| 4529 | lua_pushinteger(L, len); |
| 4530 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4531 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4532 | 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] | 4533 | } |
| 4534 | |
| 4535 | /* return the result. */ |
| 4536 | luaL_pushresult(&appctx->b); |
| 4537 | return 1; |
| 4538 | } |
| 4539 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4540 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4541 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4542 | { |
| 4543 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4544 | int len = -1; |
| 4545 | |
| 4546 | /* Check arguments. */ |
| 4547 | if (lua_gettop(L) > 2) |
| 4548 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4549 | if (lua_gettop(L) >= 2) { |
| 4550 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4551 | lua_pop(L, 1); |
| 4552 | } |
| 4553 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4554 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4555 | /* HTX version */ |
| 4556 | lua_pushinteger(L, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4557 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4558 | /* Initialise the string catenation. */ |
| 4559 | luaL_buffinit(L, &appctx->b); |
| 4560 | |
| 4561 | return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0)); |
| 4562 | } |
| 4563 | else { |
| 4564 | /* Legacy HTTP version */ |
| 4565 | /* Check the required length */ |
| 4566 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4567 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4568 | lua_pushinteger(L, len); |
| 4569 | |
| 4570 | /* Initialise the string catenation. */ |
| 4571 | luaL_buffinit(L, &appctx->b); |
| 4572 | |
| 4573 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4574 | } |
| 4575 | } |
| 4576 | |
| 4577 | /* Append data in the output side of the buffer. This data is immediately |
| 4578 | * sent. The function returns the amount of data written. If the buffer |
| 4579 | * cannot contain the data, the function yields. The function returns -1 |
| 4580 | * if the channel is closed. |
| 4581 | */ |
| 4582 | __LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4583 | { |
| 4584 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4585 | struct stream_interface *si = appctx->appctx->owner; |
| 4586 | struct channel *res = si_ic(si); |
| 4587 | struct htx *htx = htx_from_buf(&res->buf); |
| 4588 | const char *data; |
| 4589 | size_t len; |
| 4590 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4591 | int max; |
| 4592 | |
Christopher Faulet | 4e96301 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4593 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4594 | if (!max) |
| 4595 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4596 | |
| 4597 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4598 | |
| 4599 | /* Get the max amount of data which can write as input in the channel. */ |
| 4600 | if (max > (len - l)) |
| 4601 | max = len - l; |
| 4602 | |
| 4603 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4604 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4605 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4606 | |
| 4607 | /* update counters. */ |
| 4608 | l += max; |
| 4609 | lua_pop(L, 1); |
| 4610 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4611 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4612 | /* If some data is not send, declares the situation to the |
| 4613 | * applet, and returns a yield. |
| 4614 | */ |
| 4615 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4616 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4617 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4618 | si_rx_room_blk(si); |
| 4619 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0)); |
| 4620 | } |
| 4621 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4622 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4623 | return 1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4624 | } |
| 4625 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4626 | /* Append data in the output side of the buffer. This data is immediately |
| 4627 | * sent. The function returns the amount of data written. If the buffer |
| 4628 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4629 | * if the channel is closed. |
| 4630 | */ |
| 4631 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4632 | { |
| 4633 | size_t len; |
| 4634 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4635 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4636 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4637 | struct stream_interface *si = appctx->appctx->owner; |
| 4638 | struct channel *chn = si_ic(si); |
| 4639 | int max; |
| 4640 | |
| 4641 | /* Get the max amount of data which can write as input in the channel. */ |
| 4642 | max = channel_recv_max(chn); |
| 4643 | if (max > (len - l)) |
| 4644 | max = len - l; |
| 4645 | |
| 4646 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4647 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4648 | |
| 4649 | /* update counters. */ |
| 4650 | l += max; |
| 4651 | lua_pop(L, 1); |
| 4652 | lua_pushinteger(L, l); |
| 4653 | |
| 4654 | /* If some data is not send, declares the situation to the |
| 4655 | * applet, and returns a yield. |
| 4656 | */ |
| 4657 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4658 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4659 | 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] | 4660 | } |
| 4661 | |
| 4662 | return 1; |
| 4663 | } |
| 4664 | |
| 4665 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4666 | * yield the LUA process, and resume it without checking the |
| 4667 | * input arguments. |
| 4668 | */ |
| 4669 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4670 | { |
| 4671 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4672 | |
| 4673 | /* We want to send some data. Headers must be sent. */ |
| 4674 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4675 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4676 | WILL_LJMP(lua_error(L)); |
| 4677 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4678 | |
| 4679 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4680 | /* HTX version */ |
| 4681 | /* This interger is used for followinf the amount of data sent. */ |
| 4682 | lua_pushinteger(L, 0); |
| 4683 | |
| 4684 | return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0)); |
| 4685 | } |
| 4686 | else { |
| 4687 | /* Legacy HTTP version */ |
| 4688 | size_t len; |
| 4689 | char hex[10]; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4690 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4691 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4692 | |
| 4693 | /* If transfer encoding chunked is selected, we surround the data |
| 4694 | * by chunk data. |
| 4695 | */ |
| 4696 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4697 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4698 | lua_pushfstring(L, "%s\r\n", hex); |
| 4699 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4700 | lua_pushstring(L, "\r\n"); |
| 4701 | lua_concat(L, 3); |
| 4702 | } |
| 4703 | |
| 4704 | /* This interger is used for followinf the amount of data sent. */ |
| 4705 | lua_pushinteger(L, 0); |
| 4706 | |
| 4707 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4708 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4709 | } |
| 4710 | |
| 4711 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4712 | { |
| 4713 | const char *name; |
| 4714 | int ret; |
| 4715 | |
| 4716 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4717 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4718 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4719 | |
| 4720 | /* Push in the stack the "response" entry. */ |
| 4721 | ret = lua_getfield(L, 1, "response"); |
| 4722 | if (ret != LUA_TTABLE) { |
| 4723 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4724 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4725 | WILL_LJMP(lua_error(L)); |
| 4726 | } |
| 4727 | |
| 4728 | /* check if the header is already registered if it is not |
| 4729 | * the case, register it. |
| 4730 | */ |
| 4731 | ret = lua_getfield(L, -1, name); |
| 4732 | if (ret == LUA_TNIL) { |
| 4733 | |
| 4734 | /* Entry not found. */ |
| 4735 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4736 | |
| 4737 | /* Insert the new header name in the array in the top of the stack. |
| 4738 | * It left the new array in the top of the stack. |
| 4739 | */ |
| 4740 | lua_newtable(L); |
| 4741 | lua_pushvalue(L, 2); |
| 4742 | lua_pushvalue(L, -2); |
| 4743 | lua_settable(L, -4); |
| 4744 | |
| 4745 | } else if (ret != LUA_TTABLE) { |
| 4746 | |
| 4747 | /* corruption error. */ |
| 4748 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4749 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4750 | WILL_LJMP(lua_error(L)); |
| 4751 | } |
| 4752 | |
| 4753 | /* Now the top od thestack is an array of values. We push |
| 4754 | * the header value as new entry. |
| 4755 | */ |
| 4756 | lua_pushvalue(L, 3); |
| 4757 | ret = lua_rawlen(L, -2); |
| 4758 | lua_rawseti(L, -2, ret + 1); |
| 4759 | lua_pushboolean(L, 1); |
| 4760 | return 1; |
| 4761 | } |
| 4762 | |
| 4763 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4764 | { |
| 4765 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4766 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4767 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4768 | |
| 4769 | if (status < 100 || status > 599) { |
| 4770 | lua_pushboolean(L, 0); |
| 4771 | return 1; |
| 4772 | } |
| 4773 | |
| 4774 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4775 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4776 | lua_pushboolean(L, 1); |
| 4777 | return 1; |
| 4778 | } |
| 4779 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4780 | |
| 4781 | __LJMP static int hlua_applet_htx_send_response(lua_State *L) |
| 4782 | { |
| 4783 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4784 | struct stream_interface *si = appctx->appctx->owner; |
| 4785 | struct channel *res = si_ic(si); |
| 4786 | struct htx *htx; |
| 4787 | struct htx_sl *sl; |
| 4788 | struct h1m h1m; |
| 4789 | const char *status, *reason; |
| 4790 | const char *name, *value; |
| 4791 | size_t nlen, vlen; |
| 4792 | unsigned int flags; |
| 4793 | |
| 4794 | /* Send the message at once. */ |
| 4795 | htx = htx_from_buf(&res->buf); |
| 4796 | h1m_init_res(&h1m); |
| 4797 | |
| 4798 | /* Use the same http version than the request. */ |
| 4799 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4800 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4801 | if (reason == NULL) |
| 4802 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4803 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4804 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4805 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4806 | } |
| 4807 | else { |
| 4808 | flags = HTX_SL_F_IS_RESP; |
| 4809 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4810 | } |
| 4811 | if (!sl) { |
| 4812 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4813 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4814 | WILL_LJMP(lua_error(L)); |
| 4815 | } |
| 4816 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4817 | |
| 4818 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4819 | lua_pushvalue(L, 0); |
| 4820 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4821 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4822 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4823 | WILL_LJMP(lua_error(L)); |
| 4824 | } |
| 4825 | |
| 4826 | /* Browse the list of headers. */ |
| 4827 | lua_pushnil(L); |
| 4828 | while(lua_next(L, -2) != 0) { |
| 4829 | /* We expect a string as -2. */ |
| 4830 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4831 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4832 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4833 | lua_typename(L, lua_type(L, -2))); |
| 4834 | WILL_LJMP(lua_error(L)); |
| 4835 | } |
| 4836 | name = lua_tolstring(L, -2, &nlen); |
| 4837 | |
| 4838 | /* We expect an array as -1. */ |
| 4839 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4840 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4841 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4842 | name, |
| 4843 | lua_typename(L, lua_type(L, -1))); |
| 4844 | WILL_LJMP(lua_error(L)); |
| 4845 | } |
| 4846 | |
| 4847 | /* Browse the table who is on the top of the stack. */ |
| 4848 | lua_pushnil(L); |
| 4849 | while(lua_next(L, -2) != 0) { |
| 4850 | int id; |
| 4851 | |
| 4852 | /* We expect a number as -2. */ |
| 4853 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4854 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4855 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4856 | name, |
| 4857 | lua_typename(L, lua_type(L, -2))); |
| 4858 | WILL_LJMP(lua_error(L)); |
| 4859 | } |
| 4860 | id = lua_tointeger(L, -2); |
| 4861 | |
| 4862 | /* We expect a string as -2. */ |
| 4863 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4864 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4865 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4866 | name, id, |
| 4867 | lua_typename(L, lua_type(L, -1))); |
| 4868 | WILL_LJMP(lua_error(L)); |
| 4869 | } |
| 4870 | value = lua_tolstring(L, -1, &vlen); |
| 4871 | |
| 4872 | /* Simple Protocol checks. */ |
| 4873 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
| 4874 | h1_parse_xfer_enc_header(&h1m, ist2(name, nlen)); |
| 4875 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4876 | struct ist v = ist2(value, vlen); |
| 4877 | int ret; |
| 4878 | |
| 4879 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4880 | if (ret < 0) { |
| 4881 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4882 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4883 | name); |
| 4884 | WILL_LJMP(lua_error(L)); |
| 4885 | } |
| 4886 | else if (ret == 0) |
| 4887 | goto next; /* Skip it */ |
| 4888 | } |
| 4889 | |
| 4890 | /* Add a new header */ |
| 4891 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4892 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4893 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4894 | name); |
| 4895 | WILL_LJMP(lua_error(L)); |
| 4896 | } |
| 4897 | next: |
| 4898 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4899 | lua_pop(L, 1); |
| 4900 | } |
| 4901 | |
| 4902 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4903 | lua_pop(L, 1); |
| 4904 | } |
| 4905 | |
| 4906 | if (h1m.flags & H1_MF_CHNK) |
| 4907 | h1m.flags &= ~H1_MF_CLEN; |
| 4908 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4909 | h1m.flags |= H1_MF_XFER_LEN; |
| 4910 | |
| 4911 | /* Uset HTX start-line flags */ |
| 4912 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4913 | flags |= HTX_SL_F_XFER_ENC; |
| 4914 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4915 | flags |= HTX_SL_F_XFER_LEN; |
| 4916 | if (h1m.flags & H1_MF_CHNK) |
| 4917 | flags |= HTX_SL_F_CHNK; |
| 4918 | else if (h1m.flags & H1_MF_CLEN) |
| 4919 | flags |= HTX_SL_F_CLEN; |
| 4920 | if (h1m.body_len == 0) |
| 4921 | flags |= HTX_SL_F_BODYLESS; |
| 4922 | } |
| 4923 | sl->flags |= flags; |
| 4924 | |
| 4925 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4926 | * and the status code implies the presence of a message body, we must |
| 4927 | * announce a transfer encoding chunked. This is required by haproxy |
| 4928 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4929 | * chunked itslef, don't do anything. |
| 4930 | */ |
| 4931 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4932 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4933 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4934 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4935 | /* Add a new header */ |
| 4936 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4937 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4938 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4939 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4940 | WILL_LJMP(lua_error(L)); |
| 4941 | } |
| 4942 | } |
| 4943 | |
| 4944 | /* Finalize headers. */ |
| 4945 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4946 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4947 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4948 | WILL_LJMP(lua_error(L)); |
| 4949 | } |
| 4950 | |
| 4951 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4952 | b_reset(&res->buf); |
| 4953 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4954 | WILL_LJMP(lua_error(L)); |
| 4955 | } |
| 4956 | |
| 4957 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4958 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4959 | |
| 4960 | /* Headers sent, set the flag. */ |
| 4961 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4962 | return 0; |
| 4963 | |
| 4964 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 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 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4969 | __LJMP static int hlua_applet_htx_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 *res = si_ic(si); |
| 4974 | |
| 4975 | if (co_data(res)) { |
| 4976 | si_rx_room_blk(si); |
| 4977 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0)); |
| 4978 | } |
| 4979 | return MAY_LJMP(hlua_applet_htx_send_response(L)); |
| 4980 | } |
| 4981 | |
| 4982 | |
| 4983 | __LJMP static int hlua_applet_htx_start_response(lua_State *L) |
| 4984 | { |
| 4985 | return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0)); |
| 4986 | } |
| 4987 | |
| 4988 | /* We will build the status line and the headers of the HTTP response. |
| 4989 | * We will try send at once if its not possible, we give back the hand |
| 4990 | * waiting for more room. |
| 4991 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4992 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4993 | { |
| 4994 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4995 | struct stream_interface *si = appctx->appctx->owner; |
| 4996 | struct channel *chn = si_ic(si); |
| 4997 | int ret; |
| 4998 | size_t len; |
| 4999 | const char *msg; |
| 5000 | |
| 5001 | /* Get the message as the first argument on the stack. */ |
| 5002 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5003 | |
| 5004 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 5005 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5006 | |
| 5007 | /* if ret == -2 or -3 the channel closed or the message si too |
| 5008 | * big for the buffers. |
| 5009 | */ |
| 5010 | if (ret == -2 || ret == -3) { |
| 5011 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5012 | WILL_LJMP(lua_error(L)); |
| 5013 | } |
| 5014 | |
| 5015 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 5016 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 5017 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5018 | 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] | 5019 | } |
| 5020 | |
| 5021 | /* Headers sent, set the flag. */ |
| 5022 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 5023 | return 0; |
| 5024 | } |
| 5025 | |
| 5026 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 5027 | { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5028 | struct buffer *tmp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5029 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5030 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5031 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5032 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5033 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5034 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5035 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5036 | int hdr_chunked = 0; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5037 | const char *reason; |
| 5038 | |
| 5039 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 5040 | return MAY_LJMP(hlua_applet_htx_start_response(L)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5041 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5042 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5043 | if (reason == NULL) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 5044 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5045 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5046 | tmp = get_trash_chunk(); |
| 5047 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5048 | /* Use the same http version than the request. */ |
| 5049 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 5050 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5051 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5052 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5053 | |
| 5054 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5055 | lua_pushvalue(L, 0); |
| 5056 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5057 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 5058 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 5059 | WILL_LJMP(lua_error(L)); |
| 5060 | } |
| 5061 | |
| 5062 | /* Browse the list of headers. */ |
| 5063 | lua_pushnil(L); |
| 5064 | while(lua_next(L, -2) != 0) { |
| 5065 | |
| 5066 | /* We expect a string as -2. */ |
| 5067 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5068 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 5069 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5070 | lua_typename(L, lua_type(L, -2))); |
| 5071 | WILL_LJMP(lua_error(L)); |
| 5072 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5073 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5074 | |
| 5075 | /* We expect an array as -1. */ |
| 5076 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5077 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 5078 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5079 | name, |
| 5080 | lua_typename(L, lua_type(L, -1))); |
| 5081 | WILL_LJMP(lua_error(L)); |
| 5082 | } |
| 5083 | |
| 5084 | /* Browse the table who is on the top of the stack. */ |
| 5085 | lua_pushnil(L); |
| 5086 | while(lua_next(L, -2) != 0) { |
| 5087 | |
| 5088 | /* We expect a number as -2. */ |
| 5089 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5090 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 5091 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5092 | name, |
| 5093 | lua_typename(L, lua_type(L, -2))); |
| 5094 | WILL_LJMP(lua_error(L)); |
| 5095 | } |
| 5096 | id = lua_tointeger(L, -2); |
| 5097 | |
| 5098 | /* We expect a string as -2. */ |
| 5099 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5100 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 5101 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5102 | name, id, |
| 5103 | lua_typename(L, lua_type(L, -1))); |
| 5104 | WILL_LJMP(lua_error(L)); |
| 5105 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5106 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5107 | |
| 5108 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5109 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 5110 | memcpy(tmp->area + tmp->data, name, name_len); |
| 5111 | tmp->data += name_len; |
| 5112 | tmp->area[tmp->data++] = ':'; |
| 5113 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5114 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5115 | memcpy(tmp->area + tmp->data, value, |
| 5116 | value_len); |
| 5117 | tmp->data += value_len; |
| 5118 | tmp->area[tmp->data++] = '\r'; |
| 5119 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5120 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5121 | |
| 5122 | /* Protocol checks. */ |
| 5123 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5124 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5125 | * is done without control. If it contains a bad value, |
| 5126 | * the content-length remains negative so that we can |
| 5127 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5128 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5129 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5130 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5131 | |
| 5132 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5133 | if (name_len == 17 && value_len == 7 && |
| 5134 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5135 | strcasecmp("chunked", value) == 0) |
| 5136 | hdr_chunked = 1; |
| 5137 | |
| 5138 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5139 | lua_pop(L, 1); |
| 5140 | } |
| 5141 | |
| 5142 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5143 | lua_pop(L, 1); |
| 5144 | } |
| 5145 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5146 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 5147 | * and the status code implies the presence of a message body, we must |
| 5148 | * announce a transfer encoding chunked. This is required by haproxy |
| 5149 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 5150 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5151 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5152 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5153 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 5154 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5155 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5156 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5157 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 5158 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 5159 | } |
| 5160 | |
| 5161 | /* Finalize headers. */ |
| 5162 | chunk_appendf(tmp, "\r\n"); |
| 5163 | |
| 5164 | /* Remove the last entry and the array of headers */ |
| 5165 | lua_pop(L, 2); |
| 5166 | |
| 5167 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5168 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5169 | |
| 5170 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 5171 | } |
| 5172 | |
| 5173 | /* |
| 5174 | * |
| 5175 | * |
| 5176 | * Class HTTP |
| 5177 | * |
| 5178 | * |
| 5179 | */ |
| 5180 | |
| 5181 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5182 | * a class stream, otherwise it throws an error. |
| 5183 | */ |
| 5184 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 5185 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5186 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5187 | } |
| 5188 | |
| 5189 | /* This function creates and push in the stack a HTTP object |
| 5190 | * according with a current TXN. |
| 5191 | */ |
| 5192 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5193 | { |
| 5194 | struct hlua_txn *htxn; |
| 5195 | |
| 5196 | /* Check stack size. */ |
| 5197 | if (!lua_checkstack(L, 3)) |
| 5198 | return 0; |
| 5199 | |
| 5200 | /* Create the object: obj[0] = userdata. |
| 5201 | * Note that the base of the Converters object is the |
| 5202 | * same than the TXN object. |
| 5203 | */ |
| 5204 | lua_newtable(L); |
| 5205 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5206 | lua_rawseti(L, -2, 0); |
| 5207 | |
| 5208 | htxn->s = txn->s; |
| 5209 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5210 | htxn->dir = txn->dir; |
| 5211 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5212 | |
| 5213 | /* Pop a class stream metatable and affect it to the table. */ |
| 5214 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5215 | lua_setmetatable(L, -2); |
| 5216 | |
| 5217 | return 1; |
| 5218 | } |
| 5219 | |
| 5220 | /* This function creates ans returns an array of HTTP headers. |
| 5221 | * This function does not fails. It is used as wrapper with the |
| 5222 | * 2 following functions. |
| 5223 | */ |
| 5224 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5225 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5226 | /* Create the table. */ |
| 5227 | lua_newtable(L); |
| 5228 | |
| 5229 | if (!htxn->s->txn) |
| 5230 | return 1; |
| 5231 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5232 | if (IS_HTX_STRM(htxn->s)) { |
| 5233 | /* HTX version */ |
| 5234 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5235 | int32_t pos; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5236 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5237 | 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] | 5238 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5239 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5240 | struct ist n, v; |
| 5241 | int len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5242 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5243 | if (type == HTX_BLK_HDR) { |
| 5244 | n = htx_get_blk_name(htx,blk); |
| 5245 | v = htx_get_blk_value(htx, blk); |
| 5246 | } |
| 5247 | else if (type == HTX_BLK_EOH) |
| 5248 | break; |
| 5249 | else |
| 5250 | continue; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5251 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5252 | /* Check for existing entry: |
| 5253 | * assume that the table is on the top of the stack, and |
| 5254 | * push the key in the stack, the function lua_gettable() |
| 5255 | * perform the lookup. |
| 5256 | */ |
| 5257 | lua_pushlstring(L, n.ptr, n.len); |
| 5258 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5259 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5260 | switch (lua_type(L, -1)) { |
| 5261 | case LUA_TNIL: |
| 5262 | /* Table not found, create it. */ |
| 5263 | lua_pop(L, 1); /* remove the nil value. */ |
| 5264 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5265 | lua_newtable(L); /* create and push empty table. */ |
| 5266 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5267 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5268 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5269 | break; |
| 5270 | |
| 5271 | case LUA_TTABLE: |
| 5272 | /* Entry found: push the value in the table. */ |
| 5273 | len = lua_rawlen(L, -1); |
| 5274 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5275 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5276 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5277 | break; |
| 5278 | |
| 5279 | default: |
| 5280 | /* Other cases are errors. */ |
| 5281 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5282 | WILL_LJMP(lua_error(L)); |
| 5283 | } |
| 5284 | } |
| 5285 | } |
| 5286 | else { |
| 5287 | /* Legacy HTTP version */ |
| 5288 | const char *cur_ptr, *cur_next, *p; |
| 5289 | int old_idx, cur_idx; |
| 5290 | struct hdr_idx_elem *cur_hdr; |
| 5291 | const char *hn, *hv; |
| 5292 | int hnl, hvl; |
| 5293 | const char *in; |
| 5294 | char *out; |
| 5295 | int len; |
| 5296 | |
| 5297 | /* Build array of headers. */ |
| 5298 | old_idx = 0; |
| 5299 | cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 5300 | |
| 5301 | while (1) { |
| 5302 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 5303 | if (!cur_idx) |
| 5304 | break; |
| 5305 | old_idx = cur_idx; |
| 5306 | |
| 5307 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 5308 | cur_ptr = cur_next; |
| 5309 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 5310 | |
| 5311 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 5312 | * and the next header starts at cur_next. We'll check |
| 5313 | * this header in the list as well as against the default |
| 5314 | * rule. |
| 5315 | */ |
| 5316 | |
| 5317 | /* look for ': *'. */ |
| 5318 | hn = cur_ptr; |
| 5319 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 5320 | if (p >= cur_ptr+cur_hdr->len) |
| 5321 | continue; |
| 5322 | hnl = p - hn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5323 | p++; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5324 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 5325 | p++; |
| 5326 | if (p >= cur_ptr+cur_hdr->len) |
| 5327 | continue; |
| 5328 | hv = p; |
| 5329 | hvl = cur_ptr+cur_hdr->len-p; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5330 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5331 | /* Lowercase the key. Don't check the size of trash, it have |
| 5332 | * the size of one buffer and the input data contains in one |
| 5333 | * buffer. |
| 5334 | */ |
| 5335 | out = trash.area; |
| 5336 | for (in=hn; in<hn+hnl; in++, out++) |
| 5337 | *out = tolower(*in); |
| 5338 | *out = '\0'; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5339 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5340 | /* Check for existing entry: |
| 5341 | * assume that the table is on the top of the stack, and |
| 5342 | * push the key in the stack, the function lua_gettable() |
| 5343 | * perform the lookup. |
| 5344 | */ |
| 5345 | lua_pushlstring(L, trash.area, hnl); |
| 5346 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5347 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5348 | switch (lua_type(L, -1)) { |
| 5349 | case LUA_TNIL: |
| 5350 | /* Table not found, create it. */ |
| 5351 | lua_pop(L, 1); /* remove the nil value. */ |
| 5352 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
| 5353 | lua_newtable(L); /* create and push empty table. */ |
| 5354 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5355 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5356 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5357 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5358 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5359 | case LUA_TTABLE: |
| 5360 | /* Entry found: push the value in the table. */ |
| 5361 | len = lua_rawlen(L, -1); |
| 5362 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5363 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5364 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5365 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5366 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5367 | default: |
| 5368 | /* Other cases are errors. */ |
| 5369 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5370 | WILL_LJMP(lua_error(L)); |
| 5371 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5372 | } |
| 5373 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5374 | return 1; |
| 5375 | } |
| 5376 | |
| 5377 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5378 | { |
| 5379 | struct hlua_txn *htxn; |
| 5380 | |
| 5381 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5382 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5383 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5384 | 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] | 5385 | WILL_LJMP(lua_error(L)); |
| 5386 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5387 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 5388 | } |
| 5389 | |
| 5390 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5391 | { |
| 5392 | struct hlua_txn *htxn; |
| 5393 | |
| 5394 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5395 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5396 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5397 | 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] | 5398 | WILL_LJMP(lua_error(L)); |
| 5399 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5400 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 5401 | } |
| 5402 | |
| 5403 | /* This function replace full header, or just a value in |
| 5404 | * the request or in the response. It is a wrapper fir the |
| 5405 | * 4 following functions. |
| 5406 | */ |
| 5407 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 5408 | struct http_msg *msg, int action) |
| 5409 | { |
| 5410 | size_t name_len; |
| 5411 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5412 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5413 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5414 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5415 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5416 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5417 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5418 | |
Christopher Faulet | 5a8549e | 2019-06-17 13:36:06 +0200 | [diff] [blame] | 5419 | if (IS_HTX_STRM(htxn->s)) { |
| 5420 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5421 | |
| 5422 | htx_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action); |
| 5423 | } |
| 5424 | else |
| 5425 | 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] | 5426 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5427 | return 0; |
| 5428 | } |
| 5429 | |
| 5430 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5431 | { |
| 5432 | struct hlua_txn *htxn; |
| 5433 | |
| 5434 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5435 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5436 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5437 | 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] | 5438 | WILL_LJMP(lua_error(L)); |
| 5439 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5440 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 5441 | } |
| 5442 | |
| 5443 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5444 | { |
| 5445 | struct hlua_txn *htxn; |
| 5446 | |
| 5447 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5448 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5449 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5450 | 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] | 5451 | WILL_LJMP(lua_error(L)); |
| 5452 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5453 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 5454 | } |
| 5455 | |
| 5456 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5457 | { |
| 5458 | struct hlua_txn *htxn; |
| 5459 | |
| 5460 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5461 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5462 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5463 | 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] | 5464 | WILL_LJMP(lua_error(L)); |
| 5465 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5466 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 5467 | } |
| 5468 | |
| 5469 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5470 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5471 | struct hlua_txn *htxn; |
| 5472 | |
| 5473 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5474 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5475 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5476 | 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] | 5477 | WILL_LJMP(lua_error(L)); |
| 5478 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 5479 | 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] | 5480 | } |
| 5481 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5482 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5483 | * It is a wrapper for the 2 following functions. |
| 5484 | */ |
| 5485 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5486 | { |
| 5487 | size_t len; |
| 5488 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5489 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5490 | if (IS_HTX_STRM(htxn->s)) { |
| 5491 | /* HTX version */ |
| 5492 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5493 | struct http_hdr_ctx ctx; |
| 5494 | |
| 5495 | ctx.blk = NULL; |
| 5496 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5497 | http_remove_header(htx, &ctx); |
| 5498 | } |
| 5499 | else { |
| 5500 | /* Legacy HTTP version */ |
| 5501 | struct hdr_ctx ctx; |
| 5502 | struct http_txn *txn = htxn->s->txn; |
| 5503 | |
| 5504 | ctx.idx = 0; |
| 5505 | while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) |
| 5506 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5507 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5508 | return 0; |
| 5509 | } |
| 5510 | |
| 5511 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5512 | { |
| 5513 | struct hlua_txn *htxn; |
| 5514 | |
| 5515 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5516 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5517 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5518 | 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] | 5519 | WILL_LJMP(lua_error(L)); |
| 5520 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5521 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5522 | } |
| 5523 | |
| 5524 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5525 | { |
| 5526 | struct hlua_txn *htxn; |
| 5527 | |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5528 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5529 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5530 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5531 | 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] | 5532 | WILL_LJMP(lua_error(L)); |
| 5533 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5534 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5535 | } |
| 5536 | |
| 5537 | /* This function adds an header. It is a wrapper used by |
| 5538 | * the 2 following functions. |
| 5539 | */ |
| 5540 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5541 | { |
| 5542 | size_t name_len; |
| 5543 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5544 | size_t value_len; |
| 5545 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 5546 | char *p; |
| 5547 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5548 | if (IS_HTX_STRM(htxn->s)) { |
| 5549 | /* HTX version */ |
| 5550 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5551 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5552 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5553 | ist2(value, value_len))); |
| 5554 | } |
| 5555 | else { |
| 5556 | /* Legacy HTTP version */ |
| 5557 | /* Check length. */ |
| 5558 | trash.data = value_len + name_len + 2; |
| 5559 | if (trash.data > trash.size) |
| 5560 | return 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5561 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5562 | /* Creates the header string. */ |
| 5563 | p = trash.area; |
| 5564 | memcpy(p, name, name_len); |
| 5565 | p += name_len; |
| 5566 | *p = ':'; |
| 5567 | p++; |
| 5568 | *p = ' '; |
| 5569 | p++; |
| 5570 | memcpy(p, value, value_len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5571 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5572 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
| 5573 | trash.area, trash.data) != 0); |
| 5574 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5575 | return 0; |
| 5576 | } |
| 5577 | |
| 5578 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5579 | { |
| 5580 | struct hlua_txn *htxn; |
| 5581 | |
| 5582 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5583 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5584 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5585 | 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] | 5586 | WILL_LJMP(lua_error(L)); |
| 5587 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5588 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5589 | } |
| 5590 | |
| 5591 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5592 | { |
| 5593 | struct hlua_txn *htxn; |
| 5594 | |
| 5595 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5596 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5597 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5598 | 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] | 5599 | WILL_LJMP(lua_error(L)); |
| 5600 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5601 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5602 | } |
| 5603 | |
| 5604 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5605 | { |
| 5606 | struct hlua_txn *htxn; |
| 5607 | |
| 5608 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5609 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5610 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5611 | 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] | 5612 | WILL_LJMP(lua_error(L)); |
| 5613 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5614 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 5615 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5616 | } |
| 5617 | |
| 5618 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5619 | { |
| 5620 | struct hlua_txn *htxn; |
| 5621 | |
| 5622 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5623 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5624 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5625 | 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] | 5626 | WILL_LJMP(lua_error(L)); |
| 5627 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5628 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 5629 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5630 | } |
| 5631 | |
| 5632 | /* This function set the method. */ |
| 5633 | static int hlua_http_req_set_meth(lua_State *L) |
| 5634 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5635 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5636 | size_t name_len; |
| 5637 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5638 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5639 | 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] | 5640 | WILL_LJMP(lua_error(L)); |
| 5641 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5642 | 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] | 5643 | return 1; |
| 5644 | } |
| 5645 | |
| 5646 | /* This function set the method. */ |
| 5647 | static int hlua_http_req_set_path(lua_State *L) |
| 5648 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5649 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5650 | size_t name_len; |
| 5651 | 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] | 5652 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5653 | 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] | 5654 | WILL_LJMP(lua_error(L)); |
| 5655 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5656 | 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] | 5657 | return 1; |
| 5658 | } |
| 5659 | |
| 5660 | /* This function set the query-string. */ |
| 5661 | static int hlua_http_req_set_query(lua_State *L) |
| 5662 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5663 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5664 | size_t name_len; |
| 5665 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5666 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5667 | 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] | 5668 | WILL_LJMP(lua_error(L)); |
| 5669 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5670 | /* Check length. */ |
| 5671 | if (name_len > trash.size - 1) { |
| 5672 | lua_pushboolean(L, 0); |
| 5673 | return 1; |
| 5674 | } |
| 5675 | |
| 5676 | /* Add the mark question as prefix. */ |
| 5677 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5678 | trash.area[trash.data++] = '?'; |
| 5679 | memcpy(trash.area + trash.data, name, name_len); |
| 5680 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5681 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5682 | lua_pushboolean(L, |
| 5683 | 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] | 5684 | return 1; |
| 5685 | } |
| 5686 | |
| 5687 | /* This function set the uri. */ |
| 5688 | static int hlua_http_req_set_uri(lua_State *L) |
| 5689 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5690 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5691 | size_t name_len; |
| 5692 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5693 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5694 | 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] | 5695 | WILL_LJMP(lua_error(L)); |
| 5696 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5697 | 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] | 5698 | return 1; |
| 5699 | } |
| 5700 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5701 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5702 | static int hlua_http_res_set_status(lua_State *L) |
| 5703 | { |
| 5704 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5705 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5706 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5707 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5708 | 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] | 5709 | WILL_LJMP(lua_error(L)); |
| 5710 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5711 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5712 | return 0; |
| 5713 | } |
| 5714 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5715 | /* |
| 5716 | * |
| 5717 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5718 | * Class TXN |
| 5719 | * |
| 5720 | * |
| 5721 | */ |
| 5722 | |
| 5723 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5724 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5725 | */ |
| 5726 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5727 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5728 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5729 | } |
| 5730 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5731 | __LJMP static int hlua_set_var(lua_State *L) |
| 5732 | { |
| 5733 | struct hlua_txn *htxn; |
| 5734 | const char *name; |
| 5735 | size_t len; |
| 5736 | struct sample smp; |
| 5737 | |
| 5738 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5739 | |
| 5740 | /* It is useles to retrieve the stream, but this function |
| 5741 | * runs only in a stream context. |
| 5742 | */ |
| 5743 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5744 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5745 | |
| 5746 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | ca492f3 | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5747 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5748 | hlua_lua2smp(L, 3, &smp); |
| 5749 | |
| 5750 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5751 | 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] | 5752 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5753 | return 0; |
| 5754 | } |
| 5755 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5756 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5757 | { |
| 5758 | struct hlua_txn *htxn; |
| 5759 | const char *name; |
| 5760 | size_t len; |
| 5761 | struct sample smp; |
| 5762 | |
| 5763 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5764 | |
| 5765 | /* It is useles to retrieve the stream, but this function |
| 5766 | * runs only in a stream context. |
| 5767 | */ |
| 5768 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5769 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5770 | |
| 5771 | /* Unset the variable. */ |
| 5772 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5773 | vars_unset_by_name(name, len, &smp); |
| 5774 | return 0; |
| 5775 | } |
| 5776 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5777 | __LJMP static int hlua_get_var(lua_State *L) |
| 5778 | { |
| 5779 | struct hlua_txn *htxn; |
| 5780 | const char *name; |
| 5781 | size_t len; |
| 5782 | struct sample smp; |
| 5783 | |
| 5784 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5785 | |
| 5786 | /* It is useles to retrieve the stream, but this function |
| 5787 | * runs only in a stream context. |
| 5788 | */ |
| 5789 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5790 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5791 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5792 | 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] | 5793 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5794 | lua_pushnil(L); |
| 5795 | return 1; |
| 5796 | } |
| 5797 | |
| 5798 | return hlua_smp2lua(L, &smp); |
| 5799 | } |
| 5800 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5801 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5802 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5803 | struct hlua *hlua; |
| 5804 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5805 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5806 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5807 | /* It is useles to retrieve the stream, but this function |
| 5808 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5809 | */ |
| 5810 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5811 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5812 | |
| 5813 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5814 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5815 | |
| 5816 | /* Get and store new value. */ |
| 5817 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5818 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5819 | |
| 5820 | return 0; |
| 5821 | } |
| 5822 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5823 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5824 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5825 | struct hlua *hlua; |
| 5826 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5827 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5828 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5829 | /* It is useles to retrieve the stream, but this function |
| 5830 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5831 | */ |
| 5832 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5833 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5834 | |
| 5835 | /* Push configuration index in the stack. */ |
| 5836 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5837 | |
| 5838 | return 1; |
| 5839 | } |
| 5840 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5841 | /* Create stack entry containing a class TXN. This function |
| 5842 | * return 0 if the stack does not contains free slots, |
| 5843 | * otherwise it returns 1. |
| 5844 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5845 | 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] | 5846 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5847 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5848 | |
| 5849 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5850 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5851 | return 0; |
| 5852 | |
| 5853 | /* NOTE: The allocation never fails. The failure |
| 5854 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5855 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5856 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5857 | /* Create the object: obj[0] = userdata. */ |
| 5858 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5859 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5860 | lua_rawseti(L, -2, 0); |
| 5861 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5862 | htxn->s = s; |
| 5863 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5864 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5865 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5866 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5867 | /* Create the "f" field that contains a list of fetches. */ |
| 5868 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5869 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +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 | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5872 | |
| 5873 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5874 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5875 | 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] | 5876 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5877 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5878 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5879 | /* Create the "c" field that contains a list of converters. */ |
| 5880 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5881 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5882 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5883 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5884 | |
| 5885 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5886 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5887 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5888 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5889 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5890 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5891 | /* Create the "req" field that contains the request channel object. */ |
| 5892 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5893 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5894 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5895 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5896 | |
| 5897 | /* Create the "res" field that contains the response channel object. */ |
| 5898 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5899 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5900 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5901 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5902 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5903 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5904 | lua_pushstring(L, "http"); |
| 5905 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5906 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5907 | return 0; |
| 5908 | } |
| 5909 | else |
| 5910 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5911 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5912 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5913 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5914 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5915 | lua_setmetatable(L, -2); |
| 5916 | |
| 5917 | return 1; |
| 5918 | } |
| 5919 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5920 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5921 | { |
| 5922 | const char *msg; |
| 5923 | struct hlua_txn *htxn; |
| 5924 | |
| 5925 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5926 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5927 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5928 | |
| 5929 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5930 | return 0; |
| 5931 | } |
| 5932 | |
| 5933 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5934 | { |
| 5935 | int level; |
| 5936 | const char *msg; |
| 5937 | struct hlua_txn *htxn; |
| 5938 | |
| 5939 | MAY_LJMP(check_args(L, 3, "log")); |
| 5940 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5941 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5942 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5943 | |
| 5944 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5945 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5946 | |
| 5947 | hlua_sendlog(htxn->s->be, level, msg); |
| 5948 | return 0; |
| 5949 | } |
| 5950 | |
| 5951 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5952 | { |
| 5953 | const char *msg; |
| 5954 | struct hlua_txn *htxn; |
| 5955 | |
| 5956 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5957 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5958 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5959 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5960 | return 0; |
| 5961 | } |
| 5962 | |
| 5963 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5964 | { |
| 5965 | const char *msg; |
| 5966 | struct hlua_txn *htxn; |
| 5967 | |
| 5968 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5969 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5970 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5971 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5972 | return 0; |
| 5973 | } |
| 5974 | |
| 5975 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5976 | { |
| 5977 | const char *msg; |
| 5978 | struct hlua_txn *htxn; |
| 5979 | |
| 5980 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5981 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5982 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5983 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5984 | return 0; |
| 5985 | } |
| 5986 | |
| 5987 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5988 | { |
| 5989 | const char *msg; |
| 5990 | struct hlua_txn *htxn; |
| 5991 | |
| 5992 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5993 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5994 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5995 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5996 | return 0; |
| 5997 | } |
| 5998 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5999 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 6000 | { |
| 6001 | struct hlua_txn *htxn; |
| 6002 | int ll; |
| 6003 | |
| 6004 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 6005 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6006 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6007 | |
| 6008 | if (ll < 0 || ll > 7) |
| 6009 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 6010 | |
| 6011 | htxn->s->logs.level = ll; |
| 6012 | return 0; |
| 6013 | } |
| 6014 | |
| 6015 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 6016 | { |
| 6017 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6018 | int tos; |
| 6019 | |
| 6020 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 6021 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6022 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6023 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 6024 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6025 | return 0; |
| 6026 | } |
| 6027 | |
| 6028 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 6029 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6030 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6031 | int mark; |
| 6032 | |
| 6033 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 6034 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6035 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6036 | |
Lukas Tribus | b59291a | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 6037 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6038 | return 0; |
| 6039 | } |
| 6040 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 6041 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 6042 | { |
| 6043 | struct hlua_txn *htxn; |
| 6044 | |
| 6045 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 6046 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6047 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6048 | return 0; |
| 6049 | } |
| 6050 | |
| 6051 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 6052 | { |
| 6053 | struct hlua_txn *htxn; |
| 6054 | |
| 6055 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 6056 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6057 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6058 | return 0; |
| 6059 | } |
| 6060 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6061 | /* This function is an Lua binding that send pending data |
| 6062 | * to the client, and close the stream interface. |
| 6063 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6064 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6065 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6066 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6067 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6068 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6069 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6070 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6071 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6072 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6073 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6074 | /* If the flags NOTERM is set, we cannot terminate the http |
| 6075 | * session, so we just end the execution of the current |
| 6076 | * lua code. |
| 6077 | */ |
| 6078 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 6079 | WILL_LJMP(hlua_done(L)); |
| 6080 | return 0; |
| 6081 | } |
| 6082 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6083 | ic = &htxn->s->req; |
| 6084 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6085 | |
Christopher Faulet | 72c6927 | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6086 | if (IS_HTX_STRM(htxn->s)) { |
| 6087 | htxn->s->txn->status = 0; |
| 6088 | http_reply_and_close(htxn->s, 0, NULL); |
| 6089 | ic->analysers &= AN_REQ_FLT_END; |
| 6090 | oc->analysers &= AN_RES_FLT_END; |
| 6091 | } |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6092 | else { |
| 6093 | if (htxn->s->txn) { |
| 6094 | /* HTTP mode, let's stay in sync with the stream */ |
| 6095 | b_del(&ic->buf, htxn->s->txn->req.sov); |
| 6096 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 6097 | htxn->s->txn->req.sov = 0; |
| 6098 | |
| 6099 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 6100 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 6101 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 6102 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6103 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6104 | /* Note that if we want to support keep-alive, we need |
| 6105 | * to bypass the close/shutr_now calls below, but that |
| 6106 | * may only be done if the HTTP request was already |
| 6107 | * processed and the connection header is known (ie |
| 6108 | * not during TCP rules). |
| 6109 | */ |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6110 | } |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6111 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6112 | channel_auto_read(ic); |
| 6113 | channel_abort(ic); |
| 6114 | channel_auto_close(ic); |
| 6115 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6116 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6117 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 6118 | channel_auto_read(oc); |
| 6119 | channel_auto_close(oc); |
| 6120 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6121 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6122 | ic->analysers = 0; |
| 6123 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6124 | |
Christopher Faulet | 72c6927 | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6125 | if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 6126 | htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 6127 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6128 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6129 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6130 | return 0; |
| 6131 | } |
| 6132 | |
| 6133 | __LJMP static int hlua_log(lua_State *L) |
| 6134 | { |
| 6135 | int level; |
| 6136 | const char *msg; |
| 6137 | |
| 6138 | MAY_LJMP(check_args(L, 2, "log")); |
| 6139 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6140 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6141 | |
| 6142 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6143 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6144 | |
| 6145 | hlua_sendlog(NULL, level, msg); |
| 6146 | return 0; |
| 6147 | } |
| 6148 | |
| 6149 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6150 | { |
| 6151 | const char *msg; |
| 6152 | |
| 6153 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6154 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6155 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6156 | return 0; |
| 6157 | } |
| 6158 | |
| 6159 | __LJMP static int hlua_log_info(lua_State *L) |
| 6160 | { |
| 6161 | const char *msg; |
| 6162 | |
| 6163 | MAY_LJMP(check_args(L, 1, "info")); |
| 6164 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6165 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6166 | return 0; |
| 6167 | } |
| 6168 | |
| 6169 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6170 | { |
| 6171 | const char *msg; |
| 6172 | |
| 6173 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6174 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6175 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6176 | return 0; |
| 6177 | } |
| 6178 | |
| 6179 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6180 | { |
| 6181 | const char *msg; |
| 6182 | |
| 6183 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6184 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6185 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6186 | return 0; |
| 6187 | } |
| 6188 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6189 | __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] | 6190 | { |
| 6191 | int wakeup_ms = lua_tointeger(L, -1); |
| 6192 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6193 | 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] | 6194 | return 0; |
| 6195 | } |
| 6196 | |
| 6197 | __LJMP static int hlua_sleep(lua_State *L) |
| 6198 | { |
| 6199 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6200 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6201 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6202 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6203 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6204 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6205 | wakeup_ms = tick_add(now_ms, delay); |
| 6206 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6207 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6208 | 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] | 6209 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6210 | } |
| 6211 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6212 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6213 | { |
| 6214 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6215 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6216 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6217 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6218 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6219 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6220 | wakeup_ms = tick_add(now_ms, delay); |
| 6221 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6222 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6223 | 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] | 6224 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6225 | } |
| 6226 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6227 | /* This functionis an LUA binding. it permits to give back |
| 6228 | * the hand at the HAProxy scheduler. It is used when the |
| 6229 | * LUA processing consumes a lot of time. |
| 6230 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6231 | __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] | 6232 | { |
| 6233 | return 0; |
| 6234 | } |
| 6235 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6236 | __LJMP static int hlua_yield(lua_State *L) |
| 6237 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6238 | 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] | 6239 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6240 | } |
| 6241 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6242 | /* This function change the nice of the currently executed |
| 6243 | * task. It is used set low or high priority at the current |
| 6244 | * task. |
| 6245 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6246 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6247 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6248 | struct hlua *hlua; |
| 6249 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6250 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6251 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6252 | hlua = hlua_gethlua(L); |
| 6253 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6254 | |
| 6255 | /* If he task is not set, I'm in a start mode. */ |
| 6256 | if (!hlua || !hlua->task) |
| 6257 | return 0; |
| 6258 | |
| 6259 | if (nice < -1024) |
| 6260 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6261 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6262 | nice = 1024; |
| 6263 | |
| 6264 | hlua->task->nice = nice; |
| 6265 | return 0; |
| 6266 | } |
| 6267 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6268 | /* 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] | 6269 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6270 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6271 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6272 | * |
| 6273 | * Task wrapper are longjmp safe because the only one Lua code |
| 6274 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6275 | */ |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6276 | 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] | 6277 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6278 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6279 | enum hlua_exec status; |
| 6280 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6281 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6282 | task_set_affinity(task, tid_bit); |
| 6283 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6284 | /* If it is the first call to the task, we must initialize the |
| 6285 | * execution timeouts. |
| 6286 | */ |
| 6287 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6288 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6289 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6290 | /* Execute the Lua code. */ |
| 6291 | status = hlua_ctx_resume(hlua, 1); |
| 6292 | |
| 6293 | switch (status) { |
| 6294 | /* finished or yield */ |
| 6295 | case HLUA_E_OK: |
| 6296 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6297 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6298 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6299 | break; |
| 6300 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6301 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6302 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6303 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6304 | break; |
| 6305 | |
| 6306 | /* finished with error. */ |
| 6307 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6308 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6309 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6310 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6311 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6312 | break; |
| 6313 | |
| 6314 | case HLUA_E_ERR: |
| 6315 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6316 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6317 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6318 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6319 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6320 | break; |
| 6321 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6322 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6323 | } |
| 6324 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6325 | /* This function is an LUA binding that register LUA function to be |
| 6326 | * executed after the HAProxy configuration parsing and before the |
| 6327 | * HAProxy scheduler starts. This function expect only one LUA |
| 6328 | * argument that is a function. This function returns nothing, but |
| 6329 | * throws if an error is encountered. |
| 6330 | */ |
| 6331 | __LJMP static int hlua_register_init(lua_State *L) |
| 6332 | { |
| 6333 | struct hlua_init_function *init; |
| 6334 | int ref; |
| 6335 | |
| 6336 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6337 | |
| 6338 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6339 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6340 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6341 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6342 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6343 | |
| 6344 | init->function_ref = ref; |
| 6345 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6346 | return 0; |
| 6347 | } |
| 6348 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6349 | /* This functio is an LUA binding. It permits to register a task |
| 6350 | * executed in parallel of the main HAroxy activity. The task is |
| 6351 | * created and it is set in the HAProxy scheduler. It can be called |
| 6352 | * from the "init" section, "post init" or during the runtime. |
| 6353 | * |
| 6354 | * Lua prototype: |
| 6355 | * |
| 6356 | * <none> core.register_task(<function>) |
| 6357 | */ |
| 6358 | static int hlua_register_task(lua_State *L) |
| 6359 | { |
| 6360 | struct hlua *hlua; |
| 6361 | struct task *task; |
| 6362 | int ref; |
| 6363 | |
| 6364 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6365 | |
| 6366 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6367 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6368 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6369 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6370 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6371 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6372 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6373 | if (!task) |
| 6374 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6375 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6376 | task->context = hlua; |
| 6377 | task->process = hlua_process_task; |
| 6378 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6379 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6380 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6381 | |
| 6382 | /* Restore the function in the stack. */ |
| 6383 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6384 | hlua->nargs = 0; |
| 6385 | |
| 6386 | /* Schedule task. */ |
| 6387 | task_schedule(task, now_ms); |
| 6388 | |
| 6389 | return 0; |
| 6390 | } |
| 6391 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6392 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6393 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6394 | * resume converters. |
| 6395 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6396 | 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] | 6397 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6398 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6399 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6400 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6401 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6402 | if (!stream) |
| 6403 | return 0; |
| 6404 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6405 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6406 | * Lua context can be not initialized. This behavior |
| 6407 | * permits to save performances because a systematic |
| 6408 | * Lua initialization cause 5% performances loss. |
| 6409 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6410 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6411 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6412 | if (!stream->hlua) { |
| 6413 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6414 | return 0; |
| 6415 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6416 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6417 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6418 | return 0; |
| 6419 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6420 | } |
| 6421 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6422 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6423 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6424 | |
| 6425 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6426 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6427 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6428 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6429 | else |
| 6430 | error = "critical error"; |
| 6431 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6432 | return 0; |
| 6433 | } |
| 6434 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6435 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6436 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6437 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6438 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6439 | return 0; |
| 6440 | } |
| 6441 | |
| 6442 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6443 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6444 | |
| 6445 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6446 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6447 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6448 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6449 | return 0; |
| 6450 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6451 | hlua_smp2lua(stream->hlua->T, smp); |
| 6452 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6453 | |
| 6454 | /* push keywords in the stack. */ |
| 6455 | if (arg_p) { |
| 6456 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6457 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6458 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6459 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6460 | return 0; |
| 6461 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6462 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6463 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6464 | } |
| 6465 | } |
| 6466 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6467 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6468 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6469 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6470 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6471 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6472 | } |
| 6473 | |
| 6474 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6475 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6476 | /* finished. */ |
| 6477 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6478 | /* If the stack is empty, the function fails. */ |
| 6479 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6480 | return 0; |
| 6481 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6482 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6483 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6484 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6485 | return 1; |
| 6486 | |
| 6487 | /* yield. */ |
| 6488 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6489 | 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] | 6490 | return 0; |
| 6491 | |
| 6492 | /* finished with error. */ |
| 6493 | case HLUA_E_ERRMSG: |
| 6494 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6495 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6496 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6497 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6498 | return 0; |
| 6499 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6500 | case HLUA_E_ETMOUT: |
| 6501 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6502 | return 0; |
| 6503 | |
| 6504 | case HLUA_E_NOMEM: |
| 6505 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6506 | return 0; |
| 6507 | |
| 6508 | case HLUA_E_YIELD: |
| 6509 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6510 | return 0; |
| 6511 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6512 | case HLUA_E_ERR: |
| 6513 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6514 | 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] | 6515 | |
| 6516 | default: |
| 6517 | return 0; |
| 6518 | } |
| 6519 | } |
| 6520 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6521 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6522 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6523 | * resume sample-fetches. This function will be called by the sample |
| 6524 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6525 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6526 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6527 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6528 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6529 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6530 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6531 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6532 | const struct buffer msg = { }; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6533 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6534 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6535 | if (!stream) |
| 6536 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6537 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6538 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6539 | * Lua context can be not initialized. This behavior |
| 6540 | * permits to save performances because a systematic |
| 6541 | * Lua initialization cause 5% performances loss. |
| 6542 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6543 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6544 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6545 | if (!stream->hlua) { |
| 6546 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6547 | return 0; |
| 6548 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6549 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6550 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6551 | return 0; |
| 6552 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6553 | } |
| 6554 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6555 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6556 | |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6557 | if (stream->be->mode == PR_MODE_HTTP) { |
| 6558 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
| 6559 | hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 6560 | else |
| 6561 | hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 6562 | } |
| 6563 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6564 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6565 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6566 | |
| 6567 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6568 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6569 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6570 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6571 | else |
| 6572 | error = "critical error"; |
| 6573 | 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] | 6574 | return 0; |
| 6575 | } |
| 6576 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6577 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6578 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6579 | 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] | 6580 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6581 | return 0; |
| 6582 | } |
| 6583 | |
| 6584 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6585 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6586 | |
| 6587 | /* push arguments in the stack. */ |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6588 | 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] | 6589 | 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] | 6590 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6591 | return 0; |
| 6592 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6593 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6594 | |
| 6595 | /* push keywords in the stack. */ |
| 6596 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6597 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6598 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6599 | 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] | 6600 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6601 | return 0; |
| 6602 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6603 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6604 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6605 | } |
| 6606 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6607 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6608 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6609 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6610 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6611 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6612 | } |
| 6613 | |
| 6614 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6615 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6616 | /* finished. */ |
| 6617 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6618 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6619 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6620 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6621 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6622 | /* If the stack is empty, the function fails. */ |
| 6623 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6624 | return 0; |
| 6625 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6626 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6627 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6628 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6629 | |
| 6630 | /* Set the end of execution flag. */ |
| 6631 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6632 | return 1; |
| 6633 | |
| 6634 | /* yield. */ |
| 6635 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6636 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6637 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6638 | 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] | 6639 | return 0; |
| 6640 | |
| 6641 | /* finished with error. */ |
| 6642 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6643 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6644 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6645 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6646 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6647 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6648 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6649 | return 0; |
| 6650 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6651 | case HLUA_E_ETMOUT: |
| 6652 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6653 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6654 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6655 | return 0; |
| 6656 | |
| 6657 | case HLUA_E_NOMEM: |
| 6658 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6659 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6660 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6661 | return 0; |
| 6662 | |
| 6663 | case HLUA_E_YIELD: |
| 6664 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6665 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6666 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6667 | return 0; |
| 6668 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6669 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6670 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6671 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6672 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6673 | 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] | 6674 | |
| 6675 | default: |
| 6676 | return 0; |
| 6677 | } |
| 6678 | } |
| 6679 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6680 | /* This function is an LUA binding used for registering |
| 6681 | * "sample-conv" functions. It expects a converter name used |
| 6682 | * in the haproxy configuration file, and an LUA function. |
| 6683 | */ |
| 6684 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6685 | { |
| 6686 | struct sample_conv_kw_list *sck; |
| 6687 | const char *name; |
| 6688 | int ref; |
| 6689 | int len; |
| 6690 | struct hlua_function *fcn; |
| 6691 | |
| 6692 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6693 | |
| 6694 | /* First argument : converter name. */ |
| 6695 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6696 | |
| 6697 | /* Second argument : lua function. */ |
| 6698 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6699 | |
| 6700 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6701 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6702 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6703 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6704 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6705 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6706 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6707 | |
| 6708 | /* Fill fcn. */ |
| 6709 | fcn->name = strdup(name); |
| 6710 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6711 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6712 | fcn->function_ref = ref; |
| 6713 | |
| 6714 | /* List head */ |
| 6715 | sck->list.n = sck->list.p = NULL; |
| 6716 | |
| 6717 | /* converter keyword. */ |
| 6718 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6719 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6720 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6721 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6722 | |
| 6723 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6724 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6725 | 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] | 6726 | sck->kw[0].val_args = NULL; |
| 6727 | sck->kw[0].in_type = SMP_T_STR; |
| 6728 | sck->kw[0].out_type = SMP_T_STR; |
| 6729 | sck->kw[0].private = fcn; |
| 6730 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6731 | /* Register this new converter */ |
| 6732 | sample_register_convs(sck); |
| 6733 | |
| 6734 | return 0; |
| 6735 | } |
| 6736 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6737 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6738 | * "sample-fetch" functions. It expects a converter name used |
| 6739 | * in the haproxy configuration file, and an LUA function. |
| 6740 | */ |
| 6741 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6742 | { |
| 6743 | const char *name; |
| 6744 | int ref; |
| 6745 | int len; |
| 6746 | struct sample_fetch_kw_list *sfk; |
| 6747 | struct hlua_function *fcn; |
| 6748 | |
| 6749 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6750 | |
| 6751 | /* First argument : sample-fetch name. */ |
| 6752 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6753 | |
| 6754 | /* Second argument : lua function. */ |
| 6755 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6756 | |
| 6757 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6758 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6759 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6760 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6761 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6762 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6763 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6764 | |
| 6765 | /* Fill fcn. */ |
| 6766 | fcn->name = strdup(name); |
| 6767 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6768 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6769 | fcn->function_ref = ref; |
| 6770 | |
| 6771 | /* List head */ |
| 6772 | sfk->list.n = sfk->list.p = NULL; |
| 6773 | |
| 6774 | /* sample-fetch keyword. */ |
| 6775 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6776 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6777 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6778 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6779 | |
| 6780 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6781 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6782 | 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] | 6783 | sfk->kw[0].val_args = NULL; |
| 6784 | sfk->kw[0].out_type = SMP_T_STR; |
| 6785 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6786 | sfk->kw[0].val = 0; |
| 6787 | sfk->kw[0].private = fcn; |
| 6788 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6789 | /* Register this new fetch. */ |
| 6790 | sample_register_fetches(sfk); |
| 6791 | |
| 6792 | return 0; |
| 6793 | } |
| 6794 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6795 | /* This function is a wrapper to execute each LUA function declared |
| 6796 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6797 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6798 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6799 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6800 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6801 | 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] | 6802 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6803 | { |
| 6804 | char **arg; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6805 | unsigned int hflags = 0; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6806 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6807 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6808 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6809 | |
| 6810 | switch (rule->from) { |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6811 | case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break; |
| 6812 | case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break; |
| 6813 | case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break; |
| 6814 | 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] | 6815 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6816 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6817 | return ACT_RET_CONT; |
| 6818 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6819 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6820 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6821 | * Lua context can be not initialized. This behavior |
| 6822 | * permits to save performances because a systematic |
| 6823 | * Lua initialization cause 5% performances loss. |
| 6824 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6825 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6826 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6827 | if (!s->hlua) { |
| 6828 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6829 | rule->arg.hlua_rule->fcn.name); |
| 6830 | return ACT_RET_CONT; |
| 6831 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6832 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6833 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6834 | rule->arg.hlua_rule->fcn.name); |
| 6835 | return ACT_RET_CONT; |
| 6836 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6837 | } |
| 6838 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6839 | consistency_set(s, dir, &s->hlua->cons); |
| 6840 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6841 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6842 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6843 | |
| 6844 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6845 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6846 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6847 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6848 | else |
| 6849 | error = "critical error"; |
| 6850 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6851 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6852 | return ACT_RET_CONT; |
| 6853 | } |
| 6854 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6855 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6856 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6857 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6858 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6859 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6860 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6861 | } |
| 6862 | |
| 6863 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6864 | 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] | 6865 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6866 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6867 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6868 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6869 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6870 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6871 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6872 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6873 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6874 | |
| 6875 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6876 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6877 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6878 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6879 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6880 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6881 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6882 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6883 | lua_pushstring(s->hlua->T, *arg); |
| 6884 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6885 | } |
| 6886 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6887 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6888 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6889 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6890 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6891 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6892 | } |
| 6893 | |
Christopher Faulet | 719ddc8 | 2020-06-02 18:46:07 +0200 | [diff] [blame] | 6894 | /* Always reset the analyse expiration timeout for the corresponding |
| 6895 | * channel in case the lua script yield, to be sure to not keep an |
| 6896 | * expired timeout. |
| 6897 | */ |
| 6898 | if (dir == SMP_OPT_DIR_REQ) |
| 6899 | s->req.analyse_exp = TICK_ETERNITY; |
| 6900 | else |
| 6901 | s->res.analyse_exp = TICK_ETERNITY; |
| 6902 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6903 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6904 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6905 | /* finished. */ |
| 6906 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6907 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6908 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6909 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6910 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6911 | if (s->hlua->flags & HLUA_STOP) |
Christopher Faulet | 4629d08 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 6912 | return ACT_RET_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6913 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6914 | |
| 6915 | /* yield. */ |
| 6916 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6917 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6918 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Christopher Faulet | ea0b0e2 | 2019-08-14 23:19:45 +0200 | [diff] [blame] | 6919 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6920 | s->req.analyse_exp = s->hlua->wake_time; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6921 | else |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6922 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6923 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6924 | /* Some actions can be wake up when a "write" event |
| 6925 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6926 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6927 | */ |
Christopher Faulet | b22f650 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6928 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6929 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6930 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6931 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6932 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6933 | * because HAProxy is not able to manipulate data, it |
| 6934 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6935 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6936 | |
| 6937 | /* finished with error. */ |
| 6938 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6939 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6940 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6941 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6942 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6943 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6944 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6945 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6946 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6947 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6948 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6949 | case HLUA_E_ETMOUT: |
| 6950 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6951 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6952 | return ACT_RET_ERR; |
| 6953 | } |
| 6954 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6955 | return 0; |
| 6956 | |
| 6957 | case HLUA_E_NOMEM: |
| 6958 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6959 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6960 | return ACT_RET_ERR; |
| 6961 | } |
| 6962 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6963 | return 0; |
| 6964 | |
| 6965 | case HLUA_E_YIELD: |
| 6966 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6967 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6968 | return ACT_RET_ERR; |
| 6969 | } |
| 6970 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6971 | rule->arg.hlua_rule->fcn.name); |
| 6972 | return 0; |
| 6973 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6974 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6975 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6976 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6977 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6978 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6979 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6980 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6981 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6982 | |
| 6983 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6984 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6985 | } |
| 6986 | } |
| 6987 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6988 | 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] | 6989 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6990 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6991 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6992 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6993 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6994 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6995 | } |
| 6996 | |
| 6997 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6998 | { |
| 6999 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7000 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7001 | struct task *task; |
| 7002 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7003 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7004 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7005 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7006 | if (!hlua) { |
| 7007 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 7008 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7009 | return 0; |
| 7010 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7011 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7012 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7013 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7014 | |
| 7015 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7016 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7017 | if (!task) { |
| 7018 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 7019 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7020 | return 0; |
| 7021 | } |
| 7022 | task->nice = 0; |
| 7023 | task->context = ctx; |
| 7024 | task->process = hlua_applet_wakeup; |
| 7025 | ctx->ctx.hlua_apptcp.task = task; |
| 7026 | |
| 7027 | /* In the execution wrappers linked with a stream, the |
| 7028 | * Lua context can be not initialized. This behavior |
| 7029 | * permits to save performances because a systematic |
| 7030 | * Lua initialization cause 5% performances loss. |
| 7031 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7032 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7033 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 7034 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7035 | return 0; |
| 7036 | } |
| 7037 | |
| 7038 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7039 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7040 | |
| 7041 | /* The following Lua calls can fail. */ |
| 7042 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7043 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7044 | error = lua_tostring(hlua->T, -1); |
| 7045 | else |
| 7046 | error = "critical error"; |
| 7047 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7048 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7049 | return 0; |
| 7050 | } |
| 7051 | |
| 7052 | /* Check stack available size. */ |
| 7053 | if (!lua_checkstack(hlua->T, 1)) { |
| 7054 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7055 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7056 | RESET_SAFE_LJMP(hlua->T); |
| 7057 | return 0; |
| 7058 | } |
| 7059 | |
| 7060 | /* Restore the function in the stack. */ |
| 7061 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7062 | |
| 7063 | /* Create and and push object stream in the stack. */ |
| 7064 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7065 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7066 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7067 | RESET_SAFE_LJMP(hlua->T); |
| 7068 | return 0; |
| 7069 | } |
| 7070 | hlua->nargs = 1; |
| 7071 | |
| 7072 | /* push keywords in the stack. */ |
| 7073 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7074 | if (!lua_checkstack(hlua->T, 1)) { |
| 7075 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7076 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7077 | RESET_SAFE_LJMP(hlua->T); |
| 7078 | return 0; |
| 7079 | } |
| 7080 | lua_pushstring(hlua->T, *arg); |
| 7081 | hlua->nargs++; |
| 7082 | } |
| 7083 | |
| 7084 | RESET_SAFE_LJMP(hlua->T); |
| 7085 | |
| 7086 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7087 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7088 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7089 | |
| 7090 | return 1; |
| 7091 | } |
| 7092 | |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7093 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7094 | { |
| 7095 | struct stream_interface *si = ctx->owner; |
| 7096 | struct stream *strm = si_strm(si); |
| 7097 | struct channel *res = si_ic(si); |
| 7098 | struct act_rule *rule = ctx->rule; |
| 7099 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7100 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7101 | |
| 7102 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7103 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7104 | /* eat the whole request */ |
| 7105 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7106 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7107 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7108 | |
| 7109 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7110 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7111 | return; |
| 7112 | |
| 7113 | /* Execute the function. */ |
| 7114 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7115 | /* finished. */ |
| 7116 | case HLUA_E_OK: |
| 7117 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7118 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7119 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7120 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7121 | res->flags |= CF_READ_NULL; |
| 7122 | si_shutr(si); |
| 7123 | return; |
| 7124 | |
| 7125 | /* yield. */ |
| 7126 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7127 | if (hlua->wake_time != TICK_ETERNITY) |
| 7128 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7129 | return; |
| 7130 | |
| 7131 | /* finished with error. */ |
| 7132 | case HLUA_E_ERRMSG: |
| 7133 | /* Display log. */ |
| 7134 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7135 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7136 | lua_pop(hlua->T, 1); |
| 7137 | goto error; |
| 7138 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7139 | case HLUA_E_ETMOUT: |
| 7140 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 7141 | rule->arg.hlua_rule->fcn.name); |
| 7142 | goto error; |
| 7143 | |
| 7144 | case HLUA_E_NOMEM: |
| 7145 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 7146 | rule->arg.hlua_rule->fcn.name); |
| 7147 | goto error; |
| 7148 | |
| 7149 | case HLUA_E_YIELD: /* unexpected */ |
| 7150 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 7151 | rule->arg.hlua_rule->fcn.name); |
| 7152 | goto error; |
| 7153 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7154 | case HLUA_E_ERR: |
| 7155 | /* Display log. */ |
| 7156 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 7157 | rule->arg.hlua_rule->fcn.name); |
| 7158 | goto error; |
| 7159 | |
| 7160 | default: |
| 7161 | goto error; |
| 7162 | } |
| 7163 | |
| 7164 | error: |
| 7165 | |
| 7166 | /* For all other cases, just close the stream. */ |
| 7167 | si_shutw(si); |
| 7168 | si_shutr(si); |
| 7169 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7170 | } |
| 7171 | |
| 7172 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7173 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7174 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7175 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7176 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7177 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7178 | } |
| 7179 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7180 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7181 | * an errors occurs and -1 if more data are required for initializing |
| 7182 | * the applet. |
| 7183 | */ |
| 7184 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7185 | { |
| 7186 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7187 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7188 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7189 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7190 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7191 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7192 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7193 | txn = strm->txn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7194 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7195 | /* We want two things in HTTP mode : |
| 7196 | * - enforce server-close mode if we were in keep-alive, so that the |
| 7197 | * applet is released after each response ; |
| 7198 | * - enable request body transfer to the applet in order to resync |
| 7199 | * with the response body. |
| 7200 | */ |
| 7201 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 7202 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7203 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7204 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7205 | if (!hlua) { |
| 7206 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7207 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7208 | return 0; |
| 7209 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7210 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7211 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7212 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7213 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7214 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7215 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7216 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7217 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7218 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7219 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7220 | if (!task) { |
| 7221 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7222 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7223 | return 0; |
| 7224 | } |
| 7225 | task->nice = 0; |
| 7226 | task->context = ctx; |
| 7227 | task->process = hlua_applet_wakeup; |
| 7228 | ctx->ctx.hlua_apphttp.task = task; |
| 7229 | |
| 7230 | /* In the execution wrappers linked with a stream, the |
| 7231 | * Lua context can be not initialized. This behavior |
| 7232 | * permits to save performances because a systematic |
| 7233 | * Lua initialization cause 5% performances loss. |
| 7234 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7235 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7236 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 7237 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7238 | return 0; |
| 7239 | } |
| 7240 | |
| 7241 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7242 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7243 | |
| 7244 | /* The following Lua calls can fail. */ |
| 7245 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7246 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7247 | error = lua_tostring(hlua->T, -1); |
| 7248 | else |
| 7249 | error = "critical error"; |
| 7250 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7251 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7252 | return 0; |
| 7253 | } |
| 7254 | |
| 7255 | /* Check stack available size. */ |
| 7256 | if (!lua_checkstack(hlua->T, 1)) { |
| 7257 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7258 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7259 | RESET_SAFE_LJMP(hlua->T); |
| 7260 | return 0; |
| 7261 | } |
| 7262 | |
| 7263 | /* Restore the function in the stack. */ |
| 7264 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7265 | |
| 7266 | /* Create and and push object stream in the stack. */ |
| 7267 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7268 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7269 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7270 | RESET_SAFE_LJMP(hlua->T); |
| 7271 | return 0; |
| 7272 | } |
| 7273 | hlua->nargs = 1; |
| 7274 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7275 | /* push keywords in the stack. */ |
| 7276 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7277 | if (!lua_checkstack(hlua->T, 1)) { |
| 7278 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7279 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7280 | RESET_SAFE_LJMP(hlua->T); |
| 7281 | return 0; |
| 7282 | } |
| 7283 | lua_pushstring(hlua->T, *arg); |
| 7284 | hlua->nargs++; |
| 7285 | } |
| 7286 | |
| 7287 | RESET_SAFE_LJMP(hlua->T); |
| 7288 | |
| 7289 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7290 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7291 | |
| 7292 | return 1; |
| 7293 | } |
| 7294 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7295 | static void hlua_applet_htx_fct(struct appctx *ctx) |
| 7296 | { |
| 7297 | struct stream_interface *si = ctx->owner; |
| 7298 | struct stream *strm = si_strm(si); |
| 7299 | struct channel *req = si_oc(si); |
| 7300 | struct channel *res = si_ic(si); |
| 7301 | struct act_rule *rule = ctx->rule; |
| 7302 | struct proxy *px = strm->be; |
| 7303 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7304 | struct htx *req_htx, *res_htx; |
| 7305 | |
| 7306 | res_htx = htx_from_buf(&res->buf); |
| 7307 | |
| 7308 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7309 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7310 | goto out; |
| 7311 | |
| 7312 | /* Check if the input buffer is avalaible. */ |
| 7313 | if (!b_size(&res->buf)) { |
| 7314 | si_rx_room_blk(si); |
| 7315 | goto out; |
| 7316 | } |
| 7317 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7318 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7319 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7320 | |
| 7321 | /* Set the currently running flag. */ |
| 7322 | if (!HLUA_IS_RUNNING(hlua) && |
| 7323 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7324 | struct htx_blk *blk; |
| 7325 | size_t count = co_data(req); |
| 7326 | |
| 7327 | if (!count) { |
| 7328 | si_cant_get(si); |
| 7329 | goto out; |
| 7330 | } |
| 7331 | |
| 7332 | /* We need to flush the request header. This left the body for |
| 7333 | * the Lua. |
| 7334 | */ |
| 7335 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7336 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7337 | while (count && blk) { |
| 7338 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7339 | uint32_t sz = htx_get_blksz(blk); |
| 7340 | |
| 7341 | if (sz > count) { |
| 7342 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7343 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7344 | goto out; |
| 7345 | } |
| 7346 | |
| 7347 | count -= sz; |
| 7348 | co_set_data(req, co_data(req) - sz); |
| 7349 | blk = htx_remove_blk(req_htx, blk); |
| 7350 | |
| 7351 | if (type == HTX_BLK_EOH) |
| 7352 | break; |
| 7353 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7354 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7355 | } |
| 7356 | |
| 7357 | /* Executes The applet if it is not done. */ |
| 7358 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7359 | |
| 7360 | /* Execute the function. */ |
| 7361 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7362 | /* finished. */ |
| 7363 | case HLUA_E_OK: |
| 7364 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7365 | break; |
| 7366 | |
| 7367 | /* yield. */ |
| 7368 | case HLUA_E_AGAIN: |
| 7369 | if (hlua->wake_time != TICK_ETERNITY) |
| 7370 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7371 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7372 | |
| 7373 | /* finished with error. */ |
| 7374 | case HLUA_E_ERRMSG: |
| 7375 | /* Display log. */ |
| 7376 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7377 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7378 | lua_pop(hlua->T, 1); |
| 7379 | goto error; |
| 7380 | |
| 7381 | case HLUA_E_ETMOUT: |
| 7382 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7383 | rule->arg.hlua_rule->fcn.name); |
| 7384 | goto error; |
| 7385 | |
| 7386 | case HLUA_E_NOMEM: |
| 7387 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7388 | rule->arg.hlua_rule->fcn.name); |
| 7389 | goto error; |
| 7390 | |
| 7391 | case HLUA_E_YIELD: /* unexpected */ |
| 7392 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7393 | rule->arg.hlua_rule->fcn.name); |
| 7394 | goto error; |
| 7395 | |
| 7396 | case HLUA_E_ERR: |
| 7397 | /* Display log. */ |
| 7398 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7399 | rule->arg.hlua_rule->fcn.name); |
| 7400 | goto error; |
| 7401 | |
| 7402 | default: |
| 7403 | goto error; |
| 7404 | } |
| 7405 | } |
| 7406 | |
| 7407 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7408 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7409 | goto done; |
| 7410 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7411 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7412 | goto error; |
| 7413 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7414 | /* Don't add TLR because mux-h1 will take care of it */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7415 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7416 | si_rx_room_blk(si); |
| 7417 | goto out; |
| 7418 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7419 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7420 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7421 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7422 | } |
| 7423 | |
| 7424 | done: |
| 7425 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7426 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7427 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7428 | si_shutr(si); |
| 7429 | } |
| 7430 | |
| 7431 | /* eat the whole request */ |
| 7432 | if (co_data(req)) { |
| 7433 | req_htx = htx_from_buf(&req->buf); |
| 7434 | co_htx_skip(req, req_htx, co_data(req)); |
| 7435 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7436 | } |
| 7437 | } |
| 7438 | |
| 7439 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7440 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7441 | return; |
| 7442 | |
| 7443 | error: |
| 7444 | |
| 7445 | /* If we are in HTTP mode, and we are not send any |
| 7446 | * data, return a 500 server error in best effort: |
| 7447 | * if there is no room available in the buffer, |
| 7448 | * just close the connection. |
| 7449 | */ |
| 7450 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7451 | struct buffer *err = &htx_err_chunks[HTTP_ERR_500]; |
| 7452 | |
| 7453 | channel_erase(res); |
| 7454 | res->buf.data = b_data(err); |
| 7455 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7456 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7457 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7458 | } |
| 7459 | if (!(strm->flags & SF_ERR_MASK)) |
| 7460 | strm->flags |= SF_ERR_RESOURCE; |
| 7461 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7462 | goto done; |
| 7463 | } |
| 7464 | |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7465 | void hlua_applet_http_fct(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7466 | { |
| 7467 | struct stream_interface *si = ctx->owner; |
| 7468 | struct stream *strm = si_strm(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7469 | struct channel *req = si_oc(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7470 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7471 | struct act_rule *rule = ctx->rule; |
| 7472 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7473 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7474 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7475 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7476 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7477 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7478 | int ret; |
| 7479 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7480 | if (IS_HTX_STRM(strm)) |
| 7481 | return hlua_applet_htx_fct(ctx); |
| 7482 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7483 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7484 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7485 | goto out; |
| 7486 | |
| 7487 | /* Check if the input buffer is avalaible. */ |
| 7488 | if (!b_size(&res->buf)) { |
| 7489 | si_rx_room_blk(si); |
| 7490 | goto out; |
| 7491 | } |
| 7492 | /* check that the output is not closed */ |
| 7493 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
| 7494 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7495 | |
| 7496 | /* Set the currently running flag. */ |
| 7497 | if (!HLUA_IS_RUNNING(hlua) && |
| 7498 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7499 | /* Store the max amount of bytes that we can read. */ |
| 7500 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 7501 | |
| 7502 | /* We need to flush the request header. This left the body |
| 7503 | * for the Lua. |
| 7504 | */ |
| 7505 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7506 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7507 | ret = co_getblk_nc(req, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7508 | if (ret == -1) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7509 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7510 | |
| 7511 | /* No data available, ask for more data. */ |
| 7512 | if (ret == 1) |
| 7513 | len2 = 0; |
| 7514 | if (ret == 0) |
| 7515 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 7516 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7517 | si_cant_get(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7518 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7519 | } |
| 7520 | |
| 7521 | /* skip the requests bytes. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7522 | co_skip(req, strm->txn->req.eoh + strm->txn->req.eol); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7523 | } |
| 7524 | |
| 7525 | /* Executes The applet if it is not done. */ |
| 7526 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7527 | |
| 7528 | /* Execute the function. */ |
| 7529 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7530 | /* finished. */ |
| 7531 | case HLUA_E_OK: |
| 7532 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7533 | break; |
| 7534 | |
| 7535 | /* yield. */ |
| 7536 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7537 | if (hlua->wake_time != TICK_ETERNITY) |
| 7538 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7539 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7540 | |
| 7541 | /* finished with error. */ |
| 7542 | case HLUA_E_ERRMSG: |
| 7543 | /* Display log. */ |
| 7544 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7545 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7546 | lua_pop(hlua->T, 1); |
| 7547 | goto error; |
| 7548 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7549 | case HLUA_E_ETMOUT: |
| 7550 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7551 | rule->arg.hlua_rule->fcn.name); |
| 7552 | goto error; |
| 7553 | |
| 7554 | case HLUA_E_NOMEM: |
| 7555 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7556 | rule->arg.hlua_rule->fcn.name); |
| 7557 | goto error; |
| 7558 | |
| 7559 | case HLUA_E_YIELD: /* unexpected */ |
| 7560 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7561 | rule->arg.hlua_rule->fcn.name); |
| 7562 | goto error; |
| 7563 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7564 | case HLUA_E_ERR: |
| 7565 | /* Display log. */ |
| 7566 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7567 | rule->arg.hlua_rule->fcn.name); |
| 7568 | goto error; |
| 7569 | |
| 7570 | default: |
| 7571 | goto error; |
| 7572 | } |
| 7573 | } |
| 7574 | |
| 7575 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7576 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7577 | goto done; |
| 7578 | |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 7579 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7580 | goto error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7581 | |
| 7582 | /* We must send the final chunk. */ |
| 7583 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 7584 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 7585 | |
| 7586 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 7587 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7588 | |
| 7589 | /* critical error. */ |
| 7590 | if (ret == -2 || ret == -3) { |
| 7591 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 7592 | rule->arg.hlua_rule->fcn.name); |
| 7593 | goto error; |
| 7594 | } |
| 7595 | |
| 7596 | /* no enough space error. */ |
| 7597 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7598 | si_rx_room_blk(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7599 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7600 | } |
| 7601 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7602 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7603 | ctx->ctx.hlua_apphttp.flags |= (APPLET_LAST_CHK|APPLET_RSP_SENT); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7604 | } |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7605 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7606 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7607 | done: |
| 7608 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 7609 | if (!(res->flags & CF_SHUTR)) { |
| 7610 | res->flags |= CF_READ_NULL; |
| 7611 | si_shutr(si); |
| 7612 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7613 | |
| 7614 | /* eat the whole request */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7615 | if (co_data(req)) |
| 7616 | co_skip(req, co_data(req)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7617 | } |
| 7618 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7619 | out: |
| 7620 | return; |
| 7621 | |
| 7622 | error: |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7623 | |
| 7624 | /* If we are in HTTP mode, and we are not send any |
| 7625 | * data, return a 500 server error in best effort: |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7626 | * if there is no room available in the buffer, |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7627 | * just close the connection. |
| 7628 | */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7629 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7630 | channel_erase(res); |
| 7631 | ci_putblk(res, error_500, strlen(error_500)); |
| 7632 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7633 | if (!(strm->flags & SF_ERR_MASK)) |
| 7634 | strm->flags |= SF_ERR_RESOURCE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7635 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7636 | goto done; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7637 | } |
| 7638 | |
| 7639 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7640 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7641 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7642 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7643 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7644 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7645 | } |
| 7646 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7647 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 7648 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7649 | * |
| 7650 | * This function can fail with an abort() due to an Lua critical error. |
| 7651 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7652 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7653 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7654 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7655 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7656 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7657 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7658 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7659 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7660 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7661 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7662 | if (!rule->arg.hlua_rule) { |
| 7663 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7664 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7665 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7666 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7667 | /* Memory for arguments. */ |
| 7668 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7669 | if (!rule->arg.hlua_rule->args) { |
| 7670 | memprintf(err, "out of memory error"); |
| 7671 | return ACT_RET_PRS_ERR; |
| 7672 | } |
| 7673 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7674 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7675 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7676 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7677 | /* Expect some arguments */ |
| 7678 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7679 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7680 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7681 | return ACT_RET_PRS_ERR; |
| 7682 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7683 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7684 | if (!rule->arg.hlua_rule->args[i]) { |
| 7685 | memprintf(err, "out of memory error"); |
| 7686 | return ACT_RET_PRS_ERR; |
| 7687 | } |
| 7688 | (*cur_arg)++; |
| 7689 | } |
| 7690 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7691 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7692 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7693 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7694 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7695 | } |
| 7696 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7697 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7698 | struct act_rule *rule, char **err) |
| 7699 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7700 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7701 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7702 | /* HTTP applets are forbidden in tcp-request rules. |
| 7703 | * HTTP applet request requires everything initilized by |
| 7704 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 7705 | * The applet will be immediately initilized, but its before |
| 7706 | * the call of this analyzer. |
| 7707 | */ |
| 7708 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7709 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7710 | return ACT_RET_PRS_ERR; |
| 7711 | } |
| 7712 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7713 | /* Memory for the rule. */ |
| 7714 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7715 | if (!rule->arg.hlua_rule) { |
| 7716 | memprintf(err, "out of memory error"); |
| 7717 | return ACT_RET_PRS_ERR; |
| 7718 | } |
| 7719 | |
| 7720 | /* Reference the Lua function and store the reference. */ |
| 7721 | rule->arg.hlua_rule->fcn = *fcn; |
| 7722 | |
| 7723 | /* TODO: later accept arguments. */ |
| 7724 | rule->arg.hlua_rule->args = NULL; |
| 7725 | |
| 7726 | /* Add applet pointer in the rule. */ |
| 7727 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7728 | rule->applet.name = fcn->name; |
| 7729 | rule->applet.init = hlua_applet_http_init; |
| 7730 | rule->applet.fct = hlua_applet_http_fct; |
| 7731 | rule->applet.release = hlua_applet_http_release; |
| 7732 | rule->applet.timeout = hlua_timeout_applet; |
| 7733 | |
| 7734 | return ACT_RET_PRS_OK; |
| 7735 | } |
| 7736 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7737 | /* This function is an LUA binding used for registering |
| 7738 | * "sample-conv" functions. It expects a converter name used |
| 7739 | * in the haproxy configuration file, and an LUA function. |
| 7740 | */ |
| 7741 | __LJMP static int hlua_register_action(lua_State *L) |
| 7742 | { |
| 7743 | struct action_kw_list *akl; |
| 7744 | const char *name; |
| 7745 | int ref; |
| 7746 | int len; |
| 7747 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7748 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7749 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7750 | /* Initialise the number of expected arguments at 0. */ |
| 7751 | nargs = 0; |
| 7752 | |
| 7753 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7754 | 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] | 7755 | |
| 7756 | /* First argument : converter name. */ |
| 7757 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7758 | |
| 7759 | /* Second argument : environment. */ |
| 7760 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7761 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7762 | |
| 7763 | /* Third argument : lua function. */ |
| 7764 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7765 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7766 | /* 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] | 7767 | if (lua_gettop(L) >= 4) |
| 7768 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7769 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7770 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7771 | lua_pushnil(L); |
| 7772 | while (lua_next(L, 2) != 0) { |
| 7773 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7774 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7775 | |
| 7776 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7777 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7778 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7779 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7780 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7781 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7782 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7783 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7784 | |
| 7785 | /* Fill fcn. */ |
| 7786 | fcn->name = strdup(name); |
| 7787 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7788 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7789 | fcn->function_ref = ref; |
| 7790 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7791 | /* Set the expected number od arguments. */ |
| 7792 | fcn->nargs = nargs; |
| 7793 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7794 | /* List head */ |
| 7795 | akl->list.n = akl->list.p = NULL; |
| 7796 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7797 | /* action keyword. */ |
| 7798 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7799 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7800 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7801 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7802 | |
| 7803 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7804 | |
| 7805 | akl->kw[0].match_pfx = 0; |
| 7806 | akl->kw[0].private = fcn; |
| 7807 | akl->kw[0].parse = action_register_lua; |
| 7808 | |
| 7809 | /* select the action registering point. */ |
| 7810 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7811 | tcp_req_cont_keywords_register(akl); |
| 7812 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7813 | tcp_res_cont_keywords_register(akl); |
| 7814 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7815 | http_req_keywords_register(akl); |
| 7816 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7817 | http_res_keywords_register(akl); |
| 7818 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7819 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7820 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7821 | "are expected.", lua_tostring(L, -1))); |
| 7822 | |
| 7823 | /* pop the environment string. */ |
| 7824 | lua_pop(L, 1); |
| 7825 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7826 | return ACT_RET_PRS_OK; |
| 7827 | } |
| 7828 | |
| 7829 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7830 | struct act_rule *rule, char **err) |
| 7831 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7832 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7833 | |
Christopher Faulet | eb70980 | 2019-04-11 22:04:08 +0200 | [diff] [blame] | 7834 | if (px->mode == PR_MODE_HTTP && (px->options2 & PR_O2_USE_HTX)) { |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7835 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 7836 | return ACT_RET_PRS_ERR; |
| 7837 | } |
| 7838 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7839 | /* Memory for the rule. */ |
| 7840 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7841 | if (!rule->arg.hlua_rule) { |
| 7842 | memprintf(err, "out of memory error"); |
| 7843 | return ACT_RET_PRS_ERR; |
| 7844 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7845 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7846 | /* Reference the Lua function and store the reference. */ |
| 7847 | rule->arg.hlua_rule->fcn = *fcn; |
| 7848 | |
| 7849 | /* TODO: later accept arguments. */ |
| 7850 | rule->arg.hlua_rule->args = NULL; |
| 7851 | |
| 7852 | /* Add applet pointer in the rule. */ |
| 7853 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7854 | rule->applet.name = fcn->name; |
| 7855 | rule->applet.init = hlua_applet_tcp_init; |
| 7856 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7857 | rule->applet.release = hlua_applet_tcp_release; |
| 7858 | rule->applet.timeout = hlua_timeout_applet; |
| 7859 | |
| 7860 | return 0; |
| 7861 | } |
| 7862 | |
| 7863 | /* This function is an LUA binding used for registering |
| 7864 | * "sample-conv" functions. It expects a converter name used |
| 7865 | * in the haproxy configuration file, and an LUA function. |
| 7866 | */ |
| 7867 | __LJMP static int hlua_register_service(lua_State *L) |
| 7868 | { |
| 7869 | struct action_kw_list *akl; |
| 7870 | const char *name; |
| 7871 | const char *env; |
| 7872 | int ref; |
| 7873 | int len; |
| 7874 | struct hlua_function *fcn; |
| 7875 | |
| 7876 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7877 | |
| 7878 | /* First argument : converter name. */ |
| 7879 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7880 | |
| 7881 | /* Second argument : environment. */ |
| 7882 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7883 | |
| 7884 | /* Third argument : lua function. */ |
| 7885 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7886 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7887 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7888 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7889 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7890 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7891 | fcn = calloc(1, sizeof(*fcn)); |
| 7892 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7893 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7894 | |
| 7895 | /* Fill fcn. */ |
| 7896 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7897 | fcn->name = calloc(1, len); |
| 7898 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7899 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7900 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7901 | fcn->function_ref = ref; |
| 7902 | |
| 7903 | /* List head */ |
| 7904 | akl->list.n = akl->list.p = NULL; |
| 7905 | |
| 7906 | /* converter keyword. */ |
| 7907 | len = strlen("lua.") + strlen(name) + 1; |
| 7908 | akl->kw[0].kw = calloc(1, len); |
| 7909 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7910 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7911 | |
| 7912 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7913 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7914 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7915 | if (strcmp(env, "tcp") == 0) |
| 7916 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7917 | else if (strcmp(env, "http") == 0) |
| 7918 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7919 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7920 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7921 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7922 | |
| 7923 | akl->kw[0].match_pfx = 0; |
| 7924 | akl->kw[0].private = fcn; |
| 7925 | |
| 7926 | /* End of array. */ |
| 7927 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7928 | |
| 7929 | /* Register this new converter */ |
| 7930 | service_keywords_register(akl); |
| 7931 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7932 | return 0; |
| 7933 | } |
| 7934 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7935 | /* This function initialises Lua cli handler. It copies the |
| 7936 | * arguments in the Lua stack and create channel IO objects. |
| 7937 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7938 | 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] | 7939 | { |
| 7940 | struct hlua *hlua; |
| 7941 | struct hlua_function *fcn; |
| 7942 | int i; |
| 7943 | const char *error; |
| 7944 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7945 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7946 | appctx->ctx.hlua_cli.fcn = private; |
| 7947 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7948 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7949 | if (!hlua) { |
| 7950 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7951 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7952 | } |
| 7953 | HLUA_INIT(hlua); |
| 7954 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7955 | |
| 7956 | /* Create task used by signal to wakeup applets. |
| 7957 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7958 | * applet_http. It is absolutely compatible. |
| 7959 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7960 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7961 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7962 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7963 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7964 | } |
| 7965 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7966 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7967 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7968 | |
| 7969 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7970 | 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] | 7971 | 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] | 7972 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7973 | } |
| 7974 | |
| 7975 | /* The following Lua calls can fail. */ |
| 7976 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7977 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7978 | error = lua_tostring(hlua->T, -1); |
| 7979 | else |
| 7980 | error = "critical error"; |
| 7981 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7982 | goto error; |
| 7983 | } |
| 7984 | |
| 7985 | /* Check stack available size. */ |
| 7986 | if (!lua_checkstack(hlua->T, 2)) { |
| 7987 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7988 | goto error; |
| 7989 | } |
| 7990 | |
| 7991 | /* Restore the function in the stack. */ |
| 7992 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7993 | |
| 7994 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7995 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7996 | */ |
| 7997 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7998 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7999 | goto error; |
| 8000 | } |
| 8001 | hlua->nargs = 1; |
| 8002 | |
| 8003 | /* push keywords in the stack. */ |
| 8004 | for (i = 0; *args[i]; i++) { |
| 8005 | /* Check stack available size. */ |
| 8006 | if (!lua_checkstack(hlua->T, 1)) { |
| 8007 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8008 | goto error; |
| 8009 | } |
| 8010 | lua_pushstring(hlua->T, args[i]); |
| 8011 | hlua->nargs++; |
| 8012 | } |
| 8013 | |
| 8014 | /* We must initialize the execution timeouts. */ |
| 8015 | hlua->max_time = hlua_timeout_session; |
| 8016 | |
| 8017 | /* At this point the execution is safe. */ |
| 8018 | RESET_SAFE_LJMP(hlua->T); |
| 8019 | |
| 8020 | /* It's ok */ |
| 8021 | return 0; |
| 8022 | |
| 8023 | /* It's not ok. */ |
| 8024 | error: |
| 8025 | RESET_SAFE_LJMP(hlua->T); |
| 8026 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8027 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8028 | return 1; |
| 8029 | } |
| 8030 | |
| 8031 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8032 | { |
| 8033 | struct hlua *hlua; |
| 8034 | struct stream_interface *si; |
| 8035 | struct hlua_function *fcn; |
| 8036 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8037 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8038 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8039 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8040 | |
| 8041 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8042 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8043 | return 1; |
| 8044 | |
| 8045 | /* Execute the function. */ |
| 8046 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8047 | |
| 8048 | /* finished. */ |
| 8049 | case HLUA_E_OK: |
| 8050 | return 1; |
| 8051 | |
| 8052 | /* yield. */ |
| 8053 | case HLUA_E_AGAIN: |
| 8054 | /* We want write. */ |
| 8055 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8056 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8057 | /* Set the timeout. */ |
| 8058 | if (hlua->wake_time != TICK_ETERNITY) |
| 8059 | task_schedule(hlua->task, hlua->wake_time); |
| 8060 | return 0; |
| 8061 | |
| 8062 | /* finished with error. */ |
| 8063 | case HLUA_E_ERRMSG: |
| 8064 | /* Display log. */ |
| 8065 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8066 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8067 | lua_pop(hlua->T, 1); |
| 8068 | return 1; |
| 8069 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8070 | case HLUA_E_ETMOUT: |
| 8071 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8072 | fcn->name); |
| 8073 | return 1; |
| 8074 | |
| 8075 | case HLUA_E_NOMEM: |
| 8076 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8077 | fcn->name); |
| 8078 | return 1; |
| 8079 | |
| 8080 | case HLUA_E_YIELD: /* unexpected */ |
| 8081 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8082 | fcn->name); |
| 8083 | return 1; |
| 8084 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8085 | case HLUA_E_ERR: |
| 8086 | /* Display log. */ |
| 8087 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8088 | fcn->name); |
| 8089 | return 1; |
| 8090 | |
| 8091 | default: |
| 8092 | return 1; |
| 8093 | } |
| 8094 | |
| 8095 | return 1; |
| 8096 | } |
| 8097 | |
| 8098 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8099 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8100 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8101 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8102 | } |
| 8103 | |
| 8104 | /* This function is an LUA binding used for registering |
| 8105 | * new keywords in the cli. It expects a list of keywords |
| 8106 | * which are the "path". It is limited to 5 keywords. A |
| 8107 | * description of the command, a function to be executed |
| 8108 | * for the parsing and a function for io handlers. |
| 8109 | */ |
| 8110 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8111 | { |
| 8112 | struct cli_kw_list *cli_kws; |
| 8113 | const char *message; |
| 8114 | int ref_io; |
| 8115 | int len; |
| 8116 | struct hlua_function *fcn; |
| 8117 | int index; |
| 8118 | int i; |
| 8119 | |
| 8120 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8121 | |
| 8122 | /* First argument : an array of maximum 5 keywords. */ |
| 8123 | if (!lua_istable(L, 1)) |
| 8124 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8125 | |
| 8126 | /* Second argument : string with contextual message. */ |
| 8127 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8128 | |
| 8129 | /* Third and fourth argument : lua function. */ |
| 8130 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8131 | |
| 8132 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8133 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 8134 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8135 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8136 | fcn = calloc(1, sizeof(*fcn)); |
| 8137 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8138 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8139 | |
| 8140 | /* Fill path. */ |
| 8141 | index = 0; |
| 8142 | lua_pushnil(L); |
| 8143 | while(lua_next(L, 1) != 0) { |
| 8144 | if (index >= 5) |
| 8145 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8146 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8147 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8148 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 8149 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8150 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8151 | index++; |
| 8152 | lua_pop(L, 1); |
| 8153 | } |
| 8154 | |
| 8155 | /* Copy help message. */ |
| 8156 | cli_kws->kw[0].usage = strdup(message); |
| 8157 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8158 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8159 | |
| 8160 | /* Fill fcn io handler. */ |
| 8161 | len = strlen("<lua.cli>") + 1; |
| 8162 | for (i = 0; i < index; i++) |
| 8163 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8164 | fcn->name = calloc(1, len); |
| 8165 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8166 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8167 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8168 | for (i = 0; i < index; i++) { |
| 8169 | strncat((char *)fcn->name, ".", len); |
| 8170 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8171 | } |
| 8172 | strncat((char *)fcn->name, ">", len); |
| 8173 | fcn->function_ref = ref_io; |
| 8174 | |
| 8175 | /* Fill last entries. */ |
| 8176 | cli_kws->kw[0].private = fcn; |
| 8177 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8178 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8179 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8180 | |
| 8181 | /* Register this new converter */ |
| 8182 | cli_register_kw(cli_kws); |
| 8183 | |
| 8184 | return 0; |
| 8185 | } |
| 8186 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8187 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 8188 | struct proxy *defpx, const char *file, int line, |
| 8189 | char **err, unsigned int *timeout) |
| 8190 | { |
| 8191 | const char *error; |
| 8192 | |
| 8193 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8194 | if (error == PARSE_TIME_OVER) { |
| 8195 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8196 | args[1], args[0]); |
| 8197 | return -1; |
| 8198 | } |
| 8199 | else if (error == PARSE_TIME_UNDER) { |
| 8200 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8201 | args[1], args[0]); |
| 8202 | return -1; |
| 8203 | } |
| 8204 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8205 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8206 | return -1; |
| 8207 | } |
| 8208 | return 0; |
| 8209 | } |
| 8210 | |
| 8211 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 8212 | struct proxy *defpx, const char *file, int line, |
| 8213 | char **err) |
| 8214 | { |
| 8215 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8216 | file, line, err, &hlua_timeout_session); |
| 8217 | } |
| 8218 | |
| 8219 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 8220 | struct proxy *defpx, const char *file, int line, |
| 8221 | char **err) |
| 8222 | { |
| 8223 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8224 | file, line, err, &hlua_timeout_task); |
| 8225 | } |
| 8226 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8227 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 8228 | struct proxy *defpx, const char *file, int line, |
| 8229 | char **err) |
| 8230 | { |
| 8231 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8232 | file, line, err, &hlua_timeout_applet); |
| 8233 | } |
| 8234 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8235 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 8236 | struct proxy *defpx, const char *file, int line, |
| 8237 | char **err) |
| 8238 | { |
| 8239 | char *error; |
| 8240 | |
| 8241 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8242 | if (*error != '\0') { |
| 8243 | memprintf(err, "%s: invalid number", args[0]); |
| 8244 | return -1; |
| 8245 | } |
| 8246 | return 0; |
| 8247 | } |
| 8248 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8249 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 8250 | struct proxy *defpx, const char *file, int line, |
| 8251 | char **err) |
| 8252 | { |
| 8253 | char *error; |
| 8254 | |
| 8255 | if (*(args[1]) == 0) { |
| 8256 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8257 | return -1; |
| 8258 | } |
| 8259 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8260 | if (*error != '\0') { |
| 8261 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8262 | return -1; |
| 8263 | } |
| 8264 | return 0; |
| 8265 | } |
| 8266 | |
| 8267 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8268 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8269 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8270 | * the main lua entry point. |
| 8271 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8272 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8273 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8274 | * |
| 8275 | * In some error case, LUA set an error message in top of the stack. This function |
| 8276 | * 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] | 8277 | * |
| 8278 | * This function can fail with an abort() due to an Lua critical error. |
| 8279 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8280 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8281 | */ |
| 8282 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 8283 | struct proxy *defpx, const char *file, int line, |
| 8284 | char **err) |
| 8285 | { |
| 8286 | int error; |
| 8287 | |
| 8288 | /* Just load and compile the file. */ |
| 8289 | error = luaL_loadfile(gL.T, args[1]); |
| 8290 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8291 | 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] | 8292 | lua_pop(gL.T, 1); |
| 8293 | return -1; |
| 8294 | } |
| 8295 | |
| 8296 | /* If no syntax error where detected, execute the code. */ |
| 8297 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 8298 | switch (error) { |
| 8299 | case LUA_OK: |
| 8300 | break; |
| 8301 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8302 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8303 | lua_pop(gL.T, 1); |
| 8304 | return -1; |
| 8305 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8306 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8307 | return -1; |
| 8308 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8309 | 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] | 8310 | lua_pop(gL.T, 1); |
| 8311 | return -1; |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8312 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8313 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8314 | 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] | 8315 | lua_pop(gL.T, 1); |
| 8316 | return -1; |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8317 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8318 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8319 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8320 | lua_pop(gL.T, 1); |
| 8321 | return -1; |
| 8322 | } |
| 8323 | |
| 8324 | return 0; |
| 8325 | } |
| 8326 | |
| 8327 | /* configuration keywords declaration */ |
| 8328 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8329 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8330 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8331 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8332 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8333 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8334 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8335 | { 0, NULL, NULL }, |
| 8336 | }}; |
| 8337 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8338 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8339 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8340 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8341 | /* This function can fail with an abort() due to an Lua critical error. |
| 8342 | * We are in the initialisation process of HAProxy, this abort() is |
| 8343 | * tolerated. |
| 8344 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8345 | int hlua_post_init() |
| 8346 | { |
| 8347 | struct hlua_init_function *init; |
| 8348 | const char *msg; |
| 8349 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8350 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8351 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8352 | /* Call post initialisation function in safe environement. */ |
| 8353 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8354 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8355 | error = lua_tostring(gL.T, -1); |
| 8356 | else |
| 8357 | error = "critical error"; |
| 8358 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8359 | exit(1); |
| 8360 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8361 | |
| 8362 | #if USE_OPENSSL |
| 8363 | /* Initialize SSL server. */ |
| 8364 | if (socket_ssl.xprt->prepare_srv) { |
| 8365 | int saved_used_backed = global.ssl_used_backend; |
| 8366 | // don't affect maxconn automatic computation |
| 8367 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8368 | global.ssl_used_backend = saved_used_backed; |
| 8369 | } |
| 8370 | #endif |
| 8371 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8372 | hlua_fcn_post_init(gL.T); |
| 8373 | RESET_SAFE_LJMP(gL.T); |
| 8374 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8375 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8376 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 8377 | ret = hlua_ctx_resume(&gL, 0); |
| 8378 | switch (ret) { |
| 8379 | case HLUA_E_OK: |
| 8380 | lua_pop(gL.T, -1); |
| 8381 | return 1; |
| 8382 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8383 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8384 | return 0; |
| 8385 | case HLUA_E_ERRMSG: |
| 8386 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8387 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8388 | return 0; |
| 8389 | case HLUA_E_ERR: |
| 8390 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8391 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8392 | return 0; |
| 8393 | } |
| 8394 | } |
| 8395 | return 1; |
| 8396 | } |
| 8397 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8398 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8399 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8400 | * is the previously allocated size or the kind of object in case of a new |
| 8401 | * allocation. <nsize> is the requested new size. |
| 8402 | */ |
| 8403 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8404 | { |
| 8405 | struct hlua_mem_allocator *zone = ud; |
| 8406 | |
| 8407 | if (nsize == 0) { |
| 8408 | /* it's a free */ |
| 8409 | if (ptr) |
| 8410 | zone->allocated -= osize; |
| 8411 | free(ptr); |
| 8412 | return NULL; |
| 8413 | } |
| 8414 | |
| 8415 | if (!ptr) { |
| 8416 | /* it's a new allocation */ |
| 8417 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8418 | return NULL; |
| 8419 | |
| 8420 | ptr = malloc(nsize); |
| 8421 | if (ptr) |
| 8422 | zone->allocated += nsize; |
| 8423 | return ptr; |
| 8424 | } |
| 8425 | |
| 8426 | /* it's a realloc */ |
| 8427 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8428 | return NULL; |
| 8429 | |
| 8430 | ptr = realloc(ptr, nsize); |
| 8431 | if (ptr) |
| 8432 | zone->allocated += nsize - osize; |
| 8433 | return ptr; |
| 8434 | } |
| 8435 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8436 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8437 | * We are in the initialisation process of HAProxy, this abort() is |
| 8438 | * tolerated. |
| 8439 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8440 | void hlua_init(void) |
| 8441 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8442 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8443 | int idx; |
| 8444 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8445 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8446 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8447 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8448 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8449 | struct srv_kw *kw; |
| 8450 | int tmp_error; |
| 8451 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8452 | char *args[] = { /* SSL client configuration. */ |
| 8453 | "ssl", |
| 8454 | "verify", |
| 8455 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8456 | NULL |
| 8457 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8458 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8459 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8460 | /* Init main lua stack. */ |
| 8461 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8462 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8463 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8464 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8465 | hlua_sethlua(&gL); |
| 8466 | gL.Tref = LUA_REFNIL; |
| 8467 | gL.task = NULL; |
| 8468 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8469 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8470 | * the Lua function can fail with an abort. We are in the initialisation |
| 8471 | * process of HAProxy, this abort() is tolerated. |
| 8472 | */ |
| 8473 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8474 | /* Initialise lua. */ |
| 8475 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8476 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8477 | /* Set safe environment for the initialisation. */ |
| 8478 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8479 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8480 | error_msg = lua_tostring(gL.T, -1); |
| 8481 | else |
| 8482 | error_msg = "critical error"; |
| 8483 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8484 | exit(1); |
| 8485 | } |
| 8486 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8487 | /* |
| 8488 | * |
| 8489 | * Create "core" object. |
| 8490 | * |
| 8491 | */ |
| 8492 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8493 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8494 | lua_newtable(gL.T); |
| 8495 | |
| 8496 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8497 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8498 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8499 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8500 | /* Register special functions. */ |
| 8501 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8502 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8503 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8504 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8505 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8506 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8507 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8508 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8509 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8510 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8511 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8512 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8513 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8514 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8515 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8516 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8517 | hlua_class_function(gL.T, "log", hlua_log); |
| 8518 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8519 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8520 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8521 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8522 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8523 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8524 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8525 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8526 | |
| 8527 | /* |
| 8528 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8529 | * Register class Map |
| 8530 | * |
| 8531 | */ |
| 8532 | |
| 8533 | /* This table entry is the object "Map" base. */ |
| 8534 | lua_newtable(gL.T); |
| 8535 | |
| 8536 | /* register pattern types. */ |
| 8537 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8538 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8539 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8540 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8541 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8542 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8543 | |
| 8544 | /* register constructor. */ |
| 8545 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8546 | |
| 8547 | /* Create and fill the metatable. */ |
| 8548 | lua_newtable(gL.T); |
| 8549 | |
| 8550 | /* Create and fille the __index entry. */ |
| 8551 | lua_pushstring(gL.T, "__index"); |
| 8552 | lua_newtable(gL.T); |
| 8553 | |
| 8554 | /* Register . */ |
| 8555 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8556 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8557 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8558 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8559 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8560 | /* Register previous table in the registry with reference and named entry. |
| 8561 | * The function hlua_register_metatable() pops the stack, so we |
| 8562 | * previously create a copy of the table. |
| 8563 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8564 | 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] | 8565 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8566 | |
| 8567 | /* Assign the metatable to the mai Map object. */ |
| 8568 | lua_setmetatable(gL.T, -2); |
| 8569 | |
| 8570 | /* Set a name to the table. */ |
| 8571 | lua_setglobal(gL.T, "Map"); |
| 8572 | |
| 8573 | /* |
| 8574 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8575 | * Register class Channel |
| 8576 | * |
| 8577 | */ |
| 8578 | |
| 8579 | /* Create and fill the metatable. */ |
| 8580 | lua_newtable(gL.T); |
| 8581 | |
| 8582 | /* Create and fille the __index entry. */ |
| 8583 | lua_pushstring(gL.T, "__index"); |
| 8584 | lua_newtable(gL.T); |
| 8585 | |
| 8586 | /* Register . */ |
| 8587 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8588 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8589 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8590 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8591 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8592 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8593 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8594 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8595 | 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] | 8596 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8597 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8598 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8599 | |
| 8600 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8601 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8602 | |
| 8603 | /* |
| 8604 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8605 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8606 | * |
| 8607 | */ |
| 8608 | |
| 8609 | /* Create and fill the metatable. */ |
| 8610 | lua_newtable(gL.T); |
| 8611 | |
| 8612 | /* Create and fille the __index entry. */ |
| 8613 | lua_pushstring(gL.T, "__index"); |
| 8614 | lua_newtable(gL.T); |
| 8615 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8616 | /* Browse existing fetches and create the associated |
| 8617 | * object method. |
| 8618 | */ |
| 8619 | sf = NULL; |
| 8620 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8621 | |
| 8622 | /* Dont register the keywork if the arguments check function are |
| 8623 | * not safe during the runtime. |
| 8624 | */ |
| 8625 | if ((sf->val_args != NULL) && |
| 8626 | (sf->val_args != val_payload_lv) && |
| 8627 | (sf->val_args != val_hdr)) |
| 8628 | continue; |
| 8629 | |
| 8630 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8631 | * by an underscore. |
| 8632 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8633 | strncpy(trash.area, sf->kw, trash.size); |
| 8634 | trash.area[trash.size - 1] = '\0'; |
| 8635 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8636 | if (*p == '.' || *p == '-' || *p == '+') |
| 8637 | *p = '_'; |
| 8638 | |
| 8639 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8640 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8641 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8642 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8643 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8644 | } |
| 8645 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8646 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8647 | |
| 8648 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8649 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8650 | |
| 8651 | /* |
| 8652 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8653 | * Register class Converters |
| 8654 | * |
| 8655 | */ |
| 8656 | |
| 8657 | /* Create and fill the metatable. */ |
| 8658 | lua_newtable(gL.T); |
| 8659 | |
| 8660 | /* Create and fill the __index entry. */ |
| 8661 | lua_pushstring(gL.T, "__index"); |
| 8662 | lua_newtable(gL.T); |
| 8663 | |
| 8664 | /* Browse existing converters and create the associated |
| 8665 | * object method. |
| 8666 | */ |
| 8667 | sc = NULL; |
| 8668 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8669 | /* Dont register the keywork if the arguments check function are |
| 8670 | * not safe during the runtime. |
| 8671 | */ |
| 8672 | if (sc->val_args != NULL) |
| 8673 | continue; |
| 8674 | |
| 8675 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8676 | * by an underscore. |
| 8677 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8678 | strncpy(trash.area, sc->kw, trash.size); |
| 8679 | trash.area[trash.size - 1] = '\0'; |
| 8680 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8681 | if (*p == '.' || *p == '-' || *p == '+') |
| 8682 | *p = '_'; |
| 8683 | |
| 8684 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8685 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8686 | lua_pushlightuserdata(gL.T, sc); |
| 8687 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8688 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8689 | } |
| 8690 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8691 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8692 | |
| 8693 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8694 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8695 | |
| 8696 | /* |
| 8697 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8698 | * Register class HTTP |
| 8699 | * |
| 8700 | */ |
| 8701 | |
| 8702 | /* Create and fill the metatable. */ |
| 8703 | lua_newtable(gL.T); |
| 8704 | |
| 8705 | /* Create and fille the __index entry. */ |
| 8706 | lua_pushstring(gL.T, "__index"); |
| 8707 | lua_newtable(gL.T); |
| 8708 | |
| 8709 | /* Register Lua functions. */ |
| 8710 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8711 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8712 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8713 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8714 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8715 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8716 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8717 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8718 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8719 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8720 | |
| 8721 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8722 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8723 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8724 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8725 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8726 | 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] | 8727 | 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] | 8728 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8729 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8730 | |
| 8731 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8732 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8733 | |
| 8734 | /* |
| 8735 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8736 | * Register class AppletTCP |
| 8737 | * |
| 8738 | */ |
| 8739 | |
| 8740 | /* Create and fill the metatable. */ |
| 8741 | lua_newtable(gL.T); |
| 8742 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8743 | /* Create and fille the __index entry. */ |
| 8744 | lua_pushstring(gL.T, "__index"); |
| 8745 | lua_newtable(gL.T); |
| 8746 | |
| 8747 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8748 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8749 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8750 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8751 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8752 | 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] | 8753 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8754 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8755 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8756 | |
| 8757 | lua_settable(gL.T, -3); |
| 8758 | |
| 8759 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8760 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8761 | |
| 8762 | /* |
| 8763 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8764 | * Register class AppletHTTP |
| 8765 | * |
| 8766 | */ |
| 8767 | |
| 8768 | /* Create and fill the metatable. */ |
| 8769 | lua_newtable(gL.T); |
| 8770 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8771 | /* Create and fille the __index entry. */ |
| 8772 | lua_pushstring(gL.T, "__index"); |
| 8773 | lua_newtable(gL.T); |
| 8774 | |
| 8775 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8776 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8777 | 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] | 8778 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8779 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8780 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8781 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8782 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8783 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8784 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8785 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8786 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8787 | |
| 8788 | lua_settable(gL.T, -3); |
| 8789 | |
| 8790 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8791 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8792 | |
| 8793 | /* |
| 8794 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8795 | * Register class TXN |
| 8796 | * |
| 8797 | */ |
| 8798 | |
| 8799 | /* Create and fill the metatable. */ |
| 8800 | lua_newtable(gL.T); |
| 8801 | |
| 8802 | /* Create and fille the __index entry. */ |
| 8803 | lua_pushstring(gL.T, "__index"); |
| 8804 | lua_newtable(gL.T); |
| 8805 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8806 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8807 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8808 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8809 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8810 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8811 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8812 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 8813 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8814 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8815 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8816 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8817 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8818 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8819 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8820 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8821 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8822 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8823 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8824 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8825 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8826 | |
| 8827 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8828 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8829 | |
| 8830 | /* |
| 8831 | * |
| 8832 | * Register class Socket |
| 8833 | * |
| 8834 | */ |
| 8835 | |
| 8836 | /* Create and fill the metatable. */ |
| 8837 | lua_newtable(gL.T); |
| 8838 | |
| 8839 | /* Create and fille the __index entry. */ |
| 8840 | lua_pushstring(gL.T, "__index"); |
| 8841 | lua_newtable(gL.T); |
| 8842 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8843 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8844 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8845 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8846 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8847 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8848 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8849 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8850 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8851 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8852 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8853 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8854 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8855 | 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] | 8856 | |
| 8857 | /* Register the garbage collector entry. */ |
| 8858 | lua_pushstring(gL.T, "__gc"); |
| 8859 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8860 | 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] | 8861 | |
| 8862 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8863 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8864 | |
| 8865 | /* Proxy and server configuration initialisation. */ |
| 8866 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8867 | init_new_proxy(&socket_proxy); |
| 8868 | socket_proxy.parent = NULL; |
| 8869 | socket_proxy.last_change = now.tv_sec; |
| 8870 | socket_proxy.id = "LUA-SOCKET"; |
| 8871 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8872 | socket_proxy.maxconn = 0; |
| 8873 | socket_proxy.accept = NULL; |
| 8874 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8875 | socket_proxy.srv = NULL; |
| 8876 | socket_proxy.conn_retries = 0; |
| 8877 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8878 | |
| 8879 | /* Init TCP server: unchanged parameters */ |
| 8880 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8881 | socket_tcp.next = NULL; |
| 8882 | socket_tcp.proxy = &socket_proxy; |
| 8883 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8884 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8885 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8886 | socket_tcp.priv_conns = NULL; |
| 8887 | socket_tcp.idle_conns = NULL; |
| 8888 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8889 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8890 | socket_tcp.last_change = 0; |
| 8891 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8892 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8893 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8894 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8895 | |
| 8896 | /* XXX: Copy default parameter from default server, |
| 8897 | * but the default server is not initialized. |
| 8898 | */ |
| 8899 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8900 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8901 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8902 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8903 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8904 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8905 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8906 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8907 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8908 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8909 | |
| 8910 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8911 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8912 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8913 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8914 | socket_tcp.check.server = &socket_tcp; |
| 8915 | |
| 8916 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8917 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8918 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8919 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8920 | socket_tcp.agent.server = &socket_tcp; |
| 8921 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8922 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8923 | |
| 8924 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8925 | /* Init TCP server: unchanged parameters */ |
| 8926 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8927 | socket_ssl.next = NULL; |
| 8928 | socket_ssl.proxy = &socket_proxy; |
| 8929 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8930 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8931 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8932 | socket_ssl.priv_conns = NULL; |
| 8933 | socket_ssl.idle_conns = NULL; |
| 8934 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8935 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8936 | socket_ssl.last_change = 0; |
| 8937 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8938 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8939 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8940 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8941 | |
| 8942 | /* XXX: Copy default parameter from default server, |
| 8943 | * but the default server is not initialized. |
| 8944 | */ |
| 8945 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8946 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8947 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8948 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8949 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8950 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8951 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8952 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8953 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8954 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8955 | |
| 8956 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8957 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8958 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8959 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8960 | socket_ssl.check.server = &socket_ssl; |
| 8961 | |
| 8962 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8963 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8964 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8965 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8966 | socket_ssl.agent.server = &socket_ssl; |
| 8967 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8968 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8969 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8970 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8971 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8972 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8973 | /* |
| 8974 | * |
| 8975 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8976 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8977 | * features like client certificates and ssl_verify. |
| 8978 | * |
| 8979 | */ |
| 8980 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8981 | if (tmp_error != 0) { |
| 8982 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8983 | abort(); /* This must be never arrives because the command line |
| 8984 | not editable by the user. */ |
| 8985 | } |
| 8986 | idx += kw->skip; |
| 8987 | } |
| 8988 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8989 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8990 | |
| 8991 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8992 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8993 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8994 | static void hlua_register_build_options(void) |
| 8995 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8996 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8997 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8998 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8999 | hap_register_build_opts(ptr, 1); |
| 9000 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9001 | |
| 9002 | INITCALL0(STG_REGISTER, hlua_register_build_options); |