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 | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1596 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1597 | if (str) |
| 1598 | lua_pushstring(L, ""); |
| 1599 | else |
| 1600 | lua_pushnil(L); |
| 1601 | return 1; |
| 1602 | } |
| 1603 | |
| 1604 | /* 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] | 1605 | 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] | 1606 | return 1; |
| 1607 | } |
| 1608 | |
| 1609 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1610 | { |
| 1611 | return _hlua_map_lookup(L, 0); |
| 1612 | } |
| 1613 | |
| 1614 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1615 | { |
| 1616 | return _hlua_map_lookup(L, 1); |
| 1617 | } |
| 1618 | |
| 1619 | /* |
| 1620 | * |
| 1621 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1622 | * Class Socket |
| 1623 | * |
| 1624 | * |
| 1625 | */ |
| 1626 | |
| 1627 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1628 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1629 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1630 | } |
| 1631 | |
| 1632 | /* 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] | 1633 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1634 | * received. |
| 1635 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1636 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1637 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1638 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1639 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1640 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1641 | if (appctx->ctx.hlua_cosocket.die) { |
| 1642 | si_shutw(si); |
| 1643 | si_shutr(si); |
| 1644 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1645 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1646 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1647 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1648 | } |
| 1649 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1650 | /* If the connection object is not available, close all the |
| 1651 | * streams and wakeup everything waiting for. |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1652 | */ |
| 1653 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1654 | si_shutw(si); |
| 1655 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1656 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1657 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1658 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1659 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1660 | } |
| 1661 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1662 | /* If we cant write, wakeup the pending write signals. */ |
| 1663 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1664 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1665 | |
| 1666 | /* If we cant read, wakeup the pending read signals. */ |
| 1667 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1668 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1669 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1670 | /* if the connection is not estabkished, inform the stream that we want |
| 1671 | * to be notified whenever the connection completes. |
| 1672 | */ |
| 1673 | if (!(c->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1674 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1675 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1676 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1677 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1678 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1679 | |
| 1680 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1681 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1682 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1683 | /* 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] | 1684 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1685 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1686 | |
| 1687 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1688 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1689 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1690 | |
| 1691 | /* Some data were injected in the buffer, notify the stream |
| 1692 | * interface. |
| 1693 | */ |
| 1694 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1695 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1696 | |
| 1697 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1698 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1699 | */ |
| 1700 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1701 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1702 | } |
| 1703 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1704 | /* This function is called when the "struct stream" is destroyed. |
| 1705 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1706 | * Wake all the pending signals. |
| 1707 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1708 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1709 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1710 | struct xref *peer; |
| 1711 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1712 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1713 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1714 | if (peer) |
| 1715 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1716 | |
| 1717 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1718 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1719 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1720 | } |
| 1721 | |
| 1722 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1723 | * uses this object. If the stream does not exists, just quit. |
| 1724 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1725 | * pending signal can rest in the read and write lists. destroy |
| 1726 | * it. |
| 1727 | */ |
| 1728 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1729 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1730 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1731 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1732 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1733 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1734 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1735 | |
| 1736 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1737 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1738 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1739 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1740 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1741 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1742 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1743 | appctx->ctx.hlua_cosocket.die = 1; |
| 1744 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1745 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1746 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1747 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1748 | return 0; |
| 1749 | } |
| 1750 | |
| 1751 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1752 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1753 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1754 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1755 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1756 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1757 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1758 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1759 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1760 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1761 | |
| 1762 | /* Check if we run on the same thread than the xreator thread. |
| 1763 | * We cannot access to the socket if the thread is different. |
| 1764 | */ |
| 1765 | if (socket->tid != tid) |
| 1766 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1767 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1768 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1769 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1770 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1771 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1772 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1773 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1774 | appctx->ctx.hlua_cosocket.die = 1; |
| 1775 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1776 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1777 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1778 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1779 | return 0; |
| 1780 | } |
| 1781 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1782 | /* The close function calls close_helper. |
| 1783 | */ |
| 1784 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1785 | { |
| 1786 | MAY_LJMP(check_args(L, 1, "close")); |
| 1787 | return hlua_socket_close_helper(L); |
| 1788 | } |
| 1789 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1790 | /* This Lua function assumes that the stack contain three parameters. |
| 1791 | * 1 - USERDATA containing a struct socket |
| 1792 | * 2 - INTEGER with values of the macro defined below |
| 1793 | * If the integer is -1, we must read at most one line. |
| 1794 | * If the integer is -2, we ust read all the data until the |
| 1795 | * end of the stream. |
| 1796 | * If the integer is positive value, we must read a number of |
| 1797 | * bytes corresponding to this value. |
| 1798 | */ |
| 1799 | #define HLSR_READ_LINE (-1) |
| 1800 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1801 | __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] | 1802 | { |
| 1803 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1804 | int wanted = lua_tointeger(L, 2); |
| 1805 | struct hlua *hlua = hlua_gethlua(L); |
| 1806 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1807 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1808 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1809 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1810 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1811 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1812 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1813 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1814 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1815 | struct stream_interface *si; |
| 1816 | struct stream *s; |
| 1817 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1818 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1819 | |
| 1820 | /* Check if this lua stack is schedulable. */ |
| 1821 | if (!hlua || !hlua->task) |
| 1822 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1823 | "'frontend', 'backend' or 'task'")); |
| 1824 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1825 | /* Check if we run on the same thread than the xreator thread. |
| 1826 | * We cannot access to the socket if the thread is different. |
| 1827 | */ |
| 1828 | if (socket->tid != tid) |
| 1829 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1830 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1831 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1832 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1833 | if (!peer) |
| 1834 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1835 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1836 | si = appctx->owner; |
| 1837 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1838 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1839 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1840 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1841 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1842 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1843 | if (nblk < 0) /* Connection close. */ |
| 1844 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1845 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1846 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1847 | |
| 1848 | /* remove final \r\n. */ |
| 1849 | if (nblk == 1) { |
| 1850 | if (blk1[len1-1] == '\n') { |
| 1851 | len1--; |
| 1852 | skip_at_end++; |
| 1853 | if (blk1[len1-1] == '\r') { |
| 1854 | len1--; |
| 1855 | skip_at_end++; |
| 1856 | } |
| 1857 | } |
| 1858 | } |
| 1859 | else { |
| 1860 | if (blk2[len2-1] == '\n') { |
| 1861 | len2--; |
| 1862 | skip_at_end++; |
| 1863 | if (blk2[len2-1] == '\r') { |
| 1864 | len2--; |
| 1865 | skip_at_end++; |
| 1866 | } |
| 1867 | } |
| 1868 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1869 | } |
| 1870 | |
| 1871 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1872 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1873 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1874 | if (nblk < 0) /* Connection close. */ |
| 1875 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1876 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1877 | goto connection_empty; |
| 1878 | } |
| 1879 | |
| 1880 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1881 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1882 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1883 | if (nblk < 0) /* Connection close. */ |
| 1884 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1885 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1886 | goto connection_empty; |
| 1887 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1888 | missing_bytes = wanted - socket->b.n; |
| 1889 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1890 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1891 | len1 = missing_bytes; |
| 1892 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1893 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | len = len1; |
| 1897 | |
| 1898 | luaL_addlstring(&socket->b, blk1, len1); |
| 1899 | if (nblk == 2) { |
| 1900 | len += len2; |
| 1901 | luaL_addlstring(&socket->b, blk2, len2); |
| 1902 | } |
| 1903 | |
| 1904 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1905 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1906 | |
| 1907 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1908 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1909 | |
| 1910 | /* If the pattern reclaim to read all the data |
| 1911 | * in the connection, got out. |
| 1912 | */ |
| 1913 | if (wanted == HLSR_READ_ALL) |
| 1914 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1915 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1916 | goto connection_empty; |
| 1917 | |
| 1918 | /* Return result. */ |
| 1919 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1920 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1921 | return 1; |
| 1922 | |
| 1923 | connection_closed: |
| 1924 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1925 | xref_unlock(&socket->xref, peer); |
| 1926 | |
| 1927 | no_peer: |
| 1928 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1929 | /* If the buffer containds data. */ |
| 1930 | if (socket->b.n > 0) { |
| 1931 | luaL_pushresult(&socket->b); |
| 1932 | return 1; |
| 1933 | } |
| 1934 | lua_pushnil(L); |
| 1935 | lua_pushstring(L, "connection closed."); |
| 1936 | return 2; |
| 1937 | |
| 1938 | connection_empty: |
| 1939 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1940 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1941 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1942 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1943 | } |
| 1944 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1945 | 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] | 1946 | return 0; |
| 1947 | } |
| 1948 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1949 | /* This Lua function gets two parameters. The first one can be string |
| 1950 | * or a number. If the string is "*l", the user requires one line. If |
| 1951 | * 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] | 1952 | * If the value is a number, the user require a number of bytes equal |
| 1953 | * to the value. The default value is "*l" (a line). |
| 1954 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1955 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1956 | * integer takes this values: |
| 1957 | * -1 : read a line |
| 1958 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1959 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1960 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1961 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1962 | * concatenated with the read data. |
| 1963 | */ |
| 1964 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1965 | { |
| 1966 | int wanted = HLSR_READ_LINE; |
| 1967 | const char *pattern; |
| 1968 | int type; |
| 1969 | char *error; |
| 1970 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1971 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1972 | |
| 1973 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1974 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1975 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1976 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1977 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1978 | /* Check if we run on the same thread than the xreator thread. |
| 1979 | * We cannot access to the socket if the thread is different. |
| 1980 | */ |
| 1981 | if (socket->tid != tid) |
| 1982 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1983 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1984 | /* check for pattern. */ |
| 1985 | if (lua_gettop(L) >= 2) { |
| 1986 | type = lua_type(L, 2); |
| 1987 | if (type == LUA_TSTRING) { |
| 1988 | pattern = lua_tostring(L, 2); |
| 1989 | if (strcmp(pattern, "*a") == 0) |
| 1990 | wanted = HLSR_READ_ALL; |
| 1991 | else if (strcmp(pattern, "*l") == 0) |
| 1992 | wanted = HLSR_READ_LINE; |
| 1993 | else { |
| 1994 | wanted = strtoll(pattern, &error, 10); |
| 1995 | if (*error != '\0') |
| 1996 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1997 | } |
| 1998 | } |
| 1999 | else if (type == LUA_TNUMBER) { |
| 2000 | wanted = lua_tointeger(L, 2); |
| 2001 | if (wanted < 0) |
| 2002 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2003 | } |
| 2004 | } |
| 2005 | |
| 2006 | /* Set pattern. */ |
| 2007 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2008 | |
| 2009 | /* Check if we would replace the top by itself. */ |
| 2010 | if (lua_gettop(L) != 2) |
| 2011 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2012 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2013 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2014 | luaL_buffinit(L, &socket->b); |
| 2015 | |
| 2016 | /* Check prefix. */ |
| 2017 | if (lua_gettop(L) >= 3) { |
| 2018 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2019 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2020 | pattern = lua_tolstring(L, 3, &len); |
| 2021 | luaL_addlstring(&socket->b, pattern, len); |
| 2022 | } |
| 2023 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2024 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2025 | } |
| 2026 | |
| 2027 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2028 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2029 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2030 | 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] | 2031 | { |
| 2032 | struct hlua_socket *socket; |
| 2033 | struct hlua *hlua = hlua_gethlua(L); |
| 2034 | struct appctx *appctx; |
| 2035 | size_t buf_len; |
| 2036 | const char *buf; |
| 2037 | int len; |
| 2038 | int send_len; |
| 2039 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2040 | struct xref *peer; |
| 2041 | struct stream_interface *si; |
| 2042 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2043 | |
| 2044 | /* Check if this lua stack is schedulable. */ |
| 2045 | if (!hlua || !hlua->task) |
| 2046 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2047 | "'frontend', 'backend' or 'task'")); |
| 2048 | |
| 2049 | /* Get object */ |
| 2050 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2051 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2052 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2053 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2054 | /* Check if we run on the same thread than the xreator thread. |
| 2055 | * We cannot access to the socket if the thread is different. |
| 2056 | */ |
| 2057 | if (socket->tid != tid) |
| 2058 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2059 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2060 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2061 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2062 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2063 | lua_pushinteger(L, -1); |
| 2064 | return 1; |
| 2065 | } |
| 2066 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2067 | si = appctx->owner; |
| 2068 | s = si_strm(si); |
| 2069 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2070 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2071 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2072 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2073 | lua_pushinteger(L, -1); |
| 2074 | return 1; |
| 2075 | } |
| 2076 | |
| 2077 | /* Update the input buffer data. */ |
| 2078 | buf += sent; |
| 2079 | send_len = buf_len - sent; |
| 2080 | |
| 2081 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2082 | if (sent >= buf_len) { |
| 2083 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2084 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2085 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2086 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2087 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2088 | * the request buffer if its not required. |
| 2089 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2090 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2091 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2092 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2093 | } |
| 2094 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2095 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2096 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2097 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2098 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2099 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2100 | |
| 2101 | /* send data */ |
| 2102 | if (len < send_len) |
| 2103 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2104 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2105 | |
| 2106 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2107 | * the data" (-2) are not expected because the available length |
| 2108 | * is tested. |
| 2109 | * Other unknown error are also not expected. |
| 2110 | */ |
| 2111 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2112 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2113 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2114 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2115 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2116 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2117 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2118 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2119 | return 1; |
| 2120 | } |
| 2121 | |
| 2122 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2123 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2124 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2125 | s->req.rex = TICK_ETERNITY; |
| 2126 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2127 | |
| 2128 | /* Update length sent. */ |
| 2129 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2130 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2131 | |
| 2132 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2133 | if (sent + len >= buf_len) { |
| 2134 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2135 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2136 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2137 | |
| 2138 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2139 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2140 | xref_unlock(&socket->xref, peer); |
| 2141 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2142 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2143 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2144 | 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] | 2145 | return 0; |
| 2146 | } |
| 2147 | |
| 2148 | /* This function initiate the send of data. It just check the input |
| 2149 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2150 | * 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] | 2151 | * "hlua_socket_write_yield" that can yield. |
| 2152 | * |
| 2153 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2154 | * the associated object. The second is a string buffer. The third is |
| 2155 | * a facultative integer that represents where is the buffer position |
| 2156 | * of the start of the data that can send. The first byte is the |
| 2157 | * position "1". The default value is "1". The fourth argument is a |
| 2158 | * facultative integer that represents where is the buffer position |
| 2159 | * of the end of the data that can send. The default is the last byte. |
| 2160 | */ |
| 2161 | static int hlua_socket_send(struct lua_State *L) |
| 2162 | { |
| 2163 | int i; |
| 2164 | int j; |
| 2165 | const char *buf; |
| 2166 | size_t buf_len; |
| 2167 | |
| 2168 | /* Check number of arguments. */ |
| 2169 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2170 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2171 | |
| 2172 | /* Get the string. */ |
| 2173 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2174 | |
| 2175 | /* Get and check j. */ |
| 2176 | if (lua_gettop(L) == 4) { |
| 2177 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2178 | if (j < 0) |
| 2179 | j = buf_len + j + 1; |
| 2180 | if (j > buf_len) |
| 2181 | j = buf_len + 1; |
| 2182 | lua_pop(L, 1); |
| 2183 | } |
| 2184 | else |
| 2185 | j = buf_len; |
| 2186 | |
| 2187 | /* Get and check i. */ |
| 2188 | if (lua_gettop(L) == 3) { |
| 2189 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2190 | if (i < 0) |
| 2191 | i = buf_len + i + 1; |
| 2192 | if (i > buf_len) |
| 2193 | i = buf_len + 1; |
| 2194 | lua_pop(L, 1); |
| 2195 | } else |
| 2196 | i = 1; |
| 2197 | |
| 2198 | /* Check bth i and j. */ |
| 2199 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2200 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2201 | return 1; |
| 2202 | } |
| 2203 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2204 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2205 | return 1; |
| 2206 | } |
| 2207 | if (i == 0) |
| 2208 | i = 1; |
| 2209 | if (j == 0) |
| 2210 | j = 1; |
| 2211 | |
| 2212 | /* Pop the string. */ |
| 2213 | lua_pop(L, 1); |
| 2214 | |
| 2215 | /* Update the buffer length. */ |
| 2216 | buf += i - 1; |
| 2217 | buf_len = j - i + 1; |
| 2218 | lua_pushlstring(L, buf, buf_len); |
| 2219 | |
| 2220 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2221 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2222 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2223 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2224 | } |
| 2225 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2226 | #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] | 2227 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2228 | { |
| 2229 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2230 | int ret; |
| 2231 | int len; |
| 2232 | char *p; |
| 2233 | |
| 2234 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2235 | if (ret <= 0) { |
| 2236 | lua_pushnil(L); |
| 2237 | return 1; |
| 2238 | } |
| 2239 | |
| 2240 | if (ret == AF_UNIX) { |
| 2241 | lua_pushstring(L, buffer+1); |
| 2242 | return 1; |
| 2243 | } |
| 2244 | else if (ret == AF_INET6) { |
| 2245 | buffer[0] = '['; |
| 2246 | len = strlen(buffer); |
| 2247 | buffer[len] = ']'; |
| 2248 | len++; |
| 2249 | buffer[len] = ':'; |
| 2250 | len++; |
| 2251 | p = buffer; |
| 2252 | } |
| 2253 | else if (ret == AF_INET) { |
| 2254 | p = buffer + 1; |
| 2255 | len = strlen(p); |
| 2256 | p[len] = ':'; |
| 2257 | len++; |
| 2258 | } |
| 2259 | else { |
| 2260 | lua_pushnil(L); |
| 2261 | return 1; |
| 2262 | } |
| 2263 | |
| 2264 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2265 | lua_pushnil(L); |
| 2266 | return 1; |
| 2267 | } |
| 2268 | |
| 2269 | lua_pushstring(L, p); |
| 2270 | return 1; |
| 2271 | } |
| 2272 | |
| 2273 | /* Returns information about the peer of the connection. */ |
| 2274 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2275 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2276 | struct hlua_socket *socket; |
| 2277 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2278 | struct xref *peer; |
| 2279 | struct appctx *appctx; |
| 2280 | struct stream_interface *si; |
| 2281 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2282 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2283 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2284 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2285 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2286 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2287 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2288 | /* Check if we run on the same thread than the xreator thread. |
| 2289 | * We cannot access to the socket if the thread is different. |
| 2290 | */ |
| 2291 | if (socket->tid != tid) |
| 2292 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2293 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2294 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2295 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2296 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | lua_pushnil(L); |
| 2298 | return 1; |
| 2299 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2300 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2301 | si = appctx->owner; |
| 2302 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2303 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2304 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2305 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2306 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2307 | lua_pushnil(L); |
| 2308 | return 1; |
| 2309 | } |
| 2310 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2311 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2312 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2313 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2314 | lua_pushnil(L); |
| 2315 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2316 | } |
| 2317 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2318 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2319 | xref_unlock(&socket->xref, peer); |
| 2320 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | } |
| 2322 | |
| 2323 | /* Returns information about my connection side. */ |
| 2324 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2325 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2326 | struct hlua_socket *socket; |
| 2327 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2328 | struct appctx *appctx; |
| 2329 | struct xref *peer; |
| 2330 | struct stream_interface *si; |
| 2331 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2332 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2333 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2334 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2335 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2336 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2337 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2338 | /* Check if we run on the same thread than the xreator thread. |
| 2339 | * We cannot access to the socket if the thread is different. |
| 2340 | */ |
| 2341 | if (socket->tid != tid) |
| 2342 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2343 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2344 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2345 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2346 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2347 | lua_pushnil(L); |
| 2348 | return 1; |
| 2349 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2350 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2351 | si = appctx->owner; |
| 2352 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2353 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2354 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2355 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2356 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2357 | lua_pushnil(L); |
| 2358 | return 1; |
| 2359 | } |
| 2360 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2361 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2362 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2363 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2364 | lua_pushnil(L); |
| 2365 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2366 | } |
| 2367 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2368 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2369 | xref_unlock(&socket->xref, peer); |
| 2370 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2374 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2375 | .obj_type = OBJ_TYPE_APPLET, |
| 2376 | .name = "<LUA_TCP>", |
| 2377 | .fct = hlua_socket_handler, |
| 2378 | .release = hlua_socket_release, |
| 2379 | }; |
| 2380 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2381 | __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] | 2382 | { |
| 2383 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2384 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2385 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2386 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2387 | struct stream_interface *si; |
| 2388 | struct stream *s; |
| 2389 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2390 | /* Check if we run on the same thread than the xreator thread. |
| 2391 | * We cannot access to the socket if the thread is different. |
| 2392 | */ |
| 2393 | if (socket->tid != tid) |
| 2394 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2395 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2396 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2397 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2398 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2399 | lua_pushnil(L); |
| 2400 | lua_pushstring(L, "Can't connect"); |
| 2401 | return 2; |
| 2402 | } |
| 2403 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2404 | si = appctx->owner; |
| 2405 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2406 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2407 | /* Check if we run on the same thread than the xreator thread. |
| 2408 | * We cannot access to the socket if the thread is different. |
| 2409 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2410 | if (socket->tid != tid) { |
| 2411 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2412 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2413 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2414 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2415 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2416 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2417 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2418 | lua_pushnil(L); |
| 2419 | lua_pushstring(L, "Can't connect"); |
| 2420 | return 2; |
| 2421 | } |
| 2422 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2423 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2424 | |
| 2425 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2426 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2427 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2428 | lua_pushinteger(L, 1); |
| 2429 | return 1; |
| 2430 | } |
| 2431 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2432 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2433 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2434 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2435 | } |
| 2436 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2437 | 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] | 2438 | return 0; |
| 2439 | } |
| 2440 | |
| 2441 | /* This function fail or initite the connection. */ |
| 2442 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2443 | { |
| 2444 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2445 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2446 | const char *ip; |
| 2447 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2448 | struct hlua *hlua; |
| 2449 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2450 | int low, high; |
| 2451 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2452 | struct xref *peer; |
| 2453 | struct stream_interface *si; |
| 2454 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2455 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2456 | if (lua_gettop(L) < 2) |
| 2457 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2458 | |
| 2459 | /* Get args. */ |
| 2460 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2461 | |
| 2462 | /* Check if we run on the same thread than the xreator thread. |
| 2463 | * We cannot access to the socket if the thread is different. |
| 2464 | */ |
| 2465 | if (socket->tid != tid) |
| 2466 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2467 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2468 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2469 | if (lua_gettop(L) >= 3) { |
| 2470 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2471 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2472 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2473 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2474 | * that are not enclosed within square brackets. |
| 2475 | */ |
| 2476 | if (port > 0) { |
| 2477 | luaL_buffinit(L, &b); |
| 2478 | luaL_addstring(&b, ip); |
| 2479 | luaL_addchar(&b, ':'); |
| 2480 | luaL_pushresult(&b); |
| 2481 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2482 | } |
| 2483 | } |
| 2484 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2485 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2486 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2487 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2488 | lua_pushnil(L); |
| 2489 | return 1; |
| 2490 | } |
| 2491 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2492 | si = appctx->owner; |
| 2493 | s = si_strm(si); |
| 2494 | |
| 2495 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2496 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2497 | if (!conn) { |
| 2498 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2499 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2500 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2501 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2502 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2503 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2504 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2505 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2506 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2507 | if (!addr) { |
| 2508 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2509 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2510 | } |
| 2511 | if (low != high) { |
| 2512 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2513 | 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] | 2514 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2515 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2516 | |
| 2517 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2518 | if (low == 0) { |
| 2519 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2520 | if (port == -1) { |
| 2521 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2522 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2523 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2524 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2525 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2526 | if (port == -1) { |
| 2527 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2528 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2529 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2530 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2531 | } |
| 2532 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2533 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2534 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2535 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2536 | |
| 2537 | /* inform the stream that we want to be notified whenever the |
| 2538 | * connection completes. |
| 2539 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2540 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2541 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2542 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2543 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2544 | hlua->flags |= HLUA_MUST_GC; |
| 2545 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2546 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2547 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2548 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2549 | } |
| 2550 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2551 | |
| 2552 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2553 | /* Return yield waiting for connection. */ |
| 2554 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2555 | 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] | 2556 | |
| 2557 | return 0; |
| 2558 | } |
| 2559 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2560 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2561 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2562 | { |
| 2563 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2564 | struct xref *peer; |
| 2565 | struct appctx *appctx; |
| 2566 | struct stream_interface *si; |
| 2567 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2568 | |
| 2569 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2570 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2571 | |
| 2572 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2573 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2574 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2575 | lua_pushnil(L); |
| 2576 | return 1; |
| 2577 | } |
| 2578 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2579 | si = appctx->owner; |
| 2580 | s = si_strm(si); |
| 2581 | |
| 2582 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2583 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2584 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2585 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2586 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2587 | |
| 2588 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2589 | { |
| 2590 | return 0; |
| 2591 | } |
| 2592 | |
| 2593 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2594 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2595 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2596 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2597 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2598 | struct xref *peer; |
| 2599 | struct appctx *appctx; |
| 2600 | struct stream_interface *si; |
| 2601 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2602 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2603 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2604 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2605 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2606 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2607 | /* convert the timeout to millis */ |
| 2608 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2609 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2610 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2611 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2612 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2613 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2614 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2615 | 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] | 2616 | |
| 2617 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2618 | if (tmout == 0) |
| 2619 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2620 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2621 | /* Check if we run on the same thread than the xreator thread. |
| 2622 | * We cannot access to the socket if the thread is different. |
| 2623 | */ |
| 2624 | if (socket->tid != tid) |
| 2625 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2626 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2627 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2628 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2629 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2630 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2631 | WILL_LJMP(lua_error(L)); |
| 2632 | return 0; |
| 2633 | } |
| 2634 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2635 | si = appctx->owner; |
| 2636 | s = si_strm(si); |
| 2637 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2638 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2639 | s->req.rto = tmout; |
| 2640 | s->req.wto = tmout; |
| 2641 | s->res.rto = tmout; |
| 2642 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2643 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2644 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2645 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2646 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2647 | |
| 2648 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2649 | task_queue(s->task); |
| 2650 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2651 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2652 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2653 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2654 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2655 | } |
| 2656 | |
| 2657 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2658 | { |
| 2659 | struct hlua_socket *socket; |
| 2660 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2661 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2662 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2663 | |
| 2664 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2665 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2666 | hlua_pusherror(L, "socket: full stack"); |
| 2667 | goto out_fail_conf; |
| 2668 | } |
| 2669 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2670 | /* Create the object: obj[0] = userdata. */ |
| 2671 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2672 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2673 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2674 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2675 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2676 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2677 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2678 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2679 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2680 | goto out_fail_conf; |
| 2681 | } |
| 2682 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2683 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2684 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2685 | lua_setmetatable(L, -2); |
| 2686 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2687 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2688 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2689 | if (!appctx) { |
| 2690 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2691 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2692 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2693 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2694 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2695 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2696 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2697 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2698 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2699 | /* Now create a session, task and stream for this applet */ |
| 2700 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2701 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2702 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2703 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2704 | } |
| 2705 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2706 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2707 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2708 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2709 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2710 | } |
| 2711 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2712 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2713 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2714 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2715 | /* Configure "right" stream interface. this "si" is used to connect |
| 2716 | * and retrieve data from the server. The connection is initialized |
| 2717 | * with the "struct server". |
| 2718 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2719 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2720 | |
| 2721 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2722 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2723 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2724 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2725 | return 1; |
| 2726 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2727 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2728 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2729 | out_fail_sess: |
| 2730 | appctx_free(appctx); |
| 2731 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2732 | WILL_LJMP(lua_error(L)); |
| 2733 | return 0; |
| 2734 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2735 | |
| 2736 | /* |
| 2737 | * |
| 2738 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2739 | * Class Channel |
| 2740 | * |
| 2741 | * |
| 2742 | */ |
| 2743 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2744 | /* This function is called before the Lua execution. It stores |
| 2745 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2746 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2747 | 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] | 2748 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2749 | c->mode = stream->be->mode; |
| 2750 | switch (c->mode) { |
| 2751 | case PR_MODE_HTTP: |
| 2752 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2753 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2754 | c->data.http.state = stream->txn->req.msg_state; |
| 2755 | else |
| 2756 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2757 | break; |
| 2758 | default: |
| 2759 | break; |
| 2760 | } |
| 2761 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2762 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2763 | /* This function is called after the Lua execution. it |
| 2764 | * returns true if the parser state is consistent, otherwise, |
| 2765 | * it return false. |
| 2766 | * |
| 2767 | * In HTTP mode, the parser state must be in the same state |
| 2768 | * or greater when we exit the function. Even if we do a |
| 2769 | * control yield. This prevent to break the HTTP message |
| 2770 | * from the Lua code. |
| 2771 | */ |
| 2772 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2773 | { |
| 2774 | if (c->mode != stream->be->mode) |
| 2775 | return 0; |
| 2776 | |
| 2777 | switch (c->mode) { |
| 2778 | case PR_MODE_HTTP: |
| 2779 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2780 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2781 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2782 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2783 | else |
| 2784 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2785 | default: |
| 2786 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2787 | } |
| 2788 | return 1; |
| 2789 | } |
| 2790 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2791 | /* Returns the struct hlua_channel join to the class channel in the |
| 2792 | * stack entry "ud" or throws an argument error. |
| 2793 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2794 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2795 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2796 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2797 | } |
| 2798 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2799 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2800 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2801 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2802 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2803 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2804 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2805 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2806 | return 0; |
| 2807 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2808 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2809 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2810 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2811 | |
| 2812 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2813 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2814 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2815 | return 1; |
| 2816 | } |
| 2817 | |
| 2818 | /* Duplicate all the data present in the input channel and put it |
| 2819 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2820 | * the stack if the channel is closed and all the data are consumed, |
| 2821 | * returns 0 if no data are available, otherwise it returns the length |
| 2822 | * of the builded string. |
| 2823 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2824 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2825 | { |
| 2826 | char *blk1; |
| 2827 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2828 | size_t len1; |
| 2829 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2830 | int ret; |
| 2831 | luaL_Buffer b; |
| 2832 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2833 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2834 | if (unlikely(ret == 0)) |
| 2835 | return 0; |
| 2836 | |
| 2837 | if (unlikely(ret < 0)) { |
| 2838 | lua_pushnil(L); |
| 2839 | return -1; |
| 2840 | } |
| 2841 | |
| 2842 | luaL_buffinit(L, &b); |
| 2843 | luaL_addlstring(&b, blk1, len1); |
| 2844 | if (unlikely(ret == 2)) |
| 2845 | luaL_addlstring(&b, blk2, len2); |
| 2846 | luaL_pushresult(&b); |
| 2847 | |
| 2848 | if (unlikely(ret == 2)) |
| 2849 | return len1 + len2; |
| 2850 | return len1; |
| 2851 | } |
| 2852 | |
| 2853 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2854 | * a yield. This function keep the data in the buffer. |
| 2855 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2856 | __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] | 2857 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2858 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2859 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2860 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2861 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2862 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2863 | WILL_LJMP(lua_error(L)); |
| 2864 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2865 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2866 | 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] | 2867 | return 1; |
| 2868 | } |
| 2869 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2870 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2871 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2872 | { |
| 2873 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2874 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2875 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2876 | } |
| 2877 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2878 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2879 | * a yield. This function consumes the data in the buffer. It returns |
| 2880 | * a string containing the data or a nil pointer if no data are available |
| 2881 | * and the channel is closed. |
| 2882 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2883 | __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] | 2884 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2885 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2886 | int ret; |
| 2887 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2888 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2889 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2890 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2891 | WILL_LJMP(lua_error(L)); |
| 2892 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2893 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2894 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2895 | 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] | 2896 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2897 | if (unlikely(ret == -1)) |
| 2898 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2899 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2900 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2901 | return 1; |
| 2902 | } |
| 2903 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2904 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2905 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2906 | { |
| 2907 | MAY_LJMP(check_args(L, 1, "get")); |
| 2908 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2909 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2910 | } |
| 2911 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2912 | /* This functions consumes and returns one line. If the channel is closed, |
| 2913 | * 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] | 2914 | * 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] | 2915 | * value. |
| 2916 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2917 | __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] | 2918 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2919 | char *blk1; |
| 2920 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2921 | size_t len1; |
| 2922 | size_t len2; |
| 2923 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2924 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2925 | int ret; |
| 2926 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2927 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2928 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2929 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2930 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2931 | WILL_LJMP(lua_error(L)); |
| 2932 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2933 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2934 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2935 | 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] | 2936 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2937 | if (ret == -1) { |
| 2938 | lua_pushnil(L); |
| 2939 | return 1; |
| 2940 | } |
| 2941 | |
| 2942 | luaL_buffinit(L, &b); |
| 2943 | luaL_addlstring(&b, blk1, len1); |
| 2944 | len = len1; |
| 2945 | if (unlikely(ret == 2)) { |
| 2946 | luaL_addlstring(&b, blk2, len2); |
| 2947 | len += len2; |
| 2948 | } |
| 2949 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2950 | 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] | 2951 | return 1; |
| 2952 | } |
| 2953 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2954 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2955 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2956 | { |
| 2957 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2958 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2959 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2960 | } |
| 2961 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2962 | /* This function takes a string as input, and append it at the |
| 2963 | * input side of channel. If the data is too big, but a space |
| 2964 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2965 | * yields. If the data is bigger than the buffer, or if the |
| 2966 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2967 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2968 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2969 | __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] | 2970 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2971 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2972 | size_t len; |
| 2973 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2974 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2975 | int ret; |
| 2976 | int max; |
| 2977 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2978 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2979 | WILL_LJMP(lua_error(L)); |
| 2980 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2981 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2982 | * the request buffer if its not required. |
| 2983 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2984 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2985 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2986 | 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] | 2987 | } |
| 2988 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2989 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2990 | if (max > len - l) |
| 2991 | max = len - l; |
| 2992 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2993 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2994 | if (ret == -2 || ret == -3) { |
| 2995 | lua_pushinteger(L, -1); |
| 2996 | return 1; |
| 2997 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2998 | if (ret == -1) { |
| 2999 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3000 | 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] | 3001 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3002 | l += ret; |
| 3003 | lua_pop(L, 1); |
| 3004 | lua_pushinteger(L, l); |
| 3005 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3006 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3007 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3008 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3009 | * in this case, we cannot add more data, so we cannot yield, |
| 3010 | * we return the amount of copyied data. |
| 3011 | */ |
| 3012 | return 1; |
| 3013 | } |
| 3014 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3015 | 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] | 3016 | return 1; |
| 3017 | } |
| 3018 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3019 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 3020 | * 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] | 3021 | * buffer size is too little for the data. |
| 3022 | */ |
| 3023 | __LJMP static int hlua_channel_append(lua_State *L) |
| 3024 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3025 | size_t len; |
| 3026 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3027 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3028 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3029 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3030 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3031 | lua_pushinteger(L, 0); |
| 3032 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3033 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3034 | } |
| 3035 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3036 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3037 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3038 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3039 | * or -1 if the channel is closed or if the buffer size is too |
| 3040 | * little for the data. |
| 3041 | */ |
| 3042 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3043 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3044 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3045 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3046 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3047 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3048 | lua_pushinteger(L, 0); |
| 3049 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3050 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3051 | WILL_LJMP(lua_error(L)); |
| 3052 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3053 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3054 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3055 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3056 | } |
| 3057 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3058 | /* Append data in the output side of the buffer. This data is immediately |
| 3059 | * sent. The function returns the amount of data written. If the buffer |
| 3060 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3061 | * if the channel is closed. |
| 3062 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3063 | __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] | 3064 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3065 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3066 | size_t len; |
| 3067 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3068 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3069 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3070 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3071 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3072 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3073 | WILL_LJMP(lua_error(L)); |
| 3074 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3075 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3076 | lua_pushinteger(L, -1); |
| 3077 | return 1; |
| 3078 | } |
| 3079 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3080 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3081 | * the request buffer if its not required. |
| 3082 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3083 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3084 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3085 | 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] | 3086 | } |
| 3087 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3088 | /* The written data will be immediately sent, so we can check |
| 3089 | * the available space without taking in account the reserve. |
| 3090 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3091 | * data, because the buffer will be flushed. |
| 3092 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3093 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3094 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3095 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3096 | * in this case, we cannot add more data, so we cannot yield, |
| 3097 | * we return the amount of copyied data. |
| 3098 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3099 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3100 | return 1; |
| 3101 | |
| 3102 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3103 | if (max > len - l) |
| 3104 | max = len - l; |
| 3105 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3106 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3107 | * detects a non contiguous buffer and realign it. |
| 3108 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3109 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3110 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3111 | |
| 3112 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3113 | 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] | 3114 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3115 | /* buffer replace considers that the input part is filled. |
| 3116 | * so, I must forward these new data in the output part. |
| 3117 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3118 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3119 | |
| 3120 | l += max; |
| 3121 | lua_pop(L, 1); |
| 3122 | lua_pushinteger(L, l); |
| 3123 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3124 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3125 | * in this case, we cannot add more data, so we cannot yield, |
| 3126 | * we return the amount of copyied data. |
| 3127 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3128 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3129 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3130 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3131 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3132 | if (l < len) { |
| 3133 | /* If we are waiting for space in the response buffer, we |
| 3134 | * must set the flag WAKERESWR. This flag required the task |
| 3135 | * wake up if any activity is detected on the response buffer. |
| 3136 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3137 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3138 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3139 | else |
| 3140 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3141 | 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] | 3142 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3143 | |
| 3144 | return 1; |
| 3145 | } |
| 3146 | |
| 3147 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3148 | * yield the LUA process, and resume it without checking the |
| 3149 | * input arguments. |
| 3150 | */ |
| 3151 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3152 | { |
| 3153 | MAY_LJMP(check_args(L, 2, "send")); |
| 3154 | lua_pushinteger(L, 0); |
| 3155 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3156 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | /* This function forward and amount of butes. The data pass from |
| 3160 | * the input side of the buffer to the output side, and can be |
| 3161 | * forwarded. This function never fails. |
| 3162 | * |
| 3163 | * The Lua function takes an amount of bytes to be forwarded in |
| 3164 | * imput. It returns the number of bytes forwarded. |
| 3165 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3166 | __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] | 3167 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3168 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3169 | int len; |
| 3170 | int l; |
| 3171 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3172 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3173 | |
| 3174 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3175 | |
| 3176 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3177 | WILL_LJMP(lua_error(L)); |
| 3178 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3179 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3180 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3181 | |
| 3182 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3183 | if (max > ci_data(chn)) |
| 3184 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3185 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3186 | l += max; |
| 3187 | |
| 3188 | lua_pop(L, 1); |
| 3189 | lua_pushinteger(L, l); |
| 3190 | |
| 3191 | /* Check if it miss bytes to forward. */ |
| 3192 | if (l < len) { |
| 3193 | /* The the input channel or the output channel are closed, we |
| 3194 | * must return the amount of data forwarded. |
| 3195 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3196 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3197 | return 1; |
| 3198 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3199 | /* If we are waiting for space data in the response buffer, we |
| 3200 | * must set the flag WAKERESWR. This flag required the task |
| 3201 | * wake up if any activity is detected on the response buffer. |
| 3202 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3203 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3204 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3205 | else |
| 3206 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3207 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3208 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3209 | 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] | 3210 | } |
| 3211 | |
| 3212 | return 1; |
| 3213 | } |
| 3214 | |
| 3215 | /* Just check the input and prepare the stack for the previous |
| 3216 | * function "hlua_channel_forward_yield" |
| 3217 | */ |
| 3218 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3219 | { |
| 3220 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3221 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3222 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3223 | |
| 3224 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3225 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | /* Just returns the number of bytes available in the input |
| 3229 | * side of the buffer. This function never fails. |
| 3230 | */ |
| 3231 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3232 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3233 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3234 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3235 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3236 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3237 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3238 | struct htx *htx = htxbuf(&chn->buf); |
| 3239 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3240 | } |
| 3241 | else |
| 3242 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3243 | return 1; |
| 3244 | } |
| 3245 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3246 | /* Returns true if the channel is full. */ |
| 3247 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3248 | { |
| 3249 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3250 | |
| 3251 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3252 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0136ebb | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3253 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3254 | * applet). |
| 3255 | */ |
| 3256 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3257 | return 1; |
| 3258 | } |
| 3259 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3260 | /* Just returns the number of bytes available in the output |
| 3261 | * side of the buffer. This function never fails. |
| 3262 | */ |
| 3263 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3264 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3265 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3266 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3267 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3268 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3269 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3270 | return 1; |
| 3271 | } |
| 3272 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3273 | /* |
| 3274 | * |
| 3275 | * |
| 3276 | * Class Fetches |
| 3277 | * |
| 3278 | * |
| 3279 | */ |
| 3280 | |
| 3281 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3282 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3283 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3284 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3285 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3286 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3287 | } |
| 3288 | |
| 3289 | /* This function creates and push in the stack a fetch object according |
| 3290 | * with a current TXN. |
| 3291 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3292 | 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] | 3293 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3294 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3295 | |
| 3296 | /* Check stack size. */ |
| 3297 | if (!lua_checkstack(L, 3)) |
| 3298 | return 0; |
| 3299 | |
| 3300 | /* Create the object: obj[0] = userdata. |
| 3301 | * Note that the base of the Fetches object is the |
| 3302 | * transaction object. |
| 3303 | */ |
| 3304 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3305 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3306 | lua_rawseti(L, -2, 0); |
| 3307 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3308 | hsmp->s = txn->s; |
| 3309 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3310 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3311 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3312 | |
| 3313 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3314 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3315 | lua_setmetatable(L, -2); |
| 3316 | |
| 3317 | return 1; |
| 3318 | } |
| 3319 | |
| 3320 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3321 | * It uses closure argument to store the associated sample-fetch. It |
| 3322 | * returns only one argument or throws an error. An error is thrown |
| 3323 | * only if an error is encountered during the argument parsing. If |
| 3324 | * the "sample-fetch" function fails, nil is returned. |
| 3325 | */ |
| 3326 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3327 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3328 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3329 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3330 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3331 | int i; |
| 3332 | struct sample smp; |
| 3333 | |
| 3334 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3335 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3336 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3337 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3338 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3339 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3340 | /* Check execution authorization. */ |
| 3341 | if (f->use & SMP_USE_HTTP_ANY && |
| 3342 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3343 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3344 | "is not available in Lua services", f->kw); |
| 3345 | WILL_LJMP(lua_error(L)); |
| 3346 | } |
| 3347 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3348 | /* Get extra arguments. */ |
| 3349 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3350 | if (i >= ARGM_NBARGS) |
| 3351 | break; |
| 3352 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3353 | } |
| 3354 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3355 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3356 | |
| 3357 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3358 | 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] | 3359 | |
| 3360 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3361 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3362 | lua_pushfstring(L, "error in arguments"); |
| 3363 | WILL_LJMP(lua_error(L)); |
| 3364 | } |
| 3365 | |
| 3366 | /* Initialise the sample. */ |
| 3367 | memset(&smp, 0, sizeof(smp)); |
| 3368 | |
| 3369 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3370 | 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] | 3371 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3372 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3373 | lua_pushstring(L, ""); |
| 3374 | else |
| 3375 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3376 | return 1; |
| 3377 | } |
| 3378 | |
| 3379 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3380 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3381 | hlua_smp2lua_str(L, &smp); |
| 3382 | else |
| 3383 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3384 | return 1; |
| 3385 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3386 | |
| 3387 | /* |
| 3388 | * |
| 3389 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3390 | * Class Converters |
| 3391 | * |
| 3392 | * |
| 3393 | */ |
| 3394 | |
| 3395 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3396 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3397 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3398 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3399 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3400 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3401 | } |
| 3402 | |
| 3403 | /* This function creates and push in the stack a Converters object |
| 3404 | * according with a current TXN. |
| 3405 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3406 | 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] | 3407 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3408 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3409 | |
| 3410 | /* Check stack size. */ |
| 3411 | if (!lua_checkstack(L, 3)) |
| 3412 | return 0; |
| 3413 | |
| 3414 | /* Create the object: obj[0] = userdata. |
| 3415 | * Note that the base of the Converters object is the |
| 3416 | * same than the TXN object. |
| 3417 | */ |
| 3418 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3419 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3420 | lua_rawseti(L, -2, 0); |
| 3421 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3422 | hsmp->s = txn->s; |
| 3423 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3424 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3425 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3426 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3427 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3428 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3429 | lua_setmetatable(L, -2); |
| 3430 | |
| 3431 | return 1; |
| 3432 | } |
| 3433 | |
| 3434 | /* This function is an LUA binding. It is called with each converter. |
| 3435 | * It uses closure argument to store the associated converter. It |
| 3436 | * returns only one argument or throws an error. An error is thrown |
| 3437 | * only if an error is encountered during the argument parsing. If |
| 3438 | * the converter function function fails, nil is returned. |
| 3439 | */ |
| 3440 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3441 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3442 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3443 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3444 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3445 | int i; |
| 3446 | struct sample smp; |
| 3447 | |
| 3448 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3449 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3450 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3451 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3452 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3453 | |
| 3454 | /* Get extra arguments. */ |
| 3455 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3456 | if (i >= ARGM_NBARGS) |
| 3457 | break; |
| 3458 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3459 | } |
| 3460 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3461 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3462 | |
| 3463 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3464 | 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] | 3465 | |
| 3466 | /* Run the special args checker. */ |
| 3467 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3468 | hlua_pusherror(L, "error in arguments"); |
| 3469 | WILL_LJMP(lua_error(L)); |
| 3470 | } |
| 3471 | |
| 3472 | /* Initialise the sample. */ |
Amaury Denoyelle | ca492f3 | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3473 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3474 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3475 | hlua_pusherror(L, "error in the input argument"); |
| 3476 | WILL_LJMP(lua_error(L)); |
| 3477 | } |
| 3478 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3479 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3480 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3481 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3482 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3483 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3484 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3485 | WILL_LJMP(lua_error(L)); |
| 3486 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3487 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3488 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3489 | hlua_pusherror(L, "error during the input argument casting"); |
| 3490 | WILL_LJMP(lua_error(L)); |
| 3491 | } |
| 3492 | |
| 3493 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3494 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3495 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3496 | lua_pushstring(L, ""); |
| 3497 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3498 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3499 | return 1; |
| 3500 | } |
| 3501 | |
| 3502 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3503 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3504 | hlua_smp2lua_str(L, &smp); |
| 3505 | else |
| 3506 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3507 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3508 | } |
| 3509 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3510 | /* |
| 3511 | * |
| 3512 | * |
| 3513 | * Class AppletTCP |
| 3514 | * |
| 3515 | * |
| 3516 | */ |
| 3517 | |
| 3518 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3519 | * a class stream, otherwise it throws an error. |
| 3520 | */ |
| 3521 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3522 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3523 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3524 | } |
| 3525 | |
| 3526 | /* This function creates and push in the stack an Applet object |
| 3527 | * according with a current TXN. |
| 3528 | */ |
| 3529 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3530 | { |
| 3531 | struct hlua_appctx *appctx; |
| 3532 | struct stream_interface *si = ctx->owner; |
| 3533 | struct stream *s = si_strm(si); |
| 3534 | struct proxy *p = s->be; |
| 3535 | |
| 3536 | /* Check stack size. */ |
| 3537 | if (!lua_checkstack(L, 3)) |
| 3538 | return 0; |
| 3539 | |
| 3540 | /* Create the object: obj[0] = userdata. |
| 3541 | * Note that the base of the Converters object is the |
| 3542 | * same than the TXN object. |
| 3543 | */ |
| 3544 | lua_newtable(L); |
| 3545 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3546 | lua_rawseti(L, -2, 0); |
| 3547 | appctx->appctx = ctx; |
| 3548 | appctx->htxn.s = s; |
| 3549 | appctx->htxn.p = p; |
| 3550 | |
| 3551 | /* Create the "f" field that contains a list of fetches. */ |
| 3552 | lua_pushstring(L, "f"); |
| 3553 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3554 | return 0; |
| 3555 | lua_settable(L, -3); |
| 3556 | |
| 3557 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3558 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3559 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3560 | return 0; |
| 3561 | lua_settable(L, -3); |
| 3562 | |
| 3563 | /* Create the "c" field that contains a list of converters. */ |
| 3564 | lua_pushstring(L, "c"); |
| 3565 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3566 | return 0; |
| 3567 | lua_settable(L, -3); |
| 3568 | |
| 3569 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3570 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3571 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3572 | return 0; |
| 3573 | lua_settable(L, -3); |
| 3574 | |
| 3575 | /* Pop a class stream metatable and affect it to the table. */ |
| 3576 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3577 | lua_setmetatable(L, -2); |
| 3578 | |
| 3579 | return 1; |
| 3580 | } |
| 3581 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3582 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3583 | { |
| 3584 | struct hlua_appctx *appctx; |
| 3585 | struct stream *s; |
| 3586 | const char *name; |
| 3587 | size_t len; |
| 3588 | struct sample smp; |
| 3589 | |
| 3590 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3591 | |
| 3592 | /* It is useles to retrieve the stream, but this function |
| 3593 | * runs only in a stream context. |
| 3594 | */ |
| 3595 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3596 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3597 | s = appctx->htxn.s; |
| 3598 | |
| 3599 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | ca492f3 | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 3600 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3601 | hlua_lua2smp(L, 3, &smp); |
| 3602 | |
| 3603 | /* Store the sample in a variable. */ |
| 3604 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3605 | vars_set_by_name(name, len, &smp); |
| 3606 | return 0; |
| 3607 | } |
| 3608 | |
| 3609 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3610 | { |
| 3611 | struct hlua_appctx *appctx; |
| 3612 | struct stream *s; |
| 3613 | const char *name; |
| 3614 | size_t len; |
| 3615 | struct sample smp; |
| 3616 | |
| 3617 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3618 | |
| 3619 | /* It is useles to retrieve the stream, but this function |
| 3620 | * runs only in a stream context. |
| 3621 | */ |
| 3622 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3623 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3624 | s = appctx->htxn.s; |
| 3625 | |
| 3626 | /* Unset the variable. */ |
| 3627 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3628 | vars_unset_by_name(name, len, &smp); |
| 3629 | return 0; |
| 3630 | } |
| 3631 | |
| 3632 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3633 | { |
| 3634 | struct hlua_appctx *appctx; |
| 3635 | struct stream *s; |
| 3636 | const char *name; |
| 3637 | size_t len; |
| 3638 | struct sample smp; |
| 3639 | |
| 3640 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3641 | |
| 3642 | /* It is useles to retrieve the stream, but this function |
| 3643 | * runs only in a stream context. |
| 3644 | */ |
| 3645 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3646 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3647 | s = appctx->htxn.s; |
| 3648 | |
| 3649 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3650 | if (!vars_get_by_name(name, len, &smp)) { |
| 3651 | lua_pushnil(L); |
| 3652 | return 1; |
| 3653 | } |
| 3654 | |
| 3655 | return hlua_smp2lua(L, &smp); |
| 3656 | } |
| 3657 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3658 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3659 | { |
| 3660 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3661 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3662 | struct hlua *hlua; |
| 3663 | |
| 3664 | /* 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] | 3665 | if (!s->hlua) |
| 3666 | return 0; |
| 3667 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3668 | |
| 3669 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3670 | |
| 3671 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3672 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3673 | |
| 3674 | /* Get and store new value. */ |
| 3675 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3676 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3677 | |
| 3678 | return 0; |
| 3679 | } |
| 3680 | |
| 3681 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3682 | { |
| 3683 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3684 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3685 | struct hlua *hlua; |
| 3686 | |
| 3687 | /* 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] | 3688 | if (!s->hlua) { |
| 3689 | lua_pushnil(L); |
| 3690 | return 1; |
| 3691 | } |
| 3692 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3693 | |
| 3694 | /* Push configuration index in the stack. */ |
| 3695 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3696 | |
| 3697 | return 1; |
| 3698 | } |
| 3699 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3700 | /* If expected data not yet available, it returns a yield. This function |
| 3701 | * consumes the data in the buffer. It returns a string containing the |
| 3702 | * data. This string can be empty. |
| 3703 | */ |
| 3704 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3705 | { |
| 3706 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3707 | struct stream_interface *si = appctx->appctx->owner; |
| 3708 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3709 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3710 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3711 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3712 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3713 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3714 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3715 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3716 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3717 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3718 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3719 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3720 | 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] | 3721 | } |
| 3722 | |
| 3723 | /* End of data: commit the total strings and return. */ |
| 3724 | if (ret < 0) { |
| 3725 | luaL_pushresult(&appctx->b); |
| 3726 | return 1; |
| 3727 | } |
| 3728 | |
| 3729 | /* Ensure that the block 2 length is usable. */ |
| 3730 | if (ret == 1) |
| 3731 | len2 = 0; |
| 3732 | |
| 3733 | /* dont check the max length read and dont check. */ |
| 3734 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3735 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3736 | |
| 3737 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3738 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3739 | luaL_pushresult(&appctx->b); |
| 3740 | return 1; |
| 3741 | } |
| 3742 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3743 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3744 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3745 | { |
| 3746 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3747 | |
| 3748 | /* Initialise the string catenation. */ |
| 3749 | luaL_buffinit(L, &appctx->b); |
| 3750 | |
| 3751 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3752 | } |
| 3753 | |
| 3754 | /* If expected data not yet available, it returns a yield. This function |
| 3755 | * consumes the data in the buffer. It returns a string containing the |
| 3756 | * data. This string can be empty. |
| 3757 | */ |
| 3758 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3759 | { |
| 3760 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3761 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3762 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3763 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3764 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3765 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3766 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3767 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3768 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3769 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3770 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3771 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3772 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3773 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3774 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3775 | 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] | 3776 | } |
| 3777 | |
| 3778 | /* End of data: commit the total strings and return. */ |
| 3779 | if (ret < 0) { |
| 3780 | luaL_pushresult(&appctx->b); |
| 3781 | return 1; |
| 3782 | } |
| 3783 | |
| 3784 | /* Ensure that the block 2 length is usable. */ |
| 3785 | if (ret == 1) |
| 3786 | len2 = 0; |
| 3787 | |
| 3788 | if (len == -1) { |
| 3789 | |
| 3790 | /* If len == -1, catenate all the data avalaile and |
| 3791 | * yield because we want to get all the data until |
| 3792 | * the end of data stream. |
| 3793 | */ |
| 3794 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3795 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3796 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3797 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3798 | 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] | 3799 | |
| 3800 | } else { |
| 3801 | |
| 3802 | /* Copy the fisrt block caping to the length required. */ |
| 3803 | if (len1 > len) |
| 3804 | len1 = len; |
| 3805 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3806 | len -= len1; |
| 3807 | |
| 3808 | /* Copy the second block. */ |
| 3809 | if (len2 > len) |
| 3810 | len2 = len; |
| 3811 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3812 | len -= len2; |
| 3813 | |
| 3814 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3815 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3816 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3817 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3818 | if (len > 0) { |
| 3819 | lua_pushinteger(L, len); |
| 3820 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3821 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3822 | 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] | 3823 | } |
| 3824 | |
| 3825 | /* return the result. */ |
| 3826 | luaL_pushresult(&appctx->b); |
| 3827 | return 1; |
| 3828 | } |
| 3829 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3830 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3831 | hlua_pusherror(L, "Lua: internal error"); |
| 3832 | WILL_LJMP(lua_error(L)); |
| 3833 | return 0; |
| 3834 | } |
| 3835 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3836 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3837 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3838 | { |
| 3839 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3840 | int len = -1; |
| 3841 | |
| 3842 | if (lua_gettop(L) > 2) |
| 3843 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3844 | if (lua_gettop(L) >= 2) { |
| 3845 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3846 | lua_pop(L, 1); |
| 3847 | } |
| 3848 | |
| 3849 | /* Confirm or set the required length */ |
| 3850 | lua_pushinteger(L, len); |
| 3851 | |
| 3852 | /* Initialise the string catenation. */ |
| 3853 | luaL_buffinit(L, &appctx->b); |
| 3854 | |
| 3855 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3856 | } |
| 3857 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3858 | /* Append data in the output side of the buffer. This data is immediately |
| 3859 | * sent. The function returns the amount of data written. If the buffer |
| 3860 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3861 | * if the channel is closed. |
| 3862 | */ |
| 3863 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3864 | { |
| 3865 | size_t len; |
| 3866 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3867 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3868 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3869 | struct stream_interface *si = appctx->appctx->owner; |
| 3870 | struct channel *chn = si_ic(si); |
| 3871 | int max; |
| 3872 | |
| 3873 | /* Get the max amount of data which can write as input in the channel. */ |
| 3874 | max = channel_recv_max(chn); |
| 3875 | if (max > (len - l)) |
| 3876 | max = len - l; |
| 3877 | |
| 3878 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3879 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3880 | |
| 3881 | /* update counters. */ |
| 3882 | l += max; |
| 3883 | lua_pop(L, 1); |
| 3884 | lua_pushinteger(L, l); |
| 3885 | |
| 3886 | /* If some data is not send, declares the situation to the |
| 3887 | * applet, and returns a yield. |
| 3888 | */ |
| 3889 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3890 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3891 | 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] | 3892 | } |
| 3893 | |
| 3894 | return 1; |
| 3895 | } |
| 3896 | |
| 3897 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3898 | * yield the LUA process, and resume it without checking the |
| 3899 | * input arguments. |
| 3900 | */ |
| 3901 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3902 | { |
| 3903 | MAY_LJMP(check_args(L, 2, "send")); |
| 3904 | lua_pushinteger(L, 0); |
| 3905 | |
| 3906 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3907 | } |
| 3908 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3909 | /* |
| 3910 | * |
| 3911 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3912 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3913 | * |
| 3914 | * |
| 3915 | */ |
| 3916 | |
| 3917 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3918 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3919 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3920 | __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] | 3921 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3922 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3923 | } |
| 3924 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3925 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3926 | * according with a current TXN. |
| 3927 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3928 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3929 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3930 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3931 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3932 | struct stream_interface *si = ctx->owner; |
| 3933 | struct stream *s = si_strm(si); |
| 3934 | struct proxy *px = s->be; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3935 | |
| 3936 | /* Check stack size. */ |
| 3937 | if (!lua_checkstack(L, 3)) |
| 3938 | return 0; |
| 3939 | |
| 3940 | /* Create the object: obj[0] = userdata. |
| 3941 | * Note that the base of the Converters object is the |
| 3942 | * same than the TXN object. |
| 3943 | */ |
| 3944 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3945 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3946 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3947 | appctx->appctx = ctx; |
| 3948 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3949 | 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] | 3950 | appctx->htxn.s = s; |
| 3951 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3952 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3953 | /* Create the "f" field that contains a list of fetches. */ |
| 3954 | lua_pushstring(L, "f"); |
| 3955 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3956 | return 0; |
| 3957 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3958 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3959 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3960 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3961 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3962 | return 0; |
| 3963 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3964 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3965 | /* Create the "c" field that contains a list of converters. */ |
| 3966 | lua_pushstring(L, "c"); |
| 3967 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3968 | return 0; |
| 3969 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3970 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3971 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3972 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3973 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3974 | return 0; |
| 3975 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3976 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3977 | if (IS_HTX_STRM(s)) { |
| 3978 | /* HTX version */ |
| 3979 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3980 | struct htx_blk *blk; |
| 3981 | struct htx_sl *sl; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3982 | struct ist path; |
| 3983 | unsigned long long len = 0; |
| 3984 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3985 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3986 | blk = htx_get_first_blk(htx); |
Christopher Faulet | 43a686d | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 3987 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3988 | sl = htx_get_blk_ptr(htx, blk); |
| 3989 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3990 | /* Stores the request method. */ |
| 3991 | lua_pushstring(L, "method"); |
| 3992 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3993 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3994 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3995 | /* Stores the http version. */ |
| 3996 | lua_pushstring(L, "version"); |
| 3997 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3998 | lua_settable(L, -3); |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3999 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4000 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4001 | * the array on the top of the stack. |
| 4002 | */ |
| 4003 | lua_pushstring(L, "headers"); |
| 4004 | htxn.s = s; |
| 4005 | htxn.p = px; |
| 4006 | htxn.dir = SMP_OPT_DIR_REQ; |
| 4007 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 4008 | return 0; |
| 4009 | lua_settable(L, -3); |
| 4010 | |
| 4011 | path = http_get_path(htx_sl_req_uri(sl)); |
| 4012 | if (path.ptr) { |
| 4013 | char *p, *q, *end; |
| 4014 | |
| 4015 | p = path.ptr; |
| 4016 | end = path.ptr + path.len; |
| 4017 | q = p; |
| 4018 | while (q < end && *q != '?') |
| 4019 | q++; |
| 4020 | |
| 4021 | /* Stores the request path. */ |
| 4022 | lua_pushstring(L, "path"); |
| 4023 | lua_pushlstring(L, p, q - p); |
| 4024 | lua_settable(L, -3); |
| 4025 | |
| 4026 | /* Stores the query string. */ |
| 4027 | lua_pushstring(L, "qs"); |
| 4028 | if (*q == '?') |
| 4029 | q++; |
| 4030 | lua_pushlstring(L, q, end - q); |
| 4031 | lua_settable(L, -3); |
| 4032 | } |
| 4033 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4034 | 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] | 4035 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4036 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4037 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4038 | 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] | 4039 | break; |
| 4040 | if (type == HTX_BLK_DATA) |
| 4041 | len += htx_get_blksz(blk); |
| 4042 | } |
| 4043 | if (htx->extra != ULLONG_MAX) |
| 4044 | len += htx->extra; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4045 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4046 | /* Stores the request path. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4047 | lua_pushstring(L, "length"); |
| 4048 | lua_pushinteger(L, len); |
| 4049 | lua_settable(L, -3); |
| 4050 | } |
| 4051 | else { |
| 4052 | /* Legacy HTTP version */ |
| 4053 | struct http_txn *txn = s->txn; |
| 4054 | const char *path; |
| 4055 | const char *end; |
| 4056 | const char *p; |
| 4057 | |
| 4058 | /* Stores the request method. */ |
| 4059 | lua_pushstring(L, "method"); |
| 4060 | 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] | 4061 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4062 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4063 | /* Stores the http version. */ |
| 4064 | lua_pushstring(L, "version"); |
| 4065 | 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] | 4066 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4067 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4068 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4069 | * the array on the top of the stack. |
| 4070 | */ |
| 4071 | lua_pushstring(L, "headers"); |
| 4072 | htxn.s = s; |
| 4073 | htxn.p = px; |
| 4074 | htxn.dir = SMP_OPT_DIR_REQ; |
| 4075 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 4076 | return 0; |
| 4077 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4078 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4079 | /* Get path and qs */ |
| 4080 | path = http_txn_get_path(txn); |
| 4081 | if (path) { |
| 4082 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 4083 | p = path; |
| 4084 | while (p < end && *p != '?') |
| 4085 | p++; |
| 4086 | |
| 4087 | /* Stores the request path. */ |
| 4088 | lua_pushstring(L, "path"); |
| 4089 | lua_pushlstring(L, path, p - path); |
| 4090 | lua_settable(L, -3); |
| 4091 | |
| 4092 | /* Stores the query string. */ |
| 4093 | lua_pushstring(L, "qs"); |
| 4094 | if (*p == '?') |
| 4095 | p++; |
| 4096 | lua_pushlstring(L, p, end - p); |
| 4097 | lua_settable(L, -3); |
| 4098 | } |
| 4099 | |
| 4100 | /* Stores the request path. */ |
| 4101 | lua_pushstring(L, "length"); |
| 4102 | lua_pushinteger(L, txn->req.body_len); |
| 4103 | lua_settable(L, -3); |
| 4104 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4105 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4106 | /* Create an empty array of HTTP request headers. */ |
| 4107 | lua_pushstring(L, "response"); |
| 4108 | lua_newtable(L); |
| 4109 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4110 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4111 | /* Pop a class stream metatable and affect it to the table. */ |
| 4112 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4113 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4114 | |
| 4115 | return 1; |
| 4116 | } |
| 4117 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4118 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4119 | { |
| 4120 | struct hlua_appctx *appctx; |
| 4121 | struct stream *s; |
| 4122 | const char *name; |
| 4123 | size_t len; |
| 4124 | struct sample smp; |
| 4125 | |
| 4126 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4127 | |
| 4128 | /* It is useles to retrieve the stream, but this function |
| 4129 | * runs only in a stream context. |
| 4130 | */ |
| 4131 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4132 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4133 | s = appctx->htxn.s; |
| 4134 | |
| 4135 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | ca492f3 | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4136 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4137 | hlua_lua2smp(L, 3, &smp); |
| 4138 | |
| 4139 | /* Store the sample in a variable. */ |
| 4140 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4141 | vars_set_by_name(name, len, &smp); |
| 4142 | return 0; |
| 4143 | } |
| 4144 | |
| 4145 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4146 | { |
| 4147 | struct hlua_appctx *appctx; |
| 4148 | struct stream *s; |
| 4149 | const char *name; |
| 4150 | size_t len; |
| 4151 | struct sample smp; |
| 4152 | |
| 4153 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4154 | |
| 4155 | /* It is useles to retrieve the stream, but this function |
| 4156 | * runs only in a stream context. |
| 4157 | */ |
| 4158 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4159 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4160 | s = appctx->htxn.s; |
| 4161 | |
| 4162 | /* Unset the variable. */ |
| 4163 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4164 | vars_unset_by_name(name, len, &smp); |
| 4165 | return 0; |
| 4166 | } |
| 4167 | |
| 4168 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4169 | { |
| 4170 | struct hlua_appctx *appctx; |
| 4171 | struct stream *s; |
| 4172 | const char *name; |
| 4173 | size_t len; |
| 4174 | struct sample smp; |
| 4175 | |
| 4176 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4177 | |
| 4178 | /* It is useles to retrieve the stream, but this function |
| 4179 | * runs only in a stream context. |
| 4180 | */ |
| 4181 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4182 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4183 | s = appctx->htxn.s; |
| 4184 | |
| 4185 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4186 | if (!vars_get_by_name(name, len, &smp)) { |
| 4187 | lua_pushnil(L); |
| 4188 | return 1; |
| 4189 | } |
| 4190 | |
| 4191 | return hlua_smp2lua(L, &smp); |
| 4192 | } |
| 4193 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4194 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4195 | { |
| 4196 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4197 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4198 | struct hlua *hlua; |
| 4199 | |
| 4200 | /* 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] | 4201 | if (!s->hlua) |
| 4202 | return 0; |
| 4203 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4204 | |
| 4205 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4206 | |
| 4207 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4208 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4209 | |
| 4210 | /* Get and store new value. */ |
| 4211 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4212 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4213 | |
| 4214 | return 0; |
| 4215 | } |
| 4216 | |
| 4217 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4218 | { |
| 4219 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4220 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4221 | struct hlua *hlua; |
| 4222 | |
| 4223 | /* 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] | 4224 | if (!s->hlua) { |
| 4225 | lua_pushnil(L); |
| 4226 | return 1; |
| 4227 | } |
| 4228 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4229 | |
| 4230 | /* Push configuration index in the stack. */ |
| 4231 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4232 | |
| 4233 | return 1; |
| 4234 | } |
| 4235 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4236 | /* If expected data not yet available, it returns a yield. This function |
| 4237 | * consumes the data in the buffer. It returns a string containing the |
| 4238 | * data. This string can be empty. |
| 4239 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4240 | __LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4241 | { |
| 4242 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4243 | struct stream_interface *si = appctx->appctx->owner; |
| 4244 | struct channel *req = si_oc(si); |
| 4245 | struct htx *htx; |
| 4246 | struct htx_blk *blk; |
| 4247 | size_t count; |
| 4248 | int stop = 0; |
| 4249 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4250 | htx = htx_from_buf(&req->buf); |
| 4251 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4252 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4253 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4254 | while (count && !stop && blk) { |
| 4255 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4256 | uint32_t sz = htx_get_blksz(blk); |
| 4257 | struct ist v; |
| 4258 | uint32_t vlen; |
| 4259 | char *nl; |
| 4260 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4261 | if (type == HTX_BLK_EOM) { |
| 4262 | stop = 1; |
| 4263 | break; |
| 4264 | } |
| 4265 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4266 | vlen = sz; |
| 4267 | if (vlen > count) { |
| 4268 | if (type != HTX_BLK_DATA) |
| 4269 | break; |
| 4270 | vlen = count; |
| 4271 | } |
| 4272 | |
| 4273 | switch (type) { |
| 4274 | case HTX_BLK_UNUSED: |
| 4275 | break; |
| 4276 | |
| 4277 | case HTX_BLK_DATA: |
| 4278 | v = htx_get_blk_value(htx, blk); |
| 4279 | v.len = vlen; |
| 4280 | nl = istchr(v, '\n'); |
| 4281 | if (nl != NULL) { |
| 4282 | stop = 1; |
| 4283 | vlen = nl - v.ptr + 1; |
| 4284 | } |
| 4285 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4286 | break; |
| 4287 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4288 | case HTX_BLK_TLR: |
| 4289 | case HTX_BLK_EOM: |
| 4290 | stop = 1; |
| 4291 | break; |
| 4292 | |
| 4293 | default: |
| 4294 | break; |
| 4295 | } |
| 4296 | |
| 4297 | co_set_data(req, co_data(req) - vlen); |
| 4298 | count -= vlen; |
| 4299 | if (sz == vlen) |
| 4300 | blk = htx_remove_blk(htx, blk); |
| 4301 | else { |
| 4302 | htx_cut_data_blk(htx, blk, vlen); |
| 4303 | break; |
| 4304 | } |
| 4305 | } |
| 4306 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4307 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4308 | if (!stop) { |
| 4309 | si_cant_get(si); |
| 4310 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0)); |
| 4311 | } |
| 4312 | |
| 4313 | /* return the result. */ |
| 4314 | luaL_pushresult(&appctx->b); |
| 4315 | return 1; |
| 4316 | } |
| 4317 | |
| 4318 | |
| 4319 | /* If expected data not yet available, it returns a yield. This function |
| 4320 | * consumes the data in the buffer. It returns a string containing the |
| 4321 | * data. This string can be empty. |
| 4322 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4323 | __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] | 4324 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4325 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4326 | struct stream_interface *si = appctx->appctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4327 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4328 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4329 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4330 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4331 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4332 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4333 | /* Check for the end of the data. */ |
| 4334 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4335 | luaL_pushresult(&appctx->b); |
| 4336 | return 1; |
| 4337 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4338 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4339 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4340 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4341 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4342 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4343 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4344 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4345 | 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] | 4346 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4347 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4348 | /* End of data: commit the total strings and return. */ |
| 4349 | if (ret < 0) { |
| 4350 | luaL_pushresult(&appctx->b); |
| 4351 | return 1; |
| 4352 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4353 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4354 | /* Ensure that the block 2 length is usable. */ |
| 4355 | if (ret == 1) |
| 4356 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4357 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4358 | /* Copy the fisrt block caping to the length required. */ |
| 4359 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4360 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4361 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4362 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4363 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4364 | /* Copy the second block. */ |
| 4365 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4366 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4367 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4368 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4369 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4370 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4371 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4372 | luaL_pushresult(&appctx->b); |
| 4373 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4374 | } |
| 4375 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4376 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4377 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4378 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4379 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4380 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4381 | /* Initialise the string catenation. */ |
| 4382 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4383 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4384 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 4385 | return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0)); |
| 4386 | else |
| 4387 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4388 | } |
| 4389 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4390 | /* If expected data not yet available, it returns a yield. This function |
| 4391 | * consumes the data in the buffer. It returns a string containing the |
| 4392 | * data. This string can be empty. |
| 4393 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4394 | __LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4395 | { |
| 4396 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4397 | struct stream_interface *si = appctx->appctx->owner; |
| 4398 | struct channel *req = si_oc(si); |
| 4399 | struct htx *htx; |
| 4400 | struct htx_blk *blk; |
| 4401 | size_t count; |
| 4402 | int len; |
| 4403 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4404 | htx = htx_from_buf(&req->buf); |
| 4405 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4406 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4407 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4408 | while (count && len && blk) { |
| 4409 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4410 | uint32_t sz = htx_get_blksz(blk); |
| 4411 | struct ist v; |
| 4412 | uint32_t vlen; |
| 4413 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4414 | if (type == HTX_BLK_EOM) { |
| 4415 | len = 0; |
| 4416 | break; |
| 4417 | } |
| 4418 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4419 | vlen = sz; |
| 4420 | if (len > 0 && vlen > len) |
| 4421 | vlen = len; |
| 4422 | if (vlen > count) { |
| 4423 | if (type != HTX_BLK_DATA) |
| 4424 | break; |
| 4425 | vlen = count; |
| 4426 | } |
| 4427 | |
| 4428 | switch (type) { |
| 4429 | case HTX_BLK_UNUSED: |
| 4430 | break; |
| 4431 | |
| 4432 | case HTX_BLK_DATA: |
| 4433 | v = htx_get_blk_value(htx, blk); |
| 4434 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4435 | break; |
| 4436 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4437 | case HTX_BLK_TLR: |
| 4438 | case HTX_BLK_EOM: |
| 4439 | len = 0; |
| 4440 | break; |
| 4441 | |
| 4442 | default: |
| 4443 | break; |
| 4444 | } |
| 4445 | |
| 4446 | co_set_data(req, co_data(req) - vlen); |
| 4447 | count -= vlen; |
| 4448 | if (len > 0) |
| 4449 | len -= vlen; |
| 4450 | if (sz == vlen) |
| 4451 | blk = htx_remove_blk(htx, blk); |
| 4452 | else { |
| 4453 | htx_cut_data_blk(htx, blk, vlen); |
| 4454 | break; |
| 4455 | } |
| 4456 | } |
| 4457 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4458 | htx_to_buf(htx, &req->buf); |
| 4459 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4460 | /* If we are no other data available, yield waiting for new data. */ |
| 4461 | if (len) { |
| 4462 | if (len > 0) { |
| 4463 | lua_pushinteger(L, len); |
| 4464 | lua_replace(L, 2); |
| 4465 | } |
| 4466 | si_cant_get(si); |
| 4467 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0)); |
| 4468 | } |
| 4469 | |
| 4470 | /* return the result. */ |
| 4471 | luaL_pushresult(&appctx->b); |
| 4472 | return 1; |
| 4473 | } |
| 4474 | |
| 4475 | /* If expected data not yet available, it returns a yield. This function |
| 4476 | * consumes the data in the buffer. It returns a string containing the |
| 4477 | * data. This string can be empty. |
| 4478 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4479 | __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] | 4480 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4481 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4482 | struct stream_interface *si = appctx->appctx->owner; |
| 4483 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4484 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4485 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4486 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4487 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4488 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4489 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4490 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4491 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4492 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4493 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4494 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4495 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4496 | 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] | 4497 | } |
| 4498 | |
| 4499 | /* End of data: commit the total strings and return. */ |
| 4500 | if (ret < 0) { |
| 4501 | luaL_pushresult(&appctx->b); |
| 4502 | return 1; |
| 4503 | } |
| 4504 | |
| 4505 | /* Ensure that the block 2 length is usable. */ |
| 4506 | if (ret == 1) |
| 4507 | len2 = 0; |
| 4508 | |
| 4509 | /* Copy the fisrt block caping to the length required. */ |
| 4510 | if (len1 > len) |
| 4511 | len1 = len; |
| 4512 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4513 | len -= len1; |
| 4514 | |
| 4515 | /* Copy the second block. */ |
| 4516 | if (len2 > len) |
| 4517 | len2 = len; |
| 4518 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4519 | len -= len2; |
| 4520 | |
| 4521 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4522 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4523 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4524 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4525 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4526 | /* If we are no other data available, yield waiting for new data. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4527 | if (len > 0) { |
| 4528 | lua_pushinteger(L, len); |
| 4529 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4530 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4531 | 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] | 4532 | } |
| 4533 | |
| 4534 | /* return the result. */ |
| 4535 | luaL_pushresult(&appctx->b); |
| 4536 | return 1; |
| 4537 | } |
| 4538 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4539 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4540 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4541 | { |
| 4542 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4543 | int len = -1; |
| 4544 | |
| 4545 | /* Check arguments. */ |
| 4546 | if (lua_gettop(L) > 2) |
| 4547 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4548 | if (lua_gettop(L) >= 2) { |
| 4549 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4550 | lua_pop(L, 1); |
| 4551 | } |
| 4552 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4553 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4554 | /* HTX version */ |
| 4555 | lua_pushinteger(L, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4556 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4557 | /* Initialise the string catenation. */ |
| 4558 | luaL_buffinit(L, &appctx->b); |
| 4559 | |
| 4560 | return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0)); |
| 4561 | } |
| 4562 | else { |
| 4563 | /* Legacy HTTP version */ |
| 4564 | /* Check the required length */ |
| 4565 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4566 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4567 | lua_pushinteger(L, len); |
| 4568 | |
| 4569 | /* Initialise the string catenation. */ |
| 4570 | luaL_buffinit(L, &appctx->b); |
| 4571 | |
| 4572 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4573 | } |
| 4574 | } |
| 4575 | |
| 4576 | /* Append data in the output side of the buffer. This data is immediately |
| 4577 | * sent. The function returns the amount of data written. If the buffer |
| 4578 | * cannot contain the data, the function yields. The function returns -1 |
| 4579 | * if the channel is closed. |
| 4580 | */ |
| 4581 | __LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4582 | { |
| 4583 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4584 | struct stream_interface *si = appctx->appctx->owner; |
| 4585 | struct channel *res = si_ic(si); |
| 4586 | struct htx *htx = htx_from_buf(&res->buf); |
| 4587 | const char *data; |
| 4588 | size_t len; |
| 4589 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4590 | int max; |
| 4591 | |
Christopher Faulet | 4e96301 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4592 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4593 | if (!max) |
| 4594 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4595 | |
| 4596 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4597 | |
| 4598 | /* Get the max amount of data which can write as input in the channel. */ |
| 4599 | if (max > (len - l)) |
| 4600 | max = len - l; |
| 4601 | |
| 4602 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4603 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4604 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4605 | |
| 4606 | /* update counters. */ |
| 4607 | l += max; |
| 4608 | lua_pop(L, 1); |
| 4609 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4610 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4611 | /* If some data is not send, declares the situation to the |
| 4612 | * applet, and returns a yield. |
| 4613 | */ |
| 4614 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4615 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4616 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4617 | si_rx_room_blk(si); |
| 4618 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0)); |
| 4619 | } |
| 4620 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4621 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4622 | return 1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4623 | } |
| 4624 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4625 | /* Append data in the output side of the buffer. This data is immediately |
| 4626 | * sent. The function returns the amount of data written. If the buffer |
| 4627 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4628 | * if the channel is closed. |
| 4629 | */ |
| 4630 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4631 | { |
| 4632 | size_t len; |
| 4633 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4634 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4635 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4636 | struct stream_interface *si = appctx->appctx->owner; |
| 4637 | struct channel *chn = si_ic(si); |
| 4638 | int max; |
| 4639 | |
| 4640 | /* Get the max amount of data which can write as input in the channel. */ |
| 4641 | max = channel_recv_max(chn); |
| 4642 | if (max > (len - l)) |
| 4643 | max = len - l; |
| 4644 | |
| 4645 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4646 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4647 | |
| 4648 | /* update counters. */ |
| 4649 | l += max; |
| 4650 | lua_pop(L, 1); |
| 4651 | lua_pushinteger(L, l); |
| 4652 | |
| 4653 | /* If some data is not send, declares the situation to the |
| 4654 | * applet, and returns a yield. |
| 4655 | */ |
| 4656 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4657 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4658 | 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] | 4659 | } |
| 4660 | |
| 4661 | return 1; |
| 4662 | } |
| 4663 | |
| 4664 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4665 | * yield the LUA process, and resume it without checking the |
| 4666 | * input arguments. |
| 4667 | */ |
| 4668 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4669 | { |
| 4670 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4671 | |
| 4672 | /* We want to send some data. Headers must be sent. */ |
| 4673 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4674 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4675 | WILL_LJMP(lua_error(L)); |
| 4676 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4677 | |
| 4678 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4679 | /* HTX version */ |
| 4680 | /* This interger is used for followinf the amount of data sent. */ |
| 4681 | lua_pushinteger(L, 0); |
| 4682 | |
| 4683 | return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0)); |
| 4684 | } |
| 4685 | else { |
| 4686 | /* Legacy HTTP version */ |
| 4687 | size_t len; |
| 4688 | char hex[10]; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4689 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4690 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4691 | |
| 4692 | /* If transfer encoding chunked is selected, we surround the data |
| 4693 | * by chunk data. |
| 4694 | */ |
| 4695 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4696 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4697 | lua_pushfstring(L, "%s\r\n", hex); |
| 4698 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4699 | lua_pushstring(L, "\r\n"); |
| 4700 | lua_concat(L, 3); |
| 4701 | } |
| 4702 | |
| 4703 | /* This interger is used for followinf the amount of data sent. */ |
| 4704 | lua_pushinteger(L, 0); |
| 4705 | |
| 4706 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4707 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4708 | } |
| 4709 | |
| 4710 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4711 | { |
| 4712 | const char *name; |
| 4713 | int ret; |
| 4714 | |
| 4715 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4716 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4717 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4718 | |
| 4719 | /* Push in the stack the "response" entry. */ |
| 4720 | ret = lua_getfield(L, 1, "response"); |
| 4721 | if (ret != LUA_TTABLE) { |
| 4722 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4723 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4724 | WILL_LJMP(lua_error(L)); |
| 4725 | } |
| 4726 | |
| 4727 | /* check if the header is already registered if it is not |
| 4728 | * the case, register it. |
| 4729 | */ |
| 4730 | ret = lua_getfield(L, -1, name); |
| 4731 | if (ret == LUA_TNIL) { |
| 4732 | |
| 4733 | /* Entry not found. */ |
| 4734 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4735 | |
| 4736 | /* Insert the new header name in the array in the top of the stack. |
| 4737 | * It left the new array in the top of the stack. |
| 4738 | */ |
| 4739 | lua_newtable(L); |
| 4740 | lua_pushvalue(L, 2); |
| 4741 | lua_pushvalue(L, -2); |
| 4742 | lua_settable(L, -4); |
| 4743 | |
| 4744 | } else if (ret != LUA_TTABLE) { |
| 4745 | |
| 4746 | /* corruption error. */ |
| 4747 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4748 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4749 | WILL_LJMP(lua_error(L)); |
| 4750 | } |
| 4751 | |
| 4752 | /* Now the top od thestack is an array of values. We push |
| 4753 | * the header value as new entry. |
| 4754 | */ |
| 4755 | lua_pushvalue(L, 3); |
| 4756 | ret = lua_rawlen(L, -2); |
| 4757 | lua_rawseti(L, -2, ret + 1); |
| 4758 | lua_pushboolean(L, 1); |
| 4759 | return 1; |
| 4760 | } |
| 4761 | |
| 4762 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4763 | { |
| 4764 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4765 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4766 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4767 | |
| 4768 | if (status < 100 || status > 599) { |
| 4769 | lua_pushboolean(L, 0); |
| 4770 | return 1; |
| 4771 | } |
| 4772 | |
| 4773 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4774 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4775 | lua_pushboolean(L, 1); |
| 4776 | return 1; |
| 4777 | } |
| 4778 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4779 | |
| 4780 | __LJMP static int hlua_applet_htx_send_response(lua_State *L) |
| 4781 | { |
| 4782 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4783 | struct stream_interface *si = appctx->appctx->owner; |
| 4784 | struct channel *res = si_ic(si); |
| 4785 | struct htx *htx; |
| 4786 | struct htx_sl *sl; |
| 4787 | struct h1m h1m; |
| 4788 | const char *status, *reason; |
| 4789 | const char *name, *value; |
| 4790 | size_t nlen, vlen; |
| 4791 | unsigned int flags; |
| 4792 | |
| 4793 | /* Send the message at once. */ |
| 4794 | htx = htx_from_buf(&res->buf); |
| 4795 | h1m_init_res(&h1m); |
| 4796 | |
| 4797 | /* Use the same http version than the request. */ |
| 4798 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4799 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4800 | if (reason == NULL) |
| 4801 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4802 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4803 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4804 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4805 | } |
| 4806 | else { |
| 4807 | flags = HTX_SL_F_IS_RESP; |
| 4808 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4809 | } |
| 4810 | if (!sl) { |
| 4811 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4812 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4813 | WILL_LJMP(lua_error(L)); |
| 4814 | } |
| 4815 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4816 | |
| 4817 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4818 | lua_pushvalue(L, 0); |
| 4819 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4820 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4821 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4822 | WILL_LJMP(lua_error(L)); |
| 4823 | } |
| 4824 | |
| 4825 | /* Browse the list of headers. */ |
| 4826 | lua_pushnil(L); |
| 4827 | while(lua_next(L, -2) != 0) { |
| 4828 | /* We expect a string as -2. */ |
| 4829 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4830 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4831 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4832 | lua_typename(L, lua_type(L, -2))); |
| 4833 | WILL_LJMP(lua_error(L)); |
| 4834 | } |
| 4835 | name = lua_tolstring(L, -2, &nlen); |
| 4836 | |
| 4837 | /* We expect an array as -1. */ |
| 4838 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4839 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4840 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4841 | name, |
| 4842 | lua_typename(L, lua_type(L, -1))); |
| 4843 | WILL_LJMP(lua_error(L)); |
| 4844 | } |
| 4845 | |
| 4846 | /* Browse the table who is on the top of the stack. */ |
| 4847 | lua_pushnil(L); |
| 4848 | while(lua_next(L, -2) != 0) { |
| 4849 | int id; |
| 4850 | |
| 4851 | /* We expect a number as -2. */ |
| 4852 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4853 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4854 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4855 | name, |
| 4856 | lua_typename(L, lua_type(L, -2))); |
| 4857 | WILL_LJMP(lua_error(L)); |
| 4858 | } |
| 4859 | id = lua_tointeger(L, -2); |
| 4860 | |
| 4861 | /* We expect a string as -2. */ |
| 4862 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4863 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4864 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4865 | name, id, |
| 4866 | lua_typename(L, lua_type(L, -1))); |
| 4867 | WILL_LJMP(lua_error(L)); |
| 4868 | } |
| 4869 | value = lua_tolstring(L, -1, &vlen); |
| 4870 | |
| 4871 | /* Simple Protocol checks. */ |
| 4872 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
| 4873 | h1_parse_xfer_enc_header(&h1m, ist2(name, nlen)); |
| 4874 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4875 | struct ist v = ist2(value, vlen); |
| 4876 | int ret; |
| 4877 | |
| 4878 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4879 | if (ret < 0) { |
| 4880 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4881 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4882 | name); |
| 4883 | WILL_LJMP(lua_error(L)); |
| 4884 | } |
| 4885 | else if (ret == 0) |
| 4886 | goto next; /* Skip it */ |
| 4887 | } |
| 4888 | |
| 4889 | /* Add a new header */ |
| 4890 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4891 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4892 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4893 | name); |
| 4894 | WILL_LJMP(lua_error(L)); |
| 4895 | } |
| 4896 | next: |
| 4897 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4898 | lua_pop(L, 1); |
| 4899 | } |
| 4900 | |
| 4901 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4902 | lua_pop(L, 1); |
| 4903 | } |
| 4904 | |
| 4905 | if (h1m.flags & H1_MF_CHNK) |
| 4906 | h1m.flags &= ~H1_MF_CLEN; |
| 4907 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4908 | h1m.flags |= H1_MF_XFER_LEN; |
| 4909 | |
| 4910 | /* Uset HTX start-line flags */ |
| 4911 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4912 | flags |= HTX_SL_F_XFER_ENC; |
| 4913 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4914 | flags |= HTX_SL_F_XFER_LEN; |
| 4915 | if (h1m.flags & H1_MF_CHNK) |
| 4916 | flags |= HTX_SL_F_CHNK; |
| 4917 | else if (h1m.flags & H1_MF_CLEN) |
| 4918 | flags |= HTX_SL_F_CLEN; |
| 4919 | if (h1m.body_len == 0) |
| 4920 | flags |= HTX_SL_F_BODYLESS; |
| 4921 | } |
| 4922 | sl->flags |= flags; |
| 4923 | |
| 4924 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4925 | * and the status code implies the presence of a message body, we must |
| 4926 | * announce a transfer encoding chunked. This is required by haproxy |
| 4927 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4928 | * chunked itslef, don't do anything. |
| 4929 | */ |
| 4930 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4931 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4932 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4933 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4934 | /* Add a new header */ |
| 4935 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4936 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4937 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4938 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4939 | WILL_LJMP(lua_error(L)); |
| 4940 | } |
| 4941 | } |
| 4942 | |
| 4943 | /* Finalize headers. */ |
| 4944 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4945 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4946 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4947 | WILL_LJMP(lua_error(L)); |
| 4948 | } |
| 4949 | |
| 4950 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4951 | b_reset(&res->buf); |
| 4952 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4953 | WILL_LJMP(lua_error(L)); |
| 4954 | } |
| 4955 | |
| 4956 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4957 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4958 | |
| 4959 | /* Headers sent, set the flag. */ |
| 4960 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4961 | return 0; |
| 4962 | |
| 4963 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4964 | /* We will build the status line and the headers of the HTTP response. |
| 4965 | * We will try send at once if its not possible, we give back the hand |
| 4966 | * waiting for more room. |
| 4967 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4968 | __LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4969 | { |
| 4970 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4971 | struct stream_interface *si = appctx->appctx->owner; |
| 4972 | struct channel *res = si_ic(si); |
| 4973 | |
| 4974 | if (co_data(res)) { |
| 4975 | si_rx_room_blk(si); |
| 4976 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0)); |
| 4977 | } |
| 4978 | return MAY_LJMP(hlua_applet_htx_send_response(L)); |
| 4979 | } |
| 4980 | |
| 4981 | |
| 4982 | __LJMP static int hlua_applet_htx_start_response(lua_State *L) |
| 4983 | { |
| 4984 | return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0)); |
| 4985 | } |
| 4986 | |
| 4987 | /* We will build the status line and the headers of the HTTP response. |
| 4988 | * We will try send at once if its not possible, we give back the hand |
| 4989 | * waiting for more room. |
| 4990 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4991 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4992 | { |
| 4993 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4994 | struct stream_interface *si = appctx->appctx->owner; |
| 4995 | struct channel *chn = si_ic(si); |
| 4996 | int ret; |
| 4997 | size_t len; |
| 4998 | const char *msg; |
| 4999 | |
| 5000 | /* Get the message as the first argument on the stack. */ |
| 5001 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5002 | |
| 5003 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 5004 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5005 | |
| 5006 | /* if ret == -2 or -3 the channel closed or the message si too |
| 5007 | * big for the buffers. |
| 5008 | */ |
| 5009 | if (ret == -2 || ret == -3) { |
| 5010 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5011 | WILL_LJMP(lua_error(L)); |
| 5012 | } |
| 5013 | |
| 5014 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 5015 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 5016 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5017 | 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] | 5018 | } |
| 5019 | |
| 5020 | /* Headers sent, set the flag. */ |
| 5021 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 5022 | return 0; |
| 5023 | } |
| 5024 | |
| 5025 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 5026 | { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5027 | struct buffer *tmp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5028 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5029 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5030 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5031 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5032 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5033 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5034 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5035 | int hdr_chunked = 0; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5036 | const char *reason; |
| 5037 | |
| 5038 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 5039 | return MAY_LJMP(hlua_applet_htx_start_response(L)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5040 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5041 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5042 | if (reason == NULL) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 5043 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5044 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5045 | tmp = get_trash_chunk(); |
| 5046 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5047 | /* Use the same http version than the request. */ |
| 5048 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 5049 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5050 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5051 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5052 | |
| 5053 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5054 | lua_pushvalue(L, 0); |
| 5055 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5056 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 5057 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 5058 | WILL_LJMP(lua_error(L)); |
| 5059 | } |
| 5060 | |
| 5061 | /* Browse the list of headers. */ |
| 5062 | lua_pushnil(L); |
| 5063 | while(lua_next(L, -2) != 0) { |
| 5064 | |
| 5065 | /* We expect a string as -2. */ |
| 5066 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5067 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 5068 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5069 | lua_typename(L, lua_type(L, -2))); |
| 5070 | WILL_LJMP(lua_error(L)); |
| 5071 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5072 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5073 | |
| 5074 | /* We expect an array as -1. */ |
| 5075 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5076 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 5077 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5078 | name, |
| 5079 | lua_typename(L, lua_type(L, -1))); |
| 5080 | WILL_LJMP(lua_error(L)); |
| 5081 | } |
| 5082 | |
| 5083 | /* Browse the table who is on the top of the stack. */ |
| 5084 | lua_pushnil(L); |
| 5085 | while(lua_next(L, -2) != 0) { |
| 5086 | |
| 5087 | /* We expect a number as -2. */ |
| 5088 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5089 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 5090 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5091 | name, |
| 5092 | lua_typename(L, lua_type(L, -2))); |
| 5093 | WILL_LJMP(lua_error(L)); |
| 5094 | } |
| 5095 | id = lua_tointeger(L, -2); |
| 5096 | |
| 5097 | /* We expect a string as -2. */ |
| 5098 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5099 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 5100 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5101 | name, id, |
| 5102 | lua_typename(L, lua_type(L, -1))); |
| 5103 | WILL_LJMP(lua_error(L)); |
| 5104 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5105 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5106 | |
| 5107 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5108 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 5109 | memcpy(tmp->area + tmp->data, name, name_len); |
| 5110 | tmp->data += name_len; |
| 5111 | tmp->area[tmp->data++] = ':'; |
| 5112 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5113 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5114 | memcpy(tmp->area + tmp->data, value, |
| 5115 | value_len); |
| 5116 | tmp->data += value_len; |
| 5117 | tmp->area[tmp->data++] = '\r'; |
| 5118 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5119 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5120 | |
| 5121 | /* Protocol checks. */ |
| 5122 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5123 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5124 | * is done without control. If it contains a bad value, |
| 5125 | * the content-length remains negative so that we can |
| 5126 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5127 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5128 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5129 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5130 | |
| 5131 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5132 | if (name_len == 17 && value_len == 7 && |
| 5133 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5134 | strcasecmp("chunked", value) == 0) |
| 5135 | hdr_chunked = 1; |
| 5136 | |
| 5137 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5138 | lua_pop(L, 1); |
| 5139 | } |
| 5140 | |
| 5141 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5142 | lua_pop(L, 1); |
| 5143 | } |
| 5144 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5145 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 5146 | * and the status code implies the presence of a message body, we must |
| 5147 | * announce a transfer encoding chunked. This is required by haproxy |
| 5148 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 5149 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5150 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5151 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5152 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 5153 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5154 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5155 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5156 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 5157 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 5158 | } |
| 5159 | |
| 5160 | /* Finalize headers. */ |
| 5161 | chunk_appendf(tmp, "\r\n"); |
| 5162 | |
| 5163 | /* Remove the last entry and the array of headers */ |
| 5164 | lua_pop(L, 2); |
| 5165 | |
| 5166 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5167 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5168 | |
| 5169 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 5170 | } |
| 5171 | |
| 5172 | /* |
| 5173 | * |
| 5174 | * |
| 5175 | * Class HTTP |
| 5176 | * |
| 5177 | * |
| 5178 | */ |
| 5179 | |
| 5180 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5181 | * a class stream, otherwise it throws an error. |
| 5182 | */ |
| 5183 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 5184 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5185 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5186 | } |
| 5187 | |
| 5188 | /* This function creates and push in the stack a HTTP object |
| 5189 | * according with a current TXN. |
| 5190 | */ |
| 5191 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5192 | { |
| 5193 | struct hlua_txn *htxn; |
| 5194 | |
| 5195 | /* Check stack size. */ |
| 5196 | if (!lua_checkstack(L, 3)) |
| 5197 | return 0; |
| 5198 | |
| 5199 | /* Create the object: obj[0] = userdata. |
| 5200 | * Note that the base of the Converters object is the |
| 5201 | * same than the TXN object. |
| 5202 | */ |
| 5203 | lua_newtable(L); |
| 5204 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5205 | lua_rawseti(L, -2, 0); |
| 5206 | |
| 5207 | htxn->s = txn->s; |
| 5208 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5209 | htxn->dir = txn->dir; |
| 5210 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5211 | |
| 5212 | /* Pop a class stream metatable and affect it to the table. */ |
| 5213 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5214 | lua_setmetatable(L, -2); |
| 5215 | |
| 5216 | return 1; |
| 5217 | } |
| 5218 | |
| 5219 | /* This function creates ans returns an array of HTTP headers. |
| 5220 | * This function does not fails. It is used as wrapper with the |
| 5221 | * 2 following functions. |
| 5222 | */ |
| 5223 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5224 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5225 | /* Create the table. */ |
| 5226 | lua_newtable(L); |
| 5227 | |
| 5228 | if (!htxn->s->txn) |
| 5229 | return 1; |
| 5230 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5231 | if (IS_HTX_STRM(htxn->s)) { |
| 5232 | /* HTX version */ |
| 5233 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5234 | int32_t pos; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5235 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5236 | 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] | 5237 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5238 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5239 | struct ist n, v; |
| 5240 | int len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5241 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5242 | if (type == HTX_BLK_HDR) { |
| 5243 | n = htx_get_blk_name(htx,blk); |
| 5244 | v = htx_get_blk_value(htx, blk); |
| 5245 | } |
| 5246 | else if (type == HTX_BLK_EOH) |
| 5247 | break; |
| 5248 | else |
| 5249 | continue; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5250 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5251 | /* Check for existing entry: |
| 5252 | * assume that the table is on the top of the stack, and |
| 5253 | * push the key in the stack, the function lua_gettable() |
| 5254 | * perform the lookup. |
| 5255 | */ |
| 5256 | lua_pushlstring(L, n.ptr, n.len); |
| 5257 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5258 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5259 | switch (lua_type(L, -1)) { |
| 5260 | case LUA_TNIL: |
| 5261 | /* Table not found, create it. */ |
| 5262 | lua_pop(L, 1); /* remove the nil value. */ |
| 5263 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5264 | lua_newtable(L); /* create and push empty table. */ |
| 5265 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5266 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5267 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5268 | break; |
| 5269 | |
| 5270 | case LUA_TTABLE: |
| 5271 | /* Entry found: push the value in the table. */ |
| 5272 | len = lua_rawlen(L, -1); |
| 5273 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5274 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5275 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5276 | break; |
| 5277 | |
| 5278 | default: |
| 5279 | /* Other cases are errors. */ |
| 5280 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5281 | WILL_LJMP(lua_error(L)); |
| 5282 | } |
| 5283 | } |
| 5284 | } |
| 5285 | else { |
| 5286 | /* Legacy HTTP version */ |
| 5287 | const char *cur_ptr, *cur_next, *p; |
| 5288 | int old_idx, cur_idx; |
| 5289 | struct hdr_idx_elem *cur_hdr; |
| 5290 | const char *hn, *hv; |
| 5291 | int hnl, hvl; |
| 5292 | const char *in; |
| 5293 | char *out; |
| 5294 | int len; |
| 5295 | |
| 5296 | /* Build array of headers. */ |
| 5297 | old_idx = 0; |
| 5298 | cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 5299 | |
| 5300 | while (1) { |
| 5301 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 5302 | if (!cur_idx) |
| 5303 | break; |
| 5304 | old_idx = cur_idx; |
| 5305 | |
| 5306 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 5307 | cur_ptr = cur_next; |
| 5308 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 5309 | |
| 5310 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 5311 | * and the next header starts at cur_next. We'll check |
| 5312 | * this header in the list as well as against the default |
| 5313 | * rule. |
| 5314 | */ |
| 5315 | |
| 5316 | /* look for ': *'. */ |
| 5317 | hn = cur_ptr; |
| 5318 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 5319 | if (p >= cur_ptr+cur_hdr->len) |
| 5320 | continue; |
| 5321 | hnl = p - hn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5322 | p++; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5323 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 5324 | p++; |
| 5325 | if (p >= cur_ptr+cur_hdr->len) |
| 5326 | continue; |
| 5327 | hv = p; |
| 5328 | hvl = cur_ptr+cur_hdr->len-p; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5329 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5330 | /* Lowercase the key. Don't check the size of trash, it have |
| 5331 | * the size of one buffer and the input data contains in one |
| 5332 | * buffer. |
| 5333 | */ |
| 5334 | out = trash.area; |
| 5335 | for (in=hn; in<hn+hnl; in++, out++) |
| 5336 | *out = tolower(*in); |
| 5337 | *out = '\0'; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5338 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5339 | /* Check for existing entry: |
| 5340 | * assume that the table is on the top of the stack, and |
| 5341 | * push the key in the stack, the function lua_gettable() |
| 5342 | * perform the lookup. |
| 5343 | */ |
| 5344 | lua_pushlstring(L, trash.area, hnl); |
| 5345 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5346 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5347 | switch (lua_type(L, -1)) { |
| 5348 | case LUA_TNIL: |
| 5349 | /* Table not found, create it. */ |
| 5350 | lua_pop(L, 1); /* remove the nil value. */ |
| 5351 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
| 5352 | lua_newtable(L); /* create and push empty table. */ |
| 5353 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5354 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5355 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5356 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5357 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5358 | case LUA_TTABLE: |
| 5359 | /* Entry found: push the value in the table. */ |
| 5360 | len = lua_rawlen(L, -1); |
| 5361 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5362 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5363 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5364 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5365 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5366 | default: |
| 5367 | /* Other cases are errors. */ |
| 5368 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5369 | WILL_LJMP(lua_error(L)); |
| 5370 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5371 | } |
| 5372 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5373 | return 1; |
| 5374 | } |
| 5375 | |
| 5376 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5377 | { |
| 5378 | struct hlua_txn *htxn; |
| 5379 | |
| 5380 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5381 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5382 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5383 | 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] | 5384 | WILL_LJMP(lua_error(L)); |
| 5385 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5386 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 5387 | } |
| 5388 | |
| 5389 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5390 | { |
| 5391 | struct hlua_txn *htxn; |
| 5392 | |
| 5393 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5394 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5395 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5396 | 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] | 5397 | WILL_LJMP(lua_error(L)); |
| 5398 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5399 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 5400 | } |
| 5401 | |
| 5402 | /* This function replace full header, or just a value in |
| 5403 | * the request or in the response. It is a wrapper fir the |
| 5404 | * 4 following functions. |
| 5405 | */ |
| 5406 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 5407 | struct http_msg *msg, int action) |
| 5408 | { |
| 5409 | size_t name_len; |
| 5410 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5411 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5412 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5413 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5414 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5415 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5416 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5417 | |
Christopher Faulet | 5a8549e | 2019-06-17 13:36:06 +0200 | [diff] [blame] | 5418 | if (IS_HTX_STRM(htxn->s)) { |
| 5419 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5420 | |
| 5421 | htx_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action); |
| 5422 | } |
| 5423 | else |
| 5424 | 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] | 5425 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5426 | return 0; |
| 5427 | } |
| 5428 | |
| 5429 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5430 | { |
| 5431 | struct hlua_txn *htxn; |
| 5432 | |
| 5433 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5434 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5435 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5436 | 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] | 5437 | WILL_LJMP(lua_error(L)); |
| 5438 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5439 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 5440 | } |
| 5441 | |
| 5442 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5443 | { |
| 5444 | struct hlua_txn *htxn; |
| 5445 | |
| 5446 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5447 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5448 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5449 | 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] | 5450 | WILL_LJMP(lua_error(L)); |
| 5451 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5452 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 5453 | } |
| 5454 | |
| 5455 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5456 | { |
| 5457 | struct hlua_txn *htxn; |
| 5458 | |
| 5459 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5460 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5461 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5462 | 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] | 5463 | WILL_LJMP(lua_error(L)); |
| 5464 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5465 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 5466 | } |
| 5467 | |
| 5468 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5469 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5470 | struct hlua_txn *htxn; |
| 5471 | |
| 5472 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5473 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5474 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5475 | 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] | 5476 | WILL_LJMP(lua_error(L)); |
| 5477 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 5478 | 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] | 5479 | } |
| 5480 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5481 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5482 | * It is a wrapper for the 2 following functions. |
| 5483 | */ |
| 5484 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5485 | { |
| 5486 | size_t len; |
| 5487 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5488 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5489 | if (IS_HTX_STRM(htxn->s)) { |
| 5490 | /* HTX version */ |
| 5491 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5492 | struct http_hdr_ctx ctx; |
| 5493 | |
| 5494 | ctx.blk = NULL; |
| 5495 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5496 | http_remove_header(htx, &ctx); |
| 5497 | } |
| 5498 | else { |
| 5499 | /* Legacy HTTP version */ |
| 5500 | struct hdr_ctx ctx; |
| 5501 | struct http_txn *txn = htxn->s->txn; |
| 5502 | |
| 5503 | ctx.idx = 0; |
| 5504 | while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) |
| 5505 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5506 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5507 | return 0; |
| 5508 | } |
| 5509 | |
| 5510 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5511 | { |
| 5512 | struct hlua_txn *htxn; |
| 5513 | |
| 5514 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5515 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5516 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5517 | 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] | 5518 | WILL_LJMP(lua_error(L)); |
| 5519 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5520 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5521 | } |
| 5522 | |
| 5523 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5524 | { |
| 5525 | struct hlua_txn *htxn; |
| 5526 | |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5527 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5528 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5529 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5530 | 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] | 5531 | WILL_LJMP(lua_error(L)); |
| 5532 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5533 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5534 | } |
| 5535 | |
| 5536 | /* This function adds an header. It is a wrapper used by |
| 5537 | * the 2 following functions. |
| 5538 | */ |
| 5539 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5540 | { |
| 5541 | size_t name_len; |
| 5542 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5543 | size_t value_len; |
| 5544 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 5545 | char *p; |
| 5546 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5547 | if (IS_HTX_STRM(htxn->s)) { |
| 5548 | /* HTX version */ |
| 5549 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5550 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5551 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5552 | ist2(value, value_len))); |
| 5553 | } |
| 5554 | else { |
| 5555 | /* Legacy HTTP version */ |
| 5556 | /* Check length. */ |
| 5557 | trash.data = value_len + name_len + 2; |
| 5558 | if (trash.data > trash.size) |
| 5559 | return 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5560 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5561 | /* Creates the header string. */ |
| 5562 | p = trash.area; |
| 5563 | memcpy(p, name, name_len); |
| 5564 | p += name_len; |
| 5565 | *p = ':'; |
| 5566 | p++; |
| 5567 | *p = ' '; |
| 5568 | p++; |
| 5569 | memcpy(p, value, value_len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5570 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5571 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
| 5572 | trash.area, trash.data) != 0); |
| 5573 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5574 | return 0; |
| 5575 | } |
| 5576 | |
| 5577 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5578 | { |
| 5579 | struct hlua_txn *htxn; |
| 5580 | |
| 5581 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5582 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5583 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5584 | 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] | 5585 | WILL_LJMP(lua_error(L)); |
| 5586 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5587 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5588 | } |
| 5589 | |
| 5590 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5591 | { |
| 5592 | struct hlua_txn *htxn; |
| 5593 | |
| 5594 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5595 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5596 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5597 | 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] | 5598 | WILL_LJMP(lua_error(L)); |
| 5599 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5600 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5601 | } |
| 5602 | |
| 5603 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5604 | { |
| 5605 | struct hlua_txn *htxn; |
| 5606 | |
| 5607 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5608 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5609 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5610 | 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] | 5611 | WILL_LJMP(lua_error(L)); |
| 5612 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5613 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 5614 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5615 | } |
| 5616 | |
| 5617 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5618 | { |
| 5619 | struct hlua_txn *htxn; |
| 5620 | |
| 5621 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5622 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5623 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5624 | 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] | 5625 | WILL_LJMP(lua_error(L)); |
| 5626 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5627 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 5628 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5629 | } |
| 5630 | |
| 5631 | /* This function set the method. */ |
| 5632 | static int hlua_http_req_set_meth(lua_State *L) |
| 5633 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5634 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5635 | size_t name_len; |
| 5636 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5637 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5638 | 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] | 5639 | WILL_LJMP(lua_error(L)); |
| 5640 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5641 | 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] | 5642 | return 1; |
| 5643 | } |
| 5644 | |
| 5645 | /* This function set the method. */ |
| 5646 | static int hlua_http_req_set_path(lua_State *L) |
| 5647 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5648 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5649 | size_t name_len; |
| 5650 | 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] | 5651 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5652 | 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] | 5653 | WILL_LJMP(lua_error(L)); |
| 5654 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5655 | 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] | 5656 | return 1; |
| 5657 | } |
| 5658 | |
| 5659 | /* This function set the query-string. */ |
| 5660 | static int hlua_http_req_set_query(lua_State *L) |
| 5661 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5662 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5663 | size_t name_len; |
| 5664 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5665 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5666 | 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] | 5667 | WILL_LJMP(lua_error(L)); |
| 5668 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5669 | /* Check length. */ |
| 5670 | if (name_len > trash.size - 1) { |
| 5671 | lua_pushboolean(L, 0); |
| 5672 | return 1; |
| 5673 | } |
| 5674 | |
| 5675 | /* Add the mark question as prefix. */ |
| 5676 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5677 | trash.area[trash.data++] = '?'; |
| 5678 | memcpy(trash.area + trash.data, name, name_len); |
| 5679 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5680 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5681 | lua_pushboolean(L, |
| 5682 | 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] | 5683 | return 1; |
| 5684 | } |
| 5685 | |
| 5686 | /* This function set the uri. */ |
| 5687 | static int hlua_http_req_set_uri(lua_State *L) |
| 5688 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5689 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5690 | size_t name_len; |
| 5691 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5692 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5693 | 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] | 5694 | WILL_LJMP(lua_error(L)); |
| 5695 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5696 | 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] | 5697 | return 1; |
| 5698 | } |
| 5699 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5700 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5701 | static int hlua_http_res_set_status(lua_State *L) |
| 5702 | { |
| 5703 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5704 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5705 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5706 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5707 | 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] | 5708 | WILL_LJMP(lua_error(L)); |
| 5709 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5710 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5711 | return 0; |
| 5712 | } |
| 5713 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5714 | /* |
| 5715 | * |
| 5716 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5717 | * Class TXN |
| 5718 | * |
| 5719 | * |
| 5720 | */ |
| 5721 | |
| 5722 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5723 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5724 | */ |
| 5725 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5726 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5727 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5728 | } |
| 5729 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5730 | __LJMP static int hlua_set_var(lua_State *L) |
| 5731 | { |
| 5732 | struct hlua_txn *htxn; |
| 5733 | const char *name; |
| 5734 | size_t len; |
| 5735 | struct sample smp; |
| 5736 | |
| 5737 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5738 | |
| 5739 | /* It is useles to retrieve the stream, but this function |
| 5740 | * runs only in a stream context. |
| 5741 | */ |
| 5742 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5743 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5744 | |
| 5745 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | ca492f3 | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5746 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5747 | hlua_lua2smp(L, 3, &smp); |
| 5748 | |
| 5749 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5750 | 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] | 5751 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5752 | return 0; |
| 5753 | } |
| 5754 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5755 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5756 | { |
| 5757 | struct hlua_txn *htxn; |
| 5758 | const char *name; |
| 5759 | size_t len; |
| 5760 | struct sample smp; |
| 5761 | |
| 5762 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5763 | |
| 5764 | /* It is useles to retrieve the stream, but this function |
| 5765 | * runs only in a stream context. |
| 5766 | */ |
| 5767 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5768 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5769 | |
| 5770 | /* Unset the variable. */ |
| 5771 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5772 | vars_unset_by_name(name, len, &smp); |
| 5773 | return 0; |
| 5774 | } |
| 5775 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5776 | __LJMP static int hlua_get_var(lua_State *L) |
| 5777 | { |
| 5778 | struct hlua_txn *htxn; |
| 5779 | const char *name; |
| 5780 | size_t len; |
| 5781 | struct sample smp; |
| 5782 | |
| 5783 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5784 | |
| 5785 | /* It is useles to retrieve the stream, but this function |
| 5786 | * runs only in a stream context. |
| 5787 | */ |
| 5788 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5789 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5790 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5791 | 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] | 5792 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5793 | lua_pushnil(L); |
| 5794 | return 1; |
| 5795 | } |
| 5796 | |
| 5797 | return hlua_smp2lua(L, &smp); |
| 5798 | } |
| 5799 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5800 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5801 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5802 | struct hlua *hlua; |
| 5803 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5804 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5805 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5806 | /* It is useles to retrieve the stream, but this function |
| 5807 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5808 | */ |
| 5809 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5810 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5811 | |
| 5812 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5813 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5814 | |
| 5815 | /* Get and store new value. */ |
| 5816 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5817 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5818 | |
| 5819 | return 0; |
| 5820 | } |
| 5821 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5822 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5823 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5824 | struct hlua *hlua; |
| 5825 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5826 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5827 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5828 | /* It is useles to retrieve the stream, but this function |
| 5829 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5830 | */ |
| 5831 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5832 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5833 | |
| 5834 | /* Push configuration index in the stack. */ |
| 5835 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5836 | |
| 5837 | return 1; |
| 5838 | } |
| 5839 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5840 | /* Create stack entry containing a class TXN. This function |
| 5841 | * return 0 if the stack does not contains free slots, |
| 5842 | * otherwise it returns 1. |
| 5843 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5844 | 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] | 5845 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5846 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5847 | |
| 5848 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5849 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5850 | return 0; |
| 5851 | |
| 5852 | /* NOTE: The allocation never fails. The failure |
| 5853 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5854 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5855 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5856 | /* Create the object: obj[0] = userdata. */ |
| 5857 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5858 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5859 | lua_rawseti(L, -2, 0); |
| 5860 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5861 | htxn->s = s; |
| 5862 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5863 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5864 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5865 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5866 | /* Create the "f" field that contains a list of fetches. */ |
| 5867 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5868 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5869 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5870 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5871 | |
| 5872 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5873 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5874 | 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] | 5875 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5876 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5877 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5878 | /* Create the "c" field that contains a list of converters. */ |
| 5879 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5880 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5881 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5882 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5883 | |
| 5884 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5885 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5886 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5887 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5888 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5889 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5890 | /* Create the "req" field that contains the request channel object. */ |
| 5891 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5892 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5893 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5894 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5895 | |
| 5896 | /* Create the "res" field that contains the response channel object. */ |
| 5897 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5898 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5899 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5900 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5901 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5902 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5903 | lua_pushstring(L, "http"); |
| 5904 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5905 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5906 | return 0; |
| 5907 | } |
| 5908 | else |
| 5909 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5910 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5911 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5912 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5913 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5914 | lua_setmetatable(L, -2); |
| 5915 | |
| 5916 | return 1; |
| 5917 | } |
| 5918 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5919 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5920 | { |
| 5921 | const char *msg; |
| 5922 | struct hlua_txn *htxn; |
| 5923 | |
| 5924 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5925 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5926 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5927 | |
| 5928 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5929 | return 0; |
| 5930 | } |
| 5931 | |
| 5932 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5933 | { |
| 5934 | int level; |
| 5935 | const char *msg; |
| 5936 | struct hlua_txn *htxn; |
| 5937 | |
| 5938 | MAY_LJMP(check_args(L, 3, "log")); |
| 5939 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5940 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5941 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5942 | |
| 5943 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5944 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5945 | |
| 5946 | hlua_sendlog(htxn->s->be, level, msg); |
| 5947 | return 0; |
| 5948 | } |
| 5949 | |
| 5950 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5951 | { |
| 5952 | const char *msg; |
| 5953 | struct hlua_txn *htxn; |
| 5954 | |
| 5955 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5956 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5957 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5958 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5959 | return 0; |
| 5960 | } |
| 5961 | |
| 5962 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5963 | { |
| 5964 | const char *msg; |
| 5965 | struct hlua_txn *htxn; |
| 5966 | |
| 5967 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5968 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5969 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5970 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5971 | return 0; |
| 5972 | } |
| 5973 | |
| 5974 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5975 | { |
| 5976 | const char *msg; |
| 5977 | struct hlua_txn *htxn; |
| 5978 | |
| 5979 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5980 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5981 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5982 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5983 | return 0; |
| 5984 | } |
| 5985 | |
| 5986 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5987 | { |
| 5988 | const char *msg; |
| 5989 | struct hlua_txn *htxn; |
| 5990 | |
| 5991 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5992 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5993 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5994 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5995 | return 0; |
| 5996 | } |
| 5997 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5998 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5999 | { |
| 6000 | struct hlua_txn *htxn; |
| 6001 | int ll; |
| 6002 | |
| 6003 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 6004 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6005 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6006 | |
| 6007 | if (ll < 0 || ll > 7) |
| 6008 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 6009 | |
| 6010 | htxn->s->logs.level = ll; |
| 6011 | return 0; |
| 6012 | } |
| 6013 | |
| 6014 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 6015 | { |
| 6016 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6017 | int tos; |
| 6018 | |
| 6019 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 6020 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6021 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6022 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 6023 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6024 | return 0; |
| 6025 | } |
| 6026 | |
| 6027 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 6028 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6029 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6030 | int mark; |
| 6031 | |
| 6032 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 6033 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6034 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6035 | |
Lukas Tribus | b59291a | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 6036 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6037 | return 0; |
| 6038 | } |
| 6039 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 6040 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 6041 | { |
| 6042 | struct hlua_txn *htxn; |
| 6043 | |
| 6044 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 6045 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6046 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6047 | return 0; |
| 6048 | } |
| 6049 | |
| 6050 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 6051 | { |
| 6052 | struct hlua_txn *htxn; |
| 6053 | |
| 6054 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 6055 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6056 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6057 | return 0; |
| 6058 | } |
| 6059 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6060 | /* This function is an Lua binding that send pending data |
| 6061 | * to the client, and close the stream interface. |
| 6062 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6063 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6064 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6065 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6066 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6067 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6068 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6069 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6070 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6071 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6072 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6073 | /* If the flags NOTERM is set, we cannot terminate the http |
| 6074 | * session, so we just end the execution of the current |
| 6075 | * lua code. |
| 6076 | */ |
| 6077 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 6078 | WILL_LJMP(hlua_done(L)); |
| 6079 | return 0; |
| 6080 | } |
| 6081 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6082 | ic = &htxn->s->req; |
| 6083 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6084 | |
Christopher Faulet | 72c6927 | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6085 | if (IS_HTX_STRM(htxn->s)) { |
| 6086 | htxn->s->txn->status = 0; |
| 6087 | http_reply_and_close(htxn->s, 0, NULL); |
| 6088 | ic->analysers &= AN_REQ_FLT_END; |
| 6089 | oc->analysers &= AN_RES_FLT_END; |
| 6090 | } |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6091 | else { |
| 6092 | if (htxn->s->txn) { |
| 6093 | /* HTTP mode, let's stay in sync with the stream */ |
| 6094 | b_del(&ic->buf, htxn->s->txn->req.sov); |
| 6095 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 6096 | htxn->s->txn->req.sov = 0; |
| 6097 | |
| 6098 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 6099 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 6100 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 6101 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6102 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6103 | /* Note that if we want to support keep-alive, we need |
| 6104 | * to bypass the close/shutr_now calls below, but that |
| 6105 | * may only be done if the HTTP request was already |
| 6106 | * processed and the connection header is known (ie |
| 6107 | * not during TCP rules). |
| 6108 | */ |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6109 | } |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6110 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6111 | channel_auto_read(ic); |
| 6112 | channel_abort(ic); |
| 6113 | channel_auto_close(ic); |
| 6114 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6115 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6116 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 6117 | channel_auto_read(oc); |
| 6118 | channel_auto_close(oc); |
| 6119 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6120 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6121 | ic->analysers = 0; |
| 6122 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6123 | |
Christopher Faulet | 72c6927 | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6124 | if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 6125 | htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 6126 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6127 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6128 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6129 | return 0; |
| 6130 | } |
| 6131 | |
| 6132 | __LJMP static int hlua_log(lua_State *L) |
| 6133 | { |
| 6134 | int level; |
| 6135 | const char *msg; |
| 6136 | |
| 6137 | MAY_LJMP(check_args(L, 2, "log")); |
| 6138 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6139 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6140 | |
| 6141 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6142 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6143 | |
| 6144 | hlua_sendlog(NULL, level, msg); |
| 6145 | return 0; |
| 6146 | } |
| 6147 | |
| 6148 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6149 | { |
| 6150 | const char *msg; |
| 6151 | |
| 6152 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6153 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6154 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6155 | return 0; |
| 6156 | } |
| 6157 | |
| 6158 | __LJMP static int hlua_log_info(lua_State *L) |
| 6159 | { |
| 6160 | const char *msg; |
| 6161 | |
| 6162 | MAY_LJMP(check_args(L, 1, "info")); |
| 6163 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6164 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6165 | return 0; |
| 6166 | } |
| 6167 | |
| 6168 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6169 | { |
| 6170 | const char *msg; |
| 6171 | |
| 6172 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6173 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6174 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6175 | return 0; |
| 6176 | } |
| 6177 | |
| 6178 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6179 | { |
| 6180 | const char *msg; |
| 6181 | |
| 6182 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6183 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6184 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6185 | return 0; |
| 6186 | } |
| 6187 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6188 | __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] | 6189 | { |
| 6190 | int wakeup_ms = lua_tointeger(L, -1); |
| 6191 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6192 | 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] | 6193 | return 0; |
| 6194 | } |
| 6195 | |
| 6196 | __LJMP static int hlua_sleep(lua_State *L) |
| 6197 | { |
| 6198 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6199 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6200 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6201 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6202 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6203 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6204 | wakeup_ms = tick_add(now_ms, delay); |
| 6205 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6206 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6207 | 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] | 6208 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6209 | } |
| 6210 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6211 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6212 | { |
| 6213 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6214 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6215 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6216 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6217 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6218 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6219 | wakeup_ms = tick_add(now_ms, delay); |
| 6220 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6221 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6222 | 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] | 6223 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6224 | } |
| 6225 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6226 | /* This functionis an LUA binding. it permits to give back |
| 6227 | * the hand at the HAProxy scheduler. It is used when the |
| 6228 | * LUA processing consumes a lot of time. |
| 6229 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6230 | __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] | 6231 | { |
| 6232 | return 0; |
| 6233 | } |
| 6234 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6235 | __LJMP static int hlua_yield(lua_State *L) |
| 6236 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6237 | 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] | 6238 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6239 | } |
| 6240 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6241 | /* This function change the nice of the currently executed |
| 6242 | * task. It is used set low or high priority at the current |
| 6243 | * task. |
| 6244 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6245 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6246 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6247 | struct hlua *hlua; |
| 6248 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6249 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6250 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6251 | hlua = hlua_gethlua(L); |
| 6252 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6253 | |
| 6254 | /* If he task is not set, I'm in a start mode. */ |
| 6255 | if (!hlua || !hlua->task) |
| 6256 | return 0; |
| 6257 | |
| 6258 | if (nice < -1024) |
| 6259 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6260 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6261 | nice = 1024; |
| 6262 | |
| 6263 | hlua->task->nice = nice; |
| 6264 | return 0; |
| 6265 | } |
| 6266 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6267 | /* 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] | 6268 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6269 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6270 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6271 | * |
| 6272 | * Task wrapper are longjmp safe because the only one Lua code |
| 6273 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6274 | */ |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6275 | 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] | 6276 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6277 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6278 | enum hlua_exec status; |
| 6279 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6280 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6281 | task_set_affinity(task, tid_bit); |
| 6282 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6283 | /* If it is the first call to the task, we must initialize the |
| 6284 | * execution timeouts. |
| 6285 | */ |
| 6286 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6287 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6288 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6289 | /* Execute the Lua code. */ |
| 6290 | status = hlua_ctx_resume(hlua, 1); |
| 6291 | |
| 6292 | switch (status) { |
| 6293 | /* finished or yield */ |
| 6294 | case HLUA_E_OK: |
| 6295 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6296 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6297 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6298 | break; |
| 6299 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6300 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6301 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6302 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6303 | break; |
| 6304 | |
| 6305 | /* finished with error. */ |
| 6306 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6307 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6308 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6309 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6310 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6311 | break; |
| 6312 | |
| 6313 | case HLUA_E_ERR: |
| 6314 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6315 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6316 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6317 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6318 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6319 | break; |
| 6320 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6321 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6322 | } |
| 6323 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6324 | /* This function is an LUA binding that register LUA function to be |
| 6325 | * executed after the HAProxy configuration parsing and before the |
| 6326 | * HAProxy scheduler starts. This function expect only one LUA |
| 6327 | * argument that is a function. This function returns nothing, but |
| 6328 | * throws if an error is encountered. |
| 6329 | */ |
| 6330 | __LJMP static int hlua_register_init(lua_State *L) |
| 6331 | { |
| 6332 | struct hlua_init_function *init; |
| 6333 | int ref; |
| 6334 | |
| 6335 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6336 | |
| 6337 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6338 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6339 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6340 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6341 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6342 | |
| 6343 | init->function_ref = ref; |
| 6344 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6345 | return 0; |
| 6346 | } |
| 6347 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6348 | /* This functio is an LUA binding. It permits to register a task |
| 6349 | * executed in parallel of the main HAroxy activity. The task is |
| 6350 | * created and it is set in the HAProxy scheduler. It can be called |
| 6351 | * from the "init" section, "post init" or during the runtime. |
| 6352 | * |
| 6353 | * Lua prototype: |
| 6354 | * |
| 6355 | * <none> core.register_task(<function>) |
| 6356 | */ |
| 6357 | static int hlua_register_task(lua_State *L) |
| 6358 | { |
| 6359 | struct hlua *hlua; |
| 6360 | struct task *task; |
| 6361 | int ref; |
| 6362 | |
| 6363 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6364 | |
| 6365 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6366 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6367 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6368 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6369 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6370 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6371 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6372 | if (!task) |
| 6373 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6374 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6375 | task->context = hlua; |
| 6376 | task->process = hlua_process_task; |
| 6377 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6378 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6379 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6380 | |
| 6381 | /* Restore the function in the stack. */ |
| 6382 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6383 | hlua->nargs = 0; |
| 6384 | |
| 6385 | /* Schedule task. */ |
| 6386 | task_schedule(task, now_ms); |
| 6387 | |
| 6388 | return 0; |
| 6389 | } |
| 6390 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6391 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6392 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6393 | * resume converters. |
| 6394 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6395 | 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] | 6396 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6397 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6398 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6399 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6400 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6401 | if (!stream) |
| 6402 | return 0; |
| 6403 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6404 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6405 | * Lua context can be not initialized. This behavior |
| 6406 | * permits to save performances because a systematic |
| 6407 | * Lua initialization cause 5% performances loss. |
| 6408 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6409 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6410 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6411 | if (!stream->hlua) { |
| 6412 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6413 | return 0; |
| 6414 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6415 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6416 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6417 | return 0; |
| 6418 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6419 | } |
| 6420 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6421 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6422 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6423 | |
| 6424 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6425 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6426 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6427 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6428 | else |
| 6429 | error = "critical error"; |
| 6430 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6431 | return 0; |
| 6432 | } |
| 6433 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6434 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6435 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6436 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6437 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6438 | return 0; |
| 6439 | } |
| 6440 | |
| 6441 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6442 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6443 | |
| 6444 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6445 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6446 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6447 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6448 | return 0; |
| 6449 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6450 | hlua_smp2lua(stream->hlua->T, smp); |
| 6451 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6452 | |
| 6453 | /* push keywords in the stack. */ |
| 6454 | if (arg_p) { |
| 6455 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6456 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6457 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6458 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6459 | return 0; |
| 6460 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6461 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6462 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6463 | } |
| 6464 | } |
| 6465 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6466 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6467 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6468 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6469 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6470 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6471 | } |
| 6472 | |
| 6473 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6474 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6475 | /* finished. */ |
| 6476 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6477 | /* If the stack is empty, the function fails. */ |
| 6478 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6479 | return 0; |
| 6480 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6481 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6482 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6483 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6484 | return 1; |
| 6485 | |
| 6486 | /* yield. */ |
| 6487 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6488 | 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] | 6489 | return 0; |
| 6490 | |
| 6491 | /* finished with error. */ |
| 6492 | case HLUA_E_ERRMSG: |
| 6493 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6494 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6495 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6496 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6497 | return 0; |
| 6498 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6499 | case HLUA_E_ETMOUT: |
| 6500 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6501 | return 0; |
| 6502 | |
| 6503 | case HLUA_E_NOMEM: |
| 6504 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6505 | return 0; |
| 6506 | |
| 6507 | case HLUA_E_YIELD: |
| 6508 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6509 | return 0; |
| 6510 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6511 | case HLUA_E_ERR: |
| 6512 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6513 | 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] | 6514 | |
| 6515 | default: |
| 6516 | return 0; |
| 6517 | } |
| 6518 | } |
| 6519 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6520 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6521 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6522 | * resume sample-fetches. This function will be called by the sample |
| 6523 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6524 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6525 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6526 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6527 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6528 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6529 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6530 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6531 | const struct buffer msg = { }; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6532 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6533 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6534 | if (!stream) |
| 6535 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6536 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6537 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6538 | * Lua context can be not initialized. This behavior |
| 6539 | * permits to save performances because a systematic |
| 6540 | * Lua initialization cause 5% performances loss. |
| 6541 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6542 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6543 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6544 | if (!stream->hlua) { |
| 6545 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6546 | return 0; |
| 6547 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6548 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6549 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6550 | return 0; |
| 6551 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6552 | } |
| 6553 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6554 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6555 | |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6556 | if (stream->be->mode == PR_MODE_HTTP) { |
| 6557 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
| 6558 | hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 6559 | else |
| 6560 | hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 6561 | } |
| 6562 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6563 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6564 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6565 | |
| 6566 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6567 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6568 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6569 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6570 | else |
| 6571 | error = "critical error"; |
| 6572 | 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] | 6573 | return 0; |
| 6574 | } |
| 6575 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6576 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6577 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6578 | 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] | 6579 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6580 | return 0; |
| 6581 | } |
| 6582 | |
| 6583 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6584 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6585 | |
| 6586 | /* push arguments in the stack. */ |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6587 | 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] | 6588 | 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] | 6589 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6590 | return 0; |
| 6591 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6592 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6593 | |
| 6594 | /* push keywords in the stack. */ |
| 6595 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6596 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6597 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6598 | 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] | 6599 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6600 | return 0; |
| 6601 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6602 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6603 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6604 | } |
| 6605 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6606 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6607 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6608 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6609 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6610 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6611 | } |
| 6612 | |
| 6613 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6614 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6615 | /* finished. */ |
| 6616 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6617 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6618 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6619 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6620 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6621 | /* If the stack is empty, the function fails. */ |
| 6622 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6623 | return 0; |
| 6624 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6625 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6626 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6627 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6628 | |
| 6629 | /* Set the end of execution flag. */ |
| 6630 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6631 | return 1; |
| 6632 | |
| 6633 | /* yield. */ |
| 6634 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6635 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6636 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6637 | 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] | 6638 | return 0; |
| 6639 | |
| 6640 | /* finished with error. */ |
| 6641 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6642 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6643 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6644 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6645 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6646 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6647 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6648 | return 0; |
| 6649 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6650 | case HLUA_E_ETMOUT: |
| 6651 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6652 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6653 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6654 | return 0; |
| 6655 | |
| 6656 | case HLUA_E_NOMEM: |
| 6657 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6658 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6659 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6660 | return 0; |
| 6661 | |
| 6662 | case HLUA_E_YIELD: |
| 6663 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6664 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6665 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6666 | return 0; |
| 6667 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6668 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6669 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6670 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6671 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6672 | 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] | 6673 | |
| 6674 | default: |
| 6675 | return 0; |
| 6676 | } |
| 6677 | } |
| 6678 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6679 | /* This function is an LUA binding used for registering |
| 6680 | * "sample-conv" functions. It expects a converter name used |
| 6681 | * in the haproxy configuration file, and an LUA function. |
| 6682 | */ |
| 6683 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6684 | { |
| 6685 | struct sample_conv_kw_list *sck; |
| 6686 | const char *name; |
| 6687 | int ref; |
| 6688 | int len; |
| 6689 | struct hlua_function *fcn; |
| 6690 | |
| 6691 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6692 | |
| 6693 | /* First argument : converter name. */ |
| 6694 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6695 | |
| 6696 | /* Second argument : lua function. */ |
| 6697 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6698 | |
| 6699 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6700 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6701 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6702 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6703 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6704 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6705 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6706 | |
| 6707 | /* Fill fcn. */ |
| 6708 | fcn->name = strdup(name); |
| 6709 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6710 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6711 | fcn->function_ref = ref; |
| 6712 | |
| 6713 | /* List head */ |
| 6714 | sck->list.n = sck->list.p = NULL; |
| 6715 | |
| 6716 | /* converter keyword. */ |
| 6717 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6718 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6719 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6720 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6721 | |
| 6722 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6723 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6724 | 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] | 6725 | sck->kw[0].val_args = NULL; |
| 6726 | sck->kw[0].in_type = SMP_T_STR; |
| 6727 | sck->kw[0].out_type = SMP_T_STR; |
| 6728 | sck->kw[0].private = fcn; |
| 6729 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6730 | /* Register this new converter */ |
| 6731 | sample_register_convs(sck); |
| 6732 | |
| 6733 | return 0; |
| 6734 | } |
| 6735 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6736 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6737 | * "sample-fetch" functions. It expects a converter name used |
| 6738 | * in the haproxy configuration file, and an LUA function. |
| 6739 | */ |
| 6740 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6741 | { |
| 6742 | const char *name; |
| 6743 | int ref; |
| 6744 | int len; |
| 6745 | struct sample_fetch_kw_list *sfk; |
| 6746 | struct hlua_function *fcn; |
| 6747 | |
| 6748 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6749 | |
| 6750 | /* First argument : sample-fetch name. */ |
| 6751 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6752 | |
| 6753 | /* Second argument : lua function. */ |
| 6754 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6755 | |
| 6756 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6757 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6758 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6759 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6760 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6761 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6762 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6763 | |
| 6764 | /* Fill fcn. */ |
| 6765 | fcn->name = strdup(name); |
| 6766 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6767 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6768 | fcn->function_ref = ref; |
| 6769 | |
| 6770 | /* List head */ |
| 6771 | sfk->list.n = sfk->list.p = NULL; |
| 6772 | |
| 6773 | /* sample-fetch keyword. */ |
| 6774 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6775 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6776 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6777 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6778 | |
| 6779 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6780 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6781 | 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] | 6782 | sfk->kw[0].val_args = NULL; |
| 6783 | sfk->kw[0].out_type = SMP_T_STR; |
| 6784 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6785 | sfk->kw[0].val = 0; |
| 6786 | sfk->kw[0].private = fcn; |
| 6787 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6788 | /* Register this new fetch. */ |
| 6789 | sample_register_fetches(sfk); |
| 6790 | |
| 6791 | return 0; |
| 6792 | } |
| 6793 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6794 | /* This function is a wrapper to execute each LUA function declared |
| 6795 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6796 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6797 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6798 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6799 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6800 | 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] | 6801 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6802 | { |
| 6803 | char **arg; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6804 | unsigned int hflags = 0; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6805 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6806 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6807 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6808 | |
| 6809 | switch (rule->from) { |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6810 | case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break; |
| 6811 | case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break; |
| 6812 | case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break; |
| 6813 | 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] | 6814 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6815 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6816 | return ACT_RET_CONT; |
| 6817 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6818 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6819 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6820 | * Lua context can be not initialized. This behavior |
| 6821 | * permits to save performances because a systematic |
| 6822 | * Lua initialization cause 5% performances loss. |
| 6823 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6824 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6825 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6826 | if (!s->hlua) { |
| 6827 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6828 | rule->arg.hlua_rule->fcn.name); |
| 6829 | return ACT_RET_CONT; |
| 6830 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6831 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6832 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6833 | rule->arg.hlua_rule->fcn.name); |
| 6834 | return ACT_RET_CONT; |
| 6835 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6836 | } |
| 6837 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6838 | consistency_set(s, dir, &s->hlua->cons); |
| 6839 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6840 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6841 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6842 | |
| 6843 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6844 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6845 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6846 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6847 | else |
| 6848 | error = "critical error"; |
| 6849 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6850 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6851 | return ACT_RET_CONT; |
| 6852 | } |
| 6853 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6854 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6855 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6856 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6857 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6858 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6859 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6860 | } |
| 6861 | |
| 6862 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6863 | 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] | 6864 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6865 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6866 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6867 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6868 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6869 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6870 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6871 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6872 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6873 | |
| 6874 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6875 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6876 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6877 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6878 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6879 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6880 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6881 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6882 | lua_pushstring(s->hlua->T, *arg); |
| 6883 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6884 | } |
| 6885 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6886 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6887 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6888 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6889 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6890 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6891 | } |
| 6892 | |
Christopher Faulet | 719ddc8 | 2020-06-02 18:46:07 +0200 | [diff] [blame] | 6893 | /* Always reset the analyse expiration timeout for the corresponding |
| 6894 | * channel in case the lua script yield, to be sure to not keep an |
| 6895 | * expired timeout. |
| 6896 | */ |
| 6897 | if (dir == SMP_OPT_DIR_REQ) |
| 6898 | s->req.analyse_exp = TICK_ETERNITY; |
| 6899 | else |
| 6900 | s->res.analyse_exp = TICK_ETERNITY; |
| 6901 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6902 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6903 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6904 | /* finished. */ |
| 6905 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6906 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6907 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6908 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6909 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6910 | if (s->hlua->flags & HLUA_STOP) |
Christopher Faulet | 4629d08 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 6911 | return ACT_RET_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6912 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6913 | |
| 6914 | /* yield. */ |
| 6915 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6916 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6917 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Christopher Faulet | ea0b0e2 | 2019-08-14 23:19:45 +0200 | [diff] [blame] | 6918 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6919 | s->req.analyse_exp = s->hlua->wake_time; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6920 | else |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6921 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6922 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6923 | /* Some actions can be wake up when a "write" event |
| 6924 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6925 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6926 | */ |
Christopher Faulet | b22f650 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6927 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6928 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6929 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6930 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6931 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6932 | * because HAProxy is not able to manipulate data, it |
| 6933 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6934 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6935 | |
| 6936 | /* finished with error. */ |
| 6937 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6938 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6939 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6940 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6941 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6942 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6943 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6944 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6945 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6946 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6947 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6948 | case HLUA_E_ETMOUT: |
| 6949 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6950 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6951 | return ACT_RET_ERR; |
| 6952 | } |
| 6953 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6954 | return 0; |
| 6955 | |
| 6956 | case HLUA_E_NOMEM: |
| 6957 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6958 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6959 | return ACT_RET_ERR; |
| 6960 | } |
| 6961 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6962 | return 0; |
| 6963 | |
| 6964 | case HLUA_E_YIELD: |
| 6965 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6966 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6967 | return ACT_RET_ERR; |
| 6968 | } |
| 6969 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6970 | rule->arg.hlua_rule->fcn.name); |
| 6971 | return 0; |
| 6972 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6973 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6974 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6975 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6976 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6977 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6978 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6979 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6980 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6981 | |
| 6982 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6983 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6984 | } |
| 6985 | } |
| 6986 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6987 | 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] | 6988 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6989 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6990 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6991 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6992 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6993 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6994 | } |
| 6995 | |
| 6996 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6997 | { |
| 6998 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6999 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7000 | struct task *task; |
| 7001 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7002 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7003 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7004 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7005 | if (!hlua) { |
| 7006 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 7007 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7008 | return 0; |
| 7009 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7010 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7011 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7012 | ctx->ctx.hlua_apptcp.flags = 0; |
| 7013 | |
| 7014 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7015 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7016 | if (!task) { |
| 7017 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 7018 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7019 | return 0; |
| 7020 | } |
| 7021 | task->nice = 0; |
| 7022 | task->context = ctx; |
| 7023 | task->process = hlua_applet_wakeup; |
| 7024 | ctx->ctx.hlua_apptcp.task = task; |
| 7025 | |
| 7026 | /* In the execution wrappers linked with a stream, the |
| 7027 | * Lua context can be not initialized. This behavior |
| 7028 | * permits to save performances because a systematic |
| 7029 | * Lua initialization cause 5% performances loss. |
| 7030 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7031 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7032 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 7033 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7034 | return 0; |
| 7035 | } |
| 7036 | |
| 7037 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7038 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7039 | |
| 7040 | /* The following Lua calls can fail. */ |
| 7041 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7042 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7043 | error = lua_tostring(hlua->T, -1); |
| 7044 | else |
| 7045 | error = "critical error"; |
| 7046 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7047 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7048 | return 0; |
| 7049 | } |
| 7050 | |
| 7051 | /* Check stack available size. */ |
| 7052 | if (!lua_checkstack(hlua->T, 1)) { |
| 7053 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7054 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7055 | RESET_SAFE_LJMP(hlua->T); |
| 7056 | return 0; |
| 7057 | } |
| 7058 | |
| 7059 | /* Restore the function in the stack. */ |
| 7060 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7061 | |
| 7062 | /* Create and and push object stream in the stack. */ |
| 7063 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7064 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7065 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7066 | RESET_SAFE_LJMP(hlua->T); |
| 7067 | return 0; |
| 7068 | } |
| 7069 | hlua->nargs = 1; |
| 7070 | |
| 7071 | /* push keywords in the stack. */ |
| 7072 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7073 | if (!lua_checkstack(hlua->T, 1)) { |
| 7074 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7075 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7076 | RESET_SAFE_LJMP(hlua->T); |
| 7077 | return 0; |
| 7078 | } |
| 7079 | lua_pushstring(hlua->T, *arg); |
| 7080 | hlua->nargs++; |
| 7081 | } |
| 7082 | |
| 7083 | RESET_SAFE_LJMP(hlua->T); |
| 7084 | |
| 7085 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7086 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7087 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7088 | |
| 7089 | return 1; |
| 7090 | } |
| 7091 | |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7092 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7093 | { |
| 7094 | struct stream_interface *si = ctx->owner; |
| 7095 | struct stream *strm = si_strm(si); |
| 7096 | struct channel *res = si_ic(si); |
| 7097 | struct act_rule *rule = ctx->rule; |
| 7098 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7099 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7100 | |
| 7101 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7102 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7103 | /* eat the whole request */ |
| 7104 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7105 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7106 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7107 | |
| 7108 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7109 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7110 | return; |
| 7111 | |
| 7112 | /* Execute the function. */ |
| 7113 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7114 | /* finished. */ |
| 7115 | case HLUA_E_OK: |
| 7116 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7117 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7118 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7119 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7120 | res->flags |= CF_READ_NULL; |
| 7121 | si_shutr(si); |
| 7122 | return; |
| 7123 | |
| 7124 | /* yield. */ |
| 7125 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7126 | if (hlua->wake_time != TICK_ETERNITY) |
| 7127 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7128 | return; |
| 7129 | |
| 7130 | /* finished with error. */ |
| 7131 | case HLUA_E_ERRMSG: |
| 7132 | /* Display log. */ |
| 7133 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7134 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7135 | lua_pop(hlua->T, 1); |
| 7136 | goto error; |
| 7137 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7138 | case HLUA_E_ETMOUT: |
| 7139 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 7140 | rule->arg.hlua_rule->fcn.name); |
| 7141 | goto error; |
| 7142 | |
| 7143 | case HLUA_E_NOMEM: |
| 7144 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 7145 | rule->arg.hlua_rule->fcn.name); |
| 7146 | goto error; |
| 7147 | |
| 7148 | case HLUA_E_YIELD: /* unexpected */ |
| 7149 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 7150 | rule->arg.hlua_rule->fcn.name); |
| 7151 | goto error; |
| 7152 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7153 | case HLUA_E_ERR: |
| 7154 | /* Display log. */ |
| 7155 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 7156 | rule->arg.hlua_rule->fcn.name); |
| 7157 | goto error; |
| 7158 | |
| 7159 | default: |
| 7160 | goto error; |
| 7161 | } |
| 7162 | |
| 7163 | error: |
| 7164 | |
| 7165 | /* For all other cases, just close the stream. */ |
| 7166 | si_shutw(si); |
| 7167 | si_shutr(si); |
| 7168 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7169 | } |
| 7170 | |
| 7171 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7172 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7173 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7174 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7175 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7176 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7177 | } |
| 7178 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7179 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7180 | * an errors occurs and -1 if more data are required for initializing |
| 7181 | * the applet. |
| 7182 | */ |
| 7183 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7184 | { |
| 7185 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7186 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7187 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7188 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7189 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7190 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7191 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7192 | txn = strm->txn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7193 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7194 | /* We want two things in HTTP mode : |
| 7195 | * - enforce server-close mode if we were in keep-alive, so that the |
| 7196 | * applet is released after each response ; |
| 7197 | * - enable request body transfer to the applet in order to resync |
| 7198 | * with the response body. |
| 7199 | */ |
| 7200 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 7201 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7202 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7203 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7204 | if (!hlua) { |
| 7205 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7206 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7207 | return 0; |
| 7208 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7209 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7210 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7211 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7212 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7213 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7214 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7215 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7216 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7217 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7218 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7219 | if (!task) { |
| 7220 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7221 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7222 | return 0; |
| 7223 | } |
| 7224 | task->nice = 0; |
| 7225 | task->context = ctx; |
| 7226 | task->process = hlua_applet_wakeup; |
| 7227 | ctx->ctx.hlua_apphttp.task = task; |
| 7228 | |
| 7229 | /* In the execution wrappers linked with a stream, the |
| 7230 | * Lua context can be not initialized. This behavior |
| 7231 | * permits to save performances because a systematic |
| 7232 | * Lua initialization cause 5% performances loss. |
| 7233 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7234 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7235 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 7236 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7237 | return 0; |
| 7238 | } |
| 7239 | |
| 7240 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7241 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7242 | |
| 7243 | /* The following Lua calls can fail. */ |
| 7244 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7245 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7246 | error = lua_tostring(hlua->T, -1); |
| 7247 | else |
| 7248 | error = "critical error"; |
| 7249 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7250 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7251 | return 0; |
| 7252 | } |
| 7253 | |
| 7254 | /* Check stack available size. */ |
| 7255 | if (!lua_checkstack(hlua->T, 1)) { |
| 7256 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7257 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7258 | RESET_SAFE_LJMP(hlua->T); |
| 7259 | return 0; |
| 7260 | } |
| 7261 | |
| 7262 | /* Restore the function in the stack. */ |
| 7263 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7264 | |
| 7265 | /* Create and and push object stream in the stack. */ |
| 7266 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7267 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7268 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7269 | RESET_SAFE_LJMP(hlua->T); |
| 7270 | return 0; |
| 7271 | } |
| 7272 | hlua->nargs = 1; |
| 7273 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7274 | /* push keywords in the stack. */ |
| 7275 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7276 | if (!lua_checkstack(hlua->T, 1)) { |
| 7277 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7278 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7279 | RESET_SAFE_LJMP(hlua->T); |
| 7280 | return 0; |
| 7281 | } |
| 7282 | lua_pushstring(hlua->T, *arg); |
| 7283 | hlua->nargs++; |
| 7284 | } |
| 7285 | |
| 7286 | RESET_SAFE_LJMP(hlua->T); |
| 7287 | |
| 7288 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7289 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7290 | |
| 7291 | return 1; |
| 7292 | } |
| 7293 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7294 | static void hlua_applet_htx_fct(struct appctx *ctx) |
| 7295 | { |
| 7296 | struct stream_interface *si = ctx->owner; |
| 7297 | struct stream *strm = si_strm(si); |
| 7298 | struct channel *req = si_oc(si); |
| 7299 | struct channel *res = si_ic(si); |
| 7300 | struct act_rule *rule = ctx->rule; |
| 7301 | struct proxy *px = strm->be; |
| 7302 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7303 | struct htx *req_htx, *res_htx; |
| 7304 | |
| 7305 | res_htx = htx_from_buf(&res->buf); |
| 7306 | |
| 7307 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7308 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7309 | goto out; |
| 7310 | |
| 7311 | /* Check if the input buffer is avalaible. */ |
| 7312 | if (!b_size(&res->buf)) { |
| 7313 | si_rx_room_blk(si); |
| 7314 | goto out; |
| 7315 | } |
| 7316 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7317 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7318 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7319 | |
| 7320 | /* Set the currently running flag. */ |
| 7321 | if (!HLUA_IS_RUNNING(hlua) && |
| 7322 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7323 | struct htx_blk *blk; |
| 7324 | size_t count = co_data(req); |
| 7325 | |
| 7326 | if (!count) { |
| 7327 | si_cant_get(si); |
| 7328 | goto out; |
| 7329 | } |
| 7330 | |
| 7331 | /* We need to flush the request header. This left the body for |
| 7332 | * the Lua. |
| 7333 | */ |
| 7334 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7335 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7336 | while (count && blk) { |
| 7337 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7338 | uint32_t sz = htx_get_blksz(blk); |
| 7339 | |
| 7340 | if (sz > count) { |
| 7341 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7342 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7343 | goto out; |
| 7344 | } |
| 7345 | |
| 7346 | count -= sz; |
| 7347 | co_set_data(req, co_data(req) - sz); |
| 7348 | blk = htx_remove_blk(req_htx, blk); |
| 7349 | |
| 7350 | if (type == HTX_BLK_EOH) |
| 7351 | break; |
| 7352 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7353 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7354 | } |
| 7355 | |
| 7356 | /* Executes The applet if it is not done. */ |
| 7357 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7358 | |
| 7359 | /* Execute the function. */ |
| 7360 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7361 | /* finished. */ |
| 7362 | case HLUA_E_OK: |
| 7363 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7364 | break; |
| 7365 | |
| 7366 | /* yield. */ |
| 7367 | case HLUA_E_AGAIN: |
| 7368 | if (hlua->wake_time != TICK_ETERNITY) |
| 7369 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7370 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7371 | |
| 7372 | /* finished with error. */ |
| 7373 | case HLUA_E_ERRMSG: |
| 7374 | /* Display log. */ |
| 7375 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7376 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7377 | lua_pop(hlua->T, 1); |
| 7378 | goto error; |
| 7379 | |
| 7380 | case HLUA_E_ETMOUT: |
| 7381 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7382 | rule->arg.hlua_rule->fcn.name); |
| 7383 | goto error; |
| 7384 | |
| 7385 | case HLUA_E_NOMEM: |
| 7386 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7387 | rule->arg.hlua_rule->fcn.name); |
| 7388 | goto error; |
| 7389 | |
| 7390 | case HLUA_E_YIELD: /* unexpected */ |
| 7391 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7392 | rule->arg.hlua_rule->fcn.name); |
| 7393 | goto error; |
| 7394 | |
| 7395 | case HLUA_E_ERR: |
| 7396 | /* Display log. */ |
| 7397 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7398 | rule->arg.hlua_rule->fcn.name); |
| 7399 | goto error; |
| 7400 | |
| 7401 | default: |
| 7402 | goto error; |
| 7403 | } |
| 7404 | } |
| 7405 | |
| 7406 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7407 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7408 | goto done; |
| 7409 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7410 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7411 | goto error; |
| 7412 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7413 | /* Don't add TLR because mux-h1 will take care of it */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7414 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7415 | si_rx_room_blk(si); |
| 7416 | goto out; |
| 7417 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7418 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7419 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7420 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7421 | } |
| 7422 | |
| 7423 | done: |
| 7424 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7425 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7426 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7427 | si_shutr(si); |
| 7428 | } |
| 7429 | |
| 7430 | /* eat the whole request */ |
| 7431 | if (co_data(req)) { |
| 7432 | req_htx = htx_from_buf(&req->buf); |
| 7433 | co_htx_skip(req, req_htx, co_data(req)); |
| 7434 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7435 | } |
| 7436 | } |
| 7437 | |
| 7438 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7439 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7440 | return; |
| 7441 | |
| 7442 | error: |
| 7443 | |
| 7444 | /* If we are in HTTP mode, and we are not send any |
| 7445 | * data, return a 500 server error in best effort: |
| 7446 | * if there is no room available in the buffer, |
| 7447 | * just close the connection. |
| 7448 | */ |
| 7449 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7450 | struct buffer *err = &htx_err_chunks[HTTP_ERR_500]; |
| 7451 | |
| 7452 | channel_erase(res); |
| 7453 | res->buf.data = b_data(err); |
| 7454 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7455 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7456 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7457 | } |
| 7458 | if (!(strm->flags & SF_ERR_MASK)) |
| 7459 | strm->flags |= SF_ERR_RESOURCE; |
| 7460 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7461 | goto done; |
| 7462 | } |
| 7463 | |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7464 | void hlua_applet_http_fct(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7465 | { |
| 7466 | struct stream_interface *si = ctx->owner; |
| 7467 | struct stream *strm = si_strm(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7468 | struct channel *req = si_oc(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7469 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7470 | struct act_rule *rule = ctx->rule; |
| 7471 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7472 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7473 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7474 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7475 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7476 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7477 | int ret; |
| 7478 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7479 | if (IS_HTX_STRM(strm)) |
| 7480 | return hlua_applet_htx_fct(ctx); |
| 7481 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7482 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7483 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7484 | goto out; |
| 7485 | |
| 7486 | /* Check if the input buffer is avalaible. */ |
| 7487 | if (!b_size(&res->buf)) { |
| 7488 | si_rx_room_blk(si); |
| 7489 | goto out; |
| 7490 | } |
| 7491 | /* check that the output is not closed */ |
| 7492 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
| 7493 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7494 | |
| 7495 | /* Set the currently running flag. */ |
| 7496 | if (!HLUA_IS_RUNNING(hlua) && |
| 7497 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7498 | /* Store the max amount of bytes that we can read. */ |
| 7499 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 7500 | |
| 7501 | /* We need to flush the request header. This left the body |
| 7502 | * for the Lua. |
| 7503 | */ |
| 7504 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7505 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7506 | ret = co_getblk_nc(req, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7507 | if (ret == -1) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7508 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7509 | |
| 7510 | /* No data available, ask for more data. */ |
| 7511 | if (ret == 1) |
| 7512 | len2 = 0; |
| 7513 | if (ret == 0) |
| 7514 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 7515 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7516 | si_cant_get(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7517 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7518 | } |
| 7519 | |
| 7520 | /* skip the requests bytes. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7521 | co_skip(req, strm->txn->req.eoh + strm->txn->req.eol); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7522 | } |
| 7523 | |
| 7524 | /* Executes The applet if it is not done. */ |
| 7525 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7526 | |
| 7527 | /* Execute the function. */ |
| 7528 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7529 | /* finished. */ |
| 7530 | case HLUA_E_OK: |
| 7531 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7532 | break; |
| 7533 | |
| 7534 | /* yield. */ |
| 7535 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7536 | if (hlua->wake_time != TICK_ETERNITY) |
| 7537 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7538 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7539 | |
| 7540 | /* finished with error. */ |
| 7541 | case HLUA_E_ERRMSG: |
| 7542 | /* Display log. */ |
| 7543 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7544 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7545 | lua_pop(hlua->T, 1); |
| 7546 | goto error; |
| 7547 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7548 | case HLUA_E_ETMOUT: |
| 7549 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7550 | rule->arg.hlua_rule->fcn.name); |
| 7551 | goto error; |
| 7552 | |
| 7553 | case HLUA_E_NOMEM: |
| 7554 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7555 | rule->arg.hlua_rule->fcn.name); |
| 7556 | goto error; |
| 7557 | |
| 7558 | case HLUA_E_YIELD: /* unexpected */ |
| 7559 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7560 | rule->arg.hlua_rule->fcn.name); |
| 7561 | goto error; |
| 7562 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7563 | case HLUA_E_ERR: |
| 7564 | /* Display log. */ |
| 7565 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7566 | rule->arg.hlua_rule->fcn.name); |
| 7567 | goto error; |
| 7568 | |
| 7569 | default: |
| 7570 | goto error; |
| 7571 | } |
| 7572 | } |
| 7573 | |
| 7574 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7575 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7576 | goto done; |
| 7577 | |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 7578 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7579 | goto error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7580 | |
| 7581 | /* We must send the final chunk. */ |
| 7582 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 7583 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 7584 | |
| 7585 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 7586 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7587 | |
| 7588 | /* critical error. */ |
| 7589 | if (ret == -2 || ret == -3) { |
| 7590 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 7591 | rule->arg.hlua_rule->fcn.name); |
| 7592 | goto error; |
| 7593 | } |
| 7594 | |
| 7595 | /* no enough space error. */ |
| 7596 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7597 | si_rx_room_blk(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7598 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7599 | } |
| 7600 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7601 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7602 | ctx->ctx.hlua_apphttp.flags |= (APPLET_LAST_CHK|APPLET_RSP_SENT); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7603 | } |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7604 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7605 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7606 | done: |
| 7607 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 7608 | if (!(res->flags & CF_SHUTR)) { |
| 7609 | res->flags |= CF_READ_NULL; |
| 7610 | si_shutr(si); |
| 7611 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7612 | |
| 7613 | /* eat the whole request */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7614 | if (co_data(req)) |
| 7615 | co_skip(req, co_data(req)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7616 | } |
| 7617 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7618 | out: |
| 7619 | return; |
| 7620 | |
| 7621 | error: |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7622 | |
| 7623 | /* If we are in HTTP mode, and we are not send any |
| 7624 | * data, return a 500 server error in best effort: |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7625 | * if there is no room available in the buffer, |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7626 | * just close the connection. |
| 7627 | */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7628 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7629 | channel_erase(res); |
| 7630 | ci_putblk(res, error_500, strlen(error_500)); |
| 7631 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7632 | if (!(strm->flags & SF_ERR_MASK)) |
| 7633 | strm->flags |= SF_ERR_RESOURCE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7634 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7635 | goto done; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7636 | } |
| 7637 | |
| 7638 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7639 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7640 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7641 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7642 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7643 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7644 | } |
| 7645 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7646 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 7647 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7648 | * |
| 7649 | * This function can fail with an abort() due to an Lua critical error. |
| 7650 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7651 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7652 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7653 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7654 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7655 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7656 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7657 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7658 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7659 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7660 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7661 | if (!rule->arg.hlua_rule) { |
| 7662 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7663 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7664 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7665 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7666 | /* Memory for arguments. */ |
| 7667 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7668 | if (!rule->arg.hlua_rule->args) { |
| 7669 | memprintf(err, "out of memory error"); |
| 7670 | return ACT_RET_PRS_ERR; |
| 7671 | } |
| 7672 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7673 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7674 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7675 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7676 | /* Expect some arguments */ |
| 7677 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7678 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7679 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7680 | return ACT_RET_PRS_ERR; |
| 7681 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7682 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7683 | if (!rule->arg.hlua_rule->args[i]) { |
| 7684 | memprintf(err, "out of memory error"); |
| 7685 | return ACT_RET_PRS_ERR; |
| 7686 | } |
| 7687 | (*cur_arg)++; |
| 7688 | } |
| 7689 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7690 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7691 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7692 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7693 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7694 | } |
| 7695 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7696 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7697 | struct act_rule *rule, char **err) |
| 7698 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7699 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7700 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7701 | /* HTTP applets are forbidden in tcp-request rules. |
| 7702 | * HTTP applet request requires everything initilized by |
| 7703 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 7704 | * The applet will be immediately initilized, but its before |
| 7705 | * the call of this analyzer. |
| 7706 | */ |
| 7707 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7708 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7709 | return ACT_RET_PRS_ERR; |
| 7710 | } |
| 7711 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7712 | /* Memory for the rule. */ |
| 7713 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7714 | if (!rule->arg.hlua_rule) { |
| 7715 | memprintf(err, "out of memory error"); |
| 7716 | return ACT_RET_PRS_ERR; |
| 7717 | } |
| 7718 | |
| 7719 | /* Reference the Lua function and store the reference. */ |
| 7720 | rule->arg.hlua_rule->fcn = *fcn; |
| 7721 | |
| 7722 | /* TODO: later accept arguments. */ |
| 7723 | rule->arg.hlua_rule->args = NULL; |
| 7724 | |
| 7725 | /* Add applet pointer in the rule. */ |
| 7726 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7727 | rule->applet.name = fcn->name; |
| 7728 | rule->applet.init = hlua_applet_http_init; |
| 7729 | rule->applet.fct = hlua_applet_http_fct; |
| 7730 | rule->applet.release = hlua_applet_http_release; |
| 7731 | rule->applet.timeout = hlua_timeout_applet; |
| 7732 | |
| 7733 | return ACT_RET_PRS_OK; |
| 7734 | } |
| 7735 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7736 | /* This function is an LUA binding used for registering |
| 7737 | * "sample-conv" functions. It expects a converter name used |
| 7738 | * in the haproxy configuration file, and an LUA function. |
| 7739 | */ |
| 7740 | __LJMP static int hlua_register_action(lua_State *L) |
| 7741 | { |
| 7742 | struct action_kw_list *akl; |
| 7743 | const char *name; |
| 7744 | int ref; |
| 7745 | int len; |
| 7746 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7747 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7748 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7749 | /* Initialise the number of expected arguments at 0. */ |
| 7750 | nargs = 0; |
| 7751 | |
| 7752 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7753 | 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] | 7754 | |
| 7755 | /* First argument : converter name. */ |
| 7756 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7757 | |
| 7758 | /* Second argument : environment. */ |
| 7759 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7760 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7761 | |
| 7762 | /* Third argument : lua function. */ |
| 7763 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7764 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7765 | /* 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] | 7766 | if (lua_gettop(L) >= 4) |
| 7767 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7768 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7769 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7770 | lua_pushnil(L); |
| 7771 | while (lua_next(L, 2) != 0) { |
| 7772 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7773 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7774 | |
| 7775 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7776 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7777 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7778 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7779 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7780 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7781 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7782 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7783 | |
| 7784 | /* Fill fcn. */ |
| 7785 | fcn->name = strdup(name); |
| 7786 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7787 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7788 | fcn->function_ref = ref; |
| 7789 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7790 | /* Set the expected number od arguments. */ |
| 7791 | fcn->nargs = nargs; |
| 7792 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7793 | /* List head */ |
| 7794 | akl->list.n = akl->list.p = NULL; |
| 7795 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7796 | /* action keyword. */ |
| 7797 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7798 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7799 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7800 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7801 | |
| 7802 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7803 | |
| 7804 | akl->kw[0].match_pfx = 0; |
| 7805 | akl->kw[0].private = fcn; |
| 7806 | akl->kw[0].parse = action_register_lua; |
| 7807 | |
| 7808 | /* select the action registering point. */ |
| 7809 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7810 | tcp_req_cont_keywords_register(akl); |
| 7811 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7812 | tcp_res_cont_keywords_register(akl); |
| 7813 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7814 | http_req_keywords_register(akl); |
| 7815 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7816 | http_res_keywords_register(akl); |
| 7817 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7818 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7819 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7820 | "are expected.", lua_tostring(L, -1))); |
| 7821 | |
| 7822 | /* pop the environment string. */ |
| 7823 | lua_pop(L, 1); |
| 7824 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7825 | return ACT_RET_PRS_OK; |
| 7826 | } |
| 7827 | |
| 7828 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7829 | struct act_rule *rule, char **err) |
| 7830 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7831 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7832 | |
Christopher Faulet | eb70980 | 2019-04-11 22:04:08 +0200 | [diff] [blame] | 7833 | if (px->mode == PR_MODE_HTTP && (px->options2 & PR_O2_USE_HTX)) { |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7834 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 7835 | return ACT_RET_PRS_ERR; |
| 7836 | } |
| 7837 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7838 | /* Memory for the rule. */ |
| 7839 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7840 | if (!rule->arg.hlua_rule) { |
| 7841 | memprintf(err, "out of memory error"); |
| 7842 | return ACT_RET_PRS_ERR; |
| 7843 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7844 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7845 | /* Reference the Lua function and store the reference. */ |
| 7846 | rule->arg.hlua_rule->fcn = *fcn; |
| 7847 | |
| 7848 | /* TODO: later accept arguments. */ |
| 7849 | rule->arg.hlua_rule->args = NULL; |
| 7850 | |
| 7851 | /* Add applet pointer in the rule. */ |
| 7852 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7853 | rule->applet.name = fcn->name; |
| 7854 | rule->applet.init = hlua_applet_tcp_init; |
| 7855 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7856 | rule->applet.release = hlua_applet_tcp_release; |
| 7857 | rule->applet.timeout = hlua_timeout_applet; |
| 7858 | |
| 7859 | return 0; |
| 7860 | } |
| 7861 | |
| 7862 | /* This function is an LUA binding used for registering |
| 7863 | * "sample-conv" functions. It expects a converter name used |
| 7864 | * in the haproxy configuration file, and an LUA function. |
| 7865 | */ |
| 7866 | __LJMP static int hlua_register_service(lua_State *L) |
| 7867 | { |
| 7868 | struct action_kw_list *akl; |
| 7869 | const char *name; |
| 7870 | const char *env; |
| 7871 | int ref; |
| 7872 | int len; |
| 7873 | struct hlua_function *fcn; |
| 7874 | |
| 7875 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7876 | |
| 7877 | /* First argument : converter name. */ |
| 7878 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7879 | |
| 7880 | /* Second argument : environment. */ |
| 7881 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7882 | |
| 7883 | /* Third argument : lua function. */ |
| 7884 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7885 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7886 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7887 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7888 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7889 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7890 | fcn = calloc(1, sizeof(*fcn)); |
| 7891 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7892 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7893 | |
| 7894 | /* Fill fcn. */ |
| 7895 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7896 | fcn->name = calloc(1, len); |
| 7897 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7898 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7899 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7900 | fcn->function_ref = ref; |
| 7901 | |
| 7902 | /* List head */ |
| 7903 | akl->list.n = akl->list.p = NULL; |
| 7904 | |
| 7905 | /* converter keyword. */ |
| 7906 | len = strlen("lua.") + strlen(name) + 1; |
| 7907 | akl->kw[0].kw = calloc(1, len); |
| 7908 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7909 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7910 | |
| 7911 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7912 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7913 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7914 | if (strcmp(env, "tcp") == 0) |
| 7915 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7916 | else if (strcmp(env, "http") == 0) |
| 7917 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7918 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7919 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7920 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7921 | |
| 7922 | akl->kw[0].match_pfx = 0; |
| 7923 | akl->kw[0].private = fcn; |
| 7924 | |
| 7925 | /* End of array. */ |
| 7926 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7927 | |
| 7928 | /* Register this new converter */ |
| 7929 | service_keywords_register(akl); |
| 7930 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7931 | return 0; |
| 7932 | } |
| 7933 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7934 | /* This function initialises Lua cli handler. It copies the |
| 7935 | * arguments in the Lua stack and create channel IO objects. |
| 7936 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7937 | 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] | 7938 | { |
| 7939 | struct hlua *hlua; |
| 7940 | struct hlua_function *fcn; |
| 7941 | int i; |
| 7942 | const char *error; |
| 7943 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7944 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7945 | appctx->ctx.hlua_cli.fcn = private; |
| 7946 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7947 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7948 | if (!hlua) { |
| 7949 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7950 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7951 | } |
| 7952 | HLUA_INIT(hlua); |
| 7953 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7954 | |
| 7955 | /* Create task used by signal to wakeup applets. |
| 7956 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7957 | * applet_http. It is absolutely compatible. |
| 7958 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7959 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7960 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7961 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7962 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7963 | } |
| 7964 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7965 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7966 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7967 | |
| 7968 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7969 | 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] | 7970 | 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] | 7971 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7972 | } |
| 7973 | |
| 7974 | /* The following Lua calls can fail. */ |
| 7975 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7976 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7977 | error = lua_tostring(hlua->T, -1); |
| 7978 | else |
| 7979 | error = "critical error"; |
| 7980 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7981 | goto error; |
| 7982 | } |
| 7983 | |
| 7984 | /* Check stack available size. */ |
| 7985 | if (!lua_checkstack(hlua->T, 2)) { |
| 7986 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7987 | goto error; |
| 7988 | } |
| 7989 | |
| 7990 | /* Restore the function in the stack. */ |
| 7991 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7992 | |
| 7993 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7994 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7995 | */ |
| 7996 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7997 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7998 | goto error; |
| 7999 | } |
| 8000 | hlua->nargs = 1; |
| 8001 | |
| 8002 | /* push keywords in the stack. */ |
| 8003 | for (i = 0; *args[i]; i++) { |
| 8004 | /* Check stack available size. */ |
| 8005 | if (!lua_checkstack(hlua->T, 1)) { |
| 8006 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 8007 | goto error; |
| 8008 | } |
| 8009 | lua_pushstring(hlua->T, args[i]); |
| 8010 | hlua->nargs++; |
| 8011 | } |
| 8012 | |
| 8013 | /* We must initialize the execution timeouts. */ |
| 8014 | hlua->max_time = hlua_timeout_session; |
| 8015 | |
| 8016 | /* At this point the execution is safe. */ |
| 8017 | RESET_SAFE_LJMP(hlua->T); |
| 8018 | |
| 8019 | /* It's ok */ |
| 8020 | return 0; |
| 8021 | |
| 8022 | /* It's not ok. */ |
| 8023 | error: |
| 8024 | RESET_SAFE_LJMP(hlua->T); |
| 8025 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8026 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8027 | return 1; |
| 8028 | } |
| 8029 | |
| 8030 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8031 | { |
| 8032 | struct hlua *hlua; |
| 8033 | struct stream_interface *si; |
| 8034 | struct hlua_function *fcn; |
| 8035 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8036 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8037 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8038 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8039 | |
| 8040 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8041 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8042 | return 1; |
| 8043 | |
| 8044 | /* Execute the function. */ |
| 8045 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8046 | |
| 8047 | /* finished. */ |
| 8048 | case HLUA_E_OK: |
| 8049 | return 1; |
| 8050 | |
| 8051 | /* yield. */ |
| 8052 | case HLUA_E_AGAIN: |
| 8053 | /* We want write. */ |
| 8054 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8055 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8056 | /* Set the timeout. */ |
| 8057 | if (hlua->wake_time != TICK_ETERNITY) |
| 8058 | task_schedule(hlua->task, hlua->wake_time); |
| 8059 | return 0; |
| 8060 | |
| 8061 | /* finished with error. */ |
| 8062 | case HLUA_E_ERRMSG: |
| 8063 | /* Display log. */ |
| 8064 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8065 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8066 | lua_pop(hlua->T, 1); |
| 8067 | return 1; |
| 8068 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8069 | case HLUA_E_ETMOUT: |
| 8070 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8071 | fcn->name); |
| 8072 | return 1; |
| 8073 | |
| 8074 | case HLUA_E_NOMEM: |
| 8075 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8076 | fcn->name); |
| 8077 | return 1; |
| 8078 | |
| 8079 | case HLUA_E_YIELD: /* unexpected */ |
| 8080 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8081 | fcn->name); |
| 8082 | return 1; |
| 8083 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8084 | case HLUA_E_ERR: |
| 8085 | /* Display log. */ |
| 8086 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8087 | fcn->name); |
| 8088 | return 1; |
| 8089 | |
| 8090 | default: |
| 8091 | return 1; |
| 8092 | } |
| 8093 | |
| 8094 | return 1; |
| 8095 | } |
| 8096 | |
| 8097 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8098 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8099 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8100 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8101 | } |
| 8102 | |
| 8103 | /* This function is an LUA binding used for registering |
| 8104 | * new keywords in the cli. It expects a list of keywords |
| 8105 | * which are the "path". It is limited to 5 keywords. A |
| 8106 | * description of the command, a function to be executed |
| 8107 | * for the parsing and a function for io handlers. |
| 8108 | */ |
| 8109 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8110 | { |
| 8111 | struct cli_kw_list *cli_kws; |
| 8112 | const char *message; |
| 8113 | int ref_io; |
| 8114 | int len; |
| 8115 | struct hlua_function *fcn; |
| 8116 | int index; |
| 8117 | int i; |
| 8118 | |
| 8119 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8120 | |
| 8121 | /* First argument : an array of maximum 5 keywords. */ |
| 8122 | if (!lua_istable(L, 1)) |
| 8123 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8124 | |
| 8125 | /* Second argument : string with contextual message. */ |
| 8126 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8127 | |
| 8128 | /* Third and fourth argument : lua function. */ |
| 8129 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8130 | |
| 8131 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8132 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 8133 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8134 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8135 | fcn = calloc(1, sizeof(*fcn)); |
| 8136 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8137 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8138 | |
| 8139 | /* Fill path. */ |
| 8140 | index = 0; |
| 8141 | lua_pushnil(L); |
| 8142 | while(lua_next(L, 1) != 0) { |
| 8143 | if (index >= 5) |
| 8144 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8145 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8146 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8147 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 8148 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8149 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8150 | index++; |
| 8151 | lua_pop(L, 1); |
| 8152 | } |
| 8153 | |
| 8154 | /* Copy help message. */ |
| 8155 | cli_kws->kw[0].usage = strdup(message); |
| 8156 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8157 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8158 | |
| 8159 | /* Fill fcn io handler. */ |
| 8160 | len = strlen("<lua.cli>") + 1; |
| 8161 | for (i = 0; i < index; i++) |
| 8162 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8163 | fcn->name = calloc(1, len); |
| 8164 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8165 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8166 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8167 | for (i = 0; i < index; i++) { |
| 8168 | strncat((char *)fcn->name, ".", len); |
| 8169 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8170 | } |
| 8171 | strncat((char *)fcn->name, ">", len); |
| 8172 | fcn->function_ref = ref_io; |
| 8173 | |
| 8174 | /* Fill last entries. */ |
| 8175 | cli_kws->kw[0].private = fcn; |
| 8176 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8177 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8178 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8179 | |
| 8180 | /* Register this new converter */ |
| 8181 | cli_register_kw(cli_kws); |
| 8182 | |
| 8183 | return 0; |
| 8184 | } |
| 8185 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8186 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 8187 | struct proxy *defpx, const char *file, int line, |
| 8188 | char **err, unsigned int *timeout) |
| 8189 | { |
| 8190 | const char *error; |
| 8191 | |
| 8192 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8193 | if (error == PARSE_TIME_OVER) { |
| 8194 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8195 | args[1], args[0]); |
| 8196 | return -1; |
| 8197 | } |
| 8198 | else if (error == PARSE_TIME_UNDER) { |
| 8199 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8200 | args[1], args[0]); |
| 8201 | return -1; |
| 8202 | } |
| 8203 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8204 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8205 | return -1; |
| 8206 | } |
| 8207 | return 0; |
| 8208 | } |
| 8209 | |
| 8210 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 8211 | struct proxy *defpx, const char *file, int line, |
| 8212 | char **err) |
| 8213 | { |
| 8214 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8215 | file, line, err, &hlua_timeout_session); |
| 8216 | } |
| 8217 | |
| 8218 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 8219 | struct proxy *defpx, const char *file, int line, |
| 8220 | char **err) |
| 8221 | { |
| 8222 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8223 | file, line, err, &hlua_timeout_task); |
| 8224 | } |
| 8225 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8226 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 8227 | struct proxy *defpx, const char *file, int line, |
| 8228 | char **err) |
| 8229 | { |
| 8230 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8231 | file, line, err, &hlua_timeout_applet); |
| 8232 | } |
| 8233 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8234 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 8235 | struct proxy *defpx, const char *file, int line, |
| 8236 | char **err) |
| 8237 | { |
| 8238 | char *error; |
| 8239 | |
| 8240 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8241 | if (*error != '\0') { |
| 8242 | memprintf(err, "%s: invalid number", args[0]); |
| 8243 | return -1; |
| 8244 | } |
| 8245 | return 0; |
| 8246 | } |
| 8247 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8248 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 8249 | struct proxy *defpx, const char *file, int line, |
| 8250 | char **err) |
| 8251 | { |
| 8252 | char *error; |
| 8253 | |
| 8254 | if (*(args[1]) == 0) { |
| 8255 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8256 | return -1; |
| 8257 | } |
| 8258 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8259 | if (*error != '\0') { |
| 8260 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8261 | return -1; |
| 8262 | } |
| 8263 | return 0; |
| 8264 | } |
| 8265 | |
| 8266 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8267 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8268 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8269 | * the main lua entry point. |
| 8270 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8271 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8272 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8273 | * |
| 8274 | * In some error case, LUA set an error message in top of the stack. This function |
| 8275 | * 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] | 8276 | * |
| 8277 | * This function can fail with an abort() due to an Lua critical error. |
| 8278 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8279 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8280 | */ |
| 8281 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 8282 | struct proxy *defpx, const char *file, int line, |
| 8283 | char **err) |
| 8284 | { |
| 8285 | int error; |
| 8286 | |
| 8287 | /* Just load and compile the file. */ |
| 8288 | error = luaL_loadfile(gL.T, args[1]); |
| 8289 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8290 | 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] | 8291 | lua_pop(gL.T, 1); |
| 8292 | return -1; |
| 8293 | } |
| 8294 | |
| 8295 | /* If no syntax error where detected, execute the code. */ |
| 8296 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 8297 | switch (error) { |
| 8298 | case LUA_OK: |
| 8299 | break; |
| 8300 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8301 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8302 | lua_pop(gL.T, 1); |
| 8303 | return -1; |
| 8304 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8305 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8306 | return -1; |
| 8307 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8308 | 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] | 8309 | lua_pop(gL.T, 1); |
| 8310 | return -1; |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8311 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8312 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8313 | 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] | 8314 | lua_pop(gL.T, 1); |
| 8315 | return -1; |
Christopher Faulet | 2069990 | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 8316 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8317 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8318 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8319 | lua_pop(gL.T, 1); |
| 8320 | return -1; |
| 8321 | } |
| 8322 | |
| 8323 | return 0; |
| 8324 | } |
| 8325 | |
| 8326 | /* configuration keywords declaration */ |
| 8327 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8328 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8329 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8330 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8331 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8332 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8333 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8334 | { 0, NULL, NULL }, |
| 8335 | }}; |
| 8336 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8337 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8338 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8339 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8340 | /* This function can fail with an abort() due to an Lua critical error. |
| 8341 | * We are in the initialisation process of HAProxy, this abort() is |
| 8342 | * tolerated. |
| 8343 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8344 | int hlua_post_init() |
| 8345 | { |
| 8346 | struct hlua_init_function *init; |
| 8347 | const char *msg; |
| 8348 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8349 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8350 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8351 | /* Call post initialisation function in safe environement. */ |
| 8352 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8353 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8354 | error = lua_tostring(gL.T, -1); |
| 8355 | else |
| 8356 | error = "critical error"; |
| 8357 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8358 | exit(1); |
| 8359 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8360 | |
| 8361 | #if USE_OPENSSL |
| 8362 | /* Initialize SSL server. */ |
| 8363 | if (socket_ssl.xprt->prepare_srv) { |
| 8364 | int saved_used_backed = global.ssl_used_backend; |
| 8365 | // don't affect maxconn automatic computation |
| 8366 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8367 | global.ssl_used_backend = saved_used_backed; |
| 8368 | } |
| 8369 | #endif |
| 8370 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8371 | hlua_fcn_post_init(gL.T); |
| 8372 | RESET_SAFE_LJMP(gL.T); |
| 8373 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8374 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8375 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 8376 | ret = hlua_ctx_resume(&gL, 0); |
| 8377 | switch (ret) { |
| 8378 | case HLUA_E_OK: |
| 8379 | lua_pop(gL.T, -1); |
| 8380 | return 1; |
| 8381 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8382 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8383 | return 0; |
| 8384 | case HLUA_E_ERRMSG: |
| 8385 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8386 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8387 | return 0; |
| 8388 | case HLUA_E_ERR: |
| 8389 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8390 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8391 | return 0; |
| 8392 | } |
| 8393 | } |
| 8394 | return 1; |
| 8395 | } |
| 8396 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8397 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8398 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8399 | * is the previously allocated size or the kind of object in case of a new |
| 8400 | * allocation. <nsize> is the requested new size. |
| 8401 | */ |
| 8402 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8403 | { |
| 8404 | struct hlua_mem_allocator *zone = ud; |
| 8405 | |
| 8406 | if (nsize == 0) { |
| 8407 | /* it's a free */ |
| 8408 | if (ptr) |
| 8409 | zone->allocated -= osize; |
| 8410 | free(ptr); |
| 8411 | return NULL; |
| 8412 | } |
| 8413 | |
| 8414 | if (!ptr) { |
| 8415 | /* it's a new allocation */ |
| 8416 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8417 | return NULL; |
| 8418 | |
| 8419 | ptr = malloc(nsize); |
| 8420 | if (ptr) |
| 8421 | zone->allocated += nsize; |
| 8422 | return ptr; |
| 8423 | } |
| 8424 | |
| 8425 | /* it's a realloc */ |
| 8426 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8427 | return NULL; |
| 8428 | |
| 8429 | ptr = realloc(ptr, nsize); |
| 8430 | if (ptr) |
| 8431 | zone->allocated += nsize - osize; |
| 8432 | return ptr; |
| 8433 | } |
| 8434 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8435 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8436 | * We are in the initialisation process of HAProxy, this abort() is |
| 8437 | * tolerated. |
| 8438 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8439 | void hlua_init(void) |
| 8440 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8441 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8442 | int idx; |
| 8443 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8444 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8445 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8446 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8447 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8448 | struct srv_kw *kw; |
| 8449 | int tmp_error; |
| 8450 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8451 | char *args[] = { /* SSL client configuration. */ |
| 8452 | "ssl", |
| 8453 | "verify", |
| 8454 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8455 | NULL |
| 8456 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8457 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8458 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8459 | /* Init main lua stack. */ |
| 8460 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8461 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8462 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8463 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8464 | hlua_sethlua(&gL); |
| 8465 | gL.Tref = LUA_REFNIL; |
| 8466 | gL.task = NULL; |
| 8467 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8468 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8469 | * the Lua function can fail with an abort. We are in the initialisation |
| 8470 | * process of HAProxy, this abort() is tolerated. |
| 8471 | */ |
| 8472 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8473 | /* Initialise lua. */ |
| 8474 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8475 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8476 | /* Set safe environment for the initialisation. */ |
| 8477 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8478 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8479 | error_msg = lua_tostring(gL.T, -1); |
| 8480 | else |
| 8481 | error_msg = "critical error"; |
| 8482 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8483 | exit(1); |
| 8484 | } |
| 8485 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8486 | /* |
| 8487 | * |
| 8488 | * Create "core" object. |
| 8489 | * |
| 8490 | */ |
| 8491 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8492 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8493 | lua_newtable(gL.T); |
| 8494 | |
| 8495 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8496 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8497 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8498 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8499 | /* Register special functions. */ |
| 8500 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8501 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8502 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8503 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8504 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8505 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8506 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8507 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8508 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8509 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8510 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8511 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8512 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8513 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8514 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8515 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8516 | hlua_class_function(gL.T, "log", hlua_log); |
| 8517 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8518 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8519 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8520 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8521 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8522 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8523 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8524 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8525 | |
| 8526 | /* |
| 8527 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8528 | * Register class Map |
| 8529 | * |
| 8530 | */ |
| 8531 | |
| 8532 | /* This table entry is the object "Map" base. */ |
| 8533 | lua_newtable(gL.T); |
| 8534 | |
| 8535 | /* register pattern types. */ |
| 8536 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8537 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8538 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8539 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8540 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8541 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8542 | |
| 8543 | /* register constructor. */ |
| 8544 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8545 | |
| 8546 | /* Create and fill the metatable. */ |
| 8547 | lua_newtable(gL.T); |
| 8548 | |
| 8549 | /* Create and fille the __index entry. */ |
| 8550 | lua_pushstring(gL.T, "__index"); |
| 8551 | lua_newtable(gL.T); |
| 8552 | |
| 8553 | /* Register . */ |
| 8554 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8555 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8556 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8557 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8558 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8559 | /* Register previous table in the registry with reference and named entry. |
| 8560 | * The function hlua_register_metatable() pops the stack, so we |
| 8561 | * previously create a copy of the table. |
| 8562 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8563 | 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] | 8564 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8565 | |
| 8566 | /* Assign the metatable to the mai Map object. */ |
| 8567 | lua_setmetatable(gL.T, -2); |
| 8568 | |
| 8569 | /* Set a name to the table. */ |
| 8570 | lua_setglobal(gL.T, "Map"); |
| 8571 | |
| 8572 | /* |
| 8573 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8574 | * Register class Channel |
| 8575 | * |
| 8576 | */ |
| 8577 | |
| 8578 | /* Create and fill the metatable. */ |
| 8579 | lua_newtable(gL.T); |
| 8580 | |
| 8581 | /* Create and fille the __index entry. */ |
| 8582 | lua_pushstring(gL.T, "__index"); |
| 8583 | lua_newtable(gL.T); |
| 8584 | |
| 8585 | /* Register . */ |
| 8586 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8587 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8588 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8589 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8590 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8591 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8592 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8593 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8594 | 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] | 8595 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8596 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8597 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8598 | |
| 8599 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8600 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8601 | |
| 8602 | /* |
| 8603 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8604 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8605 | * |
| 8606 | */ |
| 8607 | |
| 8608 | /* Create and fill the metatable. */ |
| 8609 | lua_newtable(gL.T); |
| 8610 | |
| 8611 | /* Create and fille the __index entry. */ |
| 8612 | lua_pushstring(gL.T, "__index"); |
| 8613 | lua_newtable(gL.T); |
| 8614 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8615 | /* Browse existing fetches and create the associated |
| 8616 | * object method. |
| 8617 | */ |
| 8618 | sf = NULL; |
| 8619 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8620 | |
| 8621 | /* Dont register the keywork if the arguments check function are |
| 8622 | * not safe during the runtime. |
| 8623 | */ |
| 8624 | if ((sf->val_args != NULL) && |
| 8625 | (sf->val_args != val_payload_lv) && |
| 8626 | (sf->val_args != val_hdr)) |
| 8627 | continue; |
| 8628 | |
| 8629 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8630 | * by an underscore. |
| 8631 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8632 | strncpy(trash.area, sf->kw, trash.size); |
| 8633 | trash.area[trash.size - 1] = '\0'; |
| 8634 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8635 | if (*p == '.' || *p == '-' || *p == '+') |
| 8636 | *p = '_'; |
| 8637 | |
| 8638 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8639 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8640 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8641 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8642 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8643 | } |
| 8644 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8645 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8646 | |
| 8647 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8648 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8649 | |
| 8650 | /* |
| 8651 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8652 | * Register class Converters |
| 8653 | * |
| 8654 | */ |
| 8655 | |
| 8656 | /* Create and fill the metatable. */ |
| 8657 | lua_newtable(gL.T); |
| 8658 | |
| 8659 | /* Create and fill the __index entry. */ |
| 8660 | lua_pushstring(gL.T, "__index"); |
| 8661 | lua_newtable(gL.T); |
| 8662 | |
| 8663 | /* Browse existing converters and create the associated |
| 8664 | * object method. |
| 8665 | */ |
| 8666 | sc = NULL; |
| 8667 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8668 | /* Dont register the keywork if the arguments check function are |
| 8669 | * not safe during the runtime. |
| 8670 | */ |
| 8671 | if (sc->val_args != NULL) |
| 8672 | continue; |
| 8673 | |
| 8674 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8675 | * by an underscore. |
| 8676 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8677 | strncpy(trash.area, sc->kw, trash.size); |
| 8678 | trash.area[trash.size - 1] = '\0'; |
| 8679 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8680 | if (*p == '.' || *p == '-' || *p == '+') |
| 8681 | *p = '_'; |
| 8682 | |
| 8683 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8684 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8685 | lua_pushlightuserdata(gL.T, sc); |
| 8686 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8687 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8688 | } |
| 8689 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8690 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8691 | |
| 8692 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8693 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8694 | |
| 8695 | /* |
| 8696 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8697 | * Register class HTTP |
| 8698 | * |
| 8699 | */ |
| 8700 | |
| 8701 | /* Create and fill the metatable. */ |
| 8702 | lua_newtable(gL.T); |
| 8703 | |
| 8704 | /* Create and fille the __index entry. */ |
| 8705 | lua_pushstring(gL.T, "__index"); |
| 8706 | lua_newtable(gL.T); |
| 8707 | |
| 8708 | /* Register Lua functions. */ |
| 8709 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8710 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8711 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8712 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8713 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8714 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8715 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8716 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8717 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8718 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8719 | |
| 8720 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8721 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8722 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8723 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8724 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8725 | 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] | 8726 | 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] | 8727 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8728 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8729 | |
| 8730 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8731 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8732 | |
| 8733 | /* |
| 8734 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8735 | * Register class AppletTCP |
| 8736 | * |
| 8737 | */ |
| 8738 | |
| 8739 | /* Create and fill the metatable. */ |
| 8740 | lua_newtable(gL.T); |
| 8741 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8742 | /* Create and fille the __index entry. */ |
| 8743 | lua_pushstring(gL.T, "__index"); |
| 8744 | lua_newtable(gL.T); |
| 8745 | |
| 8746 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8747 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8748 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8749 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8750 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8751 | 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] | 8752 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8753 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8754 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8755 | |
| 8756 | lua_settable(gL.T, -3); |
| 8757 | |
| 8758 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8759 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8760 | |
| 8761 | /* |
| 8762 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8763 | * Register class AppletHTTP |
| 8764 | * |
| 8765 | */ |
| 8766 | |
| 8767 | /* Create and fill the metatable. */ |
| 8768 | lua_newtable(gL.T); |
| 8769 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8770 | /* Create and fille the __index entry. */ |
| 8771 | lua_pushstring(gL.T, "__index"); |
| 8772 | lua_newtable(gL.T); |
| 8773 | |
| 8774 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8775 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8776 | 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] | 8777 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8778 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8779 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8780 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8781 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8782 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8783 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8784 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8785 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8786 | |
| 8787 | lua_settable(gL.T, -3); |
| 8788 | |
| 8789 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8790 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8791 | |
| 8792 | /* |
| 8793 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8794 | * Register class TXN |
| 8795 | * |
| 8796 | */ |
| 8797 | |
| 8798 | /* Create and fill the metatable. */ |
| 8799 | lua_newtable(gL.T); |
| 8800 | |
| 8801 | /* Create and fille the __index entry. */ |
| 8802 | lua_pushstring(gL.T, "__index"); |
| 8803 | lua_newtable(gL.T); |
| 8804 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8805 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8806 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8807 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8808 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8809 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8810 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8811 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 8812 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8813 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8814 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8815 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8816 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8817 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8818 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8819 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8820 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8821 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8822 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8823 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8824 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8825 | |
| 8826 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8827 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8828 | |
| 8829 | /* |
| 8830 | * |
| 8831 | * Register class Socket |
| 8832 | * |
| 8833 | */ |
| 8834 | |
| 8835 | /* Create and fill the metatable. */ |
| 8836 | lua_newtable(gL.T); |
| 8837 | |
| 8838 | /* Create and fille the __index entry. */ |
| 8839 | lua_pushstring(gL.T, "__index"); |
| 8840 | lua_newtable(gL.T); |
| 8841 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8842 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8843 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8844 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8845 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8846 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8847 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8848 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8849 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8850 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8851 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8852 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8853 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8854 | 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] | 8855 | |
| 8856 | /* Register the garbage collector entry. */ |
| 8857 | lua_pushstring(gL.T, "__gc"); |
| 8858 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8859 | 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] | 8860 | |
| 8861 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8862 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8863 | |
| 8864 | /* Proxy and server configuration initialisation. */ |
| 8865 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8866 | init_new_proxy(&socket_proxy); |
| 8867 | socket_proxy.parent = NULL; |
| 8868 | socket_proxy.last_change = now.tv_sec; |
| 8869 | socket_proxy.id = "LUA-SOCKET"; |
| 8870 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8871 | socket_proxy.maxconn = 0; |
| 8872 | socket_proxy.accept = NULL; |
| 8873 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8874 | socket_proxy.srv = NULL; |
| 8875 | socket_proxy.conn_retries = 0; |
| 8876 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8877 | |
| 8878 | /* Init TCP server: unchanged parameters */ |
| 8879 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8880 | socket_tcp.next = NULL; |
| 8881 | socket_tcp.proxy = &socket_proxy; |
| 8882 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8883 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8884 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8885 | socket_tcp.priv_conns = NULL; |
| 8886 | socket_tcp.idle_conns = NULL; |
| 8887 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8888 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8889 | socket_tcp.last_change = 0; |
| 8890 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8891 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8892 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8893 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8894 | |
| 8895 | /* XXX: Copy default parameter from default server, |
| 8896 | * but the default server is not initialized. |
| 8897 | */ |
| 8898 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8899 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8900 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8901 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8902 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8903 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8904 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8905 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8906 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8907 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8908 | |
| 8909 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8910 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8911 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8912 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8913 | socket_tcp.check.server = &socket_tcp; |
| 8914 | |
| 8915 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8916 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8917 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8918 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8919 | socket_tcp.agent.server = &socket_tcp; |
| 8920 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8921 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8922 | |
| 8923 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8924 | /* Init TCP server: unchanged parameters */ |
| 8925 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8926 | socket_ssl.next = NULL; |
| 8927 | socket_ssl.proxy = &socket_proxy; |
| 8928 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8929 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8930 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8931 | socket_ssl.priv_conns = NULL; |
| 8932 | socket_ssl.idle_conns = NULL; |
| 8933 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8934 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8935 | socket_ssl.last_change = 0; |
| 8936 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8937 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8938 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8939 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8940 | |
| 8941 | /* XXX: Copy default parameter from default server, |
| 8942 | * but the default server is not initialized. |
| 8943 | */ |
| 8944 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8945 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8946 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8947 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8948 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8949 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8950 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8951 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8952 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8953 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8954 | |
| 8955 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8956 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8957 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8958 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8959 | socket_ssl.check.server = &socket_ssl; |
| 8960 | |
| 8961 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8962 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8963 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8964 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8965 | socket_ssl.agent.server = &socket_ssl; |
| 8966 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8967 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8968 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8969 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8970 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8971 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8972 | /* |
| 8973 | * |
| 8974 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8975 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8976 | * features like client certificates and ssl_verify. |
| 8977 | * |
| 8978 | */ |
| 8979 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8980 | if (tmp_error != 0) { |
| 8981 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8982 | abort(); /* This must be never arrives because the command line |
| 8983 | not editable by the user. */ |
| 8984 | } |
| 8985 | idx += kw->skip; |
| 8986 | } |
| 8987 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8988 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8989 | |
| 8990 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8991 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8992 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8993 | static void hlua_register_build_options(void) |
| 8994 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8995 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8996 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8997 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8998 | hap_register_build_opts(ptr, 1); |
| 8999 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9000 | |
| 9001 | INITCALL0(STG_REGISTER, hlua_register_build_options); |