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); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 433 | break; |
| 434 | |
| 435 | case LUA_TUSERDATA: |
| 436 | case LUA_TNIL: |
| 437 | case LUA_TTABLE: |
| 438 | case LUA_TFUNCTION: |
| 439 | case LUA_TTHREAD: |
| 440 | case LUA_TLIGHTUSERDATA: |
| 441 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 442 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 443 | break; |
| 444 | } |
| 445 | return 1; |
| 446 | } |
| 447 | |
| 448 | /* the following functions are used to convert a struct sample |
| 449 | * in Lua type. This useful to convert the return of the |
| 450 | * fetchs or converters. |
| 451 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 452 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 453 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 454 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 455 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 456 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 457 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 458 | break; |
| 459 | |
| 460 | case SMP_T_BIN: |
| 461 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 462 | 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] | 463 | break; |
| 464 | |
| 465 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 466 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 467 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 468 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 469 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 470 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 471 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 472 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 473 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 474 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 475 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 476 | 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] | 477 | break; |
| 478 | default: |
| 479 | lua_pushnil(L); |
| 480 | break; |
| 481 | } |
| 482 | break; |
| 483 | |
| 484 | case SMP_T_IPV4: |
| 485 | case SMP_T_IPV6: |
| 486 | 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] | 487 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 488 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 489 | 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] | 490 | else |
| 491 | lua_pushnil(L); |
| 492 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 493 | default: |
| 494 | lua_pushnil(L); |
| 495 | break; |
| 496 | } |
| 497 | return 1; |
| 498 | } |
| 499 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 500 | /* the following functions are used to convert a struct sample |
| 501 | * in Lua strings. This is useful to convert the return of the |
| 502 | * fetchs or converters. |
| 503 | */ |
| 504 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 505 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 506 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 507 | |
| 508 | case SMP_T_BIN: |
| 509 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 510 | 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] | 511 | break; |
| 512 | |
| 513 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 514 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 515 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 516 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 517 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 518 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 519 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 520 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 521 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 522 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 523 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 524 | 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] | 525 | break; |
| 526 | default: |
| 527 | lua_pushstring(L, ""); |
| 528 | break; |
| 529 | } |
| 530 | break; |
| 531 | |
| 532 | case SMP_T_SINT: |
| 533 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 534 | case SMP_T_IPV4: |
| 535 | case SMP_T_IPV6: |
| 536 | 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] | 537 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 538 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 539 | 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] | 540 | else |
| 541 | lua_pushstring(L, ""); |
| 542 | break; |
| 543 | default: |
| 544 | lua_pushstring(L, ""); |
| 545 | break; |
| 546 | } |
| 547 | return 1; |
| 548 | } |
| 549 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 550 | /* the following functions are used to convert an Lua type in a |
| 551 | * struct sample. This is useful to provide data from a converter |
| 552 | * to the LUA code. |
| 553 | */ |
| 554 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 555 | { |
| 556 | switch (lua_type(L, ud)) { |
| 557 | |
| 558 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 559 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 560 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 561 | break; |
| 562 | |
| 563 | |
| 564 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 565 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 566 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 567 | break; |
| 568 | |
| 569 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 570 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 571 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 572 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 573 | break; |
| 574 | |
| 575 | case LUA_TUSERDATA: |
| 576 | case LUA_TNIL: |
| 577 | case LUA_TTABLE: |
| 578 | case LUA_TFUNCTION: |
| 579 | case LUA_TTHREAD: |
| 580 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 581 | case LUA_TNONE: |
| 582 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 583 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 584 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 585 | break; |
| 586 | } |
| 587 | return 1; |
| 588 | } |
| 589 | |
| 590 | /* This function check the "argp" builded by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 591 | * 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] | 592 | * 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] | 593 | * |
| 594 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 595 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 596 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 597 | __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] | 598 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 599 | { |
| 600 | int min_arg; |
| 601 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 602 | struct proxy *px; |
| 603 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 604 | |
| 605 | idx = 0; |
| 606 | min_arg = ARGM(mask); |
| 607 | mask >>= ARGM_BITS; |
| 608 | |
| 609 | while (1) { |
| 610 | |
| 611 | /* Check oversize. */ |
| 612 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 613 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | /* Check for mandatory arguments. */ |
| 617 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 618 | if (idx < min_arg) { |
| 619 | |
| 620 | /* If miss other argument than the first one, we return an error. */ |
| 621 | if (idx > 0) |
| 622 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 623 | |
| 624 | /* If first argument have a certain type, some default values |
| 625 | * may be used. See the function smp_resolve_args(). |
| 626 | */ |
| 627 | switch (mask & ARGT_MASK) { |
| 628 | |
| 629 | case ARGT_FE: |
| 630 | if (!(p->cap & PR_CAP_FE)) |
| 631 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 632 | argp[idx].data.prx = p; |
| 633 | argp[idx].type = ARGT_FE; |
| 634 | argp[idx+1].type = ARGT_STOP; |
| 635 | break; |
| 636 | |
| 637 | case ARGT_BE: |
| 638 | if (!(p->cap & PR_CAP_BE)) |
| 639 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 640 | argp[idx].data.prx = p; |
| 641 | argp[idx].type = ARGT_BE; |
| 642 | argp[idx+1].type = ARGT_STOP; |
| 643 | break; |
| 644 | |
| 645 | case ARGT_TAB: |
| 646 | argp[idx].data.prx = p; |
| 647 | argp[idx].type = ARGT_TAB; |
| 648 | argp[idx+1].type = ARGT_STOP; |
| 649 | break; |
| 650 | |
| 651 | default: |
| 652 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 653 | break; |
| 654 | } |
| 655 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 656 | return 0; |
| 657 | } |
| 658 | |
| 659 | /* Check for exceed the number of requiered argument. */ |
| 660 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 661 | argp[idx].type != ARGT_STOP) { |
| 662 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 663 | } |
| 664 | |
| 665 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 666 | argp[idx].type == ARGT_STOP) { |
| 667 | return 0; |
| 668 | } |
| 669 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 670 | /* Convert some argument types. */ |
| 671 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 672 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 673 | if (argp[idx].type != ARGT_SINT) |
| 674 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 675 | argp[idx].type = ARGT_SINT; |
| 676 | break; |
| 677 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 678 | case ARGT_TIME: |
| 679 | if (argp[idx].type != ARGT_SINT) |
| 680 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 681 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 682 | break; |
| 683 | |
| 684 | case ARGT_SIZE: |
| 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_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 688 | break; |
| 689 | |
| 690 | case ARGT_FE: |
| 691 | if (argp[idx].type != ARGT_STR) |
| 692 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 693 | memcpy(trash.area, argp[idx].data.str.area, |
| 694 | argp[idx].data.str.data); |
| 695 | trash.area[argp[idx].data.str.data] = 0; |
| 696 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 697 | if (!argp[idx].data.prx) |
| 698 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 699 | argp[idx].type = ARGT_FE; |
| 700 | break; |
| 701 | |
| 702 | case ARGT_BE: |
| 703 | if (argp[idx].type != ARGT_STR) |
| 704 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 705 | memcpy(trash.area, argp[idx].data.str.area, |
| 706 | argp[idx].data.str.data); |
| 707 | trash.area[argp[idx].data.str.data] = 0; |
| 708 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 709 | if (!argp[idx].data.prx) |
| 710 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 711 | argp[idx].type = ARGT_BE; |
| 712 | break; |
| 713 | |
| 714 | case ARGT_TAB: |
| 715 | if (argp[idx].type != ARGT_STR) |
| 716 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 717 | memcpy(trash.area, argp[idx].data.str.area, |
| 718 | argp[idx].data.str.data); |
| 719 | trash.area[argp[idx].data.str.data] = 0; |
| 720 | argp[idx].data.prx = proxy_tbl_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 721 | if (!argp[idx].data.prx) |
| 722 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 723 | argp[idx].type = ARGT_TAB; |
| 724 | break; |
| 725 | |
| 726 | case ARGT_SRV: |
| 727 | if (argp[idx].type != ARGT_STR) |
| 728 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 729 | memcpy(trash.area, argp[idx].data.str.area, |
| 730 | argp[idx].data.str.data); |
| 731 | trash.area[argp[idx].data.str.data] = 0; |
| 732 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 733 | if (sname) { |
| 734 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 735 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 736 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 737 | if (!px) |
| 738 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 739 | } |
| 740 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 741 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 742 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 743 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 744 | argp[idx].data.srv = findserver(px, sname); |
| 745 | if (!argp[idx].data.srv) |
| 746 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 747 | argp[idx].type = ARGT_SRV; |
| 748 | break; |
| 749 | |
| 750 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 751 | memcpy(trash.area, argp[idx].data.str.area, |
| 752 | argp[idx].data.str.data); |
| 753 | trash.area[argp[idx].data.str.data] = 0; |
| 754 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 755 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 756 | argp[idx].type = ARGT_IPV4; |
| 757 | break; |
| 758 | |
| 759 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 760 | memcpy(trash.area, argp[idx].data.str.area, |
| 761 | argp[idx].data.str.data); |
| 762 | trash.area[argp[idx].data.str.data] = 0; |
| 763 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 764 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 765 | argp[idx].type = ARGT_MSK4; |
| 766 | break; |
| 767 | |
| 768 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 769 | memcpy(trash.area, argp[idx].data.str.area, |
| 770 | argp[idx].data.str.data); |
| 771 | trash.area[argp[idx].data.str.data] = 0; |
| 772 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 773 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 774 | argp[idx].type = ARGT_IPV6; |
| 775 | break; |
| 776 | |
| 777 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 778 | memcpy(trash.area, argp[idx].data.str.area, |
| 779 | argp[idx].data.str.data); |
| 780 | trash.area[argp[idx].data.str.data] = 0; |
| 781 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 782 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 783 | argp[idx].type = ARGT_MSK6; |
| 784 | break; |
| 785 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 786 | case ARGT_MAP: |
| 787 | case ARGT_REG: |
| 788 | case ARGT_USR: |
| 789 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 790 | break; |
| 791 | } |
| 792 | |
| 793 | /* Check for type of argument. */ |
| 794 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 795 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 796 | arg_type_names[(mask & ARGT_MASK)], |
| 797 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 798 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 799 | } |
| 800 | |
| 801 | /* Next argument. */ |
| 802 | mask >>= ARGT_BITS; |
| 803 | idx++; |
| 804 | } |
| 805 | } |
| 806 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 807 | /* |
| 808 | * The following functions are used to make correspondance between the the |
| 809 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 810 | * |
| 811 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 812 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 813 | */ |
| 814 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 815 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 816 | struct hlua **hlua = lua_getextraspace(L); |
| 817 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 818 | } |
| 819 | static inline void hlua_sethlua(struct hlua *hlua) |
| 820 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 821 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 822 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 823 | } |
| 824 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 825 | /* This function is used to send logs. It try to send on screen (stderr) |
| 826 | * and on the default syslog server. |
| 827 | */ |
| 828 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 829 | { |
| 830 | struct tm tm; |
| 831 | char *p; |
| 832 | |
| 833 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 834 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 835 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 836 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 837 | /* Break the message if exceed the buffer size. */ |
| 838 | *(p-4) = ' '; |
| 839 | *(p-3) = '.'; |
| 840 | *(p-2) = '.'; |
| 841 | *(p-1) = '.'; |
| 842 | break; |
| 843 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 844 | if (isprint(*msg)) |
| 845 | *p = *msg; |
| 846 | else |
| 847 | *p = '.'; |
| 848 | } |
| 849 | *p = '\0'; |
| 850 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 851 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 852 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 853 | get_localtime(date.tv_sec, &tm); |
| 854 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 855 | 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] | 856 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 857 | fflush(stderr); |
| 858 | } |
| 859 | } |
| 860 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 861 | /* This function just ensure that the yield will be always |
| 862 | * returned with a timeout and permit to set some flags |
| 863 | */ |
| 864 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 865 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 866 | { |
| 867 | struct hlua *hlua = hlua_gethlua(L); |
| 868 | |
| 869 | /* Set the wake timeout. If timeout is required, we set |
| 870 | * the expiration time. |
| 871 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 872 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 873 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 874 | hlua->flags |= flags; |
| 875 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 876 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 877 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 878 | } |
| 879 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 880 | /* This function initialises the Lua environment stored in the stream. |
| 881 | * 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] | 882 | * 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] | 883 | * |
| 884 | * This function is particular. it initialises a new Lua thread. If the |
| 885 | * initialisation fails (example: out of memory error), the lua function |
| 886 | * throws an error (longjmp). |
| 887 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 888 | * In some case (at least one), this function can be called from safe |
| 889 | * environement, so we must not initialise it. While the support of |
| 890 | * threads appear, the safe environment set a lock to ensure only one |
| 891 | * Lua execution at a time. If we initialize safe environment in another |
| 892 | * safe environmenet, we have a dead lock. |
| 893 | * |
| 894 | * set "already_safe" true if the context is initialized form safe |
| 895 | * Lua fonction. |
| 896 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 897 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 898 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 899 | * MUST NOT manipulate the created thread stack state, because it is not |
| 900 | * proctected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 901 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 902 | 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] | 903 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 904 | if (!already_safe) { |
| 905 | if (!SET_SAFE_LJMP(gL.T)) { |
| 906 | lua->Tref = LUA_REFNIL; |
| 907 | return 0; |
| 908 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 909 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 910 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 911 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 912 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 913 | lua->T = lua_newthread(gL.T); |
| 914 | if (!lua->T) { |
| 915 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 916 | if (!already_safe) |
| 917 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 918 | return 0; |
| 919 | } |
| 920 | hlua_sethlua(lua); |
| 921 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 922 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 923 | if (!already_safe) |
| 924 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 925 | return 1; |
| 926 | } |
| 927 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 928 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 929 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 930 | * is not freed. |
| 931 | */ |
| 932 | void hlua_ctx_destroy(struct hlua *lua) |
| 933 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 934 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 935 | return; |
| 936 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 937 | if (!lua->T) |
| 938 | goto end; |
| 939 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 940 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 941 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 942 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 943 | if (!SET_SAFE_LJMP(lua->T)) |
| 944 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 945 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 946 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 947 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 948 | if (!SET_SAFE_LJMP(gL.T)) |
| 949 | return; |
| 950 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 951 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 952 | /* Forces a garbage collecting process. If the Lua program is finished |
| 953 | * without error, we run the GC on the thread pointer. Its freed all |
| 954 | * the unused memory. |
| 955 | * If the thread is finnish with an error or is currently yielded, |
| 956 | * it seems that the GC applied on the thread doesn't clean anything, |
| 957 | * so e run the GC on the main thread. |
| 958 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 959 | * the garbage collection. |
| 960 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 961 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 962 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 963 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 964 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 965 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 966 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 967 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 968 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 969 | |
| 970 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 971 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 972 | } |
| 973 | |
| 974 | /* This function is used to restore the Lua context when a coroutine |
| 975 | * fails. This function copy the common memory between old coroutine |
| 976 | * and the new coroutine. The old coroutine is destroyed, and its |
| 977 | * replaced by the new coroutine. |
| 978 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 979 | * as string error message and it is copied in the new stack. |
| 980 | */ |
| 981 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 982 | { |
| 983 | lua_State *T; |
| 984 | int new_ref; |
| 985 | |
| 986 | /* Renew the main LUA stack doesn't have sense. */ |
| 987 | if (lua == &gL) |
| 988 | return 0; |
| 989 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 990 | /* New Lua coroutine. */ |
| 991 | T = lua_newthread(gL.T); |
| 992 | if (!T) |
| 993 | return 0; |
| 994 | |
| 995 | /* Copy last error message. */ |
| 996 | if (keep_msg) |
| 997 | lua_xmove(lua->T, T, 1); |
| 998 | |
| 999 | /* Copy data between the coroutines. */ |
| 1000 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1001 | lua_xmove(lua->T, T, 1); |
| 1002 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 1003 | |
| 1004 | /* Destroy old data. */ |
| 1005 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1006 | |
| 1007 | /* The thread is garbage collected by Lua. */ |
| 1008 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1009 | |
| 1010 | /* Fill the struct with the new coroutine values. */ |
| 1011 | lua->Mref = new_ref; |
| 1012 | lua->T = T; |
| 1013 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1014 | |
| 1015 | /* Set context. */ |
| 1016 | hlua_sethlua(lua); |
| 1017 | |
| 1018 | return 1; |
| 1019 | } |
| 1020 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1021 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1022 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1023 | struct hlua *hlua = hlua_gethlua(L); |
| 1024 | |
| 1025 | /* Lua cannot yield when its returning from a function, |
| 1026 | * so, we can fix the interrupt hook to 1 instruction, |
| 1027 | * expecting that the function is finnished. |
| 1028 | */ |
| 1029 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1030 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1031 | return; |
| 1032 | } |
| 1033 | |
| 1034 | /* restore the interrupt condition. */ |
| 1035 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1036 | |
| 1037 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1038 | * If the state is not yieldable, trying yield causes an error. |
| 1039 | */ |
| 1040 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1041 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1042 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1043 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1044 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1045 | hlua->run_time += now_ms - hlua->start_time; |
| 1046 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1047 | lua_pushfstring(L, "execution timeout"); |
| 1048 | WILL_LJMP(lua_error(L)); |
| 1049 | } |
| 1050 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1051 | /* Update the start time. */ |
| 1052 | hlua->start_time = now_ms; |
| 1053 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1054 | /* Try to interrupt the process at the end of the current |
| 1055 | * unyieldable function. |
| 1056 | */ |
| 1057 | 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] | 1058 | } |
| 1059 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1060 | /* This function start or resumes the Lua stack execution. If the flag |
| 1061 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1062 | * The function return an error. |
| 1063 | * |
| 1064 | * The function can returns 4 values: |
| 1065 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1066 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1067 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1068 | * - 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] | 1069 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1070 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1071 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1072 | * 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] | 1073 | * LUA code. |
| 1074 | */ |
| 1075 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1076 | { |
| 1077 | int ret; |
| 1078 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1079 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1080 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1081 | /* Initialise run time counter. */ |
| 1082 | if (!HLUA_IS_RUNNING(lua)) |
| 1083 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1084 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1085 | /* Lock the whole Lua execution. This lock must be before the |
| 1086 | * label "resume_execution". |
| 1087 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1088 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1089 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1090 | resume_execution: |
| 1091 | |
| 1092 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1093 | * instructions. it is used for preventing infinite loops. |
| 1094 | */ |
| 1095 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1096 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1097 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1098 | HLUA_SET_RUN(lua); |
| 1099 | HLUA_CLR_CTRLYIELD(lua); |
| 1100 | HLUA_CLR_WAKERESWR(lua); |
| 1101 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1102 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1103 | /* Update the start time. */ |
| 1104 | lua->start_time = now_ms; |
| 1105 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1106 | /* Call the function. */ |
| 1107 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1108 | switch (ret) { |
| 1109 | |
| 1110 | case LUA_OK: |
| 1111 | ret = HLUA_E_OK; |
| 1112 | break; |
| 1113 | |
| 1114 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1115 | /* Check if the execution timeout is expired. It it is the case, we |
| 1116 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1117 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1118 | tv_update_date(0, 1); |
| 1119 | lua->run_time += now_ms - lua->start_time; |
| 1120 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1121 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1122 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1123 | break; |
| 1124 | } |
| 1125 | /* Process the forced yield. if the general yield is not allowed or |
| 1126 | * if no task were associated this the current Lua execution |
| 1127 | * coroutine, we resume the execution. Else we want to return in the |
| 1128 | * scheduler and we want to be waked up again, to continue the |
| 1129 | * current Lua execution. So we schedule our own task. |
| 1130 | */ |
| 1131 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1132 | if (!yield_allowed || !lua->task) |
| 1133 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1134 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1135 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1136 | if (!yield_allowed) { |
| 1137 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1138 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1139 | break; |
| 1140 | } |
| 1141 | ret = HLUA_E_AGAIN; |
| 1142 | break; |
| 1143 | |
| 1144 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1145 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1146 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1147 | * because the errors ares the only one mean to return immediately |
| 1148 | * from and lua execution. |
| 1149 | */ |
| 1150 | if (lua->flags & HLUA_EXIT) { |
| 1151 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1152 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1153 | break; |
| 1154 | } |
| 1155 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1156 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1157 | if (!lua_checkstack(lua->T, 1)) { |
| 1158 | ret = HLUA_E_ERR; |
| 1159 | break; |
| 1160 | } |
| 1161 | msg = lua_tostring(lua->T, -1); |
| 1162 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1163 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1164 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1165 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1166 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1167 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1168 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1169 | ret = HLUA_E_ERRMSG; |
| 1170 | break; |
| 1171 | |
| 1172 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1173 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1174 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1175 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1176 | break; |
| 1177 | |
| 1178 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1179 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1180 | if (!lua_checkstack(lua->T, 1)) { |
| 1181 | ret = HLUA_E_ERR; |
| 1182 | break; |
| 1183 | } |
| 1184 | msg = lua_tostring(lua->T, -1); |
| 1185 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1186 | lua_pop(lua->T, 1); |
| 1187 | if (msg) |
| 1188 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1189 | else |
| 1190 | lua_pushfstring(lua->T, "message handler error"); |
| 1191 | ret = HLUA_E_ERRMSG; |
| 1192 | break; |
| 1193 | |
| 1194 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1195 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1196 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1197 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1198 | break; |
| 1199 | } |
| 1200 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1201 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1202 | if (lua->flags & HLUA_MUST_GC && |
| 1203 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1204 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1205 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1206 | switch (ret) { |
| 1207 | case HLUA_E_AGAIN: |
| 1208 | break; |
| 1209 | |
| 1210 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1211 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1212 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1213 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1214 | break; |
| 1215 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1216 | case HLUA_E_ETMOUT: |
| 1217 | case HLUA_E_NOMEM: |
| 1218 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1219 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1220 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1221 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1222 | hlua_ctx_renew(lua, 0); |
| 1223 | break; |
| 1224 | |
| 1225 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1226 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1227 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1228 | break; |
| 1229 | } |
| 1230 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1231 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1232 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1233 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1234 | return ret; |
| 1235 | } |
| 1236 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1237 | /* This function exit the current code. */ |
| 1238 | __LJMP static int hlua_done(lua_State *L) |
| 1239 | { |
| 1240 | struct hlua *hlua = hlua_gethlua(L); |
| 1241 | |
| 1242 | hlua->flags |= HLUA_EXIT; |
| 1243 | WILL_LJMP(lua_error(L)); |
| 1244 | |
| 1245 | return 0; |
| 1246 | } |
| 1247 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1248 | /* This function is an LUA binding. It provides a function |
| 1249 | * for deleting ACL from a referenced ACL file. |
| 1250 | */ |
| 1251 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1252 | { |
| 1253 | const char *name; |
| 1254 | const char *key; |
| 1255 | struct pat_ref *ref; |
| 1256 | |
| 1257 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1258 | |
| 1259 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1260 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1261 | |
| 1262 | ref = pat_ref_lookup(name); |
| 1263 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1264 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1265 | |
| 1266 | pat_ref_delete(ref, key); |
| 1267 | return 0; |
| 1268 | } |
| 1269 | |
| 1270 | /* This function is an LUA binding. It provides a function |
| 1271 | * for deleting map entry from a referenced map file. |
| 1272 | */ |
| 1273 | static int hlua_del_map(lua_State *L) |
| 1274 | { |
| 1275 | const char *name; |
| 1276 | const char *key; |
| 1277 | struct pat_ref *ref; |
| 1278 | |
| 1279 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1280 | |
| 1281 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1282 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1283 | |
| 1284 | ref = pat_ref_lookup(name); |
| 1285 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1286 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1287 | |
| 1288 | pat_ref_delete(ref, key); |
| 1289 | return 0; |
| 1290 | } |
| 1291 | |
| 1292 | /* This function is an LUA binding. It provides a function |
| 1293 | * for adding ACL pattern from a referenced ACL file. |
| 1294 | */ |
| 1295 | static int hlua_add_acl(lua_State *L) |
| 1296 | { |
| 1297 | const char *name; |
| 1298 | const char *key; |
| 1299 | struct pat_ref *ref; |
| 1300 | |
| 1301 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1302 | |
| 1303 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1304 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1305 | |
| 1306 | ref = pat_ref_lookup(name); |
| 1307 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1308 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1309 | |
| 1310 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1311 | pat_ref_add(ref, key, NULL, NULL); |
| 1312 | return 0; |
| 1313 | } |
| 1314 | |
| 1315 | /* This function is an LUA binding. It provides a function |
| 1316 | * for setting map pattern and sample from a referenced map |
| 1317 | * file. |
| 1318 | */ |
| 1319 | static int hlua_set_map(lua_State *L) |
| 1320 | { |
| 1321 | const char *name; |
| 1322 | const char *key; |
| 1323 | const char *value; |
| 1324 | struct pat_ref *ref; |
| 1325 | |
| 1326 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1327 | |
| 1328 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1329 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1330 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1331 | |
| 1332 | ref = pat_ref_lookup(name); |
| 1333 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1334 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1335 | |
| 1336 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1337 | pat_ref_set(ref, key, value, NULL); |
| 1338 | else |
| 1339 | pat_ref_add(ref, key, value, NULL); |
| 1340 | return 0; |
| 1341 | } |
| 1342 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1343 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1344 | * an integer or user data. This data is associated with a metatable. This |
| 1345 | * metatable have an original version registred in the global context with |
| 1346 | * the name of the object (_G[<name>] = <metable> ). |
| 1347 | * |
| 1348 | * A metable is a table that modify the standard behavior of a standard |
| 1349 | * access to the associated data. The entries of this new metatable are |
| 1350 | * defined as is: |
| 1351 | * |
| 1352 | * http://lua-users.org/wiki/MetatableEvents |
| 1353 | * |
| 1354 | * __index |
| 1355 | * |
| 1356 | * we access an absent field in a table, the result is nil. This is |
| 1357 | * true, but it is not the whole truth. Actually, such access triggers |
| 1358 | * the interpreter to look for an __index metamethod: If there is no |
| 1359 | * such method, as usually happens, then the access results in nil; |
| 1360 | * otherwise, the metamethod will provide the result. |
| 1361 | * |
| 1362 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1363 | * the key does not appear in the table, but the metatable has an __index |
| 1364 | * property: |
| 1365 | * |
| 1366 | * - if the value is a function, the function is called, passing in the |
| 1367 | * table and the key; the return value of that function is returned as |
| 1368 | * the result. |
| 1369 | * |
| 1370 | * - if the value is another table, the value of the key in that table is |
| 1371 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1372 | * table's metatable has an __index property, then it continues on up) |
| 1373 | * |
| 1374 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1375 | * |
| 1376 | * http://www.lua.org/pil/13.4.1.html |
| 1377 | * |
| 1378 | * __newindex |
| 1379 | * |
| 1380 | * Like __index, but control property assignment. |
| 1381 | * |
| 1382 | * __mode - Control weak references. A string value with one or both |
| 1383 | * of the characters 'k' and 'v' which specifies that the the |
| 1384 | * keys and/or values in the table are weak references. |
| 1385 | * |
| 1386 | * __call - Treat a table like a function. When a table is followed by |
| 1387 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1388 | * a __call key pointing to a function, that function is invoked |
| 1389 | * (passing any specified arguments) and the return value is |
| 1390 | * returned. |
| 1391 | * |
| 1392 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1393 | * called, if the metatable for myTable has a __metatable |
| 1394 | * key, the value of that key is returned instead of the |
| 1395 | * actual metatable. |
| 1396 | * |
| 1397 | * __tostring - Control string representation. When the builtin |
| 1398 | * "tostring( myTable )" function is called, if the metatable |
| 1399 | * for myTable has a __tostring property set to a function, |
| 1400 | * that function is invoked (passing myTable to it) and the |
| 1401 | * return value is used as the string representation. |
| 1402 | * |
| 1403 | * __len - Control table length. When the table length is requested using |
| 1404 | * the length operator ( '#' ), if the metatable for myTable has |
| 1405 | * a __len key pointing to a function, that function is invoked |
| 1406 | * (passing myTable to it) and the return value used as the value |
| 1407 | * of "#myTable". |
| 1408 | * |
| 1409 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1410 | * collected, if the metatable has a __gc field pointing to a |
| 1411 | * function, that function is first invoked, passing the userdata |
| 1412 | * to it. The __gc metamethod is not called for tables. |
| 1413 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1414 | * |
| 1415 | * Special metamethods for redefining standard operators: |
| 1416 | * http://www.lua.org/pil/13.1.html |
| 1417 | * |
| 1418 | * __add "+" |
| 1419 | * __sub "-" |
| 1420 | * __mul "*" |
| 1421 | * __div "/" |
| 1422 | * __unm "!" |
| 1423 | * __pow "^" |
| 1424 | * __concat ".." |
| 1425 | * |
| 1426 | * Special methods for redfining standar relations |
| 1427 | * http://www.lua.org/pil/13.2.html |
| 1428 | * |
| 1429 | * __eq "==" |
| 1430 | * __lt "<" |
| 1431 | * __le "<=" |
| 1432 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1433 | |
| 1434 | /* |
| 1435 | * |
| 1436 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1437 | * Class Map |
| 1438 | * |
| 1439 | * |
| 1440 | */ |
| 1441 | |
| 1442 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1443 | * a class session, otherwise it throws an error. |
| 1444 | */ |
| 1445 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1446 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1447 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | /* This function is the map constructor. It don't need |
| 1451 | * the class Map object. It creates and return a new Map |
| 1452 | * object. It must be called only during "body" or "init" |
| 1453 | * context because it process some filesystem accesses. |
| 1454 | */ |
| 1455 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1456 | { |
| 1457 | const char *fn; |
| 1458 | int match = PAT_MATCH_STR; |
| 1459 | struct sample_conv conv; |
| 1460 | const char *file = ""; |
| 1461 | int line = 0; |
| 1462 | lua_Debug ar; |
| 1463 | char *err = NULL; |
| 1464 | struct arg args[2]; |
| 1465 | |
| 1466 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1467 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1468 | |
| 1469 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1470 | |
| 1471 | if (lua_gettop(L) >= 2) { |
| 1472 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1473 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1474 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1475 | } |
| 1476 | |
| 1477 | /* Get Lua filename and line number. */ |
| 1478 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1479 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1480 | if (ar.currentline > 0) { /* is there info? */ |
| 1481 | file = ar.short_src; |
| 1482 | line = ar.currentline; |
| 1483 | } |
| 1484 | } |
| 1485 | |
| 1486 | /* fill fake sample_conv struct. */ |
| 1487 | conv.kw = ""; /* unused. */ |
| 1488 | conv.process = NULL; /* unused. */ |
| 1489 | conv.arg_mask = 0; /* unused. */ |
| 1490 | conv.val_args = NULL; /* unused. */ |
| 1491 | conv.out_type = SMP_T_STR; |
| 1492 | conv.private = (void *)(long)match; |
| 1493 | switch (match) { |
| 1494 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1495 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1496 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1497 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1498 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1499 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1500 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1501 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1502 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1503 | default: |
| 1504 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1505 | } |
| 1506 | |
| 1507 | /* fill fake args. */ |
| 1508 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1509 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1510 | args[1].type = ARGT_STOP; |
| 1511 | |
| 1512 | /* load the map. */ |
| 1513 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1514 | /* error case: we cant use luaL_error because we must |
| 1515 | * free the err variable. |
| 1516 | */ |
| 1517 | luaL_where(L, 1); |
| 1518 | lua_pushfstring(L, "'new': %s.", err); |
| 1519 | lua_concat(L, 2); |
| 1520 | free(err); |
| 1521 | WILL_LJMP(lua_error(L)); |
| 1522 | } |
| 1523 | |
| 1524 | /* create the lua object. */ |
| 1525 | lua_newtable(L); |
| 1526 | lua_pushlightuserdata(L, args[0].data.map); |
| 1527 | lua_rawseti(L, -2, 0); |
| 1528 | |
| 1529 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1530 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1531 | lua_setmetatable(L, -2); |
| 1532 | |
| 1533 | |
| 1534 | return 1; |
| 1535 | } |
| 1536 | |
| 1537 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1538 | { |
| 1539 | struct map_descriptor *desc; |
| 1540 | struct pattern *pat; |
| 1541 | struct sample smp; |
| 1542 | |
| 1543 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1544 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1545 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1546 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1547 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1548 | } |
| 1549 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1550 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1551 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1552 | 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] | 1553 | } |
| 1554 | |
| 1555 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1556 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1557 | if (str) |
| 1558 | lua_pushstring(L, ""); |
| 1559 | else |
| 1560 | lua_pushnil(L); |
| 1561 | return 1; |
| 1562 | } |
| 1563 | |
| 1564 | /* 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] | 1565 | 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] | 1566 | return 1; |
| 1567 | } |
| 1568 | |
| 1569 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1570 | { |
| 1571 | return _hlua_map_lookup(L, 0); |
| 1572 | } |
| 1573 | |
| 1574 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1575 | { |
| 1576 | return _hlua_map_lookup(L, 1); |
| 1577 | } |
| 1578 | |
| 1579 | /* |
| 1580 | * |
| 1581 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1582 | * Class Socket |
| 1583 | * |
| 1584 | * |
| 1585 | */ |
| 1586 | |
| 1587 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1588 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1589 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | /* 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] | 1593 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1594 | * received. |
| 1595 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1596 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1597 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1598 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1599 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1600 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1601 | if (appctx->ctx.hlua_cosocket.die) { |
| 1602 | si_shutw(si); |
| 1603 | si_shutr(si); |
| 1604 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1605 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1606 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1607 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1608 | } |
| 1609 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1610 | /* If the connection object is not available, close all the |
| 1611 | * streams and wakeup everything waiting for. |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1612 | */ |
| 1613 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1614 | si_shutw(si); |
| 1615 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1616 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1617 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1618 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1619 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1620 | } |
| 1621 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1622 | /* If we cant write, wakeup the pending write signals. */ |
| 1623 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1624 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1625 | |
| 1626 | /* If we cant read, wakeup the pending read signals. */ |
| 1627 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1628 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1629 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1630 | /* if the connection is not estabkished, inform the stream that we want |
| 1631 | * to be notified whenever the connection completes. |
| 1632 | */ |
| 1633 | if (!(c->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1634 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1635 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1636 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1637 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1638 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1639 | |
| 1640 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1641 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1642 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1643 | /* 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] | 1644 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1645 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1646 | |
| 1647 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1648 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1649 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1650 | |
| 1651 | /* Some data were injected in the buffer, notify the stream |
| 1652 | * interface. |
| 1653 | */ |
| 1654 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1655 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1656 | |
| 1657 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1658 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1659 | */ |
| 1660 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1661 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1662 | } |
| 1663 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1664 | /* This function is called when the "struct stream" is destroyed. |
| 1665 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1666 | * Wake all the pending signals. |
| 1667 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1668 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1669 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1670 | struct xref *peer; |
| 1671 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1672 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1673 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1674 | if (peer) |
| 1675 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1676 | |
| 1677 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1678 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1679 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1683 | * uses this object. If the stream does not exists, just quit. |
| 1684 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1685 | * pending signal can rest in the read and write lists. destroy |
| 1686 | * it. |
| 1687 | */ |
| 1688 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1689 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1690 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1691 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1692 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1693 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1694 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1695 | |
| 1696 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1697 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1698 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1699 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1700 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1701 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1702 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1703 | appctx->ctx.hlua_cosocket.die = 1; |
| 1704 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1705 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1706 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1707 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1712 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1713 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1714 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1715 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1716 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1717 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1718 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1719 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1720 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1721 | |
| 1722 | /* Check if we run on the same thread than the xreator thread. |
| 1723 | * We cannot access to the socket if the thread is different. |
| 1724 | */ |
| 1725 | if (socket->tid != tid) |
| 1726 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1727 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1728 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1729 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1730 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1731 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1732 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1733 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1734 | appctx->ctx.hlua_cosocket.die = 1; |
| 1735 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1736 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1737 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1738 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1739 | return 0; |
| 1740 | } |
| 1741 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1742 | /* The close function calls close_helper. |
| 1743 | */ |
| 1744 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1745 | { |
| 1746 | MAY_LJMP(check_args(L, 1, "close")); |
| 1747 | return hlua_socket_close_helper(L); |
| 1748 | } |
| 1749 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1750 | /* This Lua function assumes that the stack contain three parameters. |
| 1751 | * 1 - USERDATA containing a struct socket |
| 1752 | * 2 - INTEGER with values of the macro defined below |
| 1753 | * If the integer is -1, we must read at most one line. |
| 1754 | * If the integer is -2, we ust read all the data until the |
| 1755 | * end of the stream. |
| 1756 | * If the integer is positive value, we must read a number of |
| 1757 | * bytes corresponding to this value. |
| 1758 | */ |
| 1759 | #define HLSR_READ_LINE (-1) |
| 1760 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1761 | __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] | 1762 | { |
| 1763 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1764 | int wanted = lua_tointeger(L, 2); |
| 1765 | struct hlua *hlua = hlua_gethlua(L); |
| 1766 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1767 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1768 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1769 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1770 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1771 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1772 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1773 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1774 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1775 | struct stream_interface *si; |
| 1776 | struct stream *s; |
| 1777 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1778 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1779 | |
| 1780 | /* Check if this lua stack is schedulable. */ |
| 1781 | if (!hlua || !hlua->task) |
| 1782 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1783 | "'frontend', 'backend' or 'task'")); |
| 1784 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1785 | /* Check if we run on the same thread than the xreator thread. |
| 1786 | * We cannot access to the socket if the thread is different. |
| 1787 | */ |
| 1788 | if (socket->tid != tid) |
| 1789 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1790 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1791 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1792 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1793 | if (!peer) |
| 1794 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1795 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1796 | si = appctx->owner; |
| 1797 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1798 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1799 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1800 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1801 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1802 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1803 | if (nblk < 0) /* Connection close. */ |
| 1804 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1805 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1806 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1807 | |
| 1808 | /* remove final \r\n. */ |
| 1809 | if (nblk == 1) { |
| 1810 | if (blk1[len1-1] == '\n') { |
| 1811 | len1--; |
| 1812 | skip_at_end++; |
| 1813 | if (blk1[len1-1] == '\r') { |
| 1814 | len1--; |
| 1815 | skip_at_end++; |
| 1816 | } |
| 1817 | } |
| 1818 | } |
| 1819 | else { |
| 1820 | if (blk2[len2-1] == '\n') { |
| 1821 | len2--; |
| 1822 | skip_at_end++; |
| 1823 | if (blk2[len2-1] == '\r') { |
| 1824 | len2--; |
| 1825 | skip_at_end++; |
| 1826 | } |
| 1827 | } |
| 1828 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1832 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1833 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1834 | if (nblk < 0) /* Connection close. */ |
| 1835 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1836 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1837 | goto connection_empty; |
| 1838 | } |
| 1839 | |
| 1840 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1841 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1842 | nblk = co_getblk_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; |
| 1847 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1848 | missing_bytes = wanted - socket->b.n; |
| 1849 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1850 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1851 | len1 = missing_bytes; |
| 1852 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1853 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | len = len1; |
| 1857 | |
| 1858 | luaL_addlstring(&socket->b, blk1, len1); |
| 1859 | if (nblk == 2) { |
| 1860 | len += len2; |
| 1861 | luaL_addlstring(&socket->b, blk2, len2); |
| 1862 | } |
| 1863 | |
| 1864 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1865 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1866 | |
| 1867 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1868 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1869 | |
| 1870 | /* If the pattern reclaim to read all the data |
| 1871 | * in the connection, got out. |
| 1872 | */ |
| 1873 | if (wanted == HLSR_READ_ALL) |
| 1874 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1875 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1876 | goto connection_empty; |
| 1877 | |
| 1878 | /* Return result. */ |
| 1879 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1880 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1881 | return 1; |
| 1882 | |
| 1883 | connection_closed: |
| 1884 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1885 | xref_unlock(&socket->xref, peer); |
| 1886 | |
| 1887 | no_peer: |
| 1888 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1889 | /* If the buffer containds data. */ |
| 1890 | if (socket->b.n > 0) { |
| 1891 | luaL_pushresult(&socket->b); |
| 1892 | return 1; |
| 1893 | } |
| 1894 | lua_pushnil(L); |
| 1895 | lua_pushstring(L, "connection closed."); |
| 1896 | return 2; |
| 1897 | |
| 1898 | connection_empty: |
| 1899 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1900 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1901 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1902 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1903 | } |
| 1904 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1905 | 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] | 1906 | return 0; |
| 1907 | } |
| 1908 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1909 | /* This Lua function gets two parameters. The first one can be string |
| 1910 | * or a number. If the string is "*l", the user requires one line. If |
| 1911 | * 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] | 1912 | * If the value is a number, the user require a number of bytes equal |
| 1913 | * to the value. The default value is "*l" (a line). |
| 1914 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1915 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1916 | * integer takes this values: |
| 1917 | * -1 : read a line |
| 1918 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1919 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1920 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1921 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1922 | * concatenated with the read data. |
| 1923 | */ |
| 1924 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1925 | { |
| 1926 | int wanted = HLSR_READ_LINE; |
| 1927 | const char *pattern; |
| 1928 | int type; |
| 1929 | char *error; |
| 1930 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1931 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1932 | |
| 1933 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1934 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1935 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1936 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1937 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1938 | /* Check if we run on the same thread than the xreator thread. |
| 1939 | * We cannot access to the socket if the thread is different. |
| 1940 | */ |
| 1941 | if (socket->tid != tid) |
| 1942 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1943 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1944 | /* check for pattern. */ |
| 1945 | if (lua_gettop(L) >= 2) { |
| 1946 | type = lua_type(L, 2); |
| 1947 | if (type == LUA_TSTRING) { |
| 1948 | pattern = lua_tostring(L, 2); |
| 1949 | if (strcmp(pattern, "*a") == 0) |
| 1950 | wanted = HLSR_READ_ALL; |
| 1951 | else if (strcmp(pattern, "*l") == 0) |
| 1952 | wanted = HLSR_READ_LINE; |
| 1953 | else { |
| 1954 | wanted = strtoll(pattern, &error, 10); |
| 1955 | if (*error != '\0') |
| 1956 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1957 | } |
| 1958 | } |
| 1959 | else if (type == LUA_TNUMBER) { |
| 1960 | wanted = lua_tointeger(L, 2); |
| 1961 | if (wanted < 0) |
| 1962 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1963 | } |
| 1964 | } |
| 1965 | |
| 1966 | /* Set pattern. */ |
| 1967 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1968 | |
| 1969 | /* Check if we would replace the top by itself. */ |
| 1970 | if (lua_gettop(L) != 2) |
| 1971 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1972 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1973 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | luaL_buffinit(L, &socket->b); |
| 1975 | |
| 1976 | /* Check prefix. */ |
| 1977 | if (lua_gettop(L) >= 3) { |
| 1978 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1979 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1980 | pattern = lua_tolstring(L, 3, &len); |
| 1981 | luaL_addlstring(&socket->b, pattern, len); |
| 1982 | } |
| 1983 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1984 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1988 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1989 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1990 | 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] | 1991 | { |
| 1992 | struct hlua_socket *socket; |
| 1993 | struct hlua *hlua = hlua_gethlua(L); |
| 1994 | struct appctx *appctx; |
| 1995 | size_t buf_len; |
| 1996 | const char *buf; |
| 1997 | int len; |
| 1998 | int send_len; |
| 1999 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2000 | struct xref *peer; |
| 2001 | struct stream_interface *si; |
| 2002 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2003 | |
| 2004 | /* Check if this lua stack is schedulable. */ |
| 2005 | if (!hlua || !hlua->task) |
| 2006 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2007 | "'frontend', 'backend' or 'task'")); |
| 2008 | |
| 2009 | /* Get object */ |
| 2010 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2011 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2012 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2013 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2014 | /* Check if we run on the same thread than the xreator thread. |
| 2015 | * We cannot access to the socket if the thread is different. |
| 2016 | */ |
| 2017 | if (socket->tid != tid) |
| 2018 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2019 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2020 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2021 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2022 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2023 | lua_pushinteger(L, -1); |
| 2024 | return 1; |
| 2025 | } |
| 2026 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2027 | si = appctx->owner; |
| 2028 | s = si_strm(si); |
| 2029 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2030 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2031 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2032 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2033 | lua_pushinteger(L, -1); |
| 2034 | return 1; |
| 2035 | } |
| 2036 | |
| 2037 | /* Update the input buffer data. */ |
| 2038 | buf += sent; |
| 2039 | send_len = buf_len - sent; |
| 2040 | |
| 2041 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2042 | if (sent >= buf_len) { |
| 2043 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2044 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2045 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2046 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2047 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2048 | * the request buffer if its not required. |
| 2049 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2050 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2051 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2052 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2053 | } |
| 2054 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2055 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2056 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2057 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2058 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2059 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2060 | |
| 2061 | /* send data */ |
| 2062 | if (len < send_len) |
| 2063 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2064 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2065 | |
| 2066 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2067 | * the data" (-2) are not expected because the available length |
| 2068 | * is tested. |
| 2069 | * Other unknown error are also not expected. |
| 2070 | */ |
| 2071 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2072 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2073 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2074 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2075 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2076 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2077 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2078 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2079 | return 1; |
| 2080 | } |
| 2081 | |
| 2082 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2083 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2084 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2085 | s->req.rex = TICK_ETERNITY; |
| 2086 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2087 | |
| 2088 | /* Update length sent. */ |
| 2089 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2090 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2091 | |
| 2092 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2093 | if (sent + len >= buf_len) { |
| 2094 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2095 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2096 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2097 | |
| 2098 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2099 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2100 | xref_unlock(&socket->xref, peer); |
| 2101 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2102 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2103 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2104 | 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] | 2105 | return 0; |
| 2106 | } |
| 2107 | |
| 2108 | /* This function initiate the send of data. It just check the input |
| 2109 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2110 | * 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] | 2111 | * "hlua_socket_write_yield" that can yield. |
| 2112 | * |
| 2113 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2114 | * the associated object. The second is a string buffer. The third is |
| 2115 | * a facultative integer that represents where is the buffer position |
| 2116 | * of the start of the data that can send. The first byte is the |
| 2117 | * position "1". The default value is "1". The fourth argument is a |
| 2118 | * facultative integer that represents where is the buffer position |
| 2119 | * of the end of the data that can send. The default is the last byte. |
| 2120 | */ |
| 2121 | static int hlua_socket_send(struct lua_State *L) |
| 2122 | { |
| 2123 | int i; |
| 2124 | int j; |
| 2125 | const char *buf; |
| 2126 | size_t buf_len; |
| 2127 | |
| 2128 | /* Check number of arguments. */ |
| 2129 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2130 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2131 | |
| 2132 | /* Get the string. */ |
| 2133 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2134 | |
| 2135 | /* Get and check j. */ |
| 2136 | if (lua_gettop(L) == 4) { |
| 2137 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2138 | if (j < 0) |
| 2139 | j = buf_len + j + 1; |
| 2140 | if (j > buf_len) |
| 2141 | j = buf_len + 1; |
| 2142 | lua_pop(L, 1); |
| 2143 | } |
| 2144 | else |
| 2145 | j = buf_len; |
| 2146 | |
| 2147 | /* Get and check i. */ |
| 2148 | if (lua_gettop(L) == 3) { |
| 2149 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2150 | if (i < 0) |
| 2151 | i = buf_len + i + 1; |
| 2152 | if (i > buf_len) |
| 2153 | i = buf_len + 1; |
| 2154 | lua_pop(L, 1); |
| 2155 | } else |
| 2156 | i = 1; |
| 2157 | |
| 2158 | /* Check bth i and j. */ |
| 2159 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2160 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2161 | return 1; |
| 2162 | } |
| 2163 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2164 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2165 | return 1; |
| 2166 | } |
| 2167 | if (i == 0) |
| 2168 | i = 1; |
| 2169 | if (j == 0) |
| 2170 | j = 1; |
| 2171 | |
| 2172 | /* Pop the string. */ |
| 2173 | lua_pop(L, 1); |
| 2174 | |
| 2175 | /* Update the buffer length. */ |
| 2176 | buf += i - 1; |
| 2177 | buf_len = j - i + 1; |
| 2178 | lua_pushlstring(L, buf, buf_len); |
| 2179 | |
| 2180 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2181 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2182 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2183 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2184 | } |
| 2185 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2186 | #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] | 2187 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2188 | { |
| 2189 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2190 | int ret; |
| 2191 | int len; |
| 2192 | char *p; |
| 2193 | |
| 2194 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2195 | if (ret <= 0) { |
| 2196 | lua_pushnil(L); |
| 2197 | return 1; |
| 2198 | } |
| 2199 | |
| 2200 | if (ret == AF_UNIX) { |
| 2201 | lua_pushstring(L, buffer+1); |
| 2202 | return 1; |
| 2203 | } |
| 2204 | else if (ret == AF_INET6) { |
| 2205 | buffer[0] = '['; |
| 2206 | len = strlen(buffer); |
| 2207 | buffer[len] = ']'; |
| 2208 | len++; |
| 2209 | buffer[len] = ':'; |
| 2210 | len++; |
| 2211 | p = buffer; |
| 2212 | } |
| 2213 | else if (ret == AF_INET) { |
| 2214 | p = buffer + 1; |
| 2215 | len = strlen(p); |
| 2216 | p[len] = ':'; |
| 2217 | len++; |
| 2218 | } |
| 2219 | else { |
| 2220 | lua_pushnil(L); |
| 2221 | return 1; |
| 2222 | } |
| 2223 | |
| 2224 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2225 | lua_pushnil(L); |
| 2226 | return 1; |
| 2227 | } |
| 2228 | |
| 2229 | lua_pushstring(L, p); |
| 2230 | return 1; |
| 2231 | } |
| 2232 | |
| 2233 | /* Returns information about the peer of the connection. */ |
| 2234 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2235 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2236 | struct hlua_socket *socket; |
| 2237 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2238 | struct xref *peer; |
| 2239 | struct appctx *appctx; |
| 2240 | struct stream_interface *si; |
| 2241 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2242 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2243 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2244 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2245 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2246 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2247 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2248 | /* Check if we run on the same thread than the xreator thread. |
| 2249 | * We cannot access to the socket if the thread is different. |
| 2250 | */ |
| 2251 | if (socket->tid != tid) |
| 2252 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2253 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2254 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2255 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2256 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2257 | lua_pushnil(L); |
| 2258 | return 1; |
| 2259 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2260 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2261 | si = appctx->owner; |
| 2262 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2263 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2264 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2265 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2266 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2267 | lua_pushnil(L); |
| 2268 | return 1; |
| 2269 | } |
| 2270 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2271 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2272 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2273 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2274 | lua_pushnil(L); |
| 2275 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2276 | } |
| 2277 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2278 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2279 | xref_unlock(&socket->xref, peer); |
| 2280 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2281 | } |
| 2282 | |
| 2283 | /* Returns information about my connection side. */ |
| 2284 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2285 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2286 | struct hlua_socket *socket; |
| 2287 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2288 | struct appctx *appctx; |
| 2289 | struct xref *peer; |
| 2290 | struct stream_interface *si; |
| 2291 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2292 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2293 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2294 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2295 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2296 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2298 | /* Check if we run on the same thread than the xreator thread. |
| 2299 | * We cannot access to the socket if the thread is different. |
| 2300 | */ |
| 2301 | if (socket->tid != tid) |
| 2302 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2303 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2304 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2305 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2306 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2307 | lua_pushnil(L); |
| 2308 | return 1; |
| 2309 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2310 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2311 | si = appctx->owner; |
| 2312 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2313 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2314 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2315 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2316 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2317 | lua_pushnil(L); |
| 2318 | return 1; |
| 2319 | } |
| 2320 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2321 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2322 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2323 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2324 | lua_pushnil(L); |
| 2325 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2326 | } |
| 2327 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2328 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2329 | xref_unlock(&socket->xref, peer); |
| 2330 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2334 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2335 | .obj_type = OBJ_TYPE_APPLET, |
| 2336 | .name = "<LUA_TCP>", |
| 2337 | .fct = hlua_socket_handler, |
| 2338 | .release = hlua_socket_release, |
| 2339 | }; |
| 2340 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2341 | __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] | 2342 | { |
| 2343 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2344 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2345 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2346 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2347 | struct stream_interface *si; |
| 2348 | struct stream *s; |
| 2349 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2350 | /* Check if we run on the same thread than the xreator thread. |
| 2351 | * We cannot access to the socket if the thread is different. |
| 2352 | */ |
| 2353 | if (socket->tid != tid) |
| 2354 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2355 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2356 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2357 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2358 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2359 | lua_pushnil(L); |
| 2360 | lua_pushstring(L, "Can't connect"); |
| 2361 | return 2; |
| 2362 | } |
| 2363 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2364 | si = appctx->owner; |
| 2365 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2366 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2367 | /* Check if we run on the same thread than the xreator thread. |
| 2368 | * We cannot access to the socket if the thread is different. |
| 2369 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2370 | if (socket->tid != tid) { |
| 2371 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2372 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2373 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2374 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2375 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2376 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2377 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2378 | lua_pushnil(L); |
| 2379 | lua_pushstring(L, "Can't connect"); |
| 2380 | return 2; |
| 2381 | } |
| 2382 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2383 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2384 | |
| 2385 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2386 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2387 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2388 | lua_pushinteger(L, 1); |
| 2389 | return 1; |
| 2390 | } |
| 2391 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2392 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2393 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2394 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2395 | } |
| 2396 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2397 | 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] | 2398 | return 0; |
| 2399 | } |
| 2400 | |
| 2401 | /* This function fail or initite the connection. */ |
| 2402 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2403 | { |
| 2404 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2405 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2406 | const char *ip; |
| 2407 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2408 | struct hlua *hlua; |
| 2409 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2410 | int low, high; |
| 2411 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2412 | struct xref *peer; |
| 2413 | struct stream_interface *si; |
| 2414 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2415 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2416 | if (lua_gettop(L) < 2) |
| 2417 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2418 | |
| 2419 | /* Get args. */ |
| 2420 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2421 | |
| 2422 | /* Check if we run on the same thread than the xreator thread. |
| 2423 | * We cannot access to the socket if the thread is different. |
| 2424 | */ |
| 2425 | if (socket->tid != tid) |
| 2426 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2427 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2428 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2429 | if (lua_gettop(L) >= 3) { |
| 2430 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2431 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2432 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2433 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2434 | * that are not enclosed within square brackets. |
| 2435 | */ |
| 2436 | if (port > 0) { |
| 2437 | luaL_buffinit(L, &b); |
| 2438 | luaL_addstring(&b, ip); |
| 2439 | luaL_addchar(&b, ':'); |
| 2440 | luaL_pushresult(&b); |
| 2441 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2442 | } |
| 2443 | } |
| 2444 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2445 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2446 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2447 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2448 | lua_pushnil(L); |
| 2449 | return 1; |
| 2450 | } |
| 2451 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2452 | si = appctx->owner; |
| 2453 | s = si_strm(si); |
| 2454 | |
| 2455 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2456 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2457 | if (!conn) { |
| 2458 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2459 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2460 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2461 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2462 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2463 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2464 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2465 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2466 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2467 | if (!addr) { |
| 2468 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2469 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2470 | } |
| 2471 | if (low != high) { |
| 2472 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2473 | 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] | 2474 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2475 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2476 | |
| 2477 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2478 | if (low == 0) { |
| 2479 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2480 | if (port == -1) { |
| 2481 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2482 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2483 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2484 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2485 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2486 | if (port == -1) { |
| 2487 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2488 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2489 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2490 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2491 | } |
| 2492 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2493 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2494 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2495 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2496 | |
| 2497 | /* inform the stream that we want to be notified whenever the |
| 2498 | * connection completes. |
| 2499 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2500 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2501 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2502 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2503 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2504 | hlua->flags |= HLUA_MUST_GC; |
| 2505 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2506 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2507 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2508 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2509 | } |
| 2510 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2511 | |
| 2512 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2513 | /* Return yield waiting for connection. */ |
| 2514 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2515 | 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] | 2516 | |
| 2517 | return 0; |
| 2518 | } |
| 2519 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2520 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2521 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2522 | { |
| 2523 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2524 | struct xref *peer; |
| 2525 | struct appctx *appctx; |
| 2526 | struct stream_interface *si; |
| 2527 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2528 | |
| 2529 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2530 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2531 | |
| 2532 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2533 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2534 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2535 | lua_pushnil(L); |
| 2536 | return 1; |
| 2537 | } |
| 2538 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2539 | si = appctx->owner; |
| 2540 | s = si_strm(si); |
| 2541 | |
| 2542 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2543 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2544 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2545 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2546 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2547 | |
| 2548 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2549 | { |
| 2550 | return 0; |
| 2551 | } |
| 2552 | |
| 2553 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2554 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2555 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2556 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2557 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2558 | struct xref *peer; |
| 2559 | struct appctx *appctx; |
| 2560 | struct stream_interface *si; |
| 2561 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2562 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2563 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2564 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2565 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2566 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2567 | /* convert the timeout to millis */ |
| 2568 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2569 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2570 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2571 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2572 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2573 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2574 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2575 | 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] | 2576 | |
| 2577 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2578 | if (tmout == 0) |
| 2579 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2580 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2581 | /* Check if we run on the same thread than the xreator thread. |
| 2582 | * We cannot access to the socket if the thread is different. |
| 2583 | */ |
| 2584 | if (socket->tid != tid) |
| 2585 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2586 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2587 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2588 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2589 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2590 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2591 | WILL_LJMP(lua_error(L)); |
| 2592 | return 0; |
| 2593 | } |
| 2594 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2595 | si = appctx->owner; |
| 2596 | s = si_strm(si); |
| 2597 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2598 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2599 | s->req.rto = tmout; |
| 2600 | s->req.wto = tmout; |
| 2601 | s->res.rto = tmout; |
| 2602 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2603 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2604 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2605 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2606 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2607 | |
| 2608 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2609 | task_queue(s->task); |
| 2610 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2611 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2612 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2613 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2614 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2618 | { |
| 2619 | struct hlua_socket *socket; |
| 2620 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2621 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2622 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2623 | |
| 2624 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2625 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2626 | hlua_pusherror(L, "socket: full stack"); |
| 2627 | goto out_fail_conf; |
| 2628 | } |
| 2629 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2630 | /* Create the object: obj[0] = userdata. */ |
| 2631 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2632 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2633 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2634 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2635 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2636 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2637 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2638 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2639 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2640 | goto out_fail_conf; |
| 2641 | } |
| 2642 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2643 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2644 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2645 | lua_setmetatable(L, -2); |
| 2646 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2647 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2648 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2649 | if (!appctx) { |
| 2650 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2651 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2652 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2653 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2654 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2655 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2656 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2657 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2658 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2659 | /* Now create a session, task and stream for this applet */ |
| 2660 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2661 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2662 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2663 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2664 | } |
| 2665 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2666 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2667 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2668 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2669 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2670 | } |
| 2671 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2672 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2673 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2674 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2675 | /* Configure "right" stream interface. this "si" is used to connect |
| 2676 | * and retrieve data from the server. The connection is initialized |
| 2677 | * with the "struct server". |
| 2678 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2679 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2680 | |
| 2681 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2682 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2683 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2684 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2685 | return 1; |
| 2686 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2687 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2688 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2689 | out_fail_sess: |
| 2690 | appctx_free(appctx); |
| 2691 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2692 | WILL_LJMP(lua_error(L)); |
| 2693 | return 0; |
| 2694 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2695 | |
| 2696 | /* |
| 2697 | * |
| 2698 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2699 | * Class Channel |
| 2700 | * |
| 2701 | * |
| 2702 | */ |
| 2703 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2704 | /* This function is called before the Lua execution. It stores |
| 2705 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2706 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2707 | 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] | 2708 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2709 | c->mode = stream->be->mode; |
| 2710 | switch (c->mode) { |
| 2711 | case PR_MODE_HTTP: |
| 2712 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2713 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2714 | c->data.http.state = stream->txn->req.msg_state; |
| 2715 | else |
| 2716 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2717 | break; |
| 2718 | default: |
| 2719 | break; |
| 2720 | } |
| 2721 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2722 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2723 | /* This function is called after the Lua execution. it |
| 2724 | * returns true if the parser state is consistent, otherwise, |
| 2725 | * it return false. |
| 2726 | * |
| 2727 | * In HTTP mode, the parser state must be in the same state |
| 2728 | * or greater when we exit the function. Even if we do a |
| 2729 | * control yield. This prevent to break the HTTP message |
| 2730 | * from the Lua code. |
| 2731 | */ |
| 2732 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2733 | { |
| 2734 | if (c->mode != stream->be->mode) |
| 2735 | return 0; |
| 2736 | |
| 2737 | switch (c->mode) { |
| 2738 | case PR_MODE_HTTP: |
| 2739 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2740 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2741 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2742 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2743 | else |
| 2744 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2745 | default: |
| 2746 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2747 | } |
| 2748 | return 1; |
| 2749 | } |
| 2750 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2751 | /* Returns the struct hlua_channel join to the class channel in the |
| 2752 | * stack entry "ud" or throws an argument error. |
| 2753 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2754 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2755 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2756 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2757 | } |
| 2758 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2759 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2760 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2761 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2762 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2763 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2764 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2765 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2766 | return 0; |
| 2767 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2768 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2769 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2770 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2771 | |
| 2772 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2773 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2774 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2775 | return 1; |
| 2776 | } |
| 2777 | |
| 2778 | /* Duplicate all the data present in the input channel and put it |
| 2779 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2780 | * the stack if the channel is closed and all the data are consumed, |
| 2781 | * returns 0 if no data are available, otherwise it returns the length |
| 2782 | * of the builded string. |
| 2783 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2784 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2785 | { |
| 2786 | char *blk1; |
| 2787 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2788 | size_t len1; |
| 2789 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2790 | int ret; |
| 2791 | luaL_Buffer b; |
| 2792 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2793 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2794 | if (unlikely(ret == 0)) |
| 2795 | return 0; |
| 2796 | |
| 2797 | if (unlikely(ret < 0)) { |
| 2798 | lua_pushnil(L); |
| 2799 | return -1; |
| 2800 | } |
| 2801 | |
| 2802 | luaL_buffinit(L, &b); |
| 2803 | luaL_addlstring(&b, blk1, len1); |
| 2804 | if (unlikely(ret == 2)) |
| 2805 | luaL_addlstring(&b, blk2, len2); |
| 2806 | luaL_pushresult(&b); |
| 2807 | |
| 2808 | if (unlikely(ret == 2)) |
| 2809 | return len1 + len2; |
| 2810 | return len1; |
| 2811 | } |
| 2812 | |
| 2813 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2814 | * a yield. This function keep the data in the buffer. |
| 2815 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2816 | __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] | 2817 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2818 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2819 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2820 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2821 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2822 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2823 | WILL_LJMP(lua_error(L)); |
| 2824 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2825 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2826 | 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] | 2827 | return 1; |
| 2828 | } |
| 2829 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2830 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2831 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2832 | { |
| 2833 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2834 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2835 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2836 | } |
| 2837 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2838 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2839 | * a yield. This function consumes the data in the buffer. It returns |
| 2840 | * a string containing the data or a nil pointer if no data are available |
| 2841 | * and the channel is closed. |
| 2842 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2843 | __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] | 2844 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2845 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2846 | int ret; |
| 2847 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2848 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2849 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2850 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2851 | WILL_LJMP(lua_error(L)); |
| 2852 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2853 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2854 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2855 | 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] | 2856 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2857 | if (unlikely(ret == -1)) |
| 2858 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2859 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2860 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2861 | return 1; |
| 2862 | } |
| 2863 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2864 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2865 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2866 | { |
| 2867 | MAY_LJMP(check_args(L, 1, "get")); |
| 2868 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2869 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2870 | } |
| 2871 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2872 | /* This functions consumes and returns one line. If the channel is closed, |
| 2873 | * 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] | 2874 | * 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] | 2875 | * value. |
| 2876 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2877 | __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] | 2878 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2879 | char *blk1; |
| 2880 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2881 | size_t len1; |
| 2882 | size_t len2; |
| 2883 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2884 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2885 | int ret; |
| 2886 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2887 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2888 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 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 | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2893 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2894 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2895 | 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] | 2896 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2897 | if (ret == -1) { |
| 2898 | lua_pushnil(L); |
| 2899 | return 1; |
| 2900 | } |
| 2901 | |
| 2902 | luaL_buffinit(L, &b); |
| 2903 | luaL_addlstring(&b, blk1, len1); |
| 2904 | len = len1; |
| 2905 | if (unlikely(ret == 2)) { |
| 2906 | luaL_addlstring(&b, blk2, len2); |
| 2907 | len += len2; |
| 2908 | } |
| 2909 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2910 | 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] | 2911 | return 1; |
| 2912 | } |
| 2913 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2914 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2915 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2916 | { |
| 2917 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2918 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2919 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2920 | } |
| 2921 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2922 | /* This function takes a string as input, and append it at the |
| 2923 | * input side of channel. If the data is too big, but a space |
| 2924 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2925 | * yields. If the data is bigger than the buffer, or if the |
| 2926 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2927 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2928 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2929 | __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] | 2930 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2931 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2932 | size_t len; |
| 2933 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2934 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2935 | int ret; |
| 2936 | int max; |
| 2937 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2938 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2939 | WILL_LJMP(lua_error(L)); |
| 2940 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2941 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2942 | * the request buffer if its not required. |
| 2943 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2944 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2945 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2946 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2947 | } |
| 2948 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2949 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2950 | if (max > len - l) |
| 2951 | max = len - l; |
| 2952 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2953 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2954 | if (ret == -2 || ret == -3) { |
| 2955 | lua_pushinteger(L, -1); |
| 2956 | return 1; |
| 2957 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2958 | if (ret == -1) { |
| 2959 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2960 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2961 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2962 | l += ret; |
| 2963 | lua_pop(L, 1); |
| 2964 | lua_pushinteger(L, l); |
| 2965 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2966 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2967 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2968 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2969 | * in this case, we cannot add more data, so we cannot yield, |
| 2970 | * we return the amount of copyied data. |
| 2971 | */ |
| 2972 | return 1; |
| 2973 | } |
| 2974 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2975 | 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] | 2976 | return 1; |
| 2977 | } |
| 2978 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2979 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2980 | * 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] | 2981 | * buffer size is too little for the data. |
| 2982 | */ |
| 2983 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2984 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2985 | size_t len; |
| 2986 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2987 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2988 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2989 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2990 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2991 | lua_pushinteger(L, 0); |
| 2992 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2993 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2994 | } |
| 2995 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2996 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2997 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2998 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2999 | * or -1 if the channel is closed or if the buffer size is too |
| 3000 | * little for the data. |
| 3001 | */ |
| 3002 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3003 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3004 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3005 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3006 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3007 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3008 | lua_pushinteger(L, 0); |
| 3009 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3010 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3011 | WILL_LJMP(lua_error(L)); |
| 3012 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3013 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3014 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3015 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3016 | } |
| 3017 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3018 | /* Append data in the output side of the buffer. This data is immediately |
| 3019 | * sent. The function returns the amount of data written. If the buffer |
| 3020 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3021 | * if the channel is closed. |
| 3022 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3023 | __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] | 3024 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3025 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3026 | size_t len; |
| 3027 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3028 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3029 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3030 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3031 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3032 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3033 | WILL_LJMP(lua_error(L)); |
| 3034 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3035 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3036 | lua_pushinteger(L, -1); |
| 3037 | return 1; |
| 3038 | } |
| 3039 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3040 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3041 | * the request buffer if its not required. |
| 3042 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3043 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3044 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3045 | 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] | 3046 | } |
| 3047 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3048 | /* The written data will be immediately sent, so we can check |
| 3049 | * the available space without taking in account the reserve. |
| 3050 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3051 | * data, because the buffer will be flushed. |
| 3052 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3053 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3054 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3055 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3056 | * in this case, we cannot add more data, so we cannot yield, |
| 3057 | * we return the amount of copyied data. |
| 3058 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3059 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3060 | return 1; |
| 3061 | |
| 3062 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3063 | if (max > len - l) |
| 3064 | max = len - l; |
| 3065 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3066 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3067 | * detects a non contiguous buffer and realign it. |
| 3068 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3069 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3070 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3071 | |
| 3072 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3073 | 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] | 3074 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3075 | /* buffer replace considers that the input part is filled. |
| 3076 | * so, I must forward these new data in the output part. |
| 3077 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3078 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3079 | |
| 3080 | l += max; |
| 3081 | lua_pop(L, 1); |
| 3082 | lua_pushinteger(L, l); |
| 3083 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3084 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3085 | * in this case, we cannot add more data, so we cannot yield, |
| 3086 | * we return the amount of copyied data. |
| 3087 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3088 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3089 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3090 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3091 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3092 | if (l < len) { |
| 3093 | /* If we are waiting for space in the response buffer, we |
| 3094 | * must set the flag WAKERESWR. This flag required the task |
| 3095 | * wake up if any activity is detected on the response buffer. |
| 3096 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3097 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3098 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3099 | else |
| 3100 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3101 | 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] | 3102 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3103 | |
| 3104 | return 1; |
| 3105 | } |
| 3106 | |
| 3107 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3108 | * yield the LUA process, and resume it without checking the |
| 3109 | * input arguments. |
| 3110 | */ |
| 3111 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3112 | { |
| 3113 | MAY_LJMP(check_args(L, 2, "send")); |
| 3114 | lua_pushinteger(L, 0); |
| 3115 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3116 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3117 | } |
| 3118 | |
| 3119 | /* This function forward and amount of butes. The data pass from |
| 3120 | * the input side of the buffer to the output side, and can be |
| 3121 | * forwarded. This function never fails. |
| 3122 | * |
| 3123 | * The Lua function takes an amount of bytes to be forwarded in |
| 3124 | * imput. It returns the number of bytes forwarded. |
| 3125 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3126 | __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] | 3127 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3128 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3129 | int len; |
| 3130 | int l; |
| 3131 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3132 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3133 | |
| 3134 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3135 | |
| 3136 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3137 | WILL_LJMP(lua_error(L)); |
| 3138 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3139 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3140 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3141 | |
| 3142 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3143 | if (max > ci_data(chn)) |
| 3144 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3145 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3146 | l += max; |
| 3147 | |
| 3148 | lua_pop(L, 1); |
| 3149 | lua_pushinteger(L, l); |
| 3150 | |
| 3151 | /* Check if it miss bytes to forward. */ |
| 3152 | if (l < len) { |
| 3153 | /* The the input channel or the output channel are closed, we |
| 3154 | * must return the amount of data forwarded. |
| 3155 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3156 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3157 | return 1; |
| 3158 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3159 | /* If we are waiting for space data in the response buffer, we |
| 3160 | * must set the flag WAKERESWR. This flag required the task |
| 3161 | * wake up if any activity is detected on the response buffer. |
| 3162 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3163 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3164 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3165 | else |
| 3166 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3167 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3168 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3169 | 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] | 3170 | } |
| 3171 | |
| 3172 | return 1; |
| 3173 | } |
| 3174 | |
| 3175 | /* Just check the input and prepare the stack for the previous |
| 3176 | * function "hlua_channel_forward_yield" |
| 3177 | */ |
| 3178 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3179 | { |
| 3180 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3181 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3182 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3183 | |
| 3184 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3185 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | /* Just returns the number of bytes available in the input |
| 3189 | * side of the buffer. This function never fails. |
| 3190 | */ |
| 3191 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3192 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3193 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3194 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3195 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3196 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3197 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3198 | struct htx *htx = htxbuf(&chn->buf); |
| 3199 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3200 | } |
| 3201 | else |
| 3202 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3203 | return 1; |
| 3204 | } |
| 3205 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3206 | /* Returns true if the channel is full. */ |
| 3207 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3208 | { |
| 3209 | struct channel *chn; |
| 3210 | int rem; |
| 3211 | |
| 3212 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3213 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3214 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3215 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3216 | struct htx *htx = htxbuf(&chn->buf); |
| 3217 | |
| 3218 | rem = htx_free_data_space(htx); |
| 3219 | } |
| 3220 | else |
| 3221 | rem = b_room(&chn->buf); |
| 3222 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3223 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3224 | |
| 3225 | lua_pushboolean(L, rem <= 0); |
| 3226 | return 1; |
| 3227 | } |
| 3228 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3229 | /* Just returns the number of bytes available in the output |
| 3230 | * side of the buffer. This function never fails. |
| 3231 | */ |
| 3232 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3233 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3234 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3235 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3236 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3237 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3238 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3239 | return 1; |
| 3240 | } |
| 3241 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3242 | /* |
| 3243 | * |
| 3244 | * |
| 3245 | * Class Fetches |
| 3246 | * |
| 3247 | * |
| 3248 | */ |
| 3249 | |
| 3250 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3251 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3252 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3253 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3254 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3255 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3256 | } |
| 3257 | |
| 3258 | /* This function creates and push in the stack a fetch object according |
| 3259 | * with a current TXN. |
| 3260 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3261 | 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] | 3262 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3263 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3264 | |
| 3265 | /* Check stack size. */ |
| 3266 | if (!lua_checkstack(L, 3)) |
| 3267 | return 0; |
| 3268 | |
| 3269 | /* Create the object: obj[0] = userdata. |
| 3270 | * Note that the base of the Fetches object is the |
| 3271 | * transaction object. |
| 3272 | */ |
| 3273 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3274 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3275 | lua_rawseti(L, -2, 0); |
| 3276 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3277 | hsmp->s = txn->s; |
| 3278 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3279 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3280 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3281 | |
| 3282 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3283 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3284 | lua_setmetatable(L, -2); |
| 3285 | |
| 3286 | return 1; |
| 3287 | } |
| 3288 | |
| 3289 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3290 | * It uses closure argument to store the associated sample-fetch. It |
| 3291 | * returns only one argument or throws an error. An error is thrown |
| 3292 | * only if an error is encountered during the argument parsing. If |
| 3293 | * the "sample-fetch" function fails, nil is returned. |
| 3294 | */ |
| 3295 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3296 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3297 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3298 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3299 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3300 | int i; |
| 3301 | struct sample smp; |
| 3302 | |
| 3303 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3304 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3305 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3306 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3307 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3308 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3309 | /* Check execution authorization. */ |
| 3310 | if (f->use & SMP_USE_HTTP_ANY && |
| 3311 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3312 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3313 | "is not available in Lua services", f->kw); |
| 3314 | WILL_LJMP(lua_error(L)); |
| 3315 | } |
| 3316 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3317 | /* Get extra arguments. */ |
| 3318 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3319 | if (i >= ARGM_NBARGS) |
| 3320 | break; |
| 3321 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3322 | } |
| 3323 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3324 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3325 | |
| 3326 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3327 | 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] | 3328 | |
| 3329 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3330 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3331 | lua_pushfstring(L, "error in arguments"); |
| 3332 | WILL_LJMP(lua_error(L)); |
| 3333 | } |
| 3334 | |
| 3335 | /* Initialise the sample. */ |
| 3336 | memset(&smp, 0, sizeof(smp)); |
| 3337 | |
| 3338 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3339 | 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] | 3340 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3341 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3342 | lua_pushstring(L, ""); |
| 3343 | else |
| 3344 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3345 | return 1; |
| 3346 | } |
| 3347 | |
| 3348 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3349 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3350 | hlua_smp2lua_str(L, &smp); |
| 3351 | else |
| 3352 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3353 | return 1; |
| 3354 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3355 | |
| 3356 | /* |
| 3357 | * |
| 3358 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3359 | * Class Converters |
| 3360 | * |
| 3361 | * |
| 3362 | */ |
| 3363 | |
| 3364 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3365 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3366 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3367 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3368 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3369 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3370 | } |
| 3371 | |
| 3372 | /* This function creates and push in the stack a Converters object |
| 3373 | * according with a current TXN. |
| 3374 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3375 | 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] | 3376 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3377 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3378 | |
| 3379 | /* Check stack size. */ |
| 3380 | if (!lua_checkstack(L, 3)) |
| 3381 | return 0; |
| 3382 | |
| 3383 | /* Create the object: obj[0] = userdata. |
| 3384 | * Note that the base of the Converters object is the |
| 3385 | * same than the TXN object. |
| 3386 | */ |
| 3387 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3388 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3389 | lua_rawseti(L, -2, 0); |
| 3390 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3391 | hsmp->s = txn->s; |
| 3392 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3393 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3394 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3395 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3396 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3397 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3398 | lua_setmetatable(L, -2); |
| 3399 | |
| 3400 | return 1; |
| 3401 | } |
| 3402 | |
| 3403 | /* This function is an LUA binding. It is called with each converter. |
| 3404 | * It uses closure argument to store the associated converter. It |
| 3405 | * returns only one argument or throws an error. An error is thrown |
| 3406 | * only if an error is encountered during the argument parsing. If |
| 3407 | * the converter function function fails, nil is returned. |
| 3408 | */ |
| 3409 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3410 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3411 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3412 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3413 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3414 | int i; |
| 3415 | struct sample smp; |
| 3416 | |
| 3417 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3418 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3419 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3420 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3421 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3422 | |
| 3423 | /* Get extra arguments. */ |
| 3424 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3425 | if (i >= ARGM_NBARGS) |
| 3426 | break; |
| 3427 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3428 | } |
| 3429 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3430 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3431 | |
| 3432 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3433 | 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] | 3434 | |
| 3435 | /* Run the special args checker. */ |
| 3436 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3437 | hlua_pusherror(L, "error in arguments"); |
| 3438 | WILL_LJMP(lua_error(L)); |
| 3439 | } |
| 3440 | |
| 3441 | /* Initialise the sample. */ |
| 3442 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3443 | hlua_pusherror(L, "error in the input argument"); |
| 3444 | WILL_LJMP(lua_error(L)); |
| 3445 | } |
| 3446 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3447 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3448 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3449 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3450 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3451 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3452 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3453 | WILL_LJMP(lua_error(L)); |
| 3454 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3455 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3456 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3457 | hlua_pusherror(L, "error during the input argument casting"); |
| 3458 | WILL_LJMP(lua_error(L)); |
| 3459 | } |
| 3460 | |
| 3461 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3462 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3463 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3464 | lua_pushstring(L, ""); |
| 3465 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3466 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3467 | return 1; |
| 3468 | } |
| 3469 | |
| 3470 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3471 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3472 | hlua_smp2lua_str(L, &smp); |
| 3473 | else |
| 3474 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3475 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3476 | } |
| 3477 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3478 | /* |
| 3479 | * |
| 3480 | * |
| 3481 | * Class AppletTCP |
| 3482 | * |
| 3483 | * |
| 3484 | */ |
| 3485 | |
| 3486 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3487 | * a class stream, otherwise it throws an error. |
| 3488 | */ |
| 3489 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3490 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3491 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | /* This function creates and push in the stack an Applet object |
| 3495 | * according with a current TXN. |
| 3496 | */ |
| 3497 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3498 | { |
| 3499 | struct hlua_appctx *appctx; |
| 3500 | struct stream_interface *si = ctx->owner; |
| 3501 | struct stream *s = si_strm(si); |
| 3502 | struct proxy *p = s->be; |
| 3503 | |
| 3504 | /* Check stack size. */ |
| 3505 | if (!lua_checkstack(L, 3)) |
| 3506 | return 0; |
| 3507 | |
| 3508 | /* Create the object: obj[0] = userdata. |
| 3509 | * Note that the base of the Converters object is the |
| 3510 | * same than the TXN object. |
| 3511 | */ |
| 3512 | lua_newtable(L); |
| 3513 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3514 | lua_rawseti(L, -2, 0); |
| 3515 | appctx->appctx = ctx; |
| 3516 | appctx->htxn.s = s; |
| 3517 | appctx->htxn.p = p; |
| 3518 | |
| 3519 | /* Create the "f" field that contains a list of fetches. */ |
| 3520 | lua_pushstring(L, "f"); |
| 3521 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3522 | return 0; |
| 3523 | lua_settable(L, -3); |
| 3524 | |
| 3525 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3526 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3527 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3528 | return 0; |
| 3529 | lua_settable(L, -3); |
| 3530 | |
| 3531 | /* Create the "c" field that contains a list of converters. */ |
| 3532 | lua_pushstring(L, "c"); |
| 3533 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3534 | return 0; |
| 3535 | lua_settable(L, -3); |
| 3536 | |
| 3537 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3538 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3539 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3540 | return 0; |
| 3541 | lua_settable(L, -3); |
| 3542 | |
| 3543 | /* Pop a class stream metatable and affect it to the table. */ |
| 3544 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3545 | lua_setmetatable(L, -2); |
| 3546 | |
| 3547 | return 1; |
| 3548 | } |
| 3549 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3550 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3551 | { |
| 3552 | struct hlua_appctx *appctx; |
| 3553 | struct stream *s; |
| 3554 | const char *name; |
| 3555 | size_t len; |
| 3556 | struct sample smp; |
| 3557 | |
| 3558 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3559 | |
| 3560 | /* It is useles to retrieve the stream, but this function |
| 3561 | * runs only in a stream context. |
| 3562 | */ |
| 3563 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3564 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3565 | s = appctx->htxn.s; |
| 3566 | |
| 3567 | /* Converts the third argument in a sample. */ |
| 3568 | hlua_lua2smp(L, 3, &smp); |
| 3569 | |
| 3570 | /* Store the sample in a variable. */ |
| 3571 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3572 | vars_set_by_name(name, len, &smp); |
| 3573 | return 0; |
| 3574 | } |
| 3575 | |
| 3576 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3577 | { |
| 3578 | struct hlua_appctx *appctx; |
| 3579 | struct stream *s; |
| 3580 | const char *name; |
| 3581 | size_t len; |
| 3582 | struct sample smp; |
| 3583 | |
| 3584 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3585 | |
| 3586 | /* It is useles to retrieve the stream, but this function |
| 3587 | * runs only in a stream context. |
| 3588 | */ |
| 3589 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3590 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3591 | s = appctx->htxn.s; |
| 3592 | |
| 3593 | /* Unset the variable. */ |
| 3594 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3595 | vars_unset_by_name(name, len, &smp); |
| 3596 | return 0; |
| 3597 | } |
| 3598 | |
| 3599 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3600 | { |
| 3601 | struct hlua_appctx *appctx; |
| 3602 | struct stream *s; |
| 3603 | const char *name; |
| 3604 | size_t len; |
| 3605 | struct sample smp; |
| 3606 | |
| 3607 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3608 | |
| 3609 | /* It is useles to retrieve the stream, but this function |
| 3610 | * runs only in a stream context. |
| 3611 | */ |
| 3612 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3613 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3614 | s = appctx->htxn.s; |
| 3615 | |
| 3616 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3617 | if (!vars_get_by_name(name, len, &smp)) { |
| 3618 | lua_pushnil(L); |
| 3619 | return 1; |
| 3620 | } |
| 3621 | |
| 3622 | return hlua_smp2lua(L, &smp); |
| 3623 | } |
| 3624 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3625 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3626 | { |
| 3627 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3628 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3629 | struct hlua *hlua; |
| 3630 | |
| 3631 | /* 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] | 3632 | if (!s->hlua) |
| 3633 | return 0; |
| 3634 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3635 | |
| 3636 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3637 | |
| 3638 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3639 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3640 | |
| 3641 | /* Get and store new value. */ |
| 3642 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3643 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3644 | |
| 3645 | return 0; |
| 3646 | } |
| 3647 | |
| 3648 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3649 | { |
| 3650 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3651 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3652 | struct hlua *hlua; |
| 3653 | |
| 3654 | /* 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] | 3655 | if (!s->hlua) { |
| 3656 | lua_pushnil(L); |
| 3657 | return 1; |
| 3658 | } |
| 3659 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3660 | |
| 3661 | /* Push configuration index in the stack. */ |
| 3662 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3663 | |
| 3664 | return 1; |
| 3665 | } |
| 3666 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3667 | /* If expected data not yet available, it returns a yield. This function |
| 3668 | * consumes the data in the buffer. It returns a string containing the |
| 3669 | * data. This string can be empty. |
| 3670 | */ |
| 3671 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3672 | { |
| 3673 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3674 | struct stream_interface *si = appctx->appctx->owner; |
| 3675 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3676 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3677 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3678 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3679 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3680 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3681 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3682 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3683 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3684 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3685 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3686 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3687 | 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] | 3688 | } |
| 3689 | |
| 3690 | /* End of data: commit the total strings and return. */ |
| 3691 | if (ret < 0) { |
| 3692 | luaL_pushresult(&appctx->b); |
| 3693 | return 1; |
| 3694 | } |
| 3695 | |
| 3696 | /* Ensure that the block 2 length is usable. */ |
| 3697 | if (ret == 1) |
| 3698 | len2 = 0; |
| 3699 | |
| 3700 | /* dont check the max length read and dont check. */ |
| 3701 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3702 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3703 | |
| 3704 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3705 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3706 | luaL_pushresult(&appctx->b); |
| 3707 | return 1; |
| 3708 | } |
| 3709 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3710 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3711 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3712 | { |
| 3713 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3714 | |
| 3715 | /* Initialise the string catenation. */ |
| 3716 | luaL_buffinit(L, &appctx->b); |
| 3717 | |
| 3718 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3719 | } |
| 3720 | |
| 3721 | /* If expected data not yet available, it returns a yield. This function |
| 3722 | * consumes the data in the buffer. It returns a string containing the |
| 3723 | * data. This string can be empty. |
| 3724 | */ |
| 3725 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3726 | { |
| 3727 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3728 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3729 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3730 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3731 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3732 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3733 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3734 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3735 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3736 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3737 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3738 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3739 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3740 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3741 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3742 | 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] | 3743 | } |
| 3744 | |
| 3745 | /* End of data: commit the total strings and return. */ |
| 3746 | if (ret < 0) { |
| 3747 | luaL_pushresult(&appctx->b); |
| 3748 | return 1; |
| 3749 | } |
| 3750 | |
| 3751 | /* Ensure that the block 2 length is usable. */ |
| 3752 | if (ret == 1) |
| 3753 | len2 = 0; |
| 3754 | |
| 3755 | if (len == -1) { |
| 3756 | |
| 3757 | /* If len == -1, catenate all the data avalaile and |
| 3758 | * yield because we want to get all the data until |
| 3759 | * the end of data stream. |
| 3760 | */ |
| 3761 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3762 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3763 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3764 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3765 | 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] | 3766 | |
| 3767 | } else { |
| 3768 | |
| 3769 | /* Copy the fisrt block caping to the length required. */ |
| 3770 | if (len1 > len) |
| 3771 | len1 = len; |
| 3772 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3773 | len -= len1; |
| 3774 | |
| 3775 | /* Copy the second block. */ |
| 3776 | if (len2 > len) |
| 3777 | len2 = len; |
| 3778 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3779 | len -= len2; |
| 3780 | |
| 3781 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3782 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3783 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3784 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3785 | if (len > 0) { |
| 3786 | lua_pushinteger(L, len); |
| 3787 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3788 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3789 | 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] | 3790 | } |
| 3791 | |
| 3792 | /* return the result. */ |
| 3793 | luaL_pushresult(&appctx->b); |
| 3794 | return 1; |
| 3795 | } |
| 3796 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3797 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3798 | hlua_pusherror(L, "Lua: internal error"); |
| 3799 | WILL_LJMP(lua_error(L)); |
| 3800 | return 0; |
| 3801 | } |
| 3802 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3803 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3804 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3805 | { |
| 3806 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3807 | int len = -1; |
| 3808 | |
| 3809 | if (lua_gettop(L) > 2) |
| 3810 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3811 | if (lua_gettop(L) >= 2) { |
| 3812 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3813 | lua_pop(L, 1); |
| 3814 | } |
| 3815 | |
| 3816 | /* Confirm or set the required length */ |
| 3817 | lua_pushinteger(L, len); |
| 3818 | |
| 3819 | /* Initialise the string catenation. */ |
| 3820 | luaL_buffinit(L, &appctx->b); |
| 3821 | |
| 3822 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3823 | } |
| 3824 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3825 | /* Append data in the output side of the buffer. This data is immediately |
| 3826 | * sent. The function returns the amount of data written. If the buffer |
| 3827 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3828 | * if the channel is closed. |
| 3829 | */ |
| 3830 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3831 | { |
| 3832 | size_t len; |
| 3833 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3834 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3835 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3836 | struct stream_interface *si = appctx->appctx->owner; |
| 3837 | struct channel *chn = si_ic(si); |
| 3838 | int max; |
| 3839 | |
| 3840 | /* Get the max amount of data which can write as input in the channel. */ |
| 3841 | max = channel_recv_max(chn); |
| 3842 | if (max > (len - l)) |
| 3843 | max = len - l; |
| 3844 | |
| 3845 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3846 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3847 | |
| 3848 | /* update counters. */ |
| 3849 | l += max; |
| 3850 | lua_pop(L, 1); |
| 3851 | lua_pushinteger(L, l); |
| 3852 | |
| 3853 | /* If some data is not send, declares the situation to the |
| 3854 | * applet, and returns a yield. |
| 3855 | */ |
| 3856 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3857 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3858 | 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] | 3859 | } |
| 3860 | |
| 3861 | return 1; |
| 3862 | } |
| 3863 | |
| 3864 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3865 | * yield the LUA process, and resume it without checking the |
| 3866 | * input arguments. |
| 3867 | */ |
| 3868 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3869 | { |
| 3870 | MAY_LJMP(check_args(L, 2, "send")); |
| 3871 | lua_pushinteger(L, 0); |
| 3872 | |
| 3873 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3874 | } |
| 3875 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3876 | /* |
| 3877 | * |
| 3878 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3879 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3880 | * |
| 3881 | * |
| 3882 | */ |
| 3883 | |
| 3884 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3885 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3886 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3887 | __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] | 3888 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3889 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3890 | } |
| 3891 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3892 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3893 | * according with a current TXN. |
| 3894 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3895 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3896 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3897 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3898 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3899 | struct stream_interface *si = ctx->owner; |
| 3900 | struct stream *s = si_strm(si); |
| 3901 | struct proxy *px = s->be; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3902 | |
| 3903 | /* Check stack size. */ |
| 3904 | if (!lua_checkstack(L, 3)) |
| 3905 | return 0; |
| 3906 | |
| 3907 | /* Create the object: obj[0] = userdata. |
| 3908 | * Note that the base of the Converters object is the |
| 3909 | * same than the TXN object. |
| 3910 | */ |
| 3911 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3912 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3913 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3914 | appctx->appctx = ctx; |
| 3915 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3916 | 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] | 3917 | appctx->htxn.s = s; |
| 3918 | appctx->htxn.p = px; |
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 | /* Create the "f" field that contains a list of fetches. */ |
| 3921 | lua_pushstring(L, "f"); |
| 3922 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3923 | return 0; |
| 3924 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3925 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3926 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3927 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3928 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3929 | return 0; |
| 3930 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3931 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3932 | /* Create the "c" field that contains a list of converters. */ |
| 3933 | lua_pushstring(L, "c"); |
| 3934 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3935 | return 0; |
| 3936 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3937 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3938 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3939 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3940 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3941 | return 0; |
| 3942 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3943 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3944 | if (IS_HTX_STRM(s)) { |
| 3945 | /* HTX version */ |
| 3946 | struct htx *htx = htxbuf(&s->req.buf); |
| 3947 | struct htx_sl *sl = http_find_stline(htx); |
| 3948 | struct ist path; |
| 3949 | unsigned long long len = 0; |
| 3950 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3951 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3952 | /* Stores the request method. */ |
| 3953 | lua_pushstring(L, "method"); |
| 3954 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3955 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3956 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3957 | /* Stores the http version. */ |
| 3958 | lua_pushstring(L, "version"); |
| 3959 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3960 | lua_settable(L, -3); |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3961 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3962 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3963 | * the array on the top of the stack. |
| 3964 | */ |
| 3965 | lua_pushstring(L, "headers"); |
| 3966 | htxn.s = s; |
| 3967 | htxn.p = px; |
| 3968 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3969 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3970 | return 0; |
| 3971 | lua_settable(L, -3); |
| 3972 | |
| 3973 | path = http_get_path(htx_sl_req_uri(sl)); |
| 3974 | if (path.ptr) { |
| 3975 | char *p, *q, *end; |
| 3976 | |
| 3977 | p = path.ptr; |
| 3978 | end = path.ptr + path.len; |
| 3979 | q = p; |
| 3980 | while (q < end && *q != '?') |
| 3981 | q++; |
| 3982 | |
| 3983 | /* Stores the request path. */ |
| 3984 | lua_pushstring(L, "path"); |
| 3985 | lua_pushlstring(L, p, q - p); |
| 3986 | lua_settable(L, -3); |
| 3987 | |
| 3988 | /* Stores the query string. */ |
| 3989 | lua_pushstring(L, "qs"); |
| 3990 | if (*q == '?') |
| 3991 | q++; |
| 3992 | lua_pushlstring(L, q, end - q); |
| 3993 | lua_settable(L, -3); |
| 3994 | } |
| 3995 | |
| 3996 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 3997 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3998 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 3999 | |
| 4000 | if (type == HTX_BLK_EOM || type == HTX_BLK_EOD) |
| 4001 | break; |
| 4002 | if (type == HTX_BLK_DATA) |
| 4003 | len += htx_get_blksz(blk); |
| 4004 | } |
| 4005 | if (htx->extra != ULLONG_MAX) |
| 4006 | len += htx->extra; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4007 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4008 | /* Stores the request path. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4009 | lua_pushstring(L, "length"); |
| 4010 | lua_pushinteger(L, len); |
| 4011 | lua_settable(L, -3); |
| 4012 | } |
| 4013 | else { |
| 4014 | /* Legacy HTTP version */ |
| 4015 | struct http_txn *txn = s->txn; |
| 4016 | const char *path; |
| 4017 | const char *end; |
| 4018 | const char *p; |
| 4019 | |
| 4020 | /* Stores the request method. */ |
| 4021 | lua_pushstring(L, "method"); |
| 4022 | 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] | 4023 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4024 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4025 | /* Stores the http version. */ |
| 4026 | lua_pushstring(L, "version"); |
| 4027 | 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] | 4028 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4029 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4030 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4031 | * the array on the top of the stack. |
| 4032 | */ |
| 4033 | lua_pushstring(L, "headers"); |
| 4034 | htxn.s = s; |
| 4035 | htxn.p = px; |
| 4036 | htxn.dir = SMP_OPT_DIR_REQ; |
| 4037 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 4038 | return 0; |
| 4039 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4040 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4041 | /* Get path and qs */ |
| 4042 | path = http_txn_get_path(txn); |
| 4043 | if (path) { |
| 4044 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 4045 | p = path; |
| 4046 | while (p < end && *p != '?') |
| 4047 | p++; |
| 4048 | |
| 4049 | /* Stores the request path. */ |
| 4050 | lua_pushstring(L, "path"); |
| 4051 | lua_pushlstring(L, path, p - path); |
| 4052 | lua_settable(L, -3); |
| 4053 | |
| 4054 | /* Stores the query string. */ |
| 4055 | lua_pushstring(L, "qs"); |
| 4056 | if (*p == '?') |
| 4057 | p++; |
| 4058 | lua_pushlstring(L, p, end - p); |
| 4059 | lua_settable(L, -3); |
| 4060 | } |
| 4061 | |
| 4062 | /* Stores the request path. */ |
| 4063 | lua_pushstring(L, "length"); |
| 4064 | lua_pushinteger(L, txn->req.body_len); |
| 4065 | lua_settable(L, -3); |
| 4066 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4067 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4068 | /* Create an empty array of HTTP request headers. */ |
| 4069 | lua_pushstring(L, "response"); |
| 4070 | lua_newtable(L); |
| 4071 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4072 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4073 | /* Pop a class stream metatable and affect it to the table. */ |
| 4074 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4075 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4076 | |
| 4077 | return 1; |
| 4078 | } |
| 4079 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4080 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4081 | { |
| 4082 | struct hlua_appctx *appctx; |
| 4083 | struct stream *s; |
| 4084 | const char *name; |
| 4085 | size_t len; |
| 4086 | struct sample smp; |
| 4087 | |
| 4088 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4089 | |
| 4090 | /* It is useles to retrieve the stream, but this function |
| 4091 | * runs only in a stream context. |
| 4092 | */ |
| 4093 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4094 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4095 | s = appctx->htxn.s; |
| 4096 | |
| 4097 | /* Converts the third argument in a sample. */ |
| 4098 | hlua_lua2smp(L, 3, &smp); |
| 4099 | |
| 4100 | /* Store the sample in a variable. */ |
| 4101 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4102 | vars_set_by_name(name, len, &smp); |
| 4103 | return 0; |
| 4104 | } |
| 4105 | |
| 4106 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4107 | { |
| 4108 | struct hlua_appctx *appctx; |
| 4109 | struct stream *s; |
| 4110 | const char *name; |
| 4111 | size_t len; |
| 4112 | struct sample smp; |
| 4113 | |
| 4114 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4115 | |
| 4116 | /* It is useles to retrieve the stream, but this function |
| 4117 | * runs only in a stream context. |
| 4118 | */ |
| 4119 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4120 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4121 | s = appctx->htxn.s; |
| 4122 | |
| 4123 | /* Unset the variable. */ |
| 4124 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4125 | vars_unset_by_name(name, len, &smp); |
| 4126 | return 0; |
| 4127 | } |
| 4128 | |
| 4129 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4130 | { |
| 4131 | struct hlua_appctx *appctx; |
| 4132 | struct stream *s; |
| 4133 | const char *name; |
| 4134 | size_t len; |
| 4135 | struct sample smp; |
| 4136 | |
| 4137 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4138 | |
| 4139 | /* It is useles to retrieve the stream, but this function |
| 4140 | * runs only in a stream context. |
| 4141 | */ |
| 4142 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4143 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4144 | s = appctx->htxn.s; |
| 4145 | |
| 4146 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4147 | if (!vars_get_by_name(name, len, &smp)) { |
| 4148 | lua_pushnil(L); |
| 4149 | return 1; |
| 4150 | } |
| 4151 | |
| 4152 | return hlua_smp2lua(L, &smp); |
| 4153 | } |
| 4154 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4155 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4156 | { |
| 4157 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4158 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4159 | struct hlua *hlua; |
| 4160 | |
| 4161 | /* 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] | 4162 | if (!s->hlua) |
| 4163 | return 0; |
| 4164 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4165 | |
| 4166 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4167 | |
| 4168 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4169 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4170 | |
| 4171 | /* Get and store new value. */ |
| 4172 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4173 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4174 | |
| 4175 | return 0; |
| 4176 | } |
| 4177 | |
| 4178 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4179 | { |
| 4180 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4181 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4182 | struct hlua *hlua; |
| 4183 | |
| 4184 | /* 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] | 4185 | if (!s->hlua) { |
| 4186 | lua_pushnil(L); |
| 4187 | return 1; |
| 4188 | } |
| 4189 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4190 | |
| 4191 | /* Push configuration index in the stack. */ |
| 4192 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4193 | |
| 4194 | return 1; |
| 4195 | } |
| 4196 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4197 | /* If expected data not yet available, it returns a yield. This function |
| 4198 | * consumes the data in the buffer. It returns a string containing the |
| 4199 | * data. This string can be empty. |
| 4200 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4201 | __LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4202 | { |
| 4203 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4204 | struct stream_interface *si = appctx->appctx->owner; |
| 4205 | struct channel *req = si_oc(si); |
| 4206 | struct htx *htx; |
| 4207 | struct htx_blk *blk; |
| 4208 | size_t count; |
| 4209 | int stop = 0; |
| 4210 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4211 | htx = htx_from_buf(&req->buf); |
| 4212 | count = co_data(req); |
| 4213 | blk = htx_get_head_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4214 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4215 | while (count && !stop && blk) { |
| 4216 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4217 | uint32_t sz = htx_get_blksz(blk); |
| 4218 | struct ist v; |
| 4219 | uint32_t vlen; |
| 4220 | char *nl; |
| 4221 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4222 | if (type == HTX_BLK_EOM) { |
| 4223 | stop = 1; |
| 4224 | break; |
| 4225 | } |
| 4226 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4227 | vlen = sz; |
| 4228 | if (vlen > count) { |
| 4229 | if (type != HTX_BLK_DATA) |
| 4230 | break; |
| 4231 | vlen = count; |
| 4232 | } |
| 4233 | |
| 4234 | switch (type) { |
| 4235 | case HTX_BLK_UNUSED: |
| 4236 | break; |
| 4237 | |
| 4238 | case HTX_BLK_DATA: |
| 4239 | v = htx_get_blk_value(htx, blk); |
| 4240 | v.len = vlen; |
| 4241 | nl = istchr(v, '\n'); |
| 4242 | if (nl != NULL) { |
| 4243 | stop = 1; |
| 4244 | vlen = nl - v.ptr + 1; |
| 4245 | } |
| 4246 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4247 | break; |
| 4248 | |
| 4249 | case HTX_BLK_EOD: |
| 4250 | case HTX_BLK_TLR: |
| 4251 | case HTX_BLK_EOM: |
| 4252 | stop = 1; |
| 4253 | break; |
| 4254 | |
| 4255 | default: |
| 4256 | break; |
| 4257 | } |
| 4258 | |
| 4259 | co_set_data(req, co_data(req) - vlen); |
| 4260 | count -= vlen; |
| 4261 | if (sz == vlen) |
| 4262 | blk = htx_remove_blk(htx, blk); |
| 4263 | else { |
| 4264 | htx_cut_data_blk(htx, blk, vlen); |
| 4265 | break; |
| 4266 | } |
| 4267 | } |
| 4268 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4269 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4270 | if (!stop) { |
| 4271 | si_cant_get(si); |
| 4272 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0)); |
| 4273 | } |
| 4274 | |
| 4275 | /* return the result. */ |
| 4276 | luaL_pushresult(&appctx->b); |
| 4277 | return 1; |
| 4278 | } |
| 4279 | |
| 4280 | |
| 4281 | /* If expected data not yet available, it returns a yield. This function |
| 4282 | * consumes the data in the buffer. It returns a string containing the |
| 4283 | * data. This string can be empty. |
| 4284 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4285 | __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] | 4286 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4287 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4288 | struct stream_interface *si = appctx->appctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4289 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4290 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4291 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4292 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4293 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4294 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4295 | /* Check for the end of the data. */ |
| 4296 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4297 | luaL_pushresult(&appctx->b); |
| 4298 | return 1; |
| 4299 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4300 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4301 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4302 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4303 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4304 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4305 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4306 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4307 | 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] | 4308 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4309 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4310 | /* End of data: commit the total strings and return. */ |
| 4311 | if (ret < 0) { |
| 4312 | luaL_pushresult(&appctx->b); |
| 4313 | return 1; |
| 4314 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4315 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4316 | /* Ensure that the block 2 length is usable. */ |
| 4317 | if (ret == 1) |
| 4318 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4319 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4320 | /* Copy the fisrt block caping to the length required. */ |
| 4321 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4322 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4323 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4324 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4325 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4326 | /* Copy the second block. */ |
| 4327 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4328 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4329 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4330 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4331 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4332 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4333 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4334 | luaL_pushresult(&appctx->b); |
| 4335 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4336 | } |
| 4337 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4338 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4339 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4340 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4341 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4342 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4343 | /* Initialise the string catenation. */ |
| 4344 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4345 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4346 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 4347 | return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0)); |
| 4348 | else |
| 4349 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4350 | } |
| 4351 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4352 | /* If expected data not yet available, it returns a yield. This function |
| 4353 | * consumes the data in the buffer. It returns a string containing the |
| 4354 | * data. This string can be empty. |
| 4355 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4356 | __LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4357 | { |
| 4358 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4359 | struct stream_interface *si = appctx->appctx->owner; |
| 4360 | struct channel *req = si_oc(si); |
| 4361 | struct htx *htx; |
| 4362 | struct htx_blk *blk; |
| 4363 | size_t count; |
| 4364 | int len; |
| 4365 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4366 | htx = htx_from_buf(&req->buf); |
| 4367 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4368 | count = co_data(req); |
| 4369 | blk = htx_get_head_blk(htx); |
| 4370 | while (count && len && blk) { |
| 4371 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4372 | uint32_t sz = htx_get_blksz(blk); |
| 4373 | struct ist v; |
| 4374 | uint32_t vlen; |
| 4375 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4376 | if (type == HTX_BLK_EOM) { |
| 4377 | len = 0; |
| 4378 | break; |
| 4379 | } |
| 4380 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4381 | vlen = sz; |
| 4382 | if (len > 0 && vlen > len) |
| 4383 | vlen = len; |
| 4384 | if (vlen > count) { |
| 4385 | if (type != HTX_BLK_DATA) |
| 4386 | break; |
| 4387 | vlen = count; |
| 4388 | } |
| 4389 | |
| 4390 | switch (type) { |
| 4391 | case HTX_BLK_UNUSED: |
| 4392 | break; |
| 4393 | |
| 4394 | case HTX_BLK_DATA: |
| 4395 | v = htx_get_blk_value(htx, blk); |
| 4396 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4397 | break; |
| 4398 | |
| 4399 | case HTX_BLK_EOD: |
| 4400 | case HTX_BLK_TLR: |
| 4401 | case HTX_BLK_EOM: |
| 4402 | len = 0; |
| 4403 | break; |
| 4404 | |
| 4405 | default: |
| 4406 | break; |
| 4407 | } |
| 4408 | |
| 4409 | co_set_data(req, co_data(req) - vlen); |
| 4410 | count -= vlen; |
| 4411 | if (len > 0) |
| 4412 | len -= vlen; |
| 4413 | if (sz == vlen) |
| 4414 | blk = htx_remove_blk(htx, blk); |
| 4415 | else { |
| 4416 | htx_cut_data_blk(htx, blk, vlen); |
| 4417 | break; |
| 4418 | } |
| 4419 | } |
| 4420 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4421 | htx_to_buf(htx, &req->buf); |
| 4422 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4423 | /* If we are no other data available, yield waiting for new data. */ |
| 4424 | if (len) { |
| 4425 | if (len > 0) { |
| 4426 | lua_pushinteger(L, len); |
| 4427 | lua_replace(L, 2); |
| 4428 | } |
| 4429 | si_cant_get(si); |
| 4430 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0)); |
| 4431 | } |
| 4432 | |
| 4433 | /* return the result. */ |
| 4434 | luaL_pushresult(&appctx->b); |
| 4435 | return 1; |
| 4436 | } |
| 4437 | |
| 4438 | /* If expected data not yet available, it returns a yield. This function |
| 4439 | * consumes the data in the buffer. It returns a string containing the |
| 4440 | * data. This string can be empty. |
| 4441 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4442 | __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] | 4443 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4444 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4445 | struct stream_interface *si = appctx->appctx->owner; |
| 4446 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4447 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4448 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4449 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4450 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4451 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4452 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4453 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4454 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4455 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4456 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4457 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4458 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4459 | 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] | 4460 | } |
| 4461 | |
| 4462 | /* End of data: commit the total strings and return. */ |
| 4463 | if (ret < 0) { |
| 4464 | luaL_pushresult(&appctx->b); |
| 4465 | return 1; |
| 4466 | } |
| 4467 | |
| 4468 | /* Ensure that the block 2 length is usable. */ |
| 4469 | if (ret == 1) |
| 4470 | len2 = 0; |
| 4471 | |
| 4472 | /* Copy the fisrt block caping to the length required. */ |
| 4473 | if (len1 > len) |
| 4474 | len1 = len; |
| 4475 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4476 | len -= len1; |
| 4477 | |
| 4478 | /* Copy the second block. */ |
| 4479 | if (len2 > len) |
| 4480 | len2 = len; |
| 4481 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4482 | len -= len2; |
| 4483 | |
| 4484 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4485 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4486 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4487 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4488 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4489 | /* If we are no other data available, yield waiting for new data. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4490 | if (len > 0) { |
| 4491 | lua_pushinteger(L, len); |
| 4492 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4493 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4494 | 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] | 4495 | } |
| 4496 | |
| 4497 | /* return the result. */ |
| 4498 | luaL_pushresult(&appctx->b); |
| 4499 | return 1; |
| 4500 | } |
| 4501 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4502 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4503 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4504 | { |
| 4505 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4506 | int len = -1; |
| 4507 | |
| 4508 | /* Check arguments. */ |
| 4509 | if (lua_gettop(L) > 2) |
| 4510 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4511 | if (lua_gettop(L) >= 2) { |
| 4512 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4513 | lua_pop(L, 1); |
| 4514 | } |
| 4515 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4516 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4517 | /* HTX version */ |
| 4518 | lua_pushinteger(L, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4519 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4520 | /* Initialise the string catenation. */ |
| 4521 | luaL_buffinit(L, &appctx->b); |
| 4522 | |
| 4523 | return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0)); |
| 4524 | } |
| 4525 | else { |
| 4526 | /* Legacy HTTP version */ |
| 4527 | /* Check the required length */ |
| 4528 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4529 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4530 | lua_pushinteger(L, len); |
| 4531 | |
| 4532 | /* Initialise the string catenation. */ |
| 4533 | luaL_buffinit(L, &appctx->b); |
| 4534 | |
| 4535 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4536 | } |
| 4537 | } |
| 4538 | |
| 4539 | /* Append data in the output side of the buffer. This data is immediately |
| 4540 | * sent. The function returns the amount of data written. If the buffer |
| 4541 | * cannot contain the data, the function yields. The function returns -1 |
| 4542 | * if the channel is closed. |
| 4543 | */ |
| 4544 | __LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4545 | { |
| 4546 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4547 | struct stream_interface *si = appctx->appctx->owner; |
| 4548 | struct channel *res = si_ic(si); |
| 4549 | struct htx *htx = htx_from_buf(&res->buf); |
| 4550 | const char *data; |
| 4551 | size_t len; |
| 4552 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4553 | int max; |
| 4554 | |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4555 | max = channel_htx_recv_max(res, htx); |
| 4556 | if (!max) |
| 4557 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4558 | |
| 4559 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4560 | |
| 4561 | /* Get the max amount of data which can write as input in the channel. */ |
| 4562 | if (max > (len - l)) |
| 4563 | max = len - l; |
| 4564 | |
| 4565 | /* Copy data. */ |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4566 | if (!htx_add_data(htx, ist2(data + l, max))) |
| 4567 | goto snd_yield; |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4568 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4569 | |
| 4570 | /* update counters. */ |
| 4571 | l += max; |
| 4572 | lua_pop(L, 1); |
| 4573 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4574 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4575 | /* If some data is not send, declares the situation to the |
| 4576 | * applet, and returns a yield. |
| 4577 | */ |
| 4578 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4579 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4580 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4581 | si_rx_room_blk(si); |
| 4582 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0)); |
| 4583 | } |
| 4584 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4585 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4586 | return 1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4587 | } |
| 4588 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4589 | /* Append data in the output side of the buffer. This data is immediately |
| 4590 | * sent. The function returns the amount of data written. If the buffer |
| 4591 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4592 | * if the channel is closed. |
| 4593 | */ |
| 4594 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4595 | { |
| 4596 | size_t len; |
| 4597 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4598 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4599 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4600 | struct stream_interface *si = appctx->appctx->owner; |
| 4601 | struct channel *chn = si_ic(si); |
| 4602 | int max; |
| 4603 | |
| 4604 | /* Get the max amount of data which can write as input in the channel. */ |
| 4605 | max = channel_recv_max(chn); |
| 4606 | if (max > (len - l)) |
| 4607 | max = len - l; |
| 4608 | |
| 4609 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4610 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4611 | |
| 4612 | /* update counters. */ |
| 4613 | l += max; |
| 4614 | lua_pop(L, 1); |
| 4615 | lua_pushinteger(L, l); |
| 4616 | |
| 4617 | /* If some data is not send, declares the situation to the |
| 4618 | * applet, and returns a yield. |
| 4619 | */ |
| 4620 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4621 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4622 | 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] | 4623 | } |
| 4624 | |
| 4625 | return 1; |
| 4626 | } |
| 4627 | |
| 4628 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4629 | * yield the LUA process, and resume it without checking the |
| 4630 | * input arguments. |
| 4631 | */ |
| 4632 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4633 | { |
| 4634 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4635 | |
| 4636 | /* We want to send some data. Headers must be sent. */ |
| 4637 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4638 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4639 | WILL_LJMP(lua_error(L)); |
| 4640 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4641 | |
| 4642 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4643 | /* HTX version */ |
| 4644 | /* This interger is used for followinf the amount of data sent. */ |
| 4645 | lua_pushinteger(L, 0); |
| 4646 | |
| 4647 | return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0)); |
| 4648 | } |
| 4649 | else { |
| 4650 | /* Legacy HTTP version */ |
| 4651 | size_t len; |
| 4652 | char hex[10]; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4653 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4654 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4655 | |
| 4656 | /* If transfer encoding chunked is selected, we surround the data |
| 4657 | * by chunk data. |
| 4658 | */ |
| 4659 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4660 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4661 | lua_pushfstring(L, "%s\r\n", hex); |
| 4662 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4663 | lua_pushstring(L, "\r\n"); |
| 4664 | lua_concat(L, 3); |
| 4665 | } |
| 4666 | |
| 4667 | /* This interger is used for followinf the amount of data sent. */ |
| 4668 | lua_pushinteger(L, 0); |
| 4669 | |
| 4670 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4671 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4672 | } |
| 4673 | |
| 4674 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4675 | { |
| 4676 | const char *name; |
| 4677 | int ret; |
| 4678 | |
| 4679 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4680 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4681 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4682 | |
| 4683 | /* Push in the stack the "response" entry. */ |
| 4684 | ret = lua_getfield(L, 1, "response"); |
| 4685 | if (ret != LUA_TTABLE) { |
| 4686 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4687 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4688 | WILL_LJMP(lua_error(L)); |
| 4689 | } |
| 4690 | |
| 4691 | /* check if the header is already registered if it is not |
| 4692 | * the case, register it. |
| 4693 | */ |
| 4694 | ret = lua_getfield(L, -1, name); |
| 4695 | if (ret == LUA_TNIL) { |
| 4696 | |
| 4697 | /* Entry not found. */ |
| 4698 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4699 | |
| 4700 | /* Insert the new header name in the array in the top of the stack. |
| 4701 | * It left the new array in the top of the stack. |
| 4702 | */ |
| 4703 | lua_newtable(L); |
| 4704 | lua_pushvalue(L, 2); |
| 4705 | lua_pushvalue(L, -2); |
| 4706 | lua_settable(L, -4); |
| 4707 | |
| 4708 | } else if (ret != LUA_TTABLE) { |
| 4709 | |
| 4710 | /* corruption error. */ |
| 4711 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4712 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4713 | WILL_LJMP(lua_error(L)); |
| 4714 | } |
| 4715 | |
| 4716 | /* Now the top od thestack is an array of values. We push |
| 4717 | * the header value as new entry. |
| 4718 | */ |
| 4719 | lua_pushvalue(L, 3); |
| 4720 | ret = lua_rawlen(L, -2); |
| 4721 | lua_rawseti(L, -2, ret + 1); |
| 4722 | lua_pushboolean(L, 1); |
| 4723 | return 1; |
| 4724 | } |
| 4725 | |
| 4726 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4727 | { |
| 4728 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4729 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4730 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4731 | |
| 4732 | if (status < 100 || status > 599) { |
| 4733 | lua_pushboolean(L, 0); |
| 4734 | return 1; |
| 4735 | } |
| 4736 | |
| 4737 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4738 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4739 | lua_pushboolean(L, 1); |
| 4740 | return 1; |
| 4741 | } |
| 4742 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4743 | |
| 4744 | __LJMP static int hlua_applet_htx_send_response(lua_State *L) |
| 4745 | { |
| 4746 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4747 | struct stream_interface *si = appctx->appctx->owner; |
| 4748 | struct channel *res = si_ic(si); |
| 4749 | struct htx *htx; |
| 4750 | struct htx_sl *sl; |
| 4751 | struct h1m h1m; |
| 4752 | const char *status, *reason; |
| 4753 | const char *name, *value; |
| 4754 | size_t nlen, vlen; |
| 4755 | unsigned int flags; |
| 4756 | |
| 4757 | /* Send the message at once. */ |
| 4758 | htx = htx_from_buf(&res->buf); |
| 4759 | h1m_init_res(&h1m); |
| 4760 | |
| 4761 | /* Use the same http version than the request. */ |
| 4762 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4763 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4764 | if (reason == NULL) |
| 4765 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4766 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4767 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4768 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4769 | } |
| 4770 | else { |
| 4771 | flags = HTX_SL_F_IS_RESP; |
| 4772 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4773 | } |
| 4774 | if (!sl) { |
| 4775 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4776 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4777 | WILL_LJMP(lua_error(L)); |
| 4778 | } |
| 4779 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4780 | |
| 4781 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4782 | lua_pushvalue(L, 0); |
| 4783 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4784 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4785 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4786 | WILL_LJMP(lua_error(L)); |
| 4787 | } |
| 4788 | |
| 4789 | /* Browse the list of headers. */ |
| 4790 | lua_pushnil(L); |
| 4791 | while(lua_next(L, -2) != 0) { |
| 4792 | /* We expect a string as -2. */ |
| 4793 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4794 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4795 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4796 | lua_typename(L, lua_type(L, -2))); |
| 4797 | WILL_LJMP(lua_error(L)); |
| 4798 | } |
| 4799 | name = lua_tolstring(L, -2, &nlen); |
| 4800 | |
| 4801 | /* We expect an array as -1. */ |
| 4802 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4803 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4804 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4805 | name, |
| 4806 | lua_typename(L, lua_type(L, -1))); |
| 4807 | WILL_LJMP(lua_error(L)); |
| 4808 | } |
| 4809 | |
| 4810 | /* Browse the table who is on the top of the stack. */ |
| 4811 | lua_pushnil(L); |
| 4812 | while(lua_next(L, -2) != 0) { |
| 4813 | int id; |
| 4814 | |
| 4815 | /* We expect a number as -2. */ |
| 4816 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4817 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4818 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4819 | name, |
| 4820 | lua_typename(L, lua_type(L, -2))); |
| 4821 | WILL_LJMP(lua_error(L)); |
| 4822 | } |
| 4823 | id = lua_tointeger(L, -2); |
| 4824 | |
| 4825 | /* We expect a string as -2. */ |
| 4826 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4827 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4828 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4829 | name, id, |
| 4830 | lua_typename(L, lua_type(L, -1))); |
| 4831 | WILL_LJMP(lua_error(L)); |
| 4832 | } |
| 4833 | value = lua_tolstring(L, -1, &vlen); |
| 4834 | |
| 4835 | /* Simple Protocol checks. */ |
| 4836 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
| 4837 | h1_parse_xfer_enc_header(&h1m, ist2(name, nlen)); |
| 4838 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4839 | struct ist v = ist2(value, vlen); |
| 4840 | int ret; |
| 4841 | |
| 4842 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4843 | if (ret < 0) { |
| 4844 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4845 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4846 | name); |
| 4847 | WILL_LJMP(lua_error(L)); |
| 4848 | } |
| 4849 | else if (ret == 0) |
| 4850 | goto next; /* Skip it */ |
| 4851 | } |
| 4852 | |
| 4853 | /* Add a new header */ |
| 4854 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4855 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4856 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4857 | name); |
| 4858 | WILL_LJMP(lua_error(L)); |
| 4859 | } |
| 4860 | next: |
| 4861 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4862 | lua_pop(L, 1); |
| 4863 | } |
| 4864 | |
| 4865 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4866 | lua_pop(L, 1); |
| 4867 | } |
| 4868 | |
| 4869 | if (h1m.flags & H1_MF_CHNK) |
| 4870 | h1m.flags &= ~H1_MF_CLEN; |
| 4871 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4872 | h1m.flags |= H1_MF_XFER_LEN; |
| 4873 | |
| 4874 | /* Uset HTX start-line flags */ |
| 4875 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4876 | flags |= HTX_SL_F_XFER_ENC; |
| 4877 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4878 | flags |= HTX_SL_F_XFER_LEN; |
| 4879 | if (h1m.flags & H1_MF_CHNK) |
| 4880 | flags |= HTX_SL_F_CHNK; |
| 4881 | else if (h1m.flags & H1_MF_CLEN) |
| 4882 | flags |= HTX_SL_F_CLEN; |
| 4883 | if (h1m.body_len == 0) |
| 4884 | flags |= HTX_SL_F_BODYLESS; |
| 4885 | } |
| 4886 | sl->flags |= flags; |
| 4887 | |
| 4888 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4889 | * and the status code implies the presence of a message body, we must |
| 4890 | * announce a transfer encoding chunked. This is required by haproxy |
| 4891 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4892 | * chunked itslef, don't do anything. |
| 4893 | */ |
| 4894 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4895 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4896 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4897 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4898 | /* Add a new header */ |
| 4899 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4900 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4901 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4902 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4903 | WILL_LJMP(lua_error(L)); |
| 4904 | } |
| 4905 | } |
| 4906 | |
| 4907 | /* Finalize headers. */ |
| 4908 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4909 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4910 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4911 | WILL_LJMP(lua_error(L)); |
| 4912 | } |
| 4913 | |
| 4914 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4915 | b_reset(&res->buf); |
| 4916 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4917 | WILL_LJMP(lua_error(L)); |
| 4918 | } |
| 4919 | |
| 4920 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4921 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4922 | |
| 4923 | /* Headers sent, set the flag. */ |
| 4924 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4925 | return 0; |
| 4926 | |
| 4927 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4928 | /* We will build the status line and the headers of the HTTP response. |
| 4929 | * We will try send at once if its not possible, we give back the hand |
| 4930 | * waiting for more room. |
| 4931 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4932 | __LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4933 | { |
| 4934 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4935 | struct stream_interface *si = appctx->appctx->owner; |
| 4936 | struct channel *res = si_ic(si); |
| 4937 | |
| 4938 | if (co_data(res)) { |
| 4939 | si_rx_room_blk(si); |
| 4940 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0)); |
| 4941 | } |
| 4942 | return MAY_LJMP(hlua_applet_htx_send_response(L)); |
| 4943 | } |
| 4944 | |
| 4945 | |
| 4946 | __LJMP static int hlua_applet_htx_start_response(lua_State *L) |
| 4947 | { |
| 4948 | return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0)); |
| 4949 | } |
| 4950 | |
| 4951 | /* We will build the status line and the headers of the HTTP response. |
| 4952 | * We will try send at once if its not possible, we give back the hand |
| 4953 | * waiting for more room. |
| 4954 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4955 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4956 | { |
| 4957 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4958 | struct stream_interface *si = appctx->appctx->owner; |
| 4959 | struct channel *chn = si_ic(si); |
| 4960 | int ret; |
| 4961 | size_t len; |
| 4962 | const char *msg; |
| 4963 | |
| 4964 | /* Get the message as the first argument on the stack. */ |
| 4965 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4966 | |
| 4967 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4968 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4969 | |
| 4970 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4971 | * big for the buffers. |
| 4972 | */ |
| 4973 | if (ret == -2 || ret == -3) { |
| 4974 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4975 | WILL_LJMP(lua_error(L)); |
| 4976 | } |
| 4977 | |
| 4978 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4979 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4980 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4981 | 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] | 4982 | } |
| 4983 | |
| 4984 | /* Headers sent, set the flag. */ |
| 4985 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4986 | return 0; |
| 4987 | } |
| 4988 | |
| 4989 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4990 | { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4991 | struct buffer *tmp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4992 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4993 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4994 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4995 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4996 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4997 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4998 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4999 | int hdr_chunked = 0; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5000 | const char *reason; |
| 5001 | |
| 5002 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 5003 | return MAY_LJMP(hlua_applet_htx_start_response(L)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5004 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5005 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5006 | if (reason == NULL) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 5007 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5008 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5009 | tmp = get_trash_chunk(); |
| 5010 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5011 | /* Use the same http version than the request. */ |
| 5012 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 5013 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5014 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5015 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5016 | |
| 5017 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5018 | lua_pushvalue(L, 0); |
| 5019 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5020 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 5021 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 5022 | WILL_LJMP(lua_error(L)); |
| 5023 | } |
| 5024 | |
| 5025 | /* Browse the list of headers. */ |
| 5026 | lua_pushnil(L); |
| 5027 | while(lua_next(L, -2) != 0) { |
| 5028 | |
| 5029 | /* We expect a string as -2. */ |
| 5030 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5031 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 5032 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5033 | lua_typename(L, lua_type(L, -2))); |
| 5034 | WILL_LJMP(lua_error(L)); |
| 5035 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5036 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5037 | |
| 5038 | /* We expect an array as -1. */ |
| 5039 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5040 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 5041 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5042 | name, |
| 5043 | lua_typename(L, lua_type(L, -1))); |
| 5044 | WILL_LJMP(lua_error(L)); |
| 5045 | } |
| 5046 | |
| 5047 | /* Browse the table who is on the top of the stack. */ |
| 5048 | lua_pushnil(L); |
| 5049 | while(lua_next(L, -2) != 0) { |
| 5050 | |
| 5051 | /* We expect a number as -2. */ |
| 5052 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5053 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 5054 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5055 | name, |
| 5056 | lua_typename(L, lua_type(L, -2))); |
| 5057 | WILL_LJMP(lua_error(L)); |
| 5058 | } |
| 5059 | id = lua_tointeger(L, -2); |
| 5060 | |
| 5061 | /* We expect a string as -2. */ |
| 5062 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5063 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 5064 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5065 | name, id, |
| 5066 | lua_typename(L, lua_type(L, -1))); |
| 5067 | WILL_LJMP(lua_error(L)); |
| 5068 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5069 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5070 | |
| 5071 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5072 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 5073 | memcpy(tmp->area + tmp->data, name, name_len); |
| 5074 | tmp->data += name_len; |
| 5075 | tmp->area[tmp->data++] = ':'; |
| 5076 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5077 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5078 | memcpy(tmp->area + tmp->data, value, |
| 5079 | value_len); |
| 5080 | tmp->data += value_len; |
| 5081 | tmp->area[tmp->data++] = '\r'; |
| 5082 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5083 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5084 | |
| 5085 | /* Protocol checks. */ |
| 5086 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5087 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5088 | * is done without control. If it contains a bad value, |
| 5089 | * the content-length remains negative so that we can |
| 5090 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5091 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5092 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5093 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5094 | |
| 5095 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5096 | if (name_len == 17 && value_len == 7 && |
| 5097 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5098 | strcasecmp("chunked", value) == 0) |
| 5099 | hdr_chunked = 1; |
| 5100 | |
| 5101 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5102 | lua_pop(L, 1); |
| 5103 | } |
| 5104 | |
| 5105 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5106 | lua_pop(L, 1); |
| 5107 | } |
| 5108 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5109 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 5110 | * and the status code implies the presence of a message body, we must |
| 5111 | * announce a transfer encoding chunked. This is required by haproxy |
| 5112 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 5113 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5114 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5115 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5116 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 5117 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5118 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5119 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5120 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 5121 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 5122 | } |
| 5123 | |
| 5124 | /* Finalize headers. */ |
| 5125 | chunk_appendf(tmp, "\r\n"); |
| 5126 | |
| 5127 | /* Remove the last entry and the array of headers */ |
| 5128 | lua_pop(L, 2); |
| 5129 | |
| 5130 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5131 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5132 | |
| 5133 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 5134 | } |
| 5135 | |
| 5136 | /* |
| 5137 | * |
| 5138 | * |
| 5139 | * Class HTTP |
| 5140 | * |
| 5141 | * |
| 5142 | */ |
| 5143 | |
| 5144 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5145 | * a class stream, otherwise it throws an error. |
| 5146 | */ |
| 5147 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 5148 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5149 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5150 | } |
| 5151 | |
| 5152 | /* This function creates and push in the stack a HTTP object |
| 5153 | * according with a current TXN. |
| 5154 | */ |
| 5155 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5156 | { |
| 5157 | struct hlua_txn *htxn; |
| 5158 | |
| 5159 | /* Check stack size. */ |
| 5160 | if (!lua_checkstack(L, 3)) |
| 5161 | return 0; |
| 5162 | |
| 5163 | /* Create the object: obj[0] = userdata. |
| 5164 | * Note that the base of the Converters object is the |
| 5165 | * same than the TXN object. |
| 5166 | */ |
| 5167 | lua_newtable(L); |
| 5168 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5169 | lua_rawseti(L, -2, 0); |
| 5170 | |
| 5171 | htxn->s = txn->s; |
| 5172 | htxn->p = txn->p; |
| 5173 | |
| 5174 | /* Pop a class stream metatable and affect it to the table. */ |
| 5175 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5176 | lua_setmetatable(L, -2); |
| 5177 | |
| 5178 | return 1; |
| 5179 | } |
| 5180 | |
| 5181 | /* This function creates ans returns an array of HTTP headers. |
| 5182 | * This function does not fails. It is used as wrapper with the |
| 5183 | * 2 following functions. |
| 5184 | */ |
| 5185 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5186 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5187 | /* Create the table. */ |
| 5188 | lua_newtable(L); |
| 5189 | |
| 5190 | if (!htxn->s->txn) |
| 5191 | return 1; |
| 5192 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5193 | if (IS_HTX_STRM(htxn->s)) { |
| 5194 | /* HTX version */ |
| 5195 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5196 | int32_t pos; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5197 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5198 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5199 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5200 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5201 | struct ist n, v; |
| 5202 | int len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5203 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5204 | if (type == HTX_BLK_HDR) { |
| 5205 | n = htx_get_blk_name(htx,blk); |
| 5206 | v = htx_get_blk_value(htx, blk); |
| 5207 | } |
| 5208 | else if (type == HTX_BLK_EOH) |
| 5209 | break; |
| 5210 | else |
| 5211 | continue; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5212 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5213 | /* Check for existing entry: |
| 5214 | * assume that the table is on the top of the stack, and |
| 5215 | * push the key in the stack, the function lua_gettable() |
| 5216 | * perform the lookup. |
| 5217 | */ |
| 5218 | lua_pushlstring(L, n.ptr, n.len); |
| 5219 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5220 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5221 | switch (lua_type(L, -1)) { |
| 5222 | case LUA_TNIL: |
| 5223 | /* Table not found, create it. */ |
| 5224 | lua_pop(L, 1); /* remove the nil value. */ |
| 5225 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5226 | lua_newtable(L); /* create and push empty table. */ |
| 5227 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5228 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5229 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5230 | break; |
| 5231 | |
| 5232 | case LUA_TTABLE: |
| 5233 | /* Entry found: push the value in the table. */ |
| 5234 | len = lua_rawlen(L, -1); |
| 5235 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5236 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5237 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5238 | break; |
| 5239 | |
| 5240 | default: |
| 5241 | /* Other cases are errors. */ |
| 5242 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5243 | WILL_LJMP(lua_error(L)); |
| 5244 | } |
| 5245 | } |
| 5246 | } |
| 5247 | else { |
| 5248 | /* Legacy HTTP version */ |
| 5249 | const char *cur_ptr, *cur_next, *p; |
| 5250 | int old_idx, cur_idx; |
| 5251 | struct hdr_idx_elem *cur_hdr; |
| 5252 | const char *hn, *hv; |
| 5253 | int hnl, hvl; |
| 5254 | const char *in; |
| 5255 | char *out; |
| 5256 | int len; |
| 5257 | |
| 5258 | /* Build array of headers. */ |
| 5259 | old_idx = 0; |
| 5260 | cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 5261 | |
| 5262 | while (1) { |
| 5263 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 5264 | if (!cur_idx) |
| 5265 | break; |
| 5266 | old_idx = cur_idx; |
| 5267 | |
| 5268 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 5269 | cur_ptr = cur_next; |
| 5270 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 5271 | |
| 5272 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 5273 | * and the next header starts at cur_next. We'll check |
| 5274 | * this header in the list as well as against the default |
| 5275 | * rule. |
| 5276 | */ |
| 5277 | |
| 5278 | /* look for ': *'. */ |
| 5279 | hn = cur_ptr; |
| 5280 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 5281 | if (p >= cur_ptr+cur_hdr->len) |
| 5282 | continue; |
| 5283 | hnl = p - hn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5284 | p++; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5285 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 5286 | p++; |
| 5287 | if (p >= cur_ptr+cur_hdr->len) |
| 5288 | continue; |
| 5289 | hv = p; |
| 5290 | hvl = cur_ptr+cur_hdr->len-p; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5291 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5292 | /* Lowercase the key. Don't check the size of trash, it have |
| 5293 | * the size of one buffer and the input data contains in one |
| 5294 | * buffer. |
| 5295 | */ |
| 5296 | out = trash.area; |
| 5297 | for (in=hn; in<hn+hnl; in++, out++) |
| 5298 | *out = tolower(*in); |
| 5299 | *out = '\0'; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5300 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5301 | /* Check for existing entry: |
| 5302 | * assume that the table is on the top of the stack, and |
| 5303 | * push the key in the stack, the function lua_gettable() |
| 5304 | * perform the lookup. |
| 5305 | */ |
| 5306 | lua_pushlstring(L, trash.area, hnl); |
| 5307 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5308 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5309 | switch (lua_type(L, -1)) { |
| 5310 | case LUA_TNIL: |
| 5311 | /* Table not found, create it. */ |
| 5312 | lua_pop(L, 1); /* remove the nil value. */ |
| 5313 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
| 5314 | lua_newtable(L); /* create and push empty table. */ |
| 5315 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5316 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5317 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5318 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5319 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5320 | case LUA_TTABLE: |
| 5321 | /* Entry found: push the value in the table. */ |
| 5322 | len = lua_rawlen(L, -1); |
| 5323 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5324 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5325 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5326 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5327 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5328 | default: |
| 5329 | /* Other cases are errors. */ |
| 5330 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5331 | WILL_LJMP(lua_error(L)); |
| 5332 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5333 | } |
| 5334 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5335 | return 1; |
| 5336 | } |
| 5337 | |
| 5338 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5339 | { |
| 5340 | struct hlua_txn *htxn; |
| 5341 | |
| 5342 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5343 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5344 | |
| 5345 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 5346 | } |
| 5347 | |
| 5348 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5349 | { |
| 5350 | struct hlua_txn *htxn; |
| 5351 | |
| 5352 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5353 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5354 | |
| 5355 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 5356 | } |
| 5357 | |
| 5358 | /* This function replace full header, or just a value in |
| 5359 | * the request or in the response. It is a wrapper fir the |
| 5360 | * 4 following functions. |
| 5361 | */ |
| 5362 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 5363 | struct http_msg *msg, int action) |
| 5364 | { |
| 5365 | size_t name_len; |
| 5366 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5367 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5368 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5369 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5370 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5371 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5372 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5373 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5374 | http_transform_header_str(htxn->s, msg, name, name_len, value, re, action); |
| 5375 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5376 | return 0; |
| 5377 | } |
| 5378 | |
| 5379 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5380 | { |
| 5381 | struct hlua_txn *htxn; |
| 5382 | |
| 5383 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5384 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5385 | |
| 5386 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 5387 | } |
| 5388 | |
| 5389 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5390 | { |
| 5391 | struct hlua_txn *htxn; |
| 5392 | |
| 5393 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5394 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5395 | |
| 5396 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 5397 | } |
| 5398 | |
| 5399 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5400 | { |
| 5401 | struct hlua_txn *htxn; |
| 5402 | |
| 5403 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5404 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5405 | |
| 5406 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 5407 | } |
| 5408 | |
| 5409 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5410 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5411 | struct hlua_txn *htxn; |
| 5412 | |
| 5413 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5414 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5415 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 5416 | 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] | 5417 | } |
| 5418 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5419 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5420 | * It is a wrapper for the 2 following functions. |
| 5421 | */ |
| 5422 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5423 | { |
| 5424 | size_t len; |
| 5425 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5426 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5427 | if (IS_HTX_STRM(htxn->s)) { |
| 5428 | /* HTX version */ |
| 5429 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5430 | struct http_hdr_ctx ctx; |
| 5431 | |
| 5432 | ctx.blk = NULL; |
| 5433 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5434 | http_remove_header(htx, &ctx); |
| 5435 | } |
| 5436 | else { |
| 5437 | /* Legacy HTTP version */ |
| 5438 | struct hdr_ctx ctx; |
| 5439 | struct http_txn *txn = htxn->s->txn; |
| 5440 | |
| 5441 | ctx.idx = 0; |
| 5442 | while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) |
| 5443 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5444 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5445 | return 0; |
| 5446 | } |
| 5447 | |
| 5448 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5449 | { |
| 5450 | struct hlua_txn *htxn; |
| 5451 | |
| 5452 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5453 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5454 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5455 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5456 | } |
| 5457 | |
| 5458 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5459 | { |
| 5460 | struct hlua_txn *htxn; |
| 5461 | |
| 5462 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5463 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5464 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5465 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5466 | } |
| 5467 | |
| 5468 | /* This function adds an header. It is a wrapper used by |
| 5469 | * the 2 following functions. |
| 5470 | */ |
| 5471 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5472 | { |
| 5473 | size_t name_len; |
| 5474 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5475 | size_t value_len; |
| 5476 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 5477 | char *p; |
| 5478 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5479 | if (IS_HTX_STRM(htxn->s)) { |
| 5480 | /* HTX version */ |
| 5481 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5482 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5483 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5484 | ist2(value, value_len))); |
| 5485 | } |
| 5486 | else { |
| 5487 | /* Legacy HTTP version */ |
| 5488 | /* Check length. */ |
| 5489 | trash.data = value_len + name_len + 2; |
| 5490 | if (trash.data > trash.size) |
| 5491 | return 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5492 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5493 | /* Creates the header string. */ |
| 5494 | p = trash.area; |
| 5495 | memcpy(p, name, name_len); |
| 5496 | p += name_len; |
| 5497 | *p = ':'; |
| 5498 | p++; |
| 5499 | *p = ' '; |
| 5500 | p++; |
| 5501 | memcpy(p, value, value_len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5502 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5503 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
| 5504 | trash.area, trash.data) != 0); |
| 5505 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5506 | return 0; |
| 5507 | } |
| 5508 | |
| 5509 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5510 | { |
| 5511 | struct hlua_txn *htxn; |
| 5512 | |
| 5513 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5514 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5515 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5516 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5517 | } |
| 5518 | |
| 5519 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5520 | { |
| 5521 | struct hlua_txn *htxn; |
| 5522 | |
| 5523 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5524 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5525 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5526 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5527 | } |
| 5528 | |
| 5529 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5530 | { |
| 5531 | struct hlua_txn *htxn; |
| 5532 | |
| 5533 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5534 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5535 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5536 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 5537 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5538 | } |
| 5539 | |
| 5540 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5541 | { |
| 5542 | struct hlua_txn *htxn; |
| 5543 | |
| 5544 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5545 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5546 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5547 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 5548 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5549 | } |
| 5550 | |
| 5551 | /* This function set the method. */ |
| 5552 | static int hlua_http_req_set_meth(lua_State *L) |
| 5553 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5554 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5555 | size_t name_len; |
| 5556 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5557 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5558 | 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] | 5559 | return 1; |
| 5560 | } |
| 5561 | |
| 5562 | /* This function set the method. */ |
| 5563 | static int hlua_http_req_set_path(lua_State *L) |
| 5564 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5565 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5566 | size_t name_len; |
| 5567 | 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] | 5568 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5569 | 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] | 5570 | return 1; |
| 5571 | } |
| 5572 | |
| 5573 | /* This function set the query-string. */ |
| 5574 | static int hlua_http_req_set_query(lua_State *L) |
| 5575 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5576 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5577 | size_t name_len; |
| 5578 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5579 | |
| 5580 | /* Check length. */ |
| 5581 | if (name_len > trash.size - 1) { |
| 5582 | lua_pushboolean(L, 0); |
| 5583 | return 1; |
| 5584 | } |
| 5585 | |
| 5586 | /* Add the mark question as prefix. */ |
| 5587 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5588 | trash.area[trash.data++] = '?'; |
| 5589 | memcpy(trash.area + trash.data, name, name_len); |
| 5590 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5591 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5592 | lua_pushboolean(L, |
| 5593 | 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] | 5594 | return 1; |
| 5595 | } |
| 5596 | |
| 5597 | /* This function set the uri. */ |
| 5598 | static int hlua_http_req_set_uri(lua_State *L) |
| 5599 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5600 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5601 | size_t name_len; |
| 5602 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5603 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5604 | 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] | 5605 | return 1; |
| 5606 | } |
| 5607 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5608 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5609 | static int hlua_http_res_set_status(lua_State *L) |
| 5610 | { |
| 5611 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5612 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5613 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5614 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5615 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5616 | return 0; |
| 5617 | } |
| 5618 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5619 | /* |
| 5620 | * |
| 5621 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5622 | * Class TXN |
| 5623 | * |
| 5624 | * |
| 5625 | */ |
| 5626 | |
| 5627 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5628 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5629 | */ |
| 5630 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5631 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5632 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5633 | } |
| 5634 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5635 | __LJMP static int hlua_set_var(lua_State *L) |
| 5636 | { |
| 5637 | struct hlua_txn *htxn; |
| 5638 | const char *name; |
| 5639 | size_t len; |
| 5640 | struct sample smp; |
| 5641 | |
| 5642 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5643 | |
| 5644 | /* It is useles to retrieve the stream, but this function |
| 5645 | * runs only in a stream context. |
| 5646 | */ |
| 5647 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5648 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5649 | |
| 5650 | /* Converts the third argument in a sample. */ |
| 5651 | hlua_lua2smp(L, 3, &smp); |
| 5652 | |
| 5653 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5654 | 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] | 5655 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5656 | return 0; |
| 5657 | } |
| 5658 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5659 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5660 | { |
| 5661 | struct hlua_txn *htxn; |
| 5662 | const char *name; |
| 5663 | size_t len; |
| 5664 | struct sample smp; |
| 5665 | |
| 5666 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5667 | |
| 5668 | /* It is useles to retrieve the stream, but this function |
| 5669 | * runs only in a stream context. |
| 5670 | */ |
| 5671 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5672 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5673 | |
| 5674 | /* Unset the variable. */ |
| 5675 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5676 | vars_unset_by_name(name, len, &smp); |
| 5677 | return 0; |
| 5678 | } |
| 5679 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5680 | __LJMP static int hlua_get_var(lua_State *L) |
| 5681 | { |
| 5682 | struct hlua_txn *htxn; |
| 5683 | const char *name; |
| 5684 | size_t len; |
| 5685 | struct sample smp; |
| 5686 | |
| 5687 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5688 | |
| 5689 | /* It is useles to retrieve the stream, but this function |
| 5690 | * runs only in a stream context. |
| 5691 | */ |
| 5692 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5693 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5694 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5695 | 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] | 5696 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5697 | lua_pushnil(L); |
| 5698 | return 1; |
| 5699 | } |
| 5700 | |
| 5701 | return hlua_smp2lua(L, &smp); |
| 5702 | } |
| 5703 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5704 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5705 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5706 | struct hlua *hlua; |
| 5707 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5708 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5709 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5710 | /* It is useles to retrieve the stream, but this function |
| 5711 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5712 | */ |
| 5713 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5714 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5715 | |
| 5716 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5717 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5718 | |
| 5719 | /* Get and store new value. */ |
| 5720 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5721 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5722 | |
| 5723 | return 0; |
| 5724 | } |
| 5725 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5726 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5727 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5728 | struct hlua *hlua; |
| 5729 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5730 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5731 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5732 | /* It is useles to retrieve the stream, but this function |
| 5733 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5734 | */ |
| 5735 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5736 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5737 | |
| 5738 | /* Push configuration index in the stack. */ |
| 5739 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5740 | |
| 5741 | return 1; |
| 5742 | } |
| 5743 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5744 | /* Create stack entry containing a class TXN. This function |
| 5745 | * return 0 if the stack does not contains free slots, |
| 5746 | * otherwise it returns 1. |
| 5747 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5748 | 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] | 5749 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5750 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5751 | |
| 5752 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5753 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5754 | return 0; |
| 5755 | |
| 5756 | /* NOTE: The allocation never fails. The failure |
| 5757 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5758 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5759 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5760 | /* Create the object: obj[0] = userdata. */ |
| 5761 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5762 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5763 | lua_rawseti(L, -2, 0); |
| 5764 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5765 | htxn->s = s; |
| 5766 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5767 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5768 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5769 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5770 | /* Create the "f" field that contains a list of fetches. */ |
| 5771 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5772 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5773 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5774 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5775 | |
| 5776 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5777 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5778 | 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] | 5779 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5780 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5781 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5782 | /* Create the "c" field that contains a list of converters. */ |
| 5783 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5784 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5785 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5786 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5787 | |
| 5788 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5789 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5790 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5791 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5792 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5793 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5794 | /* Create the "req" field that contains the request channel object. */ |
| 5795 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5796 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5797 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5798 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5799 | |
| 5800 | /* Create the "res" field that contains the response channel object. */ |
| 5801 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5802 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5803 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5804 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5805 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5806 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5807 | lua_pushstring(L, "http"); |
| 5808 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5809 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5810 | return 0; |
| 5811 | } |
| 5812 | else |
| 5813 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5814 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5815 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5816 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5817 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5818 | lua_setmetatable(L, -2); |
| 5819 | |
| 5820 | return 1; |
| 5821 | } |
| 5822 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5823 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5824 | { |
| 5825 | const char *msg; |
| 5826 | struct hlua_txn *htxn; |
| 5827 | |
| 5828 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5829 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5830 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5831 | |
| 5832 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5833 | return 0; |
| 5834 | } |
| 5835 | |
| 5836 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5837 | { |
| 5838 | int level; |
| 5839 | const char *msg; |
| 5840 | struct hlua_txn *htxn; |
| 5841 | |
| 5842 | MAY_LJMP(check_args(L, 3, "log")); |
| 5843 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5844 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5845 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5846 | |
| 5847 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5848 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5849 | |
| 5850 | hlua_sendlog(htxn->s->be, level, msg); |
| 5851 | return 0; |
| 5852 | } |
| 5853 | |
| 5854 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5855 | { |
| 5856 | const char *msg; |
| 5857 | struct hlua_txn *htxn; |
| 5858 | |
| 5859 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5860 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5861 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5862 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5863 | return 0; |
| 5864 | } |
| 5865 | |
| 5866 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5867 | { |
| 5868 | const char *msg; |
| 5869 | struct hlua_txn *htxn; |
| 5870 | |
| 5871 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5872 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5873 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5874 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5875 | return 0; |
| 5876 | } |
| 5877 | |
| 5878 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5879 | { |
| 5880 | const char *msg; |
| 5881 | struct hlua_txn *htxn; |
| 5882 | |
| 5883 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5884 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5885 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5886 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5887 | return 0; |
| 5888 | } |
| 5889 | |
| 5890 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5891 | { |
| 5892 | const char *msg; |
| 5893 | struct hlua_txn *htxn; |
| 5894 | |
| 5895 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5896 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5897 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5898 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5899 | return 0; |
| 5900 | } |
| 5901 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5902 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5903 | { |
| 5904 | struct hlua_txn *htxn; |
| 5905 | int ll; |
| 5906 | |
| 5907 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5908 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5909 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5910 | |
| 5911 | if (ll < 0 || ll > 7) |
| 5912 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5913 | |
| 5914 | htxn->s->logs.level = ll; |
| 5915 | return 0; |
| 5916 | } |
| 5917 | |
| 5918 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5919 | { |
| 5920 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5921 | int tos; |
| 5922 | |
| 5923 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5924 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5925 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5926 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5927 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5928 | return 0; |
| 5929 | } |
| 5930 | |
| 5931 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5932 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5933 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5934 | int mark; |
| 5935 | |
| 5936 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5937 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5938 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5939 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5940 | conn_set_tos(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5941 | return 0; |
| 5942 | } |
| 5943 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5944 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5945 | { |
| 5946 | struct hlua_txn *htxn; |
| 5947 | |
| 5948 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5949 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5950 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5951 | return 0; |
| 5952 | } |
| 5953 | |
| 5954 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5955 | { |
| 5956 | struct hlua_txn *htxn; |
| 5957 | |
| 5958 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5959 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5960 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5961 | return 0; |
| 5962 | } |
| 5963 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5964 | /* This function is an Lua binding that send pending data |
| 5965 | * to the client, and close the stream interface. |
| 5966 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5967 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5968 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5969 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5970 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5971 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5972 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5973 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5974 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5975 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5976 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5977 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5978 | * session, so we just end the execution of the current |
| 5979 | * lua code. |
| 5980 | */ |
| 5981 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5982 | WILL_LJMP(hlua_done(L)); |
| 5983 | return 0; |
| 5984 | } |
| 5985 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5986 | ic = &htxn->s->req; |
| 5987 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5988 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5989 | if (htxn->s->txn) { |
| 5990 | /* HTTP mode, let's stay in sync with the stream */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5991 | b_del(&ic->buf, htxn->s->txn->req.sov); |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5992 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 5993 | htxn->s->txn->req.sov = 0; |
| 5994 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 5995 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 5996 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5997 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 5998 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5999 | /* Note that if we want to support keep-alive, we need |
| 6000 | * to bypass the close/shutr_now calls below, but that |
| 6001 | * may only be done if the HTTP request was already |
| 6002 | * processed and the connection header is known (ie |
| 6003 | * not during TCP rules). |
| 6004 | */ |
| 6005 | } |
| 6006 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6007 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6008 | channel_abort(ic); |
| 6009 | channel_auto_close(ic); |
| 6010 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6011 | |
| 6012 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6013 | channel_auto_read(oc); |
| 6014 | channel_auto_close(oc); |
| 6015 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6016 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 6017 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6018 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6019 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6020 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6021 | return 0; |
| 6022 | } |
| 6023 | |
| 6024 | __LJMP static int hlua_log(lua_State *L) |
| 6025 | { |
| 6026 | int level; |
| 6027 | const char *msg; |
| 6028 | |
| 6029 | MAY_LJMP(check_args(L, 2, "log")); |
| 6030 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6031 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6032 | |
| 6033 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6034 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6035 | |
| 6036 | hlua_sendlog(NULL, level, msg); |
| 6037 | return 0; |
| 6038 | } |
| 6039 | |
| 6040 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6041 | { |
| 6042 | const char *msg; |
| 6043 | |
| 6044 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6045 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6046 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6047 | return 0; |
| 6048 | } |
| 6049 | |
| 6050 | __LJMP static int hlua_log_info(lua_State *L) |
| 6051 | { |
| 6052 | const char *msg; |
| 6053 | |
| 6054 | MAY_LJMP(check_args(L, 1, "info")); |
| 6055 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6056 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6057 | return 0; |
| 6058 | } |
| 6059 | |
| 6060 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6061 | { |
| 6062 | const char *msg; |
| 6063 | |
| 6064 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6065 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6066 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6067 | return 0; |
| 6068 | } |
| 6069 | |
| 6070 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6071 | { |
| 6072 | const char *msg; |
| 6073 | |
| 6074 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6075 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6076 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6077 | return 0; |
| 6078 | } |
| 6079 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6080 | __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] | 6081 | { |
| 6082 | int wakeup_ms = lua_tointeger(L, -1); |
| 6083 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6084 | 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] | 6085 | return 0; |
| 6086 | } |
| 6087 | |
| 6088 | __LJMP static int hlua_sleep(lua_State *L) |
| 6089 | { |
| 6090 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6091 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6092 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6093 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6094 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6095 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6096 | wakeup_ms = tick_add(now_ms, delay); |
| 6097 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6098 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6099 | 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] | 6100 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6101 | } |
| 6102 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6103 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6104 | { |
| 6105 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6106 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6107 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6108 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6109 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6110 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6111 | wakeup_ms = tick_add(now_ms, delay); |
| 6112 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6113 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6114 | 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] | 6115 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6116 | } |
| 6117 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6118 | /* This functionis an LUA binding. it permits to give back |
| 6119 | * the hand at the HAProxy scheduler. It is used when the |
| 6120 | * LUA processing consumes a lot of time. |
| 6121 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6122 | __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] | 6123 | { |
| 6124 | return 0; |
| 6125 | } |
| 6126 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6127 | __LJMP static int hlua_yield(lua_State *L) |
| 6128 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6129 | 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] | 6130 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6131 | } |
| 6132 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6133 | /* This function change the nice of the currently executed |
| 6134 | * task. It is used set low or high priority at the current |
| 6135 | * task. |
| 6136 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6137 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6138 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6139 | struct hlua *hlua; |
| 6140 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6141 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6142 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6143 | hlua = hlua_gethlua(L); |
| 6144 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6145 | |
| 6146 | /* If he task is not set, I'm in a start mode. */ |
| 6147 | if (!hlua || !hlua->task) |
| 6148 | return 0; |
| 6149 | |
| 6150 | if (nice < -1024) |
| 6151 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6152 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6153 | nice = 1024; |
| 6154 | |
| 6155 | hlua->task->nice = nice; |
| 6156 | return 0; |
| 6157 | } |
| 6158 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6159 | /* 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] | 6160 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6161 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6162 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6163 | * |
| 6164 | * Task wrapper are longjmp safe because the only one Lua code |
| 6165 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6166 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6167 | static struct task *hlua_process_task(struct task *task, void *context, unsigned short state) |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6168 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6169 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6170 | enum hlua_exec status; |
| 6171 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6172 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6173 | task_set_affinity(task, tid_bit); |
| 6174 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6175 | /* If it is the first call to the task, we must initialize the |
| 6176 | * execution timeouts. |
| 6177 | */ |
| 6178 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6179 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6180 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6181 | /* Execute the Lua code. */ |
| 6182 | status = hlua_ctx_resume(hlua, 1); |
| 6183 | |
| 6184 | switch (status) { |
| 6185 | /* finished or yield */ |
| 6186 | case HLUA_E_OK: |
| 6187 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6188 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6189 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6190 | break; |
| 6191 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6192 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6193 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6194 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6195 | break; |
| 6196 | |
| 6197 | /* finished with error. */ |
| 6198 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6199 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6200 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6201 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6202 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6203 | break; |
| 6204 | |
| 6205 | case HLUA_E_ERR: |
| 6206 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6207 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6208 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6209 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6210 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6211 | break; |
| 6212 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6213 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6214 | } |
| 6215 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6216 | /* This function is an LUA binding that register LUA function to be |
| 6217 | * executed after the HAProxy configuration parsing and before the |
| 6218 | * HAProxy scheduler starts. This function expect only one LUA |
| 6219 | * argument that is a function. This function returns nothing, but |
| 6220 | * throws if an error is encountered. |
| 6221 | */ |
| 6222 | __LJMP static int hlua_register_init(lua_State *L) |
| 6223 | { |
| 6224 | struct hlua_init_function *init; |
| 6225 | int ref; |
| 6226 | |
| 6227 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6228 | |
| 6229 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6230 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6231 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6232 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6233 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6234 | |
| 6235 | init->function_ref = ref; |
| 6236 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6237 | return 0; |
| 6238 | } |
| 6239 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6240 | /* This functio is an LUA binding. It permits to register a task |
| 6241 | * executed in parallel of the main HAroxy activity. The task is |
| 6242 | * created and it is set in the HAProxy scheduler. It can be called |
| 6243 | * from the "init" section, "post init" or during the runtime. |
| 6244 | * |
| 6245 | * Lua prototype: |
| 6246 | * |
| 6247 | * <none> core.register_task(<function>) |
| 6248 | */ |
| 6249 | static int hlua_register_task(lua_State *L) |
| 6250 | { |
| 6251 | struct hlua *hlua; |
| 6252 | struct task *task; |
| 6253 | int ref; |
| 6254 | |
| 6255 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6256 | |
| 6257 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6258 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6259 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6260 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6261 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6262 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6263 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6264 | if (!task) |
| 6265 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6266 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6267 | task->context = hlua; |
| 6268 | task->process = hlua_process_task; |
| 6269 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6270 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6271 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6272 | |
| 6273 | /* Restore the function in the stack. */ |
| 6274 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6275 | hlua->nargs = 0; |
| 6276 | |
| 6277 | /* Schedule task. */ |
| 6278 | task_schedule(task, now_ms); |
| 6279 | |
| 6280 | return 0; |
| 6281 | } |
| 6282 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6283 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6284 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6285 | * resume converters. |
| 6286 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6287 | 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] | 6288 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6289 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6290 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6291 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6292 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6293 | if (!stream) |
| 6294 | return 0; |
| 6295 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6296 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6297 | * Lua context can be not initialized. This behavior |
| 6298 | * permits to save performances because a systematic |
| 6299 | * Lua initialization cause 5% performances loss. |
| 6300 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6301 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6302 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6303 | if (!stream->hlua) { |
| 6304 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6305 | return 0; |
| 6306 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6307 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6308 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6309 | return 0; |
| 6310 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6311 | } |
| 6312 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6313 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6314 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6315 | |
| 6316 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6317 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6318 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6319 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6320 | else |
| 6321 | error = "critical error"; |
| 6322 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6323 | return 0; |
| 6324 | } |
| 6325 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6326 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6327 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6328 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6329 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6330 | return 0; |
| 6331 | } |
| 6332 | |
| 6333 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6334 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6335 | |
| 6336 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6337 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6338 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6339 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6340 | return 0; |
| 6341 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6342 | hlua_smp2lua(stream->hlua->T, smp); |
| 6343 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6344 | |
| 6345 | /* push keywords in the stack. */ |
| 6346 | if (arg_p) { |
| 6347 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6348 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6349 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6350 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6351 | return 0; |
| 6352 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6353 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6354 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6355 | } |
| 6356 | } |
| 6357 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6358 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6359 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6360 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6361 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6362 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6363 | } |
| 6364 | |
| 6365 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6366 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6367 | /* finished. */ |
| 6368 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6369 | /* If the stack is empty, the function fails. */ |
| 6370 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6371 | return 0; |
| 6372 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6373 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6374 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6375 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6376 | return 1; |
| 6377 | |
| 6378 | /* yield. */ |
| 6379 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6380 | 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] | 6381 | return 0; |
| 6382 | |
| 6383 | /* finished with error. */ |
| 6384 | case HLUA_E_ERRMSG: |
| 6385 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6386 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6387 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6388 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6389 | return 0; |
| 6390 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6391 | case HLUA_E_ETMOUT: |
| 6392 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6393 | return 0; |
| 6394 | |
| 6395 | case HLUA_E_NOMEM: |
| 6396 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6397 | return 0; |
| 6398 | |
| 6399 | case HLUA_E_YIELD: |
| 6400 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6401 | return 0; |
| 6402 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6403 | case HLUA_E_ERR: |
| 6404 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6405 | 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] | 6406 | |
| 6407 | default: |
| 6408 | return 0; |
| 6409 | } |
| 6410 | } |
| 6411 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6412 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6413 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6414 | * resume sample-fetches. This function will be called by the sample |
| 6415 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6416 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6417 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6418 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6419 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6420 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6421 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6422 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6423 | const struct buffer msg = { }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6424 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6425 | if (!stream) |
| 6426 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6427 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6428 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6429 | * Lua context can be not initialized. This behavior |
| 6430 | * permits to save performances because a systematic |
| 6431 | * Lua initialization cause 5% performances loss. |
| 6432 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6433 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6434 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6435 | if (!stream->hlua) { |
| 6436 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6437 | return 0; |
| 6438 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6439 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6440 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6441 | return 0; |
| 6442 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6443 | } |
| 6444 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6445 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6446 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6447 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6448 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6449 | |
| 6450 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6451 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6452 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6453 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6454 | else |
| 6455 | error = "critical error"; |
| 6456 | 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] | 6457 | return 0; |
| 6458 | } |
| 6459 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6460 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6461 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6462 | 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] | 6463 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6464 | return 0; |
| 6465 | } |
| 6466 | |
| 6467 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6468 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6469 | |
| 6470 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6471 | if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6472 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6473 | 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] | 6474 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6475 | return 0; |
| 6476 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6477 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6478 | |
| 6479 | /* push keywords in the stack. */ |
| 6480 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6481 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6482 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6483 | 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] | 6484 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6485 | return 0; |
| 6486 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6487 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6488 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6489 | } |
| 6490 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6491 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6492 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6493 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6494 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6495 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6496 | } |
| 6497 | |
| 6498 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6499 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6500 | /* finished. */ |
| 6501 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6502 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6503 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6504 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6505 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6506 | /* If the stack is empty, the function fails. */ |
| 6507 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6508 | return 0; |
| 6509 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6510 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6511 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6512 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6513 | |
| 6514 | /* Set the end of execution flag. */ |
| 6515 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6516 | return 1; |
| 6517 | |
| 6518 | /* yield. */ |
| 6519 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6520 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6521 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6522 | 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] | 6523 | return 0; |
| 6524 | |
| 6525 | /* finished with error. */ |
| 6526 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6527 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6528 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6529 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6530 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6531 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6532 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6533 | return 0; |
| 6534 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6535 | case HLUA_E_ETMOUT: |
| 6536 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6537 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6538 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6539 | return 0; |
| 6540 | |
| 6541 | case HLUA_E_NOMEM: |
| 6542 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6543 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6544 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6545 | return 0; |
| 6546 | |
| 6547 | case HLUA_E_YIELD: |
| 6548 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6549 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6550 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6551 | return 0; |
| 6552 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6553 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6554 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6555 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6556 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6557 | 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] | 6558 | |
| 6559 | default: |
| 6560 | return 0; |
| 6561 | } |
| 6562 | } |
| 6563 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6564 | /* This function is an LUA binding used for registering |
| 6565 | * "sample-conv" functions. It expects a converter name used |
| 6566 | * in the haproxy configuration file, and an LUA function. |
| 6567 | */ |
| 6568 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6569 | { |
| 6570 | struct sample_conv_kw_list *sck; |
| 6571 | const char *name; |
| 6572 | int ref; |
| 6573 | int len; |
| 6574 | struct hlua_function *fcn; |
| 6575 | |
| 6576 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6577 | |
| 6578 | /* First argument : converter name. */ |
| 6579 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6580 | |
| 6581 | /* Second argument : lua function. */ |
| 6582 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6583 | |
| 6584 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6585 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6586 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6587 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6588 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6589 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6590 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6591 | |
| 6592 | /* Fill fcn. */ |
| 6593 | fcn->name = strdup(name); |
| 6594 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6595 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6596 | fcn->function_ref = ref; |
| 6597 | |
| 6598 | /* List head */ |
| 6599 | sck->list.n = sck->list.p = NULL; |
| 6600 | |
| 6601 | /* converter keyword. */ |
| 6602 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6603 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6604 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6605 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6606 | |
| 6607 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6608 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6609 | 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] | 6610 | sck->kw[0].val_args = NULL; |
| 6611 | sck->kw[0].in_type = SMP_T_STR; |
| 6612 | sck->kw[0].out_type = SMP_T_STR; |
| 6613 | sck->kw[0].private = fcn; |
| 6614 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6615 | /* Register this new converter */ |
| 6616 | sample_register_convs(sck); |
| 6617 | |
| 6618 | return 0; |
| 6619 | } |
| 6620 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6621 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6622 | * "sample-fetch" functions. It expects a converter name used |
| 6623 | * in the haproxy configuration file, and an LUA function. |
| 6624 | */ |
| 6625 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6626 | { |
| 6627 | const char *name; |
| 6628 | int ref; |
| 6629 | int len; |
| 6630 | struct sample_fetch_kw_list *sfk; |
| 6631 | struct hlua_function *fcn; |
| 6632 | |
| 6633 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6634 | |
| 6635 | /* First argument : sample-fetch name. */ |
| 6636 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6637 | |
| 6638 | /* Second argument : lua function. */ |
| 6639 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6640 | |
| 6641 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6642 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6643 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6644 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6645 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6646 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6647 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6648 | |
| 6649 | /* Fill fcn. */ |
| 6650 | fcn->name = strdup(name); |
| 6651 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6652 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6653 | fcn->function_ref = ref; |
| 6654 | |
| 6655 | /* List head */ |
| 6656 | sfk->list.n = sfk->list.p = NULL; |
| 6657 | |
| 6658 | /* sample-fetch keyword. */ |
| 6659 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6660 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6661 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6662 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6663 | |
| 6664 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6665 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6666 | 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] | 6667 | sfk->kw[0].val_args = NULL; |
| 6668 | sfk->kw[0].out_type = SMP_T_STR; |
| 6669 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6670 | sfk->kw[0].val = 0; |
| 6671 | sfk->kw[0].private = fcn; |
| 6672 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6673 | /* Register this new fetch. */ |
| 6674 | sample_register_fetches(sfk); |
| 6675 | |
| 6676 | return 0; |
| 6677 | } |
| 6678 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6679 | /* This function is a wrapper to execute each LUA function declared |
| 6680 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6681 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6682 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6683 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6684 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6685 | 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] | 6686 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6687 | { |
| 6688 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6689 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6690 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6691 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6692 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6693 | |
| 6694 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 6695 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 6696 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 6697 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 6698 | case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6699 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6700 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6701 | return ACT_RET_CONT; |
| 6702 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6703 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6704 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6705 | * Lua context can be not initialized. This behavior |
| 6706 | * permits to save performances because a systematic |
| 6707 | * Lua initialization cause 5% performances loss. |
| 6708 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6709 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6710 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6711 | if (!s->hlua) { |
| 6712 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6713 | rule->arg.hlua_rule->fcn.name); |
| 6714 | return ACT_RET_CONT; |
| 6715 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6716 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6717 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6718 | rule->arg.hlua_rule->fcn.name); |
| 6719 | return ACT_RET_CONT; |
| 6720 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6721 | } |
| 6722 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6723 | consistency_set(s, dir, &s->hlua->cons); |
| 6724 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6725 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6726 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6727 | |
| 6728 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6729 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6730 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6731 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6732 | else |
| 6733 | error = "critical error"; |
| 6734 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6735 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6736 | return ACT_RET_CONT; |
| 6737 | } |
| 6738 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6739 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6740 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6741 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6742 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6743 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6744 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6745 | } |
| 6746 | |
| 6747 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6748 | 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] | 6749 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6750 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6751 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6752 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6753 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6754 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6755 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6756 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6757 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6758 | |
| 6759 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6760 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6761 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6762 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6763 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6764 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6765 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6766 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6767 | lua_pushstring(s->hlua->T, *arg); |
| 6768 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6769 | } |
| 6770 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6771 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6772 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6773 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6774 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6775 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6776 | } |
| 6777 | |
| 6778 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6779 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6780 | /* finished. */ |
| 6781 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6782 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6783 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6784 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6785 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6786 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6787 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6788 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6789 | |
| 6790 | /* yield. */ |
| 6791 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6792 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6793 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6794 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6795 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6796 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6797 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6798 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6799 | /* Some actions can be wake up when a "write" event |
| 6800 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6801 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6802 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6803 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6804 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6805 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6806 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6807 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6808 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6809 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6810 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6811 | * because HAProxy is not able to manipulate data, it |
| 6812 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6813 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6814 | |
| 6815 | /* finished with error. */ |
| 6816 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6817 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6818 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6819 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6820 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6821 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6822 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6823 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6824 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6825 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6826 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6827 | case HLUA_E_ETMOUT: |
| 6828 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6829 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6830 | return ACT_RET_ERR; |
| 6831 | } |
| 6832 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6833 | return 0; |
| 6834 | |
| 6835 | case HLUA_E_NOMEM: |
| 6836 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6837 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6838 | return ACT_RET_ERR; |
| 6839 | } |
| 6840 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6841 | return 0; |
| 6842 | |
| 6843 | case HLUA_E_YIELD: |
| 6844 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6845 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6846 | return ACT_RET_ERR; |
| 6847 | } |
| 6848 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6849 | rule->arg.hlua_rule->fcn.name); |
| 6850 | return 0; |
| 6851 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6852 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6853 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6854 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6855 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6856 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6857 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6858 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6859 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6860 | |
| 6861 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6862 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6863 | } |
| 6864 | } |
| 6865 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6866 | 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] | 6867 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6868 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6869 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6870 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6871 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6872 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6873 | } |
| 6874 | |
| 6875 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6876 | { |
| 6877 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6878 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6879 | struct task *task; |
| 6880 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6881 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6882 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6883 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6884 | if (!hlua) { |
| 6885 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6886 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6887 | return 0; |
| 6888 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6889 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6890 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6891 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6892 | |
| 6893 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6894 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6895 | if (!task) { |
| 6896 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6897 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6898 | return 0; |
| 6899 | } |
| 6900 | task->nice = 0; |
| 6901 | task->context = ctx; |
| 6902 | task->process = hlua_applet_wakeup; |
| 6903 | ctx->ctx.hlua_apptcp.task = task; |
| 6904 | |
| 6905 | /* In the execution wrappers linked with a stream, the |
| 6906 | * Lua context can be not initialized. This behavior |
| 6907 | * permits to save performances because a systematic |
| 6908 | * Lua initialization cause 5% performances loss. |
| 6909 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6910 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6911 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6912 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6913 | return 0; |
| 6914 | } |
| 6915 | |
| 6916 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6917 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6918 | |
| 6919 | /* The following Lua calls can fail. */ |
| 6920 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6921 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6922 | error = lua_tostring(hlua->T, -1); |
| 6923 | else |
| 6924 | error = "critical error"; |
| 6925 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6926 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6927 | return 0; |
| 6928 | } |
| 6929 | |
| 6930 | /* Check stack available size. */ |
| 6931 | if (!lua_checkstack(hlua->T, 1)) { |
| 6932 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6933 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6934 | RESET_SAFE_LJMP(hlua->T); |
| 6935 | return 0; |
| 6936 | } |
| 6937 | |
| 6938 | /* Restore the function in the stack. */ |
| 6939 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6940 | |
| 6941 | /* Create and and push object stream in the stack. */ |
| 6942 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6943 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6944 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6945 | RESET_SAFE_LJMP(hlua->T); |
| 6946 | return 0; |
| 6947 | } |
| 6948 | hlua->nargs = 1; |
| 6949 | |
| 6950 | /* push keywords in the stack. */ |
| 6951 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6952 | if (!lua_checkstack(hlua->T, 1)) { |
| 6953 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6954 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6955 | RESET_SAFE_LJMP(hlua->T); |
| 6956 | return 0; |
| 6957 | } |
| 6958 | lua_pushstring(hlua->T, *arg); |
| 6959 | hlua->nargs++; |
| 6960 | } |
| 6961 | |
| 6962 | RESET_SAFE_LJMP(hlua->T); |
| 6963 | |
| 6964 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6965 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6966 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6967 | |
| 6968 | return 1; |
| 6969 | } |
| 6970 | |
| 6971 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6972 | { |
| 6973 | struct stream_interface *si = ctx->owner; |
| 6974 | struct stream *strm = si_strm(si); |
| 6975 | struct channel *res = si_ic(si); |
| 6976 | struct act_rule *rule = ctx->rule; |
| 6977 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6978 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6979 | |
| 6980 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6981 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6982 | /* eat the whole request */ |
| 6983 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6984 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6985 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6986 | |
| 6987 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6988 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6989 | return; |
| 6990 | |
| 6991 | /* Execute the function. */ |
| 6992 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6993 | /* finished. */ |
| 6994 | case HLUA_E_OK: |
| 6995 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6996 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6997 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6998 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6999 | res->flags |= CF_READ_NULL; |
| 7000 | si_shutr(si); |
| 7001 | return; |
| 7002 | |
| 7003 | /* yield. */ |
| 7004 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7005 | if (hlua->wake_time != TICK_ETERNITY) |
| 7006 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7007 | return; |
| 7008 | |
| 7009 | /* finished with error. */ |
| 7010 | case HLUA_E_ERRMSG: |
| 7011 | /* Display log. */ |
| 7012 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7013 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7014 | lua_pop(hlua->T, 1); |
| 7015 | goto error; |
| 7016 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7017 | case HLUA_E_ETMOUT: |
| 7018 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 7019 | rule->arg.hlua_rule->fcn.name); |
| 7020 | goto error; |
| 7021 | |
| 7022 | case HLUA_E_NOMEM: |
| 7023 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 7024 | rule->arg.hlua_rule->fcn.name); |
| 7025 | goto error; |
| 7026 | |
| 7027 | case HLUA_E_YIELD: /* unexpected */ |
| 7028 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 7029 | rule->arg.hlua_rule->fcn.name); |
| 7030 | goto error; |
| 7031 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7032 | case HLUA_E_ERR: |
| 7033 | /* Display log. */ |
| 7034 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 7035 | rule->arg.hlua_rule->fcn.name); |
| 7036 | goto error; |
| 7037 | |
| 7038 | default: |
| 7039 | goto error; |
| 7040 | } |
| 7041 | |
| 7042 | error: |
| 7043 | |
| 7044 | /* For all other cases, just close the stream. */ |
| 7045 | si_shutw(si); |
| 7046 | si_shutr(si); |
| 7047 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7048 | } |
| 7049 | |
| 7050 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7051 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7052 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7053 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7054 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7055 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7056 | } |
| 7057 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7058 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7059 | * an errors occurs and -1 if more data are required for initializing |
| 7060 | * the applet. |
| 7061 | */ |
| 7062 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7063 | { |
| 7064 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7065 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7066 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7067 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7068 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7069 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7070 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7071 | txn = strm->txn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7072 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7073 | /* We want two things in HTTP mode : |
| 7074 | * - enforce server-close mode if we were in keep-alive, so that the |
| 7075 | * applet is released after each response ; |
| 7076 | * - enable request body transfer to the applet in order to resync |
| 7077 | * with the response body. |
| 7078 | */ |
| 7079 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 7080 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7081 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7082 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7083 | if (!hlua) { |
| 7084 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7085 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7086 | return 0; |
| 7087 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7088 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7089 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7090 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7091 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7092 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7093 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7094 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7095 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7096 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7097 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7098 | if (!task) { |
| 7099 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7100 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7101 | return 0; |
| 7102 | } |
| 7103 | task->nice = 0; |
| 7104 | task->context = ctx; |
| 7105 | task->process = hlua_applet_wakeup; |
| 7106 | ctx->ctx.hlua_apphttp.task = task; |
| 7107 | |
| 7108 | /* In the execution wrappers linked with a stream, the |
| 7109 | * Lua context can be not initialized. This behavior |
| 7110 | * permits to save performances because a systematic |
| 7111 | * Lua initialization cause 5% performances loss. |
| 7112 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7113 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7114 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 7115 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7116 | return 0; |
| 7117 | } |
| 7118 | |
| 7119 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7120 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7121 | |
| 7122 | /* The following Lua calls can fail. */ |
| 7123 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7124 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7125 | error = lua_tostring(hlua->T, -1); |
| 7126 | else |
| 7127 | error = "critical error"; |
| 7128 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7129 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7130 | return 0; |
| 7131 | } |
| 7132 | |
| 7133 | /* Check stack available size. */ |
| 7134 | if (!lua_checkstack(hlua->T, 1)) { |
| 7135 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7136 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7137 | RESET_SAFE_LJMP(hlua->T); |
| 7138 | return 0; |
| 7139 | } |
| 7140 | |
| 7141 | /* Restore the function in the stack. */ |
| 7142 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7143 | |
| 7144 | /* Create and and push object stream in the stack. */ |
| 7145 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7146 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7147 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7148 | RESET_SAFE_LJMP(hlua->T); |
| 7149 | return 0; |
| 7150 | } |
| 7151 | hlua->nargs = 1; |
| 7152 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7153 | /* push keywords in the stack. */ |
| 7154 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7155 | if (!lua_checkstack(hlua->T, 1)) { |
| 7156 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7157 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7158 | RESET_SAFE_LJMP(hlua->T); |
| 7159 | return 0; |
| 7160 | } |
| 7161 | lua_pushstring(hlua->T, *arg); |
| 7162 | hlua->nargs++; |
| 7163 | } |
| 7164 | |
| 7165 | RESET_SAFE_LJMP(hlua->T); |
| 7166 | |
| 7167 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7168 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7169 | |
| 7170 | return 1; |
| 7171 | } |
| 7172 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7173 | static void hlua_applet_htx_fct(struct appctx *ctx) |
| 7174 | { |
| 7175 | struct stream_interface *si = ctx->owner; |
| 7176 | struct stream *strm = si_strm(si); |
| 7177 | struct channel *req = si_oc(si); |
| 7178 | struct channel *res = si_ic(si); |
| 7179 | struct act_rule *rule = ctx->rule; |
| 7180 | struct proxy *px = strm->be; |
| 7181 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7182 | struct htx *req_htx, *res_htx; |
| 7183 | |
| 7184 | res_htx = htx_from_buf(&res->buf); |
| 7185 | |
| 7186 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7187 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7188 | goto out; |
| 7189 | |
| 7190 | /* Check if the input buffer is avalaible. */ |
| 7191 | if (!b_size(&res->buf)) { |
| 7192 | si_rx_room_blk(si); |
| 7193 | goto out; |
| 7194 | } |
| 7195 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7196 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7197 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7198 | |
| 7199 | /* Set the currently running flag. */ |
| 7200 | if (!HLUA_IS_RUNNING(hlua) && |
| 7201 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7202 | struct htx_blk *blk; |
| 7203 | size_t count = co_data(req); |
| 7204 | |
| 7205 | if (!count) { |
| 7206 | si_cant_get(si); |
| 7207 | goto out; |
| 7208 | } |
| 7209 | |
| 7210 | /* We need to flush the request header. This left the body for |
| 7211 | * the Lua. |
| 7212 | */ |
| 7213 | req_htx = htx_from_buf(&req->buf); |
| 7214 | blk = htx_get_head_blk(req_htx); |
| 7215 | while (count && blk) { |
| 7216 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7217 | uint32_t sz = htx_get_blksz(blk); |
| 7218 | |
| 7219 | if (sz > count) { |
| 7220 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7221 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7222 | goto out; |
| 7223 | } |
| 7224 | |
| 7225 | count -= sz; |
| 7226 | co_set_data(req, co_data(req) - sz); |
| 7227 | blk = htx_remove_blk(req_htx, blk); |
| 7228 | |
| 7229 | if (type == HTX_BLK_EOH) |
| 7230 | break; |
| 7231 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7232 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7233 | } |
| 7234 | |
| 7235 | /* Executes The applet if it is not done. */ |
| 7236 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7237 | |
| 7238 | /* Execute the function. */ |
| 7239 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7240 | /* finished. */ |
| 7241 | case HLUA_E_OK: |
| 7242 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7243 | break; |
| 7244 | |
| 7245 | /* yield. */ |
| 7246 | case HLUA_E_AGAIN: |
| 7247 | if (hlua->wake_time != TICK_ETERNITY) |
| 7248 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7249 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7250 | |
| 7251 | /* finished with error. */ |
| 7252 | case HLUA_E_ERRMSG: |
| 7253 | /* Display log. */ |
| 7254 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7255 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7256 | lua_pop(hlua->T, 1); |
| 7257 | goto error; |
| 7258 | |
| 7259 | case HLUA_E_ETMOUT: |
| 7260 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7261 | rule->arg.hlua_rule->fcn.name); |
| 7262 | goto error; |
| 7263 | |
| 7264 | case HLUA_E_NOMEM: |
| 7265 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7266 | rule->arg.hlua_rule->fcn.name); |
| 7267 | goto error; |
| 7268 | |
| 7269 | case HLUA_E_YIELD: /* unexpected */ |
| 7270 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7271 | rule->arg.hlua_rule->fcn.name); |
| 7272 | goto error; |
| 7273 | |
| 7274 | case HLUA_E_ERR: |
| 7275 | /* Display log. */ |
| 7276 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7277 | rule->arg.hlua_rule->fcn.name); |
| 7278 | goto error; |
| 7279 | |
| 7280 | default: |
| 7281 | goto error; |
| 7282 | } |
| 7283 | } |
| 7284 | |
| 7285 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7286 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7287 | goto done; |
| 7288 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7289 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7290 | goto error; |
| 7291 | |
| 7292 | /* Don't add EOD and TLR because mux-h1 will take care of it */ |
| 7293 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7294 | si_rx_room_blk(si); |
| 7295 | goto out; |
| 7296 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7297 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7298 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7299 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7300 | } |
| 7301 | |
| 7302 | done: |
| 7303 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7304 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7305 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7306 | si_shutr(si); |
| 7307 | } |
| 7308 | |
| 7309 | /* eat the whole request */ |
| 7310 | if (co_data(req)) { |
| 7311 | req_htx = htx_from_buf(&req->buf); |
| 7312 | co_htx_skip(req, req_htx, co_data(req)); |
| 7313 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7314 | } |
| 7315 | } |
| 7316 | |
| 7317 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7318 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7319 | return; |
| 7320 | |
| 7321 | error: |
| 7322 | |
| 7323 | /* If we are in HTTP mode, and we are not send any |
| 7324 | * data, return a 500 server error in best effort: |
| 7325 | * if there is no room available in the buffer, |
| 7326 | * just close the connection. |
| 7327 | */ |
| 7328 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7329 | struct buffer *err = &htx_err_chunks[HTTP_ERR_500]; |
| 7330 | |
| 7331 | channel_erase(res); |
| 7332 | res->buf.data = b_data(err); |
| 7333 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7334 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7335 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7336 | } |
| 7337 | if (!(strm->flags & SF_ERR_MASK)) |
| 7338 | strm->flags |= SF_ERR_RESOURCE; |
| 7339 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7340 | goto done; |
| 7341 | } |
| 7342 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7343 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 7344 | { |
| 7345 | struct stream_interface *si = ctx->owner; |
| 7346 | struct stream *strm = si_strm(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7347 | struct channel *req = si_oc(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7348 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7349 | struct act_rule *rule = ctx->rule; |
| 7350 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7351 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7352 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7353 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7354 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7355 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7356 | int ret; |
| 7357 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7358 | if (IS_HTX_STRM(strm)) |
| 7359 | return hlua_applet_htx_fct(ctx); |
| 7360 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7361 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7362 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7363 | goto out; |
| 7364 | |
| 7365 | /* Check if the input buffer is avalaible. */ |
| 7366 | if (!b_size(&res->buf)) { |
| 7367 | si_rx_room_blk(si); |
| 7368 | goto out; |
| 7369 | } |
| 7370 | /* check that the output is not closed */ |
| 7371 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
| 7372 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7373 | |
| 7374 | /* Set the currently running flag. */ |
| 7375 | if (!HLUA_IS_RUNNING(hlua) && |
| 7376 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7377 | /* Store the max amount of bytes that we can read. */ |
| 7378 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 7379 | |
| 7380 | /* We need to flush the request header. This left the body |
| 7381 | * for the Lua. |
| 7382 | */ |
| 7383 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7384 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7385 | ret = co_getblk_nc(req, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7386 | if (ret == -1) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7387 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7388 | |
| 7389 | /* No data available, ask for more data. */ |
| 7390 | if (ret == 1) |
| 7391 | len2 = 0; |
| 7392 | if (ret == 0) |
| 7393 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 7394 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7395 | si_cant_get(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7396 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7397 | } |
| 7398 | |
| 7399 | /* skip the requests bytes. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7400 | co_skip(req, strm->txn->req.eoh + strm->txn->req.eol); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7401 | } |
| 7402 | |
| 7403 | /* Executes The applet if it is not done. */ |
| 7404 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7405 | |
| 7406 | /* Execute the function. */ |
| 7407 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7408 | /* finished. */ |
| 7409 | case HLUA_E_OK: |
| 7410 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7411 | break; |
| 7412 | |
| 7413 | /* yield. */ |
| 7414 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7415 | if (hlua->wake_time != TICK_ETERNITY) |
| 7416 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7417 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7418 | |
| 7419 | /* finished with error. */ |
| 7420 | case HLUA_E_ERRMSG: |
| 7421 | /* Display log. */ |
| 7422 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7423 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7424 | lua_pop(hlua->T, 1); |
| 7425 | goto error; |
| 7426 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7427 | case HLUA_E_ETMOUT: |
| 7428 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7429 | rule->arg.hlua_rule->fcn.name); |
| 7430 | goto error; |
| 7431 | |
| 7432 | case HLUA_E_NOMEM: |
| 7433 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7434 | rule->arg.hlua_rule->fcn.name); |
| 7435 | goto error; |
| 7436 | |
| 7437 | case HLUA_E_YIELD: /* unexpected */ |
| 7438 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7439 | rule->arg.hlua_rule->fcn.name); |
| 7440 | goto error; |
| 7441 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7442 | case HLUA_E_ERR: |
| 7443 | /* Display log. */ |
| 7444 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7445 | rule->arg.hlua_rule->fcn.name); |
| 7446 | goto error; |
| 7447 | |
| 7448 | default: |
| 7449 | goto error; |
| 7450 | } |
| 7451 | } |
| 7452 | |
| 7453 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7454 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7455 | goto done; |
| 7456 | |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 7457 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7458 | goto error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7459 | |
| 7460 | /* We must send the final chunk. */ |
| 7461 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 7462 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 7463 | |
| 7464 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 7465 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7466 | |
| 7467 | /* critical error. */ |
| 7468 | if (ret == -2 || ret == -3) { |
| 7469 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 7470 | rule->arg.hlua_rule->fcn.name); |
| 7471 | goto error; |
| 7472 | } |
| 7473 | |
| 7474 | /* no enough space error. */ |
| 7475 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7476 | si_rx_room_blk(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7477 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7478 | } |
| 7479 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7480 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7481 | ctx->ctx.hlua_apphttp.flags |= (APPLET_LAST_CHK|APPLET_RSP_SENT); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7482 | } |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7483 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7484 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7485 | done: |
| 7486 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 7487 | if (!(res->flags & CF_SHUTR)) { |
| 7488 | res->flags |= CF_READ_NULL; |
| 7489 | si_shutr(si); |
| 7490 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7491 | |
| 7492 | /* eat the whole request */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7493 | if (co_data(req)) |
| 7494 | co_skip(req, co_data(req)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7495 | } |
| 7496 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7497 | out: |
| 7498 | return; |
| 7499 | |
| 7500 | error: |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7501 | |
| 7502 | /* If we are in HTTP mode, and we are not send any |
| 7503 | * data, return a 500 server error in best effort: |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7504 | * if there is no room available in the buffer, |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7505 | * just close the connection. |
| 7506 | */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7507 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7508 | channel_erase(res); |
| 7509 | ci_putblk(res, error_500, strlen(error_500)); |
| 7510 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7511 | if (!(strm->flags & SF_ERR_MASK)) |
| 7512 | strm->flags |= SF_ERR_RESOURCE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7513 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7514 | goto done; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7515 | } |
| 7516 | |
| 7517 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7518 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7519 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7520 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7521 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7522 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7523 | } |
| 7524 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7525 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 7526 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7527 | * |
| 7528 | * This function can fail with an abort() due to an Lua critical error. |
| 7529 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7530 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7531 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7532 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7533 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7534 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7535 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7536 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7537 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7538 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7539 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7540 | if (!rule->arg.hlua_rule) { |
| 7541 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7542 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7543 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7544 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7545 | /* Memory for arguments. */ |
| 7546 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7547 | if (!rule->arg.hlua_rule->args) { |
| 7548 | memprintf(err, "out of memory error"); |
| 7549 | return ACT_RET_PRS_ERR; |
| 7550 | } |
| 7551 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7552 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7553 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7554 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7555 | /* Expect some arguments */ |
| 7556 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7557 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7558 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7559 | return ACT_RET_PRS_ERR; |
| 7560 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7561 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7562 | if (!rule->arg.hlua_rule->args[i]) { |
| 7563 | memprintf(err, "out of memory error"); |
| 7564 | return ACT_RET_PRS_ERR; |
| 7565 | } |
| 7566 | (*cur_arg)++; |
| 7567 | } |
| 7568 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7569 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7570 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7571 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7572 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7573 | } |
| 7574 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7575 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7576 | struct act_rule *rule, char **err) |
| 7577 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7578 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7579 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7580 | /* HTTP applets are forbidden in tcp-request rules. |
| 7581 | * HTTP applet request requires everything initilized by |
| 7582 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 7583 | * The applet will be immediately initilized, but its before |
| 7584 | * the call of this analyzer. |
| 7585 | */ |
| 7586 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7587 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7588 | return ACT_RET_PRS_ERR; |
| 7589 | } |
| 7590 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7591 | /* Memory for the rule. */ |
| 7592 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7593 | if (!rule->arg.hlua_rule) { |
| 7594 | memprintf(err, "out of memory error"); |
| 7595 | return ACT_RET_PRS_ERR; |
| 7596 | } |
| 7597 | |
| 7598 | /* Reference the Lua function and store the reference. */ |
| 7599 | rule->arg.hlua_rule->fcn = *fcn; |
| 7600 | |
| 7601 | /* TODO: later accept arguments. */ |
| 7602 | rule->arg.hlua_rule->args = NULL; |
| 7603 | |
| 7604 | /* Add applet pointer in the rule. */ |
| 7605 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7606 | rule->applet.name = fcn->name; |
| 7607 | rule->applet.init = hlua_applet_http_init; |
| 7608 | rule->applet.fct = hlua_applet_http_fct; |
| 7609 | rule->applet.release = hlua_applet_http_release; |
| 7610 | rule->applet.timeout = hlua_timeout_applet; |
| 7611 | |
| 7612 | return ACT_RET_PRS_OK; |
| 7613 | } |
| 7614 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7615 | /* This function is an LUA binding used for registering |
| 7616 | * "sample-conv" functions. It expects a converter name used |
| 7617 | * in the haproxy configuration file, and an LUA function. |
| 7618 | */ |
| 7619 | __LJMP static int hlua_register_action(lua_State *L) |
| 7620 | { |
| 7621 | struct action_kw_list *akl; |
| 7622 | const char *name; |
| 7623 | int ref; |
| 7624 | int len; |
| 7625 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7626 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7627 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7628 | /* Initialise the number of expected arguments at 0. */ |
| 7629 | nargs = 0; |
| 7630 | |
| 7631 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7632 | 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] | 7633 | |
| 7634 | /* First argument : converter name. */ |
| 7635 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7636 | |
| 7637 | /* Second argument : environment. */ |
| 7638 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7639 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7640 | |
| 7641 | /* Third argument : lua function. */ |
| 7642 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7643 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7644 | /* 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] | 7645 | if (lua_gettop(L) >= 4) |
| 7646 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7647 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7648 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7649 | lua_pushnil(L); |
| 7650 | while (lua_next(L, 2) != 0) { |
| 7651 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7652 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7653 | |
| 7654 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7655 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7656 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7657 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7658 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7659 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7660 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7661 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7662 | |
| 7663 | /* Fill fcn. */ |
| 7664 | fcn->name = strdup(name); |
| 7665 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7666 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7667 | fcn->function_ref = ref; |
| 7668 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7669 | /* Set the expected number od arguments. */ |
| 7670 | fcn->nargs = nargs; |
| 7671 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7672 | /* List head */ |
| 7673 | akl->list.n = akl->list.p = NULL; |
| 7674 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7675 | /* action keyword. */ |
| 7676 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7677 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7678 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7679 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7680 | |
| 7681 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7682 | |
| 7683 | akl->kw[0].match_pfx = 0; |
| 7684 | akl->kw[0].private = fcn; |
| 7685 | akl->kw[0].parse = action_register_lua; |
| 7686 | |
| 7687 | /* select the action registering point. */ |
| 7688 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7689 | tcp_req_cont_keywords_register(akl); |
| 7690 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7691 | tcp_res_cont_keywords_register(akl); |
| 7692 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7693 | http_req_keywords_register(akl); |
| 7694 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7695 | http_res_keywords_register(akl); |
| 7696 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7697 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7698 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7699 | "are expected.", lua_tostring(L, -1))); |
| 7700 | |
| 7701 | /* pop the environment string. */ |
| 7702 | lua_pop(L, 1); |
| 7703 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7704 | return ACT_RET_PRS_OK; |
| 7705 | } |
| 7706 | |
| 7707 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7708 | struct act_rule *rule, char **err) |
| 7709 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7710 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7711 | |
Christopher Faulet | eb70980 | 2019-04-11 22:04:08 +0200 | [diff] [blame] | 7712 | if (px->mode == PR_MODE_HTTP && (px->options2 & PR_O2_USE_HTX)) { |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7713 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 7714 | return ACT_RET_PRS_ERR; |
| 7715 | } |
| 7716 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7717 | /* Memory for the rule. */ |
| 7718 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7719 | if (!rule->arg.hlua_rule) { |
| 7720 | memprintf(err, "out of memory error"); |
| 7721 | return ACT_RET_PRS_ERR; |
| 7722 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7723 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7724 | /* Reference the Lua function and store the reference. */ |
| 7725 | rule->arg.hlua_rule->fcn = *fcn; |
| 7726 | |
| 7727 | /* TODO: later accept arguments. */ |
| 7728 | rule->arg.hlua_rule->args = NULL; |
| 7729 | |
| 7730 | /* Add applet pointer in the rule. */ |
| 7731 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7732 | rule->applet.name = fcn->name; |
| 7733 | rule->applet.init = hlua_applet_tcp_init; |
| 7734 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7735 | rule->applet.release = hlua_applet_tcp_release; |
| 7736 | rule->applet.timeout = hlua_timeout_applet; |
| 7737 | |
| 7738 | return 0; |
| 7739 | } |
| 7740 | |
| 7741 | /* This function is an LUA binding used for registering |
| 7742 | * "sample-conv" functions. It expects a converter name used |
| 7743 | * in the haproxy configuration file, and an LUA function. |
| 7744 | */ |
| 7745 | __LJMP static int hlua_register_service(lua_State *L) |
| 7746 | { |
| 7747 | struct action_kw_list *akl; |
| 7748 | const char *name; |
| 7749 | const char *env; |
| 7750 | int ref; |
| 7751 | int len; |
| 7752 | struct hlua_function *fcn; |
| 7753 | |
| 7754 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7755 | |
| 7756 | /* First argument : converter name. */ |
| 7757 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7758 | |
| 7759 | /* Second argument : environment. */ |
| 7760 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7761 | |
| 7762 | /* Third argument : lua function. */ |
| 7763 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7764 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7765 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7766 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7767 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7768 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7769 | fcn = calloc(1, sizeof(*fcn)); |
| 7770 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7771 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7772 | |
| 7773 | /* Fill fcn. */ |
| 7774 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7775 | fcn->name = calloc(1, len); |
| 7776 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7777 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7778 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7779 | fcn->function_ref = ref; |
| 7780 | |
| 7781 | /* List head */ |
| 7782 | akl->list.n = akl->list.p = NULL; |
| 7783 | |
| 7784 | /* converter keyword. */ |
| 7785 | len = strlen("lua.") + strlen(name) + 1; |
| 7786 | akl->kw[0].kw = calloc(1, len); |
| 7787 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7788 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7789 | |
| 7790 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7791 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7792 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7793 | if (strcmp(env, "tcp") == 0) |
| 7794 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7795 | else if (strcmp(env, "http") == 0) |
| 7796 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7797 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7798 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7799 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7800 | |
| 7801 | akl->kw[0].match_pfx = 0; |
| 7802 | akl->kw[0].private = fcn; |
| 7803 | |
| 7804 | /* End of array. */ |
| 7805 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7806 | |
| 7807 | /* Register this new converter */ |
| 7808 | service_keywords_register(akl); |
| 7809 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7810 | return 0; |
| 7811 | } |
| 7812 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7813 | /* This function initialises Lua cli handler. It copies the |
| 7814 | * arguments in the Lua stack and create channel IO objects. |
| 7815 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7816 | 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] | 7817 | { |
| 7818 | struct hlua *hlua; |
| 7819 | struct hlua_function *fcn; |
| 7820 | int i; |
| 7821 | const char *error; |
| 7822 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7823 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7824 | appctx->ctx.hlua_cli.fcn = private; |
| 7825 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7826 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7827 | if (!hlua) { |
| 7828 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7829 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7830 | } |
| 7831 | HLUA_INIT(hlua); |
| 7832 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7833 | |
| 7834 | /* Create task used by signal to wakeup applets. |
| 7835 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7836 | * applet_http. It is absolutely compatible. |
| 7837 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7838 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7839 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7840 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7841 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7842 | } |
| 7843 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7844 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7845 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7846 | |
| 7847 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7848 | 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] | 7849 | 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] | 7850 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7851 | } |
| 7852 | |
| 7853 | /* The following Lua calls can fail. */ |
| 7854 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7855 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7856 | error = lua_tostring(hlua->T, -1); |
| 7857 | else |
| 7858 | error = "critical error"; |
| 7859 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7860 | goto error; |
| 7861 | } |
| 7862 | |
| 7863 | /* Check stack available size. */ |
| 7864 | if (!lua_checkstack(hlua->T, 2)) { |
| 7865 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7866 | goto error; |
| 7867 | } |
| 7868 | |
| 7869 | /* Restore the function in the stack. */ |
| 7870 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7871 | |
| 7872 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7873 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7874 | */ |
| 7875 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7876 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7877 | goto error; |
| 7878 | } |
| 7879 | hlua->nargs = 1; |
| 7880 | |
| 7881 | /* push keywords in the stack. */ |
| 7882 | for (i = 0; *args[i]; i++) { |
| 7883 | /* Check stack available size. */ |
| 7884 | if (!lua_checkstack(hlua->T, 1)) { |
| 7885 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7886 | goto error; |
| 7887 | } |
| 7888 | lua_pushstring(hlua->T, args[i]); |
| 7889 | hlua->nargs++; |
| 7890 | } |
| 7891 | |
| 7892 | /* We must initialize the execution timeouts. */ |
| 7893 | hlua->max_time = hlua_timeout_session; |
| 7894 | |
| 7895 | /* At this point the execution is safe. */ |
| 7896 | RESET_SAFE_LJMP(hlua->T); |
| 7897 | |
| 7898 | /* It's ok */ |
| 7899 | return 0; |
| 7900 | |
| 7901 | /* It's not ok. */ |
| 7902 | error: |
| 7903 | RESET_SAFE_LJMP(hlua->T); |
| 7904 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7905 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7906 | return 1; |
| 7907 | } |
| 7908 | |
| 7909 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7910 | { |
| 7911 | struct hlua *hlua; |
| 7912 | struct stream_interface *si; |
| 7913 | struct hlua_function *fcn; |
| 7914 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7915 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7916 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7917 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7918 | |
| 7919 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7920 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7921 | return 1; |
| 7922 | |
| 7923 | /* Execute the function. */ |
| 7924 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7925 | |
| 7926 | /* finished. */ |
| 7927 | case HLUA_E_OK: |
| 7928 | return 1; |
| 7929 | |
| 7930 | /* yield. */ |
| 7931 | case HLUA_E_AGAIN: |
| 7932 | /* We want write. */ |
| 7933 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7934 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7935 | /* Set the timeout. */ |
| 7936 | if (hlua->wake_time != TICK_ETERNITY) |
| 7937 | task_schedule(hlua->task, hlua->wake_time); |
| 7938 | return 0; |
| 7939 | |
| 7940 | /* finished with error. */ |
| 7941 | case HLUA_E_ERRMSG: |
| 7942 | /* Display log. */ |
| 7943 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7944 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7945 | lua_pop(hlua->T, 1); |
| 7946 | return 1; |
| 7947 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7948 | case HLUA_E_ETMOUT: |
| 7949 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7950 | fcn->name); |
| 7951 | return 1; |
| 7952 | |
| 7953 | case HLUA_E_NOMEM: |
| 7954 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7955 | fcn->name); |
| 7956 | return 1; |
| 7957 | |
| 7958 | case HLUA_E_YIELD: /* unexpected */ |
| 7959 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7960 | fcn->name); |
| 7961 | return 1; |
| 7962 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7963 | case HLUA_E_ERR: |
| 7964 | /* Display log. */ |
| 7965 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7966 | fcn->name); |
| 7967 | return 1; |
| 7968 | |
| 7969 | default: |
| 7970 | return 1; |
| 7971 | } |
| 7972 | |
| 7973 | return 1; |
| 7974 | } |
| 7975 | |
| 7976 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7977 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7978 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7979 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7980 | } |
| 7981 | |
| 7982 | /* This function is an LUA binding used for registering |
| 7983 | * new keywords in the cli. It expects a list of keywords |
| 7984 | * which are the "path". It is limited to 5 keywords. A |
| 7985 | * description of the command, a function to be executed |
| 7986 | * for the parsing and a function for io handlers. |
| 7987 | */ |
| 7988 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7989 | { |
| 7990 | struct cli_kw_list *cli_kws; |
| 7991 | const char *message; |
| 7992 | int ref_io; |
| 7993 | int len; |
| 7994 | struct hlua_function *fcn; |
| 7995 | int index; |
| 7996 | int i; |
| 7997 | |
| 7998 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7999 | |
| 8000 | /* First argument : an array of maximum 5 keywords. */ |
| 8001 | if (!lua_istable(L, 1)) |
| 8002 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8003 | |
| 8004 | /* Second argument : string with contextual message. */ |
| 8005 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8006 | |
| 8007 | /* Third and fourth argument : lua function. */ |
| 8008 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8009 | |
| 8010 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8011 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 8012 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8013 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8014 | fcn = calloc(1, sizeof(*fcn)); |
| 8015 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8016 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8017 | |
| 8018 | /* Fill path. */ |
| 8019 | index = 0; |
| 8020 | lua_pushnil(L); |
| 8021 | while(lua_next(L, 1) != 0) { |
| 8022 | if (index >= 5) |
| 8023 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8024 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8025 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8026 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 8027 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8028 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8029 | index++; |
| 8030 | lua_pop(L, 1); |
| 8031 | } |
| 8032 | |
| 8033 | /* Copy help message. */ |
| 8034 | cli_kws->kw[0].usage = strdup(message); |
| 8035 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8036 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8037 | |
| 8038 | /* Fill fcn io handler. */ |
| 8039 | len = strlen("<lua.cli>") + 1; |
| 8040 | for (i = 0; i < index; i++) |
| 8041 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8042 | fcn->name = calloc(1, len); |
| 8043 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8044 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8045 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8046 | for (i = 0; i < index; i++) { |
| 8047 | strncat((char *)fcn->name, ".", len); |
| 8048 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8049 | } |
| 8050 | strncat((char *)fcn->name, ">", len); |
| 8051 | fcn->function_ref = ref_io; |
| 8052 | |
| 8053 | /* Fill last entries. */ |
| 8054 | cli_kws->kw[0].private = fcn; |
| 8055 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8056 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8057 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8058 | |
| 8059 | /* Register this new converter */ |
| 8060 | cli_register_kw(cli_kws); |
| 8061 | |
| 8062 | return 0; |
| 8063 | } |
| 8064 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8065 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 8066 | struct proxy *defpx, const char *file, int line, |
| 8067 | char **err, unsigned int *timeout) |
| 8068 | { |
| 8069 | const char *error; |
| 8070 | |
| 8071 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 8072 | if (error && *error != '\0') { |
| 8073 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8074 | return -1; |
| 8075 | } |
| 8076 | return 0; |
| 8077 | } |
| 8078 | |
| 8079 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 8080 | struct proxy *defpx, const char *file, int line, |
| 8081 | char **err) |
| 8082 | { |
| 8083 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8084 | file, line, err, &hlua_timeout_session); |
| 8085 | } |
| 8086 | |
| 8087 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 8088 | struct proxy *defpx, const char *file, int line, |
| 8089 | char **err) |
| 8090 | { |
| 8091 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8092 | file, line, err, &hlua_timeout_task); |
| 8093 | } |
| 8094 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8095 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 8096 | struct proxy *defpx, const char *file, int line, |
| 8097 | char **err) |
| 8098 | { |
| 8099 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8100 | file, line, err, &hlua_timeout_applet); |
| 8101 | } |
| 8102 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8103 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 8104 | struct proxy *defpx, const char *file, int line, |
| 8105 | char **err) |
| 8106 | { |
| 8107 | char *error; |
| 8108 | |
| 8109 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8110 | if (*error != '\0') { |
| 8111 | memprintf(err, "%s: invalid number", args[0]); |
| 8112 | return -1; |
| 8113 | } |
| 8114 | return 0; |
| 8115 | } |
| 8116 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8117 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 8118 | struct proxy *defpx, const char *file, int line, |
| 8119 | char **err) |
| 8120 | { |
| 8121 | char *error; |
| 8122 | |
| 8123 | if (*(args[1]) == 0) { |
| 8124 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8125 | return -1; |
| 8126 | } |
| 8127 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8128 | if (*error != '\0') { |
| 8129 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8130 | return -1; |
| 8131 | } |
| 8132 | return 0; |
| 8133 | } |
| 8134 | |
| 8135 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8136 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8137 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8138 | * the main lua entry point. |
| 8139 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8140 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8141 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8142 | * |
| 8143 | * In some error case, LUA set an error message in top of the stack. This function |
| 8144 | * 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] | 8145 | * |
| 8146 | * This function can fail with an abort() due to an Lua critical error. |
| 8147 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8148 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8149 | */ |
| 8150 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 8151 | struct proxy *defpx, const char *file, int line, |
| 8152 | char **err) |
| 8153 | { |
| 8154 | int error; |
| 8155 | |
| 8156 | /* Just load and compile the file. */ |
| 8157 | error = luaL_loadfile(gL.T, args[1]); |
| 8158 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8159 | 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] | 8160 | lua_pop(gL.T, 1); |
| 8161 | return -1; |
| 8162 | } |
| 8163 | |
| 8164 | /* If no syntax error where detected, execute the code. */ |
| 8165 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 8166 | switch (error) { |
| 8167 | case LUA_OK: |
| 8168 | break; |
| 8169 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8170 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8171 | lua_pop(gL.T, 1); |
| 8172 | return -1; |
| 8173 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8174 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8175 | return -1; |
| 8176 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8177 | 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] | 8178 | lua_pop(gL.T, 1); |
| 8179 | return -1; |
| 8180 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8181 | 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] | 8182 | lua_pop(gL.T, 1); |
| 8183 | return -1; |
| 8184 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8185 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8186 | lua_pop(gL.T, 1); |
| 8187 | return -1; |
| 8188 | } |
| 8189 | |
| 8190 | return 0; |
| 8191 | } |
| 8192 | |
| 8193 | /* configuration keywords declaration */ |
| 8194 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8195 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8196 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8197 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8198 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8199 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8200 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8201 | { 0, NULL, NULL }, |
| 8202 | }}; |
| 8203 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8204 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8205 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8206 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8207 | /* This function can fail with an abort() due to an Lua critical error. |
| 8208 | * We are in the initialisation process of HAProxy, this abort() is |
| 8209 | * tolerated. |
| 8210 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8211 | int hlua_post_init() |
| 8212 | { |
| 8213 | struct hlua_init_function *init; |
| 8214 | const char *msg; |
| 8215 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8216 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8217 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8218 | /* Call post initialisation function in safe environement. */ |
| 8219 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8220 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8221 | error = lua_tostring(gL.T, -1); |
| 8222 | else |
| 8223 | error = "critical error"; |
| 8224 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8225 | exit(1); |
| 8226 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8227 | |
| 8228 | #if USE_OPENSSL |
| 8229 | /* Initialize SSL server. */ |
| 8230 | if (socket_ssl.xprt->prepare_srv) { |
| 8231 | int saved_used_backed = global.ssl_used_backend; |
| 8232 | // don't affect maxconn automatic computation |
| 8233 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8234 | global.ssl_used_backend = saved_used_backed; |
| 8235 | } |
| 8236 | #endif |
| 8237 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8238 | hlua_fcn_post_init(gL.T); |
| 8239 | RESET_SAFE_LJMP(gL.T); |
| 8240 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8241 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8242 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 8243 | ret = hlua_ctx_resume(&gL, 0); |
| 8244 | switch (ret) { |
| 8245 | case HLUA_E_OK: |
| 8246 | lua_pop(gL.T, -1); |
| 8247 | return 1; |
| 8248 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8249 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8250 | return 0; |
| 8251 | case HLUA_E_ERRMSG: |
| 8252 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8253 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8254 | return 0; |
| 8255 | case HLUA_E_ERR: |
| 8256 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8257 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8258 | return 0; |
| 8259 | } |
| 8260 | } |
| 8261 | return 1; |
| 8262 | } |
| 8263 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8264 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8265 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8266 | * is the previously allocated size or the kind of object in case of a new |
| 8267 | * allocation. <nsize> is the requested new size. |
| 8268 | */ |
| 8269 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8270 | { |
| 8271 | struct hlua_mem_allocator *zone = ud; |
| 8272 | |
| 8273 | if (nsize == 0) { |
| 8274 | /* it's a free */ |
| 8275 | if (ptr) |
| 8276 | zone->allocated -= osize; |
| 8277 | free(ptr); |
| 8278 | return NULL; |
| 8279 | } |
| 8280 | |
| 8281 | if (!ptr) { |
| 8282 | /* it's a new allocation */ |
| 8283 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8284 | return NULL; |
| 8285 | |
| 8286 | ptr = malloc(nsize); |
| 8287 | if (ptr) |
| 8288 | zone->allocated += nsize; |
| 8289 | return ptr; |
| 8290 | } |
| 8291 | |
| 8292 | /* it's a realloc */ |
| 8293 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8294 | return NULL; |
| 8295 | |
| 8296 | ptr = realloc(ptr, nsize); |
| 8297 | if (ptr) |
| 8298 | zone->allocated += nsize - osize; |
| 8299 | return ptr; |
| 8300 | } |
| 8301 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8302 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8303 | * We are in the initialisation process of HAProxy, this abort() is |
| 8304 | * tolerated. |
| 8305 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8306 | void hlua_init(void) |
| 8307 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8308 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8309 | int idx; |
| 8310 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8311 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8312 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8313 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8314 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8315 | struct srv_kw *kw; |
| 8316 | int tmp_error; |
| 8317 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8318 | char *args[] = { /* SSL client configuration. */ |
| 8319 | "ssl", |
| 8320 | "verify", |
| 8321 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8322 | NULL |
| 8323 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8324 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8325 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8326 | /* Init main lua stack. */ |
| 8327 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8328 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8329 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8330 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8331 | hlua_sethlua(&gL); |
| 8332 | gL.Tref = LUA_REFNIL; |
| 8333 | gL.task = NULL; |
| 8334 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8335 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8336 | * the Lua function can fail with an abort. We are in the initialisation |
| 8337 | * process of HAProxy, this abort() is tolerated. |
| 8338 | */ |
| 8339 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8340 | /* Initialise lua. */ |
| 8341 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8342 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8343 | /* Set safe environment for the initialisation. */ |
| 8344 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8345 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8346 | error_msg = lua_tostring(gL.T, -1); |
| 8347 | else |
| 8348 | error_msg = "critical error"; |
| 8349 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8350 | exit(1); |
| 8351 | } |
| 8352 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8353 | /* |
| 8354 | * |
| 8355 | * Create "core" object. |
| 8356 | * |
| 8357 | */ |
| 8358 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8359 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8360 | lua_newtable(gL.T); |
| 8361 | |
| 8362 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8363 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8364 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8365 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8366 | /* Register special functions. */ |
| 8367 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8368 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8369 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8370 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8371 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8372 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8373 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8374 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8375 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8376 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8377 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8378 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8379 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8380 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8381 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8382 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8383 | hlua_class_function(gL.T, "log", hlua_log); |
| 8384 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8385 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8386 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8387 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8388 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8389 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8390 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8391 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8392 | |
| 8393 | /* |
| 8394 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8395 | * Register class Map |
| 8396 | * |
| 8397 | */ |
| 8398 | |
| 8399 | /* This table entry is the object "Map" base. */ |
| 8400 | lua_newtable(gL.T); |
| 8401 | |
| 8402 | /* register pattern types. */ |
| 8403 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8404 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8405 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8406 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8407 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8408 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8409 | |
| 8410 | /* register constructor. */ |
| 8411 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8412 | |
| 8413 | /* Create and fill the metatable. */ |
| 8414 | lua_newtable(gL.T); |
| 8415 | |
| 8416 | /* Create and fille the __index entry. */ |
| 8417 | lua_pushstring(gL.T, "__index"); |
| 8418 | lua_newtable(gL.T); |
| 8419 | |
| 8420 | /* Register . */ |
| 8421 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8422 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8423 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8424 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8425 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8426 | /* Register previous table in the registry with reference and named entry. |
| 8427 | * The function hlua_register_metatable() pops the stack, so we |
| 8428 | * previously create a copy of the table. |
| 8429 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8430 | 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] | 8431 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8432 | |
| 8433 | /* Assign the metatable to the mai Map object. */ |
| 8434 | lua_setmetatable(gL.T, -2); |
| 8435 | |
| 8436 | /* Set a name to the table. */ |
| 8437 | lua_setglobal(gL.T, "Map"); |
| 8438 | |
| 8439 | /* |
| 8440 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8441 | * Register class Channel |
| 8442 | * |
| 8443 | */ |
| 8444 | |
| 8445 | /* Create and fill the metatable. */ |
| 8446 | lua_newtable(gL.T); |
| 8447 | |
| 8448 | /* Create and fille the __index entry. */ |
| 8449 | lua_pushstring(gL.T, "__index"); |
| 8450 | lua_newtable(gL.T); |
| 8451 | |
| 8452 | /* Register . */ |
| 8453 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8454 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8455 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8456 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8457 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8458 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8459 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8460 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8461 | 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] | 8462 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8463 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8464 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8465 | |
| 8466 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8467 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8468 | |
| 8469 | /* |
| 8470 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8471 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8472 | * |
| 8473 | */ |
| 8474 | |
| 8475 | /* Create and fill the metatable. */ |
| 8476 | lua_newtable(gL.T); |
| 8477 | |
| 8478 | /* Create and fille the __index entry. */ |
| 8479 | lua_pushstring(gL.T, "__index"); |
| 8480 | lua_newtable(gL.T); |
| 8481 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8482 | /* Browse existing fetches and create the associated |
| 8483 | * object method. |
| 8484 | */ |
| 8485 | sf = NULL; |
| 8486 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8487 | |
| 8488 | /* Dont register the keywork if the arguments check function are |
| 8489 | * not safe during the runtime. |
| 8490 | */ |
| 8491 | if ((sf->val_args != NULL) && |
| 8492 | (sf->val_args != val_payload_lv) && |
| 8493 | (sf->val_args != val_hdr)) |
| 8494 | continue; |
| 8495 | |
| 8496 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8497 | * by an underscore. |
| 8498 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8499 | strncpy(trash.area, sf->kw, trash.size); |
| 8500 | trash.area[trash.size - 1] = '\0'; |
| 8501 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8502 | if (*p == '.' || *p == '-' || *p == '+') |
| 8503 | *p = '_'; |
| 8504 | |
| 8505 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8506 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8507 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8508 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8509 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8510 | } |
| 8511 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8512 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8513 | |
| 8514 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8515 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8516 | |
| 8517 | /* |
| 8518 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8519 | * Register class Converters |
| 8520 | * |
| 8521 | */ |
| 8522 | |
| 8523 | /* Create and fill the metatable. */ |
| 8524 | lua_newtable(gL.T); |
| 8525 | |
| 8526 | /* Create and fill the __index entry. */ |
| 8527 | lua_pushstring(gL.T, "__index"); |
| 8528 | lua_newtable(gL.T); |
| 8529 | |
| 8530 | /* Browse existing converters and create the associated |
| 8531 | * object method. |
| 8532 | */ |
| 8533 | sc = NULL; |
| 8534 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8535 | /* Dont register the keywork if the arguments check function are |
| 8536 | * not safe during the runtime. |
| 8537 | */ |
| 8538 | if (sc->val_args != NULL) |
| 8539 | continue; |
| 8540 | |
| 8541 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8542 | * by an underscore. |
| 8543 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8544 | strncpy(trash.area, sc->kw, trash.size); |
| 8545 | trash.area[trash.size - 1] = '\0'; |
| 8546 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8547 | if (*p == '.' || *p == '-' || *p == '+') |
| 8548 | *p = '_'; |
| 8549 | |
| 8550 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8551 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8552 | lua_pushlightuserdata(gL.T, sc); |
| 8553 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8554 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8555 | } |
| 8556 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8557 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8558 | |
| 8559 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8560 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8561 | |
| 8562 | /* |
| 8563 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8564 | * Register class HTTP |
| 8565 | * |
| 8566 | */ |
| 8567 | |
| 8568 | /* Create and fill the metatable. */ |
| 8569 | lua_newtable(gL.T); |
| 8570 | |
| 8571 | /* Create and fille the __index entry. */ |
| 8572 | lua_pushstring(gL.T, "__index"); |
| 8573 | lua_newtable(gL.T); |
| 8574 | |
| 8575 | /* Register Lua functions. */ |
| 8576 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8577 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8578 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8579 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8580 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8581 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8582 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8583 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8584 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8585 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8586 | |
| 8587 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8588 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8589 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8590 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8591 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8592 | 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] | 8593 | 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] | 8594 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8595 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8596 | |
| 8597 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8598 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8599 | |
| 8600 | /* |
| 8601 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8602 | * Register class AppletTCP |
| 8603 | * |
| 8604 | */ |
| 8605 | |
| 8606 | /* Create and fill the metatable. */ |
| 8607 | lua_newtable(gL.T); |
| 8608 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8609 | /* Create and fille the __index entry. */ |
| 8610 | lua_pushstring(gL.T, "__index"); |
| 8611 | lua_newtable(gL.T); |
| 8612 | |
| 8613 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8614 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8615 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8616 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8617 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8618 | 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] | 8619 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8620 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8621 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8622 | |
| 8623 | lua_settable(gL.T, -3); |
| 8624 | |
| 8625 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8626 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8627 | |
| 8628 | /* |
| 8629 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8630 | * Register class AppletHTTP |
| 8631 | * |
| 8632 | */ |
| 8633 | |
| 8634 | /* Create and fill the metatable. */ |
| 8635 | lua_newtable(gL.T); |
| 8636 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8637 | /* Create and fille the __index entry. */ |
| 8638 | lua_pushstring(gL.T, "__index"); |
| 8639 | lua_newtable(gL.T); |
| 8640 | |
| 8641 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8642 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8643 | 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] | 8644 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8645 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8646 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8647 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8648 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8649 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8650 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8651 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8652 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8653 | |
| 8654 | lua_settable(gL.T, -3); |
| 8655 | |
| 8656 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8657 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8658 | |
| 8659 | /* |
| 8660 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8661 | * Register class TXN |
| 8662 | * |
| 8663 | */ |
| 8664 | |
| 8665 | /* Create and fill the metatable. */ |
| 8666 | lua_newtable(gL.T); |
| 8667 | |
| 8668 | /* Create and fille the __index entry. */ |
| 8669 | lua_pushstring(gL.T, "__index"); |
| 8670 | lua_newtable(gL.T); |
| 8671 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8672 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8673 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8674 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8675 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8676 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8677 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8678 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 8679 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8680 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8681 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8682 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8683 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8684 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8685 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8686 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8687 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8688 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8689 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8690 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8691 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8692 | |
| 8693 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8694 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8695 | |
| 8696 | /* |
| 8697 | * |
| 8698 | * Register class Socket |
| 8699 | * |
| 8700 | */ |
| 8701 | |
| 8702 | /* Create and fill the metatable. */ |
| 8703 | lua_newtable(gL.T); |
| 8704 | |
| 8705 | /* Create and fille the __index entry. */ |
| 8706 | lua_pushstring(gL.T, "__index"); |
| 8707 | lua_newtable(gL.T); |
| 8708 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8709 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8710 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8711 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8712 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8713 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8714 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8715 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8716 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8717 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8718 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8719 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8720 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8721 | 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] | 8722 | |
| 8723 | /* Register the garbage collector entry. */ |
| 8724 | lua_pushstring(gL.T, "__gc"); |
| 8725 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8726 | 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] | 8727 | |
| 8728 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8729 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8730 | |
| 8731 | /* Proxy and server configuration initialisation. */ |
| 8732 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8733 | init_new_proxy(&socket_proxy); |
| 8734 | socket_proxy.parent = NULL; |
| 8735 | socket_proxy.last_change = now.tv_sec; |
| 8736 | socket_proxy.id = "LUA-SOCKET"; |
| 8737 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8738 | socket_proxy.maxconn = 0; |
| 8739 | socket_proxy.accept = NULL; |
| 8740 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8741 | socket_proxy.srv = NULL; |
| 8742 | socket_proxy.conn_retries = 0; |
| 8743 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8744 | |
| 8745 | /* Init TCP server: unchanged parameters */ |
| 8746 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8747 | socket_tcp.next = NULL; |
| 8748 | socket_tcp.proxy = &socket_proxy; |
| 8749 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8750 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8751 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8752 | socket_tcp.priv_conns = NULL; |
| 8753 | socket_tcp.idle_conns = NULL; |
| 8754 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8755 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8756 | socket_tcp.last_change = 0; |
| 8757 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8758 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8759 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8760 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8761 | |
| 8762 | /* XXX: Copy default parameter from default server, |
| 8763 | * but the default server is not initialized. |
| 8764 | */ |
| 8765 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8766 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8767 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8768 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8769 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8770 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8771 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8772 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8773 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8774 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8775 | |
| 8776 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8777 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8778 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8779 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8780 | socket_tcp.check.server = &socket_tcp; |
| 8781 | |
| 8782 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8783 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8784 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8785 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8786 | socket_tcp.agent.server = &socket_tcp; |
| 8787 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8788 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8789 | |
| 8790 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8791 | /* Init TCP server: unchanged parameters */ |
| 8792 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8793 | socket_ssl.next = NULL; |
| 8794 | socket_ssl.proxy = &socket_proxy; |
| 8795 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8796 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8797 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8798 | socket_ssl.priv_conns = NULL; |
| 8799 | socket_ssl.idle_conns = NULL; |
| 8800 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8801 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8802 | socket_ssl.last_change = 0; |
| 8803 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8804 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8805 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8806 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8807 | |
| 8808 | /* XXX: Copy default parameter from default server, |
| 8809 | * but the default server is not initialized. |
| 8810 | */ |
| 8811 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8812 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8813 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8814 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8815 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8816 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8817 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8818 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8819 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8820 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8821 | |
| 8822 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8823 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8824 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8825 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8826 | socket_ssl.check.server = &socket_ssl; |
| 8827 | |
| 8828 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8829 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8830 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8831 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8832 | socket_ssl.agent.server = &socket_ssl; |
| 8833 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8834 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8835 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8836 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8837 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8838 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8839 | /* |
| 8840 | * |
| 8841 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8842 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8843 | * features like client certificates and ssl_verify. |
| 8844 | * |
| 8845 | */ |
| 8846 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8847 | if (tmp_error != 0) { |
| 8848 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8849 | abort(); /* This must be never arrives because the command line |
| 8850 | not editable by the user. */ |
| 8851 | } |
| 8852 | idx += kw->skip; |
| 8853 | } |
| 8854 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8855 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8856 | |
| 8857 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8858 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8859 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8860 | static void hlua_register_build_options(void) |
| 8861 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8862 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8863 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8864 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8865 | hap_register_build_opts(ptr, 1); |
| 8866 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8867 | |
| 8868 | INITCALL0(STG_REGISTER, hlua_register_build_options); |