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 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 887 | * In some case (at least one), this function can be called from safe |
| 888 | * environement, so we must not initialise it. While the support of |
| 889 | * threads appear, the safe environment set a lock to ensure only one |
| 890 | * Lua execution at a time. If we initialize safe environment in another |
| 891 | * safe environmenet, we have a dead lock. |
| 892 | * |
| 893 | * set "already_safe" true if the context is initialized form safe |
| 894 | * Lua fonction. |
| 895 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 896 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 897 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 898 | * MUST NOT manipulate the created thread stack state, because it is not |
| 899 | * proctected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 900 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 901 | int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 902 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 903 | if (!already_safe) { |
| 904 | if (!SET_SAFE_LJMP(gL.T)) { |
| 905 | lua->Tref = LUA_REFNIL; |
| 906 | return 0; |
| 907 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 908 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 909 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 910 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 911 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 912 | lua->T = lua_newthread(gL.T); |
| 913 | if (!lua->T) { |
| 914 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 915 | if (!already_safe) |
| 916 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 917 | return 0; |
| 918 | } |
| 919 | hlua_sethlua(lua); |
| 920 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 921 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 922 | if (!already_safe) |
| 923 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 924 | return 1; |
| 925 | } |
| 926 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 927 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 928 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 929 | * is not freed. |
| 930 | */ |
| 931 | void hlua_ctx_destroy(struct hlua *lua) |
| 932 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 933 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 934 | return; |
| 935 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 936 | if (!lua->T) |
| 937 | goto end; |
| 938 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 939 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 940 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 941 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 942 | if (!SET_SAFE_LJMP(lua->T)) |
| 943 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 944 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 945 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 946 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 947 | if (!SET_SAFE_LJMP(gL.T)) |
| 948 | return; |
| 949 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 950 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 951 | /* Forces a garbage collecting process. If the Lua program is finished |
| 952 | * without error, we run the GC on the thread pointer. Its freed all |
| 953 | * the unused memory. |
| 954 | * If the thread is finnish with an error or is currently yielded, |
| 955 | * it seems that the GC applied on the thread doesn't clean anything, |
| 956 | * so e run the GC on the main thread. |
| 957 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 958 | * the garbage collection. |
| 959 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 960 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 961 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 962 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 963 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 964 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 965 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 966 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 967 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 968 | |
| 969 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 970 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /* This function is used to restore the Lua context when a coroutine |
| 974 | * fails. This function copy the common memory between old coroutine |
| 975 | * and the new coroutine. The old coroutine is destroyed, and its |
| 976 | * replaced by the new coroutine. |
| 977 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 978 | * as string error message and it is copied in the new stack. |
| 979 | */ |
| 980 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 981 | { |
| 982 | lua_State *T; |
| 983 | int new_ref; |
| 984 | |
| 985 | /* Renew the main LUA stack doesn't have sense. */ |
| 986 | if (lua == &gL) |
| 987 | return 0; |
| 988 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 989 | /* New Lua coroutine. */ |
| 990 | T = lua_newthread(gL.T); |
| 991 | if (!T) |
| 992 | return 0; |
| 993 | |
| 994 | /* Copy last error message. */ |
| 995 | if (keep_msg) |
| 996 | lua_xmove(lua->T, T, 1); |
| 997 | |
| 998 | /* Copy data between the coroutines. */ |
| 999 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1000 | lua_xmove(lua->T, T, 1); |
| 1001 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 1002 | |
| 1003 | /* Destroy old data. */ |
| 1004 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1005 | |
| 1006 | /* The thread is garbage collected by Lua. */ |
| 1007 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1008 | |
| 1009 | /* Fill the struct with the new coroutine values. */ |
| 1010 | lua->Mref = new_ref; |
| 1011 | lua->T = T; |
| 1012 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1013 | |
| 1014 | /* Set context. */ |
| 1015 | hlua_sethlua(lua); |
| 1016 | |
| 1017 | return 1; |
| 1018 | } |
| 1019 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1020 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1021 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1022 | struct hlua *hlua = hlua_gethlua(L); |
| 1023 | |
| 1024 | /* Lua cannot yield when its returning from a function, |
| 1025 | * so, we can fix the interrupt hook to 1 instruction, |
| 1026 | * expecting that the function is finnished. |
| 1027 | */ |
| 1028 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1029 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1030 | return; |
| 1031 | } |
| 1032 | |
| 1033 | /* restore the interrupt condition. */ |
| 1034 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1035 | |
| 1036 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1037 | * If the state is not yieldable, trying yield causes an error. |
| 1038 | */ |
| 1039 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1040 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1041 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1042 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1043 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1044 | hlua->run_time += now_ms - hlua->start_time; |
| 1045 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1046 | lua_pushfstring(L, "execution timeout"); |
| 1047 | WILL_LJMP(lua_error(L)); |
| 1048 | } |
| 1049 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1050 | /* Update the start time. */ |
| 1051 | hlua->start_time = now_ms; |
| 1052 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1053 | /* Try to interrupt the process at the end of the current |
| 1054 | * unyieldable function. |
| 1055 | */ |
| 1056 | 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] | 1057 | } |
| 1058 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1059 | /* This function start or resumes the Lua stack execution. If the flag |
| 1060 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1061 | * The function return an error. |
| 1062 | * |
| 1063 | * The function can returns 4 values: |
| 1064 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1065 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1066 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1067 | * - 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] | 1068 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1069 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1070 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1071 | * 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] | 1072 | * LUA code. |
| 1073 | */ |
| 1074 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1075 | { |
| 1076 | int ret; |
| 1077 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1078 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1079 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1080 | /* Initialise run time counter. */ |
| 1081 | if (!HLUA_IS_RUNNING(lua)) |
| 1082 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1083 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1084 | /* Lock the whole Lua execution. This lock must be before the |
| 1085 | * label "resume_execution". |
| 1086 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1087 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1088 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1089 | resume_execution: |
| 1090 | |
| 1091 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1092 | * instructions. it is used for preventing infinite loops. |
| 1093 | */ |
| 1094 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1095 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1096 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1097 | HLUA_SET_RUN(lua); |
| 1098 | HLUA_CLR_CTRLYIELD(lua); |
| 1099 | HLUA_CLR_WAKERESWR(lua); |
| 1100 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1101 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1102 | /* Update the start time. */ |
| 1103 | lua->start_time = now_ms; |
| 1104 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1105 | /* Call the function. */ |
| 1106 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1107 | switch (ret) { |
| 1108 | |
| 1109 | case LUA_OK: |
| 1110 | ret = HLUA_E_OK; |
| 1111 | break; |
| 1112 | |
| 1113 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1114 | /* Check if the execution timeout is expired. It it is the case, we |
| 1115 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1116 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1117 | tv_update_date(0, 1); |
| 1118 | lua->run_time += now_ms - lua->start_time; |
| 1119 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1120 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1121 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1122 | break; |
| 1123 | } |
| 1124 | /* Process the forced yield. if the general yield is not allowed or |
| 1125 | * if no task were associated this the current Lua execution |
| 1126 | * coroutine, we resume the execution. Else we want to return in the |
| 1127 | * scheduler and we want to be waked up again, to continue the |
| 1128 | * current Lua execution. So we schedule our own task. |
| 1129 | */ |
| 1130 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1131 | if (!yield_allowed || !lua->task) |
| 1132 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1133 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1134 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1135 | if (!yield_allowed) { |
| 1136 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1137 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1138 | break; |
| 1139 | } |
| 1140 | ret = HLUA_E_AGAIN; |
| 1141 | break; |
| 1142 | |
| 1143 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1144 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1145 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1146 | * because the errors ares the only one mean to return immediately |
| 1147 | * from and lua execution. |
| 1148 | */ |
| 1149 | if (lua->flags & HLUA_EXIT) { |
| 1150 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1151 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1152 | break; |
| 1153 | } |
| 1154 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1155 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1156 | if (!lua_checkstack(lua->T, 1)) { |
| 1157 | ret = HLUA_E_ERR; |
| 1158 | break; |
| 1159 | } |
| 1160 | msg = lua_tostring(lua->T, -1); |
| 1161 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1162 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1163 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1164 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1165 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1166 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1167 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1168 | ret = HLUA_E_ERRMSG; |
| 1169 | break; |
| 1170 | |
| 1171 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1172 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1173 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1174 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1175 | break; |
| 1176 | |
| 1177 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1178 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1179 | if (!lua_checkstack(lua->T, 1)) { |
| 1180 | ret = HLUA_E_ERR; |
| 1181 | break; |
| 1182 | } |
| 1183 | msg = lua_tostring(lua->T, -1); |
| 1184 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1185 | lua_pop(lua->T, 1); |
| 1186 | if (msg) |
| 1187 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1188 | else |
| 1189 | lua_pushfstring(lua->T, "message handler error"); |
| 1190 | ret = HLUA_E_ERRMSG; |
| 1191 | break; |
| 1192 | |
| 1193 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1194 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1195 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1196 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1197 | break; |
| 1198 | } |
| 1199 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1200 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1201 | if (lua->flags & HLUA_MUST_GC && |
| 1202 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1203 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1204 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1205 | switch (ret) { |
| 1206 | case HLUA_E_AGAIN: |
| 1207 | break; |
| 1208 | |
| 1209 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1210 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1211 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1212 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1213 | break; |
| 1214 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1215 | case HLUA_E_ETMOUT: |
| 1216 | case HLUA_E_NOMEM: |
| 1217 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1218 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1219 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1220 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1221 | hlua_ctx_renew(lua, 0); |
| 1222 | break; |
| 1223 | |
| 1224 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1225 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1226 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1227 | break; |
| 1228 | } |
| 1229 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1230 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1231 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1232 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1233 | return ret; |
| 1234 | } |
| 1235 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1236 | /* This function exit the current code. */ |
| 1237 | __LJMP static int hlua_done(lua_State *L) |
| 1238 | { |
| 1239 | struct hlua *hlua = hlua_gethlua(L); |
| 1240 | |
| 1241 | hlua->flags |= HLUA_EXIT; |
| 1242 | WILL_LJMP(lua_error(L)); |
| 1243 | |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1247 | /* This function is an LUA binding. It provides a function |
| 1248 | * for deleting ACL from a referenced ACL file. |
| 1249 | */ |
| 1250 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1251 | { |
| 1252 | const char *name; |
| 1253 | const char *key; |
| 1254 | struct pat_ref *ref; |
| 1255 | |
| 1256 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1257 | |
| 1258 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1259 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1260 | |
| 1261 | ref = pat_ref_lookup(name); |
| 1262 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1263 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1264 | |
| 1265 | pat_ref_delete(ref, key); |
| 1266 | return 0; |
| 1267 | } |
| 1268 | |
| 1269 | /* This function is an LUA binding. It provides a function |
| 1270 | * for deleting map entry from a referenced map file. |
| 1271 | */ |
| 1272 | static int hlua_del_map(lua_State *L) |
| 1273 | { |
| 1274 | const char *name; |
| 1275 | const char *key; |
| 1276 | struct pat_ref *ref; |
| 1277 | |
| 1278 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1279 | |
| 1280 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1281 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1282 | |
| 1283 | ref = pat_ref_lookup(name); |
| 1284 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1285 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1286 | |
| 1287 | pat_ref_delete(ref, key); |
| 1288 | return 0; |
| 1289 | } |
| 1290 | |
| 1291 | /* This function is an LUA binding. It provides a function |
| 1292 | * for adding ACL pattern from a referenced ACL file. |
| 1293 | */ |
| 1294 | static int hlua_add_acl(lua_State *L) |
| 1295 | { |
| 1296 | const char *name; |
| 1297 | const char *key; |
| 1298 | struct pat_ref *ref; |
| 1299 | |
| 1300 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1301 | |
| 1302 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1303 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1304 | |
| 1305 | ref = pat_ref_lookup(name); |
| 1306 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1307 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1308 | |
| 1309 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1310 | pat_ref_add(ref, key, NULL, NULL); |
| 1311 | return 0; |
| 1312 | } |
| 1313 | |
| 1314 | /* This function is an LUA binding. It provides a function |
| 1315 | * for setting map pattern and sample from a referenced map |
| 1316 | * file. |
| 1317 | */ |
| 1318 | static int hlua_set_map(lua_State *L) |
| 1319 | { |
| 1320 | const char *name; |
| 1321 | const char *key; |
| 1322 | const char *value; |
| 1323 | struct pat_ref *ref; |
| 1324 | |
| 1325 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1326 | |
| 1327 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1328 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1329 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1330 | |
| 1331 | ref = pat_ref_lookup(name); |
| 1332 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1333 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1334 | |
| 1335 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1336 | pat_ref_set(ref, key, value, NULL); |
| 1337 | else |
| 1338 | pat_ref_add(ref, key, value, NULL); |
| 1339 | return 0; |
| 1340 | } |
| 1341 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1342 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1343 | * an integer or user data. This data is associated with a metatable. This |
| 1344 | * metatable have an original version registred in the global context with |
| 1345 | * the name of the object (_G[<name>] = <metable> ). |
| 1346 | * |
| 1347 | * A metable is a table that modify the standard behavior of a standard |
| 1348 | * access to the associated data. The entries of this new metatable are |
| 1349 | * defined as is: |
| 1350 | * |
| 1351 | * http://lua-users.org/wiki/MetatableEvents |
| 1352 | * |
| 1353 | * __index |
| 1354 | * |
| 1355 | * we access an absent field in a table, the result is nil. This is |
| 1356 | * true, but it is not the whole truth. Actually, such access triggers |
| 1357 | * the interpreter to look for an __index metamethod: If there is no |
| 1358 | * such method, as usually happens, then the access results in nil; |
| 1359 | * otherwise, the metamethod will provide the result. |
| 1360 | * |
| 1361 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1362 | * the key does not appear in the table, but the metatable has an __index |
| 1363 | * property: |
| 1364 | * |
| 1365 | * - if the value is a function, the function is called, passing in the |
| 1366 | * table and the key; the return value of that function is returned as |
| 1367 | * the result. |
| 1368 | * |
| 1369 | * - if the value is another table, the value of the key in that table is |
| 1370 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1371 | * table's metatable has an __index property, then it continues on up) |
| 1372 | * |
| 1373 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1374 | * |
| 1375 | * http://www.lua.org/pil/13.4.1.html |
| 1376 | * |
| 1377 | * __newindex |
| 1378 | * |
| 1379 | * Like __index, but control property assignment. |
| 1380 | * |
| 1381 | * __mode - Control weak references. A string value with one or both |
| 1382 | * of the characters 'k' and 'v' which specifies that the the |
| 1383 | * keys and/or values in the table are weak references. |
| 1384 | * |
| 1385 | * __call - Treat a table like a function. When a table is followed by |
| 1386 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1387 | * a __call key pointing to a function, that function is invoked |
| 1388 | * (passing any specified arguments) and the return value is |
| 1389 | * returned. |
| 1390 | * |
| 1391 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1392 | * called, if the metatable for myTable has a __metatable |
| 1393 | * key, the value of that key is returned instead of the |
| 1394 | * actual metatable. |
| 1395 | * |
| 1396 | * __tostring - Control string representation. When the builtin |
| 1397 | * "tostring( myTable )" function is called, if the metatable |
| 1398 | * for myTable has a __tostring property set to a function, |
| 1399 | * that function is invoked (passing myTable to it) and the |
| 1400 | * return value is used as the string representation. |
| 1401 | * |
| 1402 | * __len - Control table length. When the table length is requested using |
| 1403 | * the length operator ( '#' ), if the metatable for myTable has |
| 1404 | * a __len key pointing to a function, that function is invoked |
| 1405 | * (passing myTable to it) and the return value used as the value |
| 1406 | * of "#myTable". |
| 1407 | * |
| 1408 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1409 | * collected, if the metatable has a __gc field pointing to a |
| 1410 | * function, that function is first invoked, passing the userdata |
| 1411 | * to it. The __gc metamethod is not called for tables. |
| 1412 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1413 | * |
| 1414 | * Special metamethods for redefining standard operators: |
| 1415 | * http://www.lua.org/pil/13.1.html |
| 1416 | * |
| 1417 | * __add "+" |
| 1418 | * __sub "-" |
| 1419 | * __mul "*" |
| 1420 | * __div "/" |
| 1421 | * __unm "!" |
| 1422 | * __pow "^" |
| 1423 | * __concat ".." |
| 1424 | * |
| 1425 | * Special methods for redfining standar relations |
| 1426 | * http://www.lua.org/pil/13.2.html |
| 1427 | * |
| 1428 | * __eq "==" |
| 1429 | * __lt "<" |
| 1430 | * __le "<=" |
| 1431 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1432 | |
| 1433 | /* |
| 1434 | * |
| 1435 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1436 | * Class Map |
| 1437 | * |
| 1438 | * |
| 1439 | */ |
| 1440 | |
| 1441 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1442 | * a class session, otherwise it throws an error. |
| 1443 | */ |
| 1444 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1445 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1446 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | /* This function is the map constructor. It don't need |
| 1450 | * the class Map object. It creates and return a new Map |
| 1451 | * object. It must be called only during "body" or "init" |
| 1452 | * context because it process some filesystem accesses. |
| 1453 | */ |
| 1454 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1455 | { |
| 1456 | const char *fn; |
| 1457 | int match = PAT_MATCH_STR; |
| 1458 | struct sample_conv conv; |
| 1459 | const char *file = ""; |
| 1460 | int line = 0; |
| 1461 | lua_Debug ar; |
| 1462 | char *err = NULL; |
| 1463 | struct arg args[2]; |
| 1464 | |
| 1465 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1466 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1467 | |
| 1468 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1469 | |
| 1470 | if (lua_gettop(L) >= 2) { |
| 1471 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1472 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1473 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1474 | } |
| 1475 | |
| 1476 | /* Get Lua filename and line number. */ |
| 1477 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1478 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1479 | if (ar.currentline > 0) { /* is there info? */ |
| 1480 | file = ar.short_src; |
| 1481 | line = ar.currentline; |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | /* fill fake sample_conv struct. */ |
| 1486 | conv.kw = ""; /* unused. */ |
| 1487 | conv.process = NULL; /* unused. */ |
| 1488 | conv.arg_mask = 0; /* unused. */ |
| 1489 | conv.val_args = NULL; /* unused. */ |
| 1490 | conv.out_type = SMP_T_STR; |
| 1491 | conv.private = (void *)(long)match; |
| 1492 | switch (match) { |
| 1493 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1494 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1495 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1496 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1497 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1498 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1499 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1500 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1501 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1502 | default: |
| 1503 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1504 | } |
| 1505 | |
| 1506 | /* fill fake args. */ |
| 1507 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1508 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1509 | args[1].type = ARGT_STOP; |
| 1510 | |
| 1511 | /* load the map. */ |
| 1512 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1513 | /* error case: we cant use luaL_error because we must |
| 1514 | * free the err variable. |
| 1515 | */ |
| 1516 | luaL_where(L, 1); |
| 1517 | lua_pushfstring(L, "'new': %s.", err); |
| 1518 | lua_concat(L, 2); |
| 1519 | free(err); |
| 1520 | WILL_LJMP(lua_error(L)); |
| 1521 | } |
| 1522 | |
| 1523 | /* create the lua object. */ |
| 1524 | lua_newtable(L); |
| 1525 | lua_pushlightuserdata(L, args[0].data.map); |
| 1526 | lua_rawseti(L, -2, 0); |
| 1527 | |
| 1528 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1529 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1530 | lua_setmetatable(L, -2); |
| 1531 | |
| 1532 | |
| 1533 | return 1; |
| 1534 | } |
| 1535 | |
| 1536 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1537 | { |
| 1538 | struct map_descriptor *desc; |
| 1539 | struct pattern *pat; |
| 1540 | struct sample smp; |
| 1541 | |
| 1542 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1543 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1544 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1545 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1546 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1547 | } |
| 1548 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1549 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1550 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1551 | 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] | 1552 | } |
| 1553 | |
| 1554 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1555 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1556 | if (str) |
| 1557 | lua_pushstring(L, ""); |
| 1558 | else |
| 1559 | lua_pushnil(L); |
| 1560 | return 1; |
| 1561 | } |
| 1562 | |
| 1563 | /* 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] | 1564 | 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] | 1565 | return 1; |
| 1566 | } |
| 1567 | |
| 1568 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1569 | { |
| 1570 | return _hlua_map_lookup(L, 0); |
| 1571 | } |
| 1572 | |
| 1573 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1574 | { |
| 1575 | return _hlua_map_lookup(L, 1); |
| 1576 | } |
| 1577 | |
| 1578 | /* |
| 1579 | * |
| 1580 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1581 | * Class Socket |
| 1582 | * |
| 1583 | * |
| 1584 | */ |
| 1585 | |
| 1586 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1587 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1588 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | /* 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] | 1592 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1593 | * received. |
| 1594 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1595 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1596 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1597 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1598 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1599 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1600 | if (appctx->ctx.hlua_cosocket.die) { |
| 1601 | si_shutw(si); |
| 1602 | si_shutr(si); |
| 1603 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1604 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1605 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1606 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1607 | } |
| 1608 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1609 | /* If the connection object is not available, close all the |
| 1610 | * streams and wakeup everything waiting for. |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1611 | */ |
| 1612 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1613 | si_shutw(si); |
| 1614 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1615 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1616 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1617 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1618 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1619 | } |
| 1620 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1621 | /* If we cant write, wakeup the pending write signals. */ |
| 1622 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1623 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1624 | |
| 1625 | /* If we cant read, wakeup the pending read signals. */ |
| 1626 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1627 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1628 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1629 | /* if the connection is not estabkished, inform the stream that we want |
| 1630 | * to be notified whenever the connection completes. |
| 1631 | */ |
| 1632 | if (!(c->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1633 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1634 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1635 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1636 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1637 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1638 | |
| 1639 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1640 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1641 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1642 | /* 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] | 1643 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1644 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1645 | |
| 1646 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1647 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1648 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1649 | |
| 1650 | /* Some data were injected in the buffer, notify the stream |
| 1651 | * interface. |
| 1652 | */ |
| 1653 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1654 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1655 | |
| 1656 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1657 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1658 | */ |
| 1659 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1660 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1661 | } |
| 1662 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1663 | /* This function is called when the "struct stream" is destroyed. |
| 1664 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1665 | * Wake all the pending signals. |
| 1666 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1667 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1668 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1669 | struct xref *peer; |
| 1670 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1671 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1672 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1673 | if (peer) |
| 1674 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1675 | |
| 1676 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1677 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1678 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1679 | } |
| 1680 | |
| 1681 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1682 | * uses this object. If the stream does not exists, just quit. |
| 1683 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1684 | * pending signal can rest in the read and write lists. destroy |
| 1685 | * it. |
| 1686 | */ |
| 1687 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1688 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1689 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1690 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1691 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1692 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1693 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1694 | |
| 1695 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1696 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1697 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1698 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1699 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1700 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1701 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1702 | appctx->ctx.hlua_cosocket.die = 1; |
| 1703 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1704 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1705 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1706 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1707 | return 0; |
| 1708 | } |
| 1709 | |
| 1710 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1711 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1712 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1713 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1714 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1715 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1716 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1717 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1718 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1719 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1720 | |
| 1721 | /* Check if we run on the same thread than the xreator thread. |
| 1722 | * We cannot access to the socket if the thread is different. |
| 1723 | */ |
| 1724 | if (socket->tid != tid) |
| 1725 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1726 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1727 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1728 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1729 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1730 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1731 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1732 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1733 | appctx->ctx.hlua_cosocket.die = 1; |
| 1734 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1735 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1736 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1737 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1738 | return 0; |
| 1739 | } |
| 1740 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1741 | /* The close function calls close_helper. |
| 1742 | */ |
| 1743 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1744 | { |
| 1745 | MAY_LJMP(check_args(L, 1, "close")); |
| 1746 | return hlua_socket_close_helper(L); |
| 1747 | } |
| 1748 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1749 | /* This Lua function assumes that the stack contain three parameters. |
| 1750 | * 1 - USERDATA containing a struct socket |
| 1751 | * 2 - INTEGER with values of the macro defined below |
| 1752 | * If the integer is -1, we must read at most one line. |
| 1753 | * If the integer is -2, we ust read all the data until the |
| 1754 | * end of the stream. |
| 1755 | * If the integer is positive value, we must read a number of |
| 1756 | * bytes corresponding to this value. |
| 1757 | */ |
| 1758 | #define HLSR_READ_LINE (-1) |
| 1759 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1760 | __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] | 1761 | { |
| 1762 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1763 | int wanted = lua_tointeger(L, 2); |
| 1764 | struct hlua *hlua = hlua_gethlua(L); |
| 1765 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1766 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1767 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1768 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1769 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1770 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1771 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1772 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1773 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1774 | struct stream_interface *si; |
| 1775 | struct stream *s; |
| 1776 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1777 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1778 | |
| 1779 | /* Check if this lua stack is schedulable. */ |
| 1780 | if (!hlua || !hlua->task) |
| 1781 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1782 | "'frontend', 'backend' or 'task'")); |
| 1783 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1784 | /* Check if we run on the same thread than the xreator thread. |
| 1785 | * We cannot access to the socket if the thread is different. |
| 1786 | */ |
| 1787 | if (socket->tid != tid) |
| 1788 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1789 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1790 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1791 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1792 | if (!peer) |
| 1793 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1794 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1795 | si = appctx->owner; |
| 1796 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1797 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1798 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1799 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1800 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1801 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1802 | if (nblk < 0) /* Connection close. */ |
| 1803 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1804 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1805 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1806 | |
| 1807 | /* remove final \r\n. */ |
| 1808 | if (nblk == 1) { |
| 1809 | if (blk1[len1-1] == '\n') { |
| 1810 | len1--; |
| 1811 | skip_at_end++; |
| 1812 | if (blk1[len1-1] == '\r') { |
| 1813 | len1--; |
| 1814 | skip_at_end++; |
| 1815 | } |
| 1816 | } |
| 1817 | } |
| 1818 | else { |
| 1819 | if (blk2[len2-1] == '\n') { |
| 1820 | len2--; |
| 1821 | skip_at_end++; |
| 1822 | if (blk2[len2-1] == '\r') { |
| 1823 | len2--; |
| 1824 | skip_at_end++; |
| 1825 | } |
| 1826 | } |
| 1827 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1831 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1832 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1833 | if (nblk < 0) /* Connection close. */ |
| 1834 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1835 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1836 | goto connection_empty; |
| 1837 | } |
| 1838 | |
| 1839 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1840 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1841 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1842 | if (nblk < 0) /* Connection close. */ |
| 1843 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1844 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1845 | goto connection_empty; |
| 1846 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1847 | missing_bytes = wanted - socket->b.n; |
| 1848 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1849 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1850 | len1 = missing_bytes; |
| 1851 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1852 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | len = len1; |
| 1856 | |
| 1857 | luaL_addlstring(&socket->b, blk1, len1); |
| 1858 | if (nblk == 2) { |
| 1859 | len += len2; |
| 1860 | luaL_addlstring(&socket->b, blk2, len2); |
| 1861 | } |
| 1862 | |
| 1863 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1864 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1865 | |
| 1866 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1867 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1868 | |
| 1869 | /* If the pattern reclaim to read all the data |
| 1870 | * in the connection, got out. |
| 1871 | */ |
| 1872 | if (wanted == HLSR_READ_ALL) |
| 1873 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1874 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1875 | goto connection_empty; |
| 1876 | |
| 1877 | /* Return result. */ |
| 1878 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1879 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1880 | return 1; |
| 1881 | |
| 1882 | connection_closed: |
| 1883 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1884 | xref_unlock(&socket->xref, peer); |
| 1885 | |
| 1886 | no_peer: |
| 1887 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1888 | /* If the buffer containds data. */ |
| 1889 | if (socket->b.n > 0) { |
| 1890 | luaL_pushresult(&socket->b); |
| 1891 | return 1; |
| 1892 | } |
| 1893 | lua_pushnil(L); |
| 1894 | lua_pushstring(L, "connection closed."); |
| 1895 | return 2; |
| 1896 | |
| 1897 | connection_empty: |
| 1898 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1899 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1900 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1901 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1902 | } |
| 1903 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1904 | 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] | 1905 | return 0; |
| 1906 | } |
| 1907 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1908 | /* This Lua function gets two parameters. The first one can be string |
| 1909 | * or a number. If the string is "*l", the user requires one line. If |
| 1910 | * 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] | 1911 | * If the value is a number, the user require a number of bytes equal |
| 1912 | * to the value. The default value is "*l" (a line). |
| 1913 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1914 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1915 | * integer takes this values: |
| 1916 | * -1 : read a line |
| 1917 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1918 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1919 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1920 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1921 | * concatenated with the read data. |
| 1922 | */ |
| 1923 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1924 | { |
| 1925 | int wanted = HLSR_READ_LINE; |
| 1926 | const char *pattern; |
| 1927 | int type; |
| 1928 | char *error; |
| 1929 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1930 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1931 | |
| 1932 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1933 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1934 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1935 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1936 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1937 | /* Check if we run on the same thread than the xreator thread. |
| 1938 | * We cannot access to the socket if the thread is different. |
| 1939 | */ |
| 1940 | if (socket->tid != tid) |
| 1941 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1942 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1943 | /* check for pattern. */ |
| 1944 | if (lua_gettop(L) >= 2) { |
| 1945 | type = lua_type(L, 2); |
| 1946 | if (type == LUA_TSTRING) { |
| 1947 | pattern = lua_tostring(L, 2); |
| 1948 | if (strcmp(pattern, "*a") == 0) |
| 1949 | wanted = HLSR_READ_ALL; |
| 1950 | else if (strcmp(pattern, "*l") == 0) |
| 1951 | wanted = HLSR_READ_LINE; |
| 1952 | else { |
| 1953 | wanted = strtoll(pattern, &error, 10); |
| 1954 | if (*error != '\0') |
| 1955 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1956 | } |
| 1957 | } |
| 1958 | else if (type == LUA_TNUMBER) { |
| 1959 | wanted = lua_tointeger(L, 2); |
| 1960 | if (wanted < 0) |
| 1961 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1962 | } |
| 1963 | } |
| 1964 | |
| 1965 | /* Set pattern. */ |
| 1966 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1967 | |
| 1968 | /* Check if we would replace the top by itself. */ |
| 1969 | if (lua_gettop(L) != 2) |
| 1970 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1971 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1972 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | luaL_buffinit(L, &socket->b); |
| 1974 | |
| 1975 | /* Check prefix. */ |
| 1976 | if (lua_gettop(L) >= 3) { |
| 1977 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1978 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1979 | pattern = lua_tolstring(L, 3, &len); |
| 1980 | luaL_addlstring(&socket->b, pattern, len); |
| 1981 | } |
| 1982 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1983 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1987 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1988 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1989 | 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] | 1990 | { |
| 1991 | struct hlua_socket *socket; |
| 1992 | struct hlua *hlua = hlua_gethlua(L); |
| 1993 | struct appctx *appctx; |
| 1994 | size_t buf_len; |
| 1995 | const char *buf; |
| 1996 | int len; |
| 1997 | int send_len; |
| 1998 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1999 | struct xref *peer; |
| 2000 | struct stream_interface *si; |
| 2001 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2002 | |
| 2003 | /* Check if this lua stack is schedulable. */ |
| 2004 | if (!hlua || !hlua->task) |
| 2005 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2006 | "'frontend', 'backend' or 'task'")); |
| 2007 | |
| 2008 | /* Get object */ |
| 2009 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2010 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2011 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2012 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2013 | /* Check if we run on the same thread than the xreator thread. |
| 2014 | * We cannot access to the socket if the thread is different. |
| 2015 | */ |
| 2016 | if (socket->tid != tid) |
| 2017 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2018 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2019 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2020 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2021 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2022 | lua_pushinteger(L, -1); |
| 2023 | return 1; |
| 2024 | } |
| 2025 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2026 | si = appctx->owner; |
| 2027 | s = si_strm(si); |
| 2028 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2029 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2030 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2031 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2032 | lua_pushinteger(L, -1); |
| 2033 | return 1; |
| 2034 | } |
| 2035 | |
| 2036 | /* Update the input buffer data. */ |
| 2037 | buf += sent; |
| 2038 | send_len = buf_len - sent; |
| 2039 | |
| 2040 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2041 | if (sent >= buf_len) { |
| 2042 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2043 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2044 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2045 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2046 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2047 | * the request buffer if its not required. |
| 2048 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2049 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2050 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2051 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2052 | } |
| 2053 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2054 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2055 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2056 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2057 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2058 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2059 | |
| 2060 | /* send data */ |
| 2061 | if (len < send_len) |
| 2062 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2063 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2064 | |
| 2065 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2066 | * the data" (-2) are not expected because the available length |
| 2067 | * is tested. |
| 2068 | * Other unknown error are also not expected. |
| 2069 | */ |
| 2070 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2071 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2072 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2073 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2074 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2075 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2076 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2077 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2078 | return 1; |
| 2079 | } |
| 2080 | |
| 2081 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2082 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2083 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2084 | s->req.rex = TICK_ETERNITY; |
| 2085 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2086 | |
| 2087 | /* Update length sent. */ |
| 2088 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2089 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2090 | |
| 2091 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2092 | if (sent + len >= buf_len) { |
| 2093 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2094 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2095 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2096 | |
| 2097 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2098 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2099 | xref_unlock(&socket->xref, peer); |
| 2100 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2101 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2102 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2103 | 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] | 2104 | return 0; |
| 2105 | } |
| 2106 | |
| 2107 | /* This function initiate the send of data. It just check the input |
| 2108 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2109 | * 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] | 2110 | * "hlua_socket_write_yield" that can yield. |
| 2111 | * |
| 2112 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2113 | * the associated object. The second is a string buffer. The third is |
| 2114 | * a facultative integer that represents where is the buffer position |
| 2115 | * of the start of the data that can send. The first byte is the |
| 2116 | * position "1". The default value is "1". The fourth argument is a |
| 2117 | * facultative integer that represents where is the buffer position |
| 2118 | * of the end of the data that can send. The default is the last byte. |
| 2119 | */ |
| 2120 | static int hlua_socket_send(struct lua_State *L) |
| 2121 | { |
| 2122 | int i; |
| 2123 | int j; |
| 2124 | const char *buf; |
| 2125 | size_t buf_len; |
| 2126 | |
| 2127 | /* Check number of arguments. */ |
| 2128 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2129 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2130 | |
| 2131 | /* Get the string. */ |
| 2132 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2133 | |
| 2134 | /* Get and check j. */ |
| 2135 | if (lua_gettop(L) == 4) { |
| 2136 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2137 | if (j < 0) |
| 2138 | j = buf_len + j + 1; |
| 2139 | if (j > buf_len) |
| 2140 | j = buf_len + 1; |
| 2141 | lua_pop(L, 1); |
| 2142 | } |
| 2143 | else |
| 2144 | j = buf_len; |
| 2145 | |
| 2146 | /* Get and check i. */ |
| 2147 | if (lua_gettop(L) == 3) { |
| 2148 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2149 | if (i < 0) |
| 2150 | i = buf_len + i + 1; |
| 2151 | if (i > buf_len) |
| 2152 | i = buf_len + 1; |
| 2153 | lua_pop(L, 1); |
| 2154 | } else |
| 2155 | i = 1; |
| 2156 | |
| 2157 | /* Check bth i and j. */ |
| 2158 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2159 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2160 | return 1; |
| 2161 | } |
| 2162 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2163 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2164 | return 1; |
| 2165 | } |
| 2166 | if (i == 0) |
| 2167 | i = 1; |
| 2168 | if (j == 0) |
| 2169 | j = 1; |
| 2170 | |
| 2171 | /* Pop the string. */ |
| 2172 | lua_pop(L, 1); |
| 2173 | |
| 2174 | /* Update the buffer length. */ |
| 2175 | buf += i - 1; |
| 2176 | buf_len = j - i + 1; |
| 2177 | lua_pushlstring(L, buf, buf_len); |
| 2178 | |
| 2179 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2180 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2181 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2182 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2183 | } |
| 2184 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2185 | #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] | 2186 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2187 | { |
| 2188 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2189 | int ret; |
| 2190 | int len; |
| 2191 | char *p; |
| 2192 | |
| 2193 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2194 | if (ret <= 0) { |
| 2195 | lua_pushnil(L); |
| 2196 | return 1; |
| 2197 | } |
| 2198 | |
| 2199 | if (ret == AF_UNIX) { |
| 2200 | lua_pushstring(L, buffer+1); |
| 2201 | return 1; |
| 2202 | } |
| 2203 | else if (ret == AF_INET6) { |
| 2204 | buffer[0] = '['; |
| 2205 | len = strlen(buffer); |
| 2206 | buffer[len] = ']'; |
| 2207 | len++; |
| 2208 | buffer[len] = ':'; |
| 2209 | len++; |
| 2210 | p = buffer; |
| 2211 | } |
| 2212 | else if (ret == AF_INET) { |
| 2213 | p = buffer + 1; |
| 2214 | len = strlen(p); |
| 2215 | p[len] = ':'; |
| 2216 | len++; |
| 2217 | } |
| 2218 | else { |
| 2219 | lua_pushnil(L); |
| 2220 | return 1; |
| 2221 | } |
| 2222 | |
| 2223 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2224 | lua_pushnil(L); |
| 2225 | return 1; |
| 2226 | } |
| 2227 | |
| 2228 | lua_pushstring(L, p); |
| 2229 | return 1; |
| 2230 | } |
| 2231 | |
| 2232 | /* Returns information about the peer of the connection. */ |
| 2233 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2234 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2235 | struct hlua_socket *socket; |
| 2236 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2237 | struct xref *peer; |
| 2238 | struct appctx *appctx; |
| 2239 | struct stream_interface *si; |
| 2240 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2241 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2242 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2243 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2244 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2245 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2246 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2247 | /* Check if we run on the same thread than the xreator thread. |
| 2248 | * We cannot access to the socket if the thread is different. |
| 2249 | */ |
| 2250 | if (socket->tid != tid) |
| 2251 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2252 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2253 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2254 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2255 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2256 | lua_pushnil(L); |
| 2257 | return 1; |
| 2258 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2259 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2260 | si = appctx->owner; |
| 2261 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2262 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2263 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2264 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2265 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2266 | lua_pushnil(L); |
| 2267 | return 1; |
| 2268 | } |
| 2269 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2270 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2271 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2272 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2273 | lua_pushnil(L); |
| 2274 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2275 | } |
| 2276 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2277 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2278 | xref_unlock(&socket->xref, peer); |
| 2279 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2280 | } |
| 2281 | |
| 2282 | /* Returns information about my connection side. */ |
| 2283 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2284 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2285 | struct hlua_socket *socket; |
| 2286 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2287 | struct appctx *appctx; |
| 2288 | struct xref *peer; |
| 2289 | struct stream_interface *si; |
| 2290 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2291 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2292 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2294 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2295 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2296 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2297 | /* Check if we run on the same thread than the xreator thread. |
| 2298 | * We cannot access to the socket if the thread is different. |
| 2299 | */ |
| 2300 | if (socket->tid != tid) |
| 2301 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2302 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2303 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2304 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2305 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | lua_pushnil(L); |
| 2307 | return 1; |
| 2308 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2309 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2310 | si = appctx->owner; |
| 2311 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2312 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2313 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2314 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2315 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2316 | lua_pushnil(L); |
| 2317 | return 1; |
| 2318 | } |
| 2319 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2320 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2322 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2323 | lua_pushnil(L); |
| 2324 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2325 | } |
| 2326 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2327 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2328 | xref_unlock(&socket->xref, peer); |
| 2329 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2330 | } |
| 2331 | |
| 2332 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2333 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2334 | .obj_type = OBJ_TYPE_APPLET, |
| 2335 | .name = "<LUA_TCP>", |
| 2336 | .fct = hlua_socket_handler, |
| 2337 | .release = hlua_socket_release, |
| 2338 | }; |
| 2339 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2340 | __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] | 2341 | { |
| 2342 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2343 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2344 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2345 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2346 | struct stream_interface *si; |
| 2347 | struct stream *s; |
| 2348 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2349 | /* Check if we run on the same thread than the xreator thread. |
| 2350 | * We cannot access to the socket if the thread is different. |
| 2351 | */ |
| 2352 | if (socket->tid != tid) |
| 2353 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2354 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2355 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2356 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2357 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2358 | lua_pushnil(L); |
| 2359 | lua_pushstring(L, "Can't connect"); |
| 2360 | return 2; |
| 2361 | } |
| 2362 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2363 | si = appctx->owner; |
| 2364 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2365 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2366 | /* Check if we run on the same thread than the xreator thread. |
| 2367 | * We cannot access to the socket if the thread is different. |
| 2368 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2369 | if (socket->tid != tid) { |
| 2370 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2371 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2372 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2373 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2374 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2375 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2376 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2377 | lua_pushnil(L); |
| 2378 | lua_pushstring(L, "Can't connect"); |
| 2379 | return 2; |
| 2380 | } |
| 2381 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2382 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2383 | |
| 2384 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2385 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2386 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2387 | lua_pushinteger(L, 1); |
| 2388 | return 1; |
| 2389 | } |
| 2390 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2391 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2392 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2393 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2394 | } |
| 2395 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2396 | 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] | 2397 | return 0; |
| 2398 | } |
| 2399 | |
| 2400 | /* This function fail or initite the connection. */ |
| 2401 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2402 | { |
| 2403 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2404 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2405 | const char *ip; |
| 2406 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2407 | struct hlua *hlua; |
| 2408 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2409 | int low, high; |
| 2410 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2411 | struct xref *peer; |
| 2412 | struct stream_interface *si; |
| 2413 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2414 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2415 | if (lua_gettop(L) < 2) |
| 2416 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2417 | |
| 2418 | /* Get args. */ |
| 2419 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2420 | |
| 2421 | /* Check if we run on the same thread than the xreator thread. |
| 2422 | * We cannot access to the socket if the thread is different. |
| 2423 | */ |
| 2424 | if (socket->tid != tid) |
| 2425 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2426 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2427 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2428 | if (lua_gettop(L) >= 3) { |
| 2429 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2430 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2431 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2432 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2433 | * that are not enclosed within square brackets. |
| 2434 | */ |
| 2435 | if (port > 0) { |
| 2436 | luaL_buffinit(L, &b); |
| 2437 | luaL_addstring(&b, ip); |
| 2438 | luaL_addchar(&b, ':'); |
| 2439 | luaL_pushresult(&b); |
| 2440 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2441 | } |
| 2442 | } |
| 2443 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2444 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2445 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2446 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2447 | lua_pushnil(L); |
| 2448 | return 1; |
| 2449 | } |
| 2450 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2451 | si = appctx->owner; |
| 2452 | s = si_strm(si); |
| 2453 | |
| 2454 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2455 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2456 | if (!conn) { |
| 2457 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2458 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2459 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2460 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2461 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2462 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2463 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2464 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2465 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2466 | if (!addr) { |
| 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: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2469 | } |
| 2470 | if (low != high) { |
| 2471 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2472 | 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] | 2473 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2474 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2475 | |
| 2476 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2477 | if (low == 0) { |
| 2478 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2479 | if (port == -1) { |
| 2480 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2481 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2482 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2483 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2484 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2485 | if (port == -1) { |
| 2486 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2487 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2488 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2489 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2490 | } |
| 2491 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2492 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2493 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2494 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2495 | |
| 2496 | /* inform the stream that we want to be notified whenever the |
| 2497 | * connection completes. |
| 2498 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2499 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2500 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2501 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2502 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2503 | hlua->flags |= HLUA_MUST_GC; |
| 2504 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2505 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2506 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2507 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2508 | } |
| 2509 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2510 | |
| 2511 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2512 | /* Return yield waiting for connection. */ |
| 2513 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2514 | 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] | 2515 | |
| 2516 | return 0; |
| 2517 | } |
| 2518 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2519 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2520 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2521 | { |
| 2522 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2523 | struct xref *peer; |
| 2524 | struct appctx *appctx; |
| 2525 | struct stream_interface *si; |
| 2526 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2527 | |
| 2528 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2529 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2530 | |
| 2531 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2532 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2533 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2534 | lua_pushnil(L); |
| 2535 | return 1; |
| 2536 | } |
| 2537 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2538 | si = appctx->owner; |
| 2539 | s = si_strm(si); |
| 2540 | |
| 2541 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2542 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2543 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2544 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2545 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2546 | |
| 2547 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2548 | { |
| 2549 | return 0; |
| 2550 | } |
| 2551 | |
| 2552 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2553 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2554 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2555 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2556 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2557 | struct xref *peer; |
| 2558 | struct appctx *appctx; |
| 2559 | struct stream_interface *si; |
| 2560 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2561 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2562 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2563 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2564 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2565 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2566 | /* convert the timeout to millis */ |
| 2567 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2568 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2569 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2570 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2571 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2572 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2573 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2574 | 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] | 2575 | |
| 2576 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2577 | if (tmout == 0) |
| 2578 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2579 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2580 | /* Check if we run on the same thread than the xreator thread. |
| 2581 | * We cannot access to the socket if the thread is different. |
| 2582 | */ |
| 2583 | if (socket->tid != tid) |
| 2584 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2585 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2586 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2587 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2588 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2589 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2590 | WILL_LJMP(lua_error(L)); |
| 2591 | return 0; |
| 2592 | } |
| 2593 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2594 | si = appctx->owner; |
| 2595 | s = si_strm(si); |
| 2596 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2597 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2598 | s->req.rto = tmout; |
| 2599 | s->req.wto = tmout; |
| 2600 | s->res.rto = tmout; |
| 2601 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2602 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2603 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2604 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2605 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2606 | |
| 2607 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2608 | task_queue(s->task); |
| 2609 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2610 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2611 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2612 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2613 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2614 | } |
| 2615 | |
| 2616 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2617 | { |
| 2618 | struct hlua_socket *socket; |
| 2619 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2620 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2621 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2622 | |
| 2623 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2624 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2625 | hlua_pusherror(L, "socket: full stack"); |
| 2626 | goto out_fail_conf; |
| 2627 | } |
| 2628 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2629 | /* Create the object: obj[0] = userdata. */ |
| 2630 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2631 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2632 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2633 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2634 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2635 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2636 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2637 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2638 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2639 | goto out_fail_conf; |
| 2640 | } |
| 2641 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2642 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2643 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2644 | lua_setmetatable(L, -2); |
| 2645 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2646 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2647 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2648 | if (!appctx) { |
| 2649 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2650 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2651 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2652 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2653 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2654 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2655 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2656 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2657 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2658 | /* Now create a session, task and stream for this applet */ |
| 2659 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2660 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2661 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2662 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2663 | } |
| 2664 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2665 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2666 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2667 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2668 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2669 | } |
| 2670 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2671 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2672 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2673 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2674 | /* Configure "right" stream interface. this "si" is used to connect |
| 2675 | * and retrieve data from the server. The connection is initialized |
| 2676 | * with the "struct server". |
| 2677 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2678 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2679 | |
| 2680 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2681 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2682 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2683 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2684 | return 1; |
| 2685 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2686 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2687 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2688 | out_fail_sess: |
| 2689 | appctx_free(appctx); |
| 2690 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2691 | WILL_LJMP(lua_error(L)); |
| 2692 | return 0; |
| 2693 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2694 | |
| 2695 | /* |
| 2696 | * |
| 2697 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2698 | * Class Channel |
| 2699 | * |
| 2700 | * |
| 2701 | */ |
| 2702 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2703 | /* This function is called before the Lua execution. It stores |
| 2704 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2705 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2706 | 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] | 2707 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2708 | c->mode = stream->be->mode; |
| 2709 | switch (c->mode) { |
| 2710 | case PR_MODE_HTTP: |
| 2711 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2712 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2713 | c->data.http.state = stream->txn->req.msg_state; |
| 2714 | else |
| 2715 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2716 | break; |
| 2717 | default: |
| 2718 | break; |
| 2719 | } |
| 2720 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2721 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2722 | /* This function is called after the Lua execution. it |
| 2723 | * returns true if the parser state is consistent, otherwise, |
| 2724 | * it return false. |
| 2725 | * |
| 2726 | * In HTTP mode, the parser state must be in the same state |
| 2727 | * or greater when we exit the function. Even if we do a |
| 2728 | * control yield. This prevent to break the HTTP message |
| 2729 | * from the Lua code. |
| 2730 | */ |
| 2731 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2732 | { |
| 2733 | if (c->mode != stream->be->mode) |
| 2734 | return 0; |
| 2735 | |
| 2736 | switch (c->mode) { |
| 2737 | case PR_MODE_HTTP: |
| 2738 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2739 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2740 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2741 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2742 | else |
| 2743 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2744 | default: |
| 2745 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2746 | } |
| 2747 | return 1; |
| 2748 | } |
| 2749 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2750 | /* Returns the struct hlua_channel join to the class channel in the |
| 2751 | * stack entry "ud" or throws an argument error. |
| 2752 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2753 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2754 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2755 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2756 | } |
| 2757 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2758 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2759 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2760 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2761 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2762 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2763 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2764 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2765 | return 0; |
| 2766 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2767 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2768 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2769 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2770 | |
| 2771 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2772 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2773 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2774 | return 1; |
| 2775 | } |
| 2776 | |
| 2777 | /* Duplicate all the data present in the input channel and put it |
| 2778 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2779 | * the stack if the channel is closed and all the data are consumed, |
| 2780 | * returns 0 if no data are available, otherwise it returns the length |
| 2781 | * of the builded string. |
| 2782 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2783 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2784 | { |
| 2785 | char *blk1; |
| 2786 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2787 | size_t len1; |
| 2788 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2789 | int ret; |
| 2790 | luaL_Buffer b; |
| 2791 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2792 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2793 | if (unlikely(ret == 0)) |
| 2794 | return 0; |
| 2795 | |
| 2796 | if (unlikely(ret < 0)) { |
| 2797 | lua_pushnil(L); |
| 2798 | return -1; |
| 2799 | } |
| 2800 | |
| 2801 | luaL_buffinit(L, &b); |
| 2802 | luaL_addlstring(&b, blk1, len1); |
| 2803 | if (unlikely(ret == 2)) |
| 2804 | luaL_addlstring(&b, blk2, len2); |
| 2805 | luaL_pushresult(&b); |
| 2806 | |
| 2807 | if (unlikely(ret == 2)) |
| 2808 | return len1 + len2; |
| 2809 | return len1; |
| 2810 | } |
| 2811 | |
| 2812 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2813 | * a yield. This function keep the data in the buffer. |
| 2814 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2815 | __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] | 2816 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2817 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2818 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2819 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2820 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2821 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2822 | WILL_LJMP(lua_error(L)); |
| 2823 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2824 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2825 | 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] | 2826 | return 1; |
| 2827 | } |
| 2828 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2829 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2830 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2831 | { |
| 2832 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2833 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2834 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2835 | } |
| 2836 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2837 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2838 | * a yield. This function consumes the data in the buffer. It returns |
| 2839 | * a string containing the data or a nil pointer if no data are available |
| 2840 | * and the channel is closed. |
| 2841 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2842 | __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] | 2843 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2844 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2845 | int ret; |
| 2846 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2847 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2848 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2849 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2850 | WILL_LJMP(lua_error(L)); |
| 2851 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2852 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2853 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2854 | 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] | 2855 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2856 | if (unlikely(ret == -1)) |
| 2857 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2858 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2859 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2860 | return 1; |
| 2861 | } |
| 2862 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2863 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2864 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2865 | { |
| 2866 | MAY_LJMP(check_args(L, 1, "get")); |
| 2867 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2868 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2869 | } |
| 2870 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2871 | /* This functions consumes and returns one line. If the channel is closed, |
| 2872 | * 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] | 2873 | * 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] | 2874 | * value. |
| 2875 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2876 | __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] | 2877 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2878 | char *blk1; |
| 2879 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2880 | size_t len1; |
| 2881 | size_t len2; |
| 2882 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2883 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2884 | int ret; |
| 2885 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2886 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2887 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2888 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2889 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2890 | WILL_LJMP(lua_error(L)); |
| 2891 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2892 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2893 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2894 | 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] | 2895 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2896 | if (ret == -1) { |
| 2897 | lua_pushnil(L); |
| 2898 | return 1; |
| 2899 | } |
| 2900 | |
| 2901 | luaL_buffinit(L, &b); |
| 2902 | luaL_addlstring(&b, blk1, len1); |
| 2903 | len = len1; |
| 2904 | if (unlikely(ret == 2)) { |
| 2905 | luaL_addlstring(&b, blk2, len2); |
| 2906 | len += len2; |
| 2907 | } |
| 2908 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2909 | 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] | 2910 | return 1; |
| 2911 | } |
| 2912 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2913 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2914 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2915 | { |
| 2916 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2917 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2918 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2919 | } |
| 2920 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2921 | /* This function takes a string as input, and append it at the |
| 2922 | * input side of channel. If the data is too big, but a space |
| 2923 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2924 | * yields. If the data is bigger than the buffer, or if the |
| 2925 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2926 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2927 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2928 | __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] | 2929 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2930 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2931 | size_t len; |
| 2932 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2933 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2934 | int ret; |
| 2935 | int max; |
| 2936 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2937 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2938 | WILL_LJMP(lua_error(L)); |
| 2939 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2940 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2941 | * the request buffer if its not required. |
| 2942 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2943 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2944 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2945 | 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] | 2946 | } |
| 2947 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2948 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2949 | if (max > len - l) |
| 2950 | max = len - l; |
| 2951 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2952 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2953 | if (ret == -2 || ret == -3) { |
| 2954 | lua_pushinteger(L, -1); |
| 2955 | return 1; |
| 2956 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2957 | if (ret == -1) { |
| 2958 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2959 | 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] | 2960 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2961 | l += ret; |
| 2962 | lua_pop(L, 1); |
| 2963 | lua_pushinteger(L, l); |
| 2964 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2965 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2966 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2967 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2968 | * in this case, we cannot add more data, so we cannot yield, |
| 2969 | * we return the amount of copyied data. |
| 2970 | */ |
| 2971 | return 1; |
| 2972 | } |
| 2973 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2974 | 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] | 2975 | return 1; |
| 2976 | } |
| 2977 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2978 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2979 | * 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] | 2980 | * buffer size is too little for the data. |
| 2981 | */ |
| 2982 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2983 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2984 | size_t len; |
| 2985 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2986 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2987 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2988 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2989 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2990 | lua_pushinteger(L, 0); |
| 2991 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2992 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2993 | } |
| 2994 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2995 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2996 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2997 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2998 | * or -1 if the channel is closed or if the buffer size is too |
| 2999 | * little for the data. |
| 3000 | */ |
| 3001 | __LJMP static int hlua_channel_set(lua_State *L) |
| 3002 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3003 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3004 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3005 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3006 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3007 | lua_pushinteger(L, 0); |
| 3008 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3009 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3010 | WILL_LJMP(lua_error(L)); |
| 3011 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3012 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3013 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3014 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3015 | } |
| 3016 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3017 | /* Append data in the output side of the buffer. This data is immediately |
| 3018 | * sent. The function returns the amount of data written. If the buffer |
| 3019 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3020 | * if the channel is closed. |
| 3021 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3022 | __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] | 3023 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3024 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3025 | size_t len; |
| 3026 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3027 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3028 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3029 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3030 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3031 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3032 | WILL_LJMP(lua_error(L)); |
| 3033 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3034 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3035 | lua_pushinteger(L, -1); |
| 3036 | return 1; |
| 3037 | } |
| 3038 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3039 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3040 | * the request buffer if its not required. |
| 3041 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3042 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3043 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3044 | 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] | 3045 | } |
| 3046 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3047 | /* The written data will be immediately sent, so we can check |
| 3048 | * the available space without taking in account the reserve. |
| 3049 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3050 | * data, because the buffer will be flushed. |
| 3051 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3052 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3053 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3054 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3055 | * in this case, we cannot add more data, so we cannot yield, |
| 3056 | * we return the amount of copyied data. |
| 3057 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3058 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3059 | return 1; |
| 3060 | |
| 3061 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3062 | if (max > len - l) |
| 3063 | max = len - l; |
| 3064 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3065 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3066 | * detects a non contiguous buffer and realign it. |
| 3067 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3068 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3069 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3070 | |
| 3071 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3072 | 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] | 3073 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3074 | /* buffer replace considers that the input part is filled. |
| 3075 | * so, I must forward these new data in the output part. |
| 3076 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3077 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3078 | |
| 3079 | l += max; |
| 3080 | lua_pop(L, 1); |
| 3081 | lua_pushinteger(L, l); |
| 3082 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3083 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3084 | * in this case, we cannot add more data, so we cannot yield, |
| 3085 | * we return the amount of copyied data. |
| 3086 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3087 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3088 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3089 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3090 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3091 | if (l < len) { |
| 3092 | /* If we are waiting for space in the response buffer, we |
| 3093 | * must set the flag WAKERESWR. This flag required the task |
| 3094 | * wake up if any activity is detected on the response buffer. |
| 3095 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3096 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3097 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3098 | else |
| 3099 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3100 | 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] | 3101 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3102 | |
| 3103 | return 1; |
| 3104 | } |
| 3105 | |
| 3106 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3107 | * yield the LUA process, and resume it without checking the |
| 3108 | * input arguments. |
| 3109 | */ |
| 3110 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3111 | { |
| 3112 | MAY_LJMP(check_args(L, 2, "send")); |
| 3113 | lua_pushinteger(L, 0); |
| 3114 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3115 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | /* This function forward and amount of butes. The data pass from |
| 3119 | * the input side of the buffer to the output side, and can be |
| 3120 | * forwarded. This function never fails. |
| 3121 | * |
| 3122 | * The Lua function takes an amount of bytes to be forwarded in |
| 3123 | * imput. It returns the number of bytes forwarded. |
| 3124 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3125 | __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] | 3126 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3127 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3128 | int len; |
| 3129 | int l; |
| 3130 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3131 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3132 | |
| 3133 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3134 | |
| 3135 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3136 | WILL_LJMP(lua_error(L)); |
| 3137 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3138 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3139 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3140 | |
| 3141 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3142 | if (max > ci_data(chn)) |
| 3143 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3144 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3145 | l += max; |
| 3146 | |
| 3147 | lua_pop(L, 1); |
| 3148 | lua_pushinteger(L, l); |
| 3149 | |
| 3150 | /* Check if it miss bytes to forward. */ |
| 3151 | if (l < len) { |
| 3152 | /* The the input channel or the output channel are closed, we |
| 3153 | * must return the amount of data forwarded. |
| 3154 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3155 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3156 | return 1; |
| 3157 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3158 | /* If we are waiting for space data in the response buffer, we |
| 3159 | * must set the flag WAKERESWR. This flag required the task |
| 3160 | * wake up if any activity is detected on the response buffer. |
| 3161 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3162 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3163 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3164 | else |
| 3165 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3166 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3167 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3168 | 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] | 3169 | } |
| 3170 | |
| 3171 | return 1; |
| 3172 | } |
| 3173 | |
| 3174 | /* Just check the input and prepare the stack for the previous |
| 3175 | * function "hlua_channel_forward_yield" |
| 3176 | */ |
| 3177 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3178 | { |
| 3179 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3180 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3181 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3182 | |
| 3183 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3184 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | /* Just returns the number of bytes available in the input |
| 3188 | * side of the buffer. This function never fails. |
| 3189 | */ |
| 3190 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3191 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3192 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3193 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3194 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3195 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3196 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3197 | struct htx *htx = htxbuf(&chn->buf); |
| 3198 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3199 | } |
| 3200 | else |
| 3201 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3202 | return 1; |
| 3203 | } |
| 3204 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3205 | /* Returns true if the channel is full. */ |
| 3206 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3207 | { |
| 3208 | struct channel *chn; |
| 3209 | int rem; |
| 3210 | |
| 3211 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3212 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3213 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3214 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3215 | struct htx *htx = htxbuf(&chn->buf); |
| 3216 | |
| 3217 | rem = htx_free_data_space(htx); |
| 3218 | } |
| 3219 | else |
| 3220 | rem = b_room(&chn->buf); |
| 3221 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3222 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3223 | |
| 3224 | lua_pushboolean(L, rem <= 0); |
| 3225 | return 1; |
| 3226 | } |
| 3227 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3228 | /* Just returns the number of bytes available in the output |
| 3229 | * side of the buffer. This function never fails. |
| 3230 | */ |
| 3231 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3232 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3233 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3234 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3235 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3236 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3237 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3238 | return 1; |
| 3239 | } |
| 3240 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3241 | /* |
| 3242 | * |
| 3243 | * |
| 3244 | * Class Fetches |
| 3245 | * |
| 3246 | * |
| 3247 | */ |
| 3248 | |
| 3249 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3250 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3251 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3252 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3253 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3254 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3255 | } |
| 3256 | |
| 3257 | /* This function creates and push in the stack a fetch object according |
| 3258 | * with a current TXN. |
| 3259 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3260 | 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] | 3261 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3262 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3263 | |
| 3264 | /* Check stack size. */ |
| 3265 | if (!lua_checkstack(L, 3)) |
| 3266 | return 0; |
| 3267 | |
| 3268 | /* Create the object: obj[0] = userdata. |
| 3269 | * Note that the base of the Fetches object is the |
| 3270 | * transaction object. |
| 3271 | */ |
| 3272 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3273 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3274 | lua_rawseti(L, -2, 0); |
| 3275 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3276 | hsmp->s = txn->s; |
| 3277 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3278 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3279 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3280 | |
| 3281 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3282 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3283 | lua_setmetatable(L, -2); |
| 3284 | |
| 3285 | return 1; |
| 3286 | } |
| 3287 | |
| 3288 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3289 | * It uses closure argument to store the associated sample-fetch. It |
| 3290 | * returns only one argument or throws an error. An error is thrown |
| 3291 | * only if an error is encountered during the argument parsing. If |
| 3292 | * the "sample-fetch" function fails, nil is returned. |
| 3293 | */ |
| 3294 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3295 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3296 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3297 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3298 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3299 | int i; |
| 3300 | struct sample smp; |
| 3301 | |
| 3302 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3303 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3304 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3305 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3306 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3307 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3308 | /* Check execution authorization. */ |
| 3309 | if (f->use & SMP_USE_HTTP_ANY && |
| 3310 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3311 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3312 | "is not available in Lua services", f->kw); |
| 3313 | WILL_LJMP(lua_error(L)); |
| 3314 | } |
| 3315 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3316 | /* Get extra arguments. */ |
| 3317 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3318 | if (i >= ARGM_NBARGS) |
| 3319 | break; |
| 3320 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3321 | } |
| 3322 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3323 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3324 | |
| 3325 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3326 | 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] | 3327 | |
| 3328 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3329 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3330 | lua_pushfstring(L, "error in arguments"); |
| 3331 | WILL_LJMP(lua_error(L)); |
| 3332 | } |
| 3333 | |
| 3334 | /* Initialise the sample. */ |
| 3335 | memset(&smp, 0, sizeof(smp)); |
| 3336 | |
| 3337 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3338 | 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] | 3339 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3340 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3341 | lua_pushstring(L, ""); |
| 3342 | else |
| 3343 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3344 | return 1; |
| 3345 | } |
| 3346 | |
| 3347 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3348 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3349 | hlua_smp2lua_str(L, &smp); |
| 3350 | else |
| 3351 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3352 | return 1; |
| 3353 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3354 | |
| 3355 | /* |
| 3356 | * |
| 3357 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3358 | * Class Converters |
| 3359 | * |
| 3360 | * |
| 3361 | */ |
| 3362 | |
| 3363 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3364 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3365 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3366 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3367 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3368 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3369 | } |
| 3370 | |
| 3371 | /* This function creates and push in the stack a Converters object |
| 3372 | * according with a current TXN. |
| 3373 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3374 | 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] | 3375 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3376 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3377 | |
| 3378 | /* Check stack size. */ |
| 3379 | if (!lua_checkstack(L, 3)) |
| 3380 | return 0; |
| 3381 | |
| 3382 | /* Create the object: obj[0] = userdata. |
| 3383 | * Note that the base of the Converters object is the |
| 3384 | * same than the TXN object. |
| 3385 | */ |
| 3386 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3387 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3388 | lua_rawseti(L, -2, 0); |
| 3389 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3390 | hsmp->s = txn->s; |
| 3391 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3392 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3393 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3394 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3395 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3396 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3397 | lua_setmetatable(L, -2); |
| 3398 | |
| 3399 | return 1; |
| 3400 | } |
| 3401 | |
| 3402 | /* This function is an LUA binding. It is called with each converter. |
| 3403 | * It uses closure argument to store the associated converter. It |
| 3404 | * returns only one argument or throws an error. An error is thrown |
| 3405 | * only if an error is encountered during the argument parsing. If |
| 3406 | * the converter function function fails, nil is returned. |
| 3407 | */ |
| 3408 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3409 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3410 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3411 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3412 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3413 | int i; |
| 3414 | struct sample smp; |
| 3415 | |
| 3416 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3417 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3418 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3419 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3420 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3421 | |
| 3422 | /* Get extra arguments. */ |
| 3423 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3424 | if (i >= ARGM_NBARGS) |
| 3425 | break; |
| 3426 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3427 | } |
| 3428 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3429 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3430 | |
| 3431 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3432 | 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] | 3433 | |
| 3434 | /* Run the special args checker. */ |
| 3435 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3436 | hlua_pusherror(L, "error in arguments"); |
| 3437 | WILL_LJMP(lua_error(L)); |
| 3438 | } |
| 3439 | |
| 3440 | /* Initialise the sample. */ |
| 3441 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3442 | hlua_pusherror(L, "error in the input argument"); |
| 3443 | WILL_LJMP(lua_error(L)); |
| 3444 | } |
| 3445 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3446 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3447 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3448 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3449 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3450 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3451 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3452 | WILL_LJMP(lua_error(L)); |
| 3453 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3454 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3455 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3456 | hlua_pusherror(L, "error during the input argument casting"); |
| 3457 | WILL_LJMP(lua_error(L)); |
| 3458 | } |
| 3459 | |
| 3460 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3461 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3462 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3463 | lua_pushstring(L, ""); |
| 3464 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3465 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3466 | return 1; |
| 3467 | } |
| 3468 | |
| 3469 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3470 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3471 | hlua_smp2lua_str(L, &smp); |
| 3472 | else |
| 3473 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3474 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3475 | } |
| 3476 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3477 | /* |
| 3478 | * |
| 3479 | * |
| 3480 | * Class AppletTCP |
| 3481 | * |
| 3482 | * |
| 3483 | */ |
| 3484 | |
| 3485 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3486 | * a class stream, otherwise it throws an error. |
| 3487 | */ |
| 3488 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3489 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3490 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | /* This function creates and push in the stack an Applet object |
| 3494 | * according with a current TXN. |
| 3495 | */ |
| 3496 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3497 | { |
| 3498 | struct hlua_appctx *appctx; |
| 3499 | struct stream_interface *si = ctx->owner; |
| 3500 | struct stream *s = si_strm(si); |
| 3501 | struct proxy *p = s->be; |
| 3502 | |
| 3503 | /* Check stack size. */ |
| 3504 | if (!lua_checkstack(L, 3)) |
| 3505 | return 0; |
| 3506 | |
| 3507 | /* Create the object: obj[0] = userdata. |
| 3508 | * Note that the base of the Converters object is the |
| 3509 | * same than the TXN object. |
| 3510 | */ |
| 3511 | lua_newtable(L); |
| 3512 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3513 | lua_rawseti(L, -2, 0); |
| 3514 | appctx->appctx = ctx; |
| 3515 | appctx->htxn.s = s; |
| 3516 | appctx->htxn.p = p; |
| 3517 | |
| 3518 | /* Create the "f" field that contains a list of fetches. */ |
| 3519 | lua_pushstring(L, "f"); |
| 3520 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3521 | return 0; |
| 3522 | lua_settable(L, -3); |
| 3523 | |
| 3524 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3525 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3526 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3527 | return 0; |
| 3528 | lua_settable(L, -3); |
| 3529 | |
| 3530 | /* Create the "c" field that contains a list of converters. */ |
| 3531 | lua_pushstring(L, "c"); |
| 3532 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3533 | return 0; |
| 3534 | lua_settable(L, -3); |
| 3535 | |
| 3536 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3537 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3538 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3539 | return 0; |
| 3540 | lua_settable(L, -3); |
| 3541 | |
| 3542 | /* Pop a class stream metatable and affect it to the table. */ |
| 3543 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3544 | lua_setmetatable(L, -2); |
| 3545 | |
| 3546 | return 1; |
| 3547 | } |
| 3548 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3549 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3550 | { |
| 3551 | struct hlua_appctx *appctx; |
| 3552 | struct stream *s; |
| 3553 | const char *name; |
| 3554 | size_t len; |
| 3555 | struct sample smp; |
| 3556 | |
| 3557 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3558 | |
| 3559 | /* It is useles to retrieve the stream, but this function |
| 3560 | * runs only in a stream context. |
| 3561 | */ |
| 3562 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3563 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3564 | s = appctx->htxn.s; |
| 3565 | |
| 3566 | /* Converts the third argument in a sample. */ |
| 3567 | hlua_lua2smp(L, 3, &smp); |
| 3568 | |
| 3569 | /* Store the sample in a variable. */ |
| 3570 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3571 | vars_set_by_name(name, len, &smp); |
| 3572 | return 0; |
| 3573 | } |
| 3574 | |
| 3575 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3576 | { |
| 3577 | struct hlua_appctx *appctx; |
| 3578 | struct stream *s; |
| 3579 | const char *name; |
| 3580 | size_t len; |
| 3581 | struct sample smp; |
| 3582 | |
| 3583 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3584 | |
| 3585 | /* It is useles to retrieve the stream, but this function |
| 3586 | * runs only in a stream context. |
| 3587 | */ |
| 3588 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3589 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3590 | s = appctx->htxn.s; |
| 3591 | |
| 3592 | /* Unset the variable. */ |
| 3593 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3594 | vars_unset_by_name(name, len, &smp); |
| 3595 | return 0; |
| 3596 | } |
| 3597 | |
| 3598 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3599 | { |
| 3600 | struct hlua_appctx *appctx; |
| 3601 | struct stream *s; |
| 3602 | const char *name; |
| 3603 | size_t len; |
| 3604 | struct sample smp; |
| 3605 | |
| 3606 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3607 | |
| 3608 | /* It is useles to retrieve the stream, but this function |
| 3609 | * runs only in a stream context. |
| 3610 | */ |
| 3611 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3612 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3613 | s = appctx->htxn.s; |
| 3614 | |
| 3615 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3616 | if (!vars_get_by_name(name, len, &smp)) { |
| 3617 | lua_pushnil(L); |
| 3618 | return 1; |
| 3619 | } |
| 3620 | |
| 3621 | return hlua_smp2lua(L, &smp); |
| 3622 | } |
| 3623 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3624 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3625 | { |
| 3626 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3627 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3628 | struct hlua *hlua; |
| 3629 | |
| 3630 | /* 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] | 3631 | if (!s->hlua) |
| 3632 | return 0; |
| 3633 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3634 | |
| 3635 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3636 | |
| 3637 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3638 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3639 | |
| 3640 | /* Get and store new value. */ |
| 3641 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3642 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3643 | |
| 3644 | return 0; |
| 3645 | } |
| 3646 | |
| 3647 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3648 | { |
| 3649 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3650 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3651 | struct hlua *hlua; |
| 3652 | |
| 3653 | /* 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] | 3654 | if (!s->hlua) { |
| 3655 | lua_pushnil(L); |
| 3656 | return 1; |
| 3657 | } |
| 3658 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3659 | |
| 3660 | /* Push configuration index in the stack. */ |
| 3661 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3662 | |
| 3663 | return 1; |
| 3664 | } |
| 3665 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3666 | /* If expected data not yet available, it returns a yield. This function |
| 3667 | * consumes the data in the buffer. It returns a string containing the |
| 3668 | * data. This string can be empty. |
| 3669 | */ |
| 3670 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3671 | { |
| 3672 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3673 | struct stream_interface *si = appctx->appctx->owner; |
| 3674 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3675 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3676 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3677 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3678 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3679 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3680 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3681 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3682 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3683 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3684 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3685 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3686 | 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] | 3687 | } |
| 3688 | |
| 3689 | /* End of data: commit the total strings and return. */ |
| 3690 | if (ret < 0) { |
| 3691 | luaL_pushresult(&appctx->b); |
| 3692 | return 1; |
| 3693 | } |
| 3694 | |
| 3695 | /* Ensure that the block 2 length is usable. */ |
| 3696 | if (ret == 1) |
| 3697 | len2 = 0; |
| 3698 | |
| 3699 | /* dont check the max length read and dont check. */ |
| 3700 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3701 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3702 | |
| 3703 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3704 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3705 | luaL_pushresult(&appctx->b); |
| 3706 | return 1; |
| 3707 | } |
| 3708 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3709 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3710 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3711 | { |
| 3712 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3713 | |
| 3714 | /* Initialise the string catenation. */ |
| 3715 | luaL_buffinit(L, &appctx->b); |
| 3716 | |
| 3717 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3718 | } |
| 3719 | |
| 3720 | /* If expected data not yet available, it returns a yield. This function |
| 3721 | * consumes the data in the buffer. It returns a string containing the |
| 3722 | * data. This string can be empty. |
| 3723 | */ |
| 3724 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3725 | { |
| 3726 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3727 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3728 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3729 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3730 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3731 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3732 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3733 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3734 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3735 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3736 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3737 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3738 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3739 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3740 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3741 | 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] | 3742 | } |
| 3743 | |
| 3744 | /* End of data: commit the total strings and return. */ |
| 3745 | if (ret < 0) { |
| 3746 | luaL_pushresult(&appctx->b); |
| 3747 | return 1; |
| 3748 | } |
| 3749 | |
| 3750 | /* Ensure that the block 2 length is usable. */ |
| 3751 | if (ret == 1) |
| 3752 | len2 = 0; |
| 3753 | |
| 3754 | if (len == -1) { |
| 3755 | |
| 3756 | /* If len == -1, catenate all the data avalaile and |
| 3757 | * yield because we want to get all the data until |
| 3758 | * the end of data stream. |
| 3759 | */ |
| 3760 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3761 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3762 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3763 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3764 | 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] | 3765 | |
| 3766 | } else { |
| 3767 | |
| 3768 | /* Copy the fisrt block caping to the length required. */ |
| 3769 | if (len1 > len) |
| 3770 | len1 = len; |
| 3771 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3772 | len -= len1; |
| 3773 | |
| 3774 | /* Copy the second block. */ |
| 3775 | if (len2 > len) |
| 3776 | len2 = len; |
| 3777 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3778 | len -= len2; |
| 3779 | |
| 3780 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3781 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3782 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3783 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3784 | if (len > 0) { |
| 3785 | lua_pushinteger(L, len); |
| 3786 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3787 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3788 | 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] | 3789 | } |
| 3790 | |
| 3791 | /* return the result. */ |
| 3792 | luaL_pushresult(&appctx->b); |
| 3793 | return 1; |
| 3794 | } |
| 3795 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3796 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3797 | hlua_pusherror(L, "Lua: internal error"); |
| 3798 | WILL_LJMP(lua_error(L)); |
| 3799 | return 0; |
| 3800 | } |
| 3801 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3802 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3803 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3804 | { |
| 3805 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3806 | int len = -1; |
| 3807 | |
| 3808 | if (lua_gettop(L) > 2) |
| 3809 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3810 | if (lua_gettop(L) >= 2) { |
| 3811 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3812 | lua_pop(L, 1); |
| 3813 | } |
| 3814 | |
| 3815 | /* Confirm or set the required length */ |
| 3816 | lua_pushinteger(L, len); |
| 3817 | |
| 3818 | /* Initialise the string catenation. */ |
| 3819 | luaL_buffinit(L, &appctx->b); |
| 3820 | |
| 3821 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3822 | } |
| 3823 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3824 | /* Append data in the output side of the buffer. This data is immediately |
| 3825 | * sent. The function returns the amount of data written. If the buffer |
| 3826 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3827 | * if the channel is closed. |
| 3828 | */ |
| 3829 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3830 | { |
| 3831 | size_t len; |
| 3832 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3833 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3834 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3835 | struct stream_interface *si = appctx->appctx->owner; |
| 3836 | struct channel *chn = si_ic(si); |
| 3837 | int max; |
| 3838 | |
| 3839 | /* Get the max amount of data which can write as input in the channel. */ |
| 3840 | max = channel_recv_max(chn); |
| 3841 | if (max > (len - l)) |
| 3842 | max = len - l; |
| 3843 | |
| 3844 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3845 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3846 | |
| 3847 | /* update counters. */ |
| 3848 | l += max; |
| 3849 | lua_pop(L, 1); |
| 3850 | lua_pushinteger(L, l); |
| 3851 | |
| 3852 | /* If some data is not send, declares the situation to the |
| 3853 | * applet, and returns a yield. |
| 3854 | */ |
| 3855 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3856 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3857 | 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] | 3858 | } |
| 3859 | |
| 3860 | return 1; |
| 3861 | } |
| 3862 | |
| 3863 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3864 | * yield the LUA process, and resume it without checking the |
| 3865 | * input arguments. |
| 3866 | */ |
| 3867 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3868 | { |
| 3869 | MAY_LJMP(check_args(L, 2, "send")); |
| 3870 | lua_pushinteger(L, 0); |
| 3871 | |
| 3872 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3873 | } |
| 3874 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3875 | /* |
| 3876 | * |
| 3877 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3878 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3879 | * |
| 3880 | * |
| 3881 | */ |
| 3882 | |
| 3883 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3884 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3885 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3886 | __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] | 3887 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3888 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3889 | } |
| 3890 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3891 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3892 | * according with a current TXN. |
| 3893 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3894 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3895 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3896 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3897 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3898 | struct stream_interface *si = ctx->owner; |
| 3899 | struct stream *s = si_strm(si); |
| 3900 | struct proxy *px = s->be; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3901 | |
| 3902 | /* Check stack size. */ |
| 3903 | if (!lua_checkstack(L, 3)) |
| 3904 | return 0; |
| 3905 | |
| 3906 | /* Create the object: obj[0] = userdata. |
| 3907 | * Note that the base of the Converters object is the |
| 3908 | * same than the TXN object. |
| 3909 | */ |
| 3910 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3911 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3912 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3913 | appctx->appctx = ctx; |
| 3914 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3915 | 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] | 3916 | appctx->htxn.s = s; |
| 3917 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3918 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3919 | /* Create the "f" field that contains a list of fetches. */ |
| 3920 | lua_pushstring(L, "f"); |
| 3921 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3922 | return 0; |
| 3923 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3924 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3925 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3926 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3927 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3928 | return 0; |
| 3929 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3930 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3931 | /* Create the "c" field that contains a list of converters. */ |
| 3932 | lua_pushstring(L, "c"); |
| 3933 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3934 | return 0; |
| 3935 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3936 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3937 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3938 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3939 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3940 | return 0; |
| 3941 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3942 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3943 | if (IS_HTX_STRM(s)) { |
| 3944 | /* HTX version */ |
| 3945 | struct htx *htx = htxbuf(&s->req.buf); |
| 3946 | struct htx_sl *sl = http_find_stline(htx); |
| 3947 | struct ist path; |
| 3948 | unsigned long long len = 0; |
| 3949 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3950 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3951 | /* Stores the request method. */ |
| 3952 | lua_pushstring(L, "method"); |
| 3953 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3954 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3955 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3956 | /* Stores the http version. */ |
| 3957 | lua_pushstring(L, "version"); |
| 3958 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3959 | lua_settable(L, -3); |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3960 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3961 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3962 | * the array on the top of the stack. |
| 3963 | */ |
| 3964 | lua_pushstring(L, "headers"); |
| 3965 | htxn.s = s; |
| 3966 | htxn.p = px; |
| 3967 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3968 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3969 | return 0; |
| 3970 | lua_settable(L, -3); |
| 3971 | |
| 3972 | path = http_get_path(htx_sl_req_uri(sl)); |
| 3973 | if (path.ptr) { |
| 3974 | char *p, *q, *end; |
| 3975 | |
| 3976 | p = path.ptr; |
| 3977 | end = path.ptr + path.len; |
| 3978 | q = p; |
| 3979 | while (q < end && *q != '?') |
| 3980 | q++; |
| 3981 | |
| 3982 | /* Stores the request path. */ |
| 3983 | lua_pushstring(L, "path"); |
| 3984 | lua_pushlstring(L, p, q - p); |
| 3985 | lua_settable(L, -3); |
| 3986 | |
| 3987 | /* Stores the query string. */ |
| 3988 | lua_pushstring(L, "qs"); |
| 3989 | if (*q == '?') |
| 3990 | q++; |
| 3991 | lua_pushlstring(L, q, end - q); |
| 3992 | lua_settable(L, -3); |
| 3993 | } |
| 3994 | |
| 3995 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 3996 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3997 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 3998 | |
| 3999 | if (type == HTX_BLK_EOM || type == HTX_BLK_EOD) |
| 4000 | break; |
| 4001 | if (type == HTX_BLK_DATA) |
| 4002 | len += htx_get_blksz(blk); |
| 4003 | } |
| 4004 | if (htx->extra != ULLONG_MAX) |
| 4005 | len += htx->extra; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4006 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4007 | /* Stores the request path. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4008 | lua_pushstring(L, "length"); |
| 4009 | lua_pushinteger(L, len); |
| 4010 | lua_settable(L, -3); |
| 4011 | } |
| 4012 | else { |
| 4013 | /* Legacy HTTP version */ |
| 4014 | struct http_txn *txn = s->txn; |
| 4015 | const char *path; |
| 4016 | const char *end; |
| 4017 | const char *p; |
| 4018 | |
| 4019 | /* Stores the request method. */ |
| 4020 | lua_pushstring(L, "method"); |
| 4021 | 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] | 4022 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4023 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4024 | /* Stores the http version. */ |
| 4025 | lua_pushstring(L, "version"); |
| 4026 | 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] | 4027 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4028 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4029 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4030 | * the array on the top of the stack. |
| 4031 | */ |
| 4032 | lua_pushstring(L, "headers"); |
| 4033 | htxn.s = s; |
| 4034 | htxn.p = px; |
| 4035 | htxn.dir = SMP_OPT_DIR_REQ; |
| 4036 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 4037 | return 0; |
| 4038 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4039 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4040 | /* Get path and qs */ |
| 4041 | path = http_txn_get_path(txn); |
| 4042 | if (path) { |
| 4043 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 4044 | p = path; |
| 4045 | while (p < end && *p != '?') |
| 4046 | p++; |
| 4047 | |
| 4048 | /* Stores the request path. */ |
| 4049 | lua_pushstring(L, "path"); |
| 4050 | lua_pushlstring(L, path, p - path); |
| 4051 | lua_settable(L, -3); |
| 4052 | |
| 4053 | /* Stores the query string. */ |
| 4054 | lua_pushstring(L, "qs"); |
| 4055 | if (*p == '?') |
| 4056 | p++; |
| 4057 | lua_pushlstring(L, p, end - p); |
| 4058 | lua_settable(L, -3); |
| 4059 | } |
| 4060 | |
| 4061 | /* Stores the request path. */ |
| 4062 | lua_pushstring(L, "length"); |
| 4063 | lua_pushinteger(L, txn->req.body_len); |
| 4064 | lua_settable(L, -3); |
| 4065 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4066 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4067 | /* Create an empty array of HTTP request headers. */ |
| 4068 | lua_pushstring(L, "response"); |
| 4069 | lua_newtable(L); |
| 4070 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4071 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4072 | /* Pop a class stream metatable and affect it to the table. */ |
| 4073 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4074 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4075 | |
| 4076 | return 1; |
| 4077 | } |
| 4078 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4079 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4080 | { |
| 4081 | struct hlua_appctx *appctx; |
| 4082 | struct stream *s; |
| 4083 | const char *name; |
| 4084 | size_t len; |
| 4085 | struct sample smp; |
| 4086 | |
| 4087 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4088 | |
| 4089 | /* It is useles to retrieve the stream, but this function |
| 4090 | * runs only in a stream context. |
| 4091 | */ |
| 4092 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4093 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4094 | s = appctx->htxn.s; |
| 4095 | |
| 4096 | /* Converts the third argument in a sample. */ |
| 4097 | hlua_lua2smp(L, 3, &smp); |
| 4098 | |
| 4099 | /* Store the sample in a variable. */ |
| 4100 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4101 | vars_set_by_name(name, len, &smp); |
| 4102 | return 0; |
| 4103 | } |
| 4104 | |
| 4105 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4106 | { |
| 4107 | struct hlua_appctx *appctx; |
| 4108 | struct stream *s; |
| 4109 | const char *name; |
| 4110 | size_t len; |
| 4111 | struct sample smp; |
| 4112 | |
| 4113 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4114 | |
| 4115 | /* It is useles to retrieve the stream, but this function |
| 4116 | * runs only in a stream context. |
| 4117 | */ |
| 4118 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4119 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4120 | s = appctx->htxn.s; |
| 4121 | |
| 4122 | /* Unset the variable. */ |
| 4123 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4124 | vars_unset_by_name(name, len, &smp); |
| 4125 | return 0; |
| 4126 | } |
| 4127 | |
| 4128 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4129 | { |
| 4130 | struct hlua_appctx *appctx; |
| 4131 | struct stream *s; |
| 4132 | const char *name; |
| 4133 | size_t len; |
| 4134 | struct sample smp; |
| 4135 | |
| 4136 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4137 | |
| 4138 | /* It is useles to retrieve the stream, but this function |
| 4139 | * runs only in a stream context. |
| 4140 | */ |
| 4141 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4142 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4143 | s = appctx->htxn.s; |
| 4144 | |
| 4145 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4146 | if (!vars_get_by_name(name, len, &smp)) { |
| 4147 | lua_pushnil(L); |
| 4148 | return 1; |
| 4149 | } |
| 4150 | |
| 4151 | return hlua_smp2lua(L, &smp); |
| 4152 | } |
| 4153 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4154 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4155 | { |
| 4156 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4157 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4158 | struct hlua *hlua; |
| 4159 | |
| 4160 | /* 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] | 4161 | if (!s->hlua) |
| 4162 | return 0; |
| 4163 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4164 | |
| 4165 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4166 | |
| 4167 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4168 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4169 | |
| 4170 | /* Get and store new value. */ |
| 4171 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4172 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4173 | |
| 4174 | return 0; |
| 4175 | } |
| 4176 | |
| 4177 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4178 | { |
| 4179 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4180 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4181 | struct hlua *hlua; |
| 4182 | |
| 4183 | /* 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] | 4184 | if (!s->hlua) { |
| 4185 | lua_pushnil(L); |
| 4186 | return 1; |
| 4187 | } |
| 4188 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4189 | |
| 4190 | /* Push configuration index in the stack. */ |
| 4191 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4192 | |
| 4193 | return 1; |
| 4194 | } |
| 4195 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4196 | __LJMP static void hlua_applet_htx_reply_100_continue(lua_State *L) |
| 4197 | { |
| 4198 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4199 | struct stream_interface *si = appctx->appctx->owner; |
| 4200 | struct channel *res = si_ic(si); |
| 4201 | struct htx *htx = htx_from_buf(&res->buf); |
| 4202 | struct htx_sl *sl; |
| 4203 | unsigned int flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11| |
| 4204 | HTX_SL_F_XFER_LEN|HTX_SL_F_BODYLESS); |
| 4205 | size_t data; |
| 4206 | |
| 4207 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, |
| 4208 | ist("HTTP/1.1"), ist("100"), ist("Continue")); |
| 4209 | if (!sl) |
| 4210 | goto fail; |
| 4211 | sl->info.res.status = 100; |
| 4212 | if (!htx_add_endof(htx, HTX_BLK_EOH) || !htx_add_endof(htx, HTX_BLK_EOM)) |
| 4213 | goto fail; |
| 4214 | |
| 4215 | data = htx->data - co_data(res); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4216 | channel_add_input(res, data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4217 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4218 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4219 | return; |
| 4220 | |
| 4221 | fail: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4222 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4223 | hlua_pusherror(L, "Lua applet http '%s': Failed to create 100-Continue response.\n", |
| 4224 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4225 | WILL_LJMP(lua_error(L)); |
| 4226 | } |
| 4227 | |
| 4228 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4229 | /* If expected data not yet available, it returns a yield. This function |
| 4230 | * consumes the data in the buffer. It returns a string containing the |
| 4231 | * data. This string can be empty. |
| 4232 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4233 | __LJMP static int hlua_applet_htx_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4234 | { |
| 4235 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4236 | struct stream_interface *si = appctx->appctx->owner; |
| 4237 | struct channel *req = si_oc(si); |
| 4238 | struct htx *htx; |
| 4239 | struct htx_blk *blk; |
| 4240 | size_t count; |
| 4241 | int stop = 0; |
| 4242 | |
| 4243 | /* Maybe we cant send a 100-continue ? */ |
| 4244 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) |
| 4245 | MAY_LJMP(hlua_applet_htx_reply_100_continue(L)); |
| 4246 | |
| 4247 | htx = htx_from_buf(&req->buf); |
| 4248 | count = co_data(req); |
| 4249 | blk = htx_get_head_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4250 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4251 | while (count && !stop && blk) { |
| 4252 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4253 | uint32_t sz = htx_get_blksz(blk); |
| 4254 | struct ist v; |
| 4255 | uint32_t vlen; |
| 4256 | char *nl; |
| 4257 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4258 | if (type == HTX_BLK_EOM) { |
| 4259 | stop = 1; |
| 4260 | break; |
| 4261 | } |
| 4262 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4263 | vlen = sz; |
| 4264 | if (vlen > count) { |
| 4265 | if (type != HTX_BLK_DATA) |
| 4266 | break; |
| 4267 | vlen = count; |
| 4268 | } |
| 4269 | |
| 4270 | switch (type) { |
| 4271 | case HTX_BLK_UNUSED: |
| 4272 | break; |
| 4273 | |
| 4274 | case HTX_BLK_DATA: |
| 4275 | v = htx_get_blk_value(htx, blk); |
| 4276 | v.len = vlen; |
| 4277 | nl = istchr(v, '\n'); |
| 4278 | if (nl != NULL) { |
| 4279 | stop = 1; |
| 4280 | vlen = nl - v.ptr + 1; |
| 4281 | } |
| 4282 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4283 | break; |
| 4284 | |
| 4285 | case HTX_BLK_EOD: |
| 4286 | case HTX_BLK_TLR: |
| 4287 | case HTX_BLK_EOM: |
| 4288 | stop = 1; |
| 4289 | break; |
| 4290 | |
| 4291 | default: |
| 4292 | break; |
| 4293 | } |
| 4294 | |
| 4295 | co_set_data(req, co_data(req) - vlen); |
| 4296 | count -= vlen; |
| 4297 | if (sz == vlen) |
| 4298 | blk = htx_remove_blk(htx, blk); |
| 4299 | else { |
| 4300 | htx_cut_data_blk(htx, blk, vlen); |
| 4301 | break; |
| 4302 | } |
| 4303 | } |
| 4304 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4305 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4306 | if (!stop) { |
| 4307 | si_cant_get(si); |
| 4308 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_getline_yield, TICK_ETERNITY, 0)); |
| 4309 | } |
| 4310 | |
| 4311 | /* return the result. */ |
| 4312 | luaL_pushresult(&appctx->b); |
| 4313 | return 1; |
| 4314 | } |
| 4315 | |
| 4316 | |
| 4317 | /* If expected data not yet available, it returns a yield. This function |
| 4318 | * consumes the data in the buffer. It returns a string containing the |
| 4319 | * data. This string can be empty. |
| 4320 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4321 | __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] | 4322 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4323 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4324 | struct stream_interface *si = appctx->appctx->owner; |
| 4325 | struct channel *chn = si_ic(si); |
| 4326 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4327 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4328 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4329 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4330 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4331 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4332 | /* Maybe we cant send a 100-continue ? */ |
| 4333 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4334 | ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4335 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4336 | * big for the buffers. We cant send anything. So, we ignoring |
| 4337 | * the error, considers that the 100-continue is sent, and try |
| 4338 | * to receive. |
| 4339 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4340 | */ |
| 4341 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4342 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4343 | 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] | 4344 | } |
| 4345 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 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 | /* Check for the end of the data. */ |
| 4349 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4350 | luaL_pushresult(&appctx->b); |
| 4351 | return 1; |
| 4352 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4353 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4354 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4355 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4356 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4357 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4358 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4359 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4360 | 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] | 4361 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4362 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4363 | /* End of data: commit the total strings and return. */ |
| 4364 | if (ret < 0) { |
| 4365 | luaL_pushresult(&appctx->b); |
| 4366 | return 1; |
| 4367 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4368 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4369 | /* Ensure that the block 2 length is usable. */ |
| 4370 | if (ret == 1) |
| 4371 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4372 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4373 | /* Copy the fisrt block caping to the length required. */ |
| 4374 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4375 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4376 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4377 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
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 | /* Copy the second block. */ |
| 4380 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4381 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4382 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4383 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4384 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4385 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4386 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4387 | luaL_pushresult(&appctx->b); |
| 4388 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4389 | } |
| 4390 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4391 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4392 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4393 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4394 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4395 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4396 | /* Initialise the string catenation. */ |
| 4397 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4398 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4399 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 4400 | return MAY_LJMP(hlua_applet_htx_getline_yield(L, 0, 0)); |
| 4401 | else |
| 4402 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4403 | } |
| 4404 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4405 | /* If expected data not yet available, it returns a yield. This function |
| 4406 | * consumes the data in the buffer. It returns a string containing the |
| 4407 | * data. This string can be empty. |
| 4408 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4409 | __LJMP static int hlua_applet_htx_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4410 | { |
| 4411 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4412 | struct stream_interface *si = appctx->appctx->owner; |
| 4413 | struct channel *req = si_oc(si); |
| 4414 | struct htx *htx; |
| 4415 | struct htx_blk *blk; |
| 4416 | size_t count; |
| 4417 | int len; |
| 4418 | |
| 4419 | /* Maybe we cant send a 100-continue ? */ |
| 4420 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) |
| 4421 | MAY_LJMP(hlua_applet_htx_reply_100_continue(L)); |
| 4422 | |
| 4423 | htx = htx_from_buf(&req->buf); |
| 4424 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4425 | count = co_data(req); |
| 4426 | blk = htx_get_head_blk(htx); |
| 4427 | while (count && len && blk) { |
| 4428 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4429 | uint32_t sz = htx_get_blksz(blk); |
| 4430 | struct ist v; |
| 4431 | uint32_t vlen; |
| 4432 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4433 | if (type == HTX_BLK_EOM) { |
| 4434 | len = 0; |
| 4435 | break; |
| 4436 | } |
| 4437 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4438 | vlen = sz; |
| 4439 | if (len > 0 && vlen > len) |
| 4440 | vlen = len; |
| 4441 | if (vlen > count) { |
| 4442 | if (type != HTX_BLK_DATA) |
| 4443 | break; |
| 4444 | vlen = count; |
| 4445 | } |
| 4446 | |
| 4447 | switch (type) { |
| 4448 | case HTX_BLK_UNUSED: |
| 4449 | break; |
| 4450 | |
| 4451 | case HTX_BLK_DATA: |
| 4452 | v = htx_get_blk_value(htx, blk); |
| 4453 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4454 | break; |
| 4455 | |
| 4456 | case HTX_BLK_EOD: |
| 4457 | case HTX_BLK_TLR: |
| 4458 | case HTX_BLK_EOM: |
| 4459 | len = 0; |
| 4460 | break; |
| 4461 | |
| 4462 | default: |
| 4463 | break; |
| 4464 | } |
| 4465 | |
| 4466 | co_set_data(req, co_data(req) - vlen); |
| 4467 | count -= vlen; |
| 4468 | if (len > 0) |
| 4469 | len -= vlen; |
| 4470 | if (sz == vlen) |
| 4471 | blk = htx_remove_blk(htx, blk); |
| 4472 | else { |
| 4473 | htx_cut_data_blk(htx, blk, vlen); |
| 4474 | break; |
| 4475 | } |
| 4476 | } |
| 4477 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4478 | htx_to_buf(htx, &req->buf); |
| 4479 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4480 | /* If we are no other data available, yield waiting for new data. */ |
| 4481 | if (len) { |
| 4482 | if (len > 0) { |
| 4483 | lua_pushinteger(L, len); |
| 4484 | lua_replace(L, 2); |
| 4485 | } |
| 4486 | si_cant_get(si); |
| 4487 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_recv_yield, TICK_ETERNITY, 0)); |
| 4488 | } |
| 4489 | |
| 4490 | /* return the result. */ |
| 4491 | luaL_pushresult(&appctx->b); |
| 4492 | return 1; |
| 4493 | } |
| 4494 | |
| 4495 | /* If expected data not yet available, it returns a yield. This function |
| 4496 | * consumes the data in the buffer. It returns a string containing the |
| 4497 | * data. This string can be empty. |
| 4498 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4499 | __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] | 4500 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4501 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4502 | struct stream_interface *si = appctx->appctx->owner; |
| 4503 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4504 | struct channel *chn = si_ic(si); |
| 4505 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4506 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4507 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4508 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4509 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4510 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4511 | /* Maybe we cant send a 100-continue ? */ |
| 4512 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 4513 | ret = ci_putblk(chn, HTTP_100.ptr, HTTP_100.len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4514 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4515 | * big for the buffers. We cant send anything. So, we ignoring |
| 4516 | * the error, considers that the 100-continue is sent, and try |
| 4517 | * to receive. |
| 4518 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4519 | */ |
| 4520 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4521 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4522 | 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] | 4523 | } |
| 4524 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4525 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4526 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4527 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4528 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4529 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4530 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4531 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4532 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4533 | 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] | 4534 | } |
| 4535 | |
| 4536 | /* End of data: commit the total strings and return. */ |
| 4537 | if (ret < 0) { |
| 4538 | luaL_pushresult(&appctx->b); |
| 4539 | return 1; |
| 4540 | } |
| 4541 | |
| 4542 | /* Ensure that the block 2 length is usable. */ |
| 4543 | if (ret == 1) |
| 4544 | len2 = 0; |
| 4545 | |
| 4546 | /* Copy the fisrt block caping to the length required. */ |
| 4547 | if (len1 > len) |
| 4548 | len1 = len; |
| 4549 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4550 | len -= len1; |
| 4551 | |
| 4552 | /* Copy the second block. */ |
| 4553 | if (len2 > len) |
| 4554 | len2 = len; |
| 4555 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4556 | len -= len2; |
| 4557 | |
| 4558 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4559 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4560 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4561 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4562 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4563 | /* If we are no other data available, yield waiting for new data. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4564 | if (len > 0) { |
| 4565 | lua_pushinteger(L, len); |
| 4566 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 4567 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4568 | 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] | 4569 | } |
| 4570 | |
| 4571 | /* return the result. */ |
| 4572 | luaL_pushresult(&appctx->b); |
| 4573 | return 1; |
| 4574 | } |
| 4575 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4576 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4577 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4578 | { |
| 4579 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4580 | int len = -1; |
| 4581 | |
| 4582 | /* Check arguments. */ |
| 4583 | if (lua_gettop(L) > 2) |
| 4584 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4585 | if (lua_gettop(L) >= 2) { |
| 4586 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4587 | lua_pop(L, 1); |
| 4588 | } |
| 4589 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4590 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4591 | /* HTX version */ |
| 4592 | lua_pushinteger(L, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4593 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4594 | /* Initialise the string catenation. */ |
| 4595 | luaL_buffinit(L, &appctx->b); |
| 4596 | |
| 4597 | return MAY_LJMP(hlua_applet_htx_recv_yield(L, 0, 0)); |
| 4598 | } |
| 4599 | else { |
| 4600 | /* Legacy HTTP version */ |
| 4601 | /* Check the required length */ |
| 4602 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4603 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4604 | lua_pushinteger(L, len); |
| 4605 | |
| 4606 | /* Initialise the string catenation. */ |
| 4607 | luaL_buffinit(L, &appctx->b); |
| 4608 | |
| 4609 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4610 | } |
| 4611 | } |
| 4612 | |
| 4613 | /* Append data in the output side of the buffer. This data is immediately |
| 4614 | * sent. The function returns the amount of data written. If the buffer |
| 4615 | * cannot contain the data, the function yields. The function returns -1 |
| 4616 | * if the channel is closed. |
| 4617 | */ |
| 4618 | __LJMP static int hlua_applet_htx_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4619 | { |
| 4620 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4621 | struct stream_interface *si = appctx->appctx->owner; |
| 4622 | struct channel *res = si_ic(si); |
| 4623 | struct htx *htx = htx_from_buf(&res->buf); |
| 4624 | const char *data; |
| 4625 | size_t len; |
| 4626 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4627 | int max; |
| 4628 | |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4629 | max = channel_htx_recv_max(res, htx); |
| 4630 | if (!max) |
| 4631 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4632 | |
| 4633 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4634 | |
| 4635 | /* Get the max amount of data which can write as input in the channel. */ |
| 4636 | if (max > (len - l)) |
| 4637 | max = len - l; |
| 4638 | |
| 4639 | /* Copy data. */ |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4640 | if (!htx_add_data(htx, ist2(data + l, max))) |
| 4641 | goto snd_yield; |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4642 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4643 | |
| 4644 | /* update counters. */ |
| 4645 | l += max; |
| 4646 | lua_pop(L, 1); |
| 4647 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4648 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4649 | /* If some data is not send, declares the situation to the |
| 4650 | * applet, and returns a yield. |
| 4651 | */ |
| 4652 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4653 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4654 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4655 | si_rx_room_blk(si); |
| 4656 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_send_yield, TICK_ETERNITY, 0)); |
| 4657 | } |
| 4658 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4659 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4660 | return 1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4661 | } |
| 4662 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4663 | /* Append data in the output side of the buffer. This data is immediately |
| 4664 | * sent. The function returns the amount of data written. If the buffer |
| 4665 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4666 | * if the channel is closed. |
| 4667 | */ |
| 4668 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4669 | { |
| 4670 | size_t len; |
| 4671 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4672 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4673 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4674 | struct stream_interface *si = appctx->appctx->owner; |
| 4675 | struct channel *chn = si_ic(si); |
| 4676 | int max; |
| 4677 | |
| 4678 | /* Get the max amount of data which can write as input in the channel. */ |
| 4679 | max = channel_recv_max(chn); |
| 4680 | if (max > (len - l)) |
| 4681 | max = len - l; |
| 4682 | |
| 4683 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4684 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4685 | |
| 4686 | /* update counters. */ |
| 4687 | l += max; |
| 4688 | lua_pop(L, 1); |
| 4689 | lua_pushinteger(L, l); |
| 4690 | |
| 4691 | /* If some data is not send, declares the situation to the |
| 4692 | * applet, and returns a yield. |
| 4693 | */ |
| 4694 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 4695 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4696 | 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] | 4697 | } |
| 4698 | |
| 4699 | return 1; |
| 4700 | } |
| 4701 | |
| 4702 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4703 | * yield the LUA process, and resume it without checking the |
| 4704 | * input arguments. |
| 4705 | */ |
| 4706 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4707 | { |
| 4708 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4709 | |
| 4710 | /* We want to send some data. Headers must be sent. */ |
| 4711 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4712 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4713 | WILL_LJMP(lua_error(L)); |
| 4714 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4715 | |
| 4716 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) { |
| 4717 | /* HTX version */ |
| 4718 | /* This interger is used for followinf the amount of data sent. */ |
| 4719 | lua_pushinteger(L, 0); |
| 4720 | |
| 4721 | return MAY_LJMP(hlua_applet_htx_send_yield(L, 0, 0)); |
| 4722 | } |
| 4723 | else { |
| 4724 | /* Legacy HTTP version */ |
| 4725 | size_t len; |
| 4726 | char hex[10]; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4727 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4728 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4729 | |
| 4730 | /* If transfer encoding chunked is selected, we surround the data |
| 4731 | * by chunk data. |
| 4732 | */ |
| 4733 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4734 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4735 | lua_pushfstring(L, "%s\r\n", hex); |
| 4736 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4737 | lua_pushstring(L, "\r\n"); |
| 4738 | lua_concat(L, 3); |
| 4739 | } |
| 4740 | |
| 4741 | /* This interger is used for followinf the amount of data sent. */ |
| 4742 | lua_pushinteger(L, 0); |
| 4743 | |
| 4744 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4745 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4746 | } |
| 4747 | |
| 4748 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4749 | { |
| 4750 | const char *name; |
| 4751 | int ret; |
| 4752 | |
| 4753 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4754 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4755 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4756 | |
| 4757 | /* Push in the stack the "response" entry. */ |
| 4758 | ret = lua_getfield(L, 1, "response"); |
| 4759 | if (ret != LUA_TTABLE) { |
| 4760 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4761 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4762 | WILL_LJMP(lua_error(L)); |
| 4763 | } |
| 4764 | |
| 4765 | /* check if the header is already registered if it is not |
| 4766 | * the case, register it. |
| 4767 | */ |
| 4768 | ret = lua_getfield(L, -1, name); |
| 4769 | if (ret == LUA_TNIL) { |
| 4770 | |
| 4771 | /* Entry not found. */ |
| 4772 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4773 | |
| 4774 | /* Insert the new header name in the array in the top of the stack. |
| 4775 | * It left the new array in the top of the stack. |
| 4776 | */ |
| 4777 | lua_newtable(L); |
| 4778 | lua_pushvalue(L, 2); |
| 4779 | lua_pushvalue(L, -2); |
| 4780 | lua_settable(L, -4); |
| 4781 | |
| 4782 | } else if (ret != LUA_TTABLE) { |
| 4783 | |
| 4784 | /* corruption error. */ |
| 4785 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4786 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4787 | WILL_LJMP(lua_error(L)); |
| 4788 | } |
| 4789 | |
| 4790 | /* Now the top od thestack is an array of values. We push |
| 4791 | * the header value as new entry. |
| 4792 | */ |
| 4793 | lua_pushvalue(L, 3); |
| 4794 | ret = lua_rawlen(L, -2); |
| 4795 | lua_rawseti(L, -2, ret + 1); |
| 4796 | lua_pushboolean(L, 1); |
| 4797 | return 1; |
| 4798 | } |
| 4799 | |
| 4800 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4801 | { |
| 4802 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4803 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4804 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4805 | |
| 4806 | if (status < 100 || status > 599) { |
| 4807 | lua_pushboolean(L, 0); |
| 4808 | return 1; |
| 4809 | } |
| 4810 | |
| 4811 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4812 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4813 | lua_pushboolean(L, 1); |
| 4814 | return 1; |
| 4815 | } |
| 4816 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4817 | |
| 4818 | __LJMP static int hlua_applet_htx_send_response(lua_State *L) |
| 4819 | { |
| 4820 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4821 | struct stream_interface *si = appctx->appctx->owner; |
| 4822 | struct channel *res = si_ic(si); |
| 4823 | struct htx *htx; |
| 4824 | struct htx_sl *sl; |
| 4825 | struct h1m h1m; |
| 4826 | const char *status, *reason; |
| 4827 | const char *name, *value; |
| 4828 | size_t nlen, vlen; |
| 4829 | unsigned int flags; |
| 4830 | |
| 4831 | /* Send the message at once. */ |
| 4832 | htx = htx_from_buf(&res->buf); |
| 4833 | h1m_init_res(&h1m); |
| 4834 | |
| 4835 | /* Use the same http version than the request. */ |
| 4836 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4837 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4838 | if (reason == NULL) |
| 4839 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4840 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4841 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4842 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4843 | } |
| 4844 | else { |
| 4845 | flags = HTX_SL_F_IS_RESP; |
| 4846 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4847 | } |
| 4848 | if (!sl) { |
| 4849 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4850 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4851 | WILL_LJMP(lua_error(L)); |
| 4852 | } |
| 4853 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4854 | |
| 4855 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4856 | lua_pushvalue(L, 0); |
| 4857 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4858 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4859 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4860 | WILL_LJMP(lua_error(L)); |
| 4861 | } |
| 4862 | |
| 4863 | /* Browse the list of headers. */ |
| 4864 | lua_pushnil(L); |
| 4865 | while(lua_next(L, -2) != 0) { |
| 4866 | /* We expect a string as -2. */ |
| 4867 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4868 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4869 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4870 | lua_typename(L, lua_type(L, -2))); |
| 4871 | WILL_LJMP(lua_error(L)); |
| 4872 | } |
| 4873 | name = lua_tolstring(L, -2, &nlen); |
| 4874 | |
| 4875 | /* We expect an array as -1. */ |
| 4876 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4877 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4878 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4879 | name, |
| 4880 | lua_typename(L, lua_type(L, -1))); |
| 4881 | WILL_LJMP(lua_error(L)); |
| 4882 | } |
| 4883 | |
| 4884 | /* Browse the table who is on the top of the stack. */ |
| 4885 | lua_pushnil(L); |
| 4886 | while(lua_next(L, -2) != 0) { |
| 4887 | int id; |
| 4888 | |
| 4889 | /* We expect a number as -2. */ |
| 4890 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4891 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4892 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4893 | name, |
| 4894 | lua_typename(L, lua_type(L, -2))); |
| 4895 | WILL_LJMP(lua_error(L)); |
| 4896 | } |
| 4897 | id = lua_tointeger(L, -2); |
| 4898 | |
| 4899 | /* We expect a string as -2. */ |
| 4900 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4901 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4902 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4903 | name, id, |
| 4904 | lua_typename(L, lua_type(L, -1))); |
| 4905 | WILL_LJMP(lua_error(L)); |
| 4906 | } |
| 4907 | value = lua_tolstring(L, -1, &vlen); |
| 4908 | |
| 4909 | /* Simple Protocol checks. */ |
| 4910 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
| 4911 | h1_parse_xfer_enc_header(&h1m, ist2(name, nlen)); |
| 4912 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4913 | struct ist v = ist2(value, vlen); |
| 4914 | int ret; |
| 4915 | |
| 4916 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4917 | if (ret < 0) { |
| 4918 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4919 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4920 | name); |
| 4921 | WILL_LJMP(lua_error(L)); |
| 4922 | } |
| 4923 | else if (ret == 0) |
| 4924 | goto next; /* Skip it */ |
| 4925 | } |
| 4926 | |
| 4927 | /* Add a new header */ |
| 4928 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4929 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4930 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4931 | name); |
| 4932 | WILL_LJMP(lua_error(L)); |
| 4933 | } |
| 4934 | next: |
| 4935 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4936 | lua_pop(L, 1); |
| 4937 | } |
| 4938 | |
| 4939 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4940 | lua_pop(L, 1); |
| 4941 | } |
| 4942 | |
| 4943 | if (h1m.flags & H1_MF_CHNK) |
| 4944 | h1m.flags &= ~H1_MF_CLEN; |
| 4945 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4946 | h1m.flags |= H1_MF_XFER_LEN; |
| 4947 | |
| 4948 | /* Uset HTX start-line flags */ |
| 4949 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4950 | flags |= HTX_SL_F_XFER_ENC; |
| 4951 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4952 | flags |= HTX_SL_F_XFER_LEN; |
| 4953 | if (h1m.flags & H1_MF_CHNK) |
| 4954 | flags |= HTX_SL_F_CHNK; |
| 4955 | else if (h1m.flags & H1_MF_CLEN) |
| 4956 | flags |= HTX_SL_F_CLEN; |
| 4957 | if (h1m.body_len == 0) |
| 4958 | flags |= HTX_SL_F_BODYLESS; |
| 4959 | } |
| 4960 | sl->flags |= flags; |
| 4961 | |
| 4962 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4963 | * and the status code implies the presence of a message body, we must |
| 4964 | * announce a transfer encoding chunked. This is required by haproxy |
| 4965 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4966 | * chunked itslef, don't do anything. |
| 4967 | */ |
| 4968 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4969 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4970 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4971 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4972 | /* Add a new header */ |
| 4973 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4974 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4975 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4976 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4977 | WILL_LJMP(lua_error(L)); |
| 4978 | } |
| 4979 | } |
| 4980 | |
| 4981 | /* Finalize headers. */ |
| 4982 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4983 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4984 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4985 | WILL_LJMP(lua_error(L)); |
| 4986 | } |
| 4987 | |
| 4988 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4989 | b_reset(&res->buf); |
| 4990 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4991 | WILL_LJMP(lua_error(L)); |
| 4992 | } |
| 4993 | |
| 4994 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4995 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4996 | |
| 4997 | /* Headers sent, set the flag. */ |
| 4998 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4999 | return 0; |
| 5000 | |
| 5001 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5002 | /* We will build the status line and the headers of the HTTP response. |
| 5003 | * We will try send at once if its not possible, we give back the hand |
| 5004 | * waiting for more room. |
| 5005 | */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5006 | __LJMP static int hlua_applet_htx_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 5007 | { |
| 5008 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5009 | struct stream_interface *si = appctx->appctx->owner; |
| 5010 | struct channel *res = si_ic(si); |
| 5011 | |
| 5012 | if (co_data(res)) { |
| 5013 | si_rx_room_blk(si); |
| 5014 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_htx_start_response_yield, TICK_ETERNITY, 0)); |
| 5015 | } |
| 5016 | return MAY_LJMP(hlua_applet_htx_send_response(L)); |
| 5017 | } |
| 5018 | |
| 5019 | |
| 5020 | __LJMP static int hlua_applet_htx_start_response(lua_State *L) |
| 5021 | { |
| 5022 | return MAY_LJMP(hlua_applet_htx_start_response_yield(L, 0, 0)); |
| 5023 | } |
| 5024 | |
| 5025 | /* We will build the status line and the headers of the HTTP response. |
| 5026 | * We will try send at once if its not possible, we give back the hand |
| 5027 | * waiting for more room. |
| 5028 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5029 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 5030 | { |
| 5031 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5032 | struct stream_interface *si = appctx->appctx->owner; |
| 5033 | struct channel *chn = si_ic(si); |
| 5034 | int ret; |
| 5035 | size_t len; |
| 5036 | const char *msg; |
| 5037 | |
| 5038 | /* Get the message as the first argument on the stack. */ |
| 5039 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5040 | |
| 5041 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 5042 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5043 | |
| 5044 | /* if ret == -2 or -3 the channel closed or the message si too |
| 5045 | * big for the buffers. |
| 5046 | */ |
| 5047 | if (ret == -2 || ret == -3) { |
| 5048 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5049 | WILL_LJMP(lua_error(L)); |
| 5050 | } |
| 5051 | |
| 5052 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 5053 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 5054 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5055 | 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] | 5056 | } |
| 5057 | |
| 5058 | /* Headers sent, set the flag. */ |
| 5059 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 5060 | return 0; |
| 5061 | } |
| 5062 | |
| 5063 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 5064 | { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5065 | struct buffer *tmp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5066 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5067 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5068 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5069 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5070 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5071 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5072 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5073 | int hdr_chunked = 0; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5074 | const char *reason; |
| 5075 | |
| 5076 | if (IS_HTX_STRM(si_strm(appctx->appctx->owner))) |
| 5077 | return MAY_LJMP(hlua_applet_htx_start_response(L)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5078 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5079 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5080 | if (reason == NULL) |
Willy Tarreau | 04f1e2d | 2018-09-10 18:04:24 +0200 | [diff] [blame] | 5081 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5082 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5083 | tmp = get_trash_chunk(); |
| 5084 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5085 | /* Use the same http version than the request. */ |
| 5086 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 5087 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5088 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5089 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5090 | |
| 5091 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 5092 | lua_pushvalue(L, 0); |
| 5093 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5094 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 5095 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 5096 | WILL_LJMP(lua_error(L)); |
| 5097 | } |
| 5098 | |
| 5099 | /* Browse the list of headers. */ |
| 5100 | lua_pushnil(L); |
| 5101 | while(lua_next(L, -2) != 0) { |
| 5102 | |
| 5103 | /* We expect a string as -2. */ |
| 5104 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5105 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 5106 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5107 | lua_typename(L, lua_type(L, -2))); |
| 5108 | WILL_LJMP(lua_error(L)); |
| 5109 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5110 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5111 | |
| 5112 | /* We expect an array as -1. */ |
| 5113 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5114 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 5115 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5116 | name, |
| 5117 | lua_typename(L, lua_type(L, -1))); |
| 5118 | WILL_LJMP(lua_error(L)); |
| 5119 | } |
| 5120 | |
| 5121 | /* Browse the table who is on the top of the stack. */ |
| 5122 | lua_pushnil(L); |
| 5123 | while(lua_next(L, -2) != 0) { |
| 5124 | |
| 5125 | /* We expect a number as -2. */ |
| 5126 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5127 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 5128 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5129 | name, |
| 5130 | lua_typename(L, lua_type(L, -2))); |
| 5131 | WILL_LJMP(lua_error(L)); |
| 5132 | } |
| 5133 | id = lua_tointeger(L, -2); |
| 5134 | |
| 5135 | /* We expect a string as -2. */ |
| 5136 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5137 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 5138 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 5139 | name, id, |
| 5140 | lua_typename(L, lua_type(L, -1))); |
| 5141 | WILL_LJMP(lua_error(L)); |
| 5142 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5143 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5144 | |
| 5145 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5146 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 5147 | memcpy(tmp->area + tmp->data, name, name_len); |
| 5148 | tmp->data += name_len; |
| 5149 | tmp->area[tmp->data++] = ':'; |
| 5150 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5151 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5152 | memcpy(tmp->area + tmp->data, value, |
| 5153 | value_len); |
| 5154 | tmp->data += value_len; |
| 5155 | tmp->area[tmp->data++] = '\r'; |
| 5156 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5157 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5158 | |
| 5159 | /* Protocol checks. */ |
| 5160 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5161 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5162 | * is done without control. If it contains a bad value, |
| 5163 | * the content-length remains negative so that we can |
| 5164 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5165 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5166 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5167 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5168 | |
| 5169 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 5170 | if (name_len == 17 && value_len == 7 && |
| 5171 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5172 | strcasecmp("chunked", value) == 0) |
| 5173 | hdr_chunked = 1; |
| 5174 | |
| 5175 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5176 | lua_pop(L, 1); |
| 5177 | } |
| 5178 | |
| 5179 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5180 | lua_pop(L, 1); |
| 5181 | } |
| 5182 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5183 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 5184 | * and the status code implies the presence of a message body, we must |
| 5185 | * announce a transfer encoding chunked. This is required by haproxy |
| 5186 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 5187 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5188 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 5189 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 5190 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 5191 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 5192 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 5193 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5194 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 5195 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 5196 | } |
| 5197 | |
| 5198 | /* Finalize headers. */ |
| 5199 | chunk_appendf(tmp, "\r\n"); |
| 5200 | |
| 5201 | /* Remove the last entry and the array of headers */ |
| 5202 | lua_pop(L, 2); |
| 5203 | |
| 5204 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5205 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5206 | |
| 5207 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 5208 | } |
| 5209 | |
| 5210 | /* |
| 5211 | * |
| 5212 | * |
| 5213 | * Class HTTP |
| 5214 | * |
| 5215 | * |
| 5216 | */ |
| 5217 | |
| 5218 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5219 | * a class stream, otherwise it throws an error. |
| 5220 | */ |
| 5221 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 5222 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5223 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5224 | } |
| 5225 | |
| 5226 | /* This function creates and push in the stack a HTTP object |
| 5227 | * according with a current TXN. |
| 5228 | */ |
| 5229 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5230 | { |
| 5231 | struct hlua_txn *htxn; |
| 5232 | |
| 5233 | /* Check stack size. */ |
| 5234 | if (!lua_checkstack(L, 3)) |
| 5235 | return 0; |
| 5236 | |
| 5237 | /* Create the object: obj[0] = userdata. |
| 5238 | * Note that the base of the Converters object is the |
| 5239 | * same than the TXN object. |
| 5240 | */ |
| 5241 | lua_newtable(L); |
| 5242 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5243 | lua_rawseti(L, -2, 0); |
| 5244 | |
| 5245 | htxn->s = txn->s; |
| 5246 | htxn->p = txn->p; |
| 5247 | |
| 5248 | /* Pop a class stream metatable and affect it to the table. */ |
| 5249 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5250 | lua_setmetatable(L, -2); |
| 5251 | |
| 5252 | return 1; |
| 5253 | } |
| 5254 | |
| 5255 | /* This function creates ans returns an array of HTTP headers. |
| 5256 | * This function does not fails. It is used as wrapper with the |
| 5257 | * 2 following functions. |
| 5258 | */ |
| 5259 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5260 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5261 | /* Create the table. */ |
| 5262 | lua_newtable(L); |
| 5263 | |
| 5264 | if (!htxn->s->txn) |
| 5265 | return 1; |
| 5266 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5267 | if (IS_HTX_STRM(htxn->s)) { |
| 5268 | /* HTX version */ |
| 5269 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5270 | int32_t pos; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5271 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5272 | for (pos = htx_get_head(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5273 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5274 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5275 | struct ist n, v; |
| 5276 | int len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5277 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5278 | if (type == HTX_BLK_HDR) { |
| 5279 | n = htx_get_blk_name(htx,blk); |
| 5280 | v = htx_get_blk_value(htx, blk); |
| 5281 | } |
| 5282 | else if (type == HTX_BLK_EOH) |
| 5283 | break; |
| 5284 | else |
| 5285 | continue; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5286 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5287 | /* Check for existing entry: |
| 5288 | * assume that the table is on the top of the stack, and |
| 5289 | * push the key in the stack, the function lua_gettable() |
| 5290 | * perform the lookup. |
| 5291 | */ |
| 5292 | lua_pushlstring(L, n.ptr, n.len); |
| 5293 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5294 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5295 | switch (lua_type(L, -1)) { |
| 5296 | case LUA_TNIL: |
| 5297 | /* Table not found, create it. */ |
| 5298 | lua_pop(L, 1); /* remove the nil value. */ |
| 5299 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5300 | lua_newtable(L); /* create and push empty table. */ |
| 5301 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5302 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5303 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5304 | break; |
| 5305 | |
| 5306 | case LUA_TTABLE: |
| 5307 | /* Entry found: push the value in the table. */ |
| 5308 | len = lua_rawlen(L, -1); |
| 5309 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5310 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5311 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5312 | break; |
| 5313 | |
| 5314 | default: |
| 5315 | /* Other cases are errors. */ |
| 5316 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5317 | WILL_LJMP(lua_error(L)); |
| 5318 | } |
| 5319 | } |
| 5320 | } |
| 5321 | else { |
| 5322 | /* Legacy HTTP version */ |
| 5323 | const char *cur_ptr, *cur_next, *p; |
| 5324 | int old_idx, cur_idx; |
| 5325 | struct hdr_idx_elem *cur_hdr; |
| 5326 | const char *hn, *hv; |
| 5327 | int hnl, hvl; |
| 5328 | const char *in; |
| 5329 | char *out; |
| 5330 | int len; |
| 5331 | |
| 5332 | /* Build array of headers. */ |
| 5333 | old_idx = 0; |
| 5334 | cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 5335 | |
| 5336 | while (1) { |
| 5337 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 5338 | if (!cur_idx) |
| 5339 | break; |
| 5340 | old_idx = cur_idx; |
| 5341 | |
| 5342 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 5343 | cur_ptr = cur_next; |
| 5344 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 5345 | |
| 5346 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 5347 | * and the next header starts at cur_next. We'll check |
| 5348 | * this header in the list as well as against the default |
| 5349 | * rule. |
| 5350 | */ |
| 5351 | |
| 5352 | /* look for ': *'. */ |
| 5353 | hn = cur_ptr; |
| 5354 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 5355 | if (p >= cur_ptr+cur_hdr->len) |
| 5356 | continue; |
| 5357 | hnl = p - hn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5358 | p++; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5359 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 5360 | p++; |
| 5361 | if (p >= cur_ptr+cur_hdr->len) |
| 5362 | continue; |
| 5363 | hv = p; |
| 5364 | hvl = cur_ptr+cur_hdr->len-p; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5365 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5366 | /* Lowercase the key. Don't check the size of trash, it have |
| 5367 | * the size of one buffer and the input data contains in one |
| 5368 | * buffer. |
| 5369 | */ |
| 5370 | out = trash.area; |
| 5371 | for (in=hn; in<hn+hnl; in++, out++) |
| 5372 | *out = tolower(*in); |
| 5373 | *out = '\0'; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5374 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5375 | /* Check for existing entry: |
| 5376 | * assume that the table is on the top of the stack, and |
| 5377 | * push the key in the stack, the function lua_gettable() |
| 5378 | * perform the lookup. |
| 5379 | */ |
| 5380 | lua_pushlstring(L, trash.area, hnl); |
| 5381 | lua_gettable(L, -2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5382 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5383 | switch (lua_type(L, -1)) { |
| 5384 | case LUA_TNIL: |
| 5385 | /* Table not found, create it. */ |
| 5386 | lua_pop(L, 1); /* remove the nil value. */ |
| 5387 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
| 5388 | lua_newtable(L); /* create and push empty table. */ |
| 5389 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5390 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5391 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 5392 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5393 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5394 | case LUA_TTABLE: |
| 5395 | /* Entry found: push the value in the table. */ |
| 5396 | len = lua_rawlen(L, -1); |
| 5397 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 5398 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5399 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5400 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5401 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5402 | default: |
| 5403 | /* Other cases are errors. */ |
| 5404 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5405 | WILL_LJMP(lua_error(L)); |
| 5406 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5407 | } |
| 5408 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5409 | return 1; |
| 5410 | } |
| 5411 | |
| 5412 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5413 | { |
| 5414 | struct hlua_txn *htxn; |
| 5415 | |
| 5416 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5417 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5418 | |
| 5419 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 5420 | } |
| 5421 | |
| 5422 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5423 | { |
| 5424 | struct hlua_txn *htxn; |
| 5425 | |
| 5426 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5427 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5428 | |
| 5429 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 5430 | } |
| 5431 | |
| 5432 | /* This function replace full header, or just a value in |
| 5433 | * the request or in the response. It is a wrapper fir the |
| 5434 | * 4 following functions. |
| 5435 | */ |
| 5436 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 5437 | struct http_msg *msg, int action) |
| 5438 | { |
| 5439 | size_t name_len; |
| 5440 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5441 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5442 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 5443 | struct my_regex re; |
| 5444 | |
| 5445 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 5446 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5447 | |
| 5448 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 5449 | regex_free(&re); |
| 5450 | return 0; |
| 5451 | } |
| 5452 | |
| 5453 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5454 | { |
| 5455 | struct hlua_txn *htxn; |
| 5456 | |
| 5457 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5458 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5459 | |
| 5460 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 5461 | } |
| 5462 | |
| 5463 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5464 | { |
| 5465 | struct hlua_txn *htxn; |
| 5466 | |
| 5467 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5468 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5469 | |
| 5470 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 5471 | } |
| 5472 | |
| 5473 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5474 | { |
| 5475 | struct hlua_txn *htxn; |
| 5476 | |
| 5477 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5478 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5479 | |
| 5480 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 5481 | } |
| 5482 | |
| 5483 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5484 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5485 | struct hlua_txn *htxn; |
| 5486 | |
| 5487 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5488 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5489 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 5490 | 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] | 5491 | } |
| 5492 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5493 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5494 | * It is a wrapper for the 2 following functions. |
| 5495 | */ |
| 5496 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5497 | { |
| 5498 | size_t len; |
| 5499 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5500 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5501 | if (IS_HTX_STRM(htxn->s)) { |
| 5502 | /* HTX version */ |
| 5503 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5504 | struct http_hdr_ctx ctx; |
| 5505 | |
| 5506 | ctx.blk = NULL; |
| 5507 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5508 | http_remove_header(htx, &ctx); |
| 5509 | } |
| 5510 | else { |
| 5511 | /* Legacy HTTP version */ |
| 5512 | struct hdr_ctx ctx; |
| 5513 | struct http_txn *txn = htxn->s->txn; |
| 5514 | |
| 5515 | ctx.idx = 0; |
| 5516 | while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) |
| 5517 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 5518 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5519 | return 0; |
| 5520 | } |
| 5521 | |
| 5522 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5523 | { |
| 5524 | struct hlua_txn *htxn; |
| 5525 | |
| 5526 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5527 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5528 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5529 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5530 | } |
| 5531 | |
| 5532 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5533 | { |
| 5534 | struct hlua_txn *htxn; |
| 5535 | |
| 5536 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5537 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5538 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5539 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5540 | } |
| 5541 | |
| 5542 | /* This function adds an header. It is a wrapper used by |
| 5543 | * the 2 following functions. |
| 5544 | */ |
| 5545 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 5546 | { |
| 5547 | size_t name_len; |
| 5548 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5549 | size_t value_len; |
| 5550 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 5551 | char *p; |
| 5552 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5553 | if (IS_HTX_STRM(htxn->s)) { |
| 5554 | /* HTX version */ |
| 5555 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5556 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5557 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5558 | ist2(value, value_len))); |
| 5559 | } |
| 5560 | else { |
| 5561 | /* Legacy HTTP version */ |
| 5562 | /* Check length. */ |
| 5563 | trash.data = value_len + name_len + 2; |
| 5564 | if (trash.data > trash.size) |
| 5565 | return 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5566 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5567 | /* Creates the header string. */ |
| 5568 | p = trash.area; |
| 5569 | memcpy(p, name, name_len); |
| 5570 | p += name_len; |
| 5571 | *p = ':'; |
| 5572 | p++; |
| 5573 | *p = ' '; |
| 5574 | p++; |
| 5575 | memcpy(p, value, value_len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5576 | |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5577 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
| 5578 | trash.area, trash.data) != 0); |
| 5579 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5580 | return 0; |
| 5581 | } |
| 5582 | |
| 5583 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5584 | { |
| 5585 | struct hlua_txn *htxn; |
| 5586 | |
| 5587 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5588 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5589 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5590 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5591 | } |
| 5592 | |
| 5593 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5594 | { |
| 5595 | struct hlua_txn *htxn; |
| 5596 | |
| 5597 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5598 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5599 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5600 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5601 | } |
| 5602 | |
| 5603 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5604 | { |
| 5605 | struct hlua_txn *htxn; |
| 5606 | |
| 5607 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5608 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5609 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5610 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 5611 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5612 | } |
| 5613 | |
| 5614 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5615 | { |
| 5616 | struct hlua_txn *htxn; |
| 5617 | |
| 5618 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5619 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5620 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 5621 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 5622 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5623 | } |
| 5624 | |
| 5625 | /* This function set the method. */ |
| 5626 | static int hlua_http_req_set_meth(lua_State *L) |
| 5627 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5628 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5629 | size_t name_len; |
| 5630 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5631 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5632 | 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] | 5633 | return 1; |
| 5634 | } |
| 5635 | |
| 5636 | /* This function set the method. */ |
| 5637 | static int hlua_http_req_set_path(lua_State *L) |
| 5638 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5639 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5640 | size_t name_len; |
| 5641 | 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] | 5642 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5643 | 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] | 5644 | return 1; |
| 5645 | } |
| 5646 | |
| 5647 | /* This function set the query-string. */ |
| 5648 | static int hlua_http_req_set_query(lua_State *L) |
| 5649 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5650 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5651 | size_t name_len; |
| 5652 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5653 | |
| 5654 | /* Check length. */ |
| 5655 | if (name_len > trash.size - 1) { |
| 5656 | lua_pushboolean(L, 0); |
| 5657 | return 1; |
| 5658 | } |
| 5659 | |
| 5660 | /* Add the mark question as prefix. */ |
| 5661 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5662 | trash.area[trash.data++] = '?'; |
| 5663 | memcpy(trash.area + trash.data, name, name_len); |
| 5664 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5665 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5666 | lua_pushboolean(L, |
| 5667 | 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] | 5668 | return 1; |
| 5669 | } |
| 5670 | |
| 5671 | /* This function set the uri. */ |
| 5672 | static int hlua_http_req_set_uri(lua_State *L) |
| 5673 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5674 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5675 | size_t name_len; |
| 5676 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5677 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5678 | 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] | 5679 | return 1; |
| 5680 | } |
| 5681 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5682 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5683 | static int hlua_http_res_set_status(lua_State *L) |
| 5684 | { |
| 5685 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5686 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5687 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5688 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5689 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5690 | return 0; |
| 5691 | } |
| 5692 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5693 | /* |
| 5694 | * |
| 5695 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5696 | * Class TXN |
| 5697 | * |
| 5698 | * |
| 5699 | */ |
| 5700 | |
| 5701 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5702 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5703 | */ |
| 5704 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5705 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5706 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5707 | } |
| 5708 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5709 | __LJMP static int hlua_set_var(lua_State *L) |
| 5710 | { |
| 5711 | struct hlua_txn *htxn; |
| 5712 | const char *name; |
| 5713 | size_t len; |
| 5714 | struct sample smp; |
| 5715 | |
| 5716 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5717 | |
| 5718 | /* It is useles to retrieve the stream, but this function |
| 5719 | * runs only in a stream context. |
| 5720 | */ |
| 5721 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5722 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5723 | |
| 5724 | /* Converts the third argument in a sample. */ |
| 5725 | hlua_lua2smp(L, 3, &smp); |
| 5726 | |
| 5727 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5728 | 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] | 5729 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5730 | return 0; |
| 5731 | } |
| 5732 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5733 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5734 | { |
| 5735 | struct hlua_txn *htxn; |
| 5736 | const char *name; |
| 5737 | size_t len; |
| 5738 | struct sample smp; |
| 5739 | |
| 5740 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5741 | |
| 5742 | /* It is useles to retrieve the stream, but this function |
| 5743 | * runs only in a stream context. |
| 5744 | */ |
| 5745 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5746 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5747 | |
| 5748 | /* Unset the variable. */ |
| 5749 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5750 | vars_unset_by_name(name, len, &smp); |
| 5751 | return 0; |
| 5752 | } |
| 5753 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5754 | __LJMP static int hlua_get_var(lua_State *L) |
| 5755 | { |
| 5756 | struct hlua_txn *htxn; |
| 5757 | const char *name; |
| 5758 | size_t len; |
| 5759 | struct sample smp; |
| 5760 | |
| 5761 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5762 | |
| 5763 | /* It is useles to retrieve the stream, but this function |
| 5764 | * runs only in a stream context. |
| 5765 | */ |
| 5766 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5767 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5768 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5769 | 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] | 5770 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5771 | lua_pushnil(L); |
| 5772 | return 1; |
| 5773 | } |
| 5774 | |
| 5775 | return hlua_smp2lua(L, &smp); |
| 5776 | } |
| 5777 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5778 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5779 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5780 | struct hlua *hlua; |
| 5781 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5782 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5783 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5784 | /* It is useles to retrieve the stream, but this function |
| 5785 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5786 | */ |
| 5787 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5788 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5789 | |
| 5790 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5791 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5792 | |
| 5793 | /* Get and store new value. */ |
| 5794 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5795 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5796 | |
| 5797 | return 0; |
| 5798 | } |
| 5799 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5800 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5801 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5802 | struct hlua *hlua; |
| 5803 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5804 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5805 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5806 | /* It is useles to retrieve the stream, but this function |
| 5807 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5808 | */ |
| 5809 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5810 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5811 | |
| 5812 | /* Push configuration index in the stack. */ |
| 5813 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5814 | |
| 5815 | return 1; |
| 5816 | } |
| 5817 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5818 | /* Create stack entry containing a class TXN. This function |
| 5819 | * return 0 if the stack does not contains free slots, |
| 5820 | * otherwise it returns 1. |
| 5821 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5822 | 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] | 5823 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5824 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5825 | |
| 5826 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5827 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5828 | return 0; |
| 5829 | |
| 5830 | /* NOTE: The allocation never fails. The failure |
| 5831 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5832 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5833 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5834 | /* Create the object: obj[0] = userdata. */ |
| 5835 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5836 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5837 | lua_rawseti(L, -2, 0); |
| 5838 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5839 | htxn->s = s; |
| 5840 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5841 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5842 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5843 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5844 | /* Create the "f" field that contains a list of fetches. */ |
| 5845 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5846 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5847 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5848 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5849 | |
| 5850 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5851 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5852 | 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] | 5853 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5854 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5855 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5856 | /* Create the "c" field that contains a list of converters. */ |
| 5857 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5858 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5859 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5860 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5861 | |
| 5862 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5863 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5864 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5865 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5866 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5867 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5868 | /* Create the "req" field that contains the request channel object. */ |
| 5869 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5870 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5871 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5872 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5873 | |
| 5874 | /* Create the "res" field that contains the response channel object. */ |
| 5875 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5876 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5877 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5878 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5879 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5880 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5881 | lua_pushstring(L, "http"); |
| 5882 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5883 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5884 | return 0; |
| 5885 | } |
| 5886 | else |
| 5887 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5888 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5889 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5890 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5891 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5892 | lua_setmetatable(L, -2); |
| 5893 | |
| 5894 | return 1; |
| 5895 | } |
| 5896 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5897 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5898 | { |
| 5899 | const char *msg; |
| 5900 | struct hlua_txn *htxn; |
| 5901 | |
| 5902 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5903 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5904 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5905 | |
| 5906 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5907 | return 0; |
| 5908 | } |
| 5909 | |
| 5910 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5911 | { |
| 5912 | int level; |
| 5913 | const char *msg; |
| 5914 | struct hlua_txn *htxn; |
| 5915 | |
| 5916 | MAY_LJMP(check_args(L, 3, "log")); |
| 5917 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5918 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5919 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5920 | |
| 5921 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5922 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5923 | |
| 5924 | hlua_sendlog(htxn->s->be, level, msg); |
| 5925 | return 0; |
| 5926 | } |
| 5927 | |
| 5928 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5929 | { |
| 5930 | const char *msg; |
| 5931 | struct hlua_txn *htxn; |
| 5932 | |
| 5933 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5934 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5935 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5936 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5937 | return 0; |
| 5938 | } |
| 5939 | |
| 5940 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5941 | { |
| 5942 | const char *msg; |
| 5943 | struct hlua_txn *htxn; |
| 5944 | |
| 5945 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5946 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5947 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5948 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5949 | return 0; |
| 5950 | } |
| 5951 | |
| 5952 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5953 | { |
| 5954 | const char *msg; |
| 5955 | struct hlua_txn *htxn; |
| 5956 | |
| 5957 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5958 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5959 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5960 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5961 | return 0; |
| 5962 | } |
| 5963 | |
| 5964 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5965 | { |
| 5966 | const char *msg; |
| 5967 | struct hlua_txn *htxn; |
| 5968 | |
| 5969 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5970 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5971 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5972 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5973 | return 0; |
| 5974 | } |
| 5975 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5976 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5977 | { |
| 5978 | struct hlua_txn *htxn; |
| 5979 | int ll; |
| 5980 | |
| 5981 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5982 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5983 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5984 | |
| 5985 | if (ll < 0 || ll > 7) |
| 5986 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5987 | |
| 5988 | htxn->s->logs.level = ll; |
| 5989 | return 0; |
| 5990 | } |
| 5991 | |
| 5992 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5993 | { |
| 5994 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5995 | int tos; |
| 5996 | |
| 5997 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5998 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5999 | tos = 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), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6002 | return 0; |
| 6003 | } |
| 6004 | |
| 6005 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 6006 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6007 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6008 | int mark; |
| 6009 | |
| 6010 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 6011 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6012 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6013 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 6014 | conn_set_tos(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 6015 | return 0; |
| 6016 | } |
| 6017 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 6018 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 6019 | { |
| 6020 | struct hlua_txn *htxn; |
| 6021 | |
| 6022 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 6023 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6024 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6025 | return 0; |
| 6026 | } |
| 6027 | |
| 6028 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 6029 | { |
| 6030 | struct hlua_txn *htxn; |
| 6031 | |
| 6032 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 6033 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 6034 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 6035 | return 0; |
| 6036 | } |
| 6037 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6038 | /* This function is an Lua binding that send pending data |
| 6039 | * to the client, and close the stream interface. |
| 6040 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6041 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6042 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6043 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6044 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6045 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6046 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6047 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6048 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6049 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6050 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 6051 | /* If the flags NOTERM is set, we cannot terminate the http |
| 6052 | * session, so we just end the execution of the current |
| 6053 | * lua code. |
| 6054 | */ |
| 6055 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 6056 | WILL_LJMP(hlua_done(L)); |
| 6057 | return 0; |
| 6058 | } |
| 6059 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 6060 | ic = &htxn->s->req; |
| 6061 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6062 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6063 | if (htxn->s->txn) { |
| 6064 | /* HTTP mode, let's stay in sync with the stream */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 6065 | b_del(&ic->buf, htxn->s->txn->req.sov); |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6066 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 6067 | htxn->s->txn->req.sov = 0; |
| 6068 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 6069 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 6070 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 6071 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 6072 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 6073 | /* Note that if we want to support keep-alive, we need |
| 6074 | * to bypass the close/shutr_now calls below, but that |
| 6075 | * may only be done if the HTTP request was already |
| 6076 | * processed and the connection header is known (ie |
| 6077 | * not during TCP rules). |
| 6078 | */ |
| 6079 | } |
| 6080 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6081 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6082 | channel_abort(ic); |
| 6083 | channel_auto_close(ic); |
| 6084 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 6085 | |
| 6086 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 6087 | channel_auto_read(oc); |
| 6088 | channel_auto_close(oc); |
| 6089 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6090 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 6091 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6092 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6093 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 6094 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6095 | return 0; |
| 6096 | } |
| 6097 | |
| 6098 | __LJMP static int hlua_log(lua_State *L) |
| 6099 | { |
| 6100 | int level; |
| 6101 | const char *msg; |
| 6102 | |
| 6103 | MAY_LJMP(check_args(L, 2, "log")); |
| 6104 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6105 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6106 | |
| 6107 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 6108 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 6109 | |
| 6110 | hlua_sendlog(NULL, level, msg); |
| 6111 | return 0; |
| 6112 | } |
| 6113 | |
| 6114 | __LJMP static int hlua_log_debug(lua_State *L) |
| 6115 | { |
| 6116 | const char *msg; |
| 6117 | |
| 6118 | MAY_LJMP(check_args(L, 1, "debug")); |
| 6119 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6120 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 6121 | return 0; |
| 6122 | } |
| 6123 | |
| 6124 | __LJMP static int hlua_log_info(lua_State *L) |
| 6125 | { |
| 6126 | const char *msg; |
| 6127 | |
| 6128 | MAY_LJMP(check_args(L, 1, "info")); |
| 6129 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6130 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 6131 | return 0; |
| 6132 | } |
| 6133 | |
| 6134 | __LJMP static int hlua_log_warning(lua_State *L) |
| 6135 | { |
| 6136 | const char *msg; |
| 6137 | |
| 6138 | MAY_LJMP(check_args(L, 1, "warning")); |
| 6139 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6140 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 6141 | return 0; |
| 6142 | } |
| 6143 | |
| 6144 | __LJMP static int hlua_log_alert(lua_State *L) |
| 6145 | { |
| 6146 | const char *msg; |
| 6147 | |
| 6148 | MAY_LJMP(check_args(L, 1, "alert")); |
| 6149 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6150 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 6151 | return 0; |
| 6152 | } |
| 6153 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6154 | __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] | 6155 | { |
| 6156 | int wakeup_ms = lua_tointeger(L, -1); |
| 6157 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6158 | 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] | 6159 | return 0; |
| 6160 | } |
| 6161 | |
| 6162 | __LJMP static int hlua_sleep(lua_State *L) |
| 6163 | { |
| 6164 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6165 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6166 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6167 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6168 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6169 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6170 | wakeup_ms = tick_add(now_ms, delay); |
| 6171 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6172 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6173 | 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] | 6174 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6175 | } |
| 6176 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6177 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6178 | { |
| 6179 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6180 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6181 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6182 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6183 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6184 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 6185 | wakeup_ms = tick_add(now_ms, delay); |
| 6186 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6187 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6188 | 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] | 6189 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6190 | } |
| 6191 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6192 | /* This functionis an LUA binding. it permits to give back |
| 6193 | * the hand at the HAProxy scheduler. It is used when the |
| 6194 | * LUA processing consumes a lot of time. |
| 6195 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 6196 | __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] | 6197 | { |
| 6198 | return 0; |
| 6199 | } |
| 6200 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6201 | __LJMP static int hlua_yield(lua_State *L) |
| 6202 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 6203 | 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] | 6204 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6205 | } |
| 6206 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6207 | /* This function change the nice of the currently executed |
| 6208 | * task. It is used set low or high priority at the current |
| 6209 | * task. |
| 6210 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6211 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6212 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6213 | struct hlua *hlua; |
| 6214 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6215 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6216 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 6217 | hlua = hlua_gethlua(L); |
| 6218 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6219 | |
| 6220 | /* If he task is not set, I'm in a start mode. */ |
| 6221 | if (!hlua || !hlua->task) |
| 6222 | return 0; |
| 6223 | |
| 6224 | if (nice < -1024) |
| 6225 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6226 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6227 | nice = 1024; |
| 6228 | |
| 6229 | hlua->task->nice = nice; |
| 6230 | return 0; |
| 6231 | } |
| 6232 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6233 | /* 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] | 6234 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6235 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6236 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6237 | * |
| 6238 | * Task wrapper are longjmp safe because the only one Lua code |
| 6239 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6240 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6241 | 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] | 6242 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6243 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6244 | enum hlua_exec status; |
| 6245 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6246 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6247 | task_set_affinity(task, tid_bit); |
| 6248 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6249 | /* If it is the first call to the task, we must initialize the |
| 6250 | * execution timeouts. |
| 6251 | */ |
| 6252 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6253 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6254 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6255 | /* Execute the Lua code. */ |
| 6256 | status = hlua_ctx_resume(hlua, 1); |
| 6257 | |
| 6258 | switch (status) { |
| 6259 | /* finished or yield */ |
| 6260 | case HLUA_E_OK: |
| 6261 | hlua_ctx_destroy(hlua); |
| 6262 | task_delete(task); |
| 6263 | task_free(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6264 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6265 | break; |
| 6266 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6267 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6268 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6269 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6270 | break; |
| 6271 | |
| 6272 | /* finished with error. */ |
| 6273 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6274 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6275 | hlua_ctx_destroy(hlua); |
| 6276 | task_delete(task); |
| 6277 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6278 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6279 | break; |
| 6280 | |
| 6281 | case HLUA_E_ERR: |
| 6282 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6283 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6284 | hlua_ctx_destroy(hlua); |
| 6285 | task_delete(task); |
| 6286 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6287 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6288 | break; |
| 6289 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6290 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6291 | } |
| 6292 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6293 | /* This function is an LUA binding that register LUA function to be |
| 6294 | * executed after the HAProxy configuration parsing and before the |
| 6295 | * HAProxy scheduler starts. This function expect only one LUA |
| 6296 | * argument that is a function. This function returns nothing, but |
| 6297 | * throws if an error is encountered. |
| 6298 | */ |
| 6299 | __LJMP static int hlua_register_init(lua_State *L) |
| 6300 | { |
| 6301 | struct hlua_init_function *init; |
| 6302 | int ref; |
| 6303 | |
| 6304 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6305 | |
| 6306 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6307 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6308 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6309 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6310 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6311 | |
| 6312 | init->function_ref = ref; |
| 6313 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6314 | return 0; |
| 6315 | } |
| 6316 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6317 | /* This functio is an LUA binding. It permits to register a task |
| 6318 | * executed in parallel of the main HAroxy activity. The task is |
| 6319 | * created and it is set in the HAProxy scheduler. It can be called |
| 6320 | * from the "init" section, "post init" or during the runtime. |
| 6321 | * |
| 6322 | * Lua prototype: |
| 6323 | * |
| 6324 | * <none> core.register_task(<function>) |
| 6325 | */ |
| 6326 | static int hlua_register_task(lua_State *L) |
| 6327 | { |
| 6328 | struct hlua *hlua; |
| 6329 | struct task *task; |
| 6330 | int ref; |
| 6331 | |
| 6332 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6333 | |
| 6334 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6335 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6336 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6337 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6338 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6339 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6340 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6341 | if (!task) |
| 6342 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6343 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6344 | task->context = hlua; |
| 6345 | task->process = hlua_process_task; |
| 6346 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6347 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6348 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6349 | |
| 6350 | /* Restore the function in the stack. */ |
| 6351 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6352 | hlua->nargs = 0; |
| 6353 | |
| 6354 | /* Schedule task. */ |
| 6355 | task_schedule(task, now_ms); |
| 6356 | |
| 6357 | return 0; |
| 6358 | } |
| 6359 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6360 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6361 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6362 | * resume converters. |
| 6363 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6364 | 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] | 6365 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6366 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6367 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6368 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6369 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6370 | if (!stream) |
| 6371 | return 0; |
| 6372 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6373 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6374 | * Lua context can be not initialized. This behavior |
| 6375 | * permits to save performances because a systematic |
| 6376 | * Lua initialization cause 5% performances loss. |
| 6377 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6378 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6379 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6380 | if (!stream->hlua) { |
| 6381 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6382 | return 0; |
| 6383 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6384 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6385 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6386 | return 0; |
| 6387 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6388 | } |
| 6389 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6390 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6391 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6392 | |
| 6393 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6394 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6395 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6396 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6397 | else |
| 6398 | error = "critical error"; |
| 6399 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6400 | return 0; |
| 6401 | } |
| 6402 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6403 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6404 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6405 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6406 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6407 | return 0; |
| 6408 | } |
| 6409 | |
| 6410 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6411 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6412 | |
| 6413 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6414 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6415 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6416 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6417 | return 0; |
| 6418 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6419 | hlua_smp2lua(stream->hlua->T, smp); |
| 6420 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6421 | |
| 6422 | /* push keywords in the stack. */ |
| 6423 | if (arg_p) { |
| 6424 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6425 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6426 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6427 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6428 | return 0; |
| 6429 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6430 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6431 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6432 | } |
| 6433 | } |
| 6434 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6435 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6436 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6437 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6438 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6439 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6440 | } |
| 6441 | |
| 6442 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6443 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6444 | /* finished. */ |
| 6445 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6446 | /* If the stack is empty, the function fails. */ |
| 6447 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6448 | return 0; |
| 6449 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6450 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6451 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6452 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6453 | return 1; |
| 6454 | |
| 6455 | /* yield. */ |
| 6456 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6457 | 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] | 6458 | return 0; |
| 6459 | |
| 6460 | /* finished with error. */ |
| 6461 | case HLUA_E_ERRMSG: |
| 6462 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6463 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6464 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6465 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6466 | return 0; |
| 6467 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6468 | case HLUA_E_ETMOUT: |
| 6469 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6470 | return 0; |
| 6471 | |
| 6472 | case HLUA_E_NOMEM: |
| 6473 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6474 | return 0; |
| 6475 | |
| 6476 | case HLUA_E_YIELD: |
| 6477 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6478 | return 0; |
| 6479 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6480 | case HLUA_E_ERR: |
| 6481 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6482 | 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] | 6483 | |
| 6484 | default: |
| 6485 | return 0; |
| 6486 | } |
| 6487 | } |
| 6488 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6489 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6490 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6491 | * resume sample-fetches. This function will be called by the sample |
| 6492 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6493 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6494 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6495 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6496 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6497 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6498 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6499 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6500 | const struct buffer msg = { }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6501 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6502 | if (!stream) |
| 6503 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6504 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6505 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6506 | * Lua context can be not initialized. This behavior |
| 6507 | * permits to save performances because a systematic |
| 6508 | * Lua initialization cause 5% performances loss. |
| 6509 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6510 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6511 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6512 | if (!stream->hlua) { |
| 6513 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6514 | return 0; |
| 6515 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6516 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6517 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6518 | return 0; |
| 6519 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6520 | } |
| 6521 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6522 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6523 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6524 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6525 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6526 | |
| 6527 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6528 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6529 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6530 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6531 | else |
| 6532 | error = "critical error"; |
| 6533 | 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] | 6534 | return 0; |
| 6535 | } |
| 6536 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6537 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6538 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6539 | 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] | 6540 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6541 | return 0; |
| 6542 | } |
| 6543 | |
| 6544 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6545 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6546 | |
| 6547 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6548 | 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] | 6549 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6550 | 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] | 6551 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6552 | return 0; |
| 6553 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6554 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6555 | |
| 6556 | /* push keywords in the stack. */ |
| 6557 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6558 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6559 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6560 | 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] | 6561 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6562 | return 0; |
| 6563 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6564 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6565 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6566 | } |
| 6567 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6568 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6569 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6570 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6571 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6572 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6573 | } |
| 6574 | |
| 6575 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6576 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6577 | /* finished. */ |
| 6578 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6579 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6580 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6581 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6582 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6583 | /* If the stack is empty, the function fails. */ |
| 6584 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6585 | return 0; |
| 6586 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6587 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6588 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6589 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6590 | |
| 6591 | /* Set the end of execution flag. */ |
| 6592 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6593 | return 1; |
| 6594 | |
| 6595 | /* yield. */ |
| 6596 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6597 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6598 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6599 | 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] | 6600 | return 0; |
| 6601 | |
| 6602 | /* finished with error. */ |
| 6603 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6604 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6605 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6606 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6607 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6608 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6609 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6610 | return 0; |
| 6611 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6612 | case HLUA_E_ETMOUT: |
| 6613 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6614 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6615 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6616 | return 0; |
| 6617 | |
| 6618 | case HLUA_E_NOMEM: |
| 6619 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6620 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6621 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6622 | return 0; |
| 6623 | |
| 6624 | case HLUA_E_YIELD: |
| 6625 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6626 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6627 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6628 | return 0; |
| 6629 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6630 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6631 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6632 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6633 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6634 | 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] | 6635 | |
| 6636 | default: |
| 6637 | return 0; |
| 6638 | } |
| 6639 | } |
| 6640 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6641 | /* This function is an LUA binding used for registering |
| 6642 | * "sample-conv" functions. It expects a converter name used |
| 6643 | * in the haproxy configuration file, and an LUA function. |
| 6644 | */ |
| 6645 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6646 | { |
| 6647 | struct sample_conv_kw_list *sck; |
| 6648 | const char *name; |
| 6649 | int ref; |
| 6650 | int len; |
| 6651 | struct hlua_function *fcn; |
| 6652 | |
| 6653 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6654 | |
| 6655 | /* First argument : converter name. */ |
| 6656 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6657 | |
| 6658 | /* Second argument : lua function. */ |
| 6659 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6660 | |
| 6661 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6662 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6663 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6664 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6665 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6666 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6667 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6668 | |
| 6669 | /* Fill fcn. */ |
| 6670 | fcn->name = strdup(name); |
| 6671 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6672 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6673 | fcn->function_ref = ref; |
| 6674 | |
| 6675 | /* List head */ |
| 6676 | sck->list.n = sck->list.p = NULL; |
| 6677 | |
| 6678 | /* converter keyword. */ |
| 6679 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6680 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6681 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6682 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6683 | |
| 6684 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6685 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6686 | 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] | 6687 | sck->kw[0].val_args = NULL; |
| 6688 | sck->kw[0].in_type = SMP_T_STR; |
| 6689 | sck->kw[0].out_type = SMP_T_STR; |
| 6690 | sck->kw[0].private = fcn; |
| 6691 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6692 | /* Register this new converter */ |
| 6693 | sample_register_convs(sck); |
| 6694 | |
| 6695 | return 0; |
| 6696 | } |
| 6697 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6698 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6699 | * "sample-fetch" functions. It expects a converter name used |
| 6700 | * in the haproxy configuration file, and an LUA function. |
| 6701 | */ |
| 6702 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6703 | { |
| 6704 | const char *name; |
| 6705 | int ref; |
| 6706 | int len; |
| 6707 | struct sample_fetch_kw_list *sfk; |
| 6708 | struct hlua_function *fcn; |
| 6709 | |
| 6710 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6711 | |
| 6712 | /* First argument : sample-fetch name. */ |
| 6713 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6714 | |
| 6715 | /* Second argument : lua function. */ |
| 6716 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6717 | |
| 6718 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6719 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6720 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6721 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6722 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6723 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6724 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6725 | |
| 6726 | /* Fill fcn. */ |
| 6727 | fcn->name = strdup(name); |
| 6728 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6729 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6730 | fcn->function_ref = ref; |
| 6731 | |
| 6732 | /* List head */ |
| 6733 | sfk->list.n = sfk->list.p = NULL; |
| 6734 | |
| 6735 | /* sample-fetch keyword. */ |
| 6736 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6737 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6738 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6739 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6740 | |
| 6741 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6742 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6743 | 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] | 6744 | sfk->kw[0].val_args = NULL; |
| 6745 | sfk->kw[0].out_type = SMP_T_STR; |
| 6746 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6747 | sfk->kw[0].val = 0; |
| 6748 | sfk->kw[0].private = fcn; |
| 6749 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6750 | /* Register this new fetch. */ |
| 6751 | sample_register_fetches(sfk); |
| 6752 | |
| 6753 | return 0; |
| 6754 | } |
| 6755 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6756 | /* This function is a wrapper to execute each LUA function declared |
| 6757 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6758 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6759 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6760 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6761 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6762 | 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] | 6763 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6764 | { |
| 6765 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6766 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6767 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6768 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6769 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6770 | |
| 6771 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 6772 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 6773 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 6774 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 6775 | 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] | 6776 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6777 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6778 | return ACT_RET_CONT; |
| 6779 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6780 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6781 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6782 | * Lua context can be not initialized. This behavior |
| 6783 | * permits to save performances because a systematic |
| 6784 | * Lua initialization cause 5% performances loss. |
| 6785 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6786 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6787 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6788 | if (!s->hlua) { |
| 6789 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6790 | rule->arg.hlua_rule->fcn.name); |
| 6791 | return ACT_RET_CONT; |
| 6792 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6793 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6794 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6795 | rule->arg.hlua_rule->fcn.name); |
| 6796 | return ACT_RET_CONT; |
| 6797 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6798 | } |
| 6799 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6800 | consistency_set(s, dir, &s->hlua->cons); |
| 6801 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6802 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6803 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6804 | |
| 6805 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6806 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6807 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6808 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6809 | else |
| 6810 | error = "critical error"; |
| 6811 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6812 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6813 | return ACT_RET_CONT; |
| 6814 | } |
| 6815 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6816 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6817 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6818 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6819 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6820 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6821 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6822 | } |
| 6823 | |
| 6824 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6825 | 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] | 6826 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6827 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6828 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6829 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6830 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6831 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6832 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6833 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6834 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6835 | |
| 6836 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6837 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6838 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6839 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6840 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6841 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6842 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6843 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6844 | lua_pushstring(s->hlua->T, *arg); |
| 6845 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6846 | } |
| 6847 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6848 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6849 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6850 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6851 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6852 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6853 | } |
| 6854 | |
| 6855 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6856 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6857 | /* finished. */ |
| 6858 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6859 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6860 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6861 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6862 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6863 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6864 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6865 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6866 | |
| 6867 | /* yield. */ |
| 6868 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6869 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6870 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6871 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6872 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6873 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6874 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6875 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6876 | /* Some actions can be wake up when a "write" event |
| 6877 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6878 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6879 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6880 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6881 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6882 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6883 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6884 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6885 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6886 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6887 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6888 | * because HAProxy is not able to manipulate data, it |
| 6889 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6890 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6891 | |
| 6892 | /* finished with error. */ |
| 6893 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6894 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6895 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6896 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6897 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6898 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6899 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6900 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6901 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6902 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6903 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6904 | case HLUA_E_ETMOUT: |
| 6905 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6906 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6907 | return ACT_RET_ERR; |
| 6908 | } |
| 6909 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6910 | return 0; |
| 6911 | |
| 6912 | case HLUA_E_NOMEM: |
| 6913 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6914 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6915 | return ACT_RET_ERR; |
| 6916 | } |
| 6917 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6918 | return 0; |
| 6919 | |
| 6920 | case HLUA_E_YIELD: |
| 6921 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6922 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6923 | return ACT_RET_ERR; |
| 6924 | } |
| 6925 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6926 | rule->arg.hlua_rule->fcn.name); |
| 6927 | return 0; |
| 6928 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6929 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6930 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6931 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6932 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6933 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6934 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6935 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6936 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6937 | |
| 6938 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6939 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6940 | } |
| 6941 | } |
| 6942 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6943 | 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] | 6944 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6945 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6946 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6947 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6948 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6949 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6950 | } |
| 6951 | |
| 6952 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6953 | { |
| 6954 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6955 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6956 | struct task *task; |
| 6957 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6958 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6959 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6960 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6961 | if (!hlua) { |
| 6962 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6963 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6964 | return 0; |
| 6965 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6966 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6967 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6968 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6969 | |
| 6970 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6971 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6972 | if (!task) { |
| 6973 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6974 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6975 | return 0; |
| 6976 | } |
| 6977 | task->nice = 0; |
| 6978 | task->context = ctx; |
| 6979 | task->process = hlua_applet_wakeup; |
| 6980 | ctx->ctx.hlua_apptcp.task = task; |
| 6981 | |
| 6982 | /* In the execution wrappers linked with a stream, the |
| 6983 | * Lua context can be not initialized. This behavior |
| 6984 | * permits to save performances because a systematic |
| 6985 | * Lua initialization cause 5% performances loss. |
| 6986 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6987 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6988 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6989 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6990 | return 0; |
| 6991 | } |
| 6992 | |
| 6993 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6994 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6995 | |
| 6996 | /* The following Lua calls can fail. */ |
| 6997 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6998 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6999 | error = lua_tostring(hlua->T, -1); |
| 7000 | else |
| 7001 | error = "critical error"; |
| 7002 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7003 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7004 | return 0; |
| 7005 | } |
| 7006 | |
| 7007 | /* Check stack available size. */ |
| 7008 | if (!lua_checkstack(hlua->T, 1)) { |
| 7009 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7010 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7011 | RESET_SAFE_LJMP(hlua->T); |
| 7012 | return 0; |
| 7013 | } |
| 7014 | |
| 7015 | /* Restore the function in the stack. */ |
| 7016 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7017 | |
| 7018 | /* Create and and push object stream in the stack. */ |
| 7019 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 7020 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7021 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7022 | RESET_SAFE_LJMP(hlua->T); |
| 7023 | return 0; |
| 7024 | } |
| 7025 | hlua->nargs = 1; |
| 7026 | |
| 7027 | /* push keywords in the stack. */ |
| 7028 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7029 | if (!lua_checkstack(hlua->T, 1)) { |
| 7030 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 7031 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7032 | RESET_SAFE_LJMP(hlua->T); |
| 7033 | return 0; |
| 7034 | } |
| 7035 | lua_pushstring(hlua->T, *arg); |
| 7036 | hlua->nargs++; |
| 7037 | } |
| 7038 | |
| 7039 | RESET_SAFE_LJMP(hlua->T); |
| 7040 | |
| 7041 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7042 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 7043 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7044 | |
| 7045 | return 1; |
| 7046 | } |
| 7047 | |
| 7048 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 7049 | { |
| 7050 | struct stream_interface *si = ctx->owner; |
| 7051 | struct stream *strm = si_strm(si); |
| 7052 | struct channel *res = si_ic(si); |
| 7053 | struct act_rule *rule = ctx->rule; |
| 7054 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7055 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7056 | |
| 7057 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7058 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 7059 | /* eat the whole request */ |
| 7060 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7061 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 7062 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7063 | |
| 7064 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7065 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7066 | return; |
| 7067 | |
| 7068 | /* Execute the function. */ |
| 7069 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7070 | /* finished. */ |
| 7071 | case HLUA_E_OK: |
| 7072 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7073 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7074 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7075 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7076 | res->flags |= CF_READ_NULL; |
| 7077 | si_shutr(si); |
| 7078 | return; |
| 7079 | |
| 7080 | /* yield. */ |
| 7081 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7082 | if (hlua->wake_time != TICK_ETERNITY) |
| 7083 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7084 | return; |
| 7085 | |
| 7086 | /* finished with error. */ |
| 7087 | case HLUA_E_ERRMSG: |
| 7088 | /* Display log. */ |
| 7089 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 7090 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7091 | lua_pop(hlua->T, 1); |
| 7092 | goto error; |
| 7093 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7094 | case HLUA_E_ETMOUT: |
| 7095 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 7096 | rule->arg.hlua_rule->fcn.name); |
| 7097 | goto error; |
| 7098 | |
| 7099 | case HLUA_E_NOMEM: |
| 7100 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 7101 | rule->arg.hlua_rule->fcn.name); |
| 7102 | goto error; |
| 7103 | |
| 7104 | case HLUA_E_YIELD: /* unexpected */ |
| 7105 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 7106 | rule->arg.hlua_rule->fcn.name); |
| 7107 | goto error; |
| 7108 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7109 | case HLUA_E_ERR: |
| 7110 | /* Display log. */ |
| 7111 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 7112 | rule->arg.hlua_rule->fcn.name); |
| 7113 | goto error; |
| 7114 | |
| 7115 | default: |
| 7116 | goto error; |
| 7117 | } |
| 7118 | |
| 7119 | error: |
| 7120 | |
| 7121 | /* For all other cases, just close the stream. */ |
| 7122 | si_shutw(si); |
| 7123 | si_shutr(si); |
| 7124 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 7125 | } |
| 7126 | |
| 7127 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 7128 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 7129 | task_delete(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7130 | task_free(ctx->ctx.hlua_apptcp.task); |
| 7131 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7132 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7133 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7134 | } |
| 7135 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7136 | /* The function returns 1 if the initialisation is complete, 0 if |
| 7137 | * an errors occurs and -1 if more data are required for initializing |
| 7138 | * the applet. |
| 7139 | */ |
| 7140 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 7141 | { |
| 7142 | struct stream_interface *si = ctx->owner; |
| 7143 | struct channel *req = si_oc(si); |
| 7144 | struct http_msg *msg; |
| 7145 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7146 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7147 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7148 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7149 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7150 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7151 | txn = strm->txn; |
| 7152 | msg = &txn->req; |
| 7153 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7154 | /* We want two things in HTTP mode : |
| 7155 | * - enforce server-close mode if we were in keep-alive, so that the |
| 7156 | * applet is released after each response ; |
| 7157 | * - enable request body transfer to the applet in order to resync |
| 7158 | * with the response body. |
| 7159 | */ |
| 7160 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 7161 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 7162 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7163 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7164 | if (!hlua) { |
| 7165 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7166 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7167 | return 0; |
| 7168 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7169 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7170 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7171 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 7172 | ctx->ctx.hlua_apphttp.flags = 0; |
| 7173 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 7174 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 7175 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 7176 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7177 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7178 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7179 | if (!task) { |
| 7180 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 7181 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7182 | return 0; |
| 7183 | } |
| 7184 | task->nice = 0; |
| 7185 | task->context = ctx; |
| 7186 | task->process = hlua_applet_wakeup; |
| 7187 | ctx->ctx.hlua_apphttp.task = task; |
| 7188 | |
| 7189 | /* In the execution wrappers linked with a stream, the |
| 7190 | * Lua context can be not initialized. This behavior |
| 7191 | * permits to save performances because a systematic |
| 7192 | * Lua initialization cause 5% performances loss. |
| 7193 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7194 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7195 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 7196 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7197 | return 0; |
| 7198 | } |
| 7199 | |
| 7200 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 7201 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7202 | |
| 7203 | /* The following Lua calls can fail. */ |
| 7204 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7205 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7206 | error = lua_tostring(hlua->T, -1); |
| 7207 | else |
| 7208 | error = "critical error"; |
| 7209 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7210 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7211 | return 0; |
| 7212 | } |
| 7213 | |
| 7214 | /* Check stack available size. */ |
| 7215 | if (!lua_checkstack(hlua->T, 1)) { |
| 7216 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7217 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7218 | RESET_SAFE_LJMP(hlua->T); |
| 7219 | return 0; |
| 7220 | } |
| 7221 | |
| 7222 | /* Restore the function in the stack. */ |
| 7223 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 7224 | |
| 7225 | /* Create and and push object stream in the stack. */ |
| 7226 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 7227 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7228 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7229 | RESET_SAFE_LJMP(hlua->T); |
| 7230 | return 0; |
| 7231 | } |
| 7232 | hlua->nargs = 1; |
| 7233 | |
| 7234 | /* Look for a 100-continue expected. */ |
| 7235 | if (msg->flags & HTTP_MSGF_VER_11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7236 | if (IS_HTX_STRM(si_strm(si))) { |
| 7237 | /* HTX version */ |
| 7238 | struct htx *htx = htxbuf(&req->buf); |
| 7239 | struct ist hdr = { .ptr = "Expect", .len = 6 }; |
| 7240 | struct http_hdr_ctx hdr_ctx; |
| 7241 | |
| 7242 | hdr_ctx.blk = NULL; |
| 7243 | /* Expect is allowed in 1.1, look for it */ |
| 7244 | if (http_find_header(htx, hdr, &hdr_ctx, 0) && |
| 7245 | unlikely(isteqi(hdr_ctx.value, ist2("100-continue", 12)))) |
| 7246 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 7247 | } |
| 7248 | else { |
| 7249 | /* Legacy HTTP version */ |
| 7250 | struct hdr_ctx hdr; |
| 7251 | |
| 7252 | hdr.idx = 0; |
| 7253 | if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &hdr) && |
| 7254 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 7255 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 7256 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7257 | } |
| 7258 | |
| 7259 | /* push keywords in the stack. */ |
| 7260 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 7261 | if (!lua_checkstack(hlua->T, 1)) { |
| 7262 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 7263 | ctx->rule->arg.hlua_rule->fcn.name); |
| 7264 | RESET_SAFE_LJMP(hlua->T); |
| 7265 | return 0; |
| 7266 | } |
| 7267 | lua_pushstring(hlua->T, *arg); |
| 7268 | hlua->nargs++; |
| 7269 | } |
| 7270 | |
| 7271 | RESET_SAFE_LJMP(hlua->T); |
| 7272 | |
| 7273 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7274 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7275 | |
| 7276 | return 1; |
| 7277 | } |
| 7278 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7279 | static void hlua_applet_htx_fct(struct appctx *ctx) |
| 7280 | { |
| 7281 | struct stream_interface *si = ctx->owner; |
| 7282 | struct stream *strm = si_strm(si); |
| 7283 | struct channel *req = si_oc(si); |
| 7284 | struct channel *res = si_ic(si); |
| 7285 | struct act_rule *rule = ctx->rule; |
| 7286 | struct proxy *px = strm->be; |
| 7287 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7288 | struct htx *req_htx, *res_htx; |
| 7289 | |
| 7290 | res_htx = htx_from_buf(&res->buf); |
| 7291 | |
| 7292 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7293 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7294 | goto out; |
| 7295 | |
| 7296 | /* Check if the input buffer is avalaible. */ |
| 7297 | if (!b_size(&res->buf)) { |
| 7298 | si_rx_room_blk(si); |
| 7299 | goto out; |
| 7300 | } |
| 7301 | /* check that the output is not closed */ |
| 7302 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW)) |
| 7303 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7304 | |
| 7305 | /* Set the currently running flag. */ |
| 7306 | if (!HLUA_IS_RUNNING(hlua) && |
| 7307 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7308 | struct htx_blk *blk; |
| 7309 | size_t count = co_data(req); |
| 7310 | |
| 7311 | if (!count) { |
| 7312 | si_cant_get(si); |
| 7313 | goto out; |
| 7314 | } |
| 7315 | |
| 7316 | /* We need to flush the request header. This left the body for |
| 7317 | * the Lua. |
| 7318 | */ |
| 7319 | req_htx = htx_from_buf(&req->buf); |
| 7320 | blk = htx_get_head_blk(req_htx); |
| 7321 | while (count && blk) { |
| 7322 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7323 | uint32_t sz = htx_get_blksz(blk); |
| 7324 | |
| 7325 | if (sz > count) { |
| 7326 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7327 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7328 | goto out; |
| 7329 | } |
| 7330 | |
| 7331 | count -= sz; |
| 7332 | co_set_data(req, co_data(req) - sz); |
| 7333 | blk = htx_remove_blk(req_htx, blk); |
| 7334 | |
| 7335 | if (type == HTX_BLK_EOH) |
| 7336 | break; |
| 7337 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7338 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7339 | } |
| 7340 | |
| 7341 | /* Executes The applet if it is not done. */ |
| 7342 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7343 | |
| 7344 | /* Execute the function. */ |
| 7345 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7346 | /* finished. */ |
| 7347 | case HLUA_E_OK: |
| 7348 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7349 | break; |
| 7350 | |
| 7351 | /* yield. */ |
| 7352 | case HLUA_E_AGAIN: |
| 7353 | if (hlua->wake_time != TICK_ETERNITY) |
| 7354 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
| 7355 | return; |
| 7356 | |
| 7357 | /* finished with error. */ |
| 7358 | case HLUA_E_ERRMSG: |
| 7359 | /* Display log. */ |
| 7360 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7361 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7362 | lua_pop(hlua->T, 1); |
| 7363 | goto error; |
| 7364 | |
| 7365 | case HLUA_E_ETMOUT: |
| 7366 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7367 | rule->arg.hlua_rule->fcn.name); |
| 7368 | goto error; |
| 7369 | |
| 7370 | case HLUA_E_NOMEM: |
| 7371 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7372 | rule->arg.hlua_rule->fcn.name); |
| 7373 | goto error; |
| 7374 | |
| 7375 | case HLUA_E_YIELD: /* unexpected */ |
| 7376 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7377 | rule->arg.hlua_rule->fcn.name); |
| 7378 | goto error; |
| 7379 | |
| 7380 | case HLUA_E_ERR: |
| 7381 | /* Display log. */ |
| 7382 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7383 | rule->arg.hlua_rule->fcn.name); |
| 7384 | goto error; |
| 7385 | |
| 7386 | default: |
| 7387 | goto error; |
| 7388 | } |
| 7389 | } |
| 7390 | |
| 7391 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 7392 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7393 | goto error; |
| 7394 | |
| 7395 | /* Don't add EOD and TLR because mux-h1 will take care of it */ |
| 7396 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7397 | si_rx_room_blk(si); |
| 7398 | goto out; |
| 7399 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7400 | channel_add_input(res, 1); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7401 | } |
| 7402 | |
| 7403 | done: |
| 7404 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 7405 | /* eat the whole request */ |
| 7406 | req_htx = htxbuf(&req->buf); |
| 7407 | htx_reset(req_htx); |
| 7408 | htx_to_buf(req_htx, &req->buf); |
| 7409 | co_set_data(req, 0); |
| 7410 | res->flags |= CF_READ_NULL; |
| 7411 | si_shutr(si); |
| 7412 | } |
| 7413 | |
| 7414 | if ((res->flags & CF_SHUTR) && (si->state == SI_ST_EST)) |
| 7415 | si_shutw(si); |
| 7416 | |
| 7417 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 7418 | if ((req->flags & CF_SHUTW) && (si->state == SI_ST_EST)) { |
| 7419 | si_shutr(si); |
| 7420 | res->flags |= CF_READ_NULL; |
| 7421 | } |
| 7422 | } |
| 7423 | |
| 7424 | out: |
| 7425 | /* we have left the request in the buffer for the case where we |
| 7426 | * process a POST, and this automatically re-enables activity on |
| 7427 | * read. It's better to indicate that we want to stop reading when |
| 7428 | * we're sending, so that we know there's at most one direction |
| 7429 | * deciding to wake the applet up. It saves it from looping when |
| 7430 | * emitting large blocks into small TCP windows. |
| 7431 | */ |
| 7432 | htx_to_buf(res_htx, &res->buf); |
| 7433 | if (!channel_is_empty(res)) |
| 7434 | si_stop_get(si); |
| 7435 | return; |
| 7436 | |
| 7437 | error: |
| 7438 | |
| 7439 | /* If we are in HTTP mode, and we are not send any |
| 7440 | * data, return a 500 server error in best effort: |
| 7441 | * if there is no room available in the buffer, |
| 7442 | * just close the connection. |
| 7443 | */ |
| 7444 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 7445 | struct buffer *err = &htx_err_chunks[HTTP_ERR_500]; |
| 7446 | |
| 7447 | channel_erase(res); |
| 7448 | res->buf.data = b_data(err); |
| 7449 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7450 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7451 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7452 | } |
| 7453 | if (!(strm->flags & SF_ERR_MASK)) |
| 7454 | strm->flags |= SF_ERR_RESOURCE; |
| 7455 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7456 | goto done; |
| 7457 | } |
| 7458 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7459 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 7460 | { |
| 7461 | struct stream_interface *si = ctx->owner; |
| 7462 | struct stream *strm = si_strm(si); |
| 7463 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7464 | struct act_rule *rule = ctx->rule; |
| 7465 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7466 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7467 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7468 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 7469 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 7470 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7471 | int ret; |
| 7472 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7473 | if (IS_HTX_STRM(strm)) |
| 7474 | return hlua_applet_htx_fct(ctx); |
| 7475 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7476 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7477 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7478 | return; |
| 7479 | |
| 7480 | /* Set the currently running flag. */ |
| 7481 | if (!HLUA_IS_RUNNING(hlua) && |
| 7482 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7483 | /* Store the max amount of bytes that we can read. */ |
| 7484 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 7485 | |
| 7486 | /* We need to flush the request header. This left the body |
| 7487 | * for the Lua. |
| 7488 | */ |
| 7489 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7490 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 7491 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7492 | if (ret == -1) |
| 7493 | return; |
| 7494 | |
| 7495 | /* No data available, ask for more data. */ |
| 7496 | if (ret == 1) |
| 7497 | len2 = 0; |
| 7498 | if (ret == 0) |
| 7499 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 7500 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7501 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7502 | return; |
| 7503 | } |
| 7504 | |
| 7505 | /* skip the requests bytes. */ |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 7506 | 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] | 7507 | } |
| 7508 | |
| 7509 | /* Executes The applet if it is not done. */ |
| 7510 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7511 | |
| 7512 | /* Execute the function. */ |
| 7513 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7514 | /* finished. */ |
| 7515 | case HLUA_E_OK: |
| 7516 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7517 | break; |
| 7518 | |
| 7519 | /* yield. */ |
| 7520 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 7521 | if (hlua->wake_time != TICK_ETERNITY) |
| 7522 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7523 | return; |
| 7524 | |
| 7525 | /* finished with error. */ |
| 7526 | case HLUA_E_ERRMSG: |
| 7527 | /* Display log. */ |
| 7528 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7529 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7530 | lua_pop(hlua->T, 1); |
| 7531 | goto error; |
| 7532 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7533 | case HLUA_E_ETMOUT: |
| 7534 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7535 | rule->arg.hlua_rule->fcn.name); |
| 7536 | goto error; |
| 7537 | |
| 7538 | case HLUA_E_NOMEM: |
| 7539 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7540 | rule->arg.hlua_rule->fcn.name); |
| 7541 | goto error; |
| 7542 | |
| 7543 | case HLUA_E_YIELD: /* unexpected */ |
| 7544 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7545 | rule->arg.hlua_rule->fcn.name); |
| 7546 | goto error; |
| 7547 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7548 | case HLUA_E_ERR: |
| 7549 | /* Display log. */ |
| 7550 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7551 | rule->arg.hlua_rule->fcn.name); |
| 7552 | goto error; |
| 7553 | |
| 7554 | default: |
| 7555 | goto error; |
| 7556 | } |
| 7557 | } |
| 7558 | |
| 7559 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 7560 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7561 | goto error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7562 | |
| 7563 | /* We must send the final chunk. */ |
| 7564 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 7565 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 7566 | |
| 7567 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 7568 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7569 | |
| 7570 | /* critical error. */ |
| 7571 | if (ret == -2 || ret == -3) { |
| 7572 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 7573 | rule->arg.hlua_rule->fcn.name); |
| 7574 | goto error; |
| 7575 | } |
| 7576 | |
| 7577 | /* no enough space error. */ |
| 7578 | if (ret == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7579 | si_rx_room_blk(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7580 | return; |
| 7581 | } |
| 7582 | |
| 7583 | /* set the last chunk sent. */ |
| 7584 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 7585 | } |
| 7586 | |
| 7587 | /* close the connection. */ |
| 7588 | |
Frédéric Lécaille | 83ed5d5 | 2018-07-18 14:25:26 +0200 | [diff] [blame] | 7589 | /* status */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7590 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7591 | |
| 7592 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 7593 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7594 | res->flags |= CF_READ_NULL; |
| 7595 | si_shutr(si); |
| 7596 | |
| 7597 | return; |
| 7598 | } |
| 7599 | |
| 7600 | error: |
| 7601 | |
| 7602 | /* If we are in HTTP mode, and we are not send any |
| 7603 | * data, return a 500 server error in best effort: |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7604 | * if there is no room available in the buffer, |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7605 | * just close the connection. |
| 7606 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 7607 | ci_putblk(res, error_500, strlen(error_500)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7608 | if (!(strm->flags & SF_ERR_MASK)) |
| 7609 | strm->flags |= SF_ERR_RESOURCE; |
| 7610 | si_shutw(si); |
| 7611 | si_shutr(si); |
| 7612 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7613 | } |
| 7614 | |
| 7615 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7616 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 7617 | task_delete(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7618 | task_free(ctx->ctx.hlua_apphttp.task); |
| 7619 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7620 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7621 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7622 | } |
| 7623 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7624 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 7625 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7626 | * |
| 7627 | * This function can fail with an abort() due to an Lua critical error. |
| 7628 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7629 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7630 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7631 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7632 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7633 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7634 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7635 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7636 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7637 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7638 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7639 | if (!rule->arg.hlua_rule) { |
| 7640 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7641 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7642 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7643 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7644 | /* Memory for arguments. */ |
| 7645 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7646 | if (!rule->arg.hlua_rule->args) { |
| 7647 | memprintf(err, "out of memory error"); |
| 7648 | return ACT_RET_PRS_ERR; |
| 7649 | } |
| 7650 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7651 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7652 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7653 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7654 | /* Expect some arguments */ |
| 7655 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7656 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7657 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7658 | return ACT_RET_PRS_ERR; |
| 7659 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7660 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7661 | if (!rule->arg.hlua_rule->args[i]) { |
| 7662 | memprintf(err, "out of memory error"); |
| 7663 | return ACT_RET_PRS_ERR; |
| 7664 | } |
| 7665 | (*cur_arg)++; |
| 7666 | } |
| 7667 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7668 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7669 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7670 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7671 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7672 | } |
| 7673 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7674 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7675 | struct act_rule *rule, char **err) |
| 7676 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7677 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7678 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7679 | /* HTTP applets are forbidden in tcp-request rules. |
| 7680 | * HTTP applet request requires everything initilized by |
| 7681 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 7682 | * The applet will be immediately initilized, but its before |
| 7683 | * the call of this analyzer. |
| 7684 | */ |
| 7685 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7686 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7687 | return ACT_RET_PRS_ERR; |
| 7688 | } |
| 7689 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7690 | /* Memory for the rule. */ |
| 7691 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7692 | if (!rule->arg.hlua_rule) { |
| 7693 | memprintf(err, "out of memory error"); |
| 7694 | return ACT_RET_PRS_ERR; |
| 7695 | } |
| 7696 | |
| 7697 | /* Reference the Lua function and store the reference. */ |
| 7698 | rule->arg.hlua_rule->fcn = *fcn; |
| 7699 | |
| 7700 | /* TODO: later accept arguments. */ |
| 7701 | rule->arg.hlua_rule->args = NULL; |
| 7702 | |
| 7703 | /* Add applet pointer in the rule. */ |
| 7704 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7705 | rule->applet.name = fcn->name; |
| 7706 | rule->applet.init = hlua_applet_http_init; |
| 7707 | rule->applet.fct = hlua_applet_http_fct; |
| 7708 | rule->applet.release = hlua_applet_http_release; |
| 7709 | rule->applet.timeout = hlua_timeout_applet; |
| 7710 | |
| 7711 | return ACT_RET_PRS_OK; |
| 7712 | } |
| 7713 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7714 | /* This function is an LUA binding used for registering |
| 7715 | * "sample-conv" functions. It expects a converter name used |
| 7716 | * in the haproxy configuration file, and an LUA function. |
| 7717 | */ |
| 7718 | __LJMP static int hlua_register_action(lua_State *L) |
| 7719 | { |
| 7720 | struct action_kw_list *akl; |
| 7721 | const char *name; |
| 7722 | int ref; |
| 7723 | int len; |
| 7724 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7725 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7726 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7727 | /* Initialise the number of expected arguments at 0. */ |
| 7728 | nargs = 0; |
| 7729 | |
| 7730 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7731 | 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] | 7732 | |
| 7733 | /* First argument : converter name. */ |
| 7734 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7735 | |
| 7736 | /* Second argument : environment. */ |
| 7737 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7738 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7739 | |
| 7740 | /* Third argument : lua function. */ |
| 7741 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7742 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7743 | /* 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] | 7744 | if (lua_gettop(L) >= 4) |
| 7745 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7746 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7747 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7748 | lua_pushnil(L); |
| 7749 | while (lua_next(L, 2) != 0) { |
| 7750 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7751 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7752 | |
| 7753 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7754 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7755 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7756 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7757 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7758 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7759 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7760 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7761 | |
| 7762 | /* Fill fcn. */ |
| 7763 | fcn->name = strdup(name); |
| 7764 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7765 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7766 | fcn->function_ref = ref; |
| 7767 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7768 | /* Set the expected number od arguments. */ |
| 7769 | fcn->nargs = nargs; |
| 7770 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7771 | /* List head */ |
| 7772 | akl->list.n = akl->list.p = NULL; |
| 7773 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7774 | /* action keyword. */ |
| 7775 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7776 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7777 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7778 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7779 | |
| 7780 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7781 | |
| 7782 | akl->kw[0].match_pfx = 0; |
| 7783 | akl->kw[0].private = fcn; |
| 7784 | akl->kw[0].parse = action_register_lua; |
| 7785 | |
| 7786 | /* select the action registering point. */ |
| 7787 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7788 | tcp_req_cont_keywords_register(akl); |
| 7789 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7790 | tcp_res_cont_keywords_register(akl); |
| 7791 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7792 | http_req_keywords_register(akl); |
| 7793 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7794 | http_res_keywords_register(akl); |
| 7795 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7796 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7797 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7798 | "are expected.", lua_tostring(L, -1))); |
| 7799 | |
| 7800 | /* pop the environment string. */ |
| 7801 | lua_pop(L, 1); |
| 7802 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7803 | return ACT_RET_PRS_OK; |
| 7804 | } |
| 7805 | |
| 7806 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7807 | struct act_rule *rule, char **err) |
| 7808 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7809 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7810 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7811 | if (px->options2 & PR_O2_USE_HTX) { |
| 7812 | memprintf(err, "Lua services cannot be used when the HTX internal representation is enabled"); |
| 7813 | return ACT_RET_PRS_ERR; |
| 7814 | } |
| 7815 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7816 | /* Memory for the rule. */ |
| 7817 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7818 | if (!rule->arg.hlua_rule) { |
| 7819 | memprintf(err, "out of memory error"); |
| 7820 | return ACT_RET_PRS_ERR; |
| 7821 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7822 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7823 | /* Reference the Lua function and store the reference. */ |
| 7824 | rule->arg.hlua_rule->fcn = *fcn; |
| 7825 | |
| 7826 | /* TODO: later accept arguments. */ |
| 7827 | rule->arg.hlua_rule->args = NULL; |
| 7828 | |
| 7829 | /* Add applet pointer in the rule. */ |
| 7830 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7831 | rule->applet.name = fcn->name; |
| 7832 | rule->applet.init = hlua_applet_tcp_init; |
| 7833 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7834 | rule->applet.release = hlua_applet_tcp_release; |
| 7835 | rule->applet.timeout = hlua_timeout_applet; |
| 7836 | |
| 7837 | return 0; |
| 7838 | } |
| 7839 | |
| 7840 | /* This function is an LUA binding used for registering |
| 7841 | * "sample-conv" functions. It expects a converter name used |
| 7842 | * in the haproxy configuration file, and an LUA function. |
| 7843 | */ |
| 7844 | __LJMP static int hlua_register_service(lua_State *L) |
| 7845 | { |
| 7846 | struct action_kw_list *akl; |
| 7847 | const char *name; |
| 7848 | const char *env; |
| 7849 | int ref; |
| 7850 | int len; |
| 7851 | struct hlua_function *fcn; |
| 7852 | |
| 7853 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7854 | |
| 7855 | /* First argument : converter name. */ |
| 7856 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7857 | |
| 7858 | /* Second argument : environment. */ |
| 7859 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7860 | |
| 7861 | /* Third argument : lua function. */ |
| 7862 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7863 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7864 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7865 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7866 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7867 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7868 | fcn = calloc(1, sizeof(*fcn)); |
| 7869 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7870 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7871 | |
| 7872 | /* Fill fcn. */ |
| 7873 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7874 | fcn->name = calloc(1, len); |
| 7875 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7876 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7877 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7878 | fcn->function_ref = ref; |
| 7879 | |
| 7880 | /* List head */ |
| 7881 | akl->list.n = akl->list.p = NULL; |
| 7882 | |
| 7883 | /* converter keyword. */ |
| 7884 | len = strlen("lua.") + strlen(name) + 1; |
| 7885 | akl->kw[0].kw = calloc(1, len); |
| 7886 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7887 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7888 | |
| 7889 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7890 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7891 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7892 | if (strcmp(env, "tcp") == 0) |
| 7893 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7894 | else if (strcmp(env, "http") == 0) |
| 7895 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7896 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7897 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7898 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7899 | |
| 7900 | akl->kw[0].match_pfx = 0; |
| 7901 | akl->kw[0].private = fcn; |
| 7902 | |
| 7903 | /* End of array. */ |
| 7904 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7905 | |
| 7906 | /* Register this new converter */ |
| 7907 | service_keywords_register(akl); |
| 7908 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7909 | return 0; |
| 7910 | } |
| 7911 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7912 | /* This function initialises Lua cli handler. It copies the |
| 7913 | * arguments in the Lua stack and create channel IO objects. |
| 7914 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7915 | 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] | 7916 | { |
| 7917 | struct hlua *hlua; |
| 7918 | struct hlua_function *fcn; |
| 7919 | int i; |
| 7920 | const char *error; |
| 7921 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7922 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7923 | appctx->ctx.hlua_cli.fcn = private; |
| 7924 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7925 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7926 | if (!hlua) { |
| 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 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7929 | } |
| 7930 | HLUA_INIT(hlua); |
| 7931 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7932 | |
| 7933 | /* Create task used by signal to wakeup applets. |
| 7934 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7935 | * applet_http. It is absolutely compatible. |
| 7936 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7937 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7938 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7939 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7940 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7941 | } |
| 7942 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7943 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7944 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7945 | |
| 7946 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7947 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7948 | 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] | 7949 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7950 | } |
| 7951 | |
| 7952 | /* The following Lua calls can fail. */ |
| 7953 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7954 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7955 | error = lua_tostring(hlua->T, -1); |
| 7956 | else |
| 7957 | error = "critical error"; |
| 7958 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7959 | goto error; |
| 7960 | } |
| 7961 | |
| 7962 | /* Check stack available size. */ |
| 7963 | if (!lua_checkstack(hlua->T, 2)) { |
| 7964 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7965 | goto error; |
| 7966 | } |
| 7967 | |
| 7968 | /* Restore the function in the stack. */ |
| 7969 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7970 | |
| 7971 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7972 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7973 | */ |
| 7974 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7975 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7976 | goto error; |
| 7977 | } |
| 7978 | hlua->nargs = 1; |
| 7979 | |
| 7980 | /* push keywords in the stack. */ |
| 7981 | for (i = 0; *args[i]; i++) { |
| 7982 | /* Check stack available size. */ |
| 7983 | if (!lua_checkstack(hlua->T, 1)) { |
| 7984 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7985 | goto error; |
| 7986 | } |
| 7987 | lua_pushstring(hlua->T, args[i]); |
| 7988 | hlua->nargs++; |
| 7989 | } |
| 7990 | |
| 7991 | /* We must initialize the execution timeouts. */ |
| 7992 | hlua->max_time = hlua_timeout_session; |
| 7993 | |
| 7994 | /* At this point the execution is safe. */ |
| 7995 | RESET_SAFE_LJMP(hlua->T); |
| 7996 | |
| 7997 | /* It's ok */ |
| 7998 | return 0; |
| 7999 | |
| 8000 | /* It's not ok. */ |
| 8001 | error: |
| 8002 | RESET_SAFE_LJMP(hlua->T); |
| 8003 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 8004 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8005 | return 1; |
| 8006 | } |
| 8007 | |
| 8008 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 8009 | { |
| 8010 | struct hlua *hlua; |
| 8011 | struct stream_interface *si; |
| 8012 | struct hlua_function *fcn; |
| 8013 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8014 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8015 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 8016 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8017 | |
| 8018 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 8019 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 8020 | return 1; |
| 8021 | |
| 8022 | /* Execute the function. */ |
| 8023 | switch (hlua_ctx_resume(hlua, 1)) { |
| 8024 | |
| 8025 | /* finished. */ |
| 8026 | case HLUA_E_OK: |
| 8027 | return 1; |
| 8028 | |
| 8029 | /* yield. */ |
| 8030 | case HLUA_E_AGAIN: |
| 8031 | /* We want write. */ |
| 8032 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 8033 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8034 | /* Set the timeout. */ |
| 8035 | if (hlua->wake_time != TICK_ETERNITY) |
| 8036 | task_schedule(hlua->task, hlua->wake_time); |
| 8037 | return 0; |
| 8038 | |
| 8039 | /* finished with error. */ |
| 8040 | case HLUA_E_ERRMSG: |
| 8041 | /* Display log. */ |
| 8042 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 8043 | fcn->name, lua_tostring(hlua->T, -1)); |
| 8044 | lua_pop(hlua->T, 1); |
| 8045 | return 1; |
| 8046 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8047 | case HLUA_E_ETMOUT: |
| 8048 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 8049 | fcn->name); |
| 8050 | return 1; |
| 8051 | |
| 8052 | case HLUA_E_NOMEM: |
| 8053 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 8054 | fcn->name); |
| 8055 | return 1; |
| 8056 | |
| 8057 | case HLUA_E_YIELD: /* unexpected */ |
| 8058 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 8059 | fcn->name); |
| 8060 | return 1; |
| 8061 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8062 | case HLUA_E_ERR: |
| 8063 | /* Display log. */ |
| 8064 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 8065 | fcn->name); |
| 8066 | return 1; |
| 8067 | |
| 8068 | default: |
| 8069 | return 1; |
| 8070 | } |
| 8071 | |
| 8072 | return 1; |
| 8073 | } |
| 8074 | |
| 8075 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 8076 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8077 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 8078 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8079 | } |
| 8080 | |
| 8081 | /* This function is an LUA binding used for registering |
| 8082 | * new keywords in the cli. It expects a list of keywords |
| 8083 | * which are the "path". It is limited to 5 keywords. A |
| 8084 | * description of the command, a function to be executed |
| 8085 | * for the parsing and a function for io handlers. |
| 8086 | */ |
| 8087 | __LJMP static int hlua_register_cli(lua_State *L) |
| 8088 | { |
| 8089 | struct cli_kw_list *cli_kws; |
| 8090 | const char *message; |
| 8091 | int ref_io; |
| 8092 | int len; |
| 8093 | struct hlua_function *fcn; |
| 8094 | int index; |
| 8095 | int i; |
| 8096 | |
| 8097 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 8098 | |
| 8099 | /* First argument : an array of maximum 5 keywords. */ |
| 8100 | if (!lua_istable(L, 1)) |
| 8101 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 8102 | |
| 8103 | /* Second argument : string with contextual message. */ |
| 8104 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8105 | |
| 8106 | /* Third and fourth argument : lua function. */ |
| 8107 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 8108 | |
| 8109 | /* Allocate and fill the sample fetch keyword struct. */ |
| 8110 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 8111 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8112 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8113 | fcn = calloc(1, sizeof(*fcn)); |
| 8114 | if (!fcn) |
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 | |
| 8117 | /* Fill path. */ |
| 8118 | index = 0; |
| 8119 | lua_pushnil(L); |
| 8120 | while(lua_next(L, 1) != 0) { |
| 8121 | if (index >= 5) |
| 8122 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 8123 | if (lua_type(L, -1) != LUA_TSTRING) |
| 8124 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 8125 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 8126 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8127 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8128 | index++; |
| 8129 | lua_pop(L, 1); |
| 8130 | } |
| 8131 | |
| 8132 | /* Copy help message. */ |
| 8133 | cli_kws->kw[0].usage = strdup(message); |
| 8134 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8135 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8136 | |
| 8137 | /* Fill fcn io handler. */ |
| 8138 | len = strlen("<lua.cli>") + 1; |
| 8139 | for (i = 0; i < index; i++) |
| 8140 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 8141 | fcn->name = calloc(1, len); |
| 8142 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8143 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8144 | strncat((char *)fcn->name, "<lua.cli", len); |
| 8145 | for (i = 0; i < index; i++) { |
| 8146 | strncat((char *)fcn->name, ".", len); |
| 8147 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 8148 | } |
| 8149 | strncat((char *)fcn->name, ">", len); |
| 8150 | fcn->function_ref = ref_io; |
| 8151 | |
| 8152 | /* Fill last entries. */ |
| 8153 | cli_kws->kw[0].private = fcn; |
| 8154 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 8155 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 8156 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 8157 | |
| 8158 | /* Register this new converter */ |
| 8159 | cli_register_kw(cli_kws); |
| 8160 | |
| 8161 | return 0; |
| 8162 | } |
| 8163 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8164 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 8165 | struct proxy *defpx, const char *file, int line, |
| 8166 | char **err, unsigned int *timeout) |
| 8167 | { |
| 8168 | const char *error; |
| 8169 | |
| 8170 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 8171 | if (error && *error != '\0') { |
| 8172 | memprintf(err, "%s: invalid timeout", args[0]); |
| 8173 | return -1; |
| 8174 | } |
| 8175 | return 0; |
| 8176 | } |
| 8177 | |
| 8178 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 8179 | struct proxy *defpx, const char *file, int line, |
| 8180 | char **err) |
| 8181 | { |
| 8182 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8183 | file, line, err, &hlua_timeout_session); |
| 8184 | } |
| 8185 | |
| 8186 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 8187 | struct proxy *defpx, const char *file, int line, |
| 8188 | char **err) |
| 8189 | { |
| 8190 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8191 | file, line, err, &hlua_timeout_task); |
| 8192 | } |
| 8193 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8194 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 8195 | struct proxy *defpx, const char *file, int line, |
| 8196 | char **err) |
| 8197 | { |
| 8198 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 8199 | file, line, err, &hlua_timeout_applet); |
| 8200 | } |
| 8201 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8202 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 8203 | struct proxy *defpx, const char *file, int line, |
| 8204 | char **err) |
| 8205 | { |
| 8206 | char *error; |
| 8207 | |
| 8208 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 8209 | if (*error != '\0') { |
| 8210 | memprintf(err, "%s: invalid number", args[0]); |
| 8211 | return -1; |
| 8212 | } |
| 8213 | return 0; |
| 8214 | } |
| 8215 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8216 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 8217 | struct proxy *defpx, const char *file, int line, |
| 8218 | char **err) |
| 8219 | { |
| 8220 | char *error; |
| 8221 | |
| 8222 | if (*(args[1]) == 0) { |
| 8223 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 8224 | return -1; |
| 8225 | } |
| 8226 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 8227 | if (*error != '\0') { |
| 8228 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 8229 | return -1; |
| 8230 | } |
| 8231 | return 0; |
| 8232 | } |
| 8233 | |
| 8234 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8235 | /* This function is called by the main configuration key "lua-load". It loads and |
| 8236 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 8237 | * the main lua entry point. |
| 8238 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8239 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 8240 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8241 | * |
| 8242 | * In some error case, LUA set an error message in top of the stack. This function |
| 8243 | * 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] | 8244 | * |
| 8245 | * This function can fail with an abort() due to an Lua critical error. |
| 8246 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 8247 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8248 | */ |
| 8249 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 8250 | struct proxy *defpx, const char *file, int line, |
| 8251 | char **err) |
| 8252 | { |
| 8253 | int error; |
| 8254 | |
| 8255 | /* Just load and compile the file. */ |
| 8256 | error = luaL_loadfile(gL.T, args[1]); |
| 8257 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8258 | 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] | 8259 | lua_pop(gL.T, 1); |
| 8260 | return -1; |
| 8261 | } |
| 8262 | |
| 8263 | /* If no syntax error where detected, execute the code. */ |
| 8264 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 8265 | switch (error) { |
| 8266 | case LUA_OK: |
| 8267 | break; |
| 8268 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8269 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8270 | lua_pop(gL.T, 1); |
| 8271 | return -1; |
| 8272 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8273 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8274 | return -1; |
| 8275 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8276 | 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] | 8277 | lua_pop(gL.T, 1); |
| 8278 | return -1; |
| 8279 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8280 | 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] | 8281 | lua_pop(gL.T, 1); |
| 8282 | return -1; |
| 8283 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8284 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8285 | lua_pop(gL.T, 1); |
| 8286 | return -1; |
| 8287 | } |
| 8288 | |
| 8289 | return 0; |
| 8290 | } |
| 8291 | |
| 8292 | /* configuration keywords declaration */ |
| 8293 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8294 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 8295 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 8296 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 8297 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 8298 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8299 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 8300 | { 0, NULL, NULL }, |
| 8301 | }}; |
| 8302 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8303 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 8304 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8305 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8306 | /* This function can fail with an abort() due to an Lua critical error. |
| 8307 | * We are in the initialisation process of HAProxy, this abort() is |
| 8308 | * tolerated. |
| 8309 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8310 | int hlua_post_init() |
| 8311 | { |
| 8312 | struct hlua_init_function *init; |
| 8313 | const char *msg; |
| 8314 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8315 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8316 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8317 | /* Call post initialisation function in safe environement. */ |
| 8318 | if (!SET_SAFE_LJMP(gL.T)) { |
| 8319 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8320 | error = lua_tostring(gL.T, -1); |
| 8321 | else |
| 8322 | error = "critical error"; |
| 8323 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 8324 | exit(1); |
| 8325 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 8326 | |
| 8327 | #if USE_OPENSSL |
| 8328 | /* Initialize SSL server. */ |
| 8329 | if (socket_ssl.xprt->prepare_srv) { |
| 8330 | int saved_used_backed = global.ssl_used_backend; |
| 8331 | // don't affect maxconn automatic computation |
| 8332 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 8333 | global.ssl_used_backend = saved_used_backed; |
| 8334 | } |
| 8335 | #endif |
| 8336 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 8337 | hlua_fcn_post_init(gL.T); |
| 8338 | RESET_SAFE_LJMP(gL.T); |
| 8339 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8340 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 8341 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 8342 | ret = hlua_ctx_resume(&gL, 0); |
| 8343 | switch (ret) { |
| 8344 | case HLUA_E_OK: |
| 8345 | lua_pop(gL.T, -1); |
| 8346 | return 1; |
| 8347 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8348 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8349 | return 0; |
| 8350 | case HLUA_E_ERRMSG: |
| 8351 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8352 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8353 | return 0; |
| 8354 | case HLUA_E_ERR: |
| 8355 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8356 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8357 | return 0; |
| 8358 | } |
| 8359 | } |
| 8360 | return 1; |
| 8361 | } |
| 8362 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 8363 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 8364 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 8365 | * is the previously allocated size or the kind of object in case of a new |
| 8366 | * allocation. <nsize> is the requested new size. |
| 8367 | */ |
| 8368 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 8369 | { |
| 8370 | struct hlua_mem_allocator *zone = ud; |
| 8371 | |
| 8372 | if (nsize == 0) { |
| 8373 | /* it's a free */ |
| 8374 | if (ptr) |
| 8375 | zone->allocated -= osize; |
| 8376 | free(ptr); |
| 8377 | return NULL; |
| 8378 | } |
| 8379 | |
| 8380 | if (!ptr) { |
| 8381 | /* it's a new allocation */ |
| 8382 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 8383 | return NULL; |
| 8384 | |
| 8385 | ptr = malloc(nsize); |
| 8386 | if (ptr) |
| 8387 | zone->allocated += nsize; |
| 8388 | return ptr; |
| 8389 | } |
| 8390 | |
| 8391 | /* it's a realloc */ |
| 8392 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8393 | return NULL; |
| 8394 | |
| 8395 | ptr = realloc(ptr, nsize); |
| 8396 | if (ptr) |
| 8397 | zone->allocated += nsize - osize; |
| 8398 | return ptr; |
| 8399 | } |
| 8400 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8401 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8402 | * We are in the initialisation process of HAProxy, this abort() is |
| 8403 | * tolerated. |
| 8404 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8405 | void hlua_init(void) |
| 8406 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8407 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8408 | int idx; |
| 8409 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8410 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8411 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8412 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8413 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8414 | struct srv_kw *kw; |
| 8415 | int tmp_error; |
| 8416 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8417 | char *args[] = { /* SSL client configuration. */ |
| 8418 | "ssl", |
| 8419 | "verify", |
| 8420 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8421 | NULL |
| 8422 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8423 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8424 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8425 | /* Init main lua stack. */ |
| 8426 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8427 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8428 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8429 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8430 | hlua_sethlua(&gL); |
| 8431 | gL.Tref = LUA_REFNIL; |
| 8432 | gL.task = NULL; |
| 8433 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8434 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8435 | * the Lua function can fail with an abort. We are in the initialisation |
| 8436 | * process of HAProxy, this abort() is tolerated. |
| 8437 | */ |
| 8438 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8439 | /* Initialise lua. */ |
| 8440 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8441 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8442 | /* Set safe environment for the initialisation. */ |
| 8443 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8444 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8445 | error_msg = lua_tostring(gL.T, -1); |
| 8446 | else |
| 8447 | error_msg = "critical error"; |
| 8448 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8449 | exit(1); |
| 8450 | } |
| 8451 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8452 | /* |
| 8453 | * |
| 8454 | * Create "core" object. |
| 8455 | * |
| 8456 | */ |
| 8457 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8458 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8459 | lua_newtable(gL.T); |
| 8460 | |
| 8461 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8462 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8463 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8464 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8465 | /* Register special functions. */ |
| 8466 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8467 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8468 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8469 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8470 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8471 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8472 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8473 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8474 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8475 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8476 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8477 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8478 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8479 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8480 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8481 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8482 | hlua_class_function(gL.T, "log", hlua_log); |
| 8483 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8484 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8485 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8486 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8487 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8488 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8489 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8490 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8491 | |
| 8492 | /* |
| 8493 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8494 | * Register class Map |
| 8495 | * |
| 8496 | */ |
| 8497 | |
| 8498 | /* This table entry is the object "Map" base. */ |
| 8499 | lua_newtable(gL.T); |
| 8500 | |
| 8501 | /* register pattern types. */ |
| 8502 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8503 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8504 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8505 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8506 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8507 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8508 | |
| 8509 | /* register constructor. */ |
| 8510 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8511 | |
| 8512 | /* Create and fill the metatable. */ |
| 8513 | lua_newtable(gL.T); |
| 8514 | |
| 8515 | /* Create and fille the __index entry. */ |
| 8516 | lua_pushstring(gL.T, "__index"); |
| 8517 | lua_newtable(gL.T); |
| 8518 | |
| 8519 | /* Register . */ |
| 8520 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8521 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8522 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8523 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8524 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8525 | /* Register previous table in the registry with reference and named entry. |
| 8526 | * The function hlua_register_metatable() pops the stack, so we |
| 8527 | * previously create a copy of the table. |
| 8528 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8529 | 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] | 8530 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8531 | |
| 8532 | /* Assign the metatable to the mai Map object. */ |
| 8533 | lua_setmetatable(gL.T, -2); |
| 8534 | |
| 8535 | /* Set a name to the table. */ |
| 8536 | lua_setglobal(gL.T, "Map"); |
| 8537 | |
| 8538 | /* |
| 8539 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8540 | * Register class Channel |
| 8541 | * |
| 8542 | */ |
| 8543 | |
| 8544 | /* Create and fill the metatable. */ |
| 8545 | lua_newtable(gL.T); |
| 8546 | |
| 8547 | /* Create and fille the __index entry. */ |
| 8548 | lua_pushstring(gL.T, "__index"); |
| 8549 | lua_newtable(gL.T); |
| 8550 | |
| 8551 | /* Register . */ |
| 8552 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8553 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8554 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8555 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8556 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8557 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8558 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8559 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8560 | 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] | 8561 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8562 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8563 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8564 | |
| 8565 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8566 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8567 | |
| 8568 | /* |
| 8569 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8570 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8571 | * |
| 8572 | */ |
| 8573 | |
| 8574 | /* Create and fill the metatable. */ |
| 8575 | lua_newtable(gL.T); |
| 8576 | |
| 8577 | /* Create and fille the __index entry. */ |
| 8578 | lua_pushstring(gL.T, "__index"); |
| 8579 | lua_newtable(gL.T); |
| 8580 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8581 | /* Browse existing fetches and create the associated |
| 8582 | * object method. |
| 8583 | */ |
| 8584 | sf = NULL; |
| 8585 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8586 | |
| 8587 | /* Dont register the keywork if the arguments check function are |
| 8588 | * not safe during the runtime. |
| 8589 | */ |
| 8590 | if ((sf->val_args != NULL) && |
| 8591 | (sf->val_args != val_payload_lv) && |
| 8592 | (sf->val_args != val_hdr)) |
| 8593 | continue; |
| 8594 | |
| 8595 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8596 | * by an underscore. |
| 8597 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8598 | strncpy(trash.area, sf->kw, trash.size); |
| 8599 | trash.area[trash.size - 1] = '\0'; |
| 8600 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8601 | if (*p == '.' || *p == '-' || *p == '+') |
| 8602 | *p = '_'; |
| 8603 | |
| 8604 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8605 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8606 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8607 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8608 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8609 | } |
| 8610 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8611 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8612 | |
| 8613 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8614 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8615 | |
| 8616 | /* |
| 8617 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8618 | * Register class Converters |
| 8619 | * |
| 8620 | */ |
| 8621 | |
| 8622 | /* Create and fill the metatable. */ |
| 8623 | lua_newtable(gL.T); |
| 8624 | |
| 8625 | /* Create and fill the __index entry. */ |
| 8626 | lua_pushstring(gL.T, "__index"); |
| 8627 | lua_newtable(gL.T); |
| 8628 | |
| 8629 | /* Browse existing converters and create the associated |
| 8630 | * object method. |
| 8631 | */ |
| 8632 | sc = NULL; |
| 8633 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8634 | /* Dont register the keywork if the arguments check function are |
| 8635 | * not safe during the runtime. |
| 8636 | */ |
| 8637 | if (sc->val_args != NULL) |
| 8638 | continue; |
| 8639 | |
| 8640 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8641 | * by an underscore. |
| 8642 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8643 | strncpy(trash.area, sc->kw, trash.size); |
| 8644 | trash.area[trash.size - 1] = '\0'; |
| 8645 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8646 | if (*p == '.' || *p == '-' || *p == '+') |
| 8647 | *p = '_'; |
| 8648 | |
| 8649 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8650 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8651 | lua_pushlightuserdata(gL.T, sc); |
| 8652 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8653 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8654 | } |
| 8655 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8656 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8657 | |
| 8658 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8659 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8660 | |
| 8661 | /* |
| 8662 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8663 | * Register class HTTP |
| 8664 | * |
| 8665 | */ |
| 8666 | |
| 8667 | /* Create and fill the metatable. */ |
| 8668 | lua_newtable(gL.T); |
| 8669 | |
| 8670 | /* Create and fille the __index entry. */ |
| 8671 | lua_pushstring(gL.T, "__index"); |
| 8672 | lua_newtable(gL.T); |
| 8673 | |
| 8674 | /* Register Lua functions. */ |
| 8675 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8676 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8677 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8678 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8679 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8680 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8681 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8682 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8683 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8684 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8685 | |
| 8686 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8687 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8688 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8689 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8690 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8691 | 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] | 8692 | 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] | 8693 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8694 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8695 | |
| 8696 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8697 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8698 | |
| 8699 | /* |
| 8700 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8701 | * Register class AppletTCP |
| 8702 | * |
| 8703 | */ |
| 8704 | |
| 8705 | /* Create and fill the metatable. */ |
| 8706 | lua_newtable(gL.T); |
| 8707 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8708 | /* Create and fille the __index entry. */ |
| 8709 | lua_pushstring(gL.T, "__index"); |
| 8710 | lua_newtable(gL.T); |
| 8711 | |
| 8712 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8713 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8714 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8715 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8716 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8717 | 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] | 8718 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8719 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8720 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8721 | |
| 8722 | lua_settable(gL.T, -3); |
| 8723 | |
| 8724 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8725 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8726 | |
| 8727 | /* |
| 8728 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8729 | * Register class AppletHTTP |
| 8730 | * |
| 8731 | */ |
| 8732 | |
| 8733 | /* Create and fill the metatable. */ |
| 8734 | lua_newtable(gL.T); |
| 8735 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8736 | /* Create and fille the __index entry. */ |
| 8737 | lua_pushstring(gL.T, "__index"); |
| 8738 | lua_newtable(gL.T); |
| 8739 | |
| 8740 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8741 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8742 | 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] | 8743 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8744 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8745 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8746 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8747 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8748 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8749 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8750 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8751 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8752 | |
| 8753 | lua_settable(gL.T, -3); |
| 8754 | |
| 8755 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8756 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8757 | |
| 8758 | /* |
| 8759 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8760 | * Register class TXN |
| 8761 | * |
| 8762 | */ |
| 8763 | |
| 8764 | /* Create and fill the metatable. */ |
| 8765 | lua_newtable(gL.T); |
| 8766 | |
| 8767 | /* Create and fille the __index entry. */ |
| 8768 | lua_pushstring(gL.T, "__index"); |
| 8769 | lua_newtable(gL.T); |
| 8770 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8771 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8772 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8773 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8774 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8775 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8776 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8777 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 8778 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8779 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8780 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8781 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8782 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8783 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8784 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8785 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8786 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8787 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8788 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8789 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8790 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8791 | |
| 8792 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8793 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8794 | |
| 8795 | /* |
| 8796 | * |
| 8797 | * Register class Socket |
| 8798 | * |
| 8799 | */ |
| 8800 | |
| 8801 | /* Create and fill the metatable. */ |
| 8802 | lua_newtable(gL.T); |
| 8803 | |
| 8804 | /* Create and fille the __index entry. */ |
| 8805 | lua_pushstring(gL.T, "__index"); |
| 8806 | lua_newtable(gL.T); |
| 8807 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8808 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8809 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8810 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8811 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8812 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8813 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8814 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8815 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8816 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8817 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8818 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8819 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8820 | 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] | 8821 | |
| 8822 | /* Register the garbage collector entry. */ |
| 8823 | lua_pushstring(gL.T, "__gc"); |
| 8824 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8825 | 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] | 8826 | |
| 8827 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8828 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8829 | |
| 8830 | /* Proxy and server configuration initialisation. */ |
| 8831 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8832 | init_new_proxy(&socket_proxy); |
| 8833 | socket_proxy.parent = NULL; |
| 8834 | socket_proxy.last_change = now.tv_sec; |
| 8835 | socket_proxy.id = "LUA-SOCKET"; |
| 8836 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8837 | socket_proxy.maxconn = 0; |
| 8838 | socket_proxy.accept = NULL; |
| 8839 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8840 | socket_proxy.srv = NULL; |
| 8841 | socket_proxy.conn_retries = 0; |
| 8842 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8843 | |
| 8844 | /* Init TCP server: unchanged parameters */ |
| 8845 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8846 | socket_tcp.next = NULL; |
| 8847 | socket_tcp.proxy = &socket_proxy; |
| 8848 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8849 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8850 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8851 | socket_tcp.priv_conns = NULL; |
| 8852 | socket_tcp.idle_conns = NULL; |
| 8853 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8854 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8855 | socket_tcp.last_change = 0; |
| 8856 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8857 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8858 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8859 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8860 | |
| 8861 | /* XXX: Copy default parameter from default server, |
| 8862 | * but the default server is not initialized. |
| 8863 | */ |
| 8864 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8865 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8866 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8867 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8868 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8869 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8870 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8871 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8872 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8873 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8874 | |
| 8875 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8876 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8877 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8878 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8879 | socket_tcp.check.server = &socket_tcp; |
| 8880 | |
| 8881 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8882 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8883 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8884 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8885 | socket_tcp.agent.server = &socket_tcp; |
| 8886 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8887 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8888 | |
| 8889 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8890 | /* Init TCP server: unchanged parameters */ |
| 8891 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8892 | socket_ssl.next = NULL; |
| 8893 | socket_ssl.proxy = &socket_proxy; |
| 8894 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8895 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8896 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8897 | socket_ssl.priv_conns = NULL; |
| 8898 | socket_ssl.idle_conns = NULL; |
| 8899 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8900 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8901 | socket_ssl.last_change = 0; |
| 8902 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8903 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8904 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8905 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8906 | |
| 8907 | /* XXX: Copy default parameter from default server, |
| 8908 | * but the default server is not initialized. |
| 8909 | */ |
| 8910 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8911 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8912 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8913 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8914 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8915 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8916 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8917 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8918 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8919 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8920 | |
| 8921 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8922 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8923 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8924 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8925 | socket_ssl.check.server = &socket_ssl; |
| 8926 | |
| 8927 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8928 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8929 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8930 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8931 | socket_ssl.agent.server = &socket_ssl; |
| 8932 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8933 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8934 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8935 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8936 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8937 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8938 | /* |
| 8939 | * |
| 8940 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8941 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8942 | * features like client certificates and ssl_verify. |
| 8943 | * |
| 8944 | */ |
| 8945 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8946 | if (tmp_error != 0) { |
| 8947 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8948 | abort(); /* This must be never arrives because the command line |
| 8949 | not editable by the user. */ |
| 8950 | } |
| 8951 | idx += kw->skip; |
| 8952 | } |
| 8953 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8954 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8955 | |
| 8956 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8957 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8958 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8959 | static void hlua_register_build_options(void) |
| 8960 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8961 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8962 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8963 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8964 | hap_register_build_opts(ptr, 1); |
| 8965 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8966 | |
| 8967 | INITCALL0(STG_REGISTER, hlua_register_build_options); |