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. */ |
| 157 | #define APPLET_100C 0x02 /* 100 continue expected. */ |
| 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. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 162 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 163 | /* The main Lua execution context. */ |
| 164 | struct hlua gL; |
| 165 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 166 | /* This is the memory pool containing struct lua for applets |
| 167 | * (including cli). |
| 168 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 169 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 170 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 171 | /* Used for Socket connection. */ |
| 172 | static struct proxy socket_proxy; |
| 173 | static struct server socket_tcp; |
| 174 | #ifdef USE_OPENSSL |
| 175 | static struct server socket_ssl; |
| 176 | #endif |
| 177 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 178 | /* List head of the function called at the initialisation time. */ |
| 179 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 180 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 181 | /* The following variables contains the reference of the different |
| 182 | * Lua classes. These references are useful for identify metadata |
| 183 | * associated with an object. |
| 184 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 185 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 186 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 187 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 188 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 189 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 190 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 191 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 192 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 193 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 194 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 195 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 196 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 197 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 198 | * because a task may remain alive during all the haproxy execution. |
| 199 | */ |
| 200 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 201 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 202 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 203 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 204 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 205 | * it is used for preventing infinite loops. |
| 206 | * |
| 207 | * I test the scheer with an infinite loop containing one incrementation |
| 208 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 209 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 210 | * to one interrupt each 10 000 instructions. |
| 211 | * |
| 212 | * configured | Number of |
| 213 | * instructions | loops executed |
| 214 | * between two | in milions |
| 215 | * forced yields | |
| 216 | * ---------------+--------------- |
| 217 | * 10 | 160 |
| 218 | * 500 | 670 |
| 219 | * 1000 | 680 |
| 220 | * 5000 | 700 |
| 221 | * 7000 | 700 |
| 222 | * 8000 | 700 |
| 223 | * 9000 | 710 <- ceil |
| 224 | * 10000 | 710 |
| 225 | * 100000 | 710 |
| 226 | * 1000000 | 710 |
| 227 | * |
| 228 | */ |
| 229 | static unsigned int hlua_nb_instruction = 10000; |
| 230 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 231 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 232 | * be enforced on any memory allocation. |
| 233 | */ |
| 234 | struct hlua_mem_allocator { |
| 235 | size_t allocated; |
| 236 | size_t limit; |
| 237 | }; |
| 238 | |
| 239 | static struct hlua_mem_allocator hlua_global_allocator; |
| 240 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 241 | static const char error_500[] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 242 | "HTTP/1.0 500 Internal Server Error\r\n" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 243 | "Cache-Control: no-cache\r\n" |
| 244 | "Connection: close\r\n" |
| 245 | "Content-Type: text/html\r\n" |
| 246 | "\r\n" |
Joseph Herlant | 7fe1577 | 2018-11-15 10:07:32 -0800 | [diff] [blame] | 247 | "<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] | 248 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 249 | /* These functions converts types between HAProxy internal args or |
| 250 | * sample and LUA types. Another function permits to check if the |
| 251 | * LUA stack contains arguments according with an required ARG_T |
| 252 | * format. |
| 253 | */ |
| 254 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 255 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 256 | __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] | 257 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 258 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 259 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 260 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 261 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 262 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 263 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 264 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 265 | do { \ |
| 266 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 267 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 268 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 269 | } while (0) |
| 270 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 271 | /* Used to check an Lua function type in the stack. It creates and |
| 272 | * returns a reference of the function. This function throws an |
| 273 | * error if the rgument is not a "function". |
| 274 | */ |
| 275 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 276 | { |
| 277 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 278 | 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] | 279 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 280 | } |
| 281 | lua_pushvalue(L, argno); |
| 282 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 283 | } |
| 284 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 285 | /* Return the string that is of the top of the stack. */ |
| 286 | const char *hlua_get_top_error_string(lua_State *L) |
| 287 | { |
| 288 | if (lua_gettop(L) < 1) |
| 289 | return "unknown error"; |
| 290 | if (lua_type(L, -1) != LUA_TSTRING) |
| 291 | return "unknown error"; |
| 292 | return lua_tostring(L, -1); |
| 293 | } |
| 294 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 295 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 296 | { |
| 297 | lua_Debug ar; |
| 298 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 299 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 300 | int filled = 0; |
| 301 | |
| 302 | while (lua_getstack(L, level++, &ar)) { |
| 303 | |
| 304 | /* Add separator */ |
| 305 | if (filled) |
| 306 | chunk_appendf(msg, ", "); |
| 307 | filled = 1; |
| 308 | |
| 309 | /* Fill fields: |
| 310 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 311 | * 'l': fills in the field currentline; |
| 312 | * 'n': fills in the field name and namewhat; |
| 313 | * 't': fills in the field istailcall; |
| 314 | */ |
| 315 | lua_getinfo(L, "Slnt", &ar); |
| 316 | |
| 317 | /* Append code localisation */ |
| 318 | if (ar.currentline > 0) |
| 319 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 320 | else |
| 321 | chunk_appendf(msg, "%s ", ar.short_src); |
| 322 | |
| 323 | /* |
| 324 | * Get function name |
| 325 | * |
| 326 | * if namewhat is no empty, name is defined. |
| 327 | * what contains "Lua" for Lua function, "C" for C function, |
| 328 | * or "main" for main code. |
| 329 | */ |
| 330 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 331 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 332 | |
| 333 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 334 | chunk_appendf(msg, "main chunk"); |
| 335 | |
| 336 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 337 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 338 | |
| 339 | else /* nothing left... */ |
| 340 | chunk_appendf(msg, "?"); |
| 341 | |
| 342 | |
| 343 | /* Display tailed call */ |
| 344 | if (ar.istailcall) |
| 345 | chunk_appendf(msg, " ..."); |
| 346 | } |
| 347 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 348 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 352 | /* This function check the number of arguments available in the |
| 353 | * stack. If the number of arguments available is not the same |
| 354 | * then <nb> an error is throwed. |
| 355 | */ |
| 356 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 357 | { |
| 358 | if (lua_gettop(L) == nb) |
| 359 | return; |
| 360 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 361 | } |
| 362 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 363 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 364 | * and the line number where the error is encountered. |
| 365 | */ |
| 366 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 367 | { |
| 368 | va_list argp; |
| 369 | va_start(argp, fmt); |
| 370 | luaL_where(L, 1); |
| 371 | lua_pushvfstring(L, fmt, argp); |
| 372 | va_end(argp); |
| 373 | lua_concat(L, 2); |
| 374 | return 1; |
| 375 | } |
| 376 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 377 | /* This functions is used with sample fetch and converters. It |
| 378 | * converts the HAProxy configuration argument in a lua stack |
| 379 | * values. |
| 380 | * |
| 381 | * It takes an array of "arg", and each entry of the array is |
| 382 | * converted and pushed in the LUA stack. |
| 383 | */ |
| 384 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 385 | { |
| 386 | switch (arg->type) { |
| 387 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 388 | case ARGT_TIME: |
| 389 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 390 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 391 | break; |
| 392 | |
| 393 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 394 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 395 | break; |
| 396 | |
| 397 | case ARGT_IPV4: |
| 398 | case ARGT_IPV6: |
| 399 | case ARGT_MSK4: |
| 400 | case ARGT_MSK6: |
| 401 | case ARGT_FE: |
| 402 | case ARGT_BE: |
| 403 | case ARGT_TAB: |
| 404 | case ARGT_SRV: |
| 405 | case ARGT_USR: |
| 406 | case ARGT_MAP: |
| 407 | default: |
| 408 | lua_pushnil(L); |
| 409 | break; |
| 410 | } |
| 411 | return 1; |
| 412 | } |
| 413 | |
| 414 | /* This function take one entrie in an LUA stack at the index "ud", |
| 415 | * and try to convert it in an HAProxy argument entry. This is useful |
| 416 | * with sample fetch wrappers. The input arguments are gived to the |
| 417 | * lua wrapper and converted as arg list by thi function. |
| 418 | */ |
| 419 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 420 | { |
| 421 | switch (lua_type(L, ud)) { |
| 422 | |
| 423 | case LUA_TNUMBER: |
| 424 | case LUA_TBOOLEAN: |
| 425 | arg->type = ARGT_SINT; |
| 426 | arg->data.sint = lua_tointeger(L, ud); |
| 427 | break; |
| 428 | |
| 429 | case LUA_TSTRING: |
| 430 | arg->type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 431 | arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 432 | break; |
| 433 | |
| 434 | case LUA_TUSERDATA: |
| 435 | case LUA_TNIL: |
| 436 | case LUA_TTABLE: |
| 437 | case LUA_TFUNCTION: |
| 438 | case LUA_TTHREAD: |
| 439 | case LUA_TLIGHTUSERDATA: |
| 440 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 441 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 442 | break; |
| 443 | } |
| 444 | return 1; |
| 445 | } |
| 446 | |
| 447 | /* the following functions are used to convert a struct sample |
| 448 | * in Lua type. This useful to convert the return of the |
| 449 | * fetchs or converters. |
| 450 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 451 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 452 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 453 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 454 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 455 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 456 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 457 | break; |
| 458 | |
| 459 | case SMP_T_BIN: |
| 460 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 461 | 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] | 462 | break; |
| 463 | |
| 464 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 465 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 466 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 467 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 468 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 469 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 470 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 471 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 472 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 473 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 474 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 475 | 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] | 476 | break; |
| 477 | default: |
| 478 | lua_pushnil(L); |
| 479 | break; |
| 480 | } |
| 481 | break; |
| 482 | |
| 483 | case SMP_T_IPV4: |
| 484 | case SMP_T_IPV6: |
| 485 | 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] | 486 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 487 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 488 | 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] | 489 | else |
| 490 | lua_pushnil(L); |
| 491 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 492 | default: |
| 493 | lua_pushnil(L); |
| 494 | break; |
| 495 | } |
| 496 | return 1; |
| 497 | } |
| 498 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 499 | /* the following functions are used to convert a struct sample |
| 500 | * in Lua strings. This is useful to convert the return of the |
| 501 | * fetchs or converters. |
| 502 | */ |
| 503 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 504 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 505 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 506 | |
| 507 | case SMP_T_BIN: |
| 508 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 509 | 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] | 510 | break; |
| 511 | |
| 512 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 513 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 514 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 515 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 516 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 517 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 518 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 519 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 520 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 521 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 522 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 523 | 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] | 524 | break; |
| 525 | default: |
| 526 | lua_pushstring(L, ""); |
| 527 | break; |
| 528 | } |
| 529 | break; |
| 530 | |
| 531 | case SMP_T_SINT: |
| 532 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 533 | case SMP_T_IPV4: |
| 534 | case SMP_T_IPV6: |
| 535 | 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] | 536 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 537 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 538 | 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] | 539 | else |
| 540 | lua_pushstring(L, ""); |
| 541 | break; |
| 542 | default: |
| 543 | lua_pushstring(L, ""); |
| 544 | break; |
| 545 | } |
| 546 | return 1; |
| 547 | } |
| 548 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 549 | /* the following functions are used to convert an Lua type in a |
| 550 | * struct sample. This is useful to provide data from a converter |
| 551 | * to the LUA code. |
| 552 | */ |
| 553 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 554 | { |
| 555 | switch (lua_type(L, ud)) { |
| 556 | |
| 557 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 558 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 559 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 560 | break; |
| 561 | |
| 562 | |
| 563 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 564 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 565 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 566 | break; |
| 567 | |
| 568 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 569 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 570 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 571 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 572 | break; |
| 573 | |
| 574 | case LUA_TUSERDATA: |
| 575 | case LUA_TNIL: |
| 576 | case LUA_TTABLE: |
| 577 | case LUA_TFUNCTION: |
| 578 | case LUA_TTHREAD: |
| 579 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 580 | case LUA_TNONE: |
| 581 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 582 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 583 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 584 | break; |
| 585 | } |
| 586 | return 1; |
| 587 | } |
| 588 | |
| 589 | /* This function check the "argp" builded by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 590 | * 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] | 591 | * 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] | 592 | * |
| 593 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 594 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 595 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 596 | __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] | 597 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 598 | { |
| 599 | int min_arg; |
| 600 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 601 | struct proxy *px; |
| 602 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 603 | |
| 604 | idx = 0; |
| 605 | min_arg = ARGM(mask); |
| 606 | mask >>= ARGM_BITS; |
| 607 | |
| 608 | while (1) { |
| 609 | |
| 610 | /* Check oversize. */ |
| 611 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 612 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | /* Check for mandatory arguments. */ |
| 616 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 617 | if (idx < min_arg) { |
| 618 | |
| 619 | /* If miss other argument than the first one, we return an error. */ |
| 620 | if (idx > 0) |
| 621 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 622 | |
| 623 | /* If first argument have a certain type, some default values |
| 624 | * may be used. See the function smp_resolve_args(). |
| 625 | */ |
| 626 | switch (mask & ARGT_MASK) { |
| 627 | |
| 628 | case ARGT_FE: |
| 629 | if (!(p->cap & PR_CAP_FE)) |
| 630 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 631 | argp[idx].data.prx = p; |
| 632 | argp[idx].type = ARGT_FE; |
| 633 | argp[idx+1].type = ARGT_STOP; |
| 634 | break; |
| 635 | |
| 636 | case ARGT_BE: |
| 637 | if (!(p->cap & PR_CAP_BE)) |
| 638 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 639 | argp[idx].data.prx = p; |
| 640 | argp[idx].type = ARGT_BE; |
| 641 | argp[idx+1].type = ARGT_STOP; |
| 642 | break; |
| 643 | |
| 644 | case ARGT_TAB: |
| 645 | argp[idx].data.prx = p; |
| 646 | argp[idx].type = ARGT_TAB; |
| 647 | argp[idx+1].type = ARGT_STOP; |
| 648 | break; |
| 649 | |
| 650 | default: |
| 651 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 652 | break; |
| 653 | } |
| 654 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 655 | return 0; |
| 656 | } |
| 657 | |
| 658 | /* Check for exceed the number of requiered argument. */ |
| 659 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 660 | argp[idx].type != ARGT_STOP) { |
| 661 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 662 | } |
| 663 | |
| 664 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 665 | argp[idx].type == ARGT_STOP) { |
| 666 | return 0; |
| 667 | } |
| 668 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 669 | /* Convert some argument types. */ |
| 670 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 671 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 672 | if (argp[idx].type != ARGT_SINT) |
| 673 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 674 | argp[idx].type = ARGT_SINT; |
| 675 | break; |
| 676 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 677 | case ARGT_TIME: |
| 678 | if (argp[idx].type != ARGT_SINT) |
| 679 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 680 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 681 | break; |
| 682 | |
| 683 | case ARGT_SIZE: |
| 684 | if (argp[idx].type != ARGT_SINT) |
| 685 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 686 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 687 | break; |
| 688 | |
| 689 | case ARGT_FE: |
| 690 | if (argp[idx].type != ARGT_STR) |
| 691 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 692 | memcpy(trash.area, argp[idx].data.str.area, |
| 693 | argp[idx].data.str.data); |
| 694 | trash.area[argp[idx].data.str.data] = 0; |
| 695 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 696 | if (!argp[idx].data.prx) |
| 697 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 698 | argp[idx].type = ARGT_FE; |
| 699 | break; |
| 700 | |
| 701 | case ARGT_BE: |
| 702 | if (argp[idx].type != ARGT_STR) |
| 703 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 704 | memcpy(trash.area, argp[idx].data.str.area, |
| 705 | argp[idx].data.str.data); |
| 706 | trash.area[argp[idx].data.str.data] = 0; |
| 707 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 708 | if (!argp[idx].data.prx) |
| 709 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 710 | argp[idx].type = ARGT_BE; |
| 711 | break; |
| 712 | |
| 713 | case ARGT_TAB: |
| 714 | if (argp[idx].type != ARGT_STR) |
| 715 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 716 | memcpy(trash.area, argp[idx].data.str.area, |
| 717 | argp[idx].data.str.data); |
| 718 | trash.area[argp[idx].data.str.data] = 0; |
| 719 | argp[idx].data.prx = proxy_tbl_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 720 | if (!argp[idx].data.prx) |
| 721 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 722 | argp[idx].type = ARGT_TAB; |
| 723 | break; |
| 724 | |
| 725 | case ARGT_SRV: |
| 726 | if (argp[idx].type != ARGT_STR) |
| 727 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 728 | memcpy(trash.area, argp[idx].data.str.area, |
| 729 | argp[idx].data.str.data); |
| 730 | trash.area[argp[idx].data.str.data] = 0; |
| 731 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 732 | if (sname) { |
| 733 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 734 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 735 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 736 | if (!px) |
| 737 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 738 | } |
| 739 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 740 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 741 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 742 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 743 | argp[idx].data.srv = findserver(px, sname); |
| 744 | if (!argp[idx].data.srv) |
| 745 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 746 | argp[idx].type = ARGT_SRV; |
| 747 | break; |
| 748 | |
| 749 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 750 | memcpy(trash.area, argp[idx].data.str.area, |
| 751 | argp[idx].data.str.data); |
| 752 | trash.area[argp[idx].data.str.data] = 0; |
| 753 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 754 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 755 | argp[idx].type = ARGT_IPV4; |
| 756 | break; |
| 757 | |
| 758 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 759 | memcpy(trash.area, argp[idx].data.str.area, |
| 760 | argp[idx].data.str.data); |
| 761 | trash.area[argp[idx].data.str.data] = 0; |
| 762 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 763 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 764 | argp[idx].type = ARGT_MSK4; |
| 765 | break; |
| 766 | |
| 767 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 768 | memcpy(trash.area, argp[idx].data.str.area, |
| 769 | argp[idx].data.str.data); |
| 770 | trash.area[argp[idx].data.str.data] = 0; |
| 771 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 772 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 773 | argp[idx].type = ARGT_IPV6; |
| 774 | break; |
| 775 | |
| 776 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 777 | memcpy(trash.area, argp[idx].data.str.area, |
| 778 | argp[idx].data.str.data); |
| 779 | trash.area[argp[idx].data.str.data] = 0; |
| 780 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 781 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 782 | argp[idx].type = ARGT_MSK6; |
| 783 | break; |
| 784 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 785 | case ARGT_MAP: |
| 786 | case ARGT_REG: |
| 787 | case ARGT_USR: |
| 788 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 789 | break; |
| 790 | } |
| 791 | |
| 792 | /* Check for type of argument. */ |
| 793 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 794 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 795 | arg_type_names[(mask & ARGT_MASK)], |
| 796 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 797 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 798 | } |
| 799 | |
| 800 | /* Next argument. */ |
| 801 | mask >>= ARGT_BITS; |
| 802 | idx++; |
| 803 | } |
| 804 | } |
| 805 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 806 | /* |
| 807 | * The following functions are used to make correspondance between the the |
| 808 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 809 | * |
| 810 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 811 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 812 | */ |
| 813 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 814 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 815 | struct hlua **hlua = lua_getextraspace(L); |
| 816 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 817 | } |
| 818 | static inline void hlua_sethlua(struct hlua *hlua) |
| 819 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 820 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 821 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 822 | } |
| 823 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 824 | /* This function is used to send logs. It try to send on screen (stderr) |
| 825 | * and on the default syslog server. |
| 826 | */ |
| 827 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 828 | { |
| 829 | struct tm tm; |
| 830 | char *p; |
| 831 | |
| 832 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 833 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 834 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 835 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 836 | /* Break the message if exceed the buffer size. */ |
| 837 | *(p-4) = ' '; |
| 838 | *(p-3) = '.'; |
| 839 | *(p-2) = '.'; |
| 840 | *(p-1) = '.'; |
| 841 | break; |
| 842 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 843 | if (isprint(*msg)) |
| 844 | *p = *msg; |
| 845 | else |
| 846 | *p = '.'; |
| 847 | } |
| 848 | *p = '\0'; |
| 849 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 850 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 851 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 852 | get_localtime(date.tv_sec, &tm); |
| 853 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 854 | 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] | 855 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 856 | fflush(stderr); |
| 857 | } |
| 858 | } |
| 859 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 860 | /* This function just ensure that the yield will be always |
| 861 | * returned with a timeout and permit to set some flags |
| 862 | */ |
| 863 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 864 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 865 | { |
| 866 | struct hlua *hlua = hlua_gethlua(L); |
| 867 | |
| 868 | /* Set the wake timeout. If timeout is required, we set |
| 869 | * the expiration time. |
| 870 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 871 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 872 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 873 | hlua->flags |= flags; |
| 874 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 875 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 876 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 877 | } |
| 878 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 879 | /* This function initialises the Lua environment stored in the stream. |
| 880 | * 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] | 881 | * 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] | 882 | * |
| 883 | * This function is particular. it initialises a new Lua thread. If the |
| 884 | * initialisation fails (example: out of memory error), the lua function |
| 885 | * throws an error (longjmp). |
| 886 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 887 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 888 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 889 | * MUST NOT manipulate the created thread stack state, because it is not |
| 890 | * proctected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 891 | */ |
| 892 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 893 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 894 | if (!SET_SAFE_LJMP(gL.T)) { |
| 895 | lua->Tref = LUA_REFNIL; |
| 896 | return 0; |
| 897 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 898 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 899 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 900 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 901 | lua->T = lua_newthread(gL.T); |
| 902 | if (!lua->T) { |
| 903 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | 0a97620 | 2017-07-12 11:18:00 +0200 | [diff] [blame] | 904 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 905 | return 0; |
| 906 | } |
| 907 | hlua_sethlua(lua); |
| 908 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 909 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 910 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 911 | return 1; |
| 912 | } |
| 913 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 914 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 915 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 916 | * is not freed. |
| 917 | */ |
| 918 | void hlua_ctx_destroy(struct hlua *lua) |
| 919 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 920 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 921 | return; |
| 922 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 923 | if (!lua->T) |
| 924 | goto end; |
| 925 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 926 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 927 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 928 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 929 | if (!SET_SAFE_LJMP(lua->T)) |
| 930 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 931 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 932 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 933 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 934 | if (!SET_SAFE_LJMP(gL.T)) |
| 935 | return; |
| 936 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 937 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 938 | /* Forces a garbage collecting process. If the Lua program is finished |
| 939 | * without error, we run the GC on the thread pointer. Its freed all |
| 940 | * the unused memory. |
| 941 | * If the thread is finnish with an error or is currently yielded, |
| 942 | * it seems that the GC applied on the thread doesn't clean anything, |
| 943 | * so e run the GC on the main thread. |
| 944 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 945 | * the garbage collection. |
| 946 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 947 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 948 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 949 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 950 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 951 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 952 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 953 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 954 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 955 | |
| 956 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 957 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | /* This function is used to restore the Lua context when a coroutine |
| 961 | * fails. This function copy the common memory between old coroutine |
| 962 | * and the new coroutine. The old coroutine is destroyed, and its |
| 963 | * replaced by the new coroutine. |
| 964 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 965 | * as string error message and it is copied in the new stack. |
| 966 | */ |
| 967 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 968 | { |
| 969 | lua_State *T; |
| 970 | int new_ref; |
| 971 | |
| 972 | /* Renew the main LUA stack doesn't have sense. */ |
| 973 | if (lua == &gL) |
| 974 | return 0; |
| 975 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 976 | /* New Lua coroutine. */ |
| 977 | T = lua_newthread(gL.T); |
| 978 | if (!T) |
| 979 | return 0; |
| 980 | |
| 981 | /* Copy last error message. */ |
| 982 | if (keep_msg) |
| 983 | lua_xmove(lua->T, T, 1); |
| 984 | |
| 985 | /* Copy data between the coroutines. */ |
| 986 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 987 | lua_xmove(lua->T, T, 1); |
| 988 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 989 | |
| 990 | /* Destroy old data. */ |
| 991 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 992 | |
| 993 | /* The thread is garbage collected by Lua. */ |
| 994 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 995 | |
| 996 | /* Fill the struct with the new coroutine values. */ |
| 997 | lua->Mref = new_ref; |
| 998 | lua->T = T; |
| 999 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1000 | |
| 1001 | /* Set context. */ |
| 1002 | hlua_sethlua(lua); |
| 1003 | |
| 1004 | return 1; |
| 1005 | } |
| 1006 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1007 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1008 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1009 | struct hlua *hlua = hlua_gethlua(L); |
| 1010 | |
| 1011 | /* Lua cannot yield when its returning from a function, |
| 1012 | * so, we can fix the interrupt hook to 1 instruction, |
| 1013 | * expecting that the function is finnished. |
| 1014 | */ |
| 1015 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1016 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1017 | return; |
| 1018 | } |
| 1019 | |
| 1020 | /* restore the interrupt condition. */ |
| 1021 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1022 | |
| 1023 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1024 | * If the state is not yieldable, trying yield causes an error. |
| 1025 | */ |
| 1026 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1027 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1028 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1029 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1030 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1031 | hlua->run_time += now_ms - hlua->start_time; |
| 1032 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1033 | lua_pushfstring(L, "execution timeout"); |
| 1034 | WILL_LJMP(lua_error(L)); |
| 1035 | } |
| 1036 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1037 | /* Update the start time. */ |
| 1038 | hlua->start_time = now_ms; |
| 1039 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1040 | /* Try to interrupt the process at the end of the current |
| 1041 | * unyieldable function. |
| 1042 | */ |
| 1043 | 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] | 1044 | } |
| 1045 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1046 | /* This function start or resumes the Lua stack execution. If the flag |
| 1047 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1048 | * The function return an error. |
| 1049 | * |
| 1050 | * The function can returns 4 values: |
| 1051 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1052 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1053 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1054 | * - 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] | 1055 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1056 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1057 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1058 | * 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] | 1059 | * LUA code. |
| 1060 | */ |
| 1061 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1062 | { |
| 1063 | int ret; |
| 1064 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1065 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1066 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1067 | /* Initialise run time counter. */ |
| 1068 | if (!HLUA_IS_RUNNING(lua)) |
| 1069 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1070 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1071 | /* Lock the whole Lua execution. This lock must be before the |
| 1072 | * label "resume_execution". |
| 1073 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1074 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1075 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1076 | resume_execution: |
| 1077 | |
| 1078 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1079 | * instructions. it is used for preventing infinite loops. |
| 1080 | */ |
| 1081 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1082 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1083 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1084 | HLUA_SET_RUN(lua); |
| 1085 | HLUA_CLR_CTRLYIELD(lua); |
| 1086 | HLUA_CLR_WAKERESWR(lua); |
| 1087 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1088 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1089 | /* Update the start time. */ |
| 1090 | lua->start_time = now_ms; |
| 1091 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1092 | /* Call the function. */ |
| 1093 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1094 | switch (ret) { |
| 1095 | |
| 1096 | case LUA_OK: |
| 1097 | ret = HLUA_E_OK; |
| 1098 | break; |
| 1099 | |
| 1100 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1101 | /* Check if the execution timeout is expired. It it is the case, we |
| 1102 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1103 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1104 | tv_update_date(0, 1); |
| 1105 | lua->run_time += now_ms - lua->start_time; |
| 1106 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1107 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1108 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1109 | break; |
| 1110 | } |
| 1111 | /* Process the forced yield. if the general yield is not allowed or |
| 1112 | * if no task were associated this the current Lua execution |
| 1113 | * coroutine, we resume the execution. Else we want to return in the |
| 1114 | * scheduler and we want to be waked up again, to continue the |
| 1115 | * current Lua execution. So we schedule our own task. |
| 1116 | */ |
| 1117 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1118 | if (!yield_allowed || !lua->task) |
| 1119 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1120 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1121 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1122 | if (!yield_allowed) { |
| 1123 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1124 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1125 | break; |
| 1126 | } |
| 1127 | ret = HLUA_E_AGAIN; |
| 1128 | break; |
| 1129 | |
| 1130 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1131 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1132 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1133 | * because the errors ares the only one mean to return immediately |
| 1134 | * from and lua execution. |
| 1135 | */ |
| 1136 | if (lua->flags & HLUA_EXIT) { |
| 1137 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1138 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1139 | break; |
| 1140 | } |
| 1141 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1142 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1143 | if (!lua_checkstack(lua->T, 1)) { |
| 1144 | ret = HLUA_E_ERR; |
| 1145 | break; |
| 1146 | } |
| 1147 | msg = lua_tostring(lua->T, -1); |
| 1148 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1149 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1150 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1151 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1152 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1153 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1154 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1155 | ret = HLUA_E_ERRMSG; |
| 1156 | break; |
| 1157 | |
| 1158 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1159 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1160 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1161 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1162 | break; |
| 1163 | |
| 1164 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1165 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1166 | if (!lua_checkstack(lua->T, 1)) { |
| 1167 | ret = HLUA_E_ERR; |
| 1168 | break; |
| 1169 | } |
| 1170 | msg = lua_tostring(lua->T, -1); |
| 1171 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1172 | lua_pop(lua->T, 1); |
| 1173 | if (msg) |
| 1174 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1175 | else |
| 1176 | lua_pushfstring(lua->T, "message handler error"); |
| 1177 | ret = HLUA_E_ERRMSG; |
| 1178 | break; |
| 1179 | |
| 1180 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1181 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1182 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1183 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1184 | break; |
| 1185 | } |
| 1186 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1187 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1188 | if (lua->flags & HLUA_MUST_GC && |
| 1189 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1190 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1191 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1192 | switch (ret) { |
| 1193 | case HLUA_E_AGAIN: |
| 1194 | break; |
| 1195 | |
| 1196 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1197 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1198 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1199 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1200 | break; |
| 1201 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1202 | case HLUA_E_ETMOUT: |
| 1203 | case HLUA_E_NOMEM: |
| 1204 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1205 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1206 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1207 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1208 | hlua_ctx_renew(lua, 0); |
| 1209 | break; |
| 1210 | |
| 1211 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1212 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1213 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1214 | break; |
| 1215 | } |
| 1216 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1217 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1218 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1219 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1220 | return ret; |
| 1221 | } |
| 1222 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1223 | /* This function exit the current code. */ |
| 1224 | __LJMP static int hlua_done(lua_State *L) |
| 1225 | { |
| 1226 | struct hlua *hlua = hlua_gethlua(L); |
| 1227 | |
| 1228 | hlua->flags |= HLUA_EXIT; |
| 1229 | WILL_LJMP(lua_error(L)); |
| 1230 | |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1234 | /* This function is an LUA binding. It provides a function |
| 1235 | * for deleting ACL from a referenced ACL file. |
| 1236 | */ |
| 1237 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1238 | { |
| 1239 | const char *name; |
| 1240 | const char *key; |
| 1241 | struct pat_ref *ref; |
| 1242 | |
| 1243 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1244 | |
| 1245 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1246 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1247 | |
| 1248 | ref = pat_ref_lookup(name); |
| 1249 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1250 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1251 | |
| 1252 | pat_ref_delete(ref, key); |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
| 1256 | /* This function is an LUA binding. It provides a function |
| 1257 | * for deleting map entry from a referenced map file. |
| 1258 | */ |
| 1259 | static int hlua_del_map(lua_State *L) |
| 1260 | { |
| 1261 | const char *name; |
| 1262 | const char *key; |
| 1263 | struct pat_ref *ref; |
| 1264 | |
| 1265 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1266 | |
| 1267 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1268 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1269 | |
| 1270 | ref = pat_ref_lookup(name); |
| 1271 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1272 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1273 | |
| 1274 | pat_ref_delete(ref, key); |
| 1275 | return 0; |
| 1276 | } |
| 1277 | |
| 1278 | /* This function is an LUA binding. It provides a function |
| 1279 | * for adding ACL pattern from a referenced ACL file. |
| 1280 | */ |
| 1281 | static int hlua_add_acl(lua_State *L) |
| 1282 | { |
| 1283 | const char *name; |
| 1284 | const char *key; |
| 1285 | struct pat_ref *ref; |
| 1286 | |
| 1287 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1288 | |
| 1289 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1290 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1291 | |
| 1292 | ref = pat_ref_lookup(name); |
| 1293 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1294 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1295 | |
| 1296 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1297 | pat_ref_add(ref, key, NULL, NULL); |
| 1298 | return 0; |
| 1299 | } |
| 1300 | |
| 1301 | /* This function is an LUA binding. It provides a function |
| 1302 | * for setting map pattern and sample from a referenced map |
| 1303 | * file. |
| 1304 | */ |
| 1305 | static int hlua_set_map(lua_State *L) |
| 1306 | { |
| 1307 | const char *name; |
| 1308 | const char *key; |
| 1309 | const char *value; |
| 1310 | struct pat_ref *ref; |
| 1311 | |
| 1312 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1313 | |
| 1314 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1315 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1316 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1317 | |
| 1318 | ref = pat_ref_lookup(name); |
| 1319 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1320 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1321 | |
| 1322 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1323 | pat_ref_set(ref, key, value, NULL); |
| 1324 | else |
| 1325 | pat_ref_add(ref, key, value, NULL); |
| 1326 | return 0; |
| 1327 | } |
| 1328 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1329 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1330 | * an integer or user data. This data is associated with a metatable. This |
| 1331 | * metatable have an original version registred in the global context with |
| 1332 | * the name of the object (_G[<name>] = <metable> ). |
| 1333 | * |
| 1334 | * A metable is a table that modify the standard behavior of a standard |
| 1335 | * access to the associated data. The entries of this new metatable are |
| 1336 | * defined as is: |
| 1337 | * |
| 1338 | * http://lua-users.org/wiki/MetatableEvents |
| 1339 | * |
| 1340 | * __index |
| 1341 | * |
| 1342 | * we access an absent field in a table, the result is nil. This is |
| 1343 | * true, but it is not the whole truth. Actually, such access triggers |
| 1344 | * the interpreter to look for an __index metamethod: If there is no |
| 1345 | * such method, as usually happens, then the access results in nil; |
| 1346 | * otherwise, the metamethod will provide the result. |
| 1347 | * |
| 1348 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1349 | * the key does not appear in the table, but the metatable has an __index |
| 1350 | * property: |
| 1351 | * |
| 1352 | * - if the value is a function, the function is called, passing in the |
| 1353 | * table and the key; the return value of that function is returned as |
| 1354 | * the result. |
| 1355 | * |
| 1356 | * - if the value is another table, the value of the key in that table is |
| 1357 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1358 | * table's metatable has an __index property, then it continues on up) |
| 1359 | * |
| 1360 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1361 | * |
| 1362 | * http://www.lua.org/pil/13.4.1.html |
| 1363 | * |
| 1364 | * __newindex |
| 1365 | * |
| 1366 | * Like __index, but control property assignment. |
| 1367 | * |
| 1368 | * __mode - Control weak references. A string value with one or both |
| 1369 | * of the characters 'k' and 'v' which specifies that the the |
| 1370 | * keys and/or values in the table are weak references. |
| 1371 | * |
| 1372 | * __call - Treat a table like a function. When a table is followed by |
| 1373 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1374 | * a __call key pointing to a function, that function is invoked |
| 1375 | * (passing any specified arguments) and the return value is |
| 1376 | * returned. |
| 1377 | * |
| 1378 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1379 | * called, if the metatable for myTable has a __metatable |
| 1380 | * key, the value of that key is returned instead of the |
| 1381 | * actual metatable. |
| 1382 | * |
| 1383 | * __tostring - Control string representation. When the builtin |
| 1384 | * "tostring( myTable )" function is called, if the metatable |
| 1385 | * for myTable has a __tostring property set to a function, |
| 1386 | * that function is invoked (passing myTable to it) and the |
| 1387 | * return value is used as the string representation. |
| 1388 | * |
| 1389 | * __len - Control table length. When the table length is requested using |
| 1390 | * the length operator ( '#' ), if the metatable for myTable has |
| 1391 | * a __len key pointing to a function, that function is invoked |
| 1392 | * (passing myTable to it) and the return value used as the value |
| 1393 | * of "#myTable". |
| 1394 | * |
| 1395 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1396 | * collected, if the metatable has a __gc field pointing to a |
| 1397 | * function, that function is first invoked, passing the userdata |
| 1398 | * to it. The __gc metamethod is not called for tables. |
| 1399 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1400 | * |
| 1401 | * Special metamethods for redefining standard operators: |
| 1402 | * http://www.lua.org/pil/13.1.html |
| 1403 | * |
| 1404 | * __add "+" |
| 1405 | * __sub "-" |
| 1406 | * __mul "*" |
| 1407 | * __div "/" |
| 1408 | * __unm "!" |
| 1409 | * __pow "^" |
| 1410 | * __concat ".." |
| 1411 | * |
| 1412 | * Special methods for redfining standar relations |
| 1413 | * http://www.lua.org/pil/13.2.html |
| 1414 | * |
| 1415 | * __eq "==" |
| 1416 | * __lt "<" |
| 1417 | * __le "<=" |
| 1418 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1419 | |
| 1420 | /* |
| 1421 | * |
| 1422 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1423 | * Class Map |
| 1424 | * |
| 1425 | * |
| 1426 | */ |
| 1427 | |
| 1428 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1429 | * a class session, otherwise it throws an error. |
| 1430 | */ |
| 1431 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1432 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1433 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | /* This function is the map constructor. It don't need |
| 1437 | * the class Map object. It creates and return a new Map |
| 1438 | * object. It must be called only during "body" or "init" |
| 1439 | * context because it process some filesystem accesses. |
| 1440 | */ |
| 1441 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1442 | { |
| 1443 | const char *fn; |
| 1444 | int match = PAT_MATCH_STR; |
| 1445 | struct sample_conv conv; |
| 1446 | const char *file = ""; |
| 1447 | int line = 0; |
| 1448 | lua_Debug ar; |
| 1449 | char *err = NULL; |
| 1450 | struct arg args[2]; |
| 1451 | |
| 1452 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1453 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1454 | |
| 1455 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1456 | |
| 1457 | if (lua_gettop(L) >= 2) { |
| 1458 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1459 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1460 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1461 | } |
| 1462 | |
| 1463 | /* Get Lua filename and line number. */ |
| 1464 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1465 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1466 | if (ar.currentline > 0) { /* is there info? */ |
| 1467 | file = ar.short_src; |
| 1468 | line = ar.currentline; |
| 1469 | } |
| 1470 | } |
| 1471 | |
| 1472 | /* fill fake sample_conv struct. */ |
| 1473 | conv.kw = ""; /* unused. */ |
| 1474 | conv.process = NULL; /* unused. */ |
| 1475 | conv.arg_mask = 0; /* unused. */ |
| 1476 | conv.val_args = NULL; /* unused. */ |
| 1477 | conv.out_type = SMP_T_STR; |
| 1478 | conv.private = (void *)(long)match; |
| 1479 | switch (match) { |
| 1480 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1481 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1482 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1483 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1484 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1485 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1486 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1487 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1488 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1489 | default: |
| 1490 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1491 | } |
| 1492 | |
| 1493 | /* fill fake args. */ |
| 1494 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1495 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1496 | args[1].type = ARGT_STOP; |
| 1497 | |
| 1498 | /* load the map. */ |
| 1499 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1500 | /* error case: we cant use luaL_error because we must |
| 1501 | * free the err variable. |
| 1502 | */ |
| 1503 | luaL_where(L, 1); |
| 1504 | lua_pushfstring(L, "'new': %s.", err); |
| 1505 | lua_concat(L, 2); |
| 1506 | free(err); |
| 1507 | WILL_LJMP(lua_error(L)); |
| 1508 | } |
| 1509 | |
| 1510 | /* create the lua object. */ |
| 1511 | lua_newtable(L); |
| 1512 | lua_pushlightuserdata(L, args[0].data.map); |
| 1513 | lua_rawseti(L, -2, 0); |
| 1514 | |
| 1515 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1516 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1517 | lua_setmetatable(L, -2); |
| 1518 | |
| 1519 | |
| 1520 | return 1; |
| 1521 | } |
| 1522 | |
| 1523 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1524 | { |
| 1525 | struct map_descriptor *desc; |
| 1526 | struct pattern *pat; |
| 1527 | struct sample smp; |
| 1528 | |
| 1529 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1530 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1531 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1532 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1533 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1534 | } |
| 1535 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1536 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1537 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1538 | 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] | 1539 | } |
| 1540 | |
| 1541 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1542 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1543 | if (str) |
| 1544 | lua_pushstring(L, ""); |
| 1545 | else |
| 1546 | lua_pushnil(L); |
| 1547 | return 1; |
| 1548 | } |
| 1549 | |
| 1550 | /* 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] | 1551 | 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] | 1552 | return 1; |
| 1553 | } |
| 1554 | |
| 1555 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1556 | { |
| 1557 | return _hlua_map_lookup(L, 0); |
| 1558 | } |
| 1559 | |
| 1560 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1561 | { |
| 1562 | return _hlua_map_lookup(L, 1); |
| 1563 | } |
| 1564 | |
| 1565 | /* |
| 1566 | * |
| 1567 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1568 | * Class Socket |
| 1569 | * |
| 1570 | * |
| 1571 | */ |
| 1572 | |
| 1573 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1574 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1575 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | /* 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] | 1579 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1580 | * received. |
| 1581 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1582 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1583 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1584 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1585 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1586 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1587 | if (appctx->ctx.hlua_cosocket.die) { |
| 1588 | si_shutw(si); |
| 1589 | si_shutr(si); |
| 1590 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1591 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1592 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1593 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1594 | } |
| 1595 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1596 | /* If the connection object is not available, close all the |
| 1597 | * streams and wakeup everything waiting for. |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1598 | */ |
| 1599 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1600 | si_shutw(si); |
| 1601 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1602 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1603 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1604 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1605 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1606 | } |
| 1607 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1608 | /* If we cant write, wakeup the pending write signals. */ |
| 1609 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1610 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1611 | |
| 1612 | /* If we cant read, wakeup the pending read signals. */ |
| 1613 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1614 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1615 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1616 | /* if the connection is not estabkished, inform the stream that we want |
| 1617 | * to be notified whenever the connection completes. |
| 1618 | */ |
| 1619 | if (!(c->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1620 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1621 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1622 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1623 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1624 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1625 | |
| 1626 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1627 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1628 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1629 | /* 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] | 1630 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1631 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1632 | |
| 1633 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1634 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1635 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1636 | |
| 1637 | /* Some data were injected in the buffer, notify the stream |
| 1638 | * interface. |
| 1639 | */ |
| 1640 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1641 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1642 | |
| 1643 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1644 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1645 | */ |
| 1646 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1647 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1648 | } |
| 1649 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1650 | /* This function is called when the "struct stream" is destroyed. |
| 1651 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1652 | * Wake all the pending signals. |
| 1653 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1654 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1655 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1656 | struct xref *peer; |
| 1657 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1658 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1659 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1660 | if (peer) |
| 1661 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1662 | |
| 1663 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1664 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1665 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1666 | } |
| 1667 | |
| 1668 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1669 | * uses this object. If the stream does not exists, just quit. |
| 1670 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1671 | * pending signal can rest in the read and write lists. destroy |
| 1672 | * it. |
| 1673 | */ |
| 1674 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1675 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1676 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1677 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1678 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1679 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1680 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1681 | |
| 1682 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1683 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1684 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1685 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1686 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1687 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1688 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1689 | appctx->ctx.hlua_cosocket.die = 1; |
| 1690 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1691 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1692 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1693 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1694 | return 0; |
| 1695 | } |
| 1696 | |
| 1697 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1698 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1699 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1700 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1701 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1702 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1703 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1704 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1705 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1706 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1707 | |
| 1708 | /* Check if we run on the same thread than the xreator thread. |
| 1709 | * We cannot access to the socket if the thread is different. |
| 1710 | */ |
| 1711 | if (socket->tid != tid) |
| 1712 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1713 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1714 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1715 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1716 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1717 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1718 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1719 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1720 | appctx->ctx.hlua_cosocket.die = 1; |
| 1721 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1722 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1723 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1724 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1725 | return 0; |
| 1726 | } |
| 1727 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1728 | /* The close function calls close_helper. |
| 1729 | */ |
| 1730 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1731 | { |
| 1732 | MAY_LJMP(check_args(L, 1, "close")); |
| 1733 | return hlua_socket_close_helper(L); |
| 1734 | } |
| 1735 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1736 | /* This Lua function assumes that the stack contain three parameters. |
| 1737 | * 1 - USERDATA containing a struct socket |
| 1738 | * 2 - INTEGER with values of the macro defined below |
| 1739 | * If the integer is -1, we must read at most one line. |
| 1740 | * If the integer is -2, we ust read all the data until the |
| 1741 | * end of the stream. |
| 1742 | * If the integer is positive value, we must read a number of |
| 1743 | * bytes corresponding to this value. |
| 1744 | */ |
| 1745 | #define HLSR_READ_LINE (-1) |
| 1746 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1747 | __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] | 1748 | { |
| 1749 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1750 | int wanted = lua_tointeger(L, 2); |
| 1751 | struct hlua *hlua = hlua_gethlua(L); |
| 1752 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1753 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1754 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1755 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1756 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1757 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1758 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1759 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1760 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1761 | struct stream_interface *si; |
| 1762 | struct stream *s; |
| 1763 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1764 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1765 | |
| 1766 | /* Check if this lua stack is schedulable. */ |
| 1767 | if (!hlua || !hlua->task) |
| 1768 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1769 | "'frontend', 'backend' or 'task'")); |
| 1770 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1771 | /* Check if we run on the same thread than the xreator thread. |
| 1772 | * We cannot access to the socket if the thread is different. |
| 1773 | */ |
| 1774 | if (socket->tid != tid) |
| 1775 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1776 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1777 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1778 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1779 | if (!peer) |
| 1780 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1781 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1782 | si = appctx->owner; |
| 1783 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1784 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1785 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1786 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1787 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1788 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1789 | if (nblk < 0) /* Connection close. */ |
| 1790 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1791 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1792 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1793 | |
| 1794 | /* remove final \r\n. */ |
| 1795 | if (nblk == 1) { |
| 1796 | if (blk1[len1-1] == '\n') { |
| 1797 | len1--; |
| 1798 | skip_at_end++; |
| 1799 | if (blk1[len1-1] == '\r') { |
| 1800 | len1--; |
| 1801 | skip_at_end++; |
| 1802 | } |
| 1803 | } |
| 1804 | } |
| 1805 | else { |
| 1806 | if (blk2[len2-1] == '\n') { |
| 1807 | len2--; |
| 1808 | skip_at_end++; |
| 1809 | if (blk2[len2-1] == '\r') { |
| 1810 | len2--; |
| 1811 | skip_at_end++; |
| 1812 | } |
| 1813 | } |
| 1814 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1815 | } |
| 1816 | |
| 1817 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1818 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1819 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1820 | if (nblk < 0) /* Connection close. */ |
| 1821 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1822 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1823 | goto connection_empty; |
| 1824 | } |
| 1825 | |
| 1826 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1827 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1828 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1829 | if (nblk < 0) /* Connection close. */ |
| 1830 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1831 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1832 | goto connection_empty; |
| 1833 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1834 | missing_bytes = wanted - socket->b.n; |
| 1835 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1836 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1837 | len1 = missing_bytes; |
| 1838 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1839 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1840 | } |
| 1841 | |
| 1842 | len = len1; |
| 1843 | |
| 1844 | luaL_addlstring(&socket->b, blk1, len1); |
| 1845 | if (nblk == 2) { |
| 1846 | len += len2; |
| 1847 | luaL_addlstring(&socket->b, blk2, len2); |
| 1848 | } |
| 1849 | |
| 1850 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1851 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1852 | |
| 1853 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1854 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1855 | |
| 1856 | /* If the pattern reclaim to read all the data |
| 1857 | * in the connection, got out. |
| 1858 | */ |
| 1859 | if (wanted == HLSR_READ_ALL) |
| 1860 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1861 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1862 | goto connection_empty; |
| 1863 | |
| 1864 | /* Return result. */ |
| 1865 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1866 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1867 | return 1; |
| 1868 | |
| 1869 | connection_closed: |
| 1870 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1871 | xref_unlock(&socket->xref, peer); |
| 1872 | |
| 1873 | no_peer: |
| 1874 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1875 | /* If the buffer containds data. */ |
| 1876 | if (socket->b.n > 0) { |
| 1877 | luaL_pushresult(&socket->b); |
| 1878 | return 1; |
| 1879 | } |
| 1880 | lua_pushnil(L); |
| 1881 | lua_pushstring(L, "connection closed."); |
| 1882 | return 2; |
| 1883 | |
| 1884 | connection_empty: |
| 1885 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1886 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1887 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1888 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1889 | } |
| 1890 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1891 | 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] | 1892 | return 0; |
| 1893 | } |
| 1894 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1895 | /* This Lua function gets two parameters. The first one can be string |
| 1896 | * or a number. If the string is "*l", the user requires one line. If |
| 1897 | * 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] | 1898 | * If the value is a number, the user require a number of bytes equal |
| 1899 | * to the value. The default value is "*l" (a line). |
| 1900 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1901 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1902 | * integer takes this values: |
| 1903 | * -1 : read a line |
| 1904 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1905 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1906 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1907 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1908 | * concatenated with the read data. |
| 1909 | */ |
| 1910 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1911 | { |
| 1912 | int wanted = HLSR_READ_LINE; |
| 1913 | const char *pattern; |
| 1914 | int type; |
| 1915 | char *error; |
| 1916 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1917 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1918 | |
| 1919 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1920 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1921 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1922 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1923 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1924 | /* Check if we run on the same thread than the xreator thread. |
| 1925 | * We cannot access to the socket if the thread is different. |
| 1926 | */ |
| 1927 | if (socket->tid != tid) |
| 1928 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1929 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1930 | /* check for pattern. */ |
| 1931 | if (lua_gettop(L) >= 2) { |
| 1932 | type = lua_type(L, 2); |
| 1933 | if (type == LUA_TSTRING) { |
| 1934 | pattern = lua_tostring(L, 2); |
| 1935 | if (strcmp(pattern, "*a") == 0) |
| 1936 | wanted = HLSR_READ_ALL; |
| 1937 | else if (strcmp(pattern, "*l") == 0) |
| 1938 | wanted = HLSR_READ_LINE; |
| 1939 | else { |
| 1940 | wanted = strtoll(pattern, &error, 10); |
| 1941 | if (*error != '\0') |
| 1942 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1943 | } |
| 1944 | } |
| 1945 | else if (type == LUA_TNUMBER) { |
| 1946 | wanted = lua_tointeger(L, 2); |
| 1947 | if (wanted < 0) |
| 1948 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1949 | } |
| 1950 | } |
| 1951 | |
| 1952 | /* Set pattern. */ |
| 1953 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1954 | |
| 1955 | /* Check if we would replace the top by itself. */ |
| 1956 | if (lua_gettop(L) != 2) |
| 1957 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1958 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1959 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1960 | luaL_buffinit(L, &socket->b); |
| 1961 | |
| 1962 | /* Check prefix. */ |
| 1963 | if (lua_gettop(L) >= 3) { |
| 1964 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1965 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1966 | pattern = lua_tolstring(L, 3, &len); |
| 1967 | luaL_addlstring(&socket->b, pattern, len); |
| 1968 | } |
| 1969 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1970 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1974 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1975 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1976 | 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] | 1977 | { |
| 1978 | struct hlua_socket *socket; |
| 1979 | struct hlua *hlua = hlua_gethlua(L); |
| 1980 | struct appctx *appctx; |
| 1981 | size_t buf_len; |
| 1982 | const char *buf; |
| 1983 | int len; |
| 1984 | int send_len; |
| 1985 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1986 | struct xref *peer; |
| 1987 | struct stream_interface *si; |
| 1988 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1989 | |
| 1990 | /* Check if this lua stack is schedulable. */ |
| 1991 | if (!hlua || !hlua->task) |
| 1992 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1993 | "'frontend', 'backend' or 'task'")); |
| 1994 | |
| 1995 | /* Get object */ |
| 1996 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1997 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1998 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1999 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2000 | /* Check if we run on the same thread than the xreator thread. |
| 2001 | * We cannot access to the socket if the thread is different. |
| 2002 | */ |
| 2003 | if (socket->tid != tid) |
| 2004 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2005 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2006 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2007 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2008 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2009 | lua_pushinteger(L, -1); |
| 2010 | return 1; |
| 2011 | } |
| 2012 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2013 | si = appctx->owner; |
| 2014 | s = si_strm(si); |
| 2015 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2016 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2017 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2018 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2019 | lua_pushinteger(L, -1); |
| 2020 | return 1; |
| 2021 | } |
| 2022 | |
| 2023 | /* Update the input buffer data. */ |
| 2024 | buf += sent; |
| 2025 | send_len = buf_len - sent; |
| 2026 | |
| 2027 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2028 | if (sent >= buf_len) { |
| 2029 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2030 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2031 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2032 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2033 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2034 | * the request buffer if its not required. |
| 2035 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2036 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2037 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2038 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2039 | } |
| 2040 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2041 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2042 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2043 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2044 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2045 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2046 | |
| 2047 | /* send data */ |
| 2048 | if (len < send_len) |
| 2049 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2050 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2051 | |
| 2052 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2053 | * the data" (-2) are not expected because the available length |
| 2054 | * is tested. |
| 2055 | * Other unknown error are also not expected. |
| 2056 | */ |
| 2057 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2058 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2059 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2060 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2061 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2062 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2063 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2064 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2065 | return 1; |
| 2066 | } |
| 2067 | |
| 2068 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2069 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2070 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2071 | s->req.rex = TICK_ETERNITY; |
| 2072 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2073 | |
| 2074 | /* Update length sent. */ |
| 2075 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2076 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2077 | |
| 2078 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2079 | if (sent + len >= buf_len) { |
| 2080 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2081 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2082 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2083 | |
| 2084 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2085 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2086 | xref_unlock(&socket->xref, peer); |
| 2087 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2088 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2089 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2090 | 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] | 2091 | return 0; |
| 2092 | } |
| 2093 | |
| 2094 | /* This function initiate the send of data. It just check the input |
| 2095 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2096 | * 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] | 2097 | * "hlua_socket_write_yield" that can yield. |
| 2098 | * |
| 2099 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2100 | * the associated object. The second is a string buffer. The third is |
| 2101 | * a facultative integer that represents where is the buffer position |
| 2102 | * of the start of the data that can send. The first byte is the |
| 2103 | * position "1". The default value is "1". The fourth argument is a |
| 2104 | * facultative integer that represents where is the buffer position |
| 2105 | * of the end of the data that can send. The default is the last byte. |
| 2106 | */ |
| 2107 | static int hlua_socket_send(struct lua_State *L) |
| 2108 | { |
| 2109 | int i; |
| 2110 | int j; |
| 2111 | const char *buf; |
| 2112 | size_t buf_len; |
| 2113 | |
| 2114 | /* Check number of arguments. */ |
| 2115 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2116 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2117 | |
| 2118 | /* Get the string. */ |
| 2119 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2120 | |
| 2121 | /* Get and check j. */ |
| 2122 | if (lua_gettop(L) == 4) { |
| 2123 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2124 | if (j < 0) |
| 2125 | j = buf_len + j + 1; |
| 2126 | if (j > buf_len) |
| 2127 | j = buf_len + 1; |
| 2128 | lua_pop(L, 1); |
| 2129 | } |
| 2130 | else |
| 2131 | j = buf_len; |
| 2132 | |
| 2133 | /* Get and check i. */ |
| 2134 | if (lua_gettop(L) == 3) { |
| 2135 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2136 | if (i < 0) |
| 2137 | i = buf_len + i + 1; |
| 2138 | if (i > buf_len) |
| 2139 | i = buf_len + 1; |
| 2140 | lua_pop(L, 1); |
| 2141 | } else |
| 2142 | i = 1; |
| 2143 | |
| 2144 | /* Check bth i and j. */ |
| 2145 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2146 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2147 | return 1; |
| 2148 | } |
| 2149 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2150 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2151 | return 1; |
| 2152 | } |
| 2153 | if (i == 0) |
| 2154 | i = 1; |
| 2155 | if (j == 0) |
| 2156 | j = 1; |
| 2157 | |
| 2158 | /* Pop the string. */ |
| 2159 | lua_pop(L, 1); |
| 2160 | |
| 2161 | /* Update the buffer length. */ |
| 2162 | buf += i - 1; |
| 2163 | buf_len = j - i + 1; |
| 2164 | lua_pushlstring(L, buf, buf_len); |
| 2165 | |
| 2166 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2167 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2168 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2169 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2170 | } |
| 2171 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2172 | #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] | 2173 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2174 | { |
| 2175 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2176 | int ret; |
| 2177 | int len; |
| 2178 | char *p; |
| 2179 | |
| 2180 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2181 | if (ret <= 0) { |
| 2182 | lua_pushnil(L); |
| 2183 | return 1; |
| 2184 | } |
| 2185 | |
| 2186 | if (ret == AF_UNIX) { |
| 2187 | lua_pushstring(L, buffer+1); |
| 2188 | return 1; |
| 2189 | } |
| 2190 | else if (ret == AF_INET6) { |
| 2191 | buffer[0] = '['; |
| 2192 | len = strlen(buffer); |
| 2193 | buffer[len] = ']'; |
| 2194 | len++; |
| 2195 | buffer[len] = ':'; |
| 2196 | len++; |
| 2197 | p = buffer; |
| 2198 | } |
| 2199 | else if (ret == AF_INET) { |
| 2200 | p = buffer + 1; |
| 2201 | len = strlen(p); |
| 2202 | p[len] = ':'; |
| 2203 | len++; |
| 2204 | } |
| 2205 | else { |
| 2206 | lua_pushnil(L); |
| 2207 | return 1; |
| 2208 | } |
| 2209 | |
| 2210 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2211 | lua_pushnil(L); |
| 2212 | return 1; |
| 2213 | } |
| 2214 | |
| 2215 | lua_pushstring(L, p); |
| 2216 | return 1; |
| 2217 | } |
| 2218 | |
| 2219 | /* Returns information about the peer of the connection. */ |
| 2220 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2221 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2222 | struct hlua_socket *socket; |
| 2223 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2224 | struct xref *peer; |
| 2225 | struct appctx *appctx; |
| 2226 | struct stream_interface *si; |
| 2227 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2228 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2229 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2230 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2231 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2232 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2233 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2234 | /* Check if we run on the same thread than the xreator thread. |
| 2235 | * We cannot access to the socket if the thread is different. |
| 2236 | */ |
| 2237 | if (socket->tid != tid) |
| 2238 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2239 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2240 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2241 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2242 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2243 | lua_pushnil(L); |
| 2244 | return 1; |
| 2245 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2246 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2247 | si = appctx->owner; |
| 2248 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2249 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2250 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2251 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2252 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2253 | lua_pushnil(L); |
| 2254 | return 1; |
| 2255 | } |
| 2256 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2257 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2258 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2259 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2260 | lua_pushnil(L); |
| 2261 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2262 | } |
| 2263 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2264 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2265 | xref_unlock(&socket->xref, peer); |
| 2266 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2267 | } |
| 2268 | |
| 2269 | /* Returns information about my connection side. */ |
| 2270 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2271 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2272 | struct hlua_socket *socket; |
| 2273 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2274 | struct appctx *appctx; |
| 2275 | struct xref *peer; |
| 2276 | struct stream_interface *si; |
| 2277 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2278 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2279 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2280 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2281 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2282 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2283 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2284 | /* Check if we run on the same thread than the xreator thread. |
| 2285 | * We cannot access to the socket if the thread is different. |
| 2286 | */ |
| 2287 | if (socket->tid != tid) |
| 2288 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2289 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2290 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2291 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2292 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | lua_pushnil(L); |
| 2294 | return 1; |
| 2295 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2296 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2297 | si = appctx->owner; |
| 2298 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2299 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2300 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2301 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2302 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2303 | lua_pushnil(L); |
| 2304 | return 1; |
| 2305 | } |
| 2306 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2307 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2308 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2309 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2310 | lua_pushnil(L); |
| 2311 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2312 | } |
| 2313 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2314 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2315 | xref_unlock(&socket->xref, peer); |
| 2316 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2320 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | .obj_type = OBJ_TYPE_APPLET, |
| 2322 | .name = "<LUA_TCP>", |
| 2323 | .fct = hlua_socket_handler, |
| 2324 | .release = hlua_socket_release, |
| 2325 | }; |
| 2326 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2327 | __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] | 2328 | { |
| 2329 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2330 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2331 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2332 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2333 | struct stream_interface *si; |
| 2334 | struct stream *s; |
| 2335 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2336 | /* Check if we run on the same thread than the xreator thread. |
| 2337 | * We cannot access to the socket if the thread is different. |
| 2338 | */ |
| 2339 | if (socket->tid != tid) |
| 2340 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2341 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2342 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2343 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2344 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2345 | lua_pushnil(L); |
| 2346 | lua_pushstring(L, "Can't connect"); |
| 2347 | return 2; |
| 2348 | } |
| 2349 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2350 | si = appctx->owner; |
| 2351 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2352 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2353 | /* Check if we run on the same thread than the xreator thread. |
| 2354 | * We cannot access to the socket if the thread is different. |
| 2355 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2356 | if (socket->tid != tid) { |
| 2357 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2358 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2359 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2360 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2361 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2362 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2363 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2364 | lua_pushnil(L); |
| 2365 | lua_pushstring(L, "Can't connect"); |
| 2366 | return 2; |
| 2367 | } |
| 2368 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2369 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2370 | |
| 2371 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2372 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2373 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2374 | lua_pushinteger(L, 1); |
| 2375 | return 1; |
| 2376 | } |
| 2377 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2378 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2379 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2380 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2381 | } |
| 2382 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2383 | 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] | 2384 | return 0; |
| 2385 | } |
| 2386 | |
| 2387 | /* This function fail or initite the connection. */ |
| 2388 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2389 | { |
| 2390 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2391 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2392 | const char *ip; |
| 2393 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2394 | struct hlua *hlua; |
| 2395 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2396 | int low, high; |
| 2397 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2398 | struct xref *peer; |
| 2399 | struct stream_interface *si; |
| 2400 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2401 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2402 | if (lua_gettop(L) < 2) |
| 2403 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2404 | |
| 2405 | /* Get args. */ |
| 2406 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2407 | |
| 2408 | /* Check if we run on the same thread than the xreator thread. |
| 2409 | * We cannot access to the socket if the thread is different. |
| 2410 | */ |
| 2411 | if (socket->tid != tid) |
| 2412 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2413 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2414 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2415 | if (lua_gettop(L) >= 3) { |
| 2416 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2417 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2418 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2419 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2420 | * that are not enclosed within square brackets. |
| 2421 | */ |
| 2422 | if (port > 0) { |
| 2423 | luaL_buffinit(L, &b); |
| 2424 | luaL_addstring(&b, ip); |
| 2425 | luaL_addchar(&b, ':'); |
| 2426 | luaL_pushresult(&b); |
| 2427 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2428 | } |
| 2429 | } |
| 2430 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2431 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2432 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2433 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2434 | lua_pushnil(L); |
| 2435 | return 1; |
| 2436 | } |
| 2437 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2438 | si = appctx->owner; |
| 2439 | s = si_strm(si); |
| 2440 | |
| 2441 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2442 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2443 | if (!conn) { |
| 2444 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2445 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2446 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2447 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2448 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2449 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2450 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2451 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2452 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2453 | if (!addr) { |
| 2454 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2455 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2456 | } |
| 2457 | if (low != high) { |
| 2458 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2459 | 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] | 2460 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2461 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2462 | |
| 2463 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2464 | if (low == 0) { |
| 2465 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2466 | if (port == -1) { |
| 2467 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2468 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2469 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2470 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2471 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2472 | if (port == -1) { |
| 2473 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2474 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2475 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2476 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2477 | } |
| 2478 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2479 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2480 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2481 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2482 | |
| 2483 | /* inform the stream that we want to be notified whenever the |
| 2484 | * connection completes. |
| 2485 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2486 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2487 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2488 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2489 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2490 | hlua->flags |= HLUA_MUST_GC; |
| 2491 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2492 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2493 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2494 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2495 | } |
| 2496 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2497 | |
| 2498 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2499 | /* Return yield waiting for connection. */ |
| 2500 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2501 | 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] | 2502 | |
| 2503 | return 0; |
| 2504 | } |
| 2505 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2506 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2507 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2508 | { |
| 2509 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2510 | struct xref *peer; |
| 2511 | struct appctx *appctx; |
| 2512 | struct stream_interface *si; |
| 2513 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2514 | |
| 2515 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2516 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2517 | |
| 2518 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2519 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2520 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2521 | lua_pushnil(L); |
| 2522 | return 1; |
| 2523 | } |
| 2524 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2525 | si = appctx->owner; |
| 2526 | s = si_strm(si); |
| 2527 | |
| 2528 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2529 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2530 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2531 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2532 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2533 | |
| 2534 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2535 | { |
| 2536 | return 0; |
| 2537 | } |
| 2538 | |
| 2539 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2540 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2541 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2542 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2543 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2544 | struct xref *peer; |
| 2545 | struct appctx *appctx; |
| 2546 | struct stream_interface *si; |
| 2547 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2548 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2549 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2550 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2551 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2552 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2553 | /* convert the timeout to millis */ |
| 2554 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2555 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2556 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2557 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2558 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2559 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2560 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2561 | 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] | 2562 | |
| 2563 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2564 | if (tmout == 0) |
| 2565 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2566 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2567 | /* Check if we run on the same thread than the xreator thread. |
| 2568 | * We cannot access to the socket if the thread is different. |
| 2569 | */ |
| 2570 | if (socket->tid != tid) |
| 2571 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2572 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2573 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2574 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2575 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2576 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2577 | WILL_LJMP(lua_error(L)); |
| 2578 | return 0; |
| 2579 | } |
| 2580 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2581 | si = appctx->owner; |
| 2582 | s = si_strm(si); |
| 2583 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2584 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2585 | s->req.rto = tmout; |
| 2586 | s->req.wto = tmout; |
| 2587 | s->res.rto = tmout; |
| 2588 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2589 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2590 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2591 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2592 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2593 | |
| 2594 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2595 | task_queue(s->task); |
| 2596 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2597 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2598 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2599 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2600 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2604 | { |
| 2605 | struct hlua_socket *socket; |
| 2606 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2607 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2608 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2609 | |
| 2610 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2611 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2612 | hlua_pusherror(L, "socket: full stack"); |
| 2613 | goto out_fail_conf; |
| 2614 | } |
| 2615 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2616 | /* Create the object: obj[0] = userdata. */ |
| 2617 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2618 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2619 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2620 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2621 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2622 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2623 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2624 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2625 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2626 | goto out_fail_conf; |
| 2627 | } |
| 2628 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2629 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2630 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2631 | lua_setmetatable(L, -2); |
| 2632 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2633 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2634 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2635 | if (!appctx) { |
| 2636 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2637 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2638 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2639 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2640 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2641 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2642 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2643 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2644 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2645 | /* Now create a session, task and stream for this applet */ |
| 2646 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2647 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2648 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2649 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2650 | } |
| 2651 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2652 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2653 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2654 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2655 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2656 | } |
| 2657 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2658 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2659 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2660 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2661 | /* Configure "right" stream interface. this "si" is used to connect |
| 2662 | * and retrieve data from the server. The connection is initialized |
| 2663 | * with the "struct server". |
| 2664 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2665 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2666 | |
| 2667 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2668 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2669 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2670 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2671 | return 1; |
| 2672 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2673 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2674 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2675 | out_fail_sess: |
| 2676 | appctx_free(appctx); |
| 2677 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2678 | WILL_LJMP(lua_error(L)); |
| 2679 | return 0; |
| 2680 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2681 | |
| 2682 | /* |
| 2683 | * |
| 2684 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2685 | * Class Channel |
| 2686 | * |
| 2687 | * |
| 2688 | */ |
| 2689 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2690 | /* This function is called before the Lua execution. It stores |
| 2691 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2692 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2693 | 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] | 2694 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2695 | c->mode = stream->be->mode; |
| 2696 | switch (c->mode) { |
| 2697 | case PR_MODE_HTTP: |
| 2698 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2699 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2700 | c->data.http.state = stream->txn->req.msg_state; |
| 2701 | else |
| 2702 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2703 | break; |
| 2704 | default: |
| 2705 | break; |
| 2706 | } |
| 2707 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2708 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2709 | /* This function is called after the Lua execution. it |
| 2710 | * returns true if the parser state is consistent, otherwise, |
| 2711 | * it return false. |
| 2712 | * |
| 2713 | * In HTTP mode, the parser state must be in the same state |
| 2714 | * or greater when we exit the function. Even if we do a |
| 2715 | * control yield. This prevent to break the HTTP message |
| 2716 | * from the Lua code. |
| 2717 | */ |
| 2718 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2719 | { |
| 2720 | if (c->mode != stream->be->mode) |
| 2721 | return 0; |
| 2722 | |
| 2723 | switch (c->mode) { |
| 2724 | case PR_MODE_HTTP: |
| 2725 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2726 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2727 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2728 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2729 | else |
| 2730 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2731 | default: |
| 2732 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2733 | } |
| 2734 | return 1; |
| 2735 | } |
| 2736 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2737 | /* Returns the struct hlua_channel join to the class channel in the |
| 2738 | * stack entry "ud" or throws an argument error. |
| 2739 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2740 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2741 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2742 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2743 | } |
| 2744 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2745 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2746 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2747 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2748 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2749 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2750 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2751 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2752 | return 0; |
| 2753 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2754 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2755 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2756 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2757 | |
| 2758 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2759 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2760 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2761 | return 1; |
| 2762 | } |
| 2763 | |
| 2764 | /* Duplicate all the data present in the input channel and put it |
| 2765 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2766 | * the stack if the channel is closed and all the data are consumed, |
| 2767 | * returns 0 if no data are available, otherwise it returns the length |
| 2768 | * of the builded string. |
| 2769 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2770 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2771 | { |
| 2772 | char *blk1; |
| 2773 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2774 | size_t len1; |
| 2775 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2776 | int ret; |
| 2777 | luaL_Buffer b; |
| 2778 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2779 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2780 | if (unlikely(ret == 0)) |
| 2781 | return 0; |
| 2782 | |
| 2783 | if (unlikely(ret < 0)) { |
| 2784 | lua_pushnil(L); |
| 2785 | return -1; |
| 2786 | } |
| 2787 | |
| 2788 | luaL_buffinit(L, &b); |
| 2789 | luaL_addlstring(&b, blk1, len1); |
| 2790 | if (unlikely(ret == 2)) |
| 2791 | luaL_addlstring(&b, blk2, len2); |
| 2792 | luaL_pushresult(&b); |
| 2793 | |
| 2794 | if (unlikely(ret == 2)) |
| 2795 | return len1 + len2; |
| 2796 | return len1; |
| 2797 | } |
| 2798 | |
| 2799 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2800 | * a yield. This function keep the data in the buffer. |
| 2801 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2802 | __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] | 2803 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2804 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2805 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2806 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2807 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2808 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2809 | WILL_LJMP(lua_error(L)); |
| 2810 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2811 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2812 | 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] | 2813 | return 1; |
| 2814 | } |
| 2815 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2816 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2817 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2818 | { |
| 2819 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2820 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2821 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2822 | } |
| 2823 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2824 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2825 | * a yield. This function consumes the data in the buffer. It returns |
| 2826 | * a string containing the data or a nil pointer if no data are available |
| 2827 | * and the channel is closed. |
| 2828 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2829 | __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] | 2830 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2831 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2832 | int ret; |
| 2833 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2834 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2835 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2836 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2837 | WILL_LJMP(lua_error(L)); |
| 2838 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2839 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2840 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2841 | 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] | 2842 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2843 | if (unlikely(ret == -1)) |
| 2844 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2845 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2846 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2847 | return 1; |
| 2848 | } |
| 2849 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2850 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2851 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2852 | { |
| 2853 | MAY_LJMP(check_args(L, 1, "get")); |
| 2854 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2855 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2856 | } |
| 2857 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2858 | /* This functions consumes and returns one line. If the channel is closed, |
| 2859 | * 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] | 2860 | * 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] | 2861 | * value. |
| 2862 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2863 | __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] | 2864 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2865 | char *blk1; |
| 2866 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2867 | size_t len1; |
| 2868 | size_t len2; |
| 2869 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2870 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2871 | int ret; |
| 2872 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2873 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2874 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2875 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2876 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2877 | WILL_LJMP(lua_error(L)); |
| 2878 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2879 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2880 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2881 | 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] | 2882 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2883 | if (ret == -1) { |
| 2884 | lua_pushnil(L); |
| 2885 | return 1; |
| 2886 | } |
| 2887 | |
| 2888 | luaL_buffinit(L, &b); |
| 2889 | luaL_addlstring(&b, blk1, len1); |
| 2890 | len = len1; |
| 2891 | if (unlikely(ret == 2)) { |
| 2892 | luaL_addlstring(&b, blk2, len2); |
| 2893 | len += len2; |
| 2894 | } |
| 2895 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2896 | 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] | 2897 | return 1; |
| 2898 | } |
| 2899 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2900 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2901 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2902 | { |
| 2903 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2904 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2905 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2906 | } |
| 2907 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2908 | /* This function takes a string as input, and append it at the |
| 2909 | * input side of channel. If the data is too big, but a space |
| 2910 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2911 | * yields. If the data is bigger than the buffer, or if the |
| 2912 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2913 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2914 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2915 | __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] | 2916 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2917 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2918 | size_t len; |
| 2919 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2920 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2921 | int ret; |
| 2922 | int max; |
| 2923 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2924 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2925 | WILL_LJMP(lua_error(L)); |
| 2926 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2927 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2928 | * the request buffer if its not required. |
| 2929 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2930 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2931 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2932 | 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] | 2933 | } |
| 2934 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2935 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2936 | if (max > len - l) |
| 2937 | max = len - l; |
| 2938 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2939 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2940 | if (ret == -2 || ret == -3) { |
| 2941 | lua_pushinteger(L, -1); |
| 2942 | return 1; |
| 2943 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2944 | if (ret == -1) { |
| 2945 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2946 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2947 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2948 | l += ret; |
| 2949 | lua_pop(L, 1); |
| 2950 | lua_pushinteger(L, l); |
| 2951 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2952 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2953 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2954 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2955 | * in this case, we cannot add more data, so we cannot yield, |
| 2956 | * we return the amount of copyied data. |
| 2957 | */ |
| 2958 | return 1; |
| 2959 | } |
| 2960 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2961 | 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] | 2962 | return 1; |
| 2963 | } |
| 2964 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2965 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2966 | * 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] | 2967 | * buffer size is too little for the data. |
| 2968 | */ |
| 2969 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2970 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2971 | size_t len; |
| 2972 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2973 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2974 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2975 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2976 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2977 | lua_pushinteger(L, 0); |
| 2978 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2979 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2980 | } |
| 2981 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2982 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2983 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2984 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2985 | * or -1 if the channel is closed or if the buffer size is too |
| 2986 | * little for the data. |
| 2987 | */ |
| 2988 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2989 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2990 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2991 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2992 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2993 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2994 | lua_pushinteger(L, 0); |
| 2995 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2996 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2997 | WILL_LJMP(lua_error(L)); |
| 2998 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2999 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3000 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3001 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3002 | } |
| 3003 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3004 | /* Append data in the output side of the buffer. This data is immediately |
| 3005 | * sent. The function returns the amount of data written. If the buffer |
| 3006 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3007 | * if the channel is closed. |
| 3008 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3009 | __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] | 3010 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3011 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3012 | size_t len; |
| 3013 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3014 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3015 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3016 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3017 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3018 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3019 | WILL_LJMP(lua_error(L)); |
| 3020 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3021 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3022 | lua_pushinteger(L, -1); |
| 3023 | return 1; |
| 3024 | } |
| 3025 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3026 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3027 | * the request buffer if its not required. |
| 3028 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3029 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3030 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3031 | 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] | 3032 | } |
| 3033 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3034 | /* The written data will be immediately sent, so we can check |
| 3035 | * the available space without taking in account the reserve. |
| 3036 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3037 | * data, because the buffer will be flushed. |
| 3038 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3039 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3040 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3041 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3042 | * in this case, we cannot add more data, so we cannot yield, |
| 3043 | * we return the amount of copyied data. |
| 3044 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3045 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3046 | return 1; |
| 3047 | |
| 3048 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3049 | if (max > len - l) |
| 3050 | max = len - l; |
| 3051 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3052 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3053 | * detects a non contiguous buffer and realign it. |
| 3054 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3055 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3056 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3057 | |
| 3058 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3059 | 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] | 3060 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3061 | /* buffer replace considers that the input part is filled. |
| 3062 | * so, I must forward these new data in the output part. |
| 3063 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3064 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3065 | |
| 3066 | l += max; |
| 3067 | lua_pop(L, 1); |
| 3068 | lua_pushinteger(L, l); |
| 3069 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3070 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3071 | * in this case, we cannot add more data, so we cannot yield, |
| 3072 | * we return the amount of copyied data. |
| 3073 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3074 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3075 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3076 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3077 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3078 | if (l < len) { |
| 3079 | /* If we are waiting for space in the response buffer, we |
| 3080 | * must set the flag WAKERESWR. This flag required the task |
| 3081 | * wake up if any activity is detected on the response buffer. |
| 3082 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3083 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3084 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3085 | else |
| 3086 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3087 | 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] | 3088 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3089 | |
| 3090 | return 1; |
| 3091 | } |
| 3092 | |
| 3093 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3094 | * yield the LUA process, and resume it without checking the |
| 3095 | * input arguments. |
| 3096 | */ |
| 3097 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3098 | { |
| 3099 | MAY_LJMP(check_args(L, 2, "send")); |
| 3100 | lua_pushinteger(L, 0); |
| 3101 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3102 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | /* This function forward and amount of butes. The data pass from |
| 3106 | * the input side of the buffer to the output side, and can be |
| 3107 | * forwarded. This function never fails. |
| 3108 | * |
| 3109 | * The Lua function takes an amount of bytes to be forwarded in |
| 3110 | * imput. It returns the number of bytes forwarded. |
| 3111 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3112 | __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] | 3113 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3114 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3115 | int len; |
| 3116 | int l; |
| 3117 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3118 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3119 | |
| 3120 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3121 | |
| 3122 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3123 | WILL_LJMP(lua_error(L)); |
| 3124 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3125 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3126 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3127 | |
| 3128 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3129 | if (max > ci_data(chn)) |
| 3130 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3131 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3132 | l += max; |
| 3133 | |
| 3134 | lua_pop(L, 1); |
| 3135 | lua_pushinteger(L, l); |
| 3136 | |
| 3137 | /* Check if it miss bytes to forward. */ |
| 3138 | if (l < len) { |
| 3139 | /* The the input channel or the output channel are closed, we |
| 3140 | * must return the amount of data forwarded. |
| 3141 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3142 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3143 | return 1; |
| 3144 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3145 | /* If we are waiting for space data in the response buffer, we |
| 3146 | * must set the flag WAKERESWR. This flag required the task |
| 3147 | * wake up if any activity is detected on the response buffer. |
| 3148 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3149 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3150 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3151 | else |
| 3152 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3153 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3154 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3155 | 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] | 3156 | } |
| 3157 | |
| 3158 | return 1; |
| 3159 | } |
| 3160 | |
| 3161 | /* Just check the input and prepare the stack for the previous |
| 3162 | * function "hlua_channel_forward_yield" |
| 3163 | */ |
| 3164 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3165 | { |
| 3166 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3167 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3168 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3169 | |
| 3170 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3171 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3172 | } |
| 3173 | |
| 3174 | /* Just returns the number of bytes available in the input |
| 3175 | * side of the buffer. This function never fails. |
| 3176 | */ |
| 3177 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3178 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3179 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3180 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3181 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3182 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3183 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3184 | struct htx *htx = htxbuf(&chn->buf); |
| 3185 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3186 | } |
| 3187 | else |
| 3188 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3189 | return 1; |
| 3190 | } |
| 3191 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3192 | /* Returns true if the channel is full. */ |
| 3193 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3194 | { |
| 3195 | struct channel *chn; |
| 3196 | int rem; |
| 3197 | |
| 3198 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3199 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3200 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3201 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3202 | struct htx *htx = htxbuf(&chn->buf); |
| 3203 | |
| 3204 | rem = htx_free_data_space(htx); |
| 3205 | } |
| 3206 | else |
| 3207 | rem = b_room(&chn->buf); |
| 3208 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3209 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3210 | |
| 3211 | lua_pushboolean(L, rem <= 0); |
| 3212 | return 1; |
| 3213 | } |
| 3214 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3215 | /* Just returns the number of bytes available in the output |
| 3216 | * side of the buffer. This function never fails. |
| 3217 | */ |
| 3218 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3219 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3220 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3221 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3222 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3223 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3224 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3225 | return 1; |
| 3226 | } |
| 3227 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3228 | /* |
| 3229 | * |
| 3230 | * |
| 3231 | * Class Fetches |
| 3232 | * |
| 3233 | * |
| 3234 | */ |
| 3235 | |
| 3236 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3237 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3238 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3239 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3240 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3241 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3242 | } |
| 3243 | |
| 3244 | /* This function creates and push in the stack a fetch object according |
| 3245 | * with a current TXN. |
| 3246 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3247 | 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] | 3248 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3249 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3250 | |
| 3251 | /* Check stack size. */ |
| 3252 | if (!lua_checkstack(L, 3)) |
| 3253 | return 0; |
| 3254 | |
| 3255 | /* Create the object: obj[0] = userdata. |
| 3256 | * Note that the base of the Fetches object is the |
| 3257 | * transaction object. |
| 3258 | */ |
| 3259 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3260 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3261 | lua_rawseti(L, -2, 0); |
| 3262 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3263 | hsmp->s = txn->s; |
| 3264 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3265 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3266 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3267 | |
| 3268 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3269 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3270 | lua_setmetatable(L, -2); |
| 3271 | |
| 3272 | return 1; |
| 3273 | } |
| 3274 | |
| 3275 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3276 | * It uses closure argument to store the associated sample-fetch. It |
| 3277 | * returns only one argument or throws an error. An error is thrown |
| 3278 | * only if an error is encountered during the argument parsing. If |
| 3279 | * the "sample-fetch" function fails, nil is returned. |
| 3280 | */ |
| 3281 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3282 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3283 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3284 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3285 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3286 | int i; |
| 3287 | struct sample smp; |
| 3288 | |
| 3289 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3290 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3291 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3292 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3293 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3294 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3295 | /* Check execution authorization. */ |
| 3296 | if (f->use & SMP_USE_HTTP_ANY && |
| 3297 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3298 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3299 | "is not available in Lua services", f->kw); |
| 3300 | WILL_LJMP(lua_error(L)); |
| 3301 | } |
| 3302 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3303 | /* Get extra arguments. */ |
| 3304 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3305 | if (i >= ARGM_NBARGS) |
| 3306 | break; |
| 3307 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3308 | } |
| 3309 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3310 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3311 | |
| 3312 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3313 | 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] | 3314 | |
| 3315 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3316 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3317 | lua_pushfstring(L, "error in arguments"); |
| 3318 | WILL_LJMP(lua_error(L)); |
| 3319 | } |
| 3320 | |
| 3321 | /* Initialise the sample. */ |
| 3322 | memset(&smp, 0, sizeof(smp)); |
| 3323 | |
| 3324 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3325 | 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] | 3326 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3327 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3328 | lua_pushstring(L, ""); |
| 3329 | else |
| 3330 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3331 | return 1; |
| 3332 | } |
| 3333 | |
| 3334 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3335 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3336 | hlua_smp2lua_str(L, &smp); |
| 3337 | else |
| 3338 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3339 | return 1; |
| 3340 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3341 | |
| 3342 | /* |
| 3343 | * |
| 3344 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3345 | * Class Converters |
| 3346 | * |
| 3347 | * |
| 3348 | */ |
| 3349 | |
| 3350 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3351 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3352 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3353 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3354 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3355 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3356 | } |
| 3357 | |
| 3358 | /* This function creates and push in the stack a Converters object |
| 3359 | * according with a current TXN. |
| 3360 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3361 | 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] | 3362 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3363 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3364 | |
| 3365 | /* Check stack size. */ |
| 3366 | if (!lua_checkstack(L, 3)) |
| 3367 | return 0; |
| 3368 | |
| 3369 | /* Create the object: obj[0] = userdata. |
| 3370 | * Note that the base of the Converters object is the |
| 3371 | * same than the TXN object. |
| 3372 | */ |
| 3373 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3374 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3375 | lua_rawseti(L, -2, 0); |
| 3376 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3377 | hsmp->s = txn->s; |
| 3378 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3379 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3380 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3381 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3382 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3383 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3384 | lua_setmetatable(L, -2); |
| 3385 | |
| 3386 | return 1; |
| 3387 | } |
| 3388 | |
| 3389 | /* This function is an LUA binding. It is called with each converter. |
| 3390 | * It uses closure argument to store the associated converter. It |
| 3391 | * returns only one argument or throws an error. An error is thrown |
| 3392 | * only if an error is encountered during the argument parsing. If |
| 3393 | * the converter function function fails, nil is returned. |
| 3394 | */ |
| 3395 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3396 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3397 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3398 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3399 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3400 | int i; |
| 3401 | struct sample smp; |
| 3402 | |
| 3403 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3404 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3405 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3406 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3407 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3408 | |
| 3409 | /* Get extra arguments. */ |
| 3410 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3411 | if (i >= ARGM_NBARGS) |
| 3412 | break; |
| 3413 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3414 | } |
| 3415 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3416 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3417 | |
| 3418 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3419 | 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] | 3420 | |
| 3421 | /* Run the special args checker. */ |
| 3422 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3423 | hlua_pusherror(L, "error in arguments"); |
| 3424 | WILL_LJMP(lua_error(L)); |
| 3425 | } |
| 3426 | |
| 3427 | /* Initialise the sample. */ |
| 3428 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3429 | hlua_pusherror(L, "error in the input argument"); |
| 3430 | WILL_LJMP(lua_error(L)); |
| 3431 | } |
| 3432 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3433 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3434 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3435 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3436 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3437 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3438 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3439 | WILL_LJMP(lua_error(L)); |
| 3440 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3441 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3442 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3443 | hlua_pusherror(L, "error during the input argument casting"); |
| 3444 | WILL_LJMP(lua_error(L)); |
| 3445 | } |
| 3446 | |
| 3447 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3448 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3449 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3450 | lua_pushstring(L, ""); |
| 3451 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3452 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3453 | return 1; |
| 3454 | } |
| 3455 | |
| 3456 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3457 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3458 | hlua_smp2lua_str(L, &smp); |
| 3459 | else |
| 3460 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3461 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3462 | } |
| 3463 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3464 | /* |
| 3465 | * |
| 3466 | * |
| 3467 | * Class AppletTCP |
| 3468 | * |
| 3469 | * |
| 3470 | */ |
| 3471 | |
| 3472 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3473 | * a class stream, otherwise it throws an error. |
| 3474 | */ |
| 3475 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3476 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3477 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3478 | } |
| 3479 | |
| 3480 | /* This function creates and push in the stack an Applet object |
| 3481 | * according with a current TXN. |
| 3482 | */ |
| 3483 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3484 | { |
| 3485 | struct hlua_appctx *appctx; |
| 3486 | struct stream_interface *si = ctx->owner; |
| 3487 | struct stream *s = si_strm(si); |
| 3488 | struct proxy *p = s->be; |
| 3489 | |
| 3490 | /* Check stack size. */ |
| 3491 | if (!lua_checkstack(L, 3)) |
| 3492 | return 0; |
| 3493 | |
| 3494 | /* Create the object: obj[0] = userdata. |
| 3495 | * Note that the base of the Converters object is the |
| 3496 | * same than the TXN object. |
| 3497 | */ |
| 3498 | lua_newtable(L); |
| 3499 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3500 | lua_rawseti(L, -2, 0); |
| 3501 | appctx->appctx = ctx; |
| 3502 | appctx->htxn.s = s; |
| 3503 | appctx->htxn.p = p; |
| 3504 | |
| 3505 | /* Create the "f" field that contains a list of fetches. */ |
| 3506 | lua_pushstring(L, "f"); |
| 3507 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3508 | return 0; |
| 3509 | lua_settable(L, -3); |
| 3510 | |
| 3511 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3512 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3513 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3514 | return 0; |
| 3515 | lua_settable(L, -3); |
| 3516 | |
| 3517 | /* Create the "c" field that contains a list of converters. */ |
| 3518 | lua_pushstring(L, "c"); |
| 3519 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3520 | return 0; |
| 3521 | lua_settable(L, -3); |
| 3522 | |
| 3523 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3524 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3525 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3526 | return 0; |
| 3527 | lua_settable(L, -3); |
| 3528 | |
| 3529 | /* Pop a class stream metatable and affect it to the table. */ |
| 3530 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3531 | lua_setmetatable(L, -2); |
| 3532 | |
| 3533 | return 1; |
| 3534 | } |
| 3535 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3536 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3537 | { |
| 3538 | struct hlua_appctx *appctx; |
| 3539 | struct stream *s; |
| 3540 | const char *name; |
| 3541 | size_t len; |
| 3542 | struct sample smp; |
| 3543 | |
| 3544 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3545 | |
| 3546 | /* It is useles to retrieve the stream, but this function |
| 3547 | * runs only in a stream context. |
| 3548 | */ |
| 3549 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3550 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3551 | s = appctx->htxn.s; |
| 3552 | |
| 3553 | /* Converts the third argument in a sample. */ |
| 3554 | hlua_lua2smp(L, 3, &smp); |
| 3555 | |
| 3556 | /* Store the sample in a variable. */ |
| 3557 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3558 | vars_set_by_name(name, len, &smp); |
| 3559 | return 0; |
| 3560 | } |
| 3561 | |
| 3562 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3563 | { |
| 3564 | struct hlua_appctx *appctx; |
| 3565 | struct stream *s; |
| 3566 | const char *name; |
| 3567 | size_t len; |
| 3568 | struct sample smp; |
| 3569 | |
| 3570 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3571 | |
| 3572 | /* It is useles to retrieve the stream, but this function |
| 3573 | * runs only in a stream context. |
| 3574 | */ |
| 3575 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3576 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3577 | s = appctx->htxn.s; |
| 3578 | |
| 3579 | /* Unset the variable. */ |
| 3580 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3581 | vars_unset_by_name(name, len, &smp); |
| 3582 | return 0; |
| 3583 | } |
| 3584 | |
| 3585 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3586 | { |
| 3587 | struct hlua_appctx *appctx; |
| 3588 | struct stream *s; |
| 3589 | const char *name; |
| 3590 | size_t len; |
| 3591 | struct sample smp; |
| 3592 | |
| 3593 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3594 | |
| 3595 | /* It is useles to retrieve the stream, but this function |
| 3596 | * runs only in a stream context. |
| 3597 | */ |
| 3598 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3599 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3600 | s = appctx->htxn.s; |
| 3601 | |
| 3602 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3603 | if (!vars_get_by_name(name, len, &smp)) { |
| 3604 | lua_pushnil(L); |
| 3605 | return 1; |
| 3606 | } |
| 3607 | |
| 3608 | return hlua_smp2lua(L, &smp); |
| 3609 | } |
| 3610 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3611 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3612 | { |
| 3613 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3614 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3615 | struct hlua *hlua; |
| 3616 | |
| 3617 | /* 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] | 3618 | if (!s->hlua) |
| 3619 | return 0; |
| 3620 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3621 | |
| 3622 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3623 | |
| 3624 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3625 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3626 | |
| 3627 | /* Get and store new value. */ |
| 3628 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3629 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3630 | |
| 3631 | return 0; |
| 3632 | } |
| 3633 | |
| 3634 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3635 | { |
| 3636 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3637 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3638 | struct hlua *hlua; |
| 3639 | |
| 3640 | /* 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] | 3641 | if (!s->hlua) { |
| 3642 | lua_pushnil(L); |
| 3643 | return 1; |
| 3644 | } |
| 3645 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3646 | |
| 3647 | /* Push configuration index in the stack. */ |
| 3648 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3649 | |
| 3650 | return 1; |
| 3651 | } |
| 3652 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3653 | /* If expected data not yet available, it returns a yield. This function |
| 3654 | * consumes the data in the buffer. It returns a string containing the |
| 3655 | * data. This string can be empty. |
| 3656 | */ |
| 3657 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3658 | { |
| 3659 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3660 | struct stream_interface *si = appctx->appctx->owner; |
| 3661 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3662 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3663 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3664 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3665 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3666 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3667 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3668 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3669 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3670 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3671 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3672 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3673 | 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] | 3674 | } |
| 3675 | |
| 3676 | /* End of data: commit the total strings and return. */ |
| 3677 | if (ret < 0) { |
| 3678 | luaL_pushresult(&appctx->b); |
| 3679 | return 1; |
| 3680 | } |
| 3681 | |
| 3682 | /* Ensure that the block 2 length is usable. */ |
| 3683 | if (ret == 1) |
| 3684 | len2 = 0; |
| 3685 | |
| 3686 | /* dont check the max length read and dont check. */ |
| 3687 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3688 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3689 | |
| 3690 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3691 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3692 | luaL_pushresult(&appctx->b); |
| 3693 | return 1; |
| 3694 | } |
| 3695 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3696 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3697 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3698 | { |
| 3699 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3700 | |
| 3701 | /* Initialise the string catenation. */ |
| 3702 | luaL_buffinit(L, &appctx->b); |
| 3703 | |
| 3704 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3705 | } |
| 3706 | |
| 3707 | /* If expected data not yet available, it returns a yield. This function |
| 3708 | * consumes the data in the buffer. It returns a string containing the |
| 3709 | * data. This string can be empty. |
| 3710 | */ |
| 3711 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3712 | { |
| 3713 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3714 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3715 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3716 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3717 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3718 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3719 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3720 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3721 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3722 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3723 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3724 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3725 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3726 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3727 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3728 | 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] | 3729 | } |
| 3730 | |
| 3731 | /* End of data: commit the total strings and return. */ |
| 3732 | if (ret < 0) { |
| 3733 | luaL_pushresult(&appctx->b); |
| 3734 | return 1; |
| 3735 | } |
| 3736 | |
| 3737 | /* Ensure that the block 2 length is usable. */ |
| 3738 | if (ret == 1) |
| 3739 | len2 = 0; |
| 3740 | |
| 3741 | if (len == -1) { |
| 3742 | |
| 3743 | /* If len == -1, catenate all the data avalaile and |
| 3744 | * yield because we want to get all the data until |
| 3745 | * the end of data stream. |
| 3746 | */ |
| 3747 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3748 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3749 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3750 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3751 | 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] | 3752 | |
| 3753 | } else { |
| 3754 | |
| 3755 | /* Copy the fisrt block caping to the length required. */ |
| 3756 | if (len1 > len) |
| 3757 | len1 = len; |
| 3758 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3759 | len -= len1; |
| 3760 | |
| 3761 | /* Copy the second block. */ |
| 3762 | if (len2 > len) |
| 3763 | len2 = len; |
| 3764 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3765 | len -= len2; |
| 3766 | |
| 3767 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3768 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3769 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3770 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3771 | if (len > 0) { |
| 3772 | lua_pushinteger(L, len); |
| 3773 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3774 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3775 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3776 | } |
| 3777 | |
| 3778 | /* return the result. */ |
| 3779 | luaL_pushresult(&appctx->b); |
| 3780 | return 1; |
| 3781 | } |
| 3782 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3783 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3784 | hlua_pusherror(L, "Lua: internal error"); |
| 3785 | WILL_LJMP(lua_error(L)); |
| 3786 | return 0; |
| 3787 | } |
| 3788 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3789 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3790 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3791 | { |
| 3792 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3793 | int len = -1; |
| 3794 | |
| 3795 | if (lua_gettop(L) > 2) |
| 3796 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3797 | if (lua_gettop(L) >= 2) { |
| 3798 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3799 | lua_pop(L, 1); |
| 3800 | } |
| 3801 | |
| 3802 | /* Confirm or set the required length */ |
| 3803 | lua_pushinteger(L, len); |
| 3804 | |
| 3805 | /* Initialise the string catenation. */ |
| 3806 | luaL_buffinit(L, &appctx->b); |
| 3807 | |
| 3808 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3809 | } |
| 3810 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3811 | /* Append data in the output side of the buffer. This data is immediately |
| 3812 | * sent. The function returns the amount of data written. If the buffer |
| 3813 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3814 | * if the channel is closed. |
| 3815 | */ |
| 3816 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3817 | { |
| 3818 | size_t len; |
| 3819 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3820 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3821 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3822 | struct stream_interface *si = appctx->appctx->owner; |
| 3823 | struct channel *chn = si_ic(si); |
| 3824 | int max; |
| 3825 | |
| 3826 | /* Get the max amount of data which can write as input in the channel. */ |
| 3827 | max = channel_recv_max(chn); |
| 3828 | if (max > (len - l)) |
| 3829 | max = len - l; |
| 3830 | |
| 3831 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3832 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3833 | |
| 3834 | /* update counters. */ |
| 3835 | l += max; |
| 3836 | lua_pop(L, 1); |
| 3837 | lua_pushinteger(L, l); |
| 3838 | |
| 3839 | /* If some data is not send, declares the situation to the |
| 3840 | * applet, and returns a yield. |
| 3841 | */ |
| 3842 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3843 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3844 | 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] | 3845 | } |
| 3846 | |
| 3847 | return 1; |
| 3848 | } |
| 3849 | |
| 3850 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3851 | * yield the LUA process, and resume it without checking the |
| 3852 | * input arguments. |
| 3853 | */ |
| 3854 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3855 | { |
| 3856 | MAY_LJMP(check_args(L, 2, "send")); |
| 3857 | lua_pushinteger(L, 0); |
| 3858 | |
| 3859 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3860 | } |
| 3861 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3862 | /* |
| 3863 | * |
| 3864 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3865 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3866 | * |
| 3867 | * |
| 3868 | */ |
| 3869 | |
| 3870 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3871 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3872 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3873 | __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] | 3874 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3875 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3876 | } |
| 3877 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3878 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3879 | * according with a current TXN. |
| 3880 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3881 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3882 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3883 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3884 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3885 | struct stream_interface *si = ctx->owner; |
| 3886 | struct stream *s = si_strm(si); |
| 3887 | struct proxy *px = s->be; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3888 | |
| 3889 | /* Check stack size. */ |
| 3890 | if (!lua_checkstack(L, 3)) |
| 3891 | return 0; |
| 3892 | |
| 3893 | /* Create the object: obj[0] = userdata. |
| 3894 | * Note that the base of the Converters object is the |
| 3895 | * same than the TXN object. |
| 3896 | */ |
| 3897 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3898 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3899 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3900 | appctx->appctx = ctx; |
| 3901 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3902 | 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] | 3903 | appctx->htxn.s = s; |
| 3904 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3905 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3906 | /* Create the "f" field that contains a list of fetches. */ |
| 3907 | lua_pushstring(L, "f"); |
| 3908 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3909 | return 0; |
| 3910 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3911 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3912 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3913 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3914 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3915 | return 0; |
| 3916 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3917 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3918 | /* Create the "c" field that contains a list of converters. */ |
| 3919 | lua_pushstring(L, "c"); |
| 3920 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3921 | return 0; |
| 3922 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3923 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3924 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3925 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3926 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3927 | return 0; |
| 3928 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3929 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3930 | if (IS_HTX_STRM(s)) { |
| 3931 | /* HTX version */ |
| 3932 | struct htx *htx = htxbuf(&s->req.buf); |
| 3933 | struct htx_sl *sl = http_find_stline(htx); |
| 3934 | struct ist path; |
| 3935 | unsigned long long len = 0; |
| 3936 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3937 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3938 | /* Stores the request method. */ |
| 3939 | lua_pushstring(L, "method"); |
| 3940 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3941 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3942 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3943 | /* Stores the http version. */ |
| 3944 | lua_pushstring(L, "version"); |
| 3945 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3946 | lua_settable(L, -3); |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3947 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3948 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3949 | * the array on the top of the stack. |
| 3950 | */ |
| 3951 | lua_pushstring(L, "headers"); |
| 3952 | htxn.s = s; |
| 3953 | htxn.p = px; |
| 3954 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3955 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3956 | return 0; |
| 3957 | lua_settable(L, -3); |
| 3958 | |
| 3959 | path = http_get_path(htx_sl_req_uri(sl)); |
| 3960 | if (path.ptr) { |
| 3961 | char *p, *q, *end; |
| 3962 | |
| 3963 | p = path.ptr; |
| 3964 | end = path.ptr + path.len; |
| 3965 | q = p; |
| 3966 | while (q < end && *q != '?') |
| 3967 | q++; |
| 3968 | |
| 3969 | /* Stores the request path. */ |
| 3970 | lua_pushstring(L, "path"); |
| 3971 | lua_pushlstring(L, p, q - p); |
| 3972 | lua_settable(L, -3); |
| 3973 | |
| 3974 | /* Stores the query string. */ |
| 3975 | lua_pushstring(L, "qs"); |
| 3976 | if (*q == '?') |
| 3977 | q++; |
| 3978 | lua_pushlstring(L, q, end - q); |
| 3979 | lua_settable(L, -3); |
| 3980 | } |
| 3981 | |
| 3982 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 3983 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3984 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 3985 | |
| 3986 | if (type == HTX_BLK_EOM || type == HTX_BLK_EOD) |
| 3987 | break; |
| 3988 | if (type == HTX_BLK_DATA) |
| 3989 | len += htx_get_blksz(blk); |
| 3990 | } |
| 3991 | if (htx->extra != ULLONG_MAX) |
| 3992 | len += htx->extra; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3993 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3994 | /* Stores the request path. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3995 | lua_pushstring(L, "length"); |
| 3996 | lua_pushinteger(L, len); |
| 3997 | lua_settable(L, -3); |
| 3998 | } |
| 3999 | else { |
| 4000 | /* Legacy HTTP version */ |
| 4001 | struct http_txn *txn = s->txn; |
| 4002 | const char *path; |
| 4003 | const char *end; |
| 4004 | const char *p; |
| 4005 | |
| 4006 | /* Stores the request method. */ |
| 4007 | lua_pushstring(L, "method"); |
| 4008 | 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] | 4009 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4010 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4011 | /* Stores the http version. */ |
| 4012 | lua_pushstring(L, "version"); |
| 4013 | 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] | 4014 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4015 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4016 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4017 | * the array on the top of the stack. |
| 4018 | */ |
| 4019 | lua_pushstring(L, "headers"); |
| 4020 | htxn.s = s; |
| 4021 | htxn.p = px; |
| 4022 | htxn.dir = SMP_OPT_DIR_REQ; |
| 4023 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 4024 | return 0; |
| 4025 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4026 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4027 | /* Get path and qs */ |
| 4028 | path = http_txn_get_path(txn); |
| 4029 | if (path) { |
| 4030 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 4031 | p = path; |
| 4032 | while (p < end && *p != '?') |
| 4033 | p++; |
| 4034 | |
| 4035 | /* Stores the request path. */ |
| 4036 | lua_pushstring(L, "path"); |
| 4037 | lua_pushlstring(L, path, p - path); |
| 4038 | lua_settable(L, -3); |
| 4039 | |
| 4040 | /* Stores the query string. */ |
| 4041 | lua_pushstring(L, "qs"); |
| 4042 | if (*p == '?') |
| 4043 | p++; |
| 4044 | lua_pushlstring(L, p, end - p); |
| 4045 | lua_settable(L, -3); |
| 4046 | } |
| 4047 | |
| 4048 | /* Stores the request path. */ |
| 4049 | lua_pushstring(L, "length"); |
| 4050 | lua_pushinteger(L, txn->req.body_len); |
| 4051 | lua_settable(L, -3); |
| 4052 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4053 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4054 | /* Create an empty array of HTTP request headers. */ |
| 4055 | lua_pushstring(L, "response"); |
| 4056 | lua_newtable(L); |
| 4057 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4058 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4059 | /* Pop a class stream metatable and affect it to the table. */ |
| 4060 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4061 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4062 | |
| 4063 | return 1; |
| 4064 | } |
| 4065 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4066 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4067 | { |
| 4068 | struct hlua_appctx *appctx; |
| 4069 | struct stream *s; |
| 4070 | const char *name; |
| 4071 | size_t len; |
| 4072 | struct sample smp; |
| 4073 | |
| 4074 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4075 | |
| 4076 | /* It is useles to retrieve the stream, but this function |
| 4077 | * runs only in a stream context. |
| 4078 | */ |
| 4079 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4080 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4081 | s = appctx->htxn.s; |
| 4082 | |
| 4083 | /* Converts the third argument in a sample. */ |
| 4084 | hlua_lua2smp(L, 3, &smp); |
| 4085 | |
| 4086 | /* Store the sample in a variable. */ |
| 4087 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4088 | vars_set_by_name(name, len, &smp); |
| 4089 | return 0; |
| 4090 | } |
| 4091 | |
| 4092 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4093 | { |
| 4094 | struct hlua_appctx *appctx; |
| 4095 | struct stream *s; |
| 4096 | const char *name; |
| 4097 | size_t len; |
| 4098 | struct sample smp; |
| 4099 | |
| 4100 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4101 | |
| 4102 | /* It is useles to retrieve the stream, but this function |
| 4103 | * runs only in a stream context. |
| 4104 | */ |
| 4105 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4106 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4107 | s = appctx->htxn.s; |
| 4108 | |
| 4109 | /* Unset the variable. */ |
| 4110 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4111 | vars_unset_by_name(name, len, &smp); |
| 4112 | return 0; |
| 4113 | } |
| 4114 | |
| 4115 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4116 | { |
| 4117 | struct hlua_appctx *appctx; |
| 4118 | struct stream *s; |
| 4119 | const char *name; |
| 4120 | size_t len; |
| 4121 | struct sample smp; |
| 4122 | |
| 4123 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4124 | |
| 4125 | /* It is useles to retrieve the stream, but this function |
| 4126 | * runs only in a stream context. |
| 4127 | */ |
| 4128 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4129 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4130 | s = appctx->htxn.s; |
| 4131 | |
| 4132 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4133 | if (!vars_get_by_name(name, len, &smp)) { |
| 4134 | lua_pushnil(L); |
| 4135 | return 1; |
| 4136 | } |
| 4137 | |
| 4138 | return hlua_smp2lua(L, &smp); |
| 4139 | } |
| 4140 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4141 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4142 | { |
| 4143 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4144 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4145 | struct hlua *hlua; |
| 4146 | |
| 4147 | /* 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] | 4148 | if (!s->hlua) |
| 4149 | return 0; |
| 4150 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4151 | |
| 4152 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4153 | |
| 4154 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4155 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4156 | |
| 4157 | /* Get and store new value. */ |
| 4158 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4159 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4160 | |
| 4161 | return 0; |
| 4162 | } |
| 4163 | |
| 4164 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4165 | { |
| 4166 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4167 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4168 | struct hlua *hlua; |
| 4169 | |
| 4170 | /* 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] | 4171 | if (!s->hlua) { |
| 4172 | lua_pushnil(L); |
| 4173 | return 1; |
| 4174 | } |
| 4175 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4176 | |
| 4177 | /* Push configuration index in the stack. */ |
| 4178 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4179 | |
| 4180 | return 1; |
| 4181 | } |
| 4182 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4183 | __LJMP static void hlua_applet_htx_reply_100_continue(lua_State *L) |
| 4184 | { |
| 4185 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4186 | struct stream_interface *si = appctx->appctx->owner; |
| 4187 | struct channel *res = si_ic(si); |
| 4188 | struct htx *htx = htx_from_buf(&res->buf); |
| 4189 | struct htx_sl *sl; |
| 4190 | unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11| |
| 4191 | HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 4192 | size_t data; |
| 4193 | |
| 4194 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4195 | ist("HTTP/1.1"), ist("100"), ist("Continue")); |
| 4196 | if (!sl) |
| 4197 | goto fail; |
| 4198 | sl->info.res.status = 100; |
| 4199 | if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM)) |
| 4200 | goto fail; |
| 4201 | |
| 4202 | data = htx->data - co_data(res); |
| 4203 | res->total += data; |
| 4204 | res->flags |= CF_READ_PARTIAL; |
| 4205 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4206 | return; |
| 4207 | |
| 4208 | fail: |
| 4209 | hlua_pusherror(L, "Lua applet http '%s': Failed to create 100-Continue response.\n", |
| 4210 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4211 | WILL_LJMP(lua_error(L)); |
| 4212 | } |
| 4213 | |
| 4214 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4215 | /* If expected data not yet available, it returns a yield. This function |
| 4216 | * consumes the data in the buffer. It returns a string containing the |
| 4217 | * data. This string can be empty. |
| 4218 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4219 | __LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4220 | { |
| 4221 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4222 | struct stream_interface *si = appctx->appctx->owner; |
| 4223 | struct channel *req = si_oc(si); |
| 4224 | struct htx *htx; |
| 4225 | struct htx_blk *blk; |
| 4226 | size_t count; |
| 4227 | int stop = 0; |
| 4228 | |
| 4229 | /* Maybe we cant send a 100-continue ? */ |
| 4230 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) |
| 4231 | MAY_LJMP(hlua_applet_htx_reply_100_continue(L)); |
| 4232 | |
| 4233 | htx = htx_from_buf(&req->buf); |
| 4234 | count = co_data(req); |
| 4235 | blk = htx_get_head_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4236 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4237 | while (count && !stop && blk) { |
| 4238 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4239 | uint32_t sz = htx_get_blksz(blk); |
| 4240 | struct ist v; |
| 4241 | uint32_t vlen; |
| 4242 | char *nl; |
| 4243 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4244 | if (type == HTX_BLK_EOM) { |
| 4245 | stop = 1; |
| 4246 | break; |
| 4247 | } |
| 4248 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4249 | vlen = sz; |
| 4250 | if (vlen > count) { |
| 4251 | if (type != HTX_BLK_DATA) |
| 4252 | break; |
| 4253 | vlen = count; |
| 4254 | } |
| 4255 | |
| 4256 | switch (type) { |
| 4257 | case HTX_BLK_UNUSED: |
| 4258 | break; |
| 4259 | |
| 4260 | case HTX_BLK_DATA: |
| 4261 | v = htx_get_blk_value(htx, blk); |
| 4262 | v.len = vlen; |
| 4263 | nl = istchr(v, '\n'); |
| 4264 | if (nl != NULL) { |
| 4265 | stop = 1; |
| 4266 | vlen = nl - v.ptr + 1; |
| 4267 | } |
| 4268 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4269 | break; |
| 4270 | |
| 4271 | case HTX_BLK_EOD: |
| 4272 | case HTX_BLK_TLR: |
| 4273 | case HTX_BLK_EOM: |
| 4274 | stop = 1; |
| 4275 | break; |
| 4276 | |
| 4277 | default: |
| 4278 | break; |
| 4279 | } |
| 4280 | |
| 4281 | co_set_data(req, co_data(req) - vlen); |
| 4282 | count -= vlen; |
| 4283 | if (sz == vlen) |
| 4284 | blk = htx_remove_blk(htx, blk); |
| 4285 | else { |
| 4286 | htx_cut_data_blk(htx, blk, vlen); |
| 4287 | break; |
| 4288 | } |
| 4289 | } |
| 4290 | |
| 4291 | if (!stop) { |
| 4292 | si_cant_get(si); |
| 4293 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0)); |
| 4294 | } |
| 4295 | |
| 4296 | /* return the result. */ |
| 4297 | luaL_pushresult(&appctx->b); |
| 4298 | return 1; |
| 4299 | } |
| 4300 | |
| 4301 | |
| 4302 | /* If expected data not yet available, it returns a yield. This function |
| 4303 | * consumes the data in the buffer. It returns a string containing the |
| 4304 | * data. This string can be empty. |
| 4305 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4306 | __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] | 4307 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4308 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4309 | struct stream_interface *si = appctx->appctx->owner; |
| 4310 | struct channel *chn = si_ic(si); |
| 4311 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4312 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4313 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4314 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4315 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4316 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4317 | /* Maybe we cant send a 100-continue ? */ |
| 4318 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4319 | ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4320 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4321 | * big for the buffers. We cant send anything. So, we ignoring |
| 4322 | * the error, considers that the 100-continue is sent, and try |
| 4323 | * to receive. |
| 4324 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4325 | */ |
| 4326 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4327 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4328 | 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] | 4329 | } |
| 4330 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4331 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4332 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4333 | /* Check for the end of the data. */ |
| 4334 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4335 | luaL_pushresult(&appctx->b); |
| 4336 | return 1; |
| 4337 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4338 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4339 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4340 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4341 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4342 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4343 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4344 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4345 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4346 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4347 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4348 | /* End of data: commit the total strings and return. */ |
| 4349 | if (ret < 0) { |
| 4350 | luaL_pushresult(&appctx->b); |
| 4351 | return 1; |
| 4352 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4353 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4354 | /* Ensure that the block 2 length is usable. */ |
| 4355 | if (ret == 1) |
| 4356 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4357 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4358 | /* Copy the fisrt block caping to the length required. */ |
| 4359 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4360 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4361 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4362 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4363 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4364 | /* Copy the second block. */ |
| 4365 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4366 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4367 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4368 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4369 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4370 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4371 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4372 | luaL_pushresult(&appctx->b); |
| 4373 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4374 | } |
| 4375 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4376 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4377 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4378 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4379 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4380 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4381 | /* Initialise the string catenation. */ |
| 4382 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4383 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4384 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 4385 | return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0)); |
| 4386 | else |
| 4387 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4388 | } |
| 4389 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4390 | /* If expected data not yet available, it returns a yield. This function |
| 4391 | * consumes the data in the buffer. It returns a string containing the |
| 4392 | * data. This string can be empty. |
| 4393 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4394 | __LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4395 | { |
| 4396 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4397 | struct stream_interface *si = appctx->appctx->owner; |
| 4398 | struct channel *req = si_oc(si); |
| 4399 | struct htx *htx; |
| 4400 | struct htx_blk *blk; |
| 4401 | size_t count; |
| 4402 | int len; |
| 4403 | |
| 4404 | /* Maybe we cant send a 100-continue ? */ |
| 4405 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) |
| 4406 | MAY_LJMP(hlua_applet_htx_reply_100_continue(L)); |
| 4407 | |
| 4408 | htx = htx_from_buf(&req->buf); |
| 4409 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4410 | count = co_data(req); |
| 4411 | blk = htx_get_head_blk(htx); |
| 4412 | while (count && len && blk) { |
| 4413 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4414 | uint32_t sz = htx_get_blksz(blk); |
| 4415 | struct ist v; |
| 4416 | uint32_t vlen; |
| 4417 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4418 | if (type == HTX_BLK_EOM) { |
| 4419 | len = 0; |
| 4420 | break; |
| 4421 | } |
| 4422 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4423 | vlen = sz; |
| 4424 | if (len > 0 && vlen > len) |
| 4425 | vlen = len; |
| 4426 | if (vlen > count) { |
| 4427 | if (type != HTX_BLK_DATA) |
| 4428 | break; |
| 4429 | vlen = count; |
| 4430 | } |
| 4431 | |
| 4432 | switch (type) { |
| 4433 | case HTX_BLK_UNUSED: |
| 4434 | break; |
| 4435 | |
| 4436 | case HTX_BLK_DATA: |
| 4437 | v = htx_get_blk_value(htx, blk); |
| 4438 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4439 | break; |
| 4440 | |
| 4441 | case HTX_BLK_EOD: |
| 4442 | case HTX_BLK_TLR: |
| 4443 | case HTX_BLK_EOM: |
| 4444 | len = 0; |
| 4445 | break; |
| 4446 | |
| 4447 | default: |
| 4448 | break; |
| 4449 | } |
| 4450 | |
| 4451 | co_set_data(req, co_data(req) - vlen); |
| 4452 | count -= vlen; |
| 4453 | if (len > 0) |
| 4454 | len -= vlen; |
| 4455 | if (sz == vlen) |
| 4456 | blk = htx_remove_blk(htx, blk); |
| 4457 | else { |
| 4458 | htx_cut_data_blk(htx, blk, vlen); |
| 4459 | break; |
| 4460 | } |
| 4461 | } |
| 4462 | |
| 4463 | /* If we are no other data available, yield waiting for new data. */ |
| 4464 | if (len) { |
| 4465 | if (len > 0) { |
| 4466 | lua_pushinteger(L, len); |
| 4467 | lua_replace(L, 2); |
| 4468 | } |
| 4469 | si_cant_get(si); |
| 4470 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0)); |
| 4471 | } |
| 4472 | |
| 4473 | /* return the result. */ |
| 4474 | luaL_pushresult(&appctx->b); |
| 4475 | return 1; |
| 4476 | } |
| 4477 | |
| 4478 | /* If expected data not yet available, it returns a yield. This function |
| 4479 | * consumes the data in the buffer. It returns a string containing the |
| 4480 | * data. This string can be empty. |
| 4481 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4482 | __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] | 4483 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4484 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4485 | struct stream_interface *si = appctx->appctx->owner; |
| 4486 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4487 | struct channel *chn = si_ic(si); |
| 4488 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4489 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4490 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4491 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4492 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4493 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4494 | /* Maybe we cant send a 100-continue ? */ |
| 4495 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4496 | ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4497 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4498 | * big for the buffers. We cant send anything. So, we ignoring |
| 4499 | * the error, considers that the 100-continue is sent, and try |
| 4500 | * to receive. |
| 4501 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4502 | */ |
| 4503 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4504 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4505 | 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] | 4506 | } |
| 4507 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4508 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4509 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4510 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4511 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4512 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4513 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4514 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4515 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4516 | 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] | 4517 | } |
| 4518 | |
| 4519 | /* End of data: commit the total strings and return. */ |
| 4520 | if (ret < 0) { |
| 4521 | luaL_pushresult(&appctx->b); |
| 4522 | return 1; |
| 4523 | } |
| 4524 | |
| 4525 | /* Ensure that the block 2 length is usable. */ |
| 4526 | if (ret == 1) |
| 4527 | len2 = 0; |
| 4528 | |
| 4529 | /* Copy the fisrt block caping to the length required. */ |
| 4530 | if (len1 > len) |
| 4531 | len1 = len; |
| 4532 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4533 | len -= len1; |
| 4534 | |
| 4535 | /* Copy the second block. */ |
| 4536 | if (len2 > len) |
| 4537 | len2 = len; |
| 4538 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4539 | len -= len2; |
| 4540 | |
| 4541 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4542 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4543 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4544 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4545 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4546 | /* If we are no other data available, yield waiting for new data. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4547 | if (len > 0) { |
| 4548 | lua_pushinteger(L, len); |
| 4549 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4550 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4551 | 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] | 4552 | } |
| 4553 | |
| 4554 | /* return the result. */ |
| 4555 | luaL_pushresult(&appctx->b); |
| 4556 | return 1; |
| 4557 | } |
| 4558 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4559 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4560 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4561 | { |
| 4562 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4563 | int len = -1; |
| 4564 | |
| 4565 | /* Check arguments. */ |
| 4566 | if (lua_gettop(L) > 2) |
| 4567 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4568 | if (lua_gettop(L) >= 2) { |
| 4569 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4570 | lua_pop(L, 1); |
| 4571 | } |
| 4572 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4573 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4574 | /* HTX version */ |
| 4575 | lua_pushinteger(L, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4576 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4577 | /* Initialise the string catenation. */ |
| 4578 | luaL_buffinit(L, &appctx->b); |
| 4579 | |
| 4580 | return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0)); |
| 4581 | } |
| 4582 | else { |
| 4583 | /* Legacy HTTP version */ |
| 4584 | /* Check the required length */ |
| 4585 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4586 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4587 | lua_pushinteger(L, len); |
| 4588 | |
| 4589 | /* Initialise the string catenation. */ |
| 4590 | luaL_buffinit(L, &appctx->b); |
| 4591 | |
| 4592 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4593 | } |
| 4594 | } |
| 4595 | |
| 4596 | /* Append data in the output side of the buffer. This data is immediately |
| 4597 | * sent. The function returns the amount of data written. If the buffer |
| 4598 | * cannot contain the data, the function yields. The function returns -1 |
| 4599 | * if the channel is closed. |
| 4600 | */ |
| 4601 | __LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4602 | { |
| 4603 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4604 | struct stream_interface *si = appctx->appctx->owner; |
| 4605 | struct channel *res = si_ic(si); |
| 4606 | struct htx *htx = htx_from_buf(&res->buf); |
| 4607 | const char *data; |
| 4608 | size_t len; |
| 4609 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4610 | int max; |
| 4611 | |
| 4612 | max = htx_free_space(htx); |
| 4613 | if (channel_recv_limit(res) < b_size(&res->buf)) { |
| 4614 | if (max < global.tune.maxrewrite) { |
| 4615 | si_rx_room_blk(si); |
| 4616 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0)); |
| 4617 | } |
| 4618 | max -= global.tune.maxrewrite; |
| 4619 | } |
| 4620 | |
| 4621 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4622 | |
| 4623 | /* Get the max amount of data which can write as input in the channel. */ |
| 4624 | if (max > (len - l)) |
| 4625 | max = len - l; |
| 4626 | |
| 4627 | /* Copy data. */ |
| 4628 | htx_add_data(htx, ist2(data + l, max)); |
| 4629 | res->total += l; |
| 4630 | res->flags |= CF_READ_PARTIAL; |
| 4631 | htx_to_buf(htx, &res->buf); |
| 4632 | |
| 4633 | /* update counters. */ |
| 4634 | l += max; |
| 4635 | lua_pop(L, 1); |
| 4636 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4637 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4638 | /* If some data is not send, declares the situation to the |
| 4639 | * applet, and returns a yield. |
| 4640 | */ |
| 4641 | if (l < len) { |
| 4642 | si_rx_room_blk(si); |
| 4643 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0)); |
| 4644 | } |
| 4645 | |
| 4646 | return 1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4647 | } |
| 4648 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4649 | /* Append data in the output side of the buffer. This data is immediately |
| 4650 | * sent. The function returns the amount of data written. If the buffer |
| 4651 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4652 | * if the channel is closed. |
| 4653 | */ |
| 4654 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4655 | { |
| 4656 | size_t len; |
| 4657 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4658 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4659 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4660 | struct stream_interface *si = appctx->appctx->owner; |
| 4661 | struct channel *chn = si_ic(si); |
| 4662 | int max; |
| 4663 | |
| 4664 | /* Get the max amount of data which can write as input in the channel. */ |
| 4665 | max = channel_recv_max(chn); |
| 4666 | if (max > (len - l)) |
| 4667 | max = len - l; |
| 4668 | |
| 4669 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4670 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4671 | |
| 4672 | /* update counters. */ |
| 4673 | l += max; |
| 4674 | lua_pop(L, 1); |
| 4675 | lua_pushinteger(L, l); |
| 4676 | |
| 4677 | /* If some data is not send, declares the situation to the |
| 4678 | * applet, and returns a yield. |
| 4679 | */ |
| 4680 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4681 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4682 | 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] | 4683 | } |
| 4684 | |
| 4685 | return 1; |
| 4686 | } |
| 4687 | |
| 4688 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4689 | * yield the LUA process, and resume it without checking the |
| 4690 | * input arguments. |
| 4691 | */ |
| 4692 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4693 | { |
| 4694 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4695 | |
| 4696 | /* We want to send some data. Headers must be sent. */ |
| 4697 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4698 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4699 | WILL_LJMP(lua_error(L)); |
| 4700 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4701 | |
| 4702 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4703 | /* HTX version */ |
| 4704 | /* This interger is used for followinf the amount of data sent. */ |
| 4705 | lua_pushinteger(L, 0); |
| 4706 | |
| 4707 | return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0)); |
| 4708 | } |
| 4709 | else { |
| 4710 | /* Legacy HTTP version */ |
| 4711 | size_t len; |
| 4712 | char hex[10]; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4713 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4714 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4715 | |
| 4716 | /* If transfer encoding chunked is selected, we surround the data |
| 4717 | * by chunk data. |
| 4718 | */ |
| 4719 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4720 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4721 | lua_pushfstring(L, "%s\r\n", hex); |
| 4722 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4723 | lua_pushstring(L, "\r\n"); |
| 4724 | lua_concat(L, 3); |
| 4725 | } |
| 4726 | |
| 4727 | /* This interger is used for followinf the amount of data sent. */ |
| 4728 | lua_pushinteger(L, 0); |
| 4729 | |
| 4730 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4731 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4732 | } |
| 4733 | |
| 4734 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4735 | { |
| 4736 | const char *name; |
| 4737 | int ret; |
| 4738 | |
| 4739 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4740 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4741 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4742 | |
| 4743 | /* Push in the stack the "response" entry. */ |
| 4744 | ret = lua_getfield(L, 1, "response"); |
| 4745 | if (ret != LUA_TTABLE) { |
| 4746 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4747 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4748 | WILL_LJMP(lua_error(L)); |
| 4749 | } |
| 4750 | |
| 4751 | /* check if the header is already registered if it is not |
| 4752 | * the case, register it. |
| 4753 | */ |
| 4754 | ret = lua_getfield(L, -1, name); |
| 4755 | if (ret == LUA_TNIL) { |
| 4756 | |
| 4757 | /* Entry not found. */ |
| 4758 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4759 | |
| 4760 | /* Insert the new header name in the array in the top of the stack. |
| 4761 | * It left the new array in the top of the stack. |
| 4762 | */ |
| 4763 | lua_newtable(L); |
| 4764 | lua_pushvalue(L, 2); |
| 4765 | lua_pushvalue(L, -2); |
| 4766 | lua_settable(L, -4); |
| 4767 | |
| 4768 | } else if (ret != LUA_TTABLE) { |
| 4769 | |
| 4770 | /* corruption error. */ |
| 4771 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4772 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4773 | WILL_LJMP(lua_error(L)); |
| 4774 | } |
| 4775 | |
| 4776 | /* Now the top od thestack is an array of values. We push |
| 4777 | * the header value as new entry. |
| 4778 | */ |
| 4779 | lua_pushvalue(L, 3); |
| 4780 | ret = lua_rawlen(L, -2); |
| 4781 | lua_rawseti(L, -2, ret + 1); |
| 4782 | lua_pushboolean(L, 1); |
| 4783 | return 1; |
| 4784 | } |
| 4785 | |
| 4786 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4787 | { |
| 4788 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4789 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4790 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4791 | |
| 4792 | if (status < 100 || status > 599) { |
| 4793 | lua_pushboolean(L, 0); |
| 4794 | return 1; |
| 4795 | } |
| 4796 | |
| 4797 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4798 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4799 | lua_pushboolean(L, 1); |
| 4800 | return 1; |
| 4801 | } |
| 4802 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4803 | |
| 4804 | __LJMP static int hlua_applet_htx_send_response(lua_State *L) |
| 4805 | { |
| 4806 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4807 | struct stream_interface *si = appctx->appctx->owner; |
| 4808 | struct channel *res = si_ic(si); |
| 4809 | struct htx *htx; |
| 4810 | struct htx_sl *sl; |
| 4811 | struct h1m h1m; |
| 4812 | const char *status, *reason; |
| 4813 | const char *name, *value; |
| 4814 | size_t nlen, vlen; |
| 4815 | unsigned int flags; |
| 4816 | |
| 4817 | /* Send the message at once. */ |
| 4818 | htx = htx_from_buf(&res->buf); |
| 4819 | h1m_init_res(&h1m); |
| 4820 | |
| 4821 | /* Use the same http version than the request. */ |
| 4822 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4823 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4824 | if (reason == NULL) |
| 4825 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4826 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4827 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4828 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4829 | } |
| 4830 | else { |
| 4831 | flags = HTX_SL_F_IS_RESP; |
| 4832 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4833 | } |
| 4834 | if (!sl) { |
| 4835 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4836 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4837 | WILL_LJMP(lua_error(L)); |
| 4838 | } |
| 4839 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4840 | |
| 4841 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4842 | lua_pushvalue(L, 0); |
| 4843 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4844 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4845 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4846 | WILL_LJMP(lua_error(L)); |
| 4847 | } |
| 4848 | |
| 4849 | /* Browse the list of headers. */ |
| 4850 | lua_pushnil(L); |
| 4851 | while(lua_next(L, -2) != 0) { |
| 4852 | /* We expect a string as -2. */ |
| 4853 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4854 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4855 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4856 | lua_typename(L, lua_type(L, -2))); |
| 4857 | WILL_LJMP(lua_error(L)); |
| 4858 | } |
| 4859 | name = lua_tolstring(L, -2, &nlen); |
| 4860 | |
| 4861 | /* We expect an array as -1. */ |
| 4862 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4863 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4864 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4865 | name, |
| 4866 | lua_typename(L, lua_type(L, -1))); |
| 4867 | WILL_LJMP(lua_error(L)); |
| 4868 | } |
| 4869 | |
| 4870 | /* Browse the table who is on the top of the stack. */ |
| 4871 | lua_pushnil(L); |
| 4872 | while(lua_next(L, -2) != 0) { |
| 4873 | int id; |
| 4874 | |
| 4875 | /* We expect a number as -2. */ |
| 4876 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4877 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4878 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4879 | name, |
| 4880 | lua_typename(L, lua_type(L, -2))); |
| 4881 | WILL_LJMP(lua_error(L)); |
| 4882 | } |
| 4883 | id = lua_tointeger(L, -2); |
| 4884 | |
| 4885 | /* We expect a string as -2. */ |
| 4886 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4887 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4888 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4889 | name, id, |
| 4890 | lua_typename(L, lua_type(L, -1))); |
| 4891 | WILL_LJMP(lua_error(L)); |
| 4892 | } |
| 4893 | value = lua_tolstring(L, -1, &vlen); |
| 4894 | |
| 4895 | /* Simple Protocol checks. */ |
| 4896 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
| 4897 | h1_parse_xfer_enc_header(&h1m, ist2(name, nlen)); |
| 4898 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4899 | struct ist v = ist2(value, vlen); |
| 4900 | int ret; |
| 4901 | |
| 4902 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4903 | if (ret < 0) { |
| 4904 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4905 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4906 | name); |
| 4907 | WILL_LJMP(lua_error(L)); |
| 4908 | } |
| 4909 | else if (ret == 0) |
| 4910 | goto next; /* Skip it */ |
| 4911 | } |
| 4912 | |
| 4913 | /* Add a new header */ |
| 4914 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4915 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4916 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4917 | name); |
| 4918 | WILL_LJMP(lua_error(L)); |
| 4919 | } |
| 4920 | next: |
| 4921 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4922 | lua_pop(L, 1); |
| 4923 | } |
| 4924 | |
| 4925 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4926 | lua_pop(L, 1); |
| 4927 | } |
| 4928 | |
| 4929 | if (h1m.flags & H1_MF_CHNK) |
| 4930 | h1m.flags &= ~H1_MF_CLEN; |
| 4931 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4932 | h1m.flags |= H1_MF_XFER_LEN; |
| 4933 | |
| 4934 | /* Uset HTX start-line flags */ |
| 4935 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4936 | flags |= HTX_SL_F_XFER_ENC; |
| 4937 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4938 | flags |= HTX_SL_F_XFER_LEN; |
| 4939 | if (h1m.flags & H1_MF_CHNK) |
| 4940 | flags |= HTX_SL_F_CHNK; |
| 4941 | else if (h1m.flags & H1_MF_CLEN) |
| 4942 | flags |= HTX_SL_F_CLEN; |
| 4943 | if (h1m.body_len == 0) |
| 4944 | flags |= HTX_SL_F_BODYLESS; |
| 4945 | } |
| 4946 | sl->flags |= flags; |
| 4947 | |
| 4948 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4949 | * and the status code implies the presence of a message body, we must |
| 4950 | * announce a transfer encoding chunked. This is required by haproxy |
| 4951 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4952 | * chunked itslef, don't do anything. |
| 4953 | */ |
| 4954 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4955 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4956 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4957 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4958 | /* Add a new header */ |
| 4959 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4960 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4961 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4962 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4963 | WILL_LJMP(lua_error(L)); |
| 4964 | } |
| 4965 | } |
| 4966 | |
| 4967 | /* Finalize headers. */ |
| 4968 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4969 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4970 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4971 | WILL_LJMP(lua_error(L)); |
| 4972 | } |
| 4973 | |
| 4974 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4975 | b_reset(&res->buf); |
| 4976 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4977 | WILL_LJMP(lua_error(L)); |
| 4978 | } |
| 4979 | |
| 4980 | htx_to_buf(htx, &res->buf); |
| 4981 | res->total += htx->data; |
| 4982 | res->flags |= CF_READ_PARTIAL; |
| 4983 | |
| 4984 | /* Headers sent, set the flag. */ |
| 4985 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4986 | return 0; |
| 4987 | |
| 4988 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4989 | /* We will build the status line and the headers of the HTTP response. |
| 4990 | * We will try send at once if its not possible, we give back the hand |
| 4991 | * waiting for more room. |
| 4992 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4993 | __LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4994 | { |
| 4995 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4996 | struct stream_interface *si = appctx->appctx->owner; |
| 4997 | struct channel *res = si_ic(si); |
| 4998 | |
| 4999 | if (co_data(res)) { |
| 5000 | si_rx_room_blk(si); |
| 5001 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0)); |
| 5002 | } |
| 5003 | return MAY_LJMP(hlua_applet_htx_send_response(L)); |
| 5004 | } |
| 5005 | |
| 5006 | |
| 5007 | __LJMP static int hlua_applet_htx_start_response(lua_State *L) |
| 5008 | { |
| 5009 | return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0)); |
| 5010 | } |
| 5011 | |
| 5012 | /* We will build the status line and the headers of the HTTP response. |
| 5013 | * We will try send at once if its not possible, we give back the hand |
| 5014 | * waiting for more room. |
| 5015 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5016 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 5017 | { |
| 5018 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5019 | struct stream_interface *si = appctx->appctx->owner; |
| 5020 | struct channel *chn = si_ic(si); |
| 5021 | int ret; |
| 5022 | size_t len; |
| 5023 | const char *msg; |
| 5024 | |
| 5025 | /* Get the message as the first argument on the stack. */ |
| 5026 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5027 | |
| 5028 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 5029 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5030 | |
| 5031 | /* if ret == -2 or -3 the channel closed or the message si too |
| 5032 | * big for the buffers. |
| 5033 | */ |
| 5034 | if (ret == -2 || ret == -3) { |
| 5035 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5036 | WILL_LJMP(lua_error(L)); |
| 5037 | } |
| 5038 | |
| 5039 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 5040 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 5041 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5042 | 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] | 5043 | } |
| 5044 | |
| 5045 | /* Headers sent, set the flag. */ |
| 5046 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 5047 | return 0; |
| 5048 | } |
| 5049 | |
| 5050 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 5051 | { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5052 | struct buffer *tmp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5053 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5054 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5055 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5056 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5057 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5058 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5059 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5060 | int hdr_chunked = 0; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5061 | const char *reason; |
| 5062 | |
| 5063 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 5064 | return MAY_LJMP(hlua_applet_htx_start_response(L)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5065 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5066 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5067 | if (reason == NULL) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 5068 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5069 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5070 | tmp = get_trash_chunk(); |
| 5071 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5072 | /* Use the same http version than the request. */ |
| 5073 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 5074 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5075 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5076 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5077 | |
| 5078 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5079 | lua_pushvalue(L, 0); |
| 5080 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5081 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 5082 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 5083 | WILL_LJMP(lua_error(L)); |
| 5084 | } |
| 5085 | |
| 5086 | /* Browse the list of headers. */ |
| 5087 | lua_pushnil(L); |
| 5088 | while(lua_next(L, -2) != 0) { |
| 5089 | |
| 5090 | /* We expect a string as -2. */ |
| 5091 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5092 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 5093 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5094 | lua_typename(L, lua_type(L, -2))); |
| 5095 | WILL_LJMP(lua_error(L)); |
| 5096 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5097 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5098 | |
| 5099 | /* We expect an array as -1. */ |
| 5100 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5101 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 5102 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5103 | name, |
| 5104 | lua_typename(L, lua_type(L, -1))); |
| 5105 | WILL_LJMP(lua_error(L)); |
| 5106 | } |
| 5107 | |
| 5108 | /* Browse the table who is on the top of the stack. */ |
| 5109 | lua_pushnil(L); |
| 5110 | while(lua_next(L, -2) != 0) { |
| 5111 | |
| 5112 | /* We expect a number as -2. */ |
| 5113 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5114 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 5115 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5116 | name, |
| 5117 | lua_typename(L, lua_type(L, -2))); |
| 5118 | WILL_LJMP(lua_error(L)); |
| 5119 | } |
| 5120 | id = lua_tointeger(L, -2); |
| 5121 | |
| 5122 | /* We expect a string as -2. */ |
| 5123 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5124 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 5125 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5126 | name, id, |
| 5127 | lua_typename(L, lua_type(L, -1))); |
| 5128 | WILL_LJMP(lua_error(L)); |
| 5129 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5130 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5131 | |
| 5132 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5133 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 5134 | memcpy(tmp->area + tmp->data, name, name_len); |
| 5135 | tmp->data += name_len; |
| 5136 | tmp->area[tmp->data++] = ':'; |
| 5137 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5138 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5139 | memcpy(tmp->area + tmp->data, value, |
| 5140 | value_len); |
| 5141 | tmp->data += value_len; |
| 5142 | tmp->area[tmp->data++] = '\r'; |
| 5143 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5144 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5145 | |
| 5146 | /* Protocol checks. */ |
| 5147 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5148 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5149 | * is done without control. If it contains a bad value, |
| 5150 | * the content-length remains negative so that we can |
| 5151 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5152 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5153 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5154 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5155 | |
| 5156 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5157 | if (name_len == 17 && value_len == 7 && |
| 5158 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5159 | strcasecmp("chunked", value) == 0) |
| 5160 | hdr_chunked = 1; |
| 5161 | |
| 5162 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5163 | lua_pop(L, 1); |
| 5164 | } |
| 5165 | |
| 5166 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5167 | lua_pop(L, 1); |
| 5168 | } |
| 5169 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5170 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 5171 | * and the status code implies the presence of a message body, we must |
| 5172 | * announce a transfer encoding chunked. This is required by haproxy |
| 5173 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 5174 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5175 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5176 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5177 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 5178 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5179 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5180 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5181 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 5182 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 5183 | } |
| 5184 | |
| 5185 | /* Finalize headers. */ |
| 5186 | chunk_appendf(tmp, "\r\n"); |
| 5187 | |
| 5188 | /* Remove the last entry and the array of headers */ |
| 5189 | lua_pop(L, 2); |
| 5190 | |
| 5191 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5192 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5193 | |
| 5194 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 5195 | } |
| 5196 | |
| 5197 | /* |
| 5198 | * |
| 5199 | * |
| 5200 | * Class HTTP |
| 5201 | * |
| 5202 | * |
| 5203 | */ |
| 5204 | |
| 5205 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5206 | * a class stream, otherwise it throws an error. |
| 5207 | */ |
| 5208 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 5209 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5210 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5211 | } |
| 5212 | |
| 5213 | /* This function creates and push in the stack a HTTP object |
| 5214 | * according with a current TXN. |
| 5215 | */ |
| 5216 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5217 | { |
| 5218 | struct hlua_txn *htxn; |
| 5219 | |
| 5220 | /* Check stack size. */ |
| 5221 | if (!lua_checkstack(L, 3)) |
| 5222 | return 0; |
| 5223 | |
| 5224 | /* Create the object: obj[0] = userdata. |
| 5225 | * Note that the base of the Converters object is the |
| 5226 | * same than the TXN object. |
| 5227 | */ |
| 5228 | lua_newtable(L); |
| 5229 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5230 | lua_rawseti(L, -2, 0); |
| 5231 | |
| 5232 | htxn->s = txn->s; |
| 5233 | htxn->p = txn->p; |
| 5234 | |
| 5235 | /* Pop a class stream metatable and affect it to the table. */ |
| 5236 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5237 | lua_setmetatable(L, -2); |
| 5238 | |
| 5239 | return 1; |
| 5240 | } |
| 5241 | |
| 5242 | /* This function creates ans returns an array of HTTP headers. |
| 5243 | * This function does not fails. It is used as wrapper with the |
| 5244 | * 2 following functions. |
| 5245 | */ |
| 5246 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5247 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5248 | /* Create the table. */ |
| 5249 | lua_newtable(L); |
| 5250 | |
| 5251 | if (!htxn->s->txn) |
| 5252 | return 1; |
| 5253 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5254 | if (IS_HTX_STRM(htxn->s)) { |
| 5255 | /* HTX version */ |
| 5256 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5257 | int32_t pos; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5258 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5259 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5260 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5261 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5262 | struct ist n, v; |
| 5263 | int len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5264 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5265 | if (type == HTX_BLK_HDR) { |
| 5266 | n = htx_get_blk_name(htx,blk); |
| 5267 | v = htx_get_blk_value(htx, blk); |
| 5268 | } |
| 5269 | else if (type == HTX_BLK_EOH) |
| 5270 | break; |
| 5271 | else |
| 5272 | continue; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5273 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5274 | /* Check for existing entry: |
| 5275 | * assume that the table is on the top of the stack, and |
| 5276 | * push the key in the stack, the function lua_gettable() |
| 5277 | * perform the lookup. |
| 5278 | */ |
| 5279 | lua_pushlstring(L, n.ptr, n.len); |
| 5280 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5281 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5282 | switch (lua_type(L, -1)) { |
| 5283 | case LUA_TNIL: |
| 5284 | /* Table not found, create it. */ |
| 5285 | lua_pop(L, 1); /* remove the nil value. */ |
| 5286 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5287 | lua_newtable(L); /* create and push empty table. */ |
| 5288 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5289 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5290 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5291 | break; |
| 5292 | |
| 5293 | case LUA_TTABLE: |
| 5294 | /* Entry found: push the value in the table. */ |
| 5295 | len = lua_rawlen(L, -1); |
| 5296 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5297 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5298 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5299 | break; |
| 5300 | |
| 5301 | default: |
| 5302 | /* Other cases are errors. */ |
| 5303 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5304 | WILL_LJMP(lua_error(L)); |
| 5305 | } |
| 5306 | } |
| 5307 | } |
| 5308 | else { |
| 5309 | /* Legacy HTTP version */ |
| 5310 | const char *cur_ptr, *cur_next, *p; |
| 5311 | int old_idx, cur_idx; |
| 5312 | struct hdr_idx_elem *cur_hdr; |
| 5313 | const char *hn, *hv; |
| 5314 | int hnl, hvl; |
| 5315 | const char *in; |
| 5316 | char *out; |
| 5317 | int len; |
| 5318 | |
| 5319 | /* Build array of headers. */ |
| 5320 | old_idx = 0; |
| 5321 | cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 5322 | |
| 5323 | while (1) { |
| 5324 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 5325 | if (!cur_idx) |
| 5326 | break; |
| 5327 | old_idx = cur_idx; |
| 5328 | |
| 5329 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 5330 | cur_ptr = cur_next; |
| 5331 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 5332 | |
| 5333 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 5334 | * and the next header starts at cur_next. We'll check |
| 5335 | * this header in the list as well as against the default |
| 5336 | * rule. |
| 5337 | */ |
| 5338 | |
| 5339 | /* look for ': *'. */ |
| 5340 | hn = cur_ptr; |
| 5341 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 5342 | if (p >= cur_ptr+cur_hdr->len) |
| 5343 | continue; |
| 5344 | hnl = p - hn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5345 | p++; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5346 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 5347 | p++; |
| 5348 | if (p >= cur_ptr+cur_hdr->len) |
| 5349 | continue; |
| 5350 | hv = p; |
| 5351 | hvl = cur_ptr+cur_hdr->len-p; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5352 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5353 | /* Lowercase the key. Don't check the size of trash, it have |
| 5354 | * the size of one buffer and the input data contains in one |
| 5355 | * buffer. |
| 5356 | */ |
| 5357 | out = trash.area; |
| 5358 | for (in=hn; in<hn+hnl; in++, out++) |
| 5359 | *out = tolower(*in); |
| 5360 | *out = '\0'; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5361 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5362 | /* Check for existing entry: |
| 5363 | * assume that the table is on the top of the stack, and |
| 5364 | * push the key in the stack, the function lua_gettable() |
| 5365 | * perform the lookup. |
| 5366 | */ |
| 5367 | lua_pushlstring(L, trash.area, hnl); |
| 5368 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5369 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5370 | switch (lua_type(L, -1)) { |
| 5371 | case LUA_TNIL: |
| 5372 | /* Table not found, create it. */ |
| 5373 | lua_pop(L, 1); /* remove the nil value. */ |
| 5374 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
| 5375 | lua_newtable(L); /* create and push empty table. */ |
| 5376 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5377 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5378 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5379 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5380 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5381 | case LUA_TTABLE: |
| 5382 | /* Entry found: push the value in the table. */ |
| 5383 | len = lua_rawlen(L, -1); |
| 5384 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5385 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5386 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5387 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5388 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5389 | default: |
| 5390 | /* Other cases are errors. */ |
| 5391 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5392 | WILL_LJMP(lua_error(L)); |
| 5393 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5394 | } |
| 5395 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5396 | return 1; |
| 5397 | } |
| 5398 | |
| 5399 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5400 | { |
| 5401 | struct hlua_txn *htxn; |
| 5402 | |
| 5403 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5404 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5405 | |
| 5406 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 5407 | } |
| 5408 | |
| 5409 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5410 | { |
| 5411 | struct hlua_txn *htxn; |
| 5412 | |
| 5413 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5414 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5415 | |
| 5416 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 5417 | } |
| 5418 | |
| 5419 | /* This function replace full header, or just a value in |
| 5420 | * the request or in the response. It is a wrapper fir the |
| 5421 | * 4 following functions. |
| 5422 | */ |
| 5423 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 5424 | struct http_msg *msg, int action) |
| 5425 | { |
| 5426 | size_t name_len; |
| 5427 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5428 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5429 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 5430 | struct my_regex re; |
| 5431 | |
| 5432 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 5433 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5434 | |
| 5435 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 5436 | regex_free(&re); |
| 5437 | return 0; |
| 5438 | } |
| 5439 | |
| 5440 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5441 | { |
| 5442 | struct hlua_txn *htxn; |
| 5443 | |
| 5444 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5445 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5446 | |
| 5447 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 5448 | } |
| 5449 | |
| 5450 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5451 | { |
| 5452 | struct hlua_txn *htxn; |
| 5453 | |
| 5454 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5455 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5456 | |
| 5457 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 5458 | } |
| 5459 | |
| 5460 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5461 | { |
| 5462 | struct hlua_txn *htxn; |
| 5463 | |
| 5464 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5465 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5466 | |
| 5467 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 5468 | } |
| 5469 | |
| 5470 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5471 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5472 | struct hlua_txn *htxn; |
| 5473 | |
| 5474 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5475 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5476 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 5477 | 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] | 5478 | } |
| 5479 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5480 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5481 | * It is a wrapper for the 2 following functions. |
| 5482 | */ |
| 5483 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5484 | { |
| 5485 | size_t len; |
| 5486 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5487 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5488 | if (IS_HTX_STRM(htxn->s)) { |
| 5489 | /* HTX version */ |
| 5490 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5491 | struct http_hdr_ctx ctx; |
| 5492 | |
| 5493 | ctx.blk = NULL; |
| 5494 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5495 | http_remove_header(htx, &ctx); |
| 5496 | } |
| 5497 | else { |
| 5498 | /* Legacy HTTP version */ |
| 5499 | struct hdr_ctx ctx; |
| 5500 | struct http_txn *txn = htxn->s->txn; |
| 5501 | |
| 5502 | ctx.idx = 0; |
| 5503 | while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) |
| 5504 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5505 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5506 | return 0; |
| 5507 | } |
| 5508 | |
| 5509 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5510 | { |
| 5511 | struct hlua_txn *htxn; |
| 5512 | |
| 5513 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5514 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5515 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5516 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5517 | } |
| 5518 | |
| 5519 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5520 | { |
| 5521 | struct hlua_txn *htxn; |
| 5522 | |
| 5523 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5524 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5525 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5526 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5527 | } |
| 5528 | |
| 5529 | /* This function adds an header. It is a wrapper used by |
| 5530 | * the 2 following functions. |
| 5531 | */ |
| 5532 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5533 | { |
| 5534 | size_t name_len; |
| 5535 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5536 | size_t value_len; |
| 5537 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 5538 | char *p; |
| 5539 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5540 | if (IS_HTX_STRM(htxn->s)) { |
| 5541 | /* HTX version */ |
| 5542 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5543 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5544 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5545 | ist2(value, value_len))); |
| 5546 | } |
| 5547 | else { |
| 5548 | /* Legacy HTTP version */ |
| 5549 | /* Check length. */ |
| 5550 | trash.data = value_len + name_len + 2; |
| 5551 | if (trash.data > trash.size) |
| 5552 | return 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5553 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5554 | /* Creates the header string. */ |
| 5555 | p = trash.area; |
| 5556 | memcpy(p, name, name_len); |
| 5557 | p += name_len; |
| 5558 | *p = ':'; |
| 5559 | p++; |
| 5560 | *p = ' '; |
| 5561 | p++; |
| 5562 | memcpy(p, value, value_len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5563 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5564 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
| 5565 | trash.area, trash.data) != 0); |
| 5566 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5567 | return 0; |
| 5568 | } |
| 5569 | |
| 5570 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5571 | { |
| 5572 | struct hlua_txn *htxn; |
| 5573 | |
| 5574 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5575 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5576 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5577 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5578 | } |
| 5579 | |
| 5580 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5581 | { |
| 5582 | struct hlua_txn *htxn; |
| 5583 | |
| 5584 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5585 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5586 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5587 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5588 | } |
| 5589 | |
| 5590 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5591 | { |
| 5592 | struct hlua_txn *htxn; |
| 5593 | |
| 5594 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5595 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5596 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5597 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 5598 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5599 | } |
| 5600 | |
| 5601 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5602 | { |
| 5603 | struct hlua_txn *htxn; |
| 5604 | |
| 5605 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5606 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5607 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5608 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 5609 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5610 | } |
| 5611 | |
| 5612 | /* This function set the method. */ |
| 5613 | static int hlua_http_req_set_meth(lua_State *L) |
| 5614 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5615 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5616 | size_t name_len; |
| 5617 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5618 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5619 | lua_pushboolean(L, http_replace_req_line(0, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5620 | return 1; |
| 5621 | } |
| 5622 | |
| 5623 | /* This function set the method. */ |
| 5624 | static int hlua_http_req_set_path(lua_State *L) |
| 5625 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5626 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5627 | size_t name_len; |
| 5628 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5629 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5630 | 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] | 5631 | return 1; |
| 5632 | } |
| 5633 | |
| 5634 | /* This function set the query-string. */ |
| 5635 | static int hlua_http_req_set_query(lua_State *L) |
| 5636 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5637 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5638 | size_t name_len; |
| 5639 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5640 | |
| 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 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5665 | 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] | 5666 | return 1; |
| 5667 | } |
| 5668 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5669 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5670 | static int hlua_http_res_set_status(lua_State *L) |
| 5671 | { |
| 5672 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5673 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5674 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5675 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5676 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5677 | return 0; |
| 5678 | } |
| 5679 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5680 | /* |
| 5681 | * |
| 5682 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5683 | * Class TXN |
| 5684 | * |
| 5685 | * |
| 5686 | */ |
| 5687 | |
| 5688 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5689 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5690 | */ |
| 5691 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5692 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5693 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5694 | } |
| 5695 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5696 | __LJMP static int hlua_set_var(lua_State *L) |
| 5697 | { |
| 5698 | struct hlua_txn *htxn; |
| 5699 | const char *name; |
| 5700 | size_t len; |
| 5701 | struct sample smp; |
| 5702 | |
| 5703 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5704 | |
| 5705 | /* It is useles to retrieve the stream, but this function |
| 5706 | * runs only in a stream context. |
| 5707 | */ |
| 5708 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5709 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5710 | |
| 5711 | /* Converts the third argument in a sample. */ |
| 5712 | hlua_lua2smp(L, 3, &smp); |
| 5713 | |
| 5714 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5715 | 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] | 5716 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5717 | return 0; |
| 5718 | } |
| 5719 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5720 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5721 | { |
| 5722 | struct hlua_txn *htxn; |
| 5723 | const char *name; |
| 5724 | size_t len; |
| 5725 | struct sample smp; |
| 5726 | |
| 5727 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5728 | |
| 5729 | /* It is useles to retrieve the stream, but this function |
| 5730 | * runs only in a stream context. |
| 5731 | */ |
| 5732 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5733 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5734 | |
| 5735 | /* Unset the variable. */ |
| 5736 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5737 | vars_unset_by_name(name, len, &smp); |
| 5738 | return 0; |
| 5739 | } |
| 5740 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5741 | __LJMP static int hlua_get_var(lua_State *L) |
| 5742 | { |
| 5743 | struct hlua_txn *htxn; |
| 5744 | const char *name; |
| 5745 | size_t len; |
| 5746 | struct sample smp; |
| 5747 | |
| 5748 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5749 | |
| 5750 | /* It is useles to retrieve the stream, but this function |
| 5751 | * runs only in a stream context. |
| 5752 | */ |
| 5753 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5754 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5755 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5756 | 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] | 5757 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5758 | lua_pushnil(L); |
| 5759 | return 1; |
| 5760 | } |
| 5761 | |
| 5762 | return hlua_smp2lua(L, &smp); |
| 5763 | } |
| 5764 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5765 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5766 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5767 | struct hlua *hlua; |
| 5768 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5769 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5770 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5771 | /* It is useles to retrieve the stream, but this function |
| 5772 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5773 | */ |
| 5774 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5775 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5776 | |
| 5777 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5778 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5779 | |
| 5780 | /* Get and store new value. */ |
| 5781 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5782 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5783 | |
| 5784 | return 0; |
| 5785 | } |
| 5786 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5787 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5788 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5789 | struct hlua *hlua; |
| 5790 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5791 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5792 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5793 | /* It is useles to retrieve the stream, but this function |
| 5794 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5795 | */ |
| 5796 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5797 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5798 | |
| 5799 | /* Push configuration index in the stack. */ |
| 5800 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5801 | |
| 5802 | return 1; |
| 5803 | } |
| 5804 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5805 | /* Create stack entry containing a class TXN. This function |
| 5806 | * return 0 if the stack does not contains free slots, |
| 5807 | * otherwise it returns 1. |
| 5808 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5809 | 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] | 5810 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5811 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5812 | |
| 5813 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5814 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5815 | return 0; |
| 5816 | |
| 5817 | /* NOTE: The allocation never fails. The failure |
| 5818 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5819 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5820 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5821 | /* Create the object: obj[0] = userdata. */ |
| 5822 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5823 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5824 | lua_rawseti(L, -2, 0); |
| 5825 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5826 | htxn->s = s; |
| 5827 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5828 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5829 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5830 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5831 | /* Create the "f" field that contains a list of fetches. */ |
| 5832 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5833 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5834 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5835 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5836 | |
| 5837 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5838 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5839 | 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] | 5840 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5841 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5842 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5843 | /* Create the "c" field that contains a list of converters. */ |
| 5844 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5845 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5846 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5847 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5848 | |
| 5849 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5850 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5851 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +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 | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5854 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5855 | /* Create the "req" field that contains the request channel object. */ |
| 5856 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5857 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +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 | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5860 | |
| 5861 | /* Create the "res" field that contains the response channel object. */ |
| 5862 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5863 | if (!hlua_channel_new(L, &s->res)) |
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 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5867 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5868 | lua_pushstring(L, "http"); |
| 5869 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5870 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5871 | return 0; |
| 5872 | } |
| 5873 | else |
| 5874 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5875 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5876 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5877 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5878 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5879 | lua_setmetatable(L, -2); |
| 5880 | |
| 5881 | return 1; |
| 5882 | } |
| 5883 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5884 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5885 | { |
| 5886 | const char *msg; |
| 5887 | struct hlua_txn *htxn; |
| 5888 | |
| 5889 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5890 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5891 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5892 | |
| 5893 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5894 | return 0; |
| 5895 | } |
| 5896 | |
| 5897 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5898 | { |
| 5899 | int level; |
| 5900 | const char *msg; |
| 5901 | struct hlua_txn *htxn; |
| 5902 | |
| 5903 | MAY_LJMP(check_args(L, 3, "log")); |
| 5904 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5905 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5906 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5907 | |
| 5908 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5909 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5910 | |
| 5911 | hlua_sendlog(htxn->s->be, level, msg); |
| 5912 | return 0; |
| 5913 | } |
| 5914 | |
| 5915 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5916 | { |
| 5917 | const char *msg; |
| 5918 | struct hlua_txn *htxn; |
| 5919 | |
| 5920 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5921 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5922 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5923 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5924 | return 0; |
| 5925 | } |
| 5926 | |
| 5927 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5928 | { |
| 5929 | const char *msg; |
| 5930 | struct hlua_txn *htxn; |
| 5931 | |
| 5932 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5933 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5934 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5935 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5936 | return 0; |
| 5937 | } |
| 5938 | |
| 5939 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5940 | { |
| 5941 | const char *msg; |
| 5942 | struct hlua_txn *htxn; |
| 5943 | |
| 5944 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5945 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5946 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5947 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5948 | return 0; |
| 5949 | } |
| 5950 | |
| 5951 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5952 | { |
| 5953 | const char *msg; |
| 5954 | struct hlua_txn *htxn; |
| 5955 | |
| 5956 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5957 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5958 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5959 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5960 | return 0; |
| 5961 | } |
| 5962 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5963 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5964 | { |
| 5965 | struct hlua_txn *htxn; |
| 5966 | int ll; |
| 5967 | |
| 5968 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5969 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5970 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5971 | |
| 5972 | if (ll < 0 || ll > 7) |
| 5973 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5974 | |
| 5975 | htxn->s->logs.level = ll; |
| 5976 | return 0; |
| 5977 | } |
| 5978 | |
| 5979 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5980 | { |
| 5981 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5982 | int tos; |
| 5983 | |
| 5984 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5985 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5986 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5987 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5988 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5989 | return 0; |
| 5990 | } |
| 5991 | |
| 5992 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5993 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5994 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5995 | int mark; |
| 5996 | |
| 5997 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5998 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5999 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6000 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 6001 | conn_set_tos(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6002 | return 0; |
| 6003 | } |
| 6004 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 6005 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 6006 | { |
| 6007 | struct hlua_txn *htxn; |
| 6008 | |
| 6009 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 6010 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6011 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6012 | return 0; |
| 6013 | } |
| 6014 | |
| 6015 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 6016 | { |
| 6017 | struct hlua_txn *htxn; |
| 6018 | |
| 6019 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 6020 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6021 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6022 | return 0; |
| 6023 | } |
| 6024 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6025 | /* This function is an Lua binding that send pending data |
| 6026 | * to the client, and close the stream interface. |
| 6027 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6028 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6029 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6030 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6031 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6032 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6033 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6034 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6035 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6036 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6037 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6038 | /* If the flags NOTERM is set, we cannot terminate the http |
| 6039 | * session, so we just end the execution of the current |
| 6040 | * lua code. |
| 6041 | */ |
| 6042 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 6043 | WILL_LJMP(hlua_done(L)); |
| 6044 | return 0; |
| 6045 | } |
| 6046 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6047 | ic = &htxn->s->req; |
| 6048 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6049 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6050 | if (htxn->s->txn) { |
| 6051 | /* HTTP mode, let's stay in sync with the stream */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6052 | b_del(&ic->buf, htxn->s->txn->req.sov); |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6053 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 6054 | htxn->s->txn->req.sov = 0; |
| 6055 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 6056 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 6057 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 6058 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 6059 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6060 | /* Note that if we want to support keep-alive, we need |
| 6061 | * to bypass the close/shutr_now calls below, but that |
| 6062 | * may only be done if the HTTP request was already |
| 6063 | * processed and the connection header is known (ie |
| 6064 | * not during TCP rules). |
| 6065 | */ |
| 6066 | } |
| 6067 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6068 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6069 | channel_abort(ic); |
| 6070 | channel_auto_close(ic); |
| 6071 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6072 | |
| 6073 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6074 | channel_auto_read(oc); |
| 6075 | channel_auto_close(oc); |
| 6076 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6077 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 6078 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6079 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6080 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6081 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6082 | return 0; |
| 6083 | } |
| 6084 | |
| 6085 | __LJMP static int hlua_log(lua_State *L) |
| 6086 | { |
| 6087 | int level; |
| 6088 | const char *msg; |
| 6089 | |
| 6090 | MAY_LJMP(check_args(L, 2, "log")); |
| 6091 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6092 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6093 | |
| 6094 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6095 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6096 | |
| 6097 | hlua_sendlog(NULL, level, msg); |
| 6098 | return 0; |
| 6099 | } |
| 6100 | |
| 6101 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6102 | { |
| 6103 | const char *msg; |
| 6104 | |
| 6105 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6106 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6107 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6108 | return 0; |
| 6109 | } |
| 6110 | |
| 6111 | __LJMP static int hlua_log_info(lua_State *L) |
| 6112 | { |
| 6113 | const char *msg; |
| 6114 | |
| 6115 | MAY_LJMP(check_args(L, 1, "info")); |
| 6116 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6117 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6118 | return 0; |
| 6119 | } |
| 6120 | |
| 6121 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6122 | { |
| 6123 | const char *msg; |
| 6124 | |
| 6125 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6126 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6127 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6128 | return 0; |
| 6129 | } |
| 6130 | |
| 6131 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6132 | { |
| 6133 | const char *msg; |
| 6134 | |
| 6135 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6136 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6137 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6138 | return 0; |
| 6139 | } |
| 6140 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6141 | __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] | 6142 | { |
| 6143 | int wakeup_ms = lua_tointeger(L, -1); |
| 6144 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6145 | 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] | 6146 | return 0; |
| 6147 | } |
| 6148 | |
| 6149 | __LJMP static int hlua_sleep(lua_State *L) |
| 6150 | { |
| 6151 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6152 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6153 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6154 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6155 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6156 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6157 | wakeup_ms = tick_add(now_ms, delay); |
| 6158 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6159 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6160 | 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] | 6161 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6162 | } |
| 6163 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6164 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6165 | { |
| 6166 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6167 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6168 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6169 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6170 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6171 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6172 | wakeup_ms = tick_add(now_ms, delay); |
| 6173 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6174 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6175 | 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] | 6176 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6177 | } |
| 6178 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6179 | /* This functionis an LUA binding. it permits to give back |
| 6180 | * the hand at the HAProxy scheduler. It is used when the |
| 6181 | * LUA processing consumes a lot of time. |
| 6182 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6183 | __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] | 6184 | { |
| 6185 | return 0; |
| 6186 | } |
| 6187 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6188 | __LJMP static int hlua_yield(lua_State *L) |
| 6189 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6190 | 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] | 6191 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6192 | } |
| 6193 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6194 | /* This function change the nice of the currently executed |
| 6195 | * task. It is used set low or high priority at the current |
| 6196 | * task. |
| 6197 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6198 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6199 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6200 | struct hlua *hlua; |
| 6201 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6202 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6203 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6204 | hlua = hlua_gethlua(L); |
| 6205 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6206 | |
| 6207 | /* If he task is not set, I'm in a start mode. */ |
| 6208 | if (!hlua || !hlua->task) |
| 6209 | return 0; |
| 6210 | |
| 6211 | if (nice < -1024) |
| 6212 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6213 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6214 | nice = 1024; |
| 6215 | |
| 6216 | hlua->task->nice = nice; |
| 6217 | return 0; |
| 6218 | } |
| 6219 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6220 | /* 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] | 6221 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6222 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6223 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6224 | * |
| 6225 | * Task wrapper are longjmp safe because the only one Lua code |
| 6226 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6227 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6228 | static struct task *hlua_process_task(struct task *task, void *context, unsigned short state) |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6229 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6230 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6231 | enum hlua_exec status; |
| 6232 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6233 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6234 | task_set_affinity(task, tid_bit); |
| 6235 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6236 | /* If it is the first call to the task, we must initialize the |
| 6237 | * execution timeouts. |
| 6238 | */ |
| 6239 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6240 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6241 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6242 | /* Execute the Lua code. */ |
| 6243 | status = hlua_ctx_resume(hlua, 1); |
| 6244 | |
| 6245 | switch (status) { |
| 6246 | /* finished or yield */ |
| 6247 | case HLUA_E_OK: |
| 6248 | hlua_ctx_destroy(hlua); |
| 6249 | task_delete(task); |
| 6250 | task_free(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6251 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6252 | break; |
| 6253 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6254 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6255 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6256 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6257 | break; |
| 6258 | |
| 6259 | /* finished with error. */ |
| 6260 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6261 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6262 | hlua_ctx_destroy(hlua); |
| 6263 | task_delete(task); |
| 6264 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6265 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6266 | break; |
| 6267 | |
| 6268 | case HLUA_E_ERR: |
| 6269 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6270 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6271 | hlua_ctx_destroy(hlua); |
| 6272 | task_delete(task); |
| 6273 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6274 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6275 | break; |
| 6276 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6277 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6278 | } |
| 6279 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6280 | /* This function is an LUA binding that register LUA function to be |
| 6281 | * executed after the HAProxy configuration parsing and before the |
| 6282 | * HAProxy scheduler starts. This function expect only one LUA |
| 6283 | * argument that is a function. This function returns nothing, but |
| 6284 | * throws if an error is encountered. |
| 6285 | */ |
| 6286 | __LJMP static int hlua_register_init(lua_State *L) |
| 6287 | { |
| 6288 | struct hlua_init_function *init; |
| 6289 | int ref; |
| 6290 | |
| 6291 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6292 | |
| 6293 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6294 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6295 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6296 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6297 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6298 | |
| 6299 | init->function_ref = ref; |
| 6300 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6301 | return 0; |
| 6302 | } |
| 6303 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6304 | /* This functio is an LUA binding. It permits to register a task |
| 6305 | * executed in parallel of the main HAroxy activity. The task is |
| 6306 | * created and it is set in the HAProxy scheduler. It can be called |
| 6307 | * from the "init" section, "post init" or during the runtime. |
| 6308 | * |
| 6309 | * Lua prototype: |
| 6310 | * |
| 6311 | * <none> core.register_task(<function>) |
| 6312 | */ |
| 6313 | static int hlua_register_task(lua_State *L) |
| 6314 | { |
| 6315 | struct hlua *hlua; |
| 6316 | struct task *task; |
| 6317 | int ref; |
| 6318 | |
| 6319 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6320 | |
| 6321 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6322 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6323 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6324 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6325 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6326 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6327 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6328 | if (!task) |
| 6329 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6330 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6331 | task->context = hlua; |
| 6332 | task->process = hlua_process_task; |
| 6333 | |
| 6334 | if (!hlua_ctx_init(hlua, task)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6335 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6336 | |
| 6337 | /* Restore the function in the stack. */ |
| 6338 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6339 | hlua->nargs = 0; |
| 6340 | |
| 6341 | /* Schedule task. */ |
| 6342 | task_schedule(task, now_ms); |
| 6343 | |
| 6344 | return 0; |
| 6345 | } |
| 6346 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6347 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6348 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6349 | * resume converters. |
| 6350 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6351 | 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] | 6352 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6353 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6354 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6355 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6356 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6357 | if (!stream) |
| 6358 | return 0; |
| 6359 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6360 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6361 | * Lua context can be not initialized. This behavior |
| 6362 | * permits to save performances because a systematic |
| 6363 | * Lua initialization cause 5% performances loss. |
| 6364 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6365 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6366 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6367 | if (!stream->hlua) { |
| 6368 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6369 | return 0; |
| 6370 | } |
| 6371 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 6372 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6373 | return 0; |
| 6374 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6375 | } |
| 6376 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6377 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6378 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6379 | |
| 6380 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6381 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6382 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6383 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6384 | else |
| 6385 | error = "critical error"; |
| 6386 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6387 | return 0; |
| 6388 | } |
| 6389 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6390 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6391 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6392 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6393 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6394 | return 0; |
| 6395 | } |
| 6396 | |
| 6397 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6398 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6399 | |
| 6400 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6401 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6402 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6403 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6404 | return 0; |
| 6405 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6406 | hlua_smp2lua(stream->hlua->T, smp); |
| 6407 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6408 | |
| 6409 | /* push keywords in the stack. */ |
| 6410 | if (arg_p) { |
| 6411 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6412 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6413 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6414 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6415 | return 0; |
| 6416 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6417 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6418 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6419 | } |
| 6420 | } |
| 6421 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6422 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6423 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6424 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6425 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6426 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6427 | } |
| 6428 | |
| 6429 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6430 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6431 | /* finished. */ |
| 6432 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6433 | /* If the stack is empty, the function fails. */ |
| 6434 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6435 | return 0; |
| 6436 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6437 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6438 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6439 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6440 | return 1; |
| 6441 | |
| 6442 | /* yield. */ |
| 6443 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6444 | 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] | 6445 | return 0; |
| 6446 | |
| 6447 | /* finished with error. */ |
| 6448 | case HLUA_E_ERRMSG: |
| 6449 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6450 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6451 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6452 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6453 | return 0; |
| 6454 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6455 | case HLUA_E_ETMOUT: |
| 6456 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6457 | return 0; |
| 6458 | |
| 6459 | case HLUA_E_NOMEM: |
| 6460 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6461 | return 0; |
| 6462 | |
| 6463 | case HLUA_E_YIELD: |
| 6464 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6465 | return 0; |
| 6466 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6467 | case HLUA_E_ERR: |
| 6468 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6469 | 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] | 6470 | |
| 6471 | default: |
| 6472 | return 0; |
| 6473 | } |
| 6474 | } |
| 6475 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6476 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6477 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6478 | * resume sample-fetches. This function will be called by the sample |
| 6479 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6480 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6481 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6482 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6483 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6484 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6485 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6486 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6487 | const struct buffer msg = { }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6488 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6489 | if (!stream) |
| 6490 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6491 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6492 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6493 | * Lua context can be not initialized. This behavior |
| 6494 | * permits to save performances because a systematic |
| 6495 | * Lua initialization cause 5% performances loss. |
| 6496 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6497 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6498 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6499 | if (!stream->hlua) { |
| 6500 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6501 | return 0; |
| 6502 | } |
| 6503 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 6504 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6505 | return 0; |
| 6506 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6507 | } |
| 6508 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6509 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6510 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6511 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6512 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6513 | |
| 6514 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6515 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6516 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6517 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6518 | else |
| 6519 | error = "critical error"; |
| 6520 | 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] | 6521 | return 0; |
| 6522 | } |
| 6523 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6524 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6525 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6526 | 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] | 6527 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6528 | return 0; |
| 6529 | } |
| 6530 | |
| 6531 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6532 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6533 | |
| 6534 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6535 | if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6536 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6537 | 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] | 6538 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6539 | return 0; |
| 6540 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6541 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6542 | |
| 6543 | /* push keywords in the stack. */ |
| 6544 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6545 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6546 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6547 | 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] | 6548 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6549 | return 0; |
| 6550 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6551 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6552 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6553 | } |
| 6554 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6555 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6556 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6557 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6558 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6559 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6560 | } |
| 6561 | |
| 6562 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6563 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6564 | /* finished. */ |
| 6565 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6566 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6567 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6568 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6569 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6570 | /* If the stack is empty, the function fails. */ |
| 6571 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6572 | return 0; |
| 6573 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6574 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6575 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6576 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6577 | |
| 6578 | /* Set the end of execution flag. */ |
| 6579 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6580 | return 1; |
| 6581 | |
| 6582 | /* yield. */ |
| 6583 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6584 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6585 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6586 | 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] | 6587 | return 0; |
| 6588 | |
| 6589 | /* finished with error. */ |
| 6590 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6591 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6592 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6593 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6594 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6595 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6596 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6597 | return 0; |
| 6598 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6599 | case HLUA_E_ETMOUT: |
| 6600 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6601 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6602 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6603 | return 0; |
| 6604 | |
| 6605 | case HLUA_E_NOMEM: |
| 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 | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6608 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6609 | return 0; |
| 6610 | |
| 6611 | case HLUA_E_YIELD: |
| 6612 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6613 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6614 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6615 | return 0; |
| 6616 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6617 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6618 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6619 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6620 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6621 | 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] | 6622 | |
| 6623 | default: |
| 6624 | return 0; |
| 6625 | } |
| 6626 | } |
| 6627 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6628 | /* This function is an LUA binding used for registering |
| 6629 | * "sample-conv" functions. It expects a converter name used |
| 6630 | * in the haproxy configuration file, and an LUA function. |
| 6631 | */ |
| 6632 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6633 | { |
| 6634 | struct sample_conv_kw_list *sck; |
| 6635 | const char *name; |
| 6636 | int ref; |
| 6637 | int len; |
| 6638 | struct hlua_function *fcn; |
| 6639 | |
| 6640 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6641 | |
| 6642 | /* First argument : converter name. */ |
| 6643 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6644 | |
| 6645 | /* Second argument : lua function. */ |
| 6646 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6647 | |
| 6648 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6649 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6650 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6651 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6652 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6653 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6654 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6655 | |
| 6656 | /* Fill fcn. */ |
| 6657 | fcn->name = strdup(name); |
| 6658 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6659 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6660 | fcn->function_ref = ref; |
| 6661 | |
| 6662 | /* List head */ |
| 6663 | sck->list.n = sck->list.p = NULL; |
| 6664 | |
| 6665 | /* converter keyword. */ |
| 6666 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6667 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6668 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6669 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6670 | |
| 6671 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6672 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6673 | 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] | 6674 | sck->kw[0].val_args = NULL; |
| 6675 | sck->kw[0].in_type = SMP_T_STR; |
| 6676 | sck->kw[0].out_type = SMP_T_STR; |
| 6677 | sck->kw[0].private = fcn; |
| 6678 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6679 | /* Register this new converter */ |
| 6680 | sample_register_convs(sck); |
| 6681 | |
| 6682 | return 0; |
| 6683 | } |
| 6684 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6685 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6686 | * "sample-fetch" functions. It expects a converter name used |
| 6687 | * in the haproxy configuration file, and an LUA function. |
| 6688 | */ |
| 6689 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6690 | { |
| 6691 | const char *name; |
| 6692 | int ref; |
| 6693 | int len; |
| 6694 | struct sample_fetch_kw_list *sfk; |
| 6695 | struct hlua_function *fcn; |
| 6696 | |
| 6697 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6698 | |
| 6699 | /* First argument : sample-fetch name. */ |
| 6700 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6701 | |
| 6702 | /* Second argument : lua function. */ |
| 6703 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6704 | |
| 6705 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6706 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6707 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6708 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6709 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6710 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6711 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6712 | |
| 6713 | /* Fill fcn. */ |
| 6714 | fcn->name = strdup(name); |
| 6715 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6716 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6717 | fcn->function_ref = ref; |
| 6718 | |
| 6719 | /* List head */ |
| 6720 | sfk->list.n = sfk->list.p = NULL; |
| 6721 | |
| 6722 | /* sample-fetch keyword. */ |
| 6723 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6724 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6725 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6726 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6727 | |
| 6728 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6729 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6730 | 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] | 6731 | sfk->kw[0].val_args = NULL; |
| 6732 | sfk->kw[0].out_type = SMP_T_STR; |
| 6733 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6734 | sfk->kw[0].val = 0; |
| 6735 | sfk->kw[0].private = fcn; |
| 6736 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6737 | /* Register this new fetch. */ |
| 6738 | sample_register_fetches(sfk); |
| 6739 | |
| 6740 | return 0; |
| 6741 | } |
| 6742 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6743 | /* This function is a wrapper to execute each LUA function declared |
| 6744 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6745 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6746 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6747 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6748 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6749 | 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] | 6750 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6751 | { |
| 6752 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6753 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6754 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6755 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6756 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6757 | |
| 6758 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 6759 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 6760 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 6761 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 6762 | case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6763 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6764 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6765 | return ACT_RET_CONT; |
| 6766 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6767 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6768 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6769 | * Lua context can be not initialized. This behavior |
| 6770 | * permits to save performances because a systematic |
| 6771 | * Lua initialization cause 5% performances loss. |
| 6772 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6773 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6774 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6775 | if (!s->hlua) { |
| 6776 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6777 | rule->arg.hlua_rule->fcn.name); |
| 6778 | return ACT_RET_CONT; |
| 6779 | } |
| 6780 | if (!hlua_ctx_init(s->hlua, s->task)) { |
| 6781 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6782 | rule->arg.hlua_rule->fcn.name); |
| 6783 | return ACT_RET_CONT; |
| 6784 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6785 | } |
| 6786 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6787 | consistency_set(s, dir, &s->hlua->cons); |
| 6788 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6789 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6790 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6791 | |
| 6792 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6793 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6794 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6795 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6796 | else |
| 6797 | error = "critical error"; |
| 6798 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6799 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6800 | return ACT_RET_CONT; |
| 6801 | } |
| 6802 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6803 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6804 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6805 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6806 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6807 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6808 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6809 | } |
| 6810 | |
| 6811 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6812 | 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] | 6813 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6814 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6815 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6816 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6817 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6818 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6819 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6820 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6821 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6822 | |
| 6823 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6824 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6825 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6826 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6827 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6828 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6829 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6830 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6831 | lua_pushstring(s->hlua->T, *arg); |
| 6832 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6833 | } |
| 6834 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6835 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6836 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6837 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6838 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6839 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6840 | } |
| 6841 | |
| 6842 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6843 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6844 | /* finished. */ |
| 6845 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6846 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6847 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6848 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6849 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6850 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6851 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6852 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6853 | |
| 6854 | /* yield. */ |
| 6855 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6856 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6857 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6858 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6859 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6860 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6861 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6862 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6863 | /* Some actions can be wake up when a "write" event |
| 6864 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6865 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6866 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6867 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6868 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6869 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6870 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6871 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6872 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6873 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6874 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6875 | * because HAProxy is not able to manipulate data, it |
| 6876 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6877 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6878 | |
| 6879 | /* finished with error. */ |
| 6880 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6881 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6882 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6883 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6884 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6885 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6886 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6887 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6888 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6889 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6890 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6891 | case HLUA_E_ETMOUT: |
| 6892 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6893 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6894 | return ACT_RET_ERR; |
| 6895 | } |
| 6896 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6897 | return 0; |
| 6898 | |
| 6899 | case HLUA_E_NOMEM: |
| 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 | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6902 | return ACT_RET_ERR; |
| 6903 | } |
| 6904 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6905 | return 0; |
| 6906 | |
| 6907 | case HLUA_E_YIELD: |
| 6908 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6909 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6910 | return ACT_RET_ERR; |
| 6911 | } |
| 6912 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6913 | rule->arg.hlua_rule->fcn.name); |
| 6914 | return 0; |
| 6915 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6916 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6917 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6918 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6919 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6920 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6921 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6922 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6923 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6924 | |
| 6925 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6926 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6927 | } |
| 6928 | } |
| 6929 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6930 | 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] | 6931 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6932 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6933 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6934 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6935 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6936 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6937 | } |
| 6938 | |
| 6939 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6940 | { |
| 6941 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6942 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6943 | struct task *task; |
| 6944 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6945 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6946 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6947 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6948 | if (!hlua) { |
| 6949 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6950 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6951 | return 0; |
| 6952 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6953 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6954 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6955 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6956 | |
| 6957 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6958 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6959 | if (!task) { |
| 6960 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6961 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6962 | return 0; |
| 6963 | } |
| 6964 | task->nice = 0; |
| 6965 | task->context = ctx; |
| 6966 | task->process = hlua_applet_wakeup; |
| 6967 | ctx->ctx.hlua_apptcp.task = task; |
| 6968 | |
| 6969 | /* In the execution wrappers linked with a stream, the |
| 6970 | * Lua context can be not initialized. This behavior |
| 6971 | * permits to save performances because a systematic |
| 6972 | * Lua initialization cause 5% performances loss. |
| 6973 | */ |
| 6974 | if (!hlua_ctx_init(hlua, task)) { |
| 6975 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6976 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6977 | return 0; |
| 6978 | } |
| 6979 | |
| 6980 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6981 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6982 | |
| 6983 | /* The following Lua calls can fail. */ |
| 6984 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6985 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6986 | error = lua_tostring(hlua->T, -1); |
| 6987 | else |
| 6988 | error = "critical error"; |
| 6989 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6990 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6991 | return 0; |
| 6992 | } |
| 6993 | |
| 6994 | /* Check stack available size. */ |
| 6995 | if (!lua_checkstack(hlua->T, 1)) { |
| 6996 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6997 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6998 | RESET_SAFE_LJMP(hlua->T); |
| 6999 | return 0; |
| 7000 | } |
| 7001 | |
| 7002 | /* Restore the function in the stack. */ |
| 7003 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7004 | |
| 7005 | /* Create and and push object stream in the stack. */ |
| 7006 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7007 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7008 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7009 | RESET_SAFE_LJMP(hlua->T); |
| 7010 | return 0; |
| 7011 | } |
| 7012 | hlua->nargs = 1; |
| 7013 | |
| 7014 | /* push keywords in the stack. */ |
| 7015 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7016 | if (!lua_checkstack(hlua->T, 1)) { |
| 7017 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7018 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7019 | RESET_SAFE_LJMP(hlua->T); |
| 7020 | return 0; |
| 7021 | } |
| 7022 | lua_pushstring(hlua->T, *arg); |
| 7023 | hlua->nargs++; |
| 7024 | } |
| 7025 | |
| 7026 | RESET_SAFE_LJMP(hlua->T); |
| 7027 | |
| 7028 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7029 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7030 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7031 | |
| 7032 | return 1; |
| 7033 | } |
| 7034 | |
| 7035 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 7036 | { |
| 7037 | struct stream_interface *si = ctx->owner; |
| 7038 | struct stream *strm = si_strm(si); |
| 7039 | struct channel *res = si_ic(si); |
| 7040 | struct act_rule *rule = ctx->rule; |
| 7041 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7042 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7043 | |
| 7044 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7045 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7046 | /* eat the whole request */ |
| 7047 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7048 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7049 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7050 | |
| 7051 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7052 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7053 | return; |
| 7054 | |
| 7055 | /* Execute the function. */ |
| 7056 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7057 | /* finished. */ |
| 7058 | case HLUA_E_OK: |
| 7059 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7060 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7061 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7062 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7063 | res->flags |= CF_READ_NULL; |
| 7064 | si_shutr(si); |
| 7065 | return; |
| 7066 | |
| 7067 | /* yield. */ |
| 7068 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7069 | if (hlua->wake_time != TICK_ETERNITY) |
| 7070 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7071 | return; |
| 7072 | |
| 7073 | /* finished with error. */ |
| 7074 | case HLUA_E_ERRMSG: |
| 7075 | /* Display log. */ |
| 7076 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7077 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7078 | lua_pop(hlua->T, 1); |
| 7079 | goto error; |
| 7080 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7081 | case HLUA_E_ETMOUT: |
| 7082 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 7083 | rule->arg.hlua_rule->fcn.name); |
| 7084 | goto error; |
| 7085 | |
| 7086 | case HLUA_E_NOMEM: |
| 7087 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 7088 | rule->arg.hlua_rule->fcn.name); |
| 7089 | goto error; |
| 7090 | |
| 7091 | case HLUA_E_YIELD: /* unexpected */ |
| 7092 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 7093 | rule->arg.hlua_rule->fcn.name); |
| 7094 | goto error; |
| 7095 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7096 | case HLUA_E_ERR: |
| 7097 | /* Display log. */ |
| 7098 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 7099 | rule->arg.hlua_rule->fcn.name); |
| 7100 | goto error; |
| 7101 | |
| 7102 | default: |
| 7103 | goto error; |
| 7104 | } |
| 7105 | |
| 7106 | error: |
| 7107 | |
| 7108 | /* For all other cases, just close the stream. */ |
| 7109 | si_shutw(si); |
| 7110 | si_shutr(si); |
| 7111 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7112 | } |
| 7113 | |
| 7114 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7115 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 7116 | task_delete(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7117 | task_free(ctx->ctx.hlua_apptcp.task); |
| 7118 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7119 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7120 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7121 | } |
| 7122 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7123 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7124 | * an errors occurs and -1 if more data are required for initializing |
| 7125 | * the applet. |
| 7126 | */ |
| 7127 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7128 | { |
| 7129 | struct stream_interface *si = ctx->owner; |
| 7130 | struct channel *req = si_oc(si); |
| 7131 | struct http_msg *msg; |
| 7132 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7133 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7134 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7135 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7136 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7137 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7138 | txn = strm->txn; |
| 7139 | msg = &txn->req; |
| 7140 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7141 | /* We want two things in HTTP mode : |
| 7142 | * - enforce server-close mode if we were in keep-alive, so that the |
| 7143 | * applet is released after each response ; |
| 7144 | * - enable request body transfer to the applet in order to resync |
| 7145 | * with the response body. |
| 7146 | */ |
| 7147 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 7148 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7149 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7150 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7151 | if (!hlua) { |
| 7152 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7153 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7154 | return 0; |
| 7155 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7156 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7157 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7158 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7159 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7160 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7161 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7162 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7163 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7164 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7165 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7166 | if (!task) { |
| 7167 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7168 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7169 | return 0; |
| 7170 | } |
| 7171 | task->nice = 0; |
| 7172 | task->context = ctx; |
| 7173 | task->process = hlua_applet_wakeup; |
| 7174 | ctx->ctx.hlua_apphttp.task = task; |
| 7175 | |
| 7176 | /* In the execution wrappers linked with a stream, the |
| 7177 | * Lua context can be not initialized. This behavior |
| 7178 | * permits to save performances because a systematic |
| 7179 | * Lua initialization cause 5% performances loss. |
| 7180 | */ |
| 7181 | if (!hlua_ctx_init(hlua, task)) { |
| 7182 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 7183 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7184 | return 0; |
| 7185 | } |
| 7186 | |
| 7187 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7188 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7189 | |
| 7190 | /* The following Lua calls can fail. */ |
| 7191 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7192 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7193 | error = lua_tostring(hlua->T, -1); |
| 7194 | else |
| 7195 | error = "critical error"; |
| 7196 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7197 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7198 | return 0; |
| 7199 | } |
| 7200 | |
| 7201 | /* Check stack available size. */ |
| 7202 | if (!lua_checkstack(hlua->T, 1)) { |
| 7203 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7204 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7205 | RESET_SAFE_LJMP(hlua->T); |
| 7206 | return 0; |
| 7207 | } |
| 7208 | |
| 7209 | /* Restore the function in the stack. */ |
| 7210 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7211 | |
| 7212 | /* Create and and push object stream in the stack. */ |
| 7213 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7214 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7215 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7216 | RESET_SAFE_LJMP(hlua->T); |
| 7217 | return 0; |
| 7218 | } |
| 7219 | hlua->nargs = 1; |
| 7220 | |
| 7221 | /* Look for a 100-continue expected. */ |
| 7222 | if (msg->flags & HTTP_MSGF_VER_11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7223 | if (IS_HTX_STRM(si_strm(si))) { |
| 7224 | /* HTX version */ |
| 7225 | struct htx *htx = htxbuf(&req->buf); |
| 7226 | struct ist hdr = { .ptr = "Expect", .len = 6 }; |
| 7227 | struct http_hdr_ctx hdr_ctx; |
| 7228 | |
| 7229 | hdr_ctx.blk = NULL; |
| 7230 | /* Expect is allowed in 1.1, look for it */ |
| 7231 | if (http_find_header(htx, hdr, &hdr_ctx, 0) && |
| 7232 | unlikely(isteqi(hdr_ctx.value, ist2("100-continue", 12)))) |
| 7233 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 7234 | } |
| 7235 | else { |
| 7236 | /* Legacy HTTP version */ |
| 7237 | struct hdr_ctx hdr; |
| 7238 | |
| 7239 | hdr.idx = 0; |
| 7240 | if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &hdr) && |
| 7241 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 7242 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 7243 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7244 | } |
| 7245 | |
| 7246 | /* push keywords in the stack. */ |
| 7247 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7248 | if (!lua_checkstack(hlua->T, 1)) { |
| 7249 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7250 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7251 | RESET_SAFE_LJMP(hlua->T); |
| 7252 | return 0; |
| 7253 | } |
| 7254 | lua_pushstring(hlua->T, *arg); |
| 7255 | hlua->nargs++; |
| 7256 | } |
| 7257 | |
| 7258 | RESET_SAFE_LJMP(hlua->T); |
| 7259 | |
| 7260 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7261 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7262 | |
| 7263 | return 1; |
| 7264 | } |
| 7265 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7266 | static void hlua_applet_htx_fct(struct appctx *ctx) |
| 7267 | { |
| 7268 | struct stream_interface *si = ctx->owner; |
| 7269 | struct stream *strm = si_strm(si); |
| 7270 | struct channel *req = si_oc(si); |
| 7271 | struct channel *res = si_ic(si); |
| 7272 | struct act_rule *rule = ctx->rule; |
| 7273 | struct proxy *px = strm->be; |
| 7274 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7275 | struct htx *req_htx, *res_htx; |
| 7276 | |
| 7277 | res_htx = htx_from_buf(&res->buf); |
| 7278 | |
| 7279 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7280 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7281 | goto out; |
| 7282 | |
| 7283 | /* Check if the input buffer is avalaible. */ |
| 7284 | if (!b_size(&res->buf)) { |
| 7285 | si_rx_room_blk(si); |
| 7286 | goto out; |
| 7287 | } |
| 7288 | /* check that the output is not closed */ |
| 7289 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
| 7290 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7291 | |
| 7292 | /* Set the currently running flag. */ |
| 7293 | if (!HLUA_IS_RUNNING(hlua) && |
| 7294 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7295 | struct htx_blk *blk; |
| 7296 | size_t count = co_data(req); |
| 7297 | |
| 7298 | if (!count) { |
| 7299 | si_cant_get(si); |
| 7300 | goto out; |
| 7301 | } |
| 7302 | |
| 7303 | /* We need to flush the request header. This left the body for |
| 7304 | * the Lua. |
| 7305 | */ |
| 7306 | req_htx = htx_from_buf(&req->buf); |
| 7307 | blk = htx_get_head_blk(req_htx); |
| 7308 | while (count && blk) { |
| 7309 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7310 | uint32_t sz = htx_get_blksz(blk); |
| 7311 | |
| 7312 | if (sz > count) { |
| 7313 | si_cant_get(si); |
| 7314 | goto out; |
| 7315 | } |
| 7316 | |
| 7317 | count -= sz; |
| 7318 | co_set_data(req, co_data(req) - sz); |
| 7319 | blk = htx_remove_blk(req_htx, blk); |
| 7320 | |
| 7321 | if (type == HTX_BLK_EOH) |
| 7322 | break; |
| 7323 | } |
| 7324 | } |
| 7325 | |
| 7326 | /* Executes The applet if it is not done. */ |
| 7327 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7328 | |
| 7329 | /* Execute the function. */ |
| 7330 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7331 | /* finished. */ |
| 7332 | case HLUA_E_OK: |
| 7333 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7334 | break; |
| 7335 | |
| 7336 | /* yield. */ |
| 7337 | case HLUA_E_AGAIN: |
| 7338 | if (hlua->wake_time != TICK_ETERNITY) |
| 7339 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
| 7340 | return; |
| 7341 | |
| 7342 | /* finished with error. */ |
| 7343 | case HLUA_E_ERRMSG: |
| 7344 | /* Display log. */ |
| 7345 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7346 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7347 | lua_pop(hlua->T, 1); |
| 7348 | goto error; |
| 7349 | |
| 7350 | case HLUA_E_ETMOUT: |
| 7351 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7352 | rule->arg.hlua_rule->fcn.name); |
| 7353 | goto error; |
| 7354 | |
| 7355 | case HLUA_E_NOMEM: |
| 7356 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7357 | rule->arg.hlua_rule->fcn.name); |
| 7358 | goto error; |
| 7359 | |
| 7360 | case HLUA_E_YIELD: /* unexpected */ |
| 7361 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7362 | rule->arg.hlua_rule->fcn.name); |
| 7363 | goto error; |
| 7364 | |
| 7365 | case HLUA_E_ERR: |
| 7366 | /* Display log. */ |
| 7367 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7368 | rule->arg.hlua_rule->fcn.name); |
| 7369 | goto error; |
| 7370 | |
| 7371 | default: |
| 7372 | goto error; |
| 7373 | } |
| 7374 | } |
| 7375 | |
| 7376 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 7377 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7378 | goto error; |
| 7379 | |
| 7380 | /* Don't add EOD and TLR because mux-h1 will take care of it */ |
| 7381 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7382 | si_rx_room_blk(si); |
| 7383 | goto out; |
| 7384 | } |
| 7385 | res->total++; |
| 7386 | res->flags |= CF_READ_PARTIAL; |
| 7387 | } |
| 7388 | |
| 7389 | done: |
| 7390 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 7391 | /* eat the whole request */ |
| 7392 | req_htx = htxbuf(&req->buf); |
| 7393 | htx_reset(req_htx); |
| 7394 | htx_to_buf(req_htx, &req->buf); |
| 7395 | co_set_data(req, 0); |
| 7396 | res->flags |= CF_READ_NULL; |
| 7397 | si_shutr(si); |
| 7398 | } |
| 7399 | |
| 7400 | if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) |
| 7401 | si_shutw(si); |
| 7402 | |
| 7403 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 7404 | if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) { |
| 7405 | si_shutr(si); |
| 7406 | res->flags |= CF_READ_NULL; |
| 7407 | } |
| 7408 | } |
| 7409 | |
| 7410 | out: |
| 7411 | /* we have left the request in the buffer for the case where we |
| 7412 | * process a POST, and this automatically re-enables activity on |
| 7413 | * read. It's better to indicate that we want to stop reading when |
| 7414 | * we're sending, so that we know there's at most one direction |
| 7415 | * deciding to wake the applet up. It saves it from looping when |
| 7416 | * emitting large blocks into small TCP windows. |
| 7417 | */ |
| 7418 | htx_to_buf(res_htx, &res->buf); |
| 7419 | if (!channel_is_empty(res)) |
| 7420 | si_stop_get(si); |
| 7421 | return; |
| 7422 | |
| 7423 | error: |
| 7424 | |
| 7425 | /* If we are in HTTP mode, and we are not send any |
| 7426 | * data, return a 500 server error in best effort: |
| 7427 | * if there is no room available in the buffer, |
| 7428 | * just close the connection. |
| 7429 | */ |
| 7430 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7431 | struct buffer *err = &htx_err_chunks[HTTP_ERR_500]; |
| 7432 | |
| 7433 | channel_erase(res); |
| 7434 | res->buf.data = b_data(err); |
| 7435 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7436 | res_htx = htx_from_buf(&res->buf); |
| 7437 | |
| 7438 | res->total += res_htx->data; |
| 7439 | res->flags |= CF_READ_PARTIAL; |
| 7440 | } |
| 7441 | if (!(strm->flags & SF_ERR_MASK)) |
| 7442 | strm->flags |= SF_ERR_RESOURCE; |
| 7443 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7444 | goto done; |
| 7445 | } |
| 7446 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7447 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 7448 | { |
| 7449 | struct stream_interface *si = ctx->owner; |
| 7450 | struct stream *strm = si_strm(si); |
| 7451 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7452 | struct act_rule *rule = ctx->rule; |
| 7453 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7454 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7455 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7456 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7457 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7458 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7459 | int ret; |
| 7460 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7461 | if (IS_HTX_STRM(strm)) |
| 7462 | return hlua_applet_htx_fct(ctx); |
| 7463 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7464 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7465 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7466 | return; |
| 7467 | |
| 7468 | /* Set the currently running flag. */ |
| 7469 | if (!HLUA_IS_RUNNING(hlua) && |
| 7470 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7471 | /* Store the max amount of bytes that we can read. */ |
| 7472 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 7473 | |
| 7474 | /* We need to flush the request header. This left the body |
| 7475 | * for the Lua. |
| 7476 | */ |
| 7477 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7478 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 7479 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7480 | if (ret == -1) |
| 7481 | return; |
| 7482 | |
| 7483 | /* No data available, ask for more data. */ |
| 7484 | if (ret == 1) |
| 7485 | len2 = 0; |
| 7486 | if (ret == 0) |
| 7487 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 7488 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7489 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7490 | return; |
| 7491 | } |
| 7492 | |
| 7493 | /* skip the requests bytes. */ |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 7494 | co_skip(si_oc(si), strm->txn->req.eoh + strm->txn->req.eol); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7495 | } |
| 7496 | |
| 7497 | /* Executes The applet if it is not done. */ |
| 7498 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7499 | |
| 7500 | /* Execute the function. */ |
| 7501 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7502 | /* finished. */ |
| 7503 | case HLUA_E_OK: |
| 7504 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7505 | break; |
| 7506 | |
| 7507 | /* yield. */ |
| 7508 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7509 | if (hlua->wake_time != TICK_ETERNITY) |
| 7510 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7511 | return; |
| 7512 | |
| 7513 | /* finished with error. */ |
| 7514 | case HLUA_E_ERRMSG: |
| 7515 | /* Display log. */ |
| 7516 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7517 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7518 | lua_pop(hlua->T, 1); |
| 7519 | goto error; |
| 7520 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7521 | case HLUA_E_ETMOUT: |
| 7522 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7523 | rule->arg.hlua_rule->fcn.name); |
| 7524 | goto error; |
| 7525 | |
| 7526 | case HLUA_E_NOMEM: |
| 7527 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7528 | rule->arg.hlua_rule->fcn.name); |
| 7529 | goto error; |
| 7530 | |
| 7531 | case HLUA_E_YIELD: /* unexpected */ |
| 7532 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7533 | rule->arg.hlua_rule->fcn.name); |
| 7534 | goto error; |
| 7535 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7536 | case HLUA_E_ERR: |
| 7537 | /* Display log. */ |
| 7538 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7539 | rule->arg.hlua_rule->fcn.name); |
| 7540 | goto error; |
| 7541 | |
| 7542 | default: |
| 7543 | goto error; |
| 7544 | } |
| 7545 | } |
| 7546 | |
| 7547 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 7548 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7549 | goto error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7550 | |
| 7551 | /* We must send the final chunk. */ |
| 7552 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 7553 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 7554 | |
| 7555 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 7556 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7557 | |
| 7558 | /* critical error. */ |
| 7559 | if (ret == -2 || ret == -3) { |
| 7560 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 7561 | rule->arg.hlua_rule->fcn.name); |
| 7562 | goto error; |
| 7563 | } |
| 7564 | |
| 7565 | /* no enough space error. */ |
| 7566 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7567 | si_rx_room_blk(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7568 | return; |
| 7569 | } |
| 7570 | |
| 7571 | /* set the last chunk sent. */ |
| 7572 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 7573 | } |
| 7574 | |
| 7575 | /* close the connection. */ |
| 7576 | |
Frédéric Lécaille | 83ed5d5 | 2018-07-18 14:25:26 +0200 | [diff] [blame] | 7577 | /* status */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7578 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7579 | |
| 7580 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7581 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7582 | res->flags |= CF_READ_NULL; |
| 7583 | si_shutr(si); |
| 7584 | |
| 7585 | return; |
| 7586 | } |
| 7587 | |
| 7588 | error: |
| 7589 | |
| 7590 | /* If we are in HTTP mode, and we are not send any |
| 7591 | * data, return a 500 server error in best effort: |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7592 | * if there is no room available in the buffer, |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7593 | * just close the connection. |
| 7594 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 7595 | ci_putblk(res, error_500, strlen(error_500)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7596 | if (!(strm->flags & SF_ERR_MASK)) |
| 7597 | strm->flags |= SF_ERR_RESOURCE; |
| 7598 | si_shutw(si); |
| 7599 | si_shutr(si); |
| 7600 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7601 | } |
| 7602 | |
| 7603 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7604 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 7605 | task_delete(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7606 | task_free(ctx->ctx.hlua_apphttp.task); |
| 7607 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7608 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7609 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7610 | } |
| 7611 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7612 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 7613 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7614 | * |
| 7615 | * This function can fail with an abort() due to an Lua critical error. |
| 7616 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7617 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7618 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7619 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7620 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7621 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7622 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7623 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7624 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7625 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7626 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7627 | if (!rule->arg.hlua_rule) { |
| 7628 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7629 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7630 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7631 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7632 | /* Memory for arguments. */ |
| 7633 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7634 | if (!rule->arg.hlua_rule->args) { |
| 7635 | memprintf(err, "out of memory error"); |
| 7636 | return ACT_RET_PRS_ERR; |
| 7637 | } |
| 7638 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7639 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7640 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7641 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7642 | /* Expect some arguments */ |
| 7643 | for (i = 0; i < fcn->nargs; i++) { |
| 7644 | if (*args[i+1] == '\0') { |
| 7645 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7646 | return ACT_RET_PRS_ERR; |
| 7647 | } |
| 7648 | rule->arg.hlua_rule->args[i] = strdup(args[i + 1]); |
| 7649 | if (!rule->arg.hlua_rule->args[i]) { |
| 7650 | memprintf(err, "out of memory error"); |
| 7651 | return ACT_RET_PRS_ERR; |
| 7652 | } |
| 7653 | (*cur_arg)++; |
| 7654 | } |
| 7655 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7656 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7657 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7658 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7659 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7660 | } |
| 7661 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7662 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7663 | struct act_rule *rule, char **err) |
| 7664 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7665 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7666 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7667 | /* HTTP applets are forbidden in tcp-request rules. |
| 7668 | * HTTP applet request requires everything initilized by |
| 7669 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 7670 | * The applet will be immediately initilized, but its before |
| 7671 | * the call of this analyzer. |
| 7672 | */ |
| 7673 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7674 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7675 | return ACT_RET_PRS_ERR; |
| 7676 | } |
| 7677 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7678 | /* Memory for the rule. */ |
| 7679 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7680 | if (!rule->arg.hlua_rule) { |
| 7681 | memprintf(err, "out of memory error"); |
| 7682 | return ACT_RET_PRS_ERR; |
| 7683 | } |
| 7684 | |
| 7685 | /* Reference the Lua function and store the reference. */ |
| 7686 | rule->arg.hlua_rule->fcn = *fcn; |
| 7687 | |
| 7688 | /* TODO: later accept arguments. */ |
| 7689 | rule->arg.hlua_rule->args = NULL; |
| 7690 | |
| 7691 | /* Add applet pointer in the rule. */ |
| 7692 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7693 | rule->applet.name = fcn->name; |
| 7694 | rule->applet.init = hlua_applet_http_init; |
| 7695 | rule->applet.fct = hlua_applet_http_fct; |
| 7696 | rule->applet.release = hlua_applet_http_release; |
| 7697 | rule->applet.timeout = hlua_timeout_applet; |
| 7698 | |
| 7699 | return ACT_RET_PRS_OK; |
| 7700 | } |
| 7701 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7702 | /* This function is an LUA binding used for registering |
| 7703 | * "sample-conv" functions. It expects a converter name used |
| 7704 | * in the haproxy configuration file, and an LUA function. |
| 7705 | */ |
| 7706 | __LJMP static int hlua_register_action(lua_State *L) |
| 7707 | { |
| 7708 | struct action_kw_list *akl; |
| 7709 | const char *name; |
| 7710 | int ref; |
| 7711 | int len; |
| 7712 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7713 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7714 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7715 | /* Initialise the number of expected arguments at 0. */ |
| 7716 | nargs = 0; |
| 7717 | |
| 7718 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7719 | 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] | 7720 | |
| 7721 | /* First argument : converter name. */ |
| 7722 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7723 | |
| 7724 | /* Second argument : environment. */ |
| 7725 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7726 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7727 | |
| 7728 | /* Third argument : lua function. */ |
| 7729 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7730 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7731 | /* 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] | 7732 | if (lua_gettop(L) >= 4) |
| 7733 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7734 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7735 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7736 | lua_pushnil(L); |
| 7737 | while (lua_next(L, 2) != 0) { |
| 7738 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7739 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7740 | |
| 7741 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7742 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7743 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7744 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7745 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7746 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7747 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7748 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7749 | |
| 7750 | /* Fill fcn. */ |
| 7751 | fcn->name = strdup(name); |
| 7752 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7753 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7754 | fcn->function_ref = ref; |
| 7755 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7756 | /* Set the expected number od arguments. */ |
| 7757 | fcn->nargs = nargs; |
| 7758 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7759 | /* List head */ |
| 7760 | akl->list.n = akl->list.p = NULL; |
| 7761 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7762 | /* action keyword. */ |
| 7763 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7764 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7765 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7766 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7767 | |
| 7768 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7769 | |
| 7770 | akl->kw[0].match_pfx = 0; |
| 7771 | akl->kw[0].private = fcn; |
| 7772 | akl->kw[0].parse = action_register_lua; |
| 7773 | |
| 7774 | /* select the action registering point. */ |
| 7775 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7776 | tcp_req_cont_keywords_register(akl); |
| 7777 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7778 | tcp_res_cont_keywords_register(akl); |
| 7779 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7780 | http_req_keywords_register(akl); |
| 7781 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7782 | http_res_keywords_register(akl); |
| 7783 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7784 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7785 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7786 | "are expected.", lua_tostring(L, -1))); |
| 7787 | |
| 7788 | /* pop the environment string. */ |
| 7789 | lua_pop(L, 1); |
| 7790 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7791 | return ACT_RET_PRS_OK; |
| 7792 | } |
| 7793 | |
| 7794 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7795 | struct act_rule *rule, char **err) |
| 7796 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7797 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7798 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7799 | if (px->options2 & PR_O2_USE_HTX) { |
| 7800 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 7801 | return ACT_RET_PRS_ERR; |
| 7802 | } |
| 7803 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7804 | /* Memory for the rule. */ |
| 7805 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7806 | if (!rule->arg.hlua_rule) { |
| 7807 | memprintf(err, "out of memory error"); |
| 7808 | return ACT_RET_PRS_ERR; |
| 7809 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7810 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7811 | /* Reference the Lua function and store the reference. */ |
| 7812 | rule->arg.hlua_rule->fcn = *fcn; |
| 7813 | |
| 7814 | /* TODO: later accept arguments. */ |
| 7815 | rule->arg.hlua_rule->args = NULL; |
| 7816 | |
| 7817 | /* Add applet pointer in the rule. */ |
| 7818 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7819 | rule->applet.name = fcn->name; |
| 7820 | rule->applet.init = hlua_applet_tcp_init; |
| 7821 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7822 | rule->applet.release = hlua_applet_tcp_release; |
| 7823 | rule->applet.timeout = hlua_timeout_applet; |
| 7824 | |
| 7825 | return 0; |
| 7826 | } |
| 7827 | |
| 7828 | /* This function is an LUA binding used for registering |
| 7829 | * "sample-conv" functions. It expects a converter name used |
| 7830 | * in the haproxy configuration file, and an LUA function. |
| 7831 | */ |
| 7832 | __LJMP static int hlua_register_service(lua_State *L) |
| 7833 | { |
| 7834 | struct action_kw_list *akl; |
| 7835 | const char *name; |
| 7836 | const char *env; |
| 7837 | int ref; |
| 7838 | int len; |
| 7839 | struct hlua_function *fcn; |
| 7840 | |
| 7841 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7842 | |
| 7843 | /* First argument : converter name. */ |
| 7844 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7845 | |
| 7846 | /* Second argument : environment. */ |
| 7847 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7848 | |
| 7849 | /* Third argument : lua function. */ |
| 7850 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7851 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7852 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7853 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7854 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7855 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7856 | fcn = calloc(1, sizeof(*fcn)); |
| 7857 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7858 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7859 | |
| 7860 | /* Fill fcn. */ |
| 7861 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7862 | fcn->name = calloc(1, len); |
| 7863 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7864 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7865 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7866 | fcn->function_ref = ref; |
| 7867 | |
| 7868 | /* List head */ |
| 7869 | akl->list.n = akl->list.p = NULL; |
| 7870 | |
| 7871 | /* converter keyword. */ |
| 7872 | len = strlen("lua.") + strlen(name) + 1; |
| 7873 | akl->kw[0].kw = calloc(1, len); |
| 7874 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7875 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7876 | |
| 7877 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7878 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7879 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7880 | if (strcmp(env, "tcp") == 0) |
| 7881 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7882 | else if (strcmp(env, "http") == 0) |
| 7883 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7884 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7885 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7886 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7887 | |
| 7888 | akl->kw[0].match_pfx = 0; |
| 7889 | akl->kw[0].private = fcn; |
| 7890 | |
| 7891 | /* End of array. */ |
| 7892 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7893 | |
| 7894 | /* Register this new converter */ |
| 7895 | service_keywords_register(akl); |
| 7896 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7897 | return 0; |
| 7898 | } |
| 7899 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7900 | /* This function initialises Lua cli handler. It copies the |
| 7901 | * arguments in the Lua stack and create channel IO objects. |
| 7902 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7903 | 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] | 7904 | { |
| 7905 | struct hlua *hlua; |
| 7906 | struct hlua_function *fcn; |
| 7907 | int i; |
| 7908 | const char *error; |
| 7909 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7910 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7911 | appctx->ctx.hlua_cli.fcn = private; |
| 7912 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7913 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7914 | if (!hlua) { |
| 7915 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7916 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7917 | } |
| 7918 | HLUA_INIT(hlua); |
| 7919 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7920 | |
| 7921 | /* Create task used by signal to wakeup applets. |
| 7922 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7923 | * applet_http. It is absolutely compatible. |
| 7924 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7925 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7926 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7927 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7928 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7929 | } |
| 7930 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7931 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7932 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7933 | |
| 7934 | /* Initialises the Lua context */ |
| 7935 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 7936 | 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] | 7937 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7938 | } |
| 7939 | |
| 7940 | /* The following Lua calls can fail. */ |
| 7941 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7942 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7943 | error = lua_tostring(hlua->T, -1); |
| 7944 | else |
| 7945 | error = "critical error"; |
| 7946 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7947 | goto error; |
| 7948 | } |
| 7949 | |
| 7950 | /* Check stack available size. */ |
| 7951 | if (!lua_checkstack(hlua->T, 2)) { |
| 7952 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7953 | goto error; |
| 7954 | } |
| 7955 | |
| 7956 | /* Restore the function in the stack. */ |
| 7957 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7958 | |
| 7959 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7960 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7961 | */ |
| 7962 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7963 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7964 | goto error; |
| 7965 | } |
| 7966 | hlua->nargs = 1; |
| 7967 | |
| 7968 | /* push keywords in the stack. */ |
| 7969 | for (i = 0; *args[i]; i++) { |
| 7970 | /* Check stack available size. */ |
| 7971 | if (!lua_checkstack(hlua->T, 1)) { |
| 7972 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7973 | goto error; |
| 7974 | } |
| 7975 | lua_pushstring(hlua->T, args[i]); |
| 7976 | hlua->nargs++; |
| 7977 | } |
| 7978 | |
| 7979 | /* We must initialize the execution timeouts. */ |
| 7980 | hlua->max_time = hlua_timeout_session; |
| 7981 | |
| 7982 | /* At this point the execution is safe. */ |
| 7983 | RESET_SAFE_LJMP(hlua->T); |
| 7984 | |
| 7985 | /* It's ok */ |
| 7986 | return 0; |
| 7987 | |
| 7988 | /* It's not ok. */ |
| 7989 | error: |
| 7990 | RESET_SAFE_LJMP(hlua->T); |
| 7991 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7992 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7993 | return 1; |
| 7994 | } |
| 7995 | |
| 7996 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7997 | { |
| 7998 | struct hlua *hlua; |
| 7999 | struct stream_interface *si; |
| 8000 | struct hlua_function *fcn; |
| 8001 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8002 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8003 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8004 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8005 | |
| 8006 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8007 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8008 | return 1; |
| 8009 | |
| 8010 | /* Execute the function. */ |
| 8011 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8012 | |
| 8013 | /* finished. */ |
| 8014 | case HLUA_E_OK: |
| 8015 | return 1; |
| 8016 | |
| 8017 | /* yield. */ |
| 8018 | case HLUA_E_AGAIN: |
| 8019 | /* We want write. */ |
| 8020 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8021 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8022 | /* Set the timeout. */ |
| 8023 | if (hlua->wake_time != TICK_ETERNITY) |
| 8024 | task_schedule(hlua->task, hlua->wake_time); |
| 8025 | return 0; |
| 8026 | |
| 8027 | /* finished with error. */ |
| 8028 | case HLUA_E_ERRMSG: |
| 8029 | /* Display log. */ |
| 8030 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8031 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8032 | lua_pop(hlua->T, 1); |
| 8033 | return 1; |
| 8034 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8035 | case HLUA_E_ETMOUT: |
| 8036 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8037 | fcn->name); |
| 8038 | return 1; |
| 8039 | |
| 8040 | case HLUA_E_NOMEM: |
| 8041 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8042 | fcn->name); |
| 8043 | return 1; |
| 8044 | |
| 8045 | case HLUA_E_YIELD: /* unexpected */ |
| 8046 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8047 | fcn->name); |
| 8048 | return 1; |
| 8049 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8050 | case HLUA_E_ERR: |
| 8051 | /* Display log. */ |
| 8052 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8053 | fcn->name); |
| 8054 | return 1; |
| 8055 | |
| 8056 | default: |
| 8057 | return 1; |
| 8058 | } |
| 8059 | |
| 8060 | return 1; |
| 8061 | } |
| 8062 | |
| 8063 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8064 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8065 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8066 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8067 | } |
| 8068 | |
| 8069 | /* This function is an LUA binding used for registering |
| 8070 | * new keywords in the cli. It expects a list of keywords |
| 8071 | * which are the "path". It is limited to 5 keywords. A |
| 8072 | * description of the command, a function to be executed |
| 8073 | * for the parsing and a function for io handlers. |
| 8074 | */ |
| 8075 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8076 | { |
| 8077 | struct cli_kw_list *cli_kws; |
| 8078 | const char *message; |
| 8079 | int ref_io; |
| 8080 | int len; |
| 8081 | struct hlua_function *fcn; |
| 8082 | int index; |
| 8083 | int i; |
| 8084 | |
| 8085 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8086 | |
| 8087 | /* First argument : an array of maximum 5 keywords. */ |
| 8088 | if (!lua_istable(L, 1)) |
| 8089 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8090 | |
| 8091 | /* Second argument : string with contextual message. */ |
| 8092 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8093 | |
| 8094 | /* Third and fourth argument : lua function. */ |
| 8095 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8096 | |
| 8097 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8098 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 8099 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8100 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8101 | fcn = calloc(1, sizeof(*fcn)); |
| 8102 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8103 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8104 | |
| 8105 | /* Fill path. */ |
| 8106 | index = 0; |
| 8107 | lua_pushnil(L); |
| 8108 | while(lua_next(L, 1) != 0) { |
| 8109 | if (index >= 5) |
| 8110 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8111 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8112 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8113 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 8114 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8115 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8116 | index++; |
| 8117 | lua_pop(L, 1); |
| 8118 | } |
| 8119 | |
| 8120 | /* Copy help message. */ |
| 8121 | cli_kws->kw[0].usage = strdup(message); |
| 8122 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8123 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8124 | |
| 8125 | /* Fill fcn io handler. */ |
| 8126 | len = strlen("<lua.cli>") + 1; |
| 8127 | for (i = 0; i < index; i++) |
| 8128 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8129 | fcn->name = calloc(1, len); |
| 8130 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8131 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8132 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8133 | for (i = 0; i < index; i++) { |
| 8134 | strncat((char *)fcn->name, ".", len); |
| 8135 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8136 | } |
| 8137 | strncat((char *)fcn->name, ">", len); |
| 8138 | fcn->function_ref = ref_io; |
| 8139 | |
| 8140 | /* Fill last entries. */ |
| 8141 | cli_kws->kw[0].private = fcn; |
| 8142 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8143 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8144 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8145 | |
| 8146 | /* Register this new converter */ |
| 8147 | cli_register_kw(cli_kws); |
| 8148 | |
| 8149 | return 0; |
| 8150 | } |
| 8151 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8152 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 8153 | struct proxy *defpx, const char *file, int line, |
| 8154 | char **err, unsigned int *timeout) |
| 8155 | { |
| 8156 | const char *error; |
| 8157 | |
| 8158 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 8159 | if (error && *error != '\0') { |
| 8160 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8161 | return -1; |
| 8162 | } |
| 8163 | return 0; |
| 8164 | } |
| 8165 | |
| 8166 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 8167 | struct proxy *defpx, const char *file, int line, |
| 8168 | char **err) |
| 8169 | { |
| 8170 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8171 | file, line, err, &hlua_timeout_session); |
| 8172 | } |
| 8173 | |
| 8174 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 8175 | struct proxy *defpx, const char *file, int line, |
| 8176 | char **err) |
| 8177 | { |
| 8178 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8179 | file, line, err, &hlua_timeout_task); |
| 8180 | } |
| 8181 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8182 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 8183 | struct proxy *defpx, const char *file, int line, |
| 8184 | char **err) |
| 8185 | { |
| 8186 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8187 | file, line, err, &hlua_timeout_applet); |
| 8188 | } |
| 8189 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8190 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 8191 | struct proxy *defpx, const char *file, int line, |
| 8192 | char **err) |
| 8193 | { |
| 8194 | char *error; |
| 8195 | |
| 8196 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8197 | if (*error != '\0') { |
| 8198 | memprintf(err, "%s: invalid number", args[0]); |
| 8199 | return -1; |
| 8200 | } |
| 8201 | return 0; |
| 8202 | } |
| 8203 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8204 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 8205 | struct proxy *defpx, const char *file, int line, |
| 8206 | char **err) |
| 8207 | { |
| 8208 | char *error; |
| 8209 | |
| 8210 | if (*(args[1]) == 0) { |
| 8211 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8212 | return -1; |
| 8213 | } |
| 8214 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8215 | if (*error != '\0') { |
| 8216 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8217 | return -1; |
| 8218 | } |
| 8219 | return 0; |
| 8220 | } |
| 8221 | |
| 8222 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8223 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8224 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8225 | * the main lua entry point. |
| 8226 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8227 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8228 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8229 | * |
| 8230 | * In some error case, LUA set an error message in top of the stack. This function |
| 8231 | * 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] | 8232 | * |
| 8233 | * This function can fail with an abort() due to an Lua critical error. |
| 8234 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8235 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8236 | */ |
| 8237 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 8238 | struct proxy *defpx, const char *file, int line, |
| 8239 | char **err) |
| 8240 | { |
| 8241 | int error; |
| 8242 | |
| 8243 | /* Just load and compile the file. */ |
| 8244 | error = luaL_loadfile(gL.T, args[1]); |
| 8245 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8246 | 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] | 8247 | lua_pop(gL.T, 1); |
| 8248 | return -1; |
| 8249 | } |
| 8250 | |
| 8251 | /* If no syntax error where detected, execute the code. */ |
| 8252 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 8253 | switch (error) { |
| 8254 | case LUA_OK: |
| 8255 | break; |
| 8256 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8257 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8258 | lua_pop(gL.T, 1); |
| 8259 | return -1; |
| 8260 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8261 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8262 | return -1; |
| 8263 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8264 | 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] | 8265 | lua_pop(gL.T, 1); |
| 8266 | return -1; |
| 8267 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8268 | 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] | 8269 | lua_pop(gL.T, 1); |
| 8270 | return -1; |
| 8271 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8272 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8273 | lua_pop(gL.T, 1); |
| 8274 | return -1; |
| 8275 | } |
| 8276 | |
| 8277 | return 0; |
| 8278 | } |
| 8279 | |
| 8280 | /* configuration keywords declaration */ |
| 8281 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8282 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8283 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8284 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8285 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8286 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8287 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8288 | { 0, NULL, NULL }, |
| 8289 | }}; |
| 8290 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8291 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8292 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8293 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8294 | /* This function can fail with an abort() due to an Lua critical error. |
| 8295 | * We are in the initialisation process of HAProxy, this abort() is |
| 8296 | * tolerated. |
| 8297 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8298 | int hlua_post_init() |
| 8299 | { |
| 8300 | struct hlua_init_function *init; |
| 8301 | const char *msg; |
| 8302 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8303 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8304 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8305 | /* Call post initialisation function in safe environement. */ |
| 8306 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8307 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8308 | error = lua_tostring(gL.T, -1); |
| 8309 | else |
| 8310 | error = "critical error"; |
| 8311 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8312 | exit(1); |
| 8313 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8314 | |
| 8315 | #if USE_OPENSSL |
| 8316 | /* Initialize SSL server. */ |
| 8317 | if (socket_ssl.xprt->prepare_srv) { |
| 8318 | int saved_used_backed = global.ssl_used_backend; |
| 8319 | // don't affect maxconn automatic computation |
| 8320 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8321 | global.ssl_used_backend = saved_used_backed; |
| 8322 | } |
| 8323 | #endif |
| 8324 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8325 | hlua_fcn_post_init(gL.T); |
| 8326 | RESET_SAFE_LJMP(gL.T); |
| 8327 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8328 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8329 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 8330 | ret = hlua_ctx_resume(&gL, 0); |
| 8331 | switch (ret) { |
| 8332 | case HLUA_E_OK: |
| 8333 | lua_pop(gL.T, -1); |
| 8334 | return 1; |
| 8335 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8336 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8337 | return 0; |
| 8338 | case HLUA_E_ERRMSG: |
| 8339 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8340 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8341 | return 0; |
| 8342 | case HLUA_E_ERR: |
| 8343 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8344 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8345 | return 0; |
| 8346 | } |
| 8347 | } |
| 8348 | return 1; |
| 8349 | } |
| 8350 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8351 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8352 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8353 | * is the previously allocated size or the kind of object in case of a new |
| 8354 | * allocation. <nsize> is the requested new size. |
| 8355 | */ |
| 8356 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8357 | { |
| 8358 | struct hlua_mem_allocator *zone = ud; |
| 8359 | |
| 8360 | if (nsize == 0) { |
| 8361 | /* it's a free */ |
| 8362 | if (ptr) |
| 8363 | zone->allocated -= osize; |
| 8364 | free(ptr); |
| 8365 | return NULL; |
| 8366 | } |
| 8367 | |
| 8368 | if (!ptr) { |
| 8369 | /* it's a new allocation */ |
| 8370 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8371 | return NULL; |
| 8372 | |
| 8373 | ptr = malloc(nsize); |
| 8374 | if (ptr) |
| 8375 | zone->allocated += nsize; |
| 8376 | return ptr; |
| 8377 | } |
| 8378 | |
| 8379 | /* it's a realloc */ |
| 8380 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8381 | return NULL; |
| 8382 | |
| 8383 | ptr = realloc(ptr, nsize); |
| 8384 | if (ptr) |
| 8385 | zone->allocated += nsize - osize; |
| 8386 | return ptr; |
| 8387 | } |
| 8388 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8389 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8390 | * We are in the initialisation process of HAProxy, this abort() is |
| 8391 | * tolerated. |
| 8392 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8393 | void hlua_init(void) |
| 8394 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8395 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8396 | int idx; |
| 8397 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8398 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8399 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8400 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8401 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8402 | struct srv_kw *kw; |
| 8403 | int tmp_error; |
| 8404 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8405 | char *args[] = { /* SSL client configuration. */ |
| 8406 | "ssl", |
| 8407 | "verify", |
| 8408 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8409 | NULL |
| 8410 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8411 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8412 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8413 | /* Init main lua stack. */ |
| 8414 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8415 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8416 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8417 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8418 | hlua_sethlua(&gL); |
| 8419 | gL.Tref = LUA_REFNIL; |
| 8420 | gL.task = NULL; |
| 8421 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8422 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8423 | * the Lua function can fail with an abort. We are in the initialisation |
| 8424 | * process of HAProxy, this abort() is tolerated. |
| 8425 | */ |
| 8426 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8427 | /* Initialise lua. */ |
| 8428 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8429 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8430 | /* Set safe environment for the initialisation. */ |
| 8431 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8432 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8433 | error_msg = lua_tostring(gL.T, -1); |
| 8434 | else |
| 8435 | error_msg = "critical error"; |
| 8436 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8437 | exit(1); |
| 8438 | } |
| 8439 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8440 | /* |
| 8441 | * |
| 8442 | * Create "core" object. |
| 8443 | * |
| 8444 | */ |
| 8445 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8446 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8447 | lua_newtable(gL.T); |
| 8448 | |
| 8449 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8450 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8451 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8452 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8453 | /* Register special functions. */ |
| 8454 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8455 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8456 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8457 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8458 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8459 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8460 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8461 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8462 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8463 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8464 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8465 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8466 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8467 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8468 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8469 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8470 | hlua_class_function(gL.T, "log", hlua_log); |
| 8471 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8472 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8473 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8474 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8475 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8476 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8477 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8478 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8479 | |
| 8480 | /* |
| 8481 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8482 | * Register class Map |
| 8483 | * |
| 8484 | */ |
| 8485 | |
| 8486 | /* This table entry is the object "Map" base. */ |
| 8487 | lua_newtable(gL.T); |
| 8488 | |
| 8489 | /* register pattern types. */ |
| 8490 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8491 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8492 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8493 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8494 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8495 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8496 | |
| 8497 | /* register constructor. */ |
| 8498 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8499 | |
| 8500 | /* Create and fill the metatable. */ |
| 8501 | lua_newtable(gL.T); |
| 8502 | |
| 8503 | /* Create and fille the __index entry. */ |
| 8504 | lua_pushstring(gL.T, "__index"); |
| 8505 | lua_newtable(gL.T); |
| 8506 | |
| 8507 | /* Register . */ |
| 8508 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8509 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8510 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8511 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8512 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8513 | /* Register previous table in the registry with reference and named entry. |
| 8514 | * The function hlua_register_metatable() pops the stack, so we |
| 8515 | * previously create a copy of the table. |
| 8516 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8517 | 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] | 8518 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8519 | |
| 8520 | /* Assign the metatable to the mai Map object. */ |
| 8521 | lua_setmetatable(gL.T, -2); |
| 8522 | |
| 8523 | /* Set a name to the table. */ |
| 8524 | lua_setglobal(gL.T, "Map"); |
| 8525 | |
| 8526 | /* |
| 8527 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8528 | * Register class Channel |
| 8529 | * |
| 8530 | */ |
| 8531 | |
| 8532 | /* Create and fill the metatable. */ |
| 8533 | lua_newtable(gL.T); |
| 8534 | |
| 8535 | /* Create and fille the __index entry. */ |
| 8536 | lua_pushstring(gL.T, "__index"); |
| 8537 | lua_newtable(gL.T); |
| 8538 | |
| 8539 | /* Register . */ |
| 8540 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8541 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8542 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8543 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8544 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8545 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8546 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8547 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8548 | 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] | 8549 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8550 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8551 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8552 | |
| 8553 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8554 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8555 | |
| 8556 | /* |
| 8557 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8558 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8559 | * |
| 8560 | */ |
| 8561 | |
| 8562 | /* Create and fill the metatable. */ |
| 8563 | lua_newtable(gL.T); |
| 8564 | |
| 8565 | /* Create and fille the __index entry. */ |
| 8566 | lua_pushstring(gL.T, "__index"); |
| 8567 | lua_newtable(gL.T); |
| 8568 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8569 | /* Browse existing fetches and create the associated |
| 8570 | * object method. |
| 8571 | */ |
| 8572 | sf = NULL; |
| 8573 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8574 | |
| 8575 | /* Dont register the keywork if the arguments check function are |
| 8576 | * not safe during the runtime. |
| 8577 | */ |
| 8578 | if ((sf->val_args != NULL) && |
| 8579 | (sf->val_args != val_payload_lv) && |
| 8580 | (sf->val_args != val_hdr)) |
| 8581 | continue; |
| 8582 | |
| 8583 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8584 | * by an underscore. |
| 8585 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8586 | strncpy(trash.area, sf->kw, trash.size); |
| 8587 | trash.area[trash.size - 1] = '\0'; |
| 8588 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8589 | if (*p == '.' || *p == '-' || *p == '+') |
| 8590 | *p = '_'; |
| 8591 | |
| 8592 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8593 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8594 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8595 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8596 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8597 | } |
| 8598 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8599 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8600 | |
| 8601 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8602 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8603 | |
| 8604 | /* |
| 8605 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8606 | * Register class Converters |
| 8607 | * |
| 8608 | */ |
| 8609 | |
| 8610 | /* Create and fill the metatable. */ |
| 8611 | lua_newtable(gL.T); |
| 8612 | |
| 8613 | /* Create and fill the __index entry. */ |
| 8614 | lua_pushstring(gL.T, "__index"); |
| 8615 | lua_newtable(gL.T); |
| 8616 | |
| 8617 | /* Browse existing converters and create the associated |
| 8618 | * object method. |
| 8619 | */ |
| 8620 | sc = NULL; |
| 8621 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8622 | /* Dont register the keywork if the arguments check function are |
| 8623 | * not safe during the runtime. |
| 8624 | */ |
| 8625 | if (sc->val_args != NULL) |
| 8626 | continue; |
| 8627 | |
| 8628 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8629 | * by an underscore. |
| 8630 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8631 | strncpy(trash.area, sc->kw, trash.size); |
| 8632 | trash.area[trash.size - 1] = '\0'; |
| 8633 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8634 | if (*p == '.' || *p == '-' || *p == '+') |
| 8635 | *p = '_'; |
| 8636 | |
| 8637 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8638 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8639 | lua_pushlightuserdata(gL.T, sc); |
| 8640 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8641 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8642 | } |
| 8643 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8644 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8645 | |
| 8646 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8647 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8648 | |
| 8649 | /* |
| 8650 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8651 | * Register class HTTP |
| 8652 | * |
| 8653 | */ |
| 8654 | |
| 8655 | /* Create and fill the metatable. */ |
| 8656 | lua_newtable(gL.T); |
| 8657 | |
| 8658 | /* Create and fille the __index entry. */ |
| 8659 | lua_pushstring(gL.T, "__index"); |
| 8660 | lua_newtable(gL.T); |
| 8661 | |
| 8662 | /* Register Lua functions. */ |
| 8663 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8664 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8665 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8666 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8667 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8668 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8669 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8670 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8671 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8672 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8673 | |
| 8674 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8675 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8676 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8677 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8678 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8679 | 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] | 8680 | 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] | 8681 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8682 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8683 | |
| 8684 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8685 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8686 | |
| 8687 | /* |
| 8688 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8689 | * Register class AppletTCP |
| 8690 | * |
| 8691 | */ |
| 8692 | |
| 8693 | /* Create and fill the metatable. */ |
| 8694 | lua_newtable(gL.T); |
| 8695 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8696 | /* Create and fille the __index entry. */ |
| 8697 | lua_pushstring(gL.T, "__index"); |
| 8698 | lua_newtable(gL.T); |
| 8699 | |
| 8700 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8701 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8702 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8703 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8704 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8705 | 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] | 8706 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8707 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8708 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8709 | |
| 8710 | lua_settable(gL.T, -3); |
| 8711 | |
| 8712 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8713 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8714 | |
| 8715 | /* |
| 8716 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8717 | * Register class AppletHTTP |
| 8718 | * |
| 8719 | */ |
| 8720 | |
| 8721 | /* Create and fill the metatable. */ |
| 8722 | lua_newtable(gL.T); |
| 8723 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8724 | /* Create and fille the __index entry. */ |
| 8725 | lua_pushstring(gL.T, "__index"); |
| 8726 | lua_newtable(gL.T); |
| 8727 | |
| 8728 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8729 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8730 | 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] | 8731 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8732 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8733 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8734 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8735 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8736 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8737 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8738 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8739 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8740 | |
| 8741 | lua_settable(gL.T, -3); |
| 8742 | |
| 8743 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8744 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8745 | |
| 8746 | /* |
| 8747 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8748 | * Register class TXN |
| 8749 | * |
| 8750 | */ |
| 8751 | |
| 8752 | /* Create and fill the metatable. */ |
| 8753 | lua_newtable(gL.T); |
| 8754 | |
| 8755 | /* Create and fille the __index entry. */ |
| 8756 | lua_pushstring(gL.T, "__index"); |
| 8757 | lua_newtable(gL.T); |
| 8758 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8759 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8760 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8761 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8762 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8763 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8764 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8765 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 8766 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8767 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8768 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8769 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8770 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8771 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8772 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8773 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8774 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8775 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8776 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8777 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8778 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8779 | |
| 8780 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8781 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8782 | |
| 8783 | /* |
| 8784 | * |
| 8785 | * Register class Socket |
| 8786 | * |
| 8787 | */ |
| 8788 | |
| 8789 | /* Create and fill the metatable. */ |
| 8790 | lua_newtable(gL.T); |
| 8791 | |
| 8792 | /* Create and fille the __index entry. */ |
| 8793 | lua_pushstring(gL.T, "__index"); |
| 8794 | lua_newtable(gL.T); |
| 8795 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8796 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8797 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8798 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8799 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8800 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8801 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8802 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8803 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8804 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8805 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8806 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8807 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8808 | 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] | 8809 | |
| 8810 | /* Register the garbage collector entry. */ |
| 8811 | lua_pushstring(gL.T, "__gc"); |
| 8812 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8813 | 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] | 8814 | |
| 8815 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8816 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8817 | |
| 8818 | /* Proxy and server configuration initialisation. */ |
| 8819 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8820 | init_new_proxy(&socket_proxy); |
| 8821 | socket_proxy.parent = NULL; |
| 8822 | socket_proxy.last_change = now.tv_sec; |
| 8823 | socket_proxy.id = "LUA-SOCKET"; |
| 8824 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8825 | socket_proxy.maxconn = 0; |
| 8826 | socket_proxy.accept = NULL; |
| 8827 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8828 | socket_proxy.srv = NULL; |
| 8829 | socket_proxy.conn_retries = 0; |
| 8830 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8831 | |
| 8832 | /* Init TCP server: unchanged parameters */ |
| 8833 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8834 | socket_tcp.next = NULL; |
| 8835 | socket_tcp.proxy = &socket_proxy; |
| 8836 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8837 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8838 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8839 | socket_tcp.priv_conns = NULL; |
| 8840 | socket_tcp.idle_conns = NULL; |
| 8841 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8842 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8843 | socket_tcp.last_change = 0; |
| 8844 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8845 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8846 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8847 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8848 | |
| 8849 | /* XXX: Copy default parameter from default server, |
| 8850 | * but the default server is not initialized. |
| 8851 | */ |
| 8852 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8853 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8854 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8855 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8856 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8857 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8858 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8859 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8860 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8861 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8862 | |
| 8863 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8864 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8865 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8866 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8867 | socket_tcp.check.server = &socket_tcp; |
| 8868 | |
| 8869 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8870 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8871 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8872 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8873 | socket_tcp.agent.server = &socket_tcp; |
| 8874 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8875 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8876 | |
| 8877 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8878 | /* Init TCP server: unchanged parameters */ |
| 8879 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8880 | socket_ssl.next = NULL; |
| 8881 | socket_ssl.proxy = &socket_proxy; |
| 8882 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8883 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8884 | socket_ssl.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8885 | socket_tcp.priv_conns = NULL; |
| 8886 | socket_tcp.idle_conns = NULL; |
| 8887 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8888 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8889 | socket_ssl.last_change = 0; |
| 8890 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8891 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8892 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8893 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8894 | |
| 8895 | /* XXX: Copy default parameter from default server, |
| 8896 | * but the default server is not initialized. |
| 8897 | */ |
| 8898 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8899 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8900 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8901 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8902 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8903 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8904 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8905 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8906 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8907 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8908 | |
| 8909 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8910 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8911 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8912 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8913 | socket_ssl.check.server = &socket_ssl; |
| 8914 | |
| 8915 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8916 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8917 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8918 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8919 | socket_ssl.agent.server = &socket_ssl; |
| 8920 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8921 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8922 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8923 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8924 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8925 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8926 | /* |
| 8927 | * |
| 8928 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8929 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8930 | * features like client certificates and ssl_verify. |
| 8931 | * |
| 8932 | */ |
| 8933 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8934 | if (tmp_error != 0) { |
| 8935 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8936 | abort(); /* This must be never arrives because the command line |
| 8937 | not editable by the user. */ |
| 8938 | } |
| 8939 | idx += kw->skip; |
| 8940 | } |
| 8941 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8942 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8943 | |
| 8944 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8945 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8946 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8947 | static void hlua_register_build_options(void) |
| 8948 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8949 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8950 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8951 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8952 | hap_register_build_opts(ptr, 1); |
| 8953 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8954 | |
| 8955 | INITCALL0(STG_REGISTER, hlua_register_build_options); |