Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 14 | #include <limits.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 15 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 16 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 17 | #include <lauxlib.h> |
| 18 | #include <lua.h> |
| 19 | #include <lualib.h> |
| 20 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 21 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 22 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 23 | #endif |
| 24 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 25 | #include <ebpttree.h> |
| 26 | |
| 27 | #include <common/cfgparse.h> |
Willy Tarreau | b059b89 | 2018-10-16 17:57:36 +0200 | [diff] [blame] | 28 | #include <common/compiler.h> |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 29 | #include <common/hathreads.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 30 | #include <common/initcall.h> |
| 31 | #include <common/xref.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 32 | #include <common/h1.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 33 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 34 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 35 | #include <types/hlua.h> |
| 36 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 37 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 38 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 39 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 40 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 41 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 42 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 43 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 44 | #include <proto/stats.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 45 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 46 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 47 | #include <proto/hlua_fcn.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 48 | #include <proto/http_fetch.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 49 | #include <proto/http_htx.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 50 | #include <proto/http_rules.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 51 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 52 | #include <proto/obj_type.h> |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 53 | #include <proto/queue.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 54 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 55 | #include <proto/payload.h> |
| 56 | #include <proto/proto_http.h> |
| 57 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 58 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 59 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 60 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 61 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 62 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 63 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 64 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 65 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 66 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 67 | * macro is used only for identifying the function that can |
| 68 | * not return because a longjmp is executed. |
| 69 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 70 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 71 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 72 | */ |
| 73 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 74 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 75 | #define MAY_LJMP(func) func |
| 76 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 77 | /* This couple of function executes securely some Lua calls outside of |
| 78 | * the lua runtime environment. Each Lua call can return a longjmp |
| 79 | * if it encounter a memory error. |
| 80 | * |
| 81 | * Lua documentation extract: |
| 82 | * |
| 83 | * If an error happens outside any protected environment, Lua calls |
| 84 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 85 | * exiting the host application. Your panic function can avoid this |
| 86 | * exit by never returning (e.g., doing a long jump to your own |
| 87 | * recovery point outside Lua). |
| 88 | * |
| 89 | * The panic function runs as if it were a message handler (see |
| 90 | * §2.3); in particular, the error message is at the top of the |
| 91 | * stack. However, there is no guarantee about stack space. To push |
| 92 | * anything on the stack, the panic function must first check the |
| 93 | * available space (see §4.2). |
| 94 | * |
| 95 | * We must check all the Lua entry point. This includes: |
| 96 | * - The include/proto/hlua.h exported functions |
| 97 | * - the task wrapper function |
| 98 | * - The action wrapper function |
| 99 | * - The converters wrapper function |
| 100 | * - The sample-fetch wrapper functions |
| 101 | * |
| 102 | * It is tolerated that the initilisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 103 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 104 | * |
| 105 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 106 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 107 | * because they must be exists in the program stack when the longjmp |
| 108 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 109 | * |
| 110 | * Note that the Lua processing is not really thread safe. It provides |
| 111 | * heavy system which consists to add our own lock function in the Lua |
| 112 | * code and recompile the library. This system will probably not accepted |
| 113 | * by maintainers of various distribs. |
| 114 | * |
| 115 | * Our main excution point of the Lua is the function lua_resume(). A |
| 116 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 117 | * of function and a lua_unlock() at the end of the function. So I |
| 118 | * conclude that the Lua thread safe mode just perform a mutex around |
| 119 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 120 | * easier for distro maintainers. |
| 121 | * |
| 122 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 123 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 124 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 125 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 126 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 127 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 128 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 129 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 130 | |
| 131 | #define SET_SAFE_LJMP(__L) \ |
| 132 | ({ \ |
| 133 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 134 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 135 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 136 | lua_atpanic(__L, hlua_panic_safe); \ |
| 137 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 138 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 139 | } else { \ |
| 140 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 141 | ret = 1; \ |
| 142 | } \ |
| 143 | ret; \ |
| 144 | }) |
| 145 | |
| 146 | /* If we are the last function catching Lua errors, we |
| 147 | * must reset the panic function. |
| 148 | */ |
| 149 | #define RESET_SAFE_LJMP(__L) \ |
| 150 | do { \ |
| 151 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 152 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 153 | } while(0) |
| 154 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 155 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 156 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 157 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 158 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 159 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 160 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 161 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 162 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 163 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 164 | /* The main Lua execution context. */ |
| 165 | struct hlua gL; |
| 166 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 167 | /* This is the memory pool containing struct lua for applets |
| 168 | * (including cli). |
| 169 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 170 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 171 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 172 | /* Used for Socket connection. */ |
| 173 | static struct proxy socket_proxy; |
| 174 | static struct server socket_tcp; |
| 175 | #ifdef USE_OPENSSL |
| 176 | static struct server socket_ssl; |
| 177 | #endif |
| 178 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 179 | /* List head of the function called at the initialisation time. */ |
| 180 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 181 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 182 | /* The following variables contains the reference of the different |
| 183 | * Lua classes. These references are useful for identify metadata |
| 184 | * associated with an object. |
| 185 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 186 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 187 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 188 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 189 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 190 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 191 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 192 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 193 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 194 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 195 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 196 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 197 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 198 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 199 | * because a task may remain alive during all the haproxy execution. |
| 200 | */ |
| 201 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 202 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 203 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 204 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 205 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 206 | * it is used for preventing infinite loops. |
| 207 | * |
| 208 | * I test the scheer with an infinite loop containing one incrementation |
| 209 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 210 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 211 | * to one interrupt each 10 000 instructions. |
| 212 | * |
| 213 | * configured | Number of |
| 214 | * instructions | loops executed |
| 215 | * between two | in milions |
| 216 | * forced yields | |
| 217 | * ---------------+--------------- |
| 218 | * 10 | 160 |
| 219 | * 500 | 670 |
| 220 | * 1000 | 680 |
| 221 | * 5000 | 700 |
| 222 | * 7000 | 700 |
| 223 | * 8000 | 700 |
| 224 | * 9000 | 710 <- ceil |
| 225 | * 10000 | 710 |
| 226 | * 100000 | 710 |
| 227 | * 1000000 | 710 |
| 228 | * |
| 229 | */ |
| 230 | static unsigned int hlua_nb_instruction = 10000; |
| 231 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 232 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 233 | * be enforced on any memory allocation. |
| 234 | */ |
| 235 | struct hlua_mem_allocator { |
| 236 | size_t allocated; |
| 237 | size_t limit; |
| 238 | }; |
| 239 | |
| 240 | static struct hlua_mem_allocator hlua_global_allocator; |
| 241 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 242 | static const char error_500[] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 243 | "HTTP/1.0 500 Internal Server Error\r\n" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 244 | "Cache-Control: no-cache\r\n" |
| 245 | "Connection: close\r\n" |
| 246 | "Content-Type: text/html\r\n" |
| 247 | "\r\n" |
Joseph Herlant | 7fe1577 | 2018-11-15 10:07:32 -0800 | [diff] [blame] | 248 | "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occurred.\n</body></html>\n"; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 249 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 250 | /* These functions converts types between HAProxy internal args or |
| 251 | * sample and LUA types. Another function permits to check if the |
| 252 | * LUA stack contains arguments according with an required ARG_T |
| 253 | * format. |
| 254 | */ |
| 255 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 256 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 257 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 258 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 259 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 260 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 261 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 262 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 263 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 264 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 265 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 266 | do { \ |
| 267 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 268 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 269 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 270 | } while (0) |
| 271 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 272 | /* Used to check an Lua function type in the stack. It creates and |
| 273 | * returns a reference of the function. This function throws an |
| 274 | * error if the rgument is not a "function". |
| 275 | */ |
| 276 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 277 | { |
| 278 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 279 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno)); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 280 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 281 | } |
| 282 | lua_pushvalue(L, argno); |
| 283 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 284 | } |
| 285 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 286 | /* Return the string that is of the top of the stack. */ |
| 287 | const char *hlua_get_top_error_string(lua_State *L) |
| 288 | { |
| 289 | if (lua_gettop(L) < 1) |
| 290 | return "unknown error"; |
| 291 | if (lua_type(L, -1) != LUA_TSTRING) |
| 292 | return "unknown error"; |
| 293 | return lua_tostring(L, -1); |
| 294 | } |
| 295 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 296 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 297 | { |
| 298 | lua_Debug ar; |
| 299 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 300 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 301 | int filled = 0; |
| 302 | |
| 303 | while (lua_getstack(L, level++, &ar)) { |
| 304 | |
| 305 | /* Add separator */ |
| 306 | if (filled) |
| 307 | chunk_appendf(msg, ", "); |
| 308 | filled = 1; |
| 309 | |
| 310 | /* Fill fields: |
| 311 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 312 | * 'l': fills in the field currentline; |
| 313 | * 'n': fills in the field name and namewhat; |
| 314 | * 't': fills in the field istailcall; |
| 315 | */ |
| 316 | lua_getinfo(L, "Slnt", &ar); |
| 317 | |
| 318 | /* Append code localisation */ |
| 319 | if (ar.currentline > 0) |
| 320 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 321 | else |
| 322 | chunk_appendf(msg, "%s ", ar.short_src); |
| 323 | |
| 324 | /* |
| 325 | * Get function name |
| 326 | * |
| 327 | * if namewhat is no empty, name is defined. |
| 328 | * what contains "Lua" for Lua function, "C" for C function, |
| 329 | * or "main" for main code. |
| 330 | */ |
| 331 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 332 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 333 | |
| 334 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 335 | chunk_appendf(msg, "main chunk"); |
| 336 | |
| 337 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 338 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 339 | |
| 340 | else /* nothing left... */ |
| 341 | chunk_appendf(msg, "?"); |
| 342 | |
| 343 | |
| 344 | /* Display tailed call */ |
| 345 | if (ar.istailcall) |
| 346 | chunk_appendf(msg, " ..."); |
| 347 | } |
| 348 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 349 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 353 | /* This function check the number of arguments available in the |
| 354 | * stack. If the number of arguments available is not the same |
| 355 | * then <nb> an error is throwed. |
| 356 | */ |
| 357 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 358 | { |
| 359 | if (lua_gettop(L) == nb) |
| 360 | return; |
| 361 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 362 | } |
| 363 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 364 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 365 | * and the line number where the error is encountered. |
| 366 | */ |
| 367 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 368 | { |
| 369 | va_list argp; |
| 370 | va_start(argp, fmt); |
| 371 | luaL_where(L, 1); |
| 372 | lua_pushvfstring(L, fmt, argp); |
| 373 | va_end(argp); |
| 374 | lua_concat(L, 2); |
| 375 | return 1; |
| 376 | } |
| 377 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 378 | /* This functions is used with sample fetch and converters. It |
| 379 | * converts the HAProxy configuration argument in a lua stack |
| 380 | * values. |
| 381 | * |
| 382 | * It takes an array of "arg", and each entry of the array is |
| 383 | * converted and pushed in the LUA stack. |
| 384 | */ |
| 385 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 386 | { |
| 387 | switch (arg->type) { |
| 388 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 389 | case ARGT_TIME: |
| 390 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 391 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 392 | break; |
| 393 | |
| 394 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 395 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 396 | break; |
| 397 | |
| 398 | case ARGT_IPV4: |
| 399 | case ARGT_IPV6: |
| 400 | case ARGT_MSK4: |
| 401 | case ARGT_MSK6: |
| 402 | case ARGT_FE: |
| 403 | case ARGT_BE: |
| 404 | case ARGT_TAB: |
| 405 | case ARGT_SRV: |
| 406 | case ARGT_USR: |
| 407 | case ARGT_MAP: |
| 408 | default: |
| 409 | lua_pushnil(L); |
| 410 | break; |
| 411 | } |
| 412 | return 1; |
| 413 | } |
| 414 | |
| 415 | /* This function take one entrie in an LUA stack at the index "ud", |
| 416 | * and try to convert it in an HAProxy argument entry. This is useful |
| 417 | * with sample fetch wrappers. The input arguments are gived to the |
| 418 | * lua wrapper and converted as arg list by thi function. |
| 419 | */ |
| 420 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 421 | { |
| 422 | switch (lua_type(L, ud)) { |
| 423 | |
| 424 | case LUA_TNUMBER: |
| 425 | case LUA_TBOOLEAN: |
| 426 | arg->type = ARGT_SINT; |
| 427 | arg->data.sint = lua_tointeger(L, ud); |
| 428 | break; |
| 429 | |
| 430 | case LUA_TSTRING: |
| 431 | arg->type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 432 | arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data); |
Tim Duesterhus | f371b83 | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 433 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 434 | arg->data.str.size = arg->data.str.data; |
| 435 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 436 | break; |
| 437 | |
| 438 | case LUA_TUSERDATA: |
| 439 | case LUA_TNIL: |
| 440 | case LUA_TTABLE: |
| 441 | case LUA_TFUNCTION: |
| 442 | case LUA_TTHREAD: |
| 443 | case LUA_TLIGHTUSERDATA: |
| 444 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 445 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 446 | break; |
| 447 | } |
| 448 | return 1; |
| 449 | } |
| 450 | |
| 451 | /* the following functions are used to convert a struct sample |
| 452 | * in Lua type. This useful to convert the return of the |
| 453 | * fetchs or converters. |
| 454 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 455 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 456 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 457 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 458 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 459 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 460 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 461 | break; |
| 462 | |
| 463 | case SMP_T_BIN: |
| 464 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 465 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 466 | break; |
| 467 | |
| 468 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 469 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 470 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 471 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 472 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 473 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 474 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 475 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 476 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 477 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 478 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 479 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 480 | break; |
| 481 | default: |
| 482 | lua_pushnil(L); |
| 483 | break; |
| 484 | } |
| 485 | break; |
| 486 | |
| 487 | case SMP_T_IPV4: |
| 488 | case SMP_T_IPV6: |
| 489 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 490 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 491 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 492 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 493 | else |
| 494 | lua_pushnil(L); |
| 495 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 496 | default: |
| 497 | lua_pushnil(L); |
| 498 | break; |
| 499 | } |
| 500 | return 1; |
| 501 | } |
| 502 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 503 | /* the following functions are used to convert a struct sample |
| 504 | * in Lua strings. This is useful to convert the return of the |
| 505 | * fetchs or converters. |
| 506 | */ |
| 507 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 508 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 509 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 510 | |
| 511 | case SMP_T_BIN: |
| 512 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 513 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 514 | break; |
| 515 | |
| 516 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 517 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 518 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 519 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 520 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 521 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 522 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 523 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 524 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 525 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 526 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 527 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 528 | break; |
| 529 | default: |
| 530 | lua_pushstring(L, ""); |
| 531 | break; |
| 532 | } |
| 533 | break; |
| 534 | |
| 535 | case SMP_T_SINT: |
| 536 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 537 | case SMP_T_IPV4: |
| 538 | case SMP_T_IPV6: |
| 539 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 540 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 541 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 542 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 543 | else |
| 544 | lua_pushstring(L, ""); |
| 545 | break; |
| 546 | default: |
| 547 | lua_pushstring(L, ""); |
| 548 | break; |
| 549 | } |
| 550 | return 1; |
| 551 | } |
| 552 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 553 | /* the following functions are used to convert an Lua type in a |
| 554 | * struct sample. This is useful to provide data from a converter |
| 555 | * to the LUA code. |
| 556 | */ |
| 557 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 558 | { |
| 559 | switch (lua_type(L, ud)) { |
| 560 | |
| 561 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 562 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 563 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 564 | break; |
| 565 | |
| 566 | |
| 567 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 568 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 569 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 570 | break; |
| 571 | |
| 572 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 573 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 574 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 575 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data); |
Tim Duesterhus | f371b83 | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 576 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 577 | smp->data.u.str.size = smp->data.u.str.data; |
| 578 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 579 | break; |
| 580 | |
| 581 | case LUA_TUSERDATA: |
| 582 | case LUA_TNIL: |
| 583 | case LUA_TTABLE: |
| 584 | case LUA_TFUNCTION: |
| 585 | case LUA_TTHREAD: |
| 586 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 587 | case LUA_TNONE: |
| 588 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 589 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 590 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 591 | break; |
| 592 | } |
| 593 | return 1; |
| 594 | } |
| 595 | |
| 596 | /* This function check the "argp" builded by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 597 | * is in accord with the expected argp defined by the "mask". The function |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 598 | * returns true or false. It can be adjust the types if there compatibles. |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 599 | * |
| 600 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 601 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 602 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 603 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 604 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 605 | { |
| 606 | int min_arg; |
| 607 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 608 | struct proxy *px; |
| 609 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 610 | |
| 611 | idx = 0; |
| 612 | min_arg = ARGM(mask); |
| 613 | mask >>= ARGM_BITS; |
| 614 | |
| 615 | while (1) { |
| 616 | |
| 617 | /* Check oversize. */ |
| 618 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 619 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | /* Check for mandatory arguments. */ |
| 623 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 624 | if (idx < min_arg) { |
| 625 | |
| 626 | /* If miss other argument than the first one, we return an error. */ |
| 627 | if (idx > 0) |
| 628 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 629 | |
| 630 | /* If first argument have a certain type, some default values |
| 631 | * may be used. See the function smp_resolve_args(). |
| 632 | */ |
| 633 | switch (mask & ARGT_MASK) { |
| 634 | |
| 635 | case ARGT_FE: |
| 636 | if (!(p->cap & PR_CAP_FE)) |
| 637 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 638 | argp[idx].data.prx = p; |
| 639 | argp[idx].type = ARGT_FE; |
| 640 | argp[idx+1].type = ARGT_STOP; |
| 641 | break; |
| 642 | |
| 643 | case ARGT_BE: |
| 644 | if (!(p->cap & PR_CAP_BE)) |
| 645 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 646 | argp[idx].data.prx = p; |
| 647 | argp[idx].type = ARGT_BE; |
| 648 | argp[idx+1].type = ARGT_STOP; |
| 649 | break; |
| 650 | |
| 651 | case ARGT_TAB: |
| 652 | argp[idx].data.prx = p; |
| 653 | argp[idx].type = ARGT_TAB; |
| 654 | argp[idx+1].type = ARGT_STOP; |
| 655 | break; |
| 656 | |
| 657 | default: |
| 658 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 659 | break; |
| 660 | } |
| 661 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 662 | return 0; |
| 663 | } |
| 664 | |
| 665 | /* Check for exceed the number of requiered argument. */ |
| 666 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 667 | argp[idx].type != ARGT_STOP) { |
| 668 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 669 | } |
| 670 | |
| 671 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 672 | argp[idx].type == ARGT_STOP) { |
| 673 | return 0; |
| 674 | } |
| 675 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 676 | /* Convert some argument types. */ |
| 677 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 678 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 679 | if (argp[idx].type != ARGT_SINT) |
| 680 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 681 | argp[idx].type = ARGT_SINT; |
| 682 | break; |
| 683 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 684 | case ARGT_TIME: |
| 685 | if (argp[idx].type != ARGT_SINT) |
| 686 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 687 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 688 | break; |
| 689 | |
| 690 | case ARGT_SIZE: |
| 691 | if (argp[idx].type != ARGT_SINT) |
| 692 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 693 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 694 | break; |
| 695 | |
| 696 | case ARGT_FE: |
| 697 | if (argp[idx].type != ARGT_STR) |
| 698 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 699 | memcpy(trash.area, argp[idx].data.str.area, |
| 700 | argp[idx].data.str.data); |
| 701 | trash.area[argp[idx].data.str.data] = 0; |
| 702 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 703 | if (!argp[idx].data.prx) |
| 704 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 705 | argp[idx].type = ARGT_FE; |
| 706 | break; |
| 707 | |
| 708 | case ARGT_BE: |
| 709 | if (argp[idx].type != ARGT_STR) |
| 710 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 711 | memcpy(trash.area, argp[idx].data.str.area, |
| 712 | argp[idx].data.str.data); |
| 713 | trash.area[argp[idx].data.str.data] = 0; |
| 714 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 715 | if (!argp[idx].data.prx) |
| 716 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 717 | argp[idx].type = ARGT_BE; |
| 718 | break; |
| 719 | |
| 720 | case ARGT_TAB: |
| 721 | if (argp[idx].type != ARGT_STR) |
| 722 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 723 | memcpy(trash.area, argp[idx].data.str.area, |
| 724 | argp[idx].data.str.data); |
| 725 | trash.area[argp[idx].data.str.data] = 0; |
Tim Duesterhus | e351e76 | 2019-09-29 23:03:09 +0200 | [diff] [blame] | 726 | argp[idx].data.t = stktable_find_by_name(trash.area); |
| 727 | if (!argp[idx].data.t) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 728 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 729 | argp[idx].type = ARGT_TAB; |
| 730 | break; |
| 731 | |
| 732 | case ARGT_SRV: |
| 733 | if (argp[idx].type != ARGT_STR) |
| 734 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 735 | memcpy(trash.area, argp[idx].data.str.area, |
| 736 | argp[idx].data.str.data); |
| 737 | trash.area[argp[idx].data.str.data] = 0; |
| 738 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 739 | if (sname) { |
| 740 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 741 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 742 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 743 | if (!px) |
| 744 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 745 | } |
| 746 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 747 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 748 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 749 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 750 | argp[idx].data.srv = findserver(px, sname); |
| 751 | if (!argp[idx].data.srv) |
| 752 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 753 | argp[idx].type = ARGT_SRV; |
| 754 | break; |
| 755 | |
| 756 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 757 | memcpy(trash.area, argp[idx].data.str.area, |
| 758 | argp[idx].data.str.data); |
| 759 | trash.area[argp[idx].data.str.data] = 0; |
| 760 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 761 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 762 | argp[idx].type = ARGT_IPV4; |
| 763 | break; |
| 764 | |
| 765 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 766 | memcpy(trash.area, argp[idx].data.str.area, |
| 767 | argp[idx].data.str.data); |
| 768 | trash.area[argp[idx].data.str.data] = 0; |
| 769 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 770 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 771 | argp[idx].type = ARGT_MSK4; |
| 772 | break; |
| 773 | |
| 774 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 775 | memcpy(trash.area, argp[idx].data.str.area, |
| 776 | argp[idx].data.str.data); |
| 777 | trash.area[argp[idx].data.str.data] = 0; |
| 778 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 779 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 780 | argp[idx].type = ARGT_IPV6; |
| 781 | break; |
| 782 | |
| 783 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 784 | memcpy(trash.area, argp[idx].data.str.area, |
| 785 | argp[idx].data.str.data); |
| 786 | trash.area[argp[idx].data.str.data] = 0; |
| 787 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 788 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 789 | argp[idx].type = ARGT_MSK6; |
| 790 | break; |
| 791 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 792 | case ARGT_MAP: |
| 793 | case ARGT_REG: |
| 794 | case ARGT_USR: |
| 795 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 796 | break; |
| 797 | } |
| 798 | |
| 799 | /* Check for type of argument. */ |
| 800 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 801 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 802 | arg_type_names[(mask & ARGT_MASK)], |
| 803 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 804 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 805 | } |
| 806 | |
| 807 | /* Next argument. */ |
| 808 | mask >>= ARGT_BITS; |
| 809 | idx++; |
| 810 | } |
| 811 | } |
| 812 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 813 | /* |
| 814 | * The following functions are used to make correspondance between the the |
| 815 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 816 | * |
| 817 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 818 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 819 | */ |
| 820 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 821 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 822 | struct hlua **hlua = lua_getextraspace(L); |
| 823 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 824 | } |
| 825 | static inline void hlua_sethlua(struct hlua *hlua) |
| 826 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 827 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 828 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 829 | } |
| 830 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 831 | /* This function is used to send logs. It try to send on screen (stderr) |
| 832 | * and on the default syslog server. |
| 833 | */ |
| 834 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 835 | { |
| 836 | struct tm tm; |
| 837 | char *p; |
| 838 | |
| 839 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 840 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 841 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 842 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 843 | /* Break the message if exceed the buffer size. */ |
| 844 | *(p-4) = ' '; |
| 845 | *(p-3) = '.'; |
| 846 | *(p-2) = '.'; |
| 847 | *(p-1) = '.'; |
| 848 | break; |
| 849 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 850 | if (isprint(*msg)) |
| 851 | *p = *msg; |
| 852 | else |
| 853 | *p = '.'; |
| 854 | } |
| 855 | *p = '\0'; |
| 856 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 857 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 858 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 859 | get_localtime(date.tv_sec, &tm); |
| 860 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 861 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 862 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 863 | fflush(stderr); |
| 864 | } |
| 865 | } |
| 866 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 867 | /* This function just ensure that the yield will be always |
| 868 | * returned with a timeout and permit to set some flags |
| 869 | */ |
| 870 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 871 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 872 | { |
| 873 | struct hlua *hlua = hlua_gethlua(L); |
| 874 | |
| 875 | /* Set the wake timeout. If timeout is required, we set |
| 876 | * the expiration time. |
| 877 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 878 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 879 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 880 | hlua->flags |= flags; |
| 881 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 882 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 883 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 884 | } |
| 885 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 886 | /* This function initialises the Lua environment stored in the stream. |
| 887 | * It must be called at the start of the stream. This function creates |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 888 | * an LUA coroutine. It can not be use to crete the main LUA context. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 889 | * |
| 890 | * This function is particular. it initialises a new Lua thread. If the |
| 891 | * initialisation fails (example: out of memory error), the lua function |
| 892 | * throws an error (longjmp). |
| 893 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 894 | * In some case (at least one), this function can be called from safe |
| 895 | * environement, so we must not initialise it. While the support of |
| 896 | * threads appear, the safe environment set a lock to ensure only one |
| 897 | * Lua execution at a time. If we initialize safe environment in another |
| 898 | * safe environmenet, we have a dead lock. |
| 899 | * |
| 900 | * set "already_safe" true if the context is initialized form safe |
| 901 | * Lua fonction. |
| 902 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 903 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 904 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 905 | * MUST NOT manipulate the created thread stack state, because it is not |
| 906 | * proctected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 907 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 908 | int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 909 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 910 | if (!already_safe) { |
| 911 | if (!SET_SAFE_LJMP(gL.T)) { |
| 912 | lua->Tref = LUA_REFNIL; |
| 913 | return 0; |
| 914 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 915 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 916 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 917 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 918 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 919 | lua->T = lua_newthread(gL.T); |
| 920 | if (!lua->T) { |
| 921 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 922 | if (!already_safe) |
| 923 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 924 | return 0; |
| 925 | } |
| 926 | hlua_sethlua(lua); |
| 927 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 928 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 929 | if (!already_safe) |
| 930 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 931 | return 1; |
| 932 | } |
| 933 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 934 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 935 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 936 | * is not freed. |
| 937 | */ |
| 938 | void hlua_ctx_destroy(struct hlua *lua) |
| 939 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 940 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 941 | return; |
| 942 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 943 | if (!lua->T) |
| 944 | goto end; |
| 945 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 946 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 947 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 948 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 949 | if (!SET_SAFE_LJMP(lua->T)) |
| 950 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 951 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 952 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 953 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 954 | if (!SET_SAFE_LJMP(gL.T)) |
| 955 | return; |
| 956 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 957 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 958 | /* Forces a garbage collecting process. If the Lua program is finished |
| 959 | * without error, we run the GC on the thread pointer. Its freed all |
| 960 | * the unused memory. |
| 961 | * If the thread is finnish with an error or is currently yielded, |
| 962 | * it seems that the GC applied on the thread doesn't clean anything, |
| 963 | * so e run the GC on the main thread. |
| 964 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 965 | * the garbage collection. |
| 966 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 967 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 968 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 969 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 970 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 971 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 972 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 973 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 974 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 975 | |
| 976 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 977 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | /* This function is used to restore the Lua context when a coroutine |
| 981 | * fails. This function copy the common memory between old coroutine |
| 982 | * and the new coroutine. The old coroutine is destroyed, and its |
| 983 | * replaced by the new coroutine. |
| 984 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 985 | * as string error message and it is copied in the new stack. |
| 986 | */ |
| 987 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 988 | { |
| 989 | lua_State *T; |
| 990 | int new_ref; |
| 991 | |
| 992 | /* Renew the main LUA stack doesn't have sense. */ |
| 993 | if (lua == &gL) |
| 994 | return 0; |
| 995 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 996 | /* New Lua coroutine. */ |
| 997 | T = lua_newthread(gL.T); |
| 998 | if (!T) |
| 999 | return 0; |
| 1000 | |
| 1001 | /* Copy last error message. */ |
| 1002 | if (keep_msg) |
| 1003 | lua_xmove(lua->T, T, 1); |
| 1004 | |
| 1005 | /* Copy data between the coroutines. */ |
| 1006 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1007 | lua_xmove(lua->T, T, 1); |
| 1008 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 1009 | |
| 1010 | /* Destroy old data. */ |
| 1011 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1012 | |
| 1013 | /* The thread is garbage collected by Lua. */ |
| 1014 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1015 | |
| 1016 | /* Fill the struct with the new coroutine values. */ |
| 1017 | lua->Mref = new_ref; |
| 1018 | lua->T = T; |
| 1019 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1020 | |
| 1021 | /* Set context. */ |
| 1022 | hlua_sethlua(lua); |
| 1023 | |
| 1024 | return 1; |
| 1025 | } |
| 1026 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1027 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1028 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1029 | struct hlua *hlua = hlua_gethlua(L); |
| 1030 | |
| 1031 | /* Lua cannot yield when its returning from a function, |
| 1032 | * so, we can fix the interrupt hook to 1 instruction, |
| 1033 | * expecting that the function is finnished. |
| 1034 | */ |
| 1035 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1036 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1037 | return; |
| 1038 | } |
| 1039 | |
| 1040 | /* restore the interrupt condition. */ |
| 1041 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1042 | |
| 1043 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1044 | * If the state is not yieldable, trying yield causes an error. |
| 1045 | */ |
| 1046 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1047 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1048 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1049 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1050 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1051 | hlua->run_time += now_ms - hlua->start_time; |
| 1052 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1053 | lua_pushfstring(L, "execution timeout"); |
| 1054 | WILL_LJMP(lua_error(L)); |
| 1055 | } |
| 1056 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1057 | /* Update the start time. */ |
| 1058 | hlua->start_time = now_ms; |
| 1059 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1060 | /* Try to interrupt the process at the end of the current |
| 1061 | * unyieldable function. |
| 1062 | */ |
| 1063 | lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1064 | } |
| 1065 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1066 | /* This function start or resumes the Lua stack execution. If the flag |
| 1067 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1068 | * The function return an error. |
| 1069 | * |
| 1070 | * The function can returns 4 values: |
| 1071 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1072 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1073 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1074 | * - HLUA_E_ERRMSG : An error has occurred, an error message is set in |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1075 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1076 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1077 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1078 | * If an error occurred, the stack is renewed and it is ready to run new |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1079 | * LUA code. |
| 1080 | */ |
| 1081 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1082 | { |
| 1083 | int ret; |
| 1084 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1085 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1086 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1087 | /* Initialise run time counter. */ |
| 1088 | if (!HLUA_IS_RUNNING(lua)) |
| 1089 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1090 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1091 | /* Lock the whole Lua execution. This lock must be before the |
| 1092 | * label "resume_execution". |
| 1093 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1094 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1095 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1096 | resume_execution: |
| 1097 | |
| 1098 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1099 | * instructions. it is used for preventing infinite loops. |
| 1100 | */ |
| 1101 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1102 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1103 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1104 | HLUA_SET_RUN(lua); |
| 1105 | HLUA_CLR_CTRLYIELD(lua); |
| 1106 | HLUA_CLR_WAKERESWR(lua); |
| 1107 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1108 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1109 | /* Update the start time. */ |
| 1110 | lua->start_time = now_ms; |
| 1111 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1112 | /* Call the function. */ |
| 1113 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1114 | switch (ret) { |
| 1115 | |
| 1116 | case LUA_OK: |
| 1117 | ret = HLUA_E_OK; |
| 1118 | break; |
| 1119 | |
| 1120 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1121 | /* Check if the execution timeout is expired. It it is the case, we |
| 1122 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1123 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1124 | tv_update_date(0, 1); |
| 1125 | lua->run_time += now_ms - lua->start_time; |
| 1126 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1127 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1128 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1129 | break; |
| 1130 | } |
| 1131 | /* Process the forced yield. if the general yield is not allowed or |
| 1132 | * if no task were associated this the current Lua execution |
| 1133 | * coroutine, we resume the execution. Else we want to return in the |
| 1134 | * scheduler and we want to be waked up again, to continue the |
| 1135 | * current Lua execution. So we schedule our own task. |
| 1136 | */ |
| 1137 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1138 | if (!yield_allowed || !lua->task) |
| 1139 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1140 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1141 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1142 | if (!yield_allowed) { |
| 1143 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1144 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1145 | break; |
| 1146 | } |
| 1147 | ret = HLUA_E_AGAIN; |
| 1148 | break; |
| 1149 | |
| 1150 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1151 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1152 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1153 | * because the errors ares the only one mean to return immediately |
| 1154 | * from and lua execution. |
| 1155 | */ |
| 1156 | if (lua->flags & HLUA_EXIT) { |
| 1157 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1158 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1159 | break; |
| 1160 | } |
| 1161 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1162 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1163 | if (!lua_checkstack(lua->T, 1)) { |
| 1164 | ret = HLUA_E_ERR; |
| 1165 | break; |
| 1166 | } |
| 1167 | msg = lua_tostring(lua->T, -1); |
| 1168 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1169 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1170 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1171 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1172 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1173 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1174 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1175 | ret = HLUA_E_ERRMSG; |
| 1176 | break; |
| 1177 | |
| 1178 | case LUA_ERRMEM: |
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 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1181 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1182 | break; |
| 1183 | |
| 1184 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1185 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1186 | if (!lua_checkstack(lua->T, 1)) { |
| 1187 | ret = HLUA_E_ERR; |
| 1188 | break; |
| 1189 | } |
| 1190 | msg = lua_tostring(lua->T, -1); |
| 1191 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1192 | lua_pop(lua->T, 1); |
| 1193 | if (msg) |
| 1194 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1195 | else |
| 1196 | lua_pushfstring(lua->T, "message handler error"); |
| 1197 | ret = HLUA_E_ERRMSG; |
| 1198 | break; |
| 1199 | |
| 1200 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1201 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1202 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1203 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1204 | break; |
| 1205 | } |
| 1206 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1207 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1208 | if (lua->flags & HLUA_MUST_GC && |
| 1209 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1210 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1211 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1212 | switch (ret) { |
| 1213 | case HLUA_E_AGAIN: |
| 1214 | break; |
| 1215 | |
| 1216 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1217 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1218 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1219 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1220 | break; |
| 1221 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1222 | case HLUA_E_ETMOUT: |
| 1223 | case HLUA_E_NOMEM: |
| 1224 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1225 | case HLUA_E_ERR: |
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 | hlua_ctx_renew(lua, 0); |
| 1229 | break; |
| 1230 | |
| 1231 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1232 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1233 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1234 | break; |
| 1235 | } |
| 1236 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1237 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1238 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1239 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1240 | return ret; |
| 1241 | } |
| 1242 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1243 | /* This function exit the current code. */ |
| 1244 | __LJMP static int hlua_done(lua_State *L) |
| 1245 | { |
| 1246 | struct hlua *hlua = hlua_gethlua(L); |
| 1247 | |
| 1248 | hlua->flags |= HLUA_EXIT; |
| 1249 | WILL_LJMP(lua_error(L)); |
| 1250 | |
| 1251 | return 0; |
| 1252 | } |
| 1253 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1254 | /* This function is an LUA binding. It provides a function |
| 1255 | * for deleting ACL from a referenced ACL file. |
| 1256 | */ |
| 1257 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1258 | { |
| 1259 | const char *name; |
| 1260 | const char *key; |
| 1261 | struct pat_ref *ref; |
| 1262 | |
| 1263 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1264 | |
| 1265 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1266 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1267 | |
| 1268 | ref = pat_ref_lookup(name); |
| 1269 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1270 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1271 | |
| 1272 | pat_ref_delete(ref, key); |
| 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | /* This function is an LUA binding. It provides a function |
| 1277 | * for deleting map entry from a referenced map file. |
| 1278 | */ |
| 1279 | static int hlua_del_map(lua_State *L) |
| 1280 | { |
| 1281 | const char *name; |
| 1282 | const char *key; |
| 1283 | struct pat_ref *ref; |
| 1284 | |
| 1285 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1286 | |
| 1287 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1288 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1289 | |
| 1290 | ref = pat_ref_lookup(name); |
| 1291 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1292 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1293 | |
| 1294 | pat_ref_delete(ref, key); |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | /* This function is an LUA binding. It provides a function |
| 1299 | * for adding ACL pattern from a referenced ACL file. |
| 1300 | */ |
| 1301 | static int hlua_add_acl(lua_State *L) |
| 1302 | { |
| 1303 | const char *name; |
| 1304 | const char *key; |
| 1305 | struct pat_ref *ref; |
| 1306 | |
| 1307 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1308 | |
| 1309 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1310 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1311 | |
| 1312 | ref = pat_ref_lookup(name); |
| 1313 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1314 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1315 | |
| 1316 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1317 | pat_ref_add(ref, key, NULL, NULL); |
| 1318 | return 0; |
| 1319 | } |
| 1320 | |
| 1321 | /* This function is an LUA binding. It provides a function |
| 1322 | * for setting map pattern and sample from a referenced map |
| 1323 | * file. |
| 1324 | */ |
| 1325 | static int hlua_set_map(lua_State *L) |
| 1326 | { |
| 1327 | const char *name; |
| 1328 | const char *key; |
| 1329 | const char *value; |
| 1330 | struct pat_ref *ref; |
| 1331 | |
| 1332 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1333 | |
| 1334 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1335 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1336 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1337 | |
| 1338 | ref = pat_ref_lookup(name); |
| 1339 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1340 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1341 | |
| 1342 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1343 | pat_ref_set(ref, key, value, NULL); |
| 1344 | else |
| 1345 | pat_ref_add(ref, key, value, NULL); |
| 1346 | return 0; |
| 1347 | } |
| 1348 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1349 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1350 | * an integer or user data. This data is associated with a metatable. This |
| 1351 | * metatable have an original version registred in the global context with |
| 1352 | * the name of the object (_G[<name>] = <metable> ). |
| 1353 | * |
| 1354 | * A metable is a table that modify the standard behavior of a standard |
| 1355 | * access to the associated data. The entries of this new metatable are |
| 1356 | * defined as is: |
| 1357 | * |
| 1358 | * http://lua-users.org/wiki/MetatableEvents |
| 1359 | * |
| 1360 | * __index |
| 1361 | * |
| 1362 | * we access an absent field in a table, the result is nil. This is |
| 1363 | * true, but it is not the whole truth. Actually, such access triggers |
| 1364 | * the interpreter to look for an __index metamethod: If there is no |
| 1365 | * such method, as usually happens, then the access results in nil; |
| 1366 | * otherwise, the metamethod will provide the result. |
| 1367 | * |
| 1368 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1369 | * the key does not appear in the table, but the metatable has an __index |
| 1370 | * property: |
| 1371 | * |
| 1372 | * - if the value is a function, the function is called, passing in the |
| 1373 | * table and the key; the return value of that function is returned as |
| 1374 | * the result. |
| 1375 | * |
| 1376 | * - if the value is another table, the value of the key in that table is |
| 1377 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1378 | * table's metatable has an __index property, then it continues on up) |
| 1379 | * |
| 1380 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1381 | * |
| 1382 | * http://www.lua.org/pil/13.4.1.html |
| 1383 | * |
| 1384 | * __newindex |
| 1385 | * |
| 1386 | * Like __index, but control property assignment. |
| 1387 | * |
| 1388 | * __mode - Control weak references. A string value with one or both |
| 1389 | * of the characters 'k' and 'v' which specifies that the the |
| 1390 | * keys and/or values in the table are weak references. |
| 1391 | * |
| 1392 | * __call - Treat a table like a function. When a table is followed by |
| 1393 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1394 | * a __call key pointing to a function, that function is invoked |
| 1395 | * (passing any specified arguments) and the return value is |
| 1396 | * returned. |
| 1397 | * |
| 1398 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1399 | * called, if the metatable for myTable has a __metatable |
| 1400 | * key, the value of that key is returned instead of the |
| 1401 | * actual metatable. |
| 1402 | * |
| 1403 | * __tostring - Control string representation. When the builtin |
| 1404 | * "tostring( myTable )" function is called, if the metatable |
| 1405 | * for myTable has a __tostring property set to a function, |
| 1406 | * that function is invoked (passing myTable to it) and the |
| 1407 | * return value is used as the string representation. |
| 1408 | * |
| 1409 | * __len - Control table length. When the table length is requested using |
| 1410 | * the length operator ( '#' ), if the metatable for myTable has |
| 1411 | * a __len key pointing to a function, that function is invoked |
| 1412 | * (passing myTable to it) and the return value used as the value |
| 1413 | * of "#myTable". |
| 1414 | * |
| 1415 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1416 | * collected, if the metatable has a __gc field pointing to a |
| 1417 | * function, that function is first invoked, passing the userdata |
| 1418 | * to it. The __gc metamethod is not called for tables. |
| 1419 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1420 | * |
| 1421 | * Special metamethods for redefining standard operators: |
| 1422 | * http://www.lua.org/pil/13.1.html |
| 1423 | * |
| 1424 | * __add "+" |
| 1425 | * __sub "-" |
| 1426 | * __mul "*" |
| 1427 | * __div "/" |
| 1428 | * __unm "!" |
| 1429 | * __pow "^" |
| 1430 | * __concat ".." |
| 1431 | * |
| 1432 | * Special methods for redfining standar relations |
| 1433 | * http://www.lua.org/pil/13.2.html |
| 1434 | * |
| 1435 | * __eq "==" |
| 1436 | * __lt "<" |
| 1437 | * __le "<=" |
| 1438 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1439 | |
| 1440 | /* |
| 1441 | * |
| 1442 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1443 | * Class Map |
| 1444 | * |
| 1445 | * |
| 1446 | */ |
| 1447 | |
| 1448 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1449 | * a class session, otherwise it throws an error. |
| 1450 | */ |
| 1451 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1452 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1453 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1454 | } |
| 1455 | |
| 1456 | /* This function is the map constructor. It don't need |
| 1457 | * the class Map object. It creates and return a new Map |
| 1458 | * object. It must be called only during "body" or "init" |
| 1459 | * context because it process some filesystem accesses. |
| 1460 | */ |
| 1461 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1462 | { |
| 1463 | const char *fn; |
| 1464 | int match = PAT_MATCH_STR; |
| 1465 | struct sample_conv conv; |
| 1466 | const char *file = ""; |
| 1467 | int line = 0; |
| 1468 | lua_Debug ar; |
| 1469 | char *err = NULL; |
| 1470 | struct arg args[2]; |
| 1471 | |
| 1472 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1473 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1474 | |
| 1475 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1476 | |
| 1477 | if (lua_gettop(L) >= 2) { |
| 1478 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1479 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1480 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1481 | } |
| 1482 | |
| 1483 | /* Get Lua filename and line number. */ |
| 1484 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1485 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1486 | if (ar.currentline > 0) { /* is there info? */ |
| 1487 | file = ar.short_src; |
| 1488 | line = ar.currentline; |
| 1489 | } |
| 1490 | } |
| 1491 | |
| 1492 | /* fill fake sample_conv struct. */ |
| 1493 | conv.kw = ""; /* unused. */ |
| 1494 | conv.process = NULL; /* unused. */ |
| 1495 | conv.arg_mask = 0; /* unused. */ |
| 1496 | conv.val_args = NULL; /* unused. */ |
| 1497 | conv.out_type = SMP_T_STR; |
| 1498 | conv.private = (void *)(long)match; |
| 1499 | switch (match) { |
| 1500 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1501 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1502 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1503 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1504 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1505 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1506 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1507 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1508 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1509 | default: |
| 1510 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1511 | } |
| 1512 | |
| 1513 | /* fill fake args. */ |
| 1514 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1515 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1516 | args[1].type = ARGT_STOP; |
| 1517 | |
| 1518 | /* load the map. */ |
| 1519 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1520 | /* error case: we cant use luaL_error because we must |
| 1521 | * free the err variable. |
| 1522 | */ |
| 1523 | luaL_where(L, 1); |
| 1524 | lua_pushfstring(L, "'new': %s.", err); |
| 1525 | lua_concat(L, 2); |
| 1526 | free(err); |
| 1527 | WILL_LJMP(lua_error(L)); |
| 1528 | } |
| 1529 | |
| 1530 | /* create the lua object. */ |
| 1531 | lua_newtable(L); |
| 1532 | lua_pushlightuserdata(L, args[0].data.map); |
| 1533 | lua_rawseti(L, -2, 0); |
| 1534 | |
| 1535 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1536 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1537 | lua_setmetatable(L, -2); |
| 1538 | |
| 1539 | |
| 1540 | return 1; |
| 1541 | } |
| 1542 | |
| 1543 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1544 | { |
| 1545 | struct map_descriptor *desc; |
| 1546 | struct pattern *pat; |
| 1547 | struct sample smp; |
| 1548 | |
| 1549 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1550 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1551 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1552 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1553 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1554 | } |
| 1555 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1556 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1557 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1558 | 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] | 1559 | } |
| 1560 | |
| 1561 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1562 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1563 | if (str) |
| 1564 | lua_pushstring(L, ""); |
| 1565 | else |
| 1566 | lua_pushnil(L); |
| 1567 | return 1; |
| 1568 | } |
| 1569 | |
| 1570 | /* 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] | 1571 | 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] | 1572 | return 1; |
| 1573 | } |
| 1574 | |
| 1575 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1576 | { |
| 1577 | return _hlua_map_lookup(L, 0); |
| 1578 | } |
| 1579 | |
| 1580 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1581 | { |
| 1582 | return _hlua_map_lookup(L, 1); |
| 1583 | } |
| 1584 | |
| 1585 | /* |
| 1586 | * |
| 1587 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1588 | * Class Socket |
| 1589 | * |
| 1590 | * |
| 1591 | */ |
| 1592 | |
| 1593 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1594 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1595 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | /* 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] | 1599 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1600 | * received. |
| 1601 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1602 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1603 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1604 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1605 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1606 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1607 | if (appctx->ctx.hlua_cosocket.die) { |
| 1608 | si_shutw(si); |
| 1609 | si_shutr(si); |
| 1610 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1611 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1612 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1613 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1614 | } |
| 1615 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1616 | /* If the connection object is not available, close all the |
| 1617 | * streams and wakeup everything waiting for. |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1618 | */ |
| 1619 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1620 | si_shutw(si); |
| 1621 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1622 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1623 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1624 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1625 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1626 | } |
| 1627 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1628 | /* If we cant write, wakeup the pending write signals. */ |
| 1629 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1630 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1631 | |
| 1632 | /* If we cant read, wakeup the pending read signals. */ |
| 1633 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1634 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1635 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1636 | /* if the connection is not estabkished, inform the stream that we want |
| 1637 | * to be notified whenever the connection completes. |
| 1638 | */ |
| 1639 | if (!(c->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1640 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1641 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1642 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1643 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1644 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1645 | |
| 1646 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1647 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1648 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1649 | /* 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] | 1650 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1651 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1652 | |
| 1653 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1654 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1655 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1656 | |
| 1657 | /* Some data were injected in the buffer, notify the stream |
| 1658 | * interface. |
| 1659 | */ |
| 1660 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1661 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1662 | |
| 1663 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1664 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1665 | */ |
| 1666 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1667 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1668 | } |
| 1669 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1670 | /* This function is called when the "struct stream" is destroyed. |
| 1671 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1672 | * Wake all the pending signals. |
| 1673 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1674 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1675 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1676 | struct xref *peer; |
| 1677 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1678 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1679 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1680 | if (peer) |
| 1681 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1682 | |
| 1683 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1684 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1685 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1686 | } |
| 1687 | |
| 1688 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1689 | * uses this object. If the stream does not exists, just quit. |
| 1690 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1691 | * pending signal can rest in the read and write lists. destroy |
| 1692 | * it. |
| 1693 | */ |
| 1694 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1695 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1696 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1697 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1698 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1699 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1700 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1701 | |
| 1702 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1703 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1704 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1705 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1706 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1707 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1708 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1709 | appctx->ctx.hlua_cosocket.die = 1; |
| 1710 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1711 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1712 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1713 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1714 | return 0; |
| 1715 | } |
| 1716 | |
| 1717 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1718 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1719 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1720 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1721 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1722 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1723 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1724 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1725 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1726 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1727 | |
| 1728 | /* Check if we run on the same thread than the xreator thread. |
| 1729 | * We cannot access to the socket if the thread is different. |
| 1730 | */ |
| 1731 | if (socket->tid != tid) |
| 1732 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1733 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1734 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1735 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1736 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1737 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1738 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1739 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1740 | appctx->ctx.hlua_cosocket.die = 1; |
| 1741 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1742 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1743 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1744 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1745 | return 0; |
| 1746 | } |
| 1747 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1748 | /* The close function calls close_helper. |
| 1749 | */ |
| 1750 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1751 | { |
| 1752 | MAY_LJMP(check_args(L, 1, "close")); |
| 1753 | return hlua_socket_close_helper(L); |
| 1754 | } |
| 1755 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1756 | /* This Lua function assumes that the stack contain three parameters. |
| 1757 | * 1 - USERDATA containing a struct socket |
| 1758 | * 2 - INTEGER with values of the macro defined below |
| 1759 | * If the integer is -1, we must read at most one line. |
| 1760 | * If the integer is -2, we ust read all the data until the |
| 1761 | * end of the stream. |
| 1762 | * If the integer is positive value, we must read a number of |
| 1763 | * bytes corresponding to this value. |
| 1764 | */ |
| 1765 | #define HLSR_READ_LINE (-1) |
| 1766 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1767 | __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] | 1768 | { |
| 1769 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1770 | int wanted = lua_tointeger(L, 2); |
| 1771 | struct hlua *hlua = hlua_gethlua(L); |
| 1772 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1773 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1774 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1775 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1776 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1777 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1778 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1779 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1780 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1781 | struct stream_interface *si; |
| 1782 | struct stream *s; |
| 1783 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1784 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1785 | |
| 1786 | /* Check if this lua stack is schedulable. */ |
| 1787 | if (!hlua || !hlua->task) |
| 1788 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1789 | "'frontend', 'backend' or 'task'")); |
| 1790 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1791 | /* Check if we run on the same thread than the xreator thread. |
| 1792 | * We cannot access to the socket if the thread is different. |
| 1793 | */ |
| 1794 | if (socket->tid != tid) |
| 1795 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1796 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1797 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1798 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1799 | if (!peer) |
| 1800 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1801 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1802 | si = appctx->owner; |
| 1803 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1804 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1805 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1806 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1807 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1808 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1809 | if (nblk < 0) /* Connection close. */ |
| 1810 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1811 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1812 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1813 | |
| 1814 | /* remove final \r\n. */ |
| 1815 | if (nblk == 1) { |
| 1816 | if (blk1[len1-1] == '\n') { |
| 1817 | len1--; |
| 1818 | skip_at_end++; |
| 1819 | if (blk1[len1-1] == '\r') { |
| 1820 | len1--; |
| 1821 | skip_at_end++; |
| 1822 | } |
| 1823 | } |
| 1824 | } |
| 1825 | else { |
| 1826 | if (blk2[len2-1] == '\n') { |
| 1827 | len2--; |
| 1828 | skip_at_end++; |
| 1829 | if (blk2[len2-1] == '\r') { |
| 1830 | len2--; |
| 1831 | skip_at_end++; |
| 1832 | } |
| 1833 | } |
| 1834 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1838 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1839 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1840 | if (nblk < 0) /* Connection close. */ |
| 1841 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1842 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1843 | goto connection_empty; |
| 1844 | } |
| 1845 | |
| 1846 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1847 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1848 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1849 | if (nblk < 0) /* Connection close. */ |
| 1850 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1851 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1852 | goto connection_empty; |
| 1853 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1854 | missing_bytes = wanted - socket->b.n; |
| 1855 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1856 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1857 | len1 = missing_bytes; |
| 1858 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1859 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1860 | } |
| 1861 | |
| 1862 | len = len1; |
| 1863 | |
| 1864 | luaL_addlstring(&socket->b, blk1, len1); |
| 1865 | if (nblk == 2) { |
| 1866 | len += len2; |
| 1867 | luaL_addlstring(&socket->b, blk2, len2); |
| 1868 | } |
| 1869 | |
| 1870 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1871 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1872 | |
| 1873 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1874 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1875 | |
| 1876 | /* If the pattern reclaim to read all the data |
| 1877 | * in the connection, got out. |
| 1878 | */ |
| 1879 | if (wanted == HLSR_READ_ALL) |
| 1880 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1881 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1882 | goto connection_empty; |
| 1883 | |
| 1884 | /* Return result. */ |
| 1885 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1886 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1887 | return 1; |
| 1888 | |
| 1889 | connection_closed: |
| 1890 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1891 | xref_unlock(&socket->xref, peer); |
| 1892 | |
| 1893 | no_peer: |
| 1894 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1895 | /* If the buffer containds data. */ |
| 1896 | if (socket->b.n > 0) { |
| 1897 | luaL_pushresult(&socket->b); |
| 1898 | return 1; |
| 1899 | } |
| 1900 | lua_pushnil(L); |
| 1901 | lua_pushstring(L, "connection closed."); |
| 1902 | return 2; |
| 1903 | |
| 1904 | connection_empty: |
| 1905 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1906 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1907 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1908 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1909 | } |
| 1910 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1911 | 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] | 1912 | return 0; |
| 1913 | } |
| 1914 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1915 | /* This Lua function gets two parameters. The first one can be string |
| 1916 | * or a number. If the string is "*l", the user requires one line. If |
| 1917 | * 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] | 1918 | * If the value is a number, the user require a number of bytes equal |
| 1919 | * to the value. The default value is "*l" (a line). |
| 1920 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1921 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1922 | * integer takes this values: |
| 1923 | * -1 : read a line |
| 1924 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1925 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1926 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1927 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1928 | * concatenated with the read data. |
| 1929 | */ |
| 1930 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1931 | { |
| 1932 | int wanted = HLSR_READ_LINE; |
| 1933 | const char *pattern; |
| 1934 | int type; |
| 1935 | char *error; |
| 1936 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1937 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1938 | |
| 1939 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1940 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1941 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1942 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1943 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1944 | /* Check if we run on the same thread than the xreator thread. |
| 1945 | * We cannot access to the socket if the thread is different. |
| 1946 | */ |
| 1947 | if (socket->tid != tid) |
| 1948 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1949 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1950 | /* check for pattern. */ |
| 1951 | if (lua_gettop(L) >= 2) { |
| 1952 | type = lua_type(L, 2); |
| 1953 | if (type == LUA_TSTRING) { |
| 1954 | pattern = lua_tostring(L, 2); |
| 1955 | if (strcmp(pattern, "*a") == 0) |
| 1956 | wanted = HLSR_READ_ALL; |
| 1957 | else if (strcmp(pattern, "*l") == 0) |
| 1958 | wanted = HLSR_READ_LINE; |
| 1959 | else { |
| 1960 | wanted = strtoll(pattern, &error, 10); |
| 1961 | if (*error != '\0') |
| 1962 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1963 | } |
| 1964 | } |
| 1965 | else if (type == LUA_TNUMBER) { |
| 1966 | wanted = lua_tointeger(L, 2); |
| 1967 | if (wanted < 0) |
| 1968 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1969 | } |
| 1970 | } |
| 1971 | |
| 1972 | /* Set pattern. */ |
| 1973 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1974 | |
| 1975 | /* Check if we would replace the top by itself. */ |
| 1976 | if (lua_gettop(L) != 2) |
| 1977 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1978 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1979 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1980 | luaL_buffinit(L, &socket->b); |
| 1981 | |
| 1982 | /* Check prefix. */ |
| 1983 | if (lua_gettop(L) >= 3) { |
| 1984 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1985 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1986 | pattern = lua_tolstring(L, 3, &len); |
| 1987 | luaL_addlstring(&socket->b, pattern, len); |
| 1988 | } |
| 1989 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1990 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1991 | } |
| 1992 | |
| 1993 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1994 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1995 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1996 | 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] | 1997 | { |
| 1998 | struct hlua_socket *socket; |
| 1999 | struct hlua *hlua = hlua_gethlua(L); |
| 2000 | struct appctx *appctx; |
| 2001 | size_t buf_len; |
| 2002 | const char *buf; |
| 2003 | int len; |
| 2004 | int send_len; |
| 2005 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2006 | struct xref *peer; |
| 2007 | struct stream_interface *si; |
| 2008 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2009 | |
| 2010 | /* Check if this lua stack is schedulable. */ |
| 2011 | if (!hlua || !hlua->task) |
| 2012 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2013 | "'frontend', 'backend' or 'task'")); |
| 2014 | |
| 2015 | /* Get object */ |
| 2016 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2017 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2018 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2019 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2020 | /* Check if we run on the same thread than the xreator thread. |
| 2021 | * We cannot access to the socket if the thread is different. |
| 2022 | */ |
| 2023 | if (socket->tid != tid) |
| 2024 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2025 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2026 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2027 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2028 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2029 | lua_pushinteger(L, -1); |
| 2030 | return 1; |
| 2031 | } |
| 2032 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2033 | si = appctx->owner; |
| 2034 | s = si_strm(si); |
| 2035 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2036 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2037 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2038 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2039 | lua_pushinteger(L, -1); |
| 2040 | return 1; |
| 2041 | } |
| 2042 | |
| 2043 | /* Update the input buffer data. */ |
| 2044 | buf += sent; |
| 2045 | send_len = buf_len - sent; |
| 2046 | |
| 2047 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2048 | if (sent >= buf_len) { |
| 2049 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2050 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2051 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2052 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2053 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2054 | * the request buffer if its not required. |
| 2055 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2056 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2057 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2058 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2059 | } |
| 2060 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2061 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2062 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2063 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2064 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2065 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2066 | |
| 2067 | /* send data */ |
| 2068 | if (len < send_len) |
| 2069 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2070 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2071 | |
| 2072 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2073 | * the data" (-2) are not expected because the available length |
| 2074 | * is tested. |
| 2075 | * Other unknown error are also not expected. |
| 2076 | */ |
| 2077 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2078 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2079 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2080 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2081 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2082 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2083 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2084 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2085 | return 1; |
| 2086 | } |
| 2087 | |
| 2088 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2089 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2090 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2091 | s->req.rex = TICK_ETERNITY; |
| 2092 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2093 | |
| 2094 | /* Update length sent. */ |
| 2095 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2096 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2097 | |
| 2098 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2099 | if (sent + len >= buf_len) { |
| 2100 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2101 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2102 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2103 | |
| 2104 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2105 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2106 | xref_unlock(&socket->xref, peer); |
| 2107 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2108 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2109 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2110 | 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] | 2111 | return 0; |
| 2112 | } |
| 2113 | |
| 2114 | /* This function initiate the send of data. It just check the input |
| 2115 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2116 | * 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] | 2117 | * "hlua_socket_write_yield" that can yield. |
| 2118 | * |
| 2119 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2120 | * the associated object. The second is a string buffer. The third is |
| 2121 | * a facultative integer that represents where is the buffer position |
| 2122 | * of the start of the data that can send. The first byte is the |
| 2123 | * position "1". The default value is "1". The fourth argument is a |
| 2124 | * facultative integer that represents where is the buffer position |
| 2125 | * of the end of the data that can send. The default is the last byte. |
| 2126 | */ |
| 2127 | static int hlua_socket_send(struct lua_State *L) |
| 2128 | { |
| 2129 | int i; |
| 2130 | int j; |
| 2131 | const char *buf; |
| 2132 | size_t buf_len; |
| 2133 | |
| 2134 | /* Check number of arguments. */ |
| 2135 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2136 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2137 | |
| 2138 | /* Get the string. */ |
| 2139 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2140 | |
| 2141 | /* Get and check j. */ |
| 2142 | if (lua_gettop(L) == 4) { |
| 2143 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2144 | if (j < 0) |
| 2145 | j = buf_len + j + 1; |
| 2146 | if (j > buf_len) |
| 2147 | j = buf_len + 1; |
| 2148 | lua_pop(L, 1); |
| 2149 | } |
| 2150 | else |
| 2151 | j = buf_len; |
| 2152 | |
| 2153 | /* Get and check i. */ |
| 2154 | if (lua_gettop(L) == 3) { |
| 2155 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2156 | if (i < 0) |
| 2157 | i = buf_len + i + 1; |
| 2158 | if (i > buf_len) |
| 2159 | i = buf_len + 1; |
| 2160 | lua_pop(L, 1); |
| 2161 | } else |
| 2162 | i = 1; |
| 2163 | |
| 2164 | /* Check bth i and j. */ |
| 2165 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2166 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2167 | return 1; |
| 2168 | } |
| 2169 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2170 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2171 | return 1; |
| 2172 | } |
| 2173 | if (i == 0) |
| 2174 | i = 1; |
| 2175 | if (j == 0) |
| 2176 | j = 1; |
| 2177 | |
| 2178 | /* Pop the string. */ |
| 2179 | lua_pop(L, 1); |
| 2180 | |
| 2181 | /* Update the buffer length. */ |
| 2182 | buf += i - 1; |
| 2183 | buf_len = j - i + 1; |
| 2184 | lua_pushlstring(L, buf, buf_len); |
| 2185 | |
| 2186 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2187 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2188 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2189 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2190 | } |
| 2191 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2192 | #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] | 2193 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2194 | { |
| 2195 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2196 | int ret; |
| 2197 | int len; |
| 2198 | char *p; |
| 2199 | |
| 2200 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2201 | if (ret <= 0) { |
| 2202 | lua_pushnil(L); |
| 2203 | return 1; |
| 2204 | } |
| 2205 | |
| 2206 | if (ret == AF_UNIX) { |
| 2207 | lua_pushstring(L, buffer+1); |
| 2208 | return 1; |
| 2209 | } |
| 2210 | else if (ret == AF_INET6) { |
| 2211 | buffer[0] = '['; |
| 2212 | len = strlen(buffer); |
| 2213 | buffer[len] = ']'; |
| 2214 | len++; |
| 2215 | buffer[len] = ':'; |
| 2216 | len++; |
| 2217 | p = buffer; |
| 2218 | } |
| 2219 | else if (ret == AF_INET) { |
| 2220 | p = buffer + 1; |
| 2221 | len = strlen(p); |
| 2222 | p[len] = ':'; |
| 2223 | len++; |
| 2224 | } |
| 2225 | else { |
| 2226 | lua_pushnil(L); |
| 2227 | return 1; |
| 2228 | } |
| 2229 | |
| 2230 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2231 | lua_pushnil(L); |
| 2232 | return 1; |
| 2233 | } |
| 2234 | |
| 2235 | lua_pushstring(L, p); |
| 2236 | return 1; |
| 2237 | } |
| 2238 | |
| 2239 | /* Returns information about the peer of the connection. */ |
| 2240 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2241 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2242 | struct hlua_socket *socket; |
| 2243 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2244 | struct xref *peer; |
| 2245 | struct appctx *appctx; |
| 2246 | struct stream_interface *si; |
| 2247 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2248 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2249 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2250 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2251 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2252 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2253 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2254 | /* Check if we run on the same thread than the xreator thread. |
| 2255 | * We cannot access to the socket if the thread is different. |
| 2256 | */ |
| 2257 | if (socket->tid != tid) |
| 2258 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2259 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2260 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2261 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2262 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2263 | lua_pushnil(L); |
| 2264 | return 1; |
| 2265 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2266 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2267 | si = appctx->owner; |
| 2268 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2269 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2270 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2271 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2272 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2273 | lua_pushnil(L); |
| 2274 | return 1; |
| 2275 | } |
| 2276 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2277 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2278 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2279 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2280 | lua_pushnil(L); |
| 2281 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2282 | } |
| 2283 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2284 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2285 | xref_unlock(&socket->xref, peer); |
| 2286 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | /* Returns information about my connection side. */ |
| 2290 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2291 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2292 | struct hlua_socket *socket; |
| 2293 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2294 | struct appctx *appctx; |
| 2295 | struct xref *peer; |
| 2296 | struct stream_interface *si; |
| 2297 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2298 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2299 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2301 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2302 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2303 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2304 | /* Check if we run on the same thread than the xreator thread. |
| 2305 | * We cannot access to the socket if the thread is different. |
| 2306 | */ |
| 2307 | if (socket->tid != tid) |
| 2308 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2309 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2310 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2311 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2312 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2313 | lua_pushnil(L); |
| 2314 | return 1; |
| 2315 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2316 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2317 | si = appctx->owner; |
| 2318 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2319 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2320 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2322 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2323 | lua_pushnil(L); |
| 2324 | return 1; |
| 2325 | } |
| 2326 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2327 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2328 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2329 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2330 | lua_pushnil(L); |
| 2331 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2332 | } |
| 2333 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2334 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2335 | xref_unlock(&socket->xref, peer); |
| 2336 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2340 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2341 | .obj_type = OBJ_TYPE_APPLET, |
| 2342 | .name = "<LUA_TCP>", |
| 2343 | .fct = hlua_socket_handler, |
| 2344 | .release = hlua_socket_release, |
| 2345 | }; |
| 2346 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2347 | __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] | 2348 | { |
| 2349 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2350 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2351 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2352 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2353 | struct stream_interface *si; |
| 2354 | struct stream *s; |
| 2355 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2356 | /* Check if we run on the same thread than the xreator thread. |
| 2357 | * We cannot access to the socket if the thread is different. |
| 2358 | */ |
| 2359 | if (socket->tid != tid) |
| 2360 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2361 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2362 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2363 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2364 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2365 | lua_pushnil(L); |
| 2366 | lua_pushstring(L, "Can't connect"); |
| 2367 | return 2; |
| 2368 | } |
| 2369 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2370 | si = appctx->owner; |
| 2371 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2372 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2373 | /* Check if we run on the same thread than the xreator thread. |
| 2374 | * We cannot access to the socket if the thread is different. |
| 2375 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2376 | if (socket->tid != tid) { |
| 2377 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2378 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2379 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2380 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2381 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2382 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2383 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2384 | lua_pushnil(L); |
| 2385 | lua_pushstring(L, "Can't connect"); |
| 2386 | return 2; |
| 2387 | } |
| 2388 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2389 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2390 | |
| 2391 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2392 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2393 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2394 | lua_pushinteger(L, 1); |
| 2395 | return 1; |
| 2396 | } |
| 2397 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2398 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2399 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2400 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2401 | } |
| 2402 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2403 | 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] | 2404 | return 0; |
| 2405 | } |
| 2406 | |
| 2407 | /* This function fail or initite the connection. */ |
| 2408 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2409 | { |
| 2410 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2411 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2412 | const char *ip; |
| 2413 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2414 | struct hlua *hlua; |
| 2415 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2416 | int low, high; |
| 2417 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2418 | struct xref *peer; |
| 2419 | struct stream_interface *si; |
| 2420 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2421 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2422 | if (lua_gettop(L) < 2) |
| 2423 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2424 | |
| 2425 | /* Get args. */ |
| 2426 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2427 | |
| 2428 | /* Check if we run on the same thread than the xreator thread. |
| 2429 | * We cannot access to the socket if the thread is different. |
| 2430 | */ |
| 2431 | if (socket->tid != tid) |
| 2432 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2433 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2434 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2435 | if (lua_gettop(L) >= 3) { |
| 2436 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2437 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2438 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2439 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2440 | * that are not enclosed within square brackets. |
| 2441 | */ |
| 2442 | if (port > 0) { |
| 2443 | luaL_buffinit(L, &b); |
| 2444 | luaL_addstring(&b, ip); |
| 2445 | luaL_addchar(&b, ':'); |
| 2446 | luaL_pushresult(&b); |
| 2447 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2448 | } |
| 2449 | } |
| 2450 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2451 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2452 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2453 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2454 | lua_pushnil(L); |
| 2455 | return 1; |
| 2456 | } |
| 2457 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2458 | si = appctx->owner; |
| 2459 | s = si_strm(si); |
| 2460 | |
| 2461 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2462 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2463 | if (!conn) { |
| 2464 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2465 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2466 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2467 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2468 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2469 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2470 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2471 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2472 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2473 | if (!addr) { |
| 2474 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2475 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2476 | } |
| 2477 | if (low != high) { |
| 2478 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2479 | 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] | 2480 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2481 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2482 | |
| 2483 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2484 | if (low == 0) { |
| 2485 | if (conn->addr.to.ss_family == AF_INET) { |
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_in *)&conn->addr.to)->sin_port = htons(port); |
| 2491 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2492 | if (port == -1) { |
| 2493 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2494 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2495 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2496 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2497 | } |
| 2498 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2499 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2500 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2501 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2502 | |
| 2503 | /* inform the stream that we want to be notified whenever the |
| 2504 | * connection completes. |
| 2505 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2506 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2507 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2508 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2509 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2510 | hlua->flags |= HLUA_MUST_GC; |
| 2511 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2512 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2513 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2514 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2515 | } |
| 2516 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2517 | |
| 2518 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2519 | /* Return yield waiting for connection. */ |
| 2520 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2521 | 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] | 2522 | |
| 2523 | return 0; |
| 2524 | } |
| 2525 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2526 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2527 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2528 | { |
| 2529 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2530 | struct xref *peer; |
| 2531 | struct appctx *appctx; |
| 2532 | struct stream_interface *si; |
| 2533 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2534 | |
| 2535 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2536 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2537 | |
| 2538 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2539 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2540 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2541 | lua_pushnil(L); |
| 2542 | return 1; |
| 2543 | } |
| 2544 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2545 | si = appctx->owner; |
| 2546 | s = si_strm(si); |
| 2547 | |
| 2548 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2549 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2550 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2551 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2552 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2553 | |
| 2554 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2555 | { |
| 2556 | return 0; |
| 2557 | } |
| 2558 | |
| 2559 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2560 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2561 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2562 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2563 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2564 | struct xref *peer; |
| 2565 | struct appctx *appctx; |
| 2566 | struct stream_interface *si; |
| 2567 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2568 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2569 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2570 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2571 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2572 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2573 | /* convert the timeout to millis */ |
| 2574 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2575 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2576 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2577 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2578 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2579 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2580 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2581 | 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] | 2582 | |
| 2583 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2584 | if (tmout == 0) |
| 2585 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2586 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2587 | /* Check if we run on the same thread than the xreator thread. |
| 2588 | * We cannot access to the socket if the thread is different. |
| 2589 | */ |
| 2590 | if (socket->tid != tid) |
| 2591 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2592 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2593 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2594 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2595 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2596 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2597 | WILL_LJMP(lua_error(L)); |
| 2598 | return 0; |
| 2599 | } |
| 2600 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2601 | si = appctx->owner; |
| 2602 | s = si_strm(si); |
| 2603 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2604 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2605 | s->req.rto = tmout; |
| 2606 | s->req.wto = tmout; |
| 2607 | s->res.rto = tmout; |
| 2608 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2609 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2610 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2611 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2612 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2613 | |
| 2614 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2615 | task_queue(s->task); |
| 2616 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2617 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2618 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2619 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2620 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2624 | { |
| 2625 | struct hlua_socket *socket; |
| 2626 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2627 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2628 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2629 | |
| 2630 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2631 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2632 | hlua_pusherror(L, "socket: full stack"); |
| 2633 | goto out_fail_conf; |
| 2634 | } |
| 2635 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2636 | /* Create the object: obj[0] = userdata. */ |
| 2637 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2638 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2639 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2640 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2641 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2642 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2643 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2644 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2645 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2646 | goto out_fail_conf; |
| 2647 | } |
| 2648 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2649 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2650 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2651 | lua_setmetatable(L, -2); |
| 2652 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2653 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2654 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2655 | if (!appctx) { |
| 2656 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2657 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2658 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2659 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2660 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2661 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2662 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2663 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2664 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2665 | /* Now create a session, task and stream for this applet */ |
| 2666 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2667 | if (!sess) { |
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_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2670 | } |
| 2671 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2672 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2673 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2674 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2675 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2676 | } |
| 2677 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2678 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2679 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2680 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2681 | /* Configure "right" stream interface. this "si" is used to connect |
| 2682 | * and retrieve data from the server. The connection is initialized |
| 2683 | * with the "struct server". |
| 2684 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2685 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2686 | |
| 2687 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2688 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2689 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2690 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2691 | return 1; |
| 2692 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2693 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2694 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2695 | out_fail_sess: |
| 2696 | appctx_free(appctx); |
| 2697 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2698 | WILL_LJMP(lua_error(L)); |
| 2699 | return 0; |
| 2700 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2701 | |
| 2702 | /* |
| 2703 | * |
| 2704 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2705 | * Class Channel |
| 2706 | * |
| 2707 | * |
| 2708 | */ |
| 2709 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2710 | /* This function is called before the Lua execution. It stores |
| 2711 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2712 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2713 | 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] | 2714 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2715 | c->mode = stream->be->mode; |
| 2716 | switch (c->mode) { |
| 2717 | case PR_MODE_HTTP: |
| 2718 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2719 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2720 | c->data.http.state = stream->txn->req.msg_state; |
| 2721 | else |
| 2722 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2723 | break; |
| 2724 | default: |
| 2725 | break; |
| 2726 | } |
| 2727 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2728 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2729 | /* This function is called after the Lua execution. it |
| 2730 | * returns true if the parser state is consistent, otherwise, |
| 2731 | * it return false. |
| 2732 | * |
| 2733 | * In HTTP mode, the parser state must be in the same state |
| 2734 | * or greater when we exit the function. Even if we do a |
| 2735 | * control yield. This prevent to break the HTTP message |
| 2736 | * from the Lua code. |
| 2737 | */ |
| 2738 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2739 | { |
| 2740 | if (c->mode != stream->be->mode) |
| 2741 | return 0; |
| 2742 | |
| 2743 | switch (c->mode) { |
| 2744 | case PR_MODE_HTTP: |
| 2745 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2746 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2747 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2748 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2749 | else |
| 2750 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2751 | default: |
| 2752 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2753 | } |
| 2754 | return 1; |
| 2755 | } |
| 2756 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2757 | /* Returns the struct hlua_channel join to the class channel in the |
| 2758 | * stack entry "ud" or throws an argument error. |
| 2759 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2760 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2761 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2762 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2763 | } |
| 2764 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2765 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2766 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2767 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2768 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2769 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2770 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2771 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2772 | return 0; |
| 2773 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2774 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2775 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2776 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2777 | |
| 2778 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2779 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2780 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2781 | return 1; |
| 2782 | } |
| 2783 | |
| 2784 | /* Duplicate all the data present in the input channel and put it |
| 2785 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2786 | * the stack if the channel is closed and all the data are consumed, |
| 2787 | * returns 0 if no data are available, otherwise it returns the length |
| 2788 | * of the builded string. |
| 2789 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2790 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2791 | { |
| 2792 | char *blk1; |
| 2793 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2794 | size_t len1; |
| 2795 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2796 | int ret; |
| 2797 | luaL_Buffer b; |
| 2798 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2799 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2800 | if (unlikely(ret == 0)) |
| 2801 | return 0; |
| 2802 | |
| 2803 | if (unlikely(ret < 0)) { |
| 2804 | lua_pushnil(L); |
| 2805 | return -1; |
| 2806 | } |
| 2807 | |
| 2808 | luaL_buffinit(L, &b); |
| 2809 | luaL_addlstring(&b, blk1, len1); |
| 2810 | if (unlikely(ret == 2)) |
| 2811 | luaL_addlstring(&b, blk2, len2); |
| 2812 | luaL_pushresult(&b); |
| 2813 | |
| 2814 | if (unlikely(ret == 2)) |
| 2815 | return len1 + len2; |
| 2816 | return len1; |
| 2817 | } |
| 2818 | |
| 2819 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2820 | * a yield. This function keep the data in the buffer. |
| 2821 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2822 | __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] | 2823 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2824 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2825 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2826 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2827 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2828 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2829 | WILL_LJMP(lua_error(L)); |
| 2830 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2831 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2832 | 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] | 2833 | return 1; |
| 2834 | } |
| 2835 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2836 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2837 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2838 | { |
| 2839 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2840 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2841 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2842 | } |
| 2843 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2844 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2845 | * a yield. This function consumes the data in the buffer. It returns |
| 2846 | * a string containing the data or a nil pointer if no data are available |
| 2847 | * and the channel is closed. |
| 2848 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2849 | __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] | 2850 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2851 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2852 | int ret; |
| 2853 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2854 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2855 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2856 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2857 | WILL_LJMP(lua_error(L)); |
| 2858 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2859 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2860 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2861 | 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] | 2862 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2863 | if (unlikely(ret == -1)) |
| 2864 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2865 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2866 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2867 | return 1; |
| 2868 | } |
| 2869 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2870 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2871 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2872 | { |
| 2873 | MAY_LJMP(check_args(L, 1, "get")); |
| 2874 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2875 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2876 | } |
| 2877 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2878 | /* This functions consumes and returns one line. If the channel is closed, |
| 2879 | * 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] | 2880 | * 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] | 2881 | * value. |
| 2882 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2883 | __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] | 2884 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2885 | char *blk1; |
| 2886 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2887 | size_t len1; |
| 2888 | size_t len2; |
| 2889 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2890 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2891 | int ret; |
| 2892 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2893 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2894 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2895 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2896 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2897 | WILL_LJMP(lua_error(L)); |
| 2898 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2899 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2900 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2901 | 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] | 2902 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2903 | if (ret == -1) { |
| 2904 | lua_pushnil(L); |
| 2905 | return 1; |
| 2906 | } |
| 2907 | |
| 2908 | luaL_buffinit(L, &b); |
| 2909 | luaL_addlstring(&b, blk1, len1); |
| 2910 | len = len1; |
| 2911 | if (unlikely(ret == 2)) { |
| 2912 | luaL_addlstring(&b, blk2, len2); |
| 2913 | len += len2; |
| 2914 | } |
| 2915 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2916 | 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] | 2917 | return 1; |
| 2918 | } |
| 2919 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2920 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2921 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2922 | { |
| 2923 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2924 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2925 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2926 | } |
| 2927 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2928 | /* This function takes a string as input, and append it at the |
| 2929 | * input side of channel. If the data is too big, but a space |
| 2930 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2931 | * yields. If the data is bigger than the buffer, or if the |
| 2932 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2933 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2934 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2935 | __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] | 2936 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2937 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2938 | size_t len; |
| 2939 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2940 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2941 | int ret; |
| 2942 | int max; |
| 2943 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2944 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2945 | WILL_LJMP(lua_error(L)); |
| 2946 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2947 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2948 | * the request buffer if its not required. |
| 2949 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2950 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2951 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2952 | 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] | 2953 | } |
| 2954 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2955 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2956 | if (max > len - l) |
| 2957 | max = len - l; |
| 2958 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2959 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2960 | if (ret == -2 || ret == -3) { |
| 2961 | lua_pushinteger(L, -1); |
| 2962 | return 1; |
| 2963 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2964 | if (ret == -1) { |
| 2965 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2966 | 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] | 2967 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2968 | l += ret; |
| 2969 | lua_pop(L, 1); |
| 2970 | lua_pushinteger(L, l); |
| 2971 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2972 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2973 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2974 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2975 | * in this case, we cannot add more data, so we cannot yield, |
| 2976 | * we return the amount of copyied data. |
| 2977 | */ |
| 2978 | return 1; |
| 2979 | } |
| 2980 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2981 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2982 | return 1; |
| 2983 | } |
| 2984 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2985 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2986 | * 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] | 2987 | * buffer size is too little for the data. |
| 2988 | */ |
| 2989 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2990 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2991 | size_t len; |
| 2992 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2993 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2994 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2995 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2996 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2997 | lua_pushinteger(L, 0); |
| 2998 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2999 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3000 | } |
| 3001 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3002 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3003 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3004 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3005 | * or -1 if the channel is closed or if the buffer size is too |
| 3006 | * little for the data. |
| 3007 | */ |
| 3008 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3009 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3010 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3011 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3012 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3013 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3014 | lua_pushinteger(L, 0); |
| 3015 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3016 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3017 | WILL_LJMP(lua_error(L)); |
| 3018 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3019 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3020 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3021 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3022 | } |
| 3023 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3024 | /* Append data in the output side of the buffer. This data is immediately |
| 3025 | * sent. The function returns the amount of data written. If the buffer |
| 3026 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3027 | * if the channel is closed. |
| 3028 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3029 | __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] | 3030 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3031 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3032 | size_t len; |
| 3033 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3034 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3035 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3036 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3037 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3038 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3039 | WILL_LJMP(lua_error(L)); |
| 3040 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3041 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3042 | lua_pushinteger(L, -1); |
| 3043 | return 1; |
| 3044 | } |
| 3045 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3046 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3047 | * the request buffer if its not required. |
| 3048 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3049 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3050 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3051 | 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] | 3052 | } |
| 3053 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3054 | /* The written data will be immediately sent, so we can check |
| 3055 | * the available space without taking in account the reserve. |
| 3056 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3057 | * data, because the buffer will be flushed. |
| 3058 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3059 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3060 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3061 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3062 | * in this case, we cannot add more data, so we cannot yield, |
| 3063 | * we return the amount of copyied data. |
| 3064 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3065 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3066 | return 1; |
| 3067 | |
| 3068 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3069 | if (max > len - l) |
| 3070 | max = len - l; |
| 3071 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3072 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3073 | * detects a non contiguous buffer and realign it. |
| 3074 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3075 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3076 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3077 | |
| 3078 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3079 | 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] | 3080 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3081 | /* buffer replace considers that the input part is filled. |
| 3082 | * so, I must forward these new data in the output part. |
| 3083 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3084 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3085 | |
| 3086 | l += max; |
| 3087 | lua_pop(L, 1); |
| 3088 | lua_pushinteger(L, l); |
| 3089 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3090 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3091 | * in this case, we cannot add more data, so we cannot yield, |
| 3092 | * we return the amount of copyied data. |
| 3093 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3094 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3095 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3096 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3097 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3098 | if (l < len) { |
| 3099 | /* If we are waiting for space in the response buffer, we |
| 3100 | * must set the flag WAKERESWR. This flag required the task |
| 3101 | * wake up if any activity is detected on the response buffer. |
| 3102 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3103 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3104 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3105 | else |
| 3106 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3107 | 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] | 3108 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3109 | |
| 3110 | return 1; |
| 3111 | } |
| 3112 | |
| 3113 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3114 | * yield the LUA process, and resume it without checking the |
| 3115 | * input arguments. |
| 3116 | */ |
| 3117 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3118 | { |
| 3119 | MAY_LJMP(check_args(L, 2, "send")); |
| 3120 | lua_pushinteger(L, 0); |
| 3121 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3122 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3123 | } |
| 3124 | |
| 3125 | /* This function forward and amount of butes. The data pass from |
| 3126 | * the input side of the buffer to the output side, and can be |
| 3127 | * forwarded. This function never fails. |
| 3128 | * |
| 3129 | * The Lua function takes an amount of bytes to be forwarded in |
| 3130 | * imput. It returns the number of bytes forwarded. |
| 3131 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3132 | __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] | 3133 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3134 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3135 | int len; |
| 3136 | int l; |
| 3137 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3138 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3139 | |
| 3140 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3141 | |
| 3142 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3143 | WILL_LJMP(lua_error(L)); |
| 3144 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3145 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3146 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3147 | |
| 3148 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3149 | if (max > ci_data(chn)) |
| 3150 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3151 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3152 | l += max; |
| 3153 | |
| 3154 | lua_pop(L, 1); |
| 3155 | lua_pushinteger(L, l); |
| 3156 | |
| 3157 | /* Check if it miss bytes to forward. */ |
| 3158 | if (l < len) { |
| 3159 | /* The the input channel or the output channel are closed, we |
| 3160 | * must return the amount of data forwarded. |
| 3161 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3162 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3163 | return 1; |
| 3164 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3165 | /* If we are waiting for space data in the response buffer, we |
| 3166 | * must set the flag WAKERESWR. This flag required the task |
| 3167 | * wake up if any activity is detected on the response buffer. |
| 3168 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3169 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3170 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3171 | else |
| 3172 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3173 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3174 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3175 | 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] | 3176 | } |
| 3177 | |
| 3178 | return 1; |
| 3179 | } |
| 3180 | |
| 3181 | /* Just check the input and prepare the stack for the previous |
| 3182 | * function "hlua_channel_forward_yield" |
| 3183 | */ |
| 3184 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3185 | { |
| 3186 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3187 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3188 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3189 | |
| 3190 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3191 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3192 | } |
| 3193 | |
| 3194 | /* Just returns the number of bytes available in the input |
| 3195 | * side of the buffer. This function never fails. |
| 3196 | */ |
| 3197 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3198 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3199 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3200 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3201 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3202 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3203 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3204 | struct htx *htx = htxbuf(&chn->buf); |
| 3205 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3206 | } |
| 3207 | else |
| 3208 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3209 | return 1; |
| 3210 | } |
| 3211 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3212 | /* Returns true if the channel is full. */ |
| 3213 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3214 | { |
| 3215 | struct channel *chn; |
| 3216 | int rem; |
| 3217 | |
| 3218 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3219 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3220 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3221 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3222 | struct htx *htx = htxbuf(&chn->buf); |
| 3223 | |
| 3224 | rem = htx_free_data_space(htx); |
| 3225 | } |
| 3226 | else |
| 3227 | rem = b_room(&chn->buf); |
| 3228 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3229 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3230 | |
| 3231 | lua_pushboolean(L, rem <= 0); |
| 3232 | return 1; |
| 3233 | } |
| 3234 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3235 | /* Just returns the number of bytes available in the output |
| 3236 | * side of the buffer. This function never fails. |
| 3237 | */ |
| 3238 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3239 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3240 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3241 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3242 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3243 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3244 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3245 | return 1; |
| 3246 | } |
| 3247 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3248 | /* |
| 3249 | * |
| 3250 | * |
| 3251 | * Class Fetches |
| 3252 | * |
| 3253 | * |
| 3254 | */ |
| 3255 | |
| 3256 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3257 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3258 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3259 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3260 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3261 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3262 | } |
| 3263 | |
| 3264 | /* This function creates and push in the stack a fetch object according |
| 3265 | * with a current TXN. |
| 3266 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3267 | 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] | 3268 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3269 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3270 | |
| 3271 | /* Check stack size. */ |
| 3272 | if (!lua_checkstack(L, 3)) |
| 3273 | return 0; |
| 3274 | |
| 3275 | /* Create the object: obj[0] = userdata. |
| 3276 | * Note that the base of the Fetches object is the |
| 3277 | * transaction object. |
| 3278 | */ |
| 3279 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3280 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3281 | lua_rawseti(L, -2, 0); |
| 3282 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3283 | hsmp->s = txn->s; |
| 3284 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3285 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3286 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3287 | |
| 3288 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3289 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3290 | lua_setmetatable(L, -2); |
| 3291 | |
| 3292 | return 1; |
| 3293 | } |
| 3294 | |
| 3295 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3296 | * It uses closure argument to store the associated sample-fetch. It |
| 3297 | * returns only one argument or throws an error. An error is thrown |
| 3298 | * only if an error is encountered during the argument parsing. If |
| 3299 | * the "sample-fetch" function fails, nil is returned. |
| 3300 | */ |
| 3301 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3302 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3303 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3304 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3305 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3306 | int i; |
| 3307 | struct sample smp; |
| 3308 | |
| 3309 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3310 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3311 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3312 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3313 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3314 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3315 | /* Check execution authorization. */ |
| 3316 | if (f->use & SMP_USE_HTTP_ANY && |
| 3317 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3318 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3319 | "is not available in Lua services", f->kw); |
| 3320 | WILL_LJMP(lua_error(L)); |
| 3321 | } |
| 3322 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3323 | /* Get extra arguments. */ |
| 3324 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3325 | if (i >= ARGM_NBARGS) |
| 3326 | break; |
| 3327 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3328 | } |
| 3329 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3330 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3331 | |
| 3332 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3333 | 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] | 3334 | |
| 3335 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3336 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3337 | lua_pushfstring(L, "error in arguments"); |
| 3338 | WILL_LJMP(lua_error(L)); |
| 3339 | } |
| 3340 | |
| 3341 | /* Initialise the sample. */ |
| 3342 | memset(&smp, 0, sizeof(smp)); |
| 3343 | |
| 3344 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3345 | 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] | 3346 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3347 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3348 | lua_pushstring(L, ""); |
| 3349 | else |
| 3350 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3351 | return 1; |
| 3352 | } |
| 3353 | |
| 3354 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3355 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3356 | hlua_smp2lua_str(L, &smp); |
| 3357 | else |
| 3358 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3359 | return 1; |
| 3360 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3361 | |
| 3362 | /* |
| 3363 | * |
| 3364 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3365 | * Class Converters |
| 3366 | * |
| 3367 | * |
| 3368 | */ |
| 3369 | |
| 3370 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3371 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3372 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3373 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3374 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3375 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3376 | } |
| 3377 | |
| 3378 | /* This function creates and push in the stack a Converters object |
| 3379 | * according with a current TXN. |
| 3380 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3381 | 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] | 3382 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3383 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3384 | |
| 3385 | /* Check stack size. */ |
| 3386 | if (!lua_checkstack(L, 3)) |
| 3387 | return 0; |
| 3388 | |
| 3389 | /* Create the object: obj[0] = userdata. |
| 3390 | * Note that the base of the Converters object is the |
| 3391 | * same than the TXN object. |
| 3392 | */ |
| 3393 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3394 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3395 | lua_rawseti(L, -2, 0); |
| 3396 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3397 | hsmp->s = txn->s; |
| 3398 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3399 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3400 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3401 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3402 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3403 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3404 | lua_setmetatable(L, -2); |
| 3405 | |
| 3406 | return 1; |
| 3407 | } |
| 3408 | |
| 3409 | /* This function is an LUA binding. It is called with each converter. |
| 3410 | * It uses closure argument to store the associated converter. It |
| 3411 | * returns only one argument or throws an error. An error is thrown |
| 3412 | * only if an error is encountered during the argument parsing. If |
| 3413 | * the converter function function fails, nil is returned. |
| 3414 | */ |
| 3415 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3416 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3417 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3418 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3419 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3420 | int i; |
| 3421 | struct sample smp; |
| 3422 | |
| 3423 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3424 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3425 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3426 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3427 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3428 | |
| 3429 | /* Get extra arguments. */ |
| 3430 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3431 | if (i >= ARGM_NBARGS) |
| 3432 | break; |
| 3433 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3434 | } |
| 3435 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3436 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3437 | |
| 3438 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3439 | 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] | 3440 | |
| 3441 | /* Run the special args checker. */ |
| 3442 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3443 | hlua_pusherror(L, "error in arguments"); |
| 3444 | WILL_LJMP(lua_error(L)); |
| 3445 | } |
| 3446 | |
| 3447 | /* Initialise the sample. */ |
| 3448 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3449 | hlua_pusherror(L, "error in the input argument"); |
| 3450 | WILL_LJMP(lua_error(L)); |
| 3451 | } |
| 3452 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3453 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3454 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3455 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3456 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3457 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3458 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3459 | WILL_LJMP(lua_error(L)); |
| 3460 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3461 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3462 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3463 | hlua_pusherror(L, "error during the input argument casting"); |
| 3464 | WILL_LJMP(lua_error(L)); |
| 3465 | } |
| 3466 | |
| 3467 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3468 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3469 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3470 | lua_pushstring(L, ""); |
| 3471 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3472 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3473 | return 1; |
| 3474 | } |
| 3475 | |
| 3476 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3477 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3478 | hlua_smp2lua_str(L, &smp); |
| 3479 | else |
| 3480 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3481 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3482 | } |
| 3483 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3484 | /* |
| 3485 | * |
| 3486 | * |
| 3487 | * Class AppletTCP |
| 3488 | * |
| 3489 | * |
| 3490 | */ |
| 3491 | |
| 3492 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3493 | * a class stream, otherwise it throws an error. |
| 3494 | */ |
| 3495 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3496 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3497 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3498 | } |
| 3499 | |
| 3500 | /* This function creates and push in the stack an Applet object |
| 3501 | * according with a current TXN. |
| 3502 | */ |
| 3503 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3504 | { |
| 3505 | struct hlua_appctx *appctx; |
| 3506 | struct stream_interface *si = ctx->owner; |
| 3507 | struct stream *s = si_strm(si); |
| 3508 | struct proxy *p = s->be; |
| 3509 | |
| 3510 | /* Check stack size. */ |
| 3511 | if (!lua_checkstack(L, 3)) |
| 3512 | return 0; |
| 3513 | |
| 3514 | /* Create the object: obj[0] = userdata. |
| 3515 | * Note that the base of the Converters object is the |
| 3516 | * same than the TXN object. |
| 3517 | */ |
| 3518 | lua_newtable(L); |
| 3519 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3520 | lua_rawseti(L, -2, 0); |
| 3521 | appctx->appctx = ctx; |
| 3522 | appctx->htxn.s = s; |
| 3523 | appctx->htxn.p = p; |
| 3524 | |
| 3525 | /* Create the "f" field that contains a list of fetches. */ |
| 3526 | lua_pushstring(L, "f"); |
| 3527 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3528 | return 0; |
| 3529 | lua_settable(L, -3); |
| 3530 | |
| 3531 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3532 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3533 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3534 | return 0; |
| 3535 | lua_settable(L, -3); |
| 3536 | |
| 3537 | /* Create the "c" field that contains a list of converters. */ |
| 3538 | lua_pushstring(L, "c"); |
| 3539 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3540 | return 0; |
| 3541 | lua_settable(L, -3); |
| 3542 | |
| 3543 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3544 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3545 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3546 | return 0; |
| 3547 | lua_settable(L, -3); |
| 3548 | |
| 3549 | /* Pop a class stream metatable and affect it to the table. */ |
| 3550 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3551 | lua_setmetatable(L, -2); |
| 3552 | |
| 3553 | return 1; |
| 3554 | } |
| 3555 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3556 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3557 | { |
| 3558 | struct hlua_appctx *appctx; |
| 3559 | struct stream *s; |
| 3560 | const char *name; |
| 3561 | size_t len; |
| 3562 | struct sample smp; |
| 3563 | |
| 3564 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3565 | |
| 3566 | /* It is useles to retrieve the stream, but this function |
| 3567 | * runs only in a stream context. |
| 3568 | */ |
| 3569 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3570 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3571 | s = appctx->htxn.s; |
| 3572 | |
| 3573 | /* Converts the third argument in a sample. */ |
| 3574 | hlua_lua2smp(L, 3, &smp); |
| 3575 | |
| 3576 | /* Store the sample in a variable. */ |
| 3577 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3578 | vars_set_by_name(name, len, &smp); |
| 3579 | return 0; |
| 3580 | } |
| 3581 | |
| 3582 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3583 | { |
| 3584 | struct hlua_appctx *appctx; |
| 3585 | struct stream *s; |
| 3586 | const char *name; |
| 3587 | size_t len; |
| 3588 | struct sample smp; |
| 3589 | |
| 3590 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3591 | |
| 3592 | /* It is useles to retrieve the stream, but this function |
| 3593 | * runs only in a stream context. |
| 3594 | */ |
| 3595 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3596 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3597 | s = appctx->htxn.s; |
| 3598 | |
| 3599 | /* Unset the variable. */ |
| 3600 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3601 | vars_unset_by_name(name, len, &smp); |
| 3602 | return 0; |
| 3603 | } |
| 3604 | |
| 3605 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3606 | { |
| 3607 | struct hlua_appctx *appctx; |
| 3608 | struct stream *s; |
| 3609 | const char *name; |
| 3610 | size_t len; |
| 3611 | struct sample smp; |
| 3612 | |
| 3613 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3614 | |
| 3615 | /* It is useles to retrieve the stream, but this function |
| 3616 | * runs only in a stream context. |
| 3617 | */ |
| 3618 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3619 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3620 | s = appctx->htxn.s; |
| 3621 | |
| 3622 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3623 | if (!vars_get_by_name(name, len, &smp)) { |
| 3624 | lua_pushnil(L); |
| 3625 | return 1; |
| 3626 | } |
| 3627 | |
| 3628 | return hlua_smp2lua(L, &smp); |
| 3629 | } |
| 3630 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3631 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3632 | { |
| 3633 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3634 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3635 | struct hlua *hlua; |
| 3636 | |
| 3637 | /* 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] | 3638 | if (!s->hlua) |
| 3639 | return 0; |
| 3640 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3641 | |
| 3642 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3643 | |
| 3644 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3645 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3646 | |
| 3647 | /* Get and store new value. */ |
| 3648 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3649 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3650 | |
| 3651 | return 0; |
| 3652 | } |
| 3653 | |
| 3654 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3655 | { |
| 3656 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3657 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3658 | struct hlua *hlua; |
| 3659 | |
| 3660 | /* 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] | 3661 | if (!s->hlua) { |
| 3662 | lua_pushnil(L); |
| 3663 | return 1; |
| 3664 | } |
| 3665 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3666 | |
| 3667 | /* Push configuration index in the stack. */ |
| 3668 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3669 | |
| 3670 | return 1; |
| 3671 | } |
| 3672 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3673 | /* If expected data not yet available, it returns a yield. This function |
| 3674 | * consumes the data in the buffer. It returns a string containing the |
| 3675 | * data. This string can be empty. |
| 3676 | */ |
| 3677 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3678 | { |
| 3679 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3680 | struct stream_interface *si = appctx->appctx->owner; |
| 3681 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3682 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3683 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3684 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3685 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3686 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3687 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3688 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3689 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3690 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3691 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3692 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3693 | 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] | 3694 | } |
| 3695 | |
| 3696 | /* End of data: commit the total strings and return. */ |
| 3697 | if (ret < 0) { |
| 3698 | luaL_pushresult(&appctx->b); |
| 3699 | return 1; |
| 3700 | } |
| 3701 | |
| 3702 | /* Ensure that the block 2 length is usable. */ |
| 3703 | if (ret == 1) |
| 3704 | len2 = 0; |
| 3705 | |
| 3706 | /* dont check the max length read and dont check. */ |
| 3707 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3708 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3709 | |
| 3710 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3711 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3712 | luaL_pushresult(&appctx->b); |
| 3713 | return 1; |
| 3714 | } |
| 3715 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3716 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3717 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3718 | { |
| 3719 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3720 | |
| 3721 | /* Initialise the string catenation. */ |
| 3722 | luaL_buffinit(L, &appctx->b); |
| 3723 | |
| 3724 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3725 | } |
| 3726 | |
| 3727 | /* If expected data not yet available, it returns a yield. This function |
| 3728 | * consumes the data in the buffer. It returns a string containing the |
| 3729 | * data. This string can be empty. |
| 3730 | */ |
| 3731 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3732 | { |
| 3733 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3734 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3735 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3736 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3737 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3738 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3739 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3740 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3741 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3742 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3743 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3744 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3745 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3746 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3747 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3748 | 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] | 3749 | } |
| 3750 | |
| 3751 | /* End of data: commit the total strings and return. */ |
| 3752 | if (ret < 0) { |
| 3753 | luaL_pushresult(&appctx->b); |
| 3754 | return 1; |
| 3755 | } |
| 3756 | |
| 3757 | /* Ensure that the block 2 length is usable. */ |
| 3758 | if (ret == 1) |
| 3759 | len2 = 0; |
| 3760 | |
| 3761 | if (len == -1) { |
| 3762 | |
| 3763 | /* If len == -1, catenate all the data avalaile and |
| 3764 | * yield because we want to get all the data until |
| 3765 | * the end of data stream. |
| 3766 | */ |
| 3767 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3768 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3769 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3770 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3771 | 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] | 3772 | |
| 3773 | } else { |
| 3774 | |
| 3775 | /* Copy the fisrt block caping to the length required. */ |
| 3776 | if (len1 > len) |
| 3777 | len1 = len; |
| 3778 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3779 | len -= len1; |
| 3780 | |
| 3781 | /* Copy the second block. */ |
| 3782 | if (len2 > len) |
| 3783 | len2 = len; |
| 3784 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3785 | len -= len2; |
| 3786 | |
| 3787 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3788 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3789 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3790 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3791 | if (len > 0) { |
| 3792 | lua_pushinteger(L, len); |
| 3793 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3794 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3795 | 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] | 3796 | } |
| 3797 | |
| 3798 | /* return the result. */ |
| 3799 | luaL_pushresult(&appctx->b); |
| 3800 | return 1; |
| 3801 | } |
| 3802 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3803 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3804 | hlua_pusherror(L, "Lua: internal error"); |
| 3805 | WILL_LJMP(lua_error(L)); |
| 3806 | return 0; |
| 3807 | } |
| 3808 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3809 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3810 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3811 | { |
| 3812 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3813 | int len = -1; |
| 3814 | |
| 3815 | if (lua_gettop(L) > 2) |
| 3816 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3817 | if (lua_gettop(L) >= 2) { |
| 3818 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3819 | lua_pop(L, 1); |
| 3820 | } |
| 3821 | |
| 3822 | /* Confirm or set the required length */ |
| 3823 | lua_pushinteger(L, len); |
| 3824 | |
| 3825 | /* Initialise the string catenation. */ |
| 3826 | luaL_buffinit(L, &appctx->b); |
| 3827 | |
| 3828 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3829 | } |
| 3830 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3831 | /* Append data in the output side of the buffer. This data is immediately |
| 3832 | * sent. The function returns the amount of data written. If the buffer |
| 3833 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3834 | * if the channel is closed. |
| 3835 | */ |
| 3836 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3837 | { |
| 3838 | size_t len; |
| 3839 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3840 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3841 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3842 | struct stream_interface *si = appctx->appctx->owner; |
| 3843 | struct channel *chn = si_ic(si); |
| 3844 | int max; |
| 3845 | |
| 3846 | /* Get the max amount of data which can write as input in the channel. */ |
| 3847 | max = channel_recv_max(chn); |
| 3848 | if (max > (len - l)) |
| 3849 | max = len - l; |
| 3850 | |
| 3851 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3852 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3853 | |
| 3854 | /* update counters. */ |
| 3855 | l += max; |
| 3856 | lua_pop(L, 1); |
| 3857 | lua_pushinteger(L, l); |
| 3858 | |
| 3859 | /* If some data is not send, declares the situation to the |
| 3860 | * applet, and returns a yield. |
| 3861 | */ |
| 3862 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3863 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3864 | 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] | 3865 | } |
| 3866 | |
| 3867 | return 1; |
| 3868 | } |
| 3869 | |
| 3870 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3871 | * yield the LUA process, and resume it without checking the |
| 3872 | * input arguments. |
| 3873 | */ |
| 3874 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3875 | { |
| 3876 | MAY_LJMP(check_args(L, 2, "send")); |
| 3877 | lua_pushinteger(L, 0); |
| 3878 | |
| 3879 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3880 | } |
| 3881 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3882 | /* |
| 3883 | * |
| 3884 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3885 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3886 | * |
| 3887 | * |
| 3888 | */ |
| 3889 | |
| 3890 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3891 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3892 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3893 | __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] | 3894 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3895 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3896 | } |
| 3897 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3898 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3899 | * according with a current TXN. |
| 3900 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3901 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3902 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3903 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3904 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3905 | struct stream_interface *si = ctx->owner; |
| 3906 | struct stream *s = si_strm(si); |
| 3907 | struct proxy *px = s->be; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3908 | |
| 3909 | /* Check stack size. */ |
| 3910 | if (!lua_checkstack(L, 3)) |
| 3911 | return 0; |
| 3912 | |
| 3913 | /* Create the object: obj[0] = userdata. |
| 3914 | * Note that the base of the Converters object is the |
| 3915 | * same than the TXN object. |
| 3916 | */ |
| 3917 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3918 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3919 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3920 | appctx->appctx = ctx; |
| 3921 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3922 | 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] | 3923 | appctx->htxn.s = s; |
| 3924 | appctx->htxn.p = px; |
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 "f" field that contains a list of fetches. */ |
| 3927 | lua_pushstring(L, "f"); |
| 3928 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 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 "sf" field that contains a list of stringsafe fetches. */ |
| 3933 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3934 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 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 "c" field that contains a list of converters. */ |
| 3939 | lua_pushstring(L, "c"); |
| 3940 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3941 | return 0; |
| 3942 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3943 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3944 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3945 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3946 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3947 | return 0; |
| 3948 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3949 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3950 | if (IS_HTX_STRM(s)) { |
| 3951 | /* HTX version */ |
| 3952 | struct htx *htx = htxbuf(&s->req.buf); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3953 | struct htx_blk *blk; |
| 3954 | struct htx_sl *sl; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3955 | struct ist path; |
| 3956 | unsigned long long len = 0; |
| 3957 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3958 | |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 3959 | blk = htx_get_first_blk(htx); |
| 3960 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
| 3961 | sl = htx_get_blk_ptr(htx, blk); |
| 3962 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3963 | /* Stores the request method. */ |
| 3964 | lua_pushstring(L, "method"); |
| 3965 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3966 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3967 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3968 | /* Stores the http version. */ |
| 3969 | lua_pushstring(L, "version"); |
| 3970 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3971 | lua_settable(L, -3); |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3972 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3973 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3974 | * the array on the top of the stack. |
| 3975 | */ |
| 3976 | lua_pushstring(L, "headers"); |
| 3977 | htxn.s = s; |
| 3978 | htxn.p = px; |
| 3979 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3980 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3981 | return 0; |
| 3982 | lua_settable(L, -3); |
| 3983 | |
| 3984 | path = http_get_path(htx_sl_req_uri(sl)); |
| 3985 | if (path.ptr) { |
| 3986 | char *p, *q, *end; |
| 3987 | |
| 3988 | p = path.ptr; |
| 3989 | end = path.ptr + path.len; |
| 3990 | q = p; |
| 3991 | while (q < end && *q != '?') |
| 3992 | q++; |
| 3993 | |
| 3994 | /* Stores the request path. */ |
| 3995 | lua_pushstring(L, "path"); |
| 3996 | lua_pushlstring(L, p, q - p); |
| 3997 | lua_settable(L, -3); |
| 3998 | |
| 3999 | /* Stores the query string. */ |
| 4000 | lua_pushstring(L, "qs"); |
| 4001 | if (*q == '?') |
| 4002 | q++; |
| 4003 | lua_pushlstring(L, q, end - q); |
| 4004 | lua_settable(L, -3); |
| 4005 | } |
| 4006 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4007 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4008 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4009 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4010 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 4011 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4012 | break; |
| 4013 | if (type == HTX_BLK_DATA) |
| 4014 | len += htx_get_blksz(blk); |
| 4015 | } |
| 4016 | if (htx->extra != ULLONG_MAX) |
| 4017 | len += htx->extra; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4018 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4019 | /* Stores the request path. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4020 | lua_pushstring(L, "length"); |
| 4021 | lua_pushinteger(L, len); |
| 4022 | lua_settable(L, -3); |
| 4023 | } |
| 4024 | else { |
| 4025 | /* Legacy HTTP version */ |
| 4026 | struct http_txn *txn = s->txn; |
| 4027 | const char *path; |
| 4028 | const char *end; |
| 4029 | const char *p; |
| 4030 | |
| 4031 | /* Stores the request method. */ |
| 4032 | lua_pushstring(L, "method"); |
| 4033 | 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] | 4034 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4035 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4036 | /* Stores the http version. */ |
| 4037 | lua_pushstring(L, "version"); |
| 4038 | 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] | 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 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4042 | * the array on the top of the stack. |
| 4043 | */ |
| 4044 | lua_pushstring(L, "headers"); |
| 4045 | htxn.s = s; |
| 4046 | htxn.p = px; |
| 4047 | htxn.dir = SMP_OPT_DIR_REQ; |
| 4048 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 4049 | return 0; |
| 4050 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4051 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4052 | /* Get path and qs */ |
| 4053 | path = http_txn_get_path(txn); |
| 4054 | if (path) { |
| 4055 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 4056 | p = path; |
| 4057 | while (p < end && *p != '?') |
| 4058 | p++; |
| 4059 | |
| 4060 | /* Stores the request path. */ |
| 4061 | lua_pushstring(L, "path"); |
| 4062 | lua_pushlstring(L, path, p - path); |
| 4063 | lua_settable(L, -3); |
| 4064 | |
| 4065 | /* Stores the query string. */ |
| 4066 | lua_pushstring(L, "qs"); |
| 4067 | if (*p == '?') |
| 4068 | p++; |
| 4069 | lua_pushlstring(L, p, end - p); |
| 4070 | lua_settable(L, -3); |
| 4071 | } |
| 4072 | |
| 4073 | /* Stores the request path. */ |
| 4074 | lua_pushstring(L, "length"); |
| 4075 | lua_pushinteger(L, txn->req.body_len); |
| 4076 | lua_settable(L, -3); |
| 4077 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4078 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4079 | /* Create an empty array of HTTP request headers. */ |
| 4080 | lua_pushstring(L, "response"); |
| 4081 | lua_newtable(L); |
| 4082 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4083 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4084 | /* Pop a class stream metatable and affect it to the table. */ |
| 4085 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4086 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4087 | |
| 4088 | return 1; |
| 4089 | } |
| 4090 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4091 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4092 | { |
| 4093 | struct hlua_appctx *appctx; |
| 4094 | struct stream *s; |
| 4095 | const char *name; |
| 4096 | size_t len; |
| 4097 | struct sample smp; |
| 4098 | |
| 4099 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4100 | |
| 4101 | /* It is useles to retrieve the stream, but this function |
| 4102 | * runs only in a stream context. |
| 4103 | */ |
| 4104 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4105 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4106 | s = appctx->htxn.s; |
| 4107 | |
| 4108 | /* Converts the third argument in a sample. */ |
| 4109 | hlua_lua2smp(L, 3, &smp); |
| 4110 | |
| 4111 | /* Store the sample in a variable. */ |
| 4112 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4113 | vars_set_by_name(name, len, &smp); |
| 4114 | return 0; |
| 4115 | } |
| 4116 | |
| 4117 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4118 | { |
| 4119 | struct hlua_appctx *appctx; |
| 4120 | struct stream *s; |
| 4121 | const char *name; |
| 4122 | size_t len; |
| 4123 | struct sample smp; |
| 4124 | |
| 4125 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4126 | |
| 4127 | /* It is useles to retrieve the stream, but this function |
| 4128 | * runs only in a stream context. |
| 4129 | */ |
| 4130 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4131 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4132 | s = appctx->htxn.s; |
| 4133 | |
| 4134 | /* Unset the variable. */ |
| 4135 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4136 | vars_unset_by_name(name, len, &smp); |
| 4137 | return 0; |
| 4138 | } |
| 4139 | |
| 4140 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4141 | { |
| 4142 | struct hlua_appctx *appctx; |
| 4143 | struct stream *s; |
| 4144 | const char *name; |
| 4145 | size_t len; |
| 4146 | struct sample smp; |
| 4147 | |
| 4148 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4149 | |
| 4150 | /* It is useles to retrieve the stream, but this function |
| 4151 | * runs only in a stream context. |
| 4152 | */ |
| 4153 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4154 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4155 | s = appctx->htxn.s; |
| 4156 | |
| 4157 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4158 | if (!vars_get_by_name(name, len, &smp)) { |
| 4159 | lua_pushnil(L); |
| 4160 | return 1; |
| 4161 | } |
| 4162 | |
| 4163 | return hlua_smp2lua(L, &smp); |
| 4164 | } |
| 4165 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4166 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4167 | { |
| 4168 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4169 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4170 | struct hlua *hlua; |
| 4171 | |
| 4172 | /* 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] | 4173 | if (!s->hlua) |
| 4174 | return 0; |
| 4175 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4176 | |
| 4177 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4178 | |
| 4179 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4180 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4181 | |
| 4182 | /* Get and store new value. */ |
| 4183 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4184 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4185 | |
| 4186 | return 0; |
| 4187 | } |
| 4188 | |
| 4189 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4190 | { |
| 4191 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4192 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4193 | struct hlua *hlua; |
| 4194 | |
| 4195 | /* 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] | 4196 | if (!s->hlua) { |
| 4197 | lua_pushnil(L); |
| 4198 | return 1; |
| 4199 | } |
| 4200 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4201 | |
| 4202 | /* Push configuration index in the stack. */ |
| 4203 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4204 | |
| 4205 | return 1; |
| 4206 | } |
| 4207 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4208 | /* If expected data not yet available, it returns a yield. This function |
| 4209 | * consumes the data in the buffer. It returns a string containing the |
| 4210 | * data. This string can be empty. |
| 4211 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4212 | __LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4213 | { |
| 4214 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4215 | struct stream_interface *si = appctx->appctx->owner; |
| 4216 | struct channel *req = si_oc(si); |
| 4217 | struct htx *htx; |
| 4218 | struct htx_blk *blk; |
| 4219 | size_t count; |
| 4220 | int stop = 0; |
| 4221 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4222 | htx = htx_from_buf(&req->buf); |
| 4223 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4224 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4225 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4226 | while (count && !stop && blk) { |
| 4227 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4228 | uint32_t sz = htx_get_blksz(blk); |
| 4229 | struct ist v; |
| 4230 | uint32_t vlen; |
| 4231 | char *nl; |
| 4232 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4233 | if (type == HTX_BLK_EOM) { |
| 4234 | stop = 1; |
| 4235 | break; |
| 4236 | } |
| 4237 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4238 | vlen = sz; |
| 4239 | if (vlen > count) { |
| 4240 | if (type != HTX_BLK_DATA) |
| 4241 | break; |
| 4242 | vlen = count; |
| 4243 | } |
| 4244 | |
| 4245 | switch (type) { |
| 4246 | case HTX_BLK_UNUSED: |
| 4247 | break; |
| 4248 | |
| 4249 | case HTX_BLK_DATA: |
| 4250 | v = htx_get_blk_value(htx, blk); |
| 4251 | v.len = vlen; |
| 4252 | nl = istchr(v, '\n'); |
| 4253 | if (nl != NULL) { |
| 4254 | stop = 1; |
| 4255 | vlen = nl - v.ptr + 1; |
| 4256 | } |
| 4257 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4258 | break; |
| 4259 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4260 | case HTX_BLK_TLR: |
| 4261 | case HTX_BLK_EOM: |
| 4262 | stop = 1; |
| 4263 | break; |
| 4264 | |
| 4265 | default: |
| 4266 | break; |
| 4267 | } |
| 4268 | |
| 4269 | co_set_data(req, co_data(req) - vlen); |
| 4270 | count -= vlen; |
| 4271 | if (sz == vlen) |
| 4272 | blk = htx_remove_blk(htx, blk); |
| 4273 | else { |
| 4274 | htx_cut_data_blk(htx, blk, vlen); |
| 4275 | break; |
| 4276 | } |
| 4277 | } |
| 4278 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4279 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4280 | if (!stop) { |
| 4281 | si_cant_get(si); |
| 4282 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0)); |
| 4283 | } |
| 4284 | |
| 4285 | /* return the result. */ |
| 4286 | luaL_pushresult(&appctx->b); |
| 4287 | return 1; |
| 4288 | } |
| 4289 | |
| 4290 | |
| 4291 | /* If expected data not yet available, it returns a yield. This function |
| 4292 | * consumes the data in the buffer. It returns a string containing the |
| 4293 | * data. This string can be empty. |
| 4294 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4295 | __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] | 4296 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4297 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4298 | struct stream_interface *si = appctx->appctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4299 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4300 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4301 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4302 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4303 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4304 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4305 | /* Check for the end of the data. */ |
| 4306 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4307 | luaL_pushresult(&appctx->b); |
| 4308 | return 1; |
| 4309 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4310 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4311 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4312 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4313 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4314 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4315 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4316 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4317 | 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] | 4318 | } |
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 | /* End of data: commit the total strings and return. */ |
| 4321 | if (ret < 0) { |
| 4322 | luaL_pushresult(&appctx->b); |
| 4323 | return 1; |
| 4324 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4325 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4326 | /* Ensure that the block 2 length is usable. */ |
| 4327 | if (ret == 1) |
| 4328 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4329 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4330 | /* Copy the fisrt block caping to the length required. */ |
| 4331 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4332 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4333 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4334 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4335 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4336 | /* Copy the second block. */ |
| 4337 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4338 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4339 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4340 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4341 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4342 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4343 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4344 | luaL_pushresult(&appctx->b); |
| 4345 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4346 | } |
| 4347 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4348 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4349 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4350 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4351 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4352 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4353 | /* Initialise the string catenation. */ |
| 4354 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4355 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4356 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 4357 | return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0)); |
| 4358 | else |
| 4359 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4360 | } |
| 4361 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4362 | /* If expected data not yet available, it returns a yield. This function |
| 4363 | * consumes the data in the buffer. It returns a string containing the |
| 4364 | * data. This string can be empty. |
| 4365 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4366 | __LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4367 | { |
| 4368 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4369 | struct stream_interface *si = appctx->appctx->owner; |
| 4370 | struct channel *req = si_oc(si); |
| 4371 | struct htx *htx; |
| 4372 | struct htx_blk *blk; |
| 4373 | size_t count; |
| 4374 | int len; |
| 4375 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4376 | htx = htx_from_buf(&req->buf); |
| 4377 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4378 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4379 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4380 | while (count && len && blk) { |
| 4381 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4382 | uint32_t sz = htx_get_blksz(blk); |
| 4383 | struct ist v; |
| 4384 | uint32_t vlen; |
| 4385 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4386 | if (type == HTX_BLK_EOM) { |
| 4387 | len = 0; |
| 4388 | break; |
| 4389 | } |
| 4390 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4391 | vlen = sz; |
| 4392 | if (len > 0 && vlen > len) |
| 4393 | vlen = len; |
| 4394 | if (vlen > count) { |
| 4395 | if (type != HTX_BLK_DATA) |
| 4396 | break; |
| 4397 | vlen = count; |
| 4398 | } |
| 4399 | |
| 4400 | switch (type) { |
| 4401 | case HTX_BLK_UNUSED: |
| 4402 | break; |
| 4403 | |
| 4404 | case HTX_BLK_DATA: |
| 4405 | v = htx_get_blk_value(htx, blk); |
| 4406 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4407 | break; |
| 4408 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4409 | case HTX_BLK_TLR: |
| 4410 | case HTX_BLK_EOM: |
| 4411 | len = 0; |
| 4412 | break; |
| 4413 | |
| 4414 | default: |
| 4415 | break; |
| 4416 | } |
| 4417 | |
| 4418 | co_set_data(req, co_data(req) - vlen); |
| 4419 | count -= vlen; |
| 4420 | if (len > 0) |
| 4421 | len -= vlen; |
| 4422 | if (sz == vlen) |
| 4423 | blk = htx_remove_blk(htx, blk); |
| 4424 | else { |
| 4425 | htx_cut_data_blk(htx, blk, vlen); |
| 4426 | break; |
| 4427 | } |
| 4428 | } |
| 4429 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4430 | htx_to_buf(htx, &req->buf); |
| 4431 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4432 | /* If we are no other data available, yield waiting for new data. */ |
| 4433 | if (len) { |
| 4434 | if (len > 0) { |
| 4435 | lua_pushinteger(L, len); |
| 4436 | lua_replace(L, 2); |
| 4437 | } |
| 4438 | si_cant_get(si); |
| 4439 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0)); |
| 4440 | } |
| 4441 | |
| 4442 | /* return the result. */ |
| 4443 | luaL_pushresult(&appctx->b); |
| 4444 | return 1; |
| 4445 | } |
| 4446 | |
| 4447 | /* If expected data not yet available, it returns a yield. This function |
| 4448 | * consumes the data in the buffer. It returns a string containing the |
| 4449 | * data. This string can be empty. |
| 4450 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4451 | __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] | 4452 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4453 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4454 | struct stream_interface *si = appctx->appctx->owner; |
| 4455 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4456 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4457 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4458 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4459 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4460 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4461 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4462 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4463 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4464 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4465 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4466 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4467 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4468 | 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] | 4469 | } |
| 4470 | |
| 4471 | /* End of data: commit the total strings and return. */ |
| 4472 | if (ret < 0) { |
| 4473 | luaL_pushresult(&appctx->b); |
| 4474 | return 1; |
| 4475 | } |
| 4476 | |
| 4477 | /* Ensure that the block 2 length is usable. */ |
| 4478 | if (ret == 1) |
| 4479 | len2 = 0; |
| 4480 | |
| 4481 | /* Copy the fisrt block caping to the length required. */ |
| 4482 | if (len1 > len) |
| 4483 | len1 = len; |
| 4484 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4485 | len -= len1; |
| 4486 | |
| 4487 | /* Copy the second block. */ |
| 4488 | if (len2 > len) |
| 4489 | len2 = len; |
| 4490 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4491 | len -= len2; |
| 4492 | |
| 4493 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4494 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4495 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4496 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4497 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4498 | /* If we are no other data available, yield waiting for new data. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4499 | if (len > 0) { |
| 4500 | lua_pushinteger(L, len); |
| 4501 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4502 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4503 | 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] | 4504 | } |
| 4505 | |
| 4506 | /* return the result. */ |
| 4507 | luaL_pushresult(&appctx->b); |
| 4508 | return 1; |
| 4509 | } |
| 4510 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4511 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4512 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4513 | { |
| 4514 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4515 | int len = -1; |
| 4516 | |
| 4517 | /* Check arguments. */ |
| 4518 | if (lua_gettop(L) > 2) |
| 4519 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4520 | if (lua_gettop(L) >= 2) { |
| 4521 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4522 | lua_pop(L, 1); |
| 4523 | } |
| 4524 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4525 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4526 | /* HTX version */ |
| 4527 | lua_pushinteger(L, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4528 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4529 | /* Initialise the string catenation. */ |
| 4530 | luaL_buffinit(L, &appctx->b); |
| 4531 | |
| 4532 | return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0)); |
| 4533 | } |
| 4534 | else { |
| 4535 | /* Legacy HTTP version */ |
| 4536 | /* Check the required length */ |
| 4537 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4538 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4539 | lua_pushinteger(L, len); |
| 4540 | |
| 4541 | /* Initialise the string catenation. */ |
| 4542 | luaL_buffinit(L, &appctx->b); |
| 4543 | |
| 4544 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4545 | } |
| 4546 | } |
| 4547 | |
| 4548 | /* Append data in the output side of the buffer. This data is immediately |
| 4549 | * sent. The function returns the amount of data written. If the buffer |
| 4550 | * cannot contain the data, the function yields. The function returns -1 |
| 4551 | * if the channel is closed. |
| 4552 | */ |
| 4553 | __LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4554 | { |
| 4555 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4556 | struct stream_interface *si = appctx->appctx->owner; |
| 4557 | struct channel *res = si_ic(si); |
| 4558 | struct htx *htx = htx_from_buf(&res->buf); |
| 4559 | const char *data; |
| 4560 | size_t len; |
| 4561 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4562 | int max; |
| 4563 | |
Christopher Faulet | 4e96301 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4564 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4565 | if (!max) |
| 4566 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4567 | |
| 4568 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4569 | |
| 4570 | /* Get the max amount of data which can write as input in the channel. */ |
| 4571 | if (max > (len - l)) |
| 4572 | max = len - l; |
| 4573 | |
| 4574 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4575 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4576 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4577 | |
| 4578 | /* update counters. */ |
| 4579 | l += max; |
| 4580 | lua_pop(L, 1); |
| 4581 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4582 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4583 | /* If some data is not send, declares the situation to the |
| 4584 | * applet, and returns a yield. |
| 4585 | */ |
| 4586 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4587 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4588 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4589 | si_rx_room_blk(si); |
| 4590 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0)); |
| 4591 | } |
| 4592 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4593 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4594 | return 1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4595 | } |
| 4596 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4597 | /* Append data in the output side of the buffer. This data is immediately |
| 4598 | * sent. The function returns the amount of data written. If the buffer |
| 4599 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4600 | * if the channel is closed. |
| 4601 | */ |
| 4602 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4603 | { |
| 4604 | size_t len; |
| 4605 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4606 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4607 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4608 | struct stream_interface *si = appctx->appctx->owner; |
| 4609 | struct channel *chn = si_ic(si); |
| 4610 | int max; |
| 4611 | |
| 4612 | /* Get the max amount of data which can write as input in the channel. */ |
| 4613 | max = channel_recv_max(chn); |
| 4614 | if (max > (len - l)) |
| 4615 | max = len - l; |
| 4616 | |
| 4617 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4618 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4619 | |
| 4620 | /* update counters. */ |
| 4621 | l += max; |
| 4622 | lua_pop(L, 1); |
| 4623 | lua_pushinteger(L, l); |
| 4624 | |
| 4625 | /* If some data is not send, declares the situation to the |
| 4626 | * applet, and returns a yield. |
| 4627 | */ |
| 4628 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4629 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4630 | 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] | 4631 | } |
| 4632 | |
| 4633 | return 1; |
| 4634 | } |
| 4635 | |
| 4636 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4637 | * yield the LUA process, and resume it without checking the |
| 4638 | * input arguments. |
| 4639 | */ |
| 4640 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4641 | { |
| 4642 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4643 | |
| 4644 | /* We want to send some data. Headers must be sent. */ |
| 4645 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4646 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4647 | WILL_LJMP(lua_error(L)); |
| 4648 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4649 | |
| 4650 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4651 | /* HTX version */ |
| 4652 | /* This interger is used for followinf the amount of data sent. */ |
| 4653 | lua_pushinteger(L, 0); |
| 4654 | |
| 4655 | return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0)); |
| 4656 | } |
| 4657 | else { |
| 4658 | /* Legacy HTTP version */ |
| 4659 | size_t len; |
| 4660 | char hex[10]; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4661 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4662 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4663 | |
| 4664 | /* If transfer encoding chunked is selected, we surround the data |
| 4665 | * by chunk data. |
| 4666 | */ |
| 4667 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4668 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4669 | lua_pushfstring(L, "%s\r\n", hex); |
| 4670 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4671 | lua_pushstring(L, "\r\n"); |
| 4672 | lua_concat(L, 3); |
| 4673 | } |
| 4674 | |
| 4675 | /* This interger is used for followinf the amount of data sent. */ |
| 4676 | lua_pushinteger(L, 0); |
| 4677 | |
| 4678 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4679 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4680 | } |
| 4681 | |
| 4682 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4683 | { |
| 4684 | const char *name; |
| 4685 | int ret; |
| 4686 | |
| 4687 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4688 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4689 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4690 | |
| 4691 | /* Push in the stack the "response" entry. */ |
| 4692 | ret = lua_getfield(L, 1, "response"); |
| 4693 | if (ret != LUA_TTABLE) { |
| 4694 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4695 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4696 | WILL_LJMP(lua_error(L)); |
| 4697 | } |
| 4698 | |
| 4699 | /* check if the header is already registered if it is not |
| 4700 | * the case, register it. |
| 4701 | */ |
| 4702 | ret = lua_getfield(L, -1, name); |
| 4703 | if (ret == LUA_TNIL) { |
| 4704 | |
| 4705 | /* Entry not found. */ |
| 4706 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4707 | |
| 4708 | /* Insert the new header name in the array in the top of the stack. |
| 4709 | * It left the new array in the top of the stack. |
| 4710 | */ |
| 4711 | lua_newtable(L); |
| 4712 | lua_pushvalue(L, 2); |
| 4713 | lua_pushvalue(L, -2); |
| 4714 | lua_settable(L, -4); |
| 4715 | |
| 4716 | } else if (ret != LUA_TTABLE) { |
| 4717 | |
| 4718 | /* corruption error. */ |
| 4719 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4720 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4721 | WILL_LJMP(lua_error(L)); |
| 4722 | } |
| 4723 | |
| 4724 | /* Now the top od thestack is an array of values. We push |
| 4725 | * the header value as new entry. |
| 4726 | */ |
| 4727 | lua_pushvalue(L, 3); |
| 4728 | ret = lua_rawlen(L, -2); |
| 4729 | lua_rawseti(L, -2, ret + 1); |
| 4730 | lua_pushboolean(L, 1); |
| 4731 | return 1; |
| 4732 | } |
| 4733 | |
| 4734 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4735 | { |
| 4736 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4737 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4738 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4739 | |
| 4740 | if (status < 100 || status > 599) { |
| 4741 | lua_pushboolean(L, 0); |
| 4742 | return 1; |
| 4743 | } |
| 4744 | |
| 4745 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4746 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4747 | lua_pushboolean(L, 1); |
| 4748 | return 1; |
| 4749 | } |
| 4750 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4751 | |
| 4752 | __LJMP static int hlua_applet_htx_send_response(lua_State *L) |
| 4753 | { |
| 4754 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4755 | struct stream_interface *si = appctx->appctx->owner; |
| 4756 | struct channel *res = si_ic(si); |
| 4757 | struct htx *htx; |
| 4758 | struct htx_sl *sl; |
| 4759 | struct h1m h1m; |
| 4760 | const char *status, *reason; |
| 4761 | const char *name, *value; |
| 4762 | size_t nlen, vlen; |
| 4763 | unsigned int flags; |
| 4764 | |
| 4765 | /* Send the message at once. */ |
| 4766 | htx = htx_from_buf(&res->buf); |
| 4767 | h1m_init_res(&h1m); |
| 4768 | |
| 4769 | /* Use the same http version than the request. */ |
| 4770 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4771 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4772 | if (reason == NULL) |
| 4773 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4774 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4775 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4776 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4777 | } |
| 4778 | else { |
| 4779 | flags = HTX_SL_F_IS_RESP; |
| 4780 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4781 | } |
| 4782 | if (!sl) { |
| 4783 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4784 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4785 | WILL_LJMP(lua_error(L)); |
| 4786 | } |
| 4787 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4788 | |
| 4789 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4790 | lua_pushvalue(L, 0); |
| 4791 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4792 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4793 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4794 | WILL_LJMP(lua_error(L)); |
| 4795 | } |
| 4796 | |
| 4797 | /* Browse the list of headers. */ |
| 4798 | lua_pushnil(L); |
| 4799 | while(lua_next(L, -2) != 0) { |
| 4800 | /* We expect a string as -2. */ |
| 4801 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4802 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4803 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4804 | lua_typename(L, lua_type(L, -2))); |
| 4805 | WILL_LJMP(lua_error(L)); |
| 4806 | } |
| 4807 | name = lua_tolstring(L, -2, &nlen); |
| 4808 | |
| 4809 | /* We expect an array as -1. */ |
| 4810 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4811 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4812 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4813 | name, |
| 4814 | lua_typename(L, lua_type(L, -1))); |
| 4815 | WILL_LJMP(lua_error(L)); |
| 4816 | } |
| 4817 | |
| 4818 | /* Browse the table who is on the top of the stack. */ |
| 4819 | lua_pushnil(L); |
| 4820 | while(lua_next(L, -2) != 0) { |
| 4821 | int id; |
| 4822 | |
| 4823 | /* We expect a number as -2. */ |
| 4824 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4825 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4826 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4827 | name, |
| 4828 | lua_typename(L, lua_type(L, -2))); |
| 4829 | WILL_LJMP(lua_error(L)); |
| 4830 | } |
| 4831 | id = lua_tointeger(L, -2); |
| 4832 | |
| 4833 | /* We expect a string as -2. */ |
| 4834 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4835 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4836 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4837 | name, id, |
| 4838 | lua_typename(L, lua_type(L, -1))); |
| 4839 | WILL_LJMP(lua_error(L)); |
| 4840 | } |
| 4841 | value = lua_tolstring(L, -1, &vlen); |
| 4842 | |
| 4843 | /* Simple Protocol checks. */ |
| 4844 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
| 4845 | h1_parse_xfer_enc_header(&h1m, ist2(name, nlen)); |
| 4846 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4847 | struct ist v = ist2(value, vlen); |
| 4848 | int ret; |
| 4849 | |
| 4850 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4851 | if (ret < 0) { |
| 4852 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4853 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4854 | name); |
| 4855 | WILL_LJMP(lua_error(L)); |
| 4856 | } |
| 4857 | else if (ret == 0) |
| 4858 | goto next; /* Skip it */ |
| 4859 | } |
| 4860 | |
| 4861 | /* Add a new header */ |
| 4862 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4863 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4864 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4865 | name); |
| 4866 | WILL_LJMP(lua_error(L)); |
| 4867 | } |
| 4868 | next: |
| 4869 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4870 | lua_pop(L, 1); |
| 4871 | } |
| 4872 | |
| 4873 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4874 | lua_pop(L, 1); |
| 4875 | } |
| 4876 | |
| 4877 | if (h1m.flags & H1_MF_CHNK) |
| 4878 | h1m.flags &= ~H1_MF_CLEN; |
| 4879 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4880 | h1m.flags |= H1_MF_XFER_LEN; |
| 4881 | |
| 4882 | /* Uset HTX start-line flags */ |
| 4883 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4884 | flags |= HTX_SL_F_XFER_ENC; |
| 4885 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4886 | flags |= HTX_SL_F_XFER_LEN; |
| 4887 | if (h1m.flags & H1_MF_CHNK) |
| 4888 | flags |= HTX_SL_F_CHNK; |
| 4889 | else if (h1m.flags & H1_MF_CLEN) |
| 4890 | flags |= HTX_SL_F_CLEN; |
| 4891 | if (h1m.body_len == 0) |
| 4892 | flags |= HTX_SL_F_BODYLESS; |
| 4893 | } |
| 4894 | sl->flags |= flags; |
| 4895 | |
| 4896 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4897 | * and the status code implies the presence of a message body, we must |
| 4898 | * announce a transfer encoding chunked. This is required by haproxy |
| 4899 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4900 | * chunked itslef, don't do anything. |
| 4901 | */ |
| 4902 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4903 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4904 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4905 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4906 | /* Add a new header */ |
| 4907 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4908 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4909 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4910 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4911 | WILL_LJMP(lua_error(L)); |
| 4912 | } |
| 4913 | } |
| 4914 | |
| 4915 | /* Finalize headers. */ |
| 4916 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4917 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4918 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4919 | WILL_LJMP(lua_error(L)); |
| 4920 | } |
| 4921 | |
| 4922 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4923 | b_reset(&res->buf); |
| 4924 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4925 | WILL_LJMP(lua_error(L)); |
| 4926 | } |
| 4927 | |
| 4928 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4929 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4930 | |
| 4931 | /* Headers sent, set the flag. */ |
| 4932 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4933 | return 0; |
| 4934 | |
| 4935 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4936 | /* We will build the status line and the headers of the HTTP response. |
| 4937 | * We will try send at once if its not possible, we give back the hand |
| 4938 | * waiting for more room. |
| 4939 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4940 | __LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4941 | { |
| 4942 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4943 | struct stream_interface *si = appctx->appctx->owner; |
| 4944 | struct channel *res = si_ic(si); |
| 4945 | |
| 4946 | if (co_data(res)) { |
| 4947 | si_rx_room_blk(si); |
| 4948 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0)); |
| 4949 | } |
| 4950 | return MAY_LJMP(hlua_applet_htx_send_response(L)); |
| 4951 | } |
| 4952 | |
| 4953 | |
| 4954 | __LJMP static int hlua_applet_htx_start_response(lua_State *L) |
| 4955 | { |
| 4956 | return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0)); |
| 4957 | } |
| 4958 | |
| 4959 | /* We will build the status line and the headers of the HTTP response. |
| 4960 | * We will try send at once if its not possible, we give back the hand |
| 4961 | * waiting for more room. |
| 4962 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4963 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4964 | { |
| 4965 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4966 | struct stream_interface *si = appctx->appctx->owner; |
| 4967 | struct channel *chn = si_ic(si); |
| 4968 | int ret; |
| 4969 | size_t len; |
| 4970 | const char *msg; |
| 4971 | |
| 4972 | /* Get the message as the first argument on the stack. */ |
| 4973 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4974 | |
| 4975 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4976 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4977 | |
| 4978 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4979 | * big for the buffers. |
| 4980 | */ |
| 4981 | if (ret == -2 || ret == -3) { |
| 4982 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4983 | WILL_LJMP(lua_error(L)); |
| 4984 | } |
| 4985 | |
| 4986 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4987 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4988 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4989 | 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] | 4990 | } |
| 4991 | |
| 4992 | /* Headers sent, set the flag. */ |
| 4993 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4994 | return 0; |
| 4995 | } |
| 4996 | |
| 4997 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4998 | { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4999 | struct buffer *tmp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5000 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5001 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5002 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5003 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5004 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5005 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5006 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5007 | int hdr_chunked = 0; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5008 | const char *reason; |
| 5009 | |
| 5010 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 5011 | return MAY_LJMP(hlua_applet_htx_start_response(L)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5012 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5013 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5014 | if (reason == NULL) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 5015 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5016 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5017 | tmp = get_trash_chunk(); |
| 5018 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5019 | /* Use the same http version than the request. */ |
| 5020 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 5021 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5022 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5023 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5024 | |
| 5025 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5026 | lua_pushvalue(L, 0); |
| 5027 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5028 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 5029 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 5030 | WILL_LJMP(lua_error(L)); |
| 5031 | } |
| 5032 | |
| 5033 | /* Browse the list of headers. */ |
| 5034 | lua_pushnil(L); |
| 5035 | while(lua_next(L, -2) != 0) { |
| 5036 | |
| 5037 | /* We expect a string as -2. */ |
| 5038 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5039 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 5040 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5041 | lua_typename(L, lua_type(L, -2))); |
| 5042 | WILL_LJMP(lua_error(L)); |
| 5043 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5044 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5045 | |
| 5046 | /* We expect an array as -1. */ |
| 5047 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5048 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 5049 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5050 | name, |
| 5051 | lua_typename(L, lua_type(L, -1))); |
| 5052 | WILL_LJMP(lua_error(L)); |
| 5053 | } |
| 5054 | |
| 5055 | /* Browse the table who is on the top of the stack. */ |
| 5056 | lua_pushnil(L); |
| 5057 | while(lua_next(L, -2) != 0) { |
| 5058 | |
| 5059 | /* We expect a number as -2. */ |
| 5060 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5061 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 5062 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5063 | name, |
| 5064 | lua_typename(L, lua_type(L, -2))); |
| 5065 | WILL_LJMP(lua_error(L)); |
| 5066 | } |
| 5067 | id = lua_tointeger(L, -2); |
| 5068 | |
| 5069 | /* We expect a string as -2. */ |
| 5070 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5071 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 5072 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5073 | name, id, |
| 5074 | lua_typename(L, lua_type(L, -1))); |
| 5075 | WILL_LJMP(lua_error(L)); |
| 5076 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5077 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5078 | |
| 5079 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5080 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 5081 | memcpy(tmp->area + tmp->data, name, name_len); |
| 5082 | tmp->data += name_len; |
| 5083 | tmp->area[tmp->data++] = ':'; |
| 5084 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5085 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5086 | memcpy(tmp->area + tmp->data, value, |
| 5087 | value_len); |
| 5088 | tmp->data += value_len; |
| 5089 | tmp->area[tmp->data++] = '\r'; |
| 5090 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5091 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5092 | |
| 5093 | /* Protocol checks. */ |
| 5094 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5095 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5096 | * is done without control. If it contains a bad value, |
| 5097 | * the content-length remains negative so that we can |
| 5098 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5099 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5100 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5101 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5102 | |
| 5103 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5104 | if (name_len == 17 && value_len == 7 && |
| 5105 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5106 | strcasecmp("chunked", value) == 0) |
| 5107 | hdr_chunked = 1; |
| 5108 | |
| 5109 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5110 | lua_pop(L, 1); |
| 5111 | } |
| 5112 | |
| 5113 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5114 | lua_pop(L, 1); |
| 5115 | } |
| 5116 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5117 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 5118 | * and the status code implies the presence of a message body, we must |
| 5119 | * announce a transfer encoding chunked. This is required by haproxy |
| 5120 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 5121 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5122 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5123 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5124 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 5125 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5126 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5127 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5128 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 5129 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 5130 | } |
| 5131 | |
| 5132 | /* Finalize headers. */ |
| 5133 | chunk_appendf(tmp, "\r\n"); |
| 5134 | |
| 5135 | /* Remove the last entry and the array of headers */ |
| 5136 | lua_pop(L, 2); |
| 5137 | |
| 5138 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5139 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5140 | |
| 5141 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 5142 | } |
| 5143 | |
| 5144 | /* |
| 5145 | * |
| 5146 | * |
| 5147 | * Class HTTP |
| 5148 | * |
| 5149 | * |
| 5150 | */ |
| 5151 | |
| 5152 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5153 | * a class stream, otherwise it throws an error. |
| 5154 | */ |
| 5155 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 5156 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5157 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5158 | } |
| 5159 | |
| 5160 | /* This function creates and push in the stack a HTTP object |
| 5161 | * according with a current TXN. |
| 5162 | */ |
| 5163 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5164 | { |
| 5165 | struct hlua_txn *htxn; |
| 5166 | |
| 5167 | /* Check stack size. */ |
| 5168 | if (!lua_checkstack(L, 3)) |
| 5169 | return 0; |
| 5170 | |
| 5171 | /* Create the object: obj[0] = userdata. |
| 5172 | * Note that the base of the Converters object is the |
| 5173 | * same than the TXN object. |
| 5174 | */ |
| 5175 | lua_newtable(L); |
| 5176 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5177 | lua_rawseti(L, -2, 0); |
| 5178 | |
| 5179 | htxn->s = txn->s; |
| 5180 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5181 | htxn->dir = txn->dir; |
| 5182 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5183 | |
| 5184 | /* Pop a class stream metatable and affect it to the table. */ |
| 5185 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5186 | lua_setmetatable(L, -2); |
| 5187 | |
| 5188 | return 1; |
| 5189 | } |
| 5190 | |
| 5191 | /* This function creates ans returns an array of HTTP headers. |
| 5192 | * This function does not fails. It is used as wrapper with the |
| 5193 | * 2 following functions. |
| 5194 | */ |
| 5195 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5196 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5197 | /* Create the table. */ |
| 5198 | lua_newtable(L); |
| 5199 | |
| 5200 | if (!htxn->s->txn) |
| 5201 | return 1; |
| 5202 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5203 | if (IS_HTX_STRM(htxn->s)) { |
| 5204 | /* HTX version */ |
| 5205 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5206 | int32_t pos; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5207 | |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5208 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5209 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5210 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5211 | struct ist n, v; |
| 5212 | int len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5213 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5214 | if (type == HTX_BLK_HDR) { |
| 5215 | n = htx_get_blk_name(htx,blk); |
| 5216 | v = htx_get_blk_value(htx, blk); |
| 5217 | } |
| 5218 | else if (type == HTX_BLK_EOH) |
| 5219 | break; |
| 5220 | else |
| 5221 | continue; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5222 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5223 | /* Check for existing entry: |
| 5224 | * assume that the table is on the top of the stack, and |
| 5225 | * push the key in the stack, the function lua_gettable() |
| 5226 | * perform the lookup. |
| 5227 | */ |
| 5228 | lua_pushlstring(L, n.ptr, n.len); |
| 5229 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5230 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5231 | switch (lua_type(L, -1)) { |
| 5232 | case LUA_TNIL: |
| 5233 | /* Table not found, create it. */ |
| 5234 | lua_pop(L, 1); /* remove the nil value. */ |
| 5235 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5236 | lua_newtable(L); /* create and push empty table. */ |
| 5237 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5238 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5239 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5240 | break; |
| 5241 | |
| 5242 | case LUA_TTABLE: |
| 5243 | /* Entry found: push the value in the table. */ |
| 5244 | len = lua_rawlen(L, -1); |
| 5245 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5246 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5247 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5248 | break; |
| 5249 | |
| 5250 | default: |
| 5251 | /* Other cases are errors. */ |
| 5252 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5253 | WILL_LJMP(lua_error(L)); |
| 5254 | } |
| 5255 | } |
| 5256 | } |
| 5257 | else { |
| 5258 | /* Legacy HTTP version */ |
| 5259 | const char *cur_ptr, *cur_next, *p; |
| 5260 | int old_idx, cur_idx; |
| 5261 | struct hdr_idx_elem *cur_hdr; |
| 5262 | const char *hn, *hv; |
| 5263 | int hnl, hvl; |
| 5264 | const char *in; |
| 5265 | char *out; |
| 5266 | int len; |
| 5267 | |
| 5268 | /* Build array of headers. */ |
| 5269 | old_idx = 0; |
| 5270 | cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 5271 | |
| 5272 | while (1) { |
| 5273 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 5274 | if (!cur_idx) |
| 5275 | break; |
| 5276 | old_idx = cur_idx; |
| 5277 | |
| 5278 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 5279 | cur_ptr = cur_next; |
| 5280 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 5281 | |
| 5282 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 5283 | * and the next header starts at cur_next. We'll check |
| 5284 | * this header in the list as well as against the default |
| 5285 | * rule. |
| 5286 | */ |
| 5287 | |
| 5288 | /* look for ': *'. */ |
| 5289 | hn = cur_ptr; |
| 5290 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 5291 | if (p >= cur_ptr+cur_hdr->len) |
| 5292 | continue; |
| 5293 | hnl = p - hn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5294 | p++; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5295 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 5296 | p++; |
| 5297 | if (p >= cur_ptr+cur_hdr->len) |
| 5298 | continue; |
| 5299 | hv = p; |
| 5300 | hvl = cur_ptr+cur_hdr->len-p; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5301 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5302 | /* Lowercase the key. Don't check the size of trash, it have |
| 5303 | * the size of one buffer and the input data contains in one |
| 5304 | * buffer. |
| 5305 | */ |
| 5306 | out = trash.area; |
| 5307 | for (in=hn; in<hn+hnl; in++, out++) |
| 5308 | *out = tolower(*in); |
| 5309 | *out = '\0'; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5310 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5311 | /* Check for existing entry: |
| 5312 | * assume that the table is on the top of the stack, and |
| 5313 | * push the key in the stack, the function lua_gettable() |
| 5314 | * perform the lookup. |
| 5315 | */ |
| 5316 | lua_pushlstring(L, trash.area, hnl); |
| 5317 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5318 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5319 | switch (lua_type(L, -1)) { |
| 5320 | case LUA_TNIL: |
| 5321 | /* Table not found, create it. */ |
| 5322 | lua_pop(L, 1); /* remove the nil value. */ |
| 5323 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
| 5324 | lua_newtable(L); /* create and push empty table. */ |
| 5325 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5326 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5327 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5328 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5329 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5330 | case LUA_TTABLE: |
| 5331 | /* Entry found: push the value in the table. */ |
| 5332 | len = lua_rawlen(L, -1); |
| 5333 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5334 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5335 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5336 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5337 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5338 | default: |
| 5339 | /* Other cases are errors. */ |
| 5340 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5341 | WILL_LJMP(lua_error(L)); |
| 5342 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5343 | } |
| 5344 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5345 | return 1; |
| 5346 | } |
| 5347 | |
| 5348 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5349 | { |
| 5350 | struct hlua_txn *htxn; |
| 5351 | |
| 5352 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5353 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5354 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5355 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5356 | WILL_LJMP(lua_error(L)); |
| 5357 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5358 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 5359 | } |
| 5360 | |
| 5361 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5362 | { |
| 5363 | struct hlua_txn *htxn; |
| 5364 | |
| 5365 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5366 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5367 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5368 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5369 | WILL_LJMP(lua_error(L)); |
| 5370 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5371 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 5372 | } |
| 5373 | |
| 5374 | /* This function replace full header, or just a value in |
| 5375 | * the request or in the response. It is a wrapper fir the |
| 5376 | * 4 following functions. |
| 5377 | */ |
| 5378 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 5379 | struct http_msg *msg, int action) |
| 5380 | { |
| 5381 | size_t name_len; |
| 5382 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5383 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5384 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5385 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5386 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5387 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5388 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5389 | |
Christopher Faulet | 5a8549e | 2019-06-17 13:36:06 +0200 | [diff] [blame] | 5390 | if (IS_HTX_STRM(htxn->s)) { |
| 5391 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5392 | |
| 5393 | htx_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action); |
| 5394 | } |
| 5395 | else |
| 5396 | http_transform_header_str(htxn->s, msg, name, name_len, value, re, action); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5397 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5398 | return 0; |
| 5399 | } |
| 5400 | |
| 5401 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5402 | { |
| 5403 | struct hlua_txn *htxn; |
| 5404 | |
| 5405 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5406 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5407 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5408 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5409 | WILL_LJMP(lua_error(L)); |
| 5410 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5411 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 5412 | } |
| 5413 | |
| 5414 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5415 | { |
| 5416 | struct hlua_txn *htxn; |
| 5417 | |
| 5418 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5419 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5420 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5421 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5422 | WILL_LJMP(lua_error(L)); |
| 5423 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5424 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 5425 | } |
| 5426 | |
| 5427 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5428 | { |
| 5429 | struct hlua_txn *htxn; |
| 5430 | |
| 5431 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5432 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5433 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5434 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5435 | WILL_LJMP(lua_error(L)); |
| 5436 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5437 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 5438 | } |
| 5439 | |
| 5440 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5441 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5442 | struct hlua_txn *htxn; |
| 5443 | |
| 5444 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5445 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5446 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5447 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5448 | WILL_LJMP(lua_error(L)); |
| 5449 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 5450 | 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] | 5451 | } |
| 5452 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5453 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5454 | * It is a wrapper for the 2 following functions. |
| 5455 | */ |
| 5456 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5457 | { |
| 5458 | size_t len; |
| 5459 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5460 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5461 | if (IS_HTX_STRM(htxn->s)) { |
| 5462 | /* HTX version */ |
| 5463 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5464 | struct http_hdr_ctx ctx; |
| 5465 | |
| 5466 | ctx.blk = NULL; |
| 5467 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5468 | http_remove_header(htx, &ctx); |
| 5469 | } |
| 5470 | else { |
| 5471 | /* Legacy HTTP version */ |
| 5472 | struct hdr_ctx ctx; |
| 5473 | struct http_txn *txn = htxn->s->txn; |
| 5474 | |
| 5475 | ctx.idx = 0; |
| 5476 | while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) |
| 5477 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5478 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5479 | return 0; |
| 5480 | } |
| 5481 | |
| 5482 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5483 | { |
| 5484 | struct hlua_txn *htxn; |
| 5485 | |
| 5486 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5487 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5488 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5489 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5490 | WILL_LJMP(lua_error(L)); |
| 5491 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5492 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5493 | } |
| 5494 | |
| 5495 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5496 | { |
| 5497 | struct hlua_txn *htxn; |
| 5498 | |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5499 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5500 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5501 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5502 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5503 | WILL_LJMP(lua_error(L)); |
| 5504 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5505 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5506 | } |
| 5507 | |
| 5508 | /* This function adds an header. It is a wrapper used by |
| 5509 | * the 2 following functions. |
| 5510 | */ |
| 5511 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5512 | { |
| 5513 | size_t name_len; |
| 5514 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5515 | size_t value_len; |
| 5516 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 5517 | char *p; |
| 5518 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5519 | if (IS_HTX_STRM(htxn->s)) { |
| 5520 | /* HTX version */ |
| 5521 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5522 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5523 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5524 | ist2(value, value_len))); |
| 5525 | } |
| 5526 | else { |
| 5527 | /* Legacy HTTP version */ |
| 5528 | /* Check length. */ |
| 5529 | trash.data = value_len + name_len + 2; |
| 5530 | if (trash.data > trash.size) |
| 5531 | return 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5532 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5533 | /* Creates the header string. */ |
| 5534 | p = trash.area; |
| 5535 | memcpy(p, name, name_len); |
| 5536 | p += name_len; |
| 5537 | *p = ':'; |
| 5538 | p++; |
| 5539 | *p = ' '; |
| 5540 | p++; |
| 5541 | memcpy(p, value, value_len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5542 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5543 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
| 5544 | trash.area, trash.data) != 0); |
| 5545 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5546 | return 0; |
| 5547 | } |
| 5548 | |
| 5549 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5550 | { |
| 5551 | struct hlua_txn *htxn; |
| 5552 | |
| 5553 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5554 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5555 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5556 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5557 | WILL_LJMP(lua_error(L)); |
| 5558 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5559 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5560 | } |
| 5561 | |
| 5562 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5563 | { |
| 5564 | struct hlua_txn *htxn; |
| 5565 | |
| 5566 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5567 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5568 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5569 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5570 | WILL_LJMP(lua_error(L)); |
| 5571 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5572 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5573 | } |
| 5574 | |
| 5575 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5576 | { |
| 5577 | struct hlua_txn *htxn; |
| 5578 | |
| 5579 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5580 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5581 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5582 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5583 | WILL_LJMP(lua_error(L)); |
| 5584 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5585 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 5586 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5587 | } |
| 5588 | |
| 5589 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5590 | { |
| 5591 | struct hlua_txn *htxn; |
| 5592 | |
| 5593 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5594 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5595 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5596 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5597 | WILL_LJMP(lua_error(L)); |
| 5598 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5599 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 5600 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5601 | } |
| 5602 | |
| 5603 | /* This function set the method. */ |
| 5604 | static int hlua_http_req_set_meth(lua_State *L) |
| 5605 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5606 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5607 | size_t name_len; |
| 5608 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5609 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5610 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5611 | WILL_LJMP(lua_error(L)); |
| 5612 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5613 | 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] | 5614 | return 1; |
| 5615 | } |
| 5616 | |
| 5617 | /* This function set the method. */ |
| 5618 | static int hlua_http_req_set_path(lua_State *L) |
| 5619 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5620 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5621 | size_t name_len; |
| 5622 | 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] | 5623 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5624 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5625 | WILL_LJMP(lua_error(L)); |
| 5626 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5627 | 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] | 5628 | return 1; |
| 5629 | } |
| 5630 | |
| 5631 | /* This function set the query-string. */ |
| 5632 | static int hlua_http_req_set_query(lua_State *L) |
| 5633 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5634 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5635 | size_t name_len; |
| 5636 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5637 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5638 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5639 | WILL_LJMP(lua_error(L)); |
| 5640 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5641 | /* Check length. */ |
| 5642 | if (name_len > trash.size - 1) { |
| 5643 | lua_pushboolean(L, 0); |
| 5644 | return 1; |
| 5645 | } |
| 5646 | |
| 5647 | /* Add the mark question as prefix. */ |
| 5648 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5649 | trash.area[trash.data++] = '?'; |
| 5650 | memcpy(trash.area + trash.data, name, name_len); |
| 5651 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5652 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5653 | lua_pushboolean(L, |
| 5654 | 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] | 5655 | return 1; |
| 5656 | } |
| 5657 | |
| 5658 | /* This function set the uri. */ |
| 5659 | static int hlua_http_req_set_uri(lua_State *L) |
| 5660 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5661 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5662 | size_t name_len; |
| 5663 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5664 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5665 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5666 | WILL_LJMP(lua_error(L)); |
| 5667 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5668 | 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] | 5669 | return 1; |
| 5670 | } |
| 5671 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5672 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5673 | static int hlua_http_res_set_status(lua_State *L) |
| 5674 | { |
| 5675 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5676 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5677 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5678 | |
Christopher Faulet | 2351ca2 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5679 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | dc2ee27 | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5680 | WILL_LJMP(lua_error(L)); |
| 5681 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5682 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5683 | return 0; |
| 5684 | } |
| 5685 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5686 | /* |
| 5687 | * |
| 5688 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5689 | * Class TXN |
| 5690 | * |
| 5691 | * |
| 5692 | */ |
| 5693 | |
| 5694 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5695 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5696 | */ |
| 5697 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5698 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5699 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5700 | } |
| 5701 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5702 | __LJMP static int hlua_set_var(lua_State *L) |
| 5703 | { |
| 5704 | struct hlua_txn *htxn; |
| 5705 | const char *name; |
| 5706 | size_t len; |
| 5707 | struct sample smp; |
| 5708 | |
| 5709 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5710 | |
| 5711 | /* It is useles to retrieve the stream, but this function |
| 5712 | * runs only in a stream context. |
| 5713 | */ |
| 5714 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5715 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5716 | |
| 5717 | /* Converts the third argument in a sample. */ |
| 5718 | hlua_lua2smp(L, 3, &smp); |
| 5719 | |
| 5720 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5721 | 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] | 5722 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5723 | return 0; |
| 5724 | } |
| 5725 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5726 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5727 | { |
| 5728 | struct hlua_txn *htxn; |
| 5729 | const char *name; |
| 5730 | size_t len; |
| 5731 | struct sample smp; |
| 5732 | |
| 5733 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5734 | |
| 5735 | /* It is useles to retrieve the stream, but this function |
| 5736 | * runs only in a stream context. |
| 5737 | */ |
| 5738 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5739 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5740 | |
| 5741 | /* Unset the variable. */ |
| 5742 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5743 | vars_unset_by_name(name, len, &smp); |
| 5744 | return 0; |
| 5745 | } |
| 5746 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5747 | __LJMP static int hlua_get_var(lua_State *L) |
| 5748 | { |
| 5749 | struct hlua_txn *htxn; |
| 5750 | const char *name; |
| 5751 | size_t len; |
| 5752 | struct sample smp; |
| 5753 | |
| 5754 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5755 | |
| 5756 | /* It is useles to retrieve the stream, but this function |
| 5757 | * runs only in a stream context. |
| 5758 | */ |
| 5759 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5760 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5761 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5762 | 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] | 5763 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5764 | lua_pushnil(L); |
| 5765 | return 1; |
| 5766 | } |
| 5767 | |
| 5768 | return hlua_smp2lua(L, &smp); |
| 5769 | } |
| 5770 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5771 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5772 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5773 | struct hlua *hlua; |
| 5774 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5775 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5776 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5777 | /* It is useles to retrieve the stream, but this function |
| 5778 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5779 | */ |
| 5780 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5781 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5782 | |
| 5783 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5784 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5785 | |
| 5786 | /* Get and store new value. */ |
| 5787 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5788 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5789 | |
| 5790 | return 0; |
| 5791 | } |
| 5792 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5793 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5794 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5795 | struct hlua *hlua; |
| 5796 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5797 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5798 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5799 | /* It is useles to retrieve the stream, but this function |
| 5800 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5801 | */ |
| 5802 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5803 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5804 | |
| 5805 | /* Push configuration index in the stack. */ |
| 5806 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5807 | |
| 5808 | return 1; |
| 5809 | } |
| 5810 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5811 | /* Create stack entry containing a class TXN. This function |
| 5812 | * return 0 if the stack does not contains free slots, |
| 5813 | * otherwise it returns 1. |
| 5814 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5815 | 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] | 5816 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5817 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5818 | |
| 5819 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5820 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5821 | return 0; |
| 5822 | |
| 5823 | /* NOTE: The allocation never fails. The failure |
| 5824 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5825 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5826 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5827 | /* Create the object: obj[0] = userdata. */ |
| 5828 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5829 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5830 | lua_rawseti(L, -2, 0); |
| 5831 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5832 | htxn->s = s; |
| 5833 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5834 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5835 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5836 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5837 | /* Create the "f" field that contains a list of fetches. */ |
| 5838 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5839 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5840 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5841 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5842 | |
| 5843 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5844 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5845 | 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] | 5846 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5847 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5848 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5849 | /* Create the "c" field that contains a list of converters. */ |
| 5850 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5851 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5852 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5853 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5854 | |
| 5855 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5856 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5857 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5858 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5859 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5860 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5861 | /* Create the "req" field that contains the request channel object. */ |
| 5862 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5863 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5864 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5865 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5866 | |
| 5867 | /* Create the "res" field that contains the response channel object. */ |
| 5868 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5869 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5870 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5871 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5872 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5873 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5874 | lua_pushstring(L, "http"); |
| 5875 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5876 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5877 | return 0; |
| 5878 | } |
| 5879 | else |
| 5880 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5881 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5882 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5883 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5884 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5885 | lua_setmetatable(L, -2); |
| 5886 | |
| 5887 | return 1; |
| 5888 | } |
| 5889 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5890 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5891 | { |
| 5892 | const char *msg; |
| 5893 | struct hlua_txn *htxn; |
| 5894 | |
| 5895 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5896 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5897 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5898 | |
| 5899 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5900 | return 0; |
| 5901 | } |
| 5902 | |
| 5903 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5904 | { |
| 5905 | int level; |
| 5906 | const char *msg; |
| 5907 | struct hlua_txn *htxn; |
| 5908 | |
| 5909 | MAY_LJMP(check_args(L, 3, "log")); |
| 5910 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5911 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5912 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5913 | |
| 5914 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5915 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5916 | |
| 5917 | hlua_sendlog(htxn->s->be, level, msg); |
| 5918 | return 0; |
| 5919 | } |
| 5920 | |
| 5921 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5922 | { |
| 5923 | const char *msg; |
| 5924 | struct hlua_txn *htxn; |
| 5925 | |
| 5926 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5927 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5928 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5929 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5930 | return 0; |
| 5931 | } |
| 5932 | |
| 5933 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5934 | { |
| 5935 | const char *msg; |
| 5936 | struct hlua_txn *htxn; |
| 5937 | |
| 5938 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5939 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5940 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5941 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5942 | return 0; |
| 5943 | } |
| 5944 | |
| 5945 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5946 | { |
| 5947 | const char *msg; |
| 5948 | struct hlua_txn *htxn; |
| 5949 | |
| 5950 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5951 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5952 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5953 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5954 | return 0; |
| 5955 | } |
| 5956 | |
| 5957 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5958 | { |
| 5959 | const char *msg; |
| 5960 | struct hlua_txn *htxn; |
| 5961 | |
| 5962 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5963 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5964 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5965 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5966 | return 0; |
| 5967 | } |
| 5968 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5969 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5970 | { |
| 5971 | struct hlua_txn *htxn; |
| 5972 | int ll; |
| 5973 | |
| 5974 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5975 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5976 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5977 | |
| 5978 | if (ll < 0 || ll > 7) |
| 5979 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5980 | |
| 5981 | htxn->s->logs.level = ll; |
| 5982 | return 0; |
| 5983 | } |
| 5984 | |
| 5985 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5986 | { |
| 5987 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5988 | int tos; |
| 5989 | |
| 5990 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5991 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5992 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5993 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5994 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5995 | return 0; |
| 5996 | } |
| 5997 | |
| 5998 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5999 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6000 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6001 | int mark; |
| 6002 | |
| 6003 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 6004 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6005 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6006 | |
Lukas Tribus | b59291a | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 6007 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6008 | return 0; |
| 6009 | } |
| 6010 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 6011 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 6012 | { |
| 6013 | struct hlua_txn *htxn; |
| 6014 | |
| 6015 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 6016 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6017 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6018 | return 0; |
| 6019 | } |
| 6020 | |
| 6021 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 6022 | { |
| 6023 | struct hlua_txn *htxn; |
| 6024 | |
| 6025 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 6026 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6027 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6028 | return 0; |
| 6029 | } |
| 6030 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6031 | /* This function is an Lua binding that send pending data |
| 6032 | * to the client, and close the stream interface. |
| 6033 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6034 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6035 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6036 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6037 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6038 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6039 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6040 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6041 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6042 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6043 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6044 | /* If the flags NOTERM is set, we cannot terminate the http |
| 6045 | * session, so we just end the execution of the current |
| 6046 | * lua code. |
| 6047 | */ |
| 6048 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 6049 | WILL_LJMP(hlua_done(L)); |
| 6050 | return 0; |
| 6051 | } |
| 6052 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6053 | ic = &htxn->s->req; |
| 6054 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6055 | |
Christopher Faulet | 72c6927 | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6056 | if (IS_HTX_STRM(htxn->s)) { |
| 6057 | htxn->s->txn->status = 0; |
| 6058 | http_reply_and_close(htxn->s, 0, NULL); |
| 6059 | ic->analysers &= AN_REQ_FLT_END; |
| 6060 | oc->analysers &= AN_RES_FLT_END; |
| 6061 | } |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6062 | else { |
| 6063 | if (htxn->s->txn) { |
| 6064 | /* HTTP mode, let's stay in sync with the stream */ |
| 6065 | b_del(&ic->buf, htxn->s->txn->req.sov); |
| 6066 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 6067 | htxn->s->txn->req.sov = 0; |
| 6068 | |
| 6069 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 6070 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 6071 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 6072 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6073 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6074 | /* Note that if we want to support keep-alive, we need |
| 6075 | * to bypass the close/shutr_now calls below, but that |
| 6076 | * may only be done if the HTTP request was already |
| 6077 | * processed and the connection header is known (ie |
| 6078 | * not during TCP rules). |
| 6079 | */ |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6080 | } |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6081 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6082 | channel_auto_read(ic); |
| 6083 | channel_abort(ic); |
| 6084 | channel_auto_close(ic); |
| 6085 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6086 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6087 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 6088 | channel_auto_read(oc); |
| 6089 | channel_auto_close(oc); |
| 6090 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6091 | |
Christopher Faulet | b58fb79 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 6092 | ic->analysers = 0; |
| 6093 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6094 | |
Christopher Faulet | 72c6927 | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 6095 | if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 6096 | htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 6097 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6098 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6099 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6100 | return 0; |
| 6101 | } |
| 6102 | |
| 6103 | __LJMP static int hlua_log(lua_State *L) |
| 6104 | { |
| 6105 | int level; |
| 6106 | const char *msg; |
| 6107 | |
| 6108 | MAY_LJMP(check_args(L, 2, "log")); |
| 6109 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6110 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6111 | |
| 6112 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6113 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6114 | |
| 6115 | hlua_sendlog(NULL, level, msg); |
| 6116 | return 0; |
| 6117 | } |
| 6118 | |
| 6119 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6120 | { |
| 6121 | const char *msg; |
| 6122 | |
| 6123 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6124 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6125 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6126 | return 0; |
| 6127 | } |
| 6128 | |
| 6129 | __LJMP static int hlua_log_info(lua_State *L) |
| 6130 | { |
| 6131 | const char *msg; |
| 6132 | |
| 6133 | MAY_LJMP(check_args(L, 1, "info")); |
| 6134 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6135 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6136 | return 0; |
| 6137 | } |
| 6138 | |
| 6139 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6140 | { |
| 6141 | const char *msg; |
| 6142 | |
| 6143 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6144 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6145 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6146 | return 0; |
| 6147 | } |
| 6148 | |
| 6149 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6150 | { |
| 6151 | const char *msg; |
| 6152 | |
| 6153 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6154 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6155 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6156 | return 0; |
| 6157 | } |
| 6158 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6159 | __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] | 6160 | { |
| 6161 | int wakeup_ms = lua_tointeger(L, -1); |
| 6162 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6163 | 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] | 6164 | return 0; |
| 6165 | } |
| 6166 | |
| 6167 | __LJMP static int hlua_sleep(lua_State *L) |
| 6168 | { |
| 6169 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6170 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6171 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6172 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6173 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6174 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6175 | wakeup_ms = tick_add(now_ms, delay); |
| 6176 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6177 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6178 | 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] | 6179 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6180 | } |
| 6181 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6182 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6183 | { |
| 6184 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6185 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6186 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6187 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6188 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6189 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6190 | wakeup_ms = tick_add(now_ms, delay); |
| 6191 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6192 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6193 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6194 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6195 | } |
| 6196 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6197 | /* This functionis an LUA binding. it permits to give back |
| 6198 | * the hand at the HAProxy scheduler. It is used when the |
| 6199 | * LUA processing consumes a lot of time. |
| 6200 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6201 | __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] | 6202 | { |
| 6203 | return 0; |
| 6204 | } |
| 6205 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6206 | __LJMP static int hlua_yield(lua_State *L) |
| 6207 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6208 | 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] | 6209 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6210 | } |
| 6211 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6212 | /* This function change the nice of the currently executed |
| 6213 | * task. It is used set low or high priority at the current |
| 6214 | * task. |
| 6215 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6216 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6217 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6218 | struct hlua *hlua; |
| 6219 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6220 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6221 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6222 | hlua = hlua_gethlua(L); |
| 6223 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6224 | |
| 6225 | /* If he task is not set, I'm in a start mode. */ |
| 6226 | if (!hlua || !hlua->task) |
| 6227 | return 0; |
| 6228 | |
| 6229 | if (nice < -1024) |
| 6230 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6231 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6232 | nice = 1024; |
| 6233 | |
| 6234 | hlua->task->nice = nice; |
| 6235 | return 0; |
| 6236 | } |
| 6237 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6238 | /* 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] | 6239 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6240 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6241 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6242 | * |
| 6243 | * Task wrapper are longjmp safe because the only one Lua code |
| 6244 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6245 | */ |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6246 | 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] | 6247 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6248 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6249 | enum hlua_exec status; |
| 6250 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6251 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6252 | task_set_affinity(task, tid_bit); |
| 6253 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6254 | /* If it is the first call to the task, we must initialize the |
| 6255 | * execution timeouts. |
| 6256 | */ |
| 6257 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6258 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6259 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6260 | /* Execute the Lua code. */ |
| 6261 | status = hlua_ctx_resume(hlua, 1); |
| 6262 | |
| 6263 | switch (status) { |
| 6264 | /* finished or yield */ |
| 6265 | case HLUA_E_OK: |
| 6266 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6267 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6268 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6269 | break; |
| 6270 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6271 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6272 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6273 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6274 | break; |
| 6275 | |
| 6276 | /* finished with error. */ |
| 6277 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6278 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6279 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6280 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6281 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6282 | break; |
| 6283 | |
| 6284 | case HLUA_E_ERR: |
| 6285 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6286 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6287 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6288 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6289 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6290 | break; |
| 6291 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6292 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6293 | } |
| 6294 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6295 | /* This function is an LUA binding that register LUA function to be |
| 6296 | * executed after the HAProxy configuration parsing and before the |
| 6297 | * HAProxy scheduler starts. This function expect only one LUA |
| 6298 | * argument that is a function. This function returns nothing, but |
| 6299 | * throws if an error is encountered. |
| 6300 | */ |
| 6301 | __LJMP static int hlua_register_init(lua_State *L) |
| 6302 | { |
| 6303 | struct hlua_init_function *init; |
| 6304 | int ref; |
| 6305 | |
| 6306 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6307 | |
| 6308 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6309 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6310 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6311 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6312 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6313 | |
| 6314 | init->function_ref = ref; |
| 6315 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6316 | return 0; |
| 6317 | } |
| 6318 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6319 | /* This functio is an LUA binding. It permits to register a task |
| 6320 | * executed in parallel of the main HAroxy activity. The task is |
| 6321 | * created and it is set in the HAProxy scheduler. It can be called |
| 6322 | * from the "init" section, "post init" or during the runtime. |
| 6323 | * |
| 6324 | * Lua prototype: |
| 6325 | * |
| 6326 | * <none> core.register_task(<function>) |
| 6327 | */ |
| 6328 | static int hlua_register_task(lua_State *L) |
| 6329 | { |
| 6330 | struct hlua *hlua; |
| 6331 | struct task *task; |
| 6332 | int ref; |
| 6333 | |
| 6334 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6335 | |
| 6336 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6337 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6338 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6339 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6340 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6341 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6342 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6343 | if (!task) |
| 6344 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6345 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6346 | task->context = hlua; |
| 6347 | task->process = hlua_process_task; |
| 6348 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6349 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6350 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6351 | |
| 6352 | /* Restore the function in the stack. */ |
| 6353 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6354 | hlua->nargs = 0; |
| 6355 | |
| 6356 | /* Schedule task. */ |
| 6357 | task_schedule(task, now_ms); |
| 6358 | |
| 6359 | return 0; |
| 6360 | } |
| 6361 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6362 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6363 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6364 | * resume converters. |
| 6365 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6366 | 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] | 6367 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6368 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6369 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6370 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6371 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6372 | if (!stream) |
| 6373 | return 0; |
| 6374 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6375 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6376 | * Lua context can be not initialized. This behavior |
| 6377 | * permits to save performances because a systematic |
| 6378 | * Lua initialization cause 5% performances loss. |
| 6379 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6380 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6381 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6382 | if (!stream->hlua) { |
| 6383 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6384 | return 0; |
| 6385 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6386 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6387 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6388 | return 0; |
| 6389 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6390 | } |
| 6391 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6392 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6393 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6394 | |
| 6395 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6396 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6397 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6398 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6399 | else |
| 6400 | error = "critical error"; |
| 6401 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6402 | return 0; |
| 6403 | } |
| 6404 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6405 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6406 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6407 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6408 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6409 | return 0; |
| 6410 | } |
| 6411 | |
| 6412 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6413 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6414 | |
| 6415 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6416 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6417 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6418 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6419 | return 0; |
| 6420 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6421 | hlua_smp2lua(stream->hlua->T, smp); |
| 6422 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6423 | |
| 6424 | /* push keywords in the stack. */ |
| 6425 | if (arg_p) { |
| 6426 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6427 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6428 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6429 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6430 | return 0; |
| 6431 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6432 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6433 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6434 | } |
| 6435 | } |
| 6436 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6437 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6438 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6439 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6440 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6441 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6442 | } |
| 6443 | |
| 6444 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6445 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6446 | /* finished. */ |
| 6447 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6448 | /* If the stack is empty, the function fails. */ |
| 6449 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6450 | return 0; |
| 6451 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6452 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6453 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6454 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6455 | return 1; |
| 6456 | |
| 6457 | /* yield. */ |
| 6458 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6459 | 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] | 6460 | return 0; |
| 6461 | |
| 6462 | /* finished with error. */ |
| 6463 | case HLUA_E_ERRMSG: |
| 6464 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6465 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6466 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6467 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6468 | return 0; |
| 6469 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6470 | case HLUA_E_ETMOUT: |
| 6471 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6472 | return 0; |
| 6473 | |
| 6474 | case HLUA_E_NOMEM: |
| 6475 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6476 | return 0; |
| 6477 | |
| 6478 | case HLUA_E_YIELD: |
| 6479 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6480 | return 0; |
| 6481 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6482 | case HLUA_E_ERR: |
| 6483 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6484 | 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] | 6485 | |
| 6486 | default: |
| 6487 | return 0; |
| 6488 | } |
| 6489 | } |
| 6490 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6491 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6492 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6493 | * resume sample-fetches. This function will be called by the sample |
| 6494 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6495 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6496 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6497 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6498 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6499 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6500 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6501 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6502 | const struct buffer msg = { }; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6503 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6504 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6505 | if (!stream) |
| 6506 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6507 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6508 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6509 | * Lua context can be not initialized. This behavior |
| 6510 | * permits to save performances because a systematic |
| 6511 | * Lua initialization cause 5% performances loss. |
| 6512 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6513 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6514 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6515 | if (!stream->hlua) { |
| 6516 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6517 | return 0; |
| 6518 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6519 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6520 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6521 | return 0; |
| 6522 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6523 | } |
| 6524 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6525 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6526 | |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6527 | if (stream->be->mode == PR_MODE_HTTP) { |
| 6528 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
| 6529 | hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 6530 | else |
| 6531 | hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 6532 | } |
| 6533 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6534 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6535 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6536 | |
| 6537 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6538 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6539 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6540 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6541 | else |
| 6542 | error = "critical error"; |
| 6543 | 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] | 6544 | return 0; |
| 6545 | } |
| 6546 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6547 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6548 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6549 | 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] | 6550 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6551 | return 0; |
| 6552 | } |
| 6553 | |
| 6554 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6555 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6556 | |
| 6557 | /* push arguments in the stack. */ |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6558 | if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6559 | 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] | 6560 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6561 | return 0; |
| 6562 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6563 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6564 | |
| 6565 | /* push keywords in the stack. */ |
| 6566 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6567 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6568 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6569 | 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] | 6570 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6571 | return 0; |
| 6572 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6573 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6574 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6575 | } |
| 6576 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6577 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6578 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6579 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6580 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6581 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6582 | } |
| 6583 | |
| 6584 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6585 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6586 | /* finished. */ |
| 6587 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6588 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6589 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6590 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6591 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6592 | /* If the stack is empty, the function fails. */ |
| 6593 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6594 | return 0; |
| 6595 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6596 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6597 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6598 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6599 | |
| 6600 | /* Set the end of execution flag. */ |
| 6601 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6602 | return 1; |
| 6603 | |
| 6604 | /* yield. */ |
| 6605 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6606 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6607 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6608 | 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] | 6609 | return 0; |
| 6610 | |
| 6611 | /* finished with error. */ |
| 6612 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6613 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6614 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6615 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6616 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6617 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6618 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6619 | return 0; |
| 6620 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6621 | case HLUA_E_ETMOUT: |
| 6622 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6623 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6624 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6625 | return 0; |
| 6626 | |
| 6627 | case HLUA_E_NOMEM: |
| 6628 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6629 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6630 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6631 | return 0; |
| 6632 | |
| 6633 | case HLUA_E_YIELD: |
| 6634 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6635 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6636 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6637 | return 0; |
| 6638 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6639 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6640 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6641 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6642 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6643 | 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] | 6644 | |
| 6645 | default: |
| 6646 | return 0; |
| 6647 | } |
| 6648 | } |
| 6649 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6650 | /* This function is an LUA binding used for registering |
| 6651 | * "sample-conv" functions. It expects a converter name used |
| 6652 | * in the haproxy configuration file, and an LUA function. |
| 6653 | */ |
| 6654 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6655 | { |
| 6656 | struct sample_conv_kw_list *sck; |
| 6657 | const char *name; |
| 6658 | int ref; |
| 6659 | int len; |
| 6660 | struct hlua_function *fcn; |
| 6661 | |
| 6662 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6663 | |
| 6664 | /* First argument : converter name. */ |
| 6665 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6666 | |
| 6667 | /* Second argument : lua function. */ |
| 6668 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6669 | |
| 6670 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6671 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6672 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6673 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6674 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6675 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6676 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6677 | |
| 6678 | /* Fill fcn. */ |
| 6679 | fcn->name = strdup(name); |
| 6680 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6681 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6682 | fcn->function_ref = ref; |
| 6683 | |
| 6684 | /* List head */ |
| 6685 | sck->list.n = sck->list.p = NULL; |
| 6686 | |
| 6687 | /* converter keyword. */ |
| 6688 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6689 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6690 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6691 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6692 | |
| 6693 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6694 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6695 | 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] | 6696 | sck->kw[0].val_args = NULL; |
| 6697 | sck->kw[0].in_type = SMP_T_STR; |
| 6698 | sck->kw[0].out_type = SMP_T_STR; |
| 6699 | sck->kw[0].private = fcn; |
| 6700 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6701 | /* Register this new converter */ |
| 6702 | sample_register_convs(sck); |
| 6703 | |
| 6704 | return 0; |
| 6705 | } |
| 6706 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6707 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6708 | * "sample-fetch" functions. It expects a converter name used |
| 6709 | * in the haproxy configuration file, and an LUA function. |
| 6710 | */ |
| 6711 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6712 | { |
| 6713 | const char *name; |
| 6714 | int ref; |
| 6715 | int len; |
| 6716 | struct sample_fetch_kw_list *sfk; |
| 6717 | struct hlua_function *fcn; |
| 6718 | |
| 6719 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6720 | |
| 6721 | /* First argument : sample-fetch name. */ |
| 6722 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6723 | |
| 6724 | /* Second argument : lua function. */ |
| 6725 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6726 | |
| 6727 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6728 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6729 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6730 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6731 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6732 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6733 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6734 | |
| 6735 | /* Fill fcn. */ |
| 6736 | fcn->name = strdup(name); |
| 6737 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6738 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6739 | fcn->function_ref = ref; |
| 6740 | |
| 6741 | /* List head */ |
| 6742 | sfk->list.n = sfk->list.p = NULL; |
| 6743 | |
| 6744 | /* sample-fetch keyword. */ |
| 6745 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6746 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6747 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6748 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6749 | |
| 6750 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6751 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6752 | 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] | 6753 | sfk->kw[0].val_args = NULL; |
| 6754 | sfk->kw[0].out_type = SMP_T_STR; |
| 6755 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6756 | sfk->kw[0].val = 0; |
| 6757 | sfk->kw[0].private = fcn; |
| 6758 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6759 | /* Register this new fetch. */ |
| 6760 | sample_register_fetches(sfk); |
| 6761 | |
| 6762 | return 0; |
| 6763 | } |
| 6764 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6765 | /* This function is a wrapper to execute each LUA function declared |
| 6766 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6767 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6768 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6769 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6770 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6771 | 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] | 6772 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6773 | { |
| 6774 | char **arg; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6775 | unsigned int hflags = 0; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6776 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6777 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6778 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6779 | |
| 6780 | switch (rule->from) { |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6781 | case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break; |
| 6782 | case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break; |
| 6783 | case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break; |
| 6784 | case ACT_F_HTTP_RES: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6785 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6786 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6787 | return ACT_RET_CONT; |
| 6788 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6789 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6790 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6791 | * Lua context can be not initialized. This behavior |
| 6792 | * permits to save performances because a systematic |
| 6793 | * Lua initialization cause 5% performances loss. |
| 6794 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6795 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6796 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6797 | if (!s->hlua) { |
| 6798 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6799 | rule->arg.hlua_rule->fcn.name); |
| 6800 | return ACT_RET_CONT; |
| 6801 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6802 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6803 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6804 | rule->arg.hlua_rule->fcn.name); |
| 6805 | return ACT_RET_CONT; |
| 6806 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6807 | } |
| 6808 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6809 | consistency_set(s, dir, &s->hlua->cons); |
| 6810 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6811 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6812 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6813 | |
| 6814 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6815 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6816 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6817 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6818 | else |
| 6819 | error = "critical error"; |
| 6820 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6821 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6822 | return ACT_RET_CONT; |
| 6823 | } |
| 6824 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6825 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6826 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6827 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6828 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6829 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6830 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6831 | } |
| 6832 | |
| 6833 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6834 | 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] | 6835 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6836 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6837 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6838 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6839 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6840 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6841 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6842 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6843 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6844 | |
| 6845 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6846 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6847 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6848 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6849 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6850 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6851 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6852 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6853 | lua_pushstring(s->hlua->T, *arg); |
| 6854 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6855 | } |
| 6856 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6857 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6858 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6859 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6860 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6861 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6862 | } |
| 6863 | |
| 6864 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6865 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6866 | /* finished. */ |
| 6867 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6868 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6869 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6870 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6871 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6872 | if (s->hlua->flags & HLUA_STOP) |
Christopher Faulet | 4629d08 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 6873 | return ACT_RET_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6874 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6875 | |
| 6876 | /* yield. */ |
| 6877 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6878 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6879 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Christopher Faulet | ea0b0e2 | 2019-08-14 23:19:45 +0200 | [diff] [blame] | 6880 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6881 | s->req.analyse_exp = s->hlua->wake_time; |
Christopher Faulet | ff96b8b | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6882 | else |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6883 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6884 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6885 | /* Some actions can be wake up when a "write" event |
| 6886 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6887 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6888 | */ |
Christopher Faulet | b22f650 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6889 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6890 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6891 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6892 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6893 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6894 | * because HAProxy is not able to manipulate data, it |
| 6895 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6896 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6897 | |
| 6898 | /* finished with error. */ |
| 6899 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6900 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6901 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6902 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6903 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6904 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6905 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6906 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6907 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6908 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6909 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6910 | case HLUA_E_ETMOUT: |
| 6911 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6912 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6913 | return ACT_RET_ERR; |
| 6914 | } |
| 6915 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6916 | return 0; |
| 6917 | |
| 6918 | case HLUA_E_NOMEM: |
| 6919 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6920 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6921 | return ACT_RET_ERR; |
| 6922 | } |
| 6923 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6924 | return 0; |
| 6925 | |
| 6926 | case HLUA_E_YIELD: |
| 6927 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6928 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6929 | return ACT_RET_ERR; |
| 6930 | } |
| 6931 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6932 | rule->arg.hlua_rule->fcn.name); |
| 6933 | return 0; |
| 6934 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6935 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6936 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6937 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6938 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6939 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6940 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6941 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6942 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6943 | |
| 6944 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6945 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6946 | } |
| 6947 | } |
| 6948 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6949 | 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] | 6950 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6951 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6952 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6953 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6954 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6955 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6956 | } |
| 6957 | |
| 6958 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6959 | { |
| 6960 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6961 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6962 | struct task *task; |
| 6963 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6964 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6965 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6966 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6967 | if (!hlua) { |
| 6968 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6969 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6970 | return 0; |
| 6971 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6972 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6973 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6974 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6975 | |
| 6976 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6977 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6978 | if (!task) { |
| 6979 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6980 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6981 | return 0; |
| 6982 | } |
| 6983 | task->nice = 0; |
| 6984 | task->context = ctx; |
| 6985 | task->process = hlua_applet_wakeup; |
| 6986 | ctx->ctx.hlua_apptcp.task = task; |
| 6987 | |
| 6988 | /* In the execution wrappers linked with a stream, the |
| 6989 | * Lua context can be not initialized. This behavior |
| 6990 | * permits to save performances because a systematic |
| 6991 | * Lua initialization cause 5% performances loss. |
| 6992 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6993 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6994 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6995 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6996 | return 0; |
| 6997 | } |
| 6998 | |
| 6999 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7000 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7001 | |
| 7002 | /* The following Lua calls can fail. */ |
| 7003 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7004 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7005 | error = lua_tostring(hlua->T, -1); |
| 7006 | else |
| 7007 | error = "critical error"; |
| 7008 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7009 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7010 | return 0; |
| 7011 | } |
| 7012 | |
| 7013 | /* Check stack available size. */ |
| 7014 | if (!lua_checkstack(hlua->T, 1)) { |
| 7015 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7016 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7017 | RESET_SAFE_LJMP(hlua->T); |
| 7018 | return 0; |
| 7019 | } |
| 7020 | |
| 7021 | /* Restore the function in the stack. */ |
| 7022 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7023 | |
| 7024 | /* Create and and push object stream in the stack. */ |
| 7025 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7026 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7027 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7028 | RESET_SAFE_LJMP(hlua->T); |
| 7029 | return 0; |
| 7030 | } |
| 7031 | hlua->nargs = 1; |
| 7032 | |
| 7033 | /* push keywords in the stack. */ |
| 7034 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7035 | if (!lua_checkstack(hlua->T, 1)) { |
| 7036 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7037 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7038 | RESET_SAFE_LJMP(hlua->T); |
| 7039 | return 0; |
| 7040 | } |
| 7041 | lua_pushstring(hlua->T, *arg); |
| 7042 | hlua->nargs++; |
| 7043 | } |
| 7044 | |
| 7045 | RESET_SAFE_LJMP(hlua->T); |
| 7046 | |
| 7047 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7048 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7049 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7050 | |
| 7051 | return 1; |
| 7052 | } |
| 7053 | |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7054 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7055 | { |
| 7056 | struct stream_interface *si = ctx->owner; |
| 7057 | struct stream *strm = si_strm(si); |
| 7058 | struct channel *res = si_ic(si); |
| 7059 | struct act_rule *rule = ctx->rule; |
| 7060 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7061 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7062 | |
| 7063 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7064 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7065 | /* eat the whole request */ |
| 7066 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7067 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7068 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7069 | |
| 7070 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7071 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7072 | return; |
| 7073 | |
| 7074 | /* Execute the function. */ |
| 7075 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7076 | /* finished. */ |
| 7077 | case HLUA_E_OK: |
| 7078 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7079 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7080 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7081 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7082 | res->flags |= CF_READ_NULL; |
| 7083 | si_shutr(si); |
| 7084 | return; |
| 7085 | |
| 7086 | /* yield. */ |
| 7087 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7088 | if (hlua->wake_time != TICK_ETERNITY) |
| 7089 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7090 | return; |
| 7091 | |
| 7092 | /* finished with error. */ |
| 7093 | case HLUA_E_ERRMSG: |
| 7094 | /* Display log. */ |
| 7095 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7096 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7097 | lua_pop(hlua->T, 1); |
| 7098 | goto error; |
| 7099 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7100 | case HLUA_E_ETMOUT: |
| 7101 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 7102 | rule->arg.hlua_rule->fcn.name); |
| 7103 | goto error; |
| 7104 | |
| 7105 | case HLUA_E_NOMEM: |
| 7106 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 7107 | rule->arg.hlua_rule->fcn.name); |
| 7108 | goto error; |
| 7109 | |
| 7110 | case HLUA_E_YIELD: /* unexpected */ |
| 7111 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 7112 | rule->arg.hlua_rule->fcn.name); |
| 7113 | goto error; |
| 7114 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7115 | case HLUA_E_ERR: |
| 7116 | /* Display log. */ |
| 7117 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 7118 | rule->arg.hlua_rule->fcn.name); |
| 7119 | goto error; |
| 7120 | |
| 7121 | default: |
| 7122 | goto error; |
| 7123 | } |
| 7124 | |
| 7125 | error: |
| 7126 | |
| 7127 | /* For all other cases, just close the stream. */ |
| 7128 | si_shutw(si); |
| 7129 | si_shutr(si); |
| 7130 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7131 | } |
| 7132 | |
| 7133 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7134 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7135 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7136 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7137 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7138 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7139 | } |
| 7140 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7141 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7142 | * an errors occurs and -1 if more data are required for initializing |
| 7143 | * the applet. |
| 7144 | */ |
| 7145 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7146 | { |
| 7147 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7148 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7149 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7150 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7151 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7152 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7153 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7154 | txn = strm->txn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7155 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7156 | /* We want two things in HTTP mode : |
| 7157 | * - enforce server-close mode if we were in keep-alive, so that the |
| 7158 | * applet is released after each response ; |
| 7159 | * - enable request body transfer to the applet in order to resync |
| 7160 | * with the response body. |
| 7161 | */ |
| 7162 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 7163 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7164 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7165 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7166 | if (!hlua) { |
| 7167 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7168 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7169 | return 0; |
| 7170 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7171 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7172 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7173 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7174 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7175 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7176 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7177 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7178 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7179 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7180 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7181 | if (!task) { |
| 7182 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7183 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7184 | return 0; |
| 7185 | } |
| 7186 | task->nice = 0; |
| 7187 | task->context = ctx; |
| 7188 | task->process = hlua_applet_wakeup; |
| 7189 | ctx->ctx.hlua_apphttp.task = task; |
| 7190 | |
| 7191 | /* In the execution wrappers linked with a stream, the |
| 7192 | * Lua context can be not initialized. This behavior |
| 7193 | * permits to save performances because a systematic |
| 7194 | * Lua initialization cause 5% performances loss. |
| 7195 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7196 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7197 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 7198 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7199 | return 0; |
| 7200 | } |
| 7201 | |
| 7202 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7203 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7204 | |
| 7205 | /* The following Lua calls can fail. */ |
| 7206 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7207 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7208 | error = lua_tostring(hlua->T, -1); |
| 7209 | else |
| 7210 | error = "critical error"; |
| 7211 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7212 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7213 | return 0; |
| 7214 | } |
| 7215 | |
| 7216 | /* Check stack available size. */ |
| 7217 | if (!lua_checkstack(hlua->T, 1)) { |
| 7218 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7219 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7220 | RESET_SAFE_LJMP(hlua->T); |
| 7221 | return 0; |
| 7222 | } |
| 7223 | |
| 7224 | /* Restore the function in the stack. */ |
| 7225 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7226 | |
| 7227 | /* Create and and push object stream in the stack. */ |
| 7228 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7229 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7230 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7231 | RESET_SAFE_LJMP(hlua->T); |
| 7232 | return 0; |
| 7233 | } |
| 7234 | hlua->nargs = 1; |
| 7235 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7236 | /* push keywords in the stack. */ |
| 7237 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7238 | if (!lua_checkstack(hlua->T, 1)) { |
| 7239 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7240 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7241 | RESET_SAFE_LJMP(hlua->T); |
| 7242 | return 0; |
| 7243 | } |
| 7244 | lua_pushstring(hlua->T, *arg); |
| 7245 | hlua->nargs++; |
| 7246 | } |
| 7247 | |
| 7248 | RESET_SAFE_LJMP(hlua->T); |
| 7249 | |
| 7250 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7251 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7252 | |
| 7253 | return 1; |
| 7254 | } |
| 7255 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7256 | static void hlua_applet_htx_fct(struct appctx *ctx) |
| 7257 | { |
| 7258 | struct stream_interface *si = ctx->owner; |
| 7259 | struct stream *strm = si_strm(si); |
| 7260 | struct channel *req = si_oc(si); |
| 7261 | struct channel *res = si_ic(si); |
| 7262 | struct act_rule *rule = ctx->rule; |
| 7263 | struct proxy *px = strm->be; |
| 7264 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7265 | struct htx *req_htx, *res_htx; |
| 7266 | |
| 7267 | res_htx = htx_from_buf(&res->buf); |
| 7268 | |
| 7269 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7270 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7271 | goto out; |
| 7272 | |
| 7273 | /* Check if the input buffer is avalaible. */ |
| 7274 | if (!b_size(&res->buf)) { |
| 7275 | si_rx_room_blk(si); |
| 7276 | goto out; |
| 7277 | } |
| 7278 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7279 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7280 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7281 | |
| 7282 | /* Set the currently running flag. */ |
| 7283 | if (!HLUA_IS_RUNNING(hlua) && |
| 7284 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7285 | struct htx_blk *blk; |
| 7286 | size_t count = co_data(req); |
| 7287 | |
| 7288 | if (!count) { |
| 7289 | si_cant_get(si); |
| 7290 | goto out; |
| 7291 | } |
| 7292 | |
| 7293 | /* We need to flush the request header. This left the body for |
| 7294 | * the Lua. |
| 7295 | */ |
| 7296 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7297 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7298 | while (count && blk) { |
| 7299 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7300 | uint32_t sz = htx_get_blksz(blk); |
| 7301 | |
| 7302 | if (sz > count) { |
| 7303 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7304 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7305 | goto out; |
| 7306 | } |
| 7307 | |
| 7308 | count -= sz; |
| 7309 | co_set_data(req, co_data(req) - sz); |
| 7310 | blk = htx_remove_blk(req_htx, blk); |
| 7311 | |
| 7312 | if (type == HTX_BLK_EOH) |
| 7313 | break; |
| 7314 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7315 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7316 | } |
| 7317 | |
| 7318 | /* Executes The applet if it is not done. */ |
| 7319 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7320 | |
| 7321 | /* Execute the function. */ |
| 7322 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7323 | /* finished. */ |
| 7324 | case HLUA_E_OK: |
| 7325 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7326 | break; |
| 7327 | |
| 7328 | /* yield. */ |
| 7329 | case HLUA_E_AGAIN: |
| 7330 | if (hlua->wake_time != TICK_ETERNITY) |
| 7331 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7332 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7333 | |
| 7334 | /* finished with error. */ |
| 7335 | case HLUA_E_ERRMSG: |
| 7336 | /* Display log. */ |
| 7337 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7338 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7339 | lua_pop(hlua->T, 1); |
| 7340 | goto error; |
| 7341 | |
| 7342 | case HLUA_E_ETMOUT: |
| 7343 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7344 | rule->arg.hlua_rule->fcn.name); |
| 7345 | goto error; |
| 7346 | |
| 7347 | case HLUA_E_NOMEM: |
| 7348 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7349 | rule->arg.hlua_rule->fcn.name); |
| 7350 | goto error; |
| 7351 | |
| 7352 | case HLUA_E_YIELD: /* unexpected */ |
| 7353 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7354 | rule->arg.hlua_rule->fcn.name); |
| 7355 | goto error; |
| 7356 | |
| 7357 | case HLUA_E_ERR: |
| 7358 | /* Display log. */ |
| 7359 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7360 | rule->arg.hlua_rule->fcn.name); |
| 7361 | goto error; |
| 7362 | |
| 7363 | default: |
| 7364 | goto error; |
| 7365 | } |
| 7366 | } |
| 7367 | |
| 7368 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7369 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7370 | goto done; |
| 7371 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7372 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7373 | goto error; |
| 7374 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7375 | /* Don't add TLR because mux-h1 will take care of it */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7376 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7377 | si_rx_room_blk(si); |
| 7378 | goto out; |
| 7379 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7380 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7381 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7382 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7383 | } |
| 7384 | |
| 7385 | done: |
| 7386 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7387 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7388 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7389 | si_shutr(si); |
| 7390 | } |
| 7391 | |
| 7392 | /* eat the whole request */ |
| 7393 | if (co_data(req)) { |
| 7394 | req_htx = htx_from_buf(&req->buf); |
| 7395 | co_htx_skip(req, req_htx, co_data(req)); |
| 7396 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7397 | } |
| 7398 | } |
| 7399 | |
| 7400 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7401 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7402 | return; |
| 7403 | |
| 7404 | error: |
| 7405 | |
| 7406 | /* If we are in HTTP mode, and we are not send any |
| 7407 | * data, return a 500 server error in best effort: |
| 7408 | * if there is no room available in the buffer, |
| 7409 | * just close the connection. |
| 7410 | */ |
| 7411 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7412 | struct buffer *err = &htx_err_chunks[HTTP_ERR_500]; |
| 7413 | |
| 7414 | channel_erase(res); |
| 7415 | res->buf.data = b_data(err); |
| 7416 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7417 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7418 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7419 | } |
| 7420 | if (!(strm->flags & SF_ERR_MASK)) |
| 7421 | strm->flags |= SF_ERR_RESOURCE; |
| 7422 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7423 | goto done; |
| 7424 | } |
| 7425 | |
Willy Tarreau | 83a5ff4 | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7426 | void hlua_applet_http_fct(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7427 | { |
| 7428 | struct stream_interface *si = ctx->owner; |
| 7429 | struct stream *strm = si_strm(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7430 | struct channel *req = si_oc(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7431 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7432 | struct act_rule *rule = ctx->rule; |
| 7433 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7434 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7435 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7436 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7437 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7438 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7439 | int ret; |
| 7440 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7441 | if (IS_HTX_STRM(strm)) |
| 7442 | return hlua_applet_htx_fct(ctx); |
| 7443 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7444 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7445 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7446 | goto out; |
| 7447 | |
| 7448 | /* Check if the input buffer is avalaible. */ |
| 7449 | if (!b_size(&res->buf)) { |
| 7450 | si_rx_room_blk(si); |
| 7451 | goto out; |
| 7452 | } |
| 7453 | /* check that the output is not closed */ |
| 7454 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
| 7455 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7456 | |
| 7457 | /* Set the currently running flag. */ |
| 7458 | if (!HLUA_IS_RUNNING(hlua) && |
| 7459 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7460 | /* Store the max amount of bytes that we can read. */ |
| 7461 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 7462 | |
| 7463 | /* We need to flush the request header. This left the body |
| 7464 | * for the Lua. |
| 7465 | */ |
| 7466 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7467 | /* Read the maximum amount of data available. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7468 | ret = co_getblk_nc(req, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7469 | if (ret == -1) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7470 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7471 | |
| 7472 | /* No data available, ask for more data. */ |
| 7473 | if (ret == 1) |
| 7474 | len2 = 0; |
| 7475 | if (ret == 0) |
| 7476 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 7477 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7478 | si_cant_get(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7479 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7480 | } |
| 7481 | |
| 7482 | /* skip the requests bytes. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7483 | co_skip(req, strm->txn->req.eoh + strm->txn->req.eol); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7484 | } |
| 7485 | |
| 7486 | /* Executes The applet if it is not done. */ |
| 7487 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7488 | |
| 7489 | /* Execute the function. */ |
| 7490 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7491 | /* finished. */ |
| 7492 | case HLUA_E_OK: |
| 7493 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7494 | break; |
| 7495 | |
| 7496 | /* yield. */ |
| 7497 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7498 | if (hlua->wake_time != TICK_ETERNITY) |
| 7499 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7500 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7501 | |
| 7502 | /* finished with error. */ |
| 7503 | case HLUA_E_ERRMSG: |
| 7504 | /* Display log. */ |
| 7505 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7506 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7507 | lua_pop(hlua->T, 1); |
| 7508 | goto error; |
| 7509 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7510 | case HLUA_E_ETMOUT: |
| 7511 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7512 | rule->arg.hlua_rule->fcn.name); |
| 7513 | goto error; |
| 7514 | |
| 7515 | case HLUA_E_NOMEM: |
| 7516 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7517 | rule->arg.hlua_rule->fcn.name); |
| 7518 | goto error; |
| 7519 | |
| 7520 | case HLUA_E_YIELD: /* unexpected */ |
| 7521 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7522 | rule->arg.hlua_rule->fcn.name); |
| 7523 | goto error; |
| 7524 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7525 | case HLUA_E_ERR: |
| 7526 | /* Display log. */ |
| 7527 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7528 | rule->arg.hlua_rule->fcn.name); |
| 7529 | goto error; |
| 7530 | |
| 7531 | default: |
| 7532 | goto error; |
| 7533 | } |
| 7534 | } |
| 7535 | |
| 7536 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7537 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7538 | goto done; |
| 7539 | |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 7540 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7541 | goto error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7542 | |
| 7543 | /* We must send the final chunk. */ |
| 7544 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 7545 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 7546 | |
| 7547 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 7548 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7549 | |
| 7550 | /* critical error. */ |
| 7551 | if (ret == -2 || ret == -3) { |
| 7552 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 7553 | rule->arg.hlua_rule->fcn.name); |
| 7554 | goto error; |
| 7555 | } |
| 7556 | |
| 7557 | /* no enough space error. */ |
| 7558 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7559 | si_rx_room_blk(si); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7560 | goto out; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7561 | } |
| 7562 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7563 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7564 | ctx->ctx.hlua_apphttp.flags |= (APPLET_LAST_CHK|APPLET_RSP_SENT); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7565 | } |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7566 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7567 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7568 | done: |
| 7569 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 7570 | if (!(res->flags & CF_SHUTR)) { |
| 7571 | res->flags |= CF_READ_NULL; |
| 7572 | si_shutr(si); |
| 7573 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7574 | |
| 7575 | /* eat the whole request */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7576 | if (co_data(req)) |
| 7577 | co_skip(req, co_data(req)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7578 | } |
| 7579 | |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7580 | out: |
| 7581 | return; |
| 7582 | |
| 7583 | error: |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7584 | |
| 7585 | /* If we are in HTTP mode, and we are not send any |
| 7586 | * data, return a 500 server error in best effort: |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7587 | * if there is no room available in the buffer, |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7588 | * just close the connection. |
| 7589 | */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7590 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7591 | channel_erase(res); |
| 7592 | ci_putblk(res, error_500, strlen(error_500)); |
| 7593 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7594 | if (!(strm->flags & SF_ERR_MASK)) |
| 7595 | strm->flags |= SF_ERR_RESOURCE; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7596 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7597 | goto done; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7598 | } |
| 7599 | |
| 7600 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7601 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7602 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7603 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7604 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7605 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7606 | } |
| 7607 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7608 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 7609 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7610 | * |
| 7611 | * This function can fail with an abort() due to an Lua critical error. |
| 7612 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7613 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7614 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7615 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7616 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7617 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7618 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7619 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7620 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7621 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7622 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7623 | if (!rule->arg.hlua_rule) { |
| 7624 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7625 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7626 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7627 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7628 | /* Memory for arguments. */ |
| 7629 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7630 | if (!rule->arg.hlua_rule->args) { |
| 7631 | memprintf(err, "out of memory error"); |
| 7632 | return ACT_RET_PRS_ERR; |
| 7633 | } |
| 7634 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7635 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7636 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7637 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7638 | /* Expect some arguments */ |
| 7639 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7640 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7641 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7642 | return ACT_RET_PRS_ERR; |
| 7643 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7644 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7645 | if (!rule->arg.hlua_rule->args[i]) { |
| 7646 | memprintf(err, "out of memory error"); |
| 7647 | return ACT_RET_PRS_ERR; |
| 7648 | } |
| 7649 | (*cur_arg)++; |
| 7650 | } |
| 7651 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7652 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7653 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7654 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7655 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7656 | } |
| 7657 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7658 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7659 | struct act_rule *rule, char **err) |
| 7660 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7661 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7662 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7663 | /* HTTP applets are forbidden in tcp-request rules. |
| 7664 | * HTTP applet request requires everything initilized by |
| 7665 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 7666 | * The applet will be immediately initilized, but its before |
| 7667 | * the call of this analyzer. |
| 7668 | */ |
| 7669 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7670 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7671 | return ACT_RET_PRS_ERR; |
| 7672 | } |
| 7673 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7674 | /* Memory for the rule. */ |
| 7675 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7676 | if (!rule->arg.hlua_rule) { |
| 7677 | memprintf(err, "out of memory error"); |
| 7678 | return ACT_RET_PRS_ERR; |
| 7679 | } |
| 7680 | |
| 7681 | /* Reference the Lua function and store the reference. */ |
| 7682 | rule->arg.hlua_rule->fcn = *fcn; |
| 7683 | |
| 7684 | /* TODO: later accept arguments. */ |
| 7685 | rule->arg.hlua_rule->args = NULL; |
| 7686 | |
| 7687 | /* Add applet pointer in the rule. */ |
| 7688 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7689 | rule->applet.name = fcn->name; |
| 7690 | rule->applet.init = hlua_applet_http_init; |
| 7691 | rule->applet.fct = hlua_applet_http_fct; |
| 7692 | rule->applet.release = hlua_applet_http_release; |
| 7693 | rule->applet.timeout = hlua_timeout_applet; |
| 7694 | |
| 7695 | return ACT_RET_PRS_OK; |
| 7696 | } |
| 7697 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7698 | /* This function is an LUA binding used for registering |
| 7699 | * "sample-conv" functions. It expects a converter name used |
| 7700 | * in the haproxy configuration file, and an LUA function. |
| 7701 | */ |
| 7702 | __LJMP static int hlua_register_action(lua_State *L) |
| 7703 | { |
| 7704 | struct action_kw_list *akl; |
| 7705 | const char *name; |
| 7706 | int ref; |
| 7707 | int len; |
| 7708 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7709 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7710 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7711 | /* Initialise the number of expected arguments at 0. */ |
| 7712 | nargs = 0; |
| 7713 | |
| 7714 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7715 | 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] | 7716 | |
| 7717 | /* First argument : converter name. */ |
| 7718 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7719 | |
| 7720 | /* Second argument : environment. */ |
| 7721 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7722 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7723 | |
| 7724 | /* Third argument : lua function. */ |
| 7725 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7726 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7727 | /* 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] | 7728 | if (lua_gettop(L) >= 4) |
| 7729 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7730 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7731 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7732 | lua_pushnil(L); |
| 7733 | while (lua_next(L, 2) != 0) { |
| 7734 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7735 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7736 | |
| 7737 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7738 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7739 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7740 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7741 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7742 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7743 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7744 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7745 | |
| 7746 | /* Fill fcn. */ |
| 7747 | fcn->name = strdup(name); |
| 7748 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7749 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7750 | fcn->function_ref = ref; |
| 7751 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7752 | /* Set the expected number od arguments. */ |
| 7753 | fcn->nargs = nargs; |
| 7754 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7755 | /* List head */ |
| 7756 | akl->list.n = akl->list.p = NULL; |
| 7757 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7758 | /* action keyword. */ |
| 7759 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7760 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7761 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7762 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7763 | |
| 7764 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7765 | |
| 7766 | akl->kw[0].match_pfx = 0; |
| 7767 | akl->kw[0].private = fcn; |
| 7768 | akl->kw[0].parse = action_register_lua; |
| 7769 | |
| 7770 | /* select the action registering point. */ |
| 7771 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7772 | tcp_req_cont_keywords_register(akl); |
| 7773 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7774 | tcp_res_cont_keywords_register(akl); |
| 7775 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7776 | http_req_keywords_register(akl); |
| 7777 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7778 | http_res_keywords_register(akl); |
| 7779 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7780 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7781 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7782 | "are expected.", lua_tostring(L, -1))); |
| 7783 | |
| 7784 | /* pop the environment string. */ |
| 7785 | lua_pop(L, 1); |
| 7786 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7787 | return ACT_RET_PRS_OK; |
| 7788 | } |
| 7789 | |
| 7790 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7791 | struct act_rule *rule, char **err) |
| 7792 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7793 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7794 | |
Christopher Faulet | eb70980 | 2019-04-11 22:04:08 +0200 | [diff] [blame] | 7795 | if (px->mode == PR_MODE_HTTP && (px->options2 & PR_O2_USE_HTX)) { |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7796 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 7797 | return ACT_RET_PRS_ERR; |
| 7798 | } |
| 7799 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7800 | /* Memory for the rule. */ |
| 7801 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7802 | if (!rule->arg.hlua_rule) { |
| 7803 | memprintf(err, "out of memory error"); |
| 7804 | return ACT_RET_PRS_ERR; |
| 7805 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7806 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7807 | /* Reference the Lua function and store the reference. */ |
| 7808 | rule->arg.hlua_rule->fcn = *fcn; |
| 7809 | |
| 7810 | /* TODO: later accept arguments. */ |
| 7811 | rule->arg.hlua_rule->args = NULL; |
| 7812 | |
| 7813 | /* Add applet pointer in the rule. */ |
| 7814 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7815 | rule->applet.name = fcn->name; |
| 7816 | rule->applet.init = hlua_applet_tcp_init; |
| 7817 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7818 | rule->applet.release = hlua_applet_tcp_release; |
| 7819 | rule->applet.timeout = hlua_timeout_applet; |
| 7820 | |
| 7821 | return 0; |
| 7822 | } |
| 7823 | |
| 7824 | /* This function is an LUA binding used for registering |
| 7825 | * "sample-conv" functions. It expects a converter name used |
| 7826 | * in the haproxy configuration file, and an LUA function. |
| 7827 | */ |
| 7828 | __LJMP static int hlua_register_service(lua_State *L) |
| 7829 | { |
| 7830 | struct action_kw_list *akl; |
| 7831 | const char *name; |
| 7832 | const char *env; |
| 7833 | int ref; |
| 7834 | int len; |
| 7835 | struct hlua_function *fcn; |
| 7836 | |
| 7837 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7838 | |
| 7839 | /* First argument : converter name. */ |
| 7840 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7841 | |
| 7842 | /* Second argument : environment. */ |
| 7843 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7844 | |
| 7845 | /* Third argument : lua function. */ |
| 7846 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7847 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7848 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7849 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7850 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7851 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7852 | fcn = calloc(1, sizeof(*fcn)); |
| 7853 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7854 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7855 | |
| 7856 | /* Fill fcn. */ |
| 7857 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7858 | fcn->name = calloc(1, len); |
| 7859 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7860 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7861 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7862 | fcn->function_ref = ref; |
| 7863 | |
| 7864 | /* List head */ |
| 7865 | akl->list.n = akl->list.p = NULL; |
| 7866 | |
| 7867 | /* converter keyword. */ |
| 7868 | len = strlen("lua.") + strlen(name) + 1; |
| 7869 | akl->kw[0].kw = calloc(1, len); |
| 7870 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7871 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7872 | |
| 7873 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7874 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7875 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7876 | if (strcmp(env, "tcp") == 0) |
| 7877 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7878 | else if (strcmp(env, "http") == 0) |
| 7879 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7880 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7881 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7882 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7883 | |
| 7884 | akl->kw[0].match_pfx = 0; |
| 7885 | akl->kw[0].private = fcn; |
| 7886 | |
| 7887 | /* End of array. */ |
| 7888 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7889 | |
| 7890 | /* Register this new converter */ |
| 7891 | service_keywords_register(akl); |
| 7892 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7893 | return 0; |
| 7894 | } |
| 7895 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7896 | /* This function initialises Lua cli handler. It copies the |
| 7897 | * arguments in the Lua stack and create channel IO objects. |
| 7898 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7899 | 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] | 7900 | { |
| 7901 | struct hlua *hlua; |
| 7902 | struct hlua_function *fcn; |
| 7903 | int i; |
| 7904 | const char *error; |
| 7905 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7906 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7907 | appctx->ctx.hlua_cli.fcn = private; |
| 7908 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7909 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7910 | if (!hlua) { |
| 7911 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7912 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7913 | } |
| 7914 | HLUA_INIT(hlua); |
| 7915 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7916 | |
| 7917 | /* Create task used by signal to wakeup applets. |
| 7918 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7919 | * applet_http. It is absolutely compatible. |
| 7920 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7921 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7922 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7923 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7924 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7925 | } |
| 7926 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7927 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7928 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7929 | |
| 7930 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7931 | 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] | 7932 | 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] | 7933 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7934 | } |
| 7935 | |
| 7936 | /* The following Lua calls can fail. */ |
| 7937 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7938 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7939 | error = lua_tostring(hlua->T, -1); |
| 7940 | else |
| 7941 | error = "critical error"; |
| 7942 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7943 | goto error; |
| 7944 | } |
| 7945 | |
| 7946 | /* Check stack available size. */ |
| 7947 | if (!lua_checkstack(hlua->T, 2)) { |
| 7948 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7949 | goto error; |
| 7950 | } |
| 7951 | |
| 7952 | /* Restore the function in the stack. */ |
| 7953 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7954 | |
| 7955 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7956 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7957 | */ |
| 7958 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7959 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7960 | goto error; |
| 7961 | } |
| 7962 | hlua->nargs = 1; |
| 7963 | |
| 7964 | /* push keywords in the stack. */ |
| 7965 | for (i = 0; *args[i]; i++) { |
| 7966 | /* Check stack available size. */ |
| 7967 | if (!lua_checkstack(hlua->T, 1)) { |
| 7968 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7969 | goto error; |
| 7970 | } |
| 7971 | lua_pushstring(hlua->T, args[i]); |
| 7972 | hlua->nargs++; |
| 7973 | } |
| 7974 | |
| 7975 | /* We must initialize the execution timeouts. */ |
| 7976 | hlua->max_time = hlua_timeout_session; |
| 7977 | |
| 7978 | /* At this point the execution is safe. */ |
| 7979 | RESET_SAFE_LJMP(hlua->T); |
| 7980 | |
| 7981 | /* It's ok */ |
| 7982 | return 0; |
| 7983 | |
| 7984 | /* It's not ok. */ |
| 7985 | error: |
| 7986 | RESET_SAFE_LJMP(hlua->T); |
| 7987 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7988 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7989 | return 1; |
| 7990 | } |
| 7991 | |
| 7992 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7993 | { |
| 7994 | struct hlua *hlua; |
| 7995 | struct stream_interface *si; |
| 7996 | struct hlua_function *fcn; |
| 7997 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7998 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7999 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8000 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8001 | |
| 8002 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8003 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8004 | return 1; |
| 8005 | |
| 8006 | /* Execute the function. */ |
| 8007 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8008 | |
| 8009 | /* finished. */ |
| 8010 | case HLUA_E_OK: |
| 8011 | return 1; |
| 8012 | |
| 8013 | /* yield. */ |
| 8014 | case HLUA_E_AGAIN: |
| 8015 | /* We want write. */ |
| 8016 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8017 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8018 | /* Set the timeout. */ |
| 8019 | if (hlua->wake_time != TICK_ETERNITY) |
| 8020 | task_schedule(hlua->task, hlua->wake_time); |
| 8021 | return 0; |
| 8022 | |
| 8023 | /* finished with error. */ |
| 8024 | case HLUA_E_ERRMSG: |
| 8025 | /* Display log. */ |
| 8026 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8027 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8028 | lua_pop(hlua->T, 1); |
| 8029 | return 1; |
| 8030 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8031 | case HLUA_E_ETMOUT: |
| 8032 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8033 | fcn->name); |
| 8034 | return 1; |
| 8035 | |
| 8036 | case HLUA_E_NOMEM: |
| 8037 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8038 | fcn->name); |
| 8039 | return 1; |
| 8040 | |
| 8041 | case HLUA_E_YIELD: /* unexpected */ |
| 8042 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8043 | fcn->name); |
| 8044 | return 1; |
| 8045 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8046 | case HLUA_E_ERR: |
| 8047 | /* Display log. */ |
| 8048 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8049 | fcn->name); |
| 8050 | return 1; |
| 8051 | |
| 8052 | default: |
| 8053 | return 1; |
| 8054 | } |
| 8055 | |
| 8056 | return 1; |
| 8057 | } |
| 8058 | |
| 8059 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8060 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8061 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8062 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8063 | } |
| 8064 | |
| 8065 | /* This function is an LUA binding used for registering |
| 8066 | * new keywords in the cli. It expects a list of keywords |
| 8067 | * which are the "path". It is limited to 5 keywords. A |
| 8068 | * description of the command, a function to be executed |
| 8069 | * for the parsing and a function for io handlers. |
| 8070 | */ |
| 8071 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8072 | { |
| 8073 | struct cli_kw_list *cli_kws; |
| 8074 | const char *message; |
| 8075 | int ref_io; |
| 8076 | int len; |
| 8077 | struct hlua_function *fcn; |
| 8078 | int index; |
| 8079 | int i; |
| 8080 | |
| 8081 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8082 | |
| 8083 | /* First argument : an array of maximum 5 keywords. */ |
| 8084 | if (!lua_istable(L, 1)) |
| 8085 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8086 | |
| 8087 | /* Second argument : string with contextual message. */ |
| 8088 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8089 | |
| 8090 | /* Third and fourth argument : lua function. */ |
| 8091 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8092 | |
| 8093 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8094 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 8095 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8096 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8097 | fcn = calloc(1, sizeof(*fcn)); |
| 8098 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8099 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8100 | |
| 8101 | /* Fill path. */ |
| 8102 | index = 0; |
| 8103 | lua_pushnil(L); |
| 8104 | while(lua_next(L, 1) != 0) { |
| 8105 | if (index >= 5) |
| 8106 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8107 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8108 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8109 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 8110 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8111 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8112 | index++; |
| 8113 | lua_pop(L, 1); |
| 8114 | } |
| 8115 | |
| 8116 | /* Copy help message. */ |
| 8117 | cli_kws->kw[0].usage = strdup(message); |
| 8118 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8119 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8120 | |
| 8121 | /* Fill fcn io handler. */ |
| 8122 | len = strlen("<lua.cli>") + 1; |
| 8123 | for (i = 0; i < index; i++) |
| 8124 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8125 | fcn->name = calloc(1, len); |
| 8126 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8127 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8128 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8129 | for (i = 0; i < index; i++) { |
| 8130 | strncat((char *)fcn->name, ".", len); |
| 8131 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8132 | } |
| 8133 | strncat((char *)fcn->name, ">", len); |
| 8134 | fcn->function_ref = ref_io; |
| 8135 | |
| 8136 | /* Fill last entries. */ |
| 8137 | cli_kws->kw[0].private = fcn; |
| 8138 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8139 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8140 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8141 | |
| 8142 | /* Register this new converter */ |
| 8143 | cli_register_kw(cli_kws); |
| 8144 | |
| 8145 | return 0; |
| 8146 | } |
| 8147 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8148 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 8149 | struct proxy *defpx, const char *file, int line, |
| 8150 | char **err, unsigned int *timeout) |
| 8151 | { |
| 8152 | const char *error; |
| 8153 | |
| 8154 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 8155 | if (error == PARSE_TIME_OVER) { |
| 8156 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 8157 | args[1], args[0]); |
| 8158 | return -1; |
| 8159 | } |
| 8160 | else if (error == PARSE_TIME_UNDER) { |
| 8161 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 8162 | args[1], args[0]); |
| 8163 | return -1; |
| 8164 | } |
| 8165 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8166 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8167 | return -1; |
| 8168 | } |
| 8169 | return 0; |
| 8170 | } |
| 8171 | |
| 8172 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 8173 | struct proxy *defpx, const char *file, int line, |
| 8174 | char **err) |
| 8175 | { |
| 8176 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8177 | file, line, err, &hlua_timeout_session); |
| 8178 | } |
| 8179 | |
| 8180 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 8181 | struct proxy *defpx, const char *file, int line, |
| 8182 | char **err) |
| 8183 | { |
| 8184 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8185 | file, line, err, &hlua_timeout_task); |
| 8186 | } |
| 8187 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8188 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 8189 | struct proxy *defpx, const char *file, int line, |
| 8190 | char **err) |
| 8191 | { |
| 8192 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8193 | file, line, err, &hlua_timeout_applet); |
| 8194 | } |
| 8195 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8196 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 8197 | struct proxy *defpx, const char *file, int line, |
| 8198 | char **err) |
| 8199 | { |
| 8200 | char *error; |
| 8201 | |
| 8202 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8203 | if (*error != '\0') { |
| 8204 | memprintf(err, "%s: invalid number", args[0]); |
| 8205 | return -1; |
| 8206 | } |
| 8207 | return 0; |
| 8208 | } |
| 8209 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8210 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 8211 | struct proxy *defpx, const char *file, int line, |
| 8212 | char **err) |
| 8213 | { |
| 8214 | char *error; |
| 8215 | |
| 8216 | if (*(args[1]) == 0) { |
| 8217 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8218 | return -1; |
| 8219 | } |
| 8220 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8221 | if (*error != '\0') { |
| 8222 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8223 | return -1; |
| 8224 | } |
| 8225 | return 0; |
| 8226 | } |
| 8227 | |
| 8228 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8229 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8230 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8231 | * the main lua entry point. |
| 8232 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8233 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8234 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8235 | * |
| 8236 | * In some error case, LUA set an error message in top of the stack. This function |
| 8237 | * 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] | 8238 | * |
| 8239 | * This function can fail with an abort() due to an Lua critical error. |
| 8240 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8241 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8242 | */ |
| 8243 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 8244 | struct proxy *defpx, const char *file, int line, |
| 8245 | char **err) |
| 8246 | { |
| 8247 | int error; |
| 8248 | |
| 8249 | /* Just load and compile the file. */ |
| 8250 | error = luaL_loadfile(gL.T, args[1]); |
| 8251 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8252 | 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] | 8253 | lua_pop(gL.T, 1); |
| 8254 | return -1; |
| 8255 | } |
| 8256 | |
| 8257 | /* If no syntax error where detected, execute the code. */ |
| 8258 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 8259 | switch (error) { |
| 8260 | case LUA_OK: |
| 8261 | break; |
| 8262 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8263 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8264 | lua_pop(gL.T, 1); |
| 8265 | return -1; |
| 8266 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8267 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8268 | return -1; |
| 8269 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8270 | 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] | 8271 | lua_pop(gL.T, 1); |
| 8272 | return -1; |
| 8273 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8274 | 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] | 8275 | lua_pop(gL.T, 1); |
| 8276 | return -1; |
| 8277 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8278 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8279 | lua_pop(gL.T, 1); |
| 8280 | return -1; |
| 8281 | } |
| 8282 | |
| 8283 | return 0; |
| 8284 | } |
| 8285 | |
| 8286 | /* configuration keywords declaration */ |
| 8287 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8288 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8289 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8290 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8291 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8292 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8293 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8294 | { 0, NULL, NULL }, |
| 8295 | }}; |
| 8296 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8297 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8298 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8299 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8300 | /* This function can fail with an abort() due to an Lua critical error. |
| 8301 | * We are in the initialisation process of HAProxy, this abort() is |
| 8302 | * tolerated. |
| 8303 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8304 | int hlua_post_init() |
| 8305 | { |
| 8306 | struct hlua_init_function *init; |
| 8307 | const char *msg; |
| 8308 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8309 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8310 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8311 | /* Call post initialisation function in safe environement. */ |
| 8312 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8313 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8314 | error = lua_tostring(gL.T, -1); |
| 8315 | else |
| 8316 | error = "critical error"; |
| 8317 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8318 | exit(1); |
| 8319 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8320 | |
| 8321 | #if USE_OPENSSL |
| 8322 | /* Initialize SSL server. */ |
| 8323 | if (socket_ssl.xprt->prepare_srv) { |
| 8324 | int saved_used_backed = global.ssl_used_backend; |
| 8325 | // don't affect maxconn automatic computation |
| 8326 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8327 | global.ssl_used_backend = saved_used_backed; |
| 8328 | } |
| 8329 | #endif |
| 8330 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8331 | hlua_fcn_post_init(gL.T); |
| 8332 | RESET_SAFE_LJMP(gL.T); |
| 8333 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8334 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8335 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 8336 | ret = hlua_ctx_resume(&gL, 0); |
| 8337 | switch (ret) { |
| 8338 | case HLUA_E_OK: |
| 8339 | lua_pop(gL.T, -1); |
| 8340 | return 1; |
| 8341 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8342 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8343 | return 0; |
| 8344 | case HLUA_E_ERRMSG: |
| 8345 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8346 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8347 | return 0; |
| 8348 | case HLUA_E_ERR: |
| 8349 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8350 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8351 | return 0; |
| 8352 | } |
| 8353 | } |
| 8354 | return 1; |
| 8355 | } |
| 8356 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8357 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8358 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8359 | * is the previously allocated size or the kind of object in case of a new |
| 8360 | * allocation. <nsize> is the requested new size. |
| 8361 | */ |
| 8362 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8363 | { |
| 8364 | struct hlua_mem_allocator *zone = ud; |
| 8365 | |
| 8366 | if (nsize == 0) { |
| 8367 | /* it's a free */ |
| 8368 | if (ptr) |
| 8369 | zone->allocated -= osize; |
| 8370 | free(ptr); |
| 8371 | return NULL; |
| 8372 | } |
| 8373 | |
| 8374 | if (!ptr) { |
| 8375 | /* it's a new allocation */ |
| 8376 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8377 | return NULL; |
| 8378 | |
| 8379 | ptr = malloc(nsize); |
| 8380 | if (ptr) |
| 8381 | zone->allocated += nsize; |
| 8382 | return ptr; |
| 8383 | } |
| 8384 | |
| 8385 | /* it's a realloc */ |
| 8386 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8387 | return NULL; |
| 8388 | |
| 8389 | ptr = realloc(ptr, nsize); |
| 8390 | if (ptr) |
| 8391 | zone->allocated += nsize - osize; |
| 8392 | return ptr; |
| 8393 | } |
| 8394 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8395 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8396 | * We are in the initialisation process of HAProxy, this abort() is |
| 8397 | * tolerated. |
| 8398 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8399 | void hlua_init(void) |
| 8400 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8401 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8402 | int idx; |
| 8403 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8404 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8405 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8406 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8407 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8408 | struct srv_kw *kw; |
| 8409 | int tmp_error; |
| 8410 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8411 | char *args[] = { /* SSL client configuration. */ |
| 8412 | "ssl", |
| 8413 | "verify", |
| 8414 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8415 | NULL |
| 8416 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8417 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8418 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8419 | /* Init main lua stack. */ |
| 8420 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8421 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8422 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8423 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8424 | hlua_sethlua(&gL); |
| 8425 | gL.Tref = LUA_REFNIL; |
| 8426 | gL.task = NULL; |
| 8427 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8428 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8429 | * the Lua function can fail with an abort. We are in the initialisation |
| 8430 | * process of HAProxy, this abort() is tolerated. |
| 8431 | */ |
| 8432 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8433 | /* Initialise lua. */ |
| 8434 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8435 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8436 | /* Set safe environment for the initialisation. */ |
| 8437 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8438 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8439 | error_msg = lua_tostring(gL.T, -1); |
| 8440 | else |
| 8441 | error_msg = "critical error"; |
| 8442 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8443 | exit(1); |
| 8444 | } |
| 8445 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8446 | /* |
| 8447 | * |
| 8448 | * Create "core" object. |
| 8449 | * |
| 8450 | */ |
| 8451 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8452 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8453 | lua_newtable(gL.T); |
| 8454 | |
| 8455 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8456 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8457 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8458 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8459 | /* Register special functions. */ |
| 8460 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8461 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8462 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8463 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8464 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8465 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8466 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8467 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8468 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8469 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8470 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8471 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8472 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8473 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8474 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8475 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8476 | hlua_class_function(gL.T, "log", hlua_log); |
| 8477 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8478 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8479 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8480 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8481 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8482 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8483 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8484 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8485 | |
| 8486 | /* |
| 8487 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8488 | * Register class Map |
| 8489 | * |
| 8490 | */ |
| 8491 | |
| 8492 | /* This table entry is the object "Map" base. */ |
| 8493 | lua_newtable(gL.T); |
| 8494 | |
| 8495 | /* register pattern types. */ |
| 8496 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8497 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8498 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8499 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8500 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8501 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8502 | |
| 8503 | /* register constructor. */ |
| 8504 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8505 | |
| 8506 | /* Create and fill the metatable. */ |
| 8507 | lua_newtable(gL.T); |
| 8508 | |
| 8509 | /* Create and fille the __index entry. */ |
| 8510 | lua_pushstring(gL.T, "__index"); |
| 8511 | lua_newtable(gL.T); |
| 8512 | |
| 8513 | /* Register . */ |
| 8514 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8515 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8516 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8517 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8518 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8519 | /* Register previous table in the registry with reference and named entry. |
| 8520 | * The function hlua_register_metatable() pops the stack, so we |
| 8521 | * previously create a copy of the table. |
| 8522 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8523 | 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] | 8524 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8525 | |
| 8526 | /* Assign the metatable to the mai Map object. */ |
| 8527 | lua_setmetatable(gL.T, -2); |
| 8528 | |
| 8529 | /* Set a name to the table. */ |
| 8530 | lua_setglobal(gL.T, "Map"); |
| 8531 | |
| 8532 | /* |
| 8533 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8534 | * Register class Channel |
| 8535 | * |
| 8536 | */ |
| 8537 | |
| 8538 | /* Create and fill the metatable. */ |
| 8539 | lua_newtable(gL.T); |
| 8540 | |
| 8541 | /* Create and fille the __index entry. */ |
| 8542 | lua_pushstring(gL.T, "__index"); |
| 8543 | lua_newtable(gL.T); |
| 8544 | |
| 8545 | /* Register . */ |
| 8546 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8547 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8548 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8549 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8550 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8551 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8552 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8553 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8554 | 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] | 8555 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8556 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8557 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +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_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8561 | |
| 8562 | /* |
| 8563 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8564 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 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 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8575 | /* Browse existing fetches and create the associated |
| 8576 | * object method. |
| 8577 | */ |
| 8578 | sf = NULL; |
| 8579 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8580 | |
| 8581 | /* Dont register the keywork if the arguments check function are |
| 8582 | * not safe during the runtime. |
| 8583 | */ |
| 8584 | if ((sf->val_args != NULL) && |
| 8585 | (sf->val_args != val_payload_lv) && |
| 8586 | (sf->val_args != val_hdr)) |
| 8587 | continue; |
| 8588 | |
| 8589 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8590 | * by an underscore. |
| 8591 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8592 | strncpy(trash.area, sf->kw, trash.size); |
| 8593 | trash.area[trash.size - 1] = '\0'; |
| 8594 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8595 | if (*p == '.' || *p == '-' || *p == '+') |
| 8596 | *p = '_'; |
| 8597 | |
| 8598 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8599 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8600 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8601 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8602 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8603 | } |
| 8604 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8605 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8606 | |
| 8607 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8608 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8609 | |
| 8610 | /* |
| 8611 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8612 | * Register class Converters |
| 8613 | * |
| 8614 | */ |
| 8615 | |
| 8616 | /* Create and fill the metatable. */ |
| 8617 | lua_newtable(gL.T); |
| 8618 | |
| 8619 | /* Create and fill the __index entry. */ |
| 8620 | lua_pushstring(gL.T, "__index"); |
| 8621 | lua_newtable(gL.T); |
| 8622 | |
| 8623 | /* Browse existing converters and create the associated |
| 8624 | * object method. |
| 8625 | */ |
| 8626 | sc = NULL; |
| 8627 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8628 | /* Dont register the keywork if the arguments check function are |
| 8629 | * not safe during the runtime. |
| 8630 | */ |
| 8631 | if (sc->val_args != NULL) |
| 8632 | continue; |
| 8633 | |
| 8634 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8635 | * by an underscore. |
| 8636 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8637 | strncpy(trash.area, sc->kw, trash.size); |
| 8638 | trash.area[trash.size - 1] = '\0'; |
| 8639 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8640 | if (*p == '.' || *p == '-' || *p == '+') |
| 8641 | *p = '_'; |
| 8642 | |
| 8643 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8644 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8645 | lua_pushlightuserdata(gL.T, sc); |
| 8646 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8647 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8648 | } |
| 8649 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8650 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8651 | |
| 8652 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8653 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8654 | |
| 8655 | /* |
| 8656 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8657 | * Register class HTTP |
| 8658 | * |
| 8659 | */ |
| 8660 | |
| 8661 | /* Create and fill the metatable. */ |
| 8662 | lua_newtable(gL.T); |
| 8663 | |
| 8664 | /* Create and fille the __index entry. */ |
| 8665 | lua_pushstring(gL.T, "__index"); |
| 8666 | lua_newtable(gL.T); |
| 8667 | |
| 8668 | /* Register Lua functions. */ |
| 8669 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8670 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8671 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8672 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8673 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8674 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8675 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8676 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8677 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8678 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8679 | |
| 8680 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8681 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8682 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8683 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8684 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8685 | 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] | 8686 | 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] | 8687 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8688 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8689 | |
| 8690 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8691 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8692 | |
| 8693 | /* |
| 8694 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8695 | * Register class AppletTCP |
| 8696 | * |
| 8697 | */ |
| 8698 | |
| 8699 | /* Create and fill the metatable. */ |
| 8700 | lua_newtable(gL.T); |
| 8701 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8702 | /* Create and fille the __index entry. */ |
| 8703 | lua_pushstring(gL.T, "__index"); |
| 8704 | lua_newtable(gL.T); |
| 8705 | |
| 8706 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8707 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8708 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8709 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8710 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8711 | 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] | 8712 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8713 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8714 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8715 | |
| 8716 | lua_settable(gL.T, -3); |
| 8717 | |
| 8718 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8719 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8720 | |
| 8721 | /* |
| 8722 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8723 | * Register class AppletHTTP |
| 8724 | * |
| 8725 | */ |
| 8726 | |
| 8727 | /* Create and fill the metatable. */ |
| 8728 | lua_newtable(gL.T); |
| 8729 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8730 | /* Create and fille the __index entry. */ |
| 8731 | lua_pushstring(gL.T, "__index"); |
| 8732 | lua_newtable(gL.T); |
| 8733 | |
| 8734 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8735 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8736 | 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] | 8737 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8738 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8739 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8740 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8741 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8742 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8743 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8744 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8745 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8746 | |
| 8747 | lua_settable(gL.T, -3); |
| 8748 | |
| 8749 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8750 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8751 | |
| 8752 | /* |
| 8753 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8754 | * Register class TXN |
| 8755 | * |
| 8756 | */ |
| 8757 | |
| 8758 | /* Create and fill the metatable. */ |
| 8759 | lua_newtable(gL.T); |
| 8760 | |
| 8761 | /* Create and fille the __index entry. */ |
| 8762 | lua_pushstring(gL.T, "__index"); |
| 8763 | lua_newtable(gL.T); |
| 8764 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8765 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8766 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8767 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8768 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8769 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8770 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8771 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 8772 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8773 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8774 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8775 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8776 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8777 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8778 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8779 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8780 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8781 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8782 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8783 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8784 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8785 | |
| 8786 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8787 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8788 | |
| 8789 | /* |
| 8790 | * |
| 8791 | * Register class Socket |
| 8792 | * |
| 8793 | */ |
| 8794 | |
| 8795 | /* Create and fill the metatable. */ |
| 8796 | lua_newtable(gL.T); |
| 8797 | |
| 8798 | /* Create and fille the __index entry. */ |
| 8799 | lua_pushstring(gL.T, "__index"); |
| 8800 | lua_newtable(gL.T); |
| 8801 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8802 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8803 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8804 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8805 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8806 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8807 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8808 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8809 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8810 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8811 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8812 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8813 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8814 | 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] | 8815 | |
| 8816 | /* Register the garbage collector entry. */ |
| 8817 | lua_pushstring(gL.T, "__gc"); |
| 8818 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8819 | 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] | 8820 | |
| 8821 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8822 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8823 | |
| 8824 | /* Proxy and server configuration initialisation. */ |
| 8825 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8826 | init_new_proxy(&socket_proxy); |
| 8827 | socket_proxy.parent = NULL; |
| 8828 | socket_proxy.last_change = now.tv_sec; |
| 8829 | socket_proxy.id = "LUA-SOCKET"; |
| 8830 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8831 | socket_proxy.maxconn = 0; |
| 8832 | socket_proxy.accept = NULL; |
| 8833 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8834 | socket_proxy.srv = NULL; |
| 8835 | socket_proxy.conn_retries = 0; |
| 8836 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8837 | |
| 8838 | /* Init TCP server: unchanged parameters */ |
| 8839 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8840 | socket_tcp.next = NULL; |
| 8841 | socket_tcp.proxy = &socket_proxy; |
| 8842 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8843 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8844 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8845 | socket_tcp.priv_conns = NULL; |
| 8846 | socket_tcp.idle_conns = NULL; |
| 8847 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8848 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8849 | socket_tcp.last_change = 0; |
| 8850 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8851 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8852 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8853 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8854 | |
| 8855 | /* XXX: Copy default parameter from default server, |
| 8856 | * but the default server is not initialized. |
| 8857 | */ |
| 8858 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8859 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8860 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8861 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8862 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8863 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8864 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8865 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8866 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8867 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8868 | |
| 8869 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8870 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8871 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8872 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8873 | socket_tcp.check.server = &socket_tcp; |
| 8874 | |
| 8875 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8876 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8877 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8878 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8879 | socket_tcp.agent.server = &socket_tcp; |
| 8880 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8881 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8882 | |
| 8883 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8884 | /* Init TCP server: unchanged parameters */ |
| 8885 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8886 | socket_ssl.next = NULL; |
| 8887 | socket_ssl.proxy = &socket_proxy; |
| 8888 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8889 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8890 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8891 | socket_ssl.priv_conns = NULL; |
| 8892 | socket_ssl.idle_conns = NULL; |
| 8893 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8894 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8895 | socket_ssl.last_change = 0; |
| 8896 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8897 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8898 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8899 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8900 | |
| 8901 | /* XXX: Copy default parameter from default server, |
| 8902 | * but the default server is not initialized. |
| 8903 | */ |
| 8904 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8905 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8906 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8907 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8908 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8909 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8910 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8911 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8912 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8913 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8914 | |
| 8915 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8916 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8917 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8918 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8919 | socket_ssl.check.server = &socket_ssl; |
| 8920 | |
| 8921 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8922 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8923 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8924 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8925 | socket_ssl.agent.server = &socket_ssl; |
| 8926 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8927 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8928 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8929 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8930 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8931 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8932 | /* |
| 8933 | * |
| 8934 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8935 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8936 | * features like client certificates and ssl_verify. |
| 8937 | * |
| 8938 | */ |
| 8939 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8940 | if (tmp_error != 0) { |
| 8941 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8942 | abort(); /* This must be never arrives because the command line |
| 8943 | not editable by the user. */ |
| 8944 | } |
| 8945 | idx += kw->skip; |
| 8946 | } |
| 8947 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8948 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8949 | |
| 8950 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8951 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8952 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8953 | static void hlua_register_build_options(void) |
| 8954 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8955 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8956 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8957 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8958 | hap_register_build_opts(ptr, 1); |
| 8959 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8960 | |
| 8961 | INITCALL0(STG_REGISTER, hlua_register_build_options); |