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> |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 28 | #include <common/xref.h> |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 29 | #include <common/hathreads.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 30 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 31 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 32 | #include <types/hlua.h> |
| 33 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 34 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 35 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 36 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 37 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 38 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 39 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 40 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 41 | #include <proto/stats.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 42 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 43 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 44 | #include <proto/hlua_fcn.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 45 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 46 | #include <proto/obj_type.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 47 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 48 | #include <proto/payload.h> |
| 49 | #include <proto/proto_http.h> |
| 50 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 51 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 52 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 53 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 54 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 55 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 56 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 57 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 58 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 59 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 60 | * macro is used only for identifying the function that can |
| 61 | * not return because a longjmp is executed. |
| 62 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 63 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 64 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 65 | */ |
| 66 | #define __LJMP |
| 67 | #define WILL_LJMP(func) func |
| 68 | #define MAY_LJMP(func) func |
| 69 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 70 | /* This couple of function executes securely some Lua calls outside of |
| 71 | * the lua runtime environment. Each Lua call can return a longjmp |
| 72 | * if it encounter a memory error. |
| 73 | * |
| 74 | * Lua documentation extract: |
| 75 | * |
| 76 | * If an error happens outside any protected environment, Lua calls |
| 77 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 78 | * exiting the host application. Your panic function can avoid this |
| 79 | * exit by never returning (e.g., doing a long jump to your own |
| 80 | * recovery point outside Lua). |
| 81 | * |
| 82 | * The panic function runs as if it were a message handler (see |
| 83 | * §2.3); in particular, the error message is at the top of the |
| 84 | * stack. However, there is no guarantee about stack space. To push |
| 85 | * anything on the stack, the panic function must first check the |
| 86 | * available space (see §4.2). |
| 87 | * |
| 88 | * We must check all the Lua entry point. This includes: |
| 89 | * - The include/proto/hlua.h exported functions |
| 90 | * - the task wrapper function |
| 91 | * - The action wrapper function |
| 92 | * - The converters wrapper function |
| 93 | * - The sample-fetch wrapper functions |
| 94 | * |
| 95 | * It is tolerated that the initilisation function returns an abort. |
| 96 | * Before each Lua abort, an error message is writed on stderr. |
| 97 | * |
| 98 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 99 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 100 | * because they must be exists in the program stack when the longjmp |
| 101 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 102 | * |
| 103 | * Note that the Lua processing is not really thread safe. It provides |
| 104 | * heavy system which consists to add our own lock function in the Lua |
| 105 | * code and recompile the library. This system will probably not accepted |
| 106 | * by maintainers of various distribs. |
| 107 | * |
| 108 | * Our main excution point of the Lua is the function lua_resume(). A |
| 109 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 110 | * of function and a lua_unlock() at the end of the function. So I |
| 111 | * conclude that the Lua thread safe mode just perform a mutex around |
| 112 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 113 | * easier for distro maintainers. |
| 114 | * |
| 115 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 116 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 117 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 118 | */ |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 119 | __decl_hathreads(HA_SPINLOCK_T hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 120 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 121 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 122 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 123 | |
| 124 | #define SET_SAFE_LJMP(__L) \ |
| 125 | ({ \ |
| 126 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 127 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 128 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 129 | lua_atpanic(__L, hlua_panic_safe); \ |
| 130 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 131 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 132 | } else { \ |
| 133 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 134 | ret = 1; \ |
| 135 | } \ |
| 136 | ret; \ |
| 137 | }) |
| 138 | |
| 139 | /* If we are the last function catching Lua errors, we |
| 140 | * must reset the panic function. |
| 141 | */ |
| 142 | #define RESET_SAFE_LJMP(__L) \ |
| 143 | do { \ |
| 144 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 145 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 146 | } while(0) |
| 147 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 148 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 149 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
| 150 | #define APPLET_100C 0x02 /* 100 continue expected. */ |
| 151 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 152 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 153 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 154 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 155 | |
| 156 | #define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n" |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 157 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 158 | /* The main Lua execution context. */ |
| 159 | struct hlua gL; |
| 160 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 161 | /* This is the memory pool containing struct lua for applets |
| 162 | * (including cli). |
| 163 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 164 | struct pool_head *pool_head_hlua; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 165 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 166 | /* Used for Socket connection. */ |
| 167 | static struct proxy socket_proxy; |
| 168 | static struct server socket_tcp; |
| 169 | #ifdef USE_OPENSSL |
| 170 | static struct server socket_ssl; |
| 171 | #endif |
| 172 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 173 | /* List head of the function called at the initialisation time. */ |
| 174 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 175 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 176 | /* The following variables contains the reference of the different |
| 177 | * Lua classes. These references are useful for identify metadata |
| 178 | * associated with an object. |
| 179 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 180 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 181 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 182 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 183 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 184 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 185 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 186 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 187 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 188 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 189 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 190 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 191 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 192 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 193 | * because a task may remain alive during all the haproxy execution. |
| 194 | */ |
| 195 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 196 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 197 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 198 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 199 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 200 | * it is used for preventing infinite loops. |
| 201 | * |
| 202 | * I test the scheer with an infinite loop containing one incrementation |
| 203 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 204 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 205 | * to one interrupt each 10 000 instructions. |
| 206 | * |
| 207 | * configured | Number of |
| 208 | * instructions | loops executed |
| 209 | * between two | in milions |
| 210 | * forced yields | |
| 211 | * ---------------+--------------- |
| 212 | * 10 | 160 |
| 213 | * 500 | 670 |
| 214 | * 1000 | 680 |
| 215 | * 5000 | 700 |
| 216 | * 7000 | 700 |
| 217 | * 8000 | 700 |
| 218 | * 9000 | 710 <- ceil |
| 219 | * 10000 | 710 |
| 220 | * 100000 | 710 |
| 221 | * 1000000 | 710 |
| 222 | * |
| 223 | */ |
| 224 | static unsigned int hlua_nb_instruction = 10000; |
| 225 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 226 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 227 | * be enforced on any memory allocation. |
| 228 | */ |
| 229 | struct hlua_mem_allocator { |
| 230 | size_t allocated; |
| 231 | size_t limit; |
| 232 | }; |
| 233 | |
| 234 | static struct hlua_mem_allocator hlua_global_allocator; |
| 235 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 236 | static const char error_500[] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 237 | "HTTP/1.0 500 Internal Server Error\r\n" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 238 | "Cache-Control: no-cache\r\n" |
| 239 | "Connection: close\r\n" |
| 240 | "Content-Type: text/html\r\n" |
| 241 | "\r\n" |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 242 | "<html><body><h1>500 Internal Server Error</h1>\nAn internal server error occured.\n</body></html>\n"; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 243 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 244 | /* These functions converts types between HAProxy internal args or |
| 245 | * sample and LUA types. Another function permits to check if the |
| 246 | * LUA stack contains arguments according with an required ARG_T |
| 247 | * format. |
| 248 | */ |
| 249 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 250 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 251 | __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] | 252 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 253 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 254 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 255 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 256 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 257 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 258 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 259 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 260 | do { \ |
| 261 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 262 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 263 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 264 | } while (0) |
| 265 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 266 | /* Used to check an Lua function type in the stack. It creates and |
| 267 | * returns a reference of the function. This function throws an |
| 268 | * error if the rgument is not a "function". |
| 269 | */ |
| 270 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 271 | { |
| 272 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 273 | 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] | 274 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 275 | } |
| 276 | lua_pushvalue(L, argno); |
| 277 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 278 | } |
| 279 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 280 | /* Return the string that is of the top of the stack. */ |
| 281 | const char *hlua_get_top_error_string(lua_State *L) |
| 282 | { |
| 283 | if (lua_gettop(L) < 1) |
| 284 | return "unknown error"; |
| 285 | if (lua_type(L, -1) != LUA_TSTRING) |
| 286 | return "unknown error"; |
| 287 | return lua_tostring(L, -1); |
| 288 | } |
| 289 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 290 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 291 | { |
| 292 | lua_Debug ar; |
| 293 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 294 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 295 | int filled = 0; |
| 296 | |
| 297 | while (lua_getstack(L, level++, &ar)) { |
| 298 | |
| 299 | /* Add separator */ |
| 300 | if (filled) |
| 301 | chunk_appendf(msg, ", "); |
| 302 | filled = 1; |
| 303 | |
| 304 | /* Fill fields: |
| 305 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 306 | * 'l': fills in the field currentline; |
| 307 | * 'n': fills in the field name and namewhat; |
| 308 | * 't': fills in the field istailcall; |
| 309 | */ |
| 310 | lua_getinfo(L, "Slnt", &ar); |
| 311 | |
| 312 | /* Append code localisation */ |
| 313 | if (ar.currentline > 0) |
| 314 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 315 | else |
| 316 | chunk_appendf(msg, "%s ", ar.short_src); |
| 317 | |
| 318 | /* |
| 319 | * Get function name |
| 320 | * |
| 321 | * if namewhat is no empty, name is defined. |
| 322 | * what contains "Lua" for Lua function, "C" for C function, |
| 323 | * or "main" for main code. |
| 324 | */ |
| 325 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 326 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 327 | |
| 328 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 329 | chunk_appendf(msg, "main chunk"); |
| 330 | |
| 331 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 332 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 333 | |
| 334 | else /* nothing left... */ |
| 335 | chunk_appendf(msg, "?"); |
| 336 | |
| 337 | |
| 338 | /* Display tailed call */ |
| 339 | if (ar.istailcall) |
| 340 | chunk_appendf(msg, " ..."); |
| 341 | } |
| 342 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 343 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 347 | /* This function check the number of arguments available in the |
| 348 | * stack. If the number of arguments available is not the same |
| 349 | * then <nb> an error is throwed. |
| 350 | */ |
| 351 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 352 | { |
| 353 | if (lua_gettop(L) == nb) |
| 354 | return; |
| 355 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 356 | } |
| 357 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 358 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 359 | * and the line number where the error is encountered. |
| 360 | */ |
| 361 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 362 | { |
| 363 | va_list argp; |
| 364 | va_start(argp, fmt); |
| 365 | luaL_where(L, 1); |
| 366 | lua_pushvfstring(L, fmt, argp); |
| 367 | va_end(argp); |
| 368 | lua_concat(L, 2); |
| 369 | return 1; |
| 370 | } |
| 371 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 372 | /* This functions is used with sample fetch and converters. It |
| 373 | * converts the HAProxy configuration argument in a lua stack |
| 374 | * values. |
| 375 | * |
| 376 | * It takes an array of "arg", and each entry of the array is |
| 377 | * converted and pushed in the LUA stack. |
| 378 | */ |
| 379 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 380 | { |
| 381 | switch (arg->type) { |
| 382 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 383 | case ARGT_TIME: |
| 384 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 385 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 386 | break; |
| 387 | |
| 388 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 389 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 390 | break; |
| 391 | |
| 392 | case ARGT_IPV4: |
| 393 | case ARGT_IPV6: |
| 394 | case ARGT_MSK4: |
| 395 | case ARGT_MSK6: |
| 396 | case ARGT_FE: |
| 397 | case ARGT_BE: |
| 398 | case ARGT_TAB: |
| 399 | case ARGT_SRV: |
| 400 | case ARGT_USR: |
| 401 | case ARGT_MAP: |
| 402 | default: |
| 403 | lua_pushnil(L); |
| 404 | break; |
| 405 | } |
| 406 | return 1; |
| 407 | } |
| 408 | |
| 409 | /* This function take one entrie in an LUA stack at the index "ud", |
| 410 | * and try to convert it in an HAProxy argument entry. This is useful |
| 411 | * with sample fetch wrappers. The input arguments are gived to the |
| 412 | * lua wrapper and converted as arg list by thi function. |
| 413 | */ |
| 414 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 415 | { |
| 416 | switch (lua_type(L, ud)) { |
| 417 | |
| 418 | case LUA_TNUMBER: |
| 419 | case LUA_TBOOLEAN: |
| 420 | arg->type = ARGT_SINT; |
| 421 | arg->data.sint = lua_tointeger(L, ud); |
| 422 | break; |
| 423 | |
| 424 | case LUA_TSTRING: |
| 425 | arg->type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 426 | 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] | 427 | break; |
| 428 | |
| 429 | case LUA_TUSERDATA: |
| 430 | case LUA_TNIL: |
| 431 | case LUA_TTABLE: |
| 432 | case LUA_TFUNCTION: |
| 433 | case LUA_TTHREAD: |
| 434 | case LUA_TLIGHTUSERDATA: |
| 435 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 436 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 437 | break; |
| 438 | } |
| 439 | return 1; |
| 440 | } |
| 441 | |
| 442 | /* the following functions are used to convert a struct sample |
| 443 | * in Lua type. This useful to convert the return of the |
| 444 | * fetchs or converters. |
| 445 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 446 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 447 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 448 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 449 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 450 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 451 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 452 | break; |
| 453 | |
| 454 | case SMP_T_BIN: |
| 455 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 456 | 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] | 457 | break; |
| 458 | |
| 459 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 460 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 461 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 462 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 463 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 464 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 465 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 466 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 467 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 468 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 469 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 470 | 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] | 471 | break; |
| 472 | default: |
| 473 | lua_pushnil(L); |
| 474 | break; |
| 475 | } |
| 476 | break; |
| 477 | |
| 478 | case SMP_T_IPV4: |
| 479 | case SMP_T_IPV6: |
| 480 | 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] | 481 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 482 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 483 | 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] | 484 | else |
| 485 | lua_pushnil(L); |
| 486 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 487 | default: |
| 488 | lua_pushnil(L); |
| 489 | break; |
| 490 | } |
| 491 | return 1; |
| 492 | } |
| 493 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 494 | /* the following functions are used to convert a struct sample |
| 495 | * in Lua strings. This is useful to convert the return of the |
| 496 | * fetchs or converters. |
| 497 | */ |
| 498 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 499 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 500 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 501 | |
| 502 | case SMP_T_BIN: |
| 503 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 504 | 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] | 505 | break; |
| 506 | |
| 507 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 508 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 509 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 510 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 511 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 512 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 513 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 514 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 515 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 516 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 517 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 518 | 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] | 519 | break; |
| 520 | default: |
| 521 | lua_pushstring(L, ""); |
| 522 | break; |
| 523 | } |
| 524 | break; |
| 525 | |
| 526 | case SMP_T_SINT: |
| 527 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 528 | case SMP_T_IPV4: |
| 529 | case SMP_T_IPV6: |
| 530 | 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] | 531 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 532 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 533 | 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] | 534 | else |
| 535 | lua_pushstring(L, ""); |
| 536 | break; |
| 537 | default: |
| 538 | lua_pushstring(L, ""); |
| 539 | break; |
| 540 | } |
| 541 | return 1; |
| 542 | } |
| 543 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 544 | /* the following functions are used to convert an Lua type in a |
| 545 | * struct sample. This is useful to provide data from a converter |
| 546 | * to the LUA code. |
| 547 | */ |
| 548 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 549 | { |
| 550 | switch (lua_type(L, ud)) { |
| 551 | |
| 552 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 553 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 554 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 555 | break; |
| 556 | |
| 557 | |
| 558 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 559 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 560 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 561 | break; |
| 562 | |
| 563 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 564 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 565 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 566 | 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] | 567 | break; |
| 568 | |
| 569 | case LUA_TUSERDATA: |
| 570 | case LUA_TNIL: |
| 571 | case LUA_TTABLE: |
| 572 | case LUA_TFUNCTION: |
| 573 | case LUA_TTHREAD: |
| 574 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 575 | case LUA_TNONE: |
| 576 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 577 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 578 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 579 | break; |
| 580 | } |
| 581 | return 1; |
| 582 | } |
| 583 | |
| 584 | /* This function check the "argp" builded by another conversion function |
| 585 | * is in accord with the expected argp defined by the "mask". The fucntion |
| 586 | * 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] | 587 | * |
| 588 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 589 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 590 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 591 | __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] | 592 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 593 | { |
| 594 | int min_arg; |
| 595 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 596 | struct proxy *px; |
| 597 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 598 | |
| 599 | idx = 0; |
| 600 | min_arg = ARGM(mask); |
| 601 | mask >>= ARGM_BITS; |
| 602 | |
| 603 | while (1) { |
| 604 | |
| 605 | /* Check oversize. */ |
| 606 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 607 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | /* Check for mandatory arguments. */ |
| 611 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 612 | if (idx < min_arg) { |
| 613 | |
| 614 | /* If miss other argument than the first one, we return an error. */ |
| 615 | if (idx > 0) |
| 616 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 617 | |
| 618 | /* If first argument have a certain type, some default values |
| 619 | * may be used. See the function smp_resolve_args(). |
| 620 | */ |
| 621 | switch (mask & ARGT_MASK) { |
| 622 | |
| 623 | case ARGT_FE: |
| 624 | if (!(p->cap & PR_CAP_FE)) |
| 625 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 626 | argp[idx].data.prx = p; |
| 627 | argp[idx].type = ARGT_FE; |
| 628 | argp[idx+1].type = ARGT_STOP; |
| 629 | break; |
| 630 | |
| 631 | case ARGT_BE: |
| 632 | if (!(p->cap & PR_CAP_BE)) |
| 633 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 634 | argp[idx].data.prx = p; |
| 635 | argp[idx].type = ARGT_BE; |
| 636 | argp[idx+1].type = ARGT_STOP; |
| 637 | break; |
| 638 | |
| 639 | case ARGT_TAB: |
| 640 | argp[idx].data.prx = p; |
| 641 | argp[idx].type = ARGT_TAB; |
| 642 | argp[idx+1].type = ARGT_STOP; |
| 643 | break; |
| 644 | |
| 645 | default: |
| 646 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 647 | break; |
| 648 | } |
| 649 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | /* Check for exceed the number of requiered argument. */ |
| 654 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 655 | argp[idx].type != ARGT_STOP) { |
| 656 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 657 | } |
| 658 | |
| 659 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 660 | argp[idx].type == ARGT_STOP) { |
| 661 | return 0; |
| 662 | } |
| 663 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 664 | /* Convert some argument types. */ |
| 665 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 666 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 667 | if (argp[idx].type != ARGT_SINT) |
| 668 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 669 | argp[idx].type = ARGT_SINT; |
| 670 | break; |
| 671 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 672 | case ARGT_TIME: |
| 673 | if (argp[idx].type != ARGT_SINT) |
| 674 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 675 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 676 | break; |
| 677 | |
| 678 | case ARGT_SIZE: |
| 679 | if (argp[idx].type != ARGT_SINT) |
| 680 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 681 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 682 | break; |
| 683 | |
| 684 | case ARGT_FE: |
| 685 | if (argp[idx].type != ARGT_STR) |
| 686 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 687 | memcpy(trash.area, argp[idx].data.str.area, |
| 688 | argp[idx].data.str.data); |
| 689 | trash.area[argp[idx].data.str.data] = 0; |
| 690 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 691 | if (!argp[idx].data.prx) |
| 692 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 693 | argp[idx].type = ARGT_FE; |
| 694 | break; |
| 695 | |
| 696 | case ARGT_BE: |
| 697 | if (argp[idx].type != ARGT_STR) |
| 698 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 699 | memcpy(trash.area, argp[idx].data.str.area, |
| 700 | argp[idx].data.str.data); |
| 701 | trash.area[argp[idx].data.str.data] = 0; |
| 702 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 703 | if (!argp[idx].data.prx) |
| 704 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 705 | argp[idx].type = ARGT_BE; |
| 706 | break; |
| 707 | |
| 708 | case ARGT_TAB: |
| 709 | if (argp[idx].type != ARGT_STR) |
| 710 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 711 | memcpy(trash.area, argp[idx].data.str.area, |
| 712 | argp[idx].data.str.data); |
| 713 | trash.area[argp[idx].data.str.data] = 0; |
| 714 | argp[idx].data.prx = proxy_tbl_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 715 | if (!argp[idx].data.prx) |
| 716 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 717 | argp[idx].type = ARGT_TAB; |
| 718 | break; |
| 719 | |
| 720 | case ARGT_SRV: |
| 721 | if (argp[idx].type != ARGT_STR) |
| 722 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 723 | memcpy(trash.area, argp[idx].data.str.area, |
| 724 | argp[idx].data.str.data); |
| 725 | trash.area[argp[idx].data.str.data] = 0; |
| 726 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 727 | if (sname) { |
| 728 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 729 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 730 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 731 | if (!px) |
| 732 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 733 | } |
| 734 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 735 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 736 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 737 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 738 | argp[idx].data.srv = findserver(px, sname); |
| 739 | if (!argp[idx].data.srv) |
| 740 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 741 | argp[idx].type = ARGT_SRV; |
| 742 | break; |
| 743 | |
| 744 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 745 | memcpy(trash.area, argp[idx].data.str.area, |
| 746 | argp[idx].data.str.data); |
| 747 | trash.area[argp[idx].data.str.data] = 0; |
| 748 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 749 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 750 | argp[idx].type = ARGT_IPV4; |
| 751 | break; |
| 752 | |
| 753 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 754 | memcpy(trash.area, argp[idx].data.str.area, |
| 755 | argp[idx].data.str.data); |
| 756 | trash.area[argp[idx].data.str.data] = 0; |
| 757 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 758 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 759 | argp[idx].type = ARGT_MSK4; |
| 760 | break; |
| 761 | |
| 762 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 763 | memcpy(trash.area, argp[idx].data.str.area, |
| 764 | argp[idx].data.str.data); |
| 765 | trash.area[argp[idx].data.str.data] = 0; |
| 766 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 767 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 768 | argp[idx].type = ARGT_IPV6; |
| 769 | break; |
| 770 | |
| 771 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 772 | memcpy(trash.area, argp[idx].data.str.area, |
| 773 | argp[idx].data.str.data); |
| 774 | trash.area[argp[idx].data.str.data] = 0; |
| 775 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 776 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 777 | argp[idx].type = ARGT_MSK6; |
| 778 | break; |
| 779 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 780 | case ARGT_MAP: |
| 781 | case ARGT_REG: |
| 782 | case ARGT_USR: |
| 783 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 784 | break; |
| 785 | } |
| 786 | |
| 787 | /* Check for type of argument. */ |
| 788 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 789 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 790 | arg_type_names[(mask & ARGT_MASK)], |
| 791 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 792 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 793 | } |
| 794 | |
| 795 | /* Next argument. */ |
| 796 | mask >>= ARGT_BITS; |
| 797 | idx++; |
| 798 | } |
| 799 | } |
| 800 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 801 | /* |
| 802 | * The following functions are used to make correspondance between the the |
| 803 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 804 | * |
| 805 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 806 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 807 | */ |
| 808 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 809 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 810 | struct hlua **hlua = lua_getextraspace(L); |
| 811 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 812 | } |
| 813 | static inline void hlua_sethlua(struct hlua *hlua) |
| 814 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 815 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 816 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 817 | } |
| 818 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 819 | /* This function is used to send logs. It try to send on screen (stderr) |
| 820 | * and on the default syslog server. |
| 821 | */ |
| 822 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 823 | { |
| 824 | struct tm tm; |
| 825 | char *p; |
| 826 | |
| 827 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 828 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 829 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 830 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 831 | /* Break the message if exceed the buffer size. */ |
| 832 | *(p-4) = ' '; |
| 833 | *(p-3) = '.'; |
| 834 | *(p-2) = '.'; |
| 835 | *(p-1) = '.'; |
| 836 | break; |
| 837 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 838 | if (isprint(*msg)) |
| 839 | *p = *msg; |
| 840 | else |
| 841 | *p = '.'; |
| 842 | } |
| 843 | *p = '\0'; |
| 844 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 845 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 846 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 847 | get_localtime(date.tv_sec, &tm); |
| 848 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 849 | 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] | 850 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 851 | fflush(stderr); |
| 852 | } |
| 853 | } |
| 854 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 855 | /* This function just ensure that the yield will be always |
| 856 | * returned with a timeout and permit to set some flags |
| 857 | */ |
| 858 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 859 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 860 | { |
| 861 | struct hlua *hlua = hlua_gethlua(L); |
| 862 | |
| 863 | /* Set the wake timeout. If timeout is required, we set |
| 864 | * the expiration time. |
| 865 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 866 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 867 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 868 | hlua->flags |= flags; |
| 869 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 870 | /* Process the yield. */ |
| 871 | WILL_LJMP(lua_yieldk(L, nresults, ctx, k)); |
| 872 | } |
| 873 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 874 | /* This function initialises the Lua environment stored in the stream. |
| 875 | * 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] | 876 | * 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] | 877 | * |
| 878 | * This function is particular. it initialises a new Lua thread. If the |
| 879 | * initialisation fails (example: out of memory error), the lua function |
| 880 | * throws an error (longjmp). |
| 881 | * |
| 882 | * This function manipulates two Lua stack: the main and the thread. Only |
| 883 | * the main stack can fail. The thread is not manipulated. This function |
| 884 | * MUST NOT manipulate the created thread stack state, because is not |
| 885 | * proctected agains error throwed by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 886 | */ |
| 887 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 888 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 889 | if (!SET_SAFE_LJMP(gL.T)) { |
| 890 | lua->Tref = LUA_REFNIL; |
| 891 | return 0; |
| 892 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 893 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 894 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 895 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 896 | lua->T = lua_newthread(gL.T); |
| 897 | if (!lua->T) { |
| 898 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | 0a97620 | 2017-07-12 11:18:00 +0200 | [diff] [blame] | 899 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 900 | return 0; |
| 901 | } |
| 902 | hlua_sethlua(lua); |
| 903 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 904 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 905 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 906 | return 1; |
| 907 | } |
| 908 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 909 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 910 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 911 | * is not freed. |
| 912 | */ |
| 913 | void hlua_ctx_destroy(struct hlua *lua) |
| 914 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 915 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 916 | return; |
| 917 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 918 | if (!lua->T) |
| 919 | goto end; |
| 920 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 921 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 922 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 923 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 924 | if (!SET_SAFE_LJMP(lua->T)) |
| 925 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 926 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 927 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 928 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 929 | if (!SET_SAFE_LJMP(gL.T)) |
| 930 | return; |
| 931 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 932 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 933 | /* Forces a garbage collecting process. If the Lua program is finished |
| 934 | * without error, we run the GC on the thread pointer. Its freed all |
| 935 | * the unused memory. |
| 936 | * If the thread is finnish with an error or is currently yielded, |
| 937 | * it seems that the GC applied on the thread doesn't clean anything, |
| 938 | * so e run the GC on the main thread. |
| 939 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 940 | * the garbage collection. |
| 941 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 942 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 943 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 944 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 945 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 946 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 947 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 948 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 949 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 950 | |
| 951 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 952 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | /* This function is used to restore the Lua context when a coroutine |
| 956 | * fails. This function copy the common memory between old coroutine |
| 957 | * and the new coroutine. The old coroutine is destroyed, and its |
| 958 | * replaced by the new coroutine. |
| 959 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 960 | * as string error message and it is copied in the new stack. |
| 961 | */ |
| 962 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 963 | { |
| 964 | lua_State *T; |
| 965 | int new_ref; |
| 966 | |
| 967 | /* Renew the main LUA stack doesn't have sense. */ |
| 968 | if (lua == &gL) |
| 969 | return 0; |
| 970 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 971 | /* New Lua coroutine. */ |
| 972 | T = lua_newthread(gL.T); |
| 973 | if (!T) |
| 974 | return 0; |
| 975 | |
| 976 | /* Copy last error message. */ |
| 977 | if (keep_msg) |
| 978 | lua_xmove(lua->T, T, 1); |
| 979 | |
| 980 | /* Copy data between the coroutines. */ |
| 981 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 982 | lua_xmove(lua->T, T, 1); |
| 983 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 984 | |
| 985 | /* Destroy old data. */ |
| 986 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 987 | |
| 988 | /* The thread is garbage collected by Lua. */ |
| 989 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 990 | |
| 991 | /* Fill the struct with the new coroutine values. */ |
| 992 | lua->Mref = new_ref; |
| 993 | lua->T = T; |
| 994 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 995 | |
| 996 | /* Set context. */ |
| 997 | hlua_sethlua(lua); |
| 998 | |
| 999 | return 1; |
| 1000 | } |
| 1001 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1002 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1003 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1004 | struct hlua *hlua = hlua_gethlua(L); |
| 1005 | |
| 1006 | /* Lua cannot yield when its returning from a function, |
| 1007 | * so, we can fix the interrupt hook to 1 instruction, |
| 1008 | * expecting that the function is finnished. |
| 1009 | */ |
| 1010 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1011 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1012 | return; |
| 1013 | } |
| 1014 | |
| 1015 | /* restore the interrupt condition. */ |
| 1016 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1017 | |
| 1018 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1019 | * If the state is not yieldable, trying yield causes an error. |
| 1020 | */ |
| 1021 | if (lua_isyieldable(L)) |
| 1022 | WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 1023 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1024 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1025 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1026 | hlua->run_time += now_ms - hlua->start_time; |
| 1027 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1028 | lua_pushfstring(L, "execution timeout"); |
| 1029 | WILL_LJMP(lua_error(L)); |
| 1030 | } |
| 1031 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1032 | /* Update the start time. */ |
| 1033 | hlua->start_time = now_ms; |
| 1034 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1035 | /* Try to interrupt the process at the end of the current |
| 1036 | * unyieldable function. |
| 1037 | */ |
| 1038 | 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] | 1039 | } |
| 1040 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1041 | /* This function start or resumes the Lua stack execution. If the flag |
| 1042 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1043 | * The function return an error. |
| 1044 | * |
| 1045 | * The function can returns 4 values: |
| 1046 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1047 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1048 | * task wakeup. |
| 1049 | * - HLUA_E_ERRMSG : An error has occured, an error message is set in |
| 1050 | * the top of the stack. |
| 1051 | * - HLUA_E_ERR : An error has occured without error message. |
| 1052 | * |
| 1053 | * If an error occured, the stack is renewed and it is ready to run new |
| 1054 | * LUA code. |
| 1055 | */ |
| 1056 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1057 | { |
| 1058 | int ret; |
| 1059 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1060 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1061 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1062 | /* Initialise run time counter. */ |
| 1063 | if (!HLUA_IS_RUNNING(lua)) |
| 1064 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1065 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1066 | /* Lock the whole Lua execution. This lock must be before the |
| 1067 | * label "resume_execution". |
| 1068 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1069 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1070 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1071 | resume_execution: |
| 1072 | |
| 1073 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1074 | * instructions. it is used for preventing infinite loops. |
| 1075 | */ |
| 1076 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1077 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1078 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1079 | HLUA_SET_RUN(lua); |
| 1080 | HLUA_CLR_CTRLYIELD(lua); |
| 1081 | HLUA_CLR_WAKERESWR(lua); |
| 1082 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1083 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1084 | /* Update the start time. */ |
| 1085 | lua->start_time = now_ms; |
| 1086 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1087 | /* Call the function. */ |
| 1088 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1089 | switch (ret) { |
| 1090 | |
| 1091 | case LUA_OK: |
| 1092 | ret = HLUA_E_OK; |
| 1093 | break; |
| 1094 | |
| 1095 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1096 | /* Check if the execution timeout is expired. It it is the case, we |
| 1097 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1098 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1099 | tv_update_date(0, 1); |
| 1100 | lua->run_time += now_ms - lua->start_time; |
| 1101 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1102 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1103 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1104 | break; |
| 1105 | } |
| 1106 | /* Process the forced yield. if the general yield is not allowed or |
| 1107 | * if no task were associated this the current Lua execution |
| 1108 | * coroutine, we resume the execution. Else we want to return in the |
| 1109 | * scheduler and we want to be waked up again, to continue the |
| 1110 | * current Lua execution. So we schedule our own task. |
| 1111 | */ |
| 1112 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1113 | if (!yield_allowed || !lua->task) |
| 1114 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1115 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1116 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1117 | if (!yield_allowed) { |
| 1118 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1119 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1120 | break; |
| 1121 | } |
| 1122 | ret = HLUA_E_AGAIN; |
| 1123 | break; |
| 1124 | |
| 1125 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1126 | |
| 1127 | /* Special exit case. The traditionnal exit is returned as an error |
| 1128 | * because the errors ares the only one mean to return immediately |
| 1129 | * from and lua execution. |
| 1130 | */ |
| 1131 | if (lua->flags & HLUA_EXIT) { |
| 1132 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1133 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1134 | break; |
| 1135 | } |
| 1136 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1137 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1138 | if (!lua_checkstack(lua->T, 1)) { |
| 1139 | ret = HLUA_E_ERR; |
| 1140 | break; |
| 1141 | } |
| 1142 | msg = lua_tostring(lua->T, -1); |
| 1143 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1144 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1145 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1146 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1147 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1148 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1149 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1150 | ret = HLUA_E_ERRMSG; |
| 1151 | break; |
| 1152 | |
| 1153 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1154 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1155 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1156 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1157 | break; |
| 1158 | |
| 1159 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1160 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1161 | if (!lua_checkstack(lua->T, 1)) { |
| 1162 | ret = HLUA_E_ERR; |
| 1163 | break; |
| 1164 | } |
| 1165 | msg = lua_tostring(lua->T, -1); |
| 1166 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1167 | lua_pop(lua->T, 1); |
| 1168 | if (msg) |
| 1169 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1170 | else |
| 1171 | lua_pushfstring(lua->T, "message handler error"); |
| 1172 | ret = HLUA_E_ERRMSG; |
| 1173 | break; |
| 1174 | |
| 1175 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1176 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1177 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1178 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1179 | break; |
| 1180 | } |
| 1181 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1182 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1183 | if (lua->flags & HLUA_MUST_GC && |
| 1184 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1185 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1186 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1187 | switch (ret) { |
| 1188 | case HLUA_E_AGAIN: |
| 1189 | break; |
| 1190 | |
| 1191 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1192 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1193 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1194 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1195 | break; |
| 1196 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1197 | case HLUA_E_ETMOUT: |
| 1198 | case HLUA_E_NOMEM: |
| 1199 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1200 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1201 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1202 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1203 | hlua_ctx_renew(lua, 0); |
| 1204 | break; |
| 1205 | |
| 1206 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1207 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1208 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1209 | break; |
| 1210 | } |
| 1211 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1212 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1213 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1214 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1215 | return ret; |
| 1216 | } |
| 1217 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1218 | /* This function exit the current code. */ |
| 1219 | __LJMP static int hlua_done(lua_State *L) |
| 1220 | { |
| 1221 | struct hlua *hlua = hlua_gethlua(L); |
| 1222 | |
| 1223 | hlua->flags |= HLUA_EXIT; |
| 1224 | WILL_LJMP(lua_error(L)); |
| 1225 | |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1229 | /* This function is an LUA binding. It provides a function |
| 1230 | * for deleting ACL from a referenced ACL file. |
| 1231 | */ |
| 1232 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1233 | { |
| 1234 | const char *name; |
| 1235 | const char *key; |
| 1236 | struct pat_ref *ref; |
| 1237 | |
| 1238 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1239 | |
| 1240 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1241 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1242 | |
| 1243 | ref = pat_ref_lookup(name); |
| 1244 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1245 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1246 | |
| 1247 | pat_ref_delete(ref, key); |
| 1248 | return 0; |
| 1249 | } |
| 1250 | |
| 1251 | /* This function is an LUA binding. It provides a function |
| 1252 | * for deleting map entry from a referenced map file. |
| 1253 | */ |
| 1254 | static int hlua_del_map(lua_State *L) |
| 1255 | { |
| 1256 | const char *name; |
| 1257 | const char *key; |
| 1258 | struct pat_ref *ref; |
| 1259 | |
| 1260 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1261 | |
| 1262 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1263 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1264 | |
| 1265 | ref = pat_ref_lookup(name); |
| 1266 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1267 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1268 | |
| 1269 | pat_ref_delete(ref, key); |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | /* This function is an LUA binding. It provides a function |
| 1274 | * for adding ACL pattern from a referenced ACL file. |
| 1275 | */ |
| 1276 | static int hlua_add_acl(lua_State *L) |
| 1277 | { |
| 1278 | const char *name; |
| 1279 | const char *key; |
| 1280 | struct pat_ref *ref; |
| 1281 | |
| 1282 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1283 | |
| 1284 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1285 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1286 | |
| 1287 | ref = pat_ref_lookup(name); |
| 1288 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1289 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1290 | |
| 1291 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1292 | pat_ref_add(ref, key, NULL, NULL); |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
| 1296 | /* This function is an LUA binding. It provides a function |
| 1297 | * for setting map pattern and sample from a referenced map |
| 1298 | * file. |
| 1299 | */ |
| 1300 | static int hlua_set_map(lua_State *L) |
| 1301 | { |
| 1302 | const char *name; |
| 1303 | const char *key; |
| 1304 | const char *value; |
| 1305 | struct pat_ref *ref; |
| 1306 | |
| 1307 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1308 | |
| 1309 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1310 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1311 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1312 | |
| 1313 | ref = pat_ref_lookup(name); |
| 1314 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1315 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1316 | |
| 1317 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1318 | pat_ref_set(ref, key, value, NULL); |
| 1319 | else |
| 1320 | pat_ref_add(ref, key, value, NULL); |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1324 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1325 | * an integer or user data. This data is associated with a metatable. This |
| 1326 | * metatable have an original version registred in the global context with |
| 1327 | * the name of the object (_G[<name>] = <metable> ). |
| 1328 | * |
| 1329 | * A metable is a table that modify the standard behavior of a standard |
| 1330 | * access to the associated data. The entries of this new metatable are |
| 1331 | * defined as is: |
| 1332 | * |
| 1333 | * http://lua-users.org/wiki/MetatableEvents |
| 1334 | * |
| 1335 | * __index |
| 1336 | * |
| 1337 | * we access an absent field in a table, the result is nil. This is |
| 1338 | * true, but it is not the whole truth. Actually, such access triggers |
| 1339 | * the interpreter to look for an __index metamethod: If there is no |
| 1340 | * such method, as usually happens, then the access results in nil; |
| 1341 | * otherwise, the metamethod will provide the result. |
| 1342 | * |
| 1343 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1344 | * the key does not appear in the table, but the metatable has an __index |
| 1345 | * property: |
| 1346 | * |
| 1347 | * - if the value is a function, the function is called, passing in the |
| 1348 | * table and the key; the return value of that function is returned as |
| 1349 | * the result. |
| 1350 | * |
| 1351 | * - if the value is another table, the value of the key in that table is |
| 1352 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1353 | * table's metatable has an __index property, then it continues on up) |
| 1354 | * |
| 1355 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1356 | * |
| 1357 | * http://www.lua.org/pil/13.4.1.html |
| 1358 | * |
| 1359 | * __newindex |
| 1360 | * |
| 1361 | * Like __index, but control property assignment. |
| 1362 | * |
| 1363 | * __mode - Control weak references. A string value with one or both |
| 1364 | * of the characters 'k' and 'v' which specifies that the the |
| 1365 | * keys and/or values in the table are weak references. |
| 1366 | * |
| 1367 | * __call - Treat a table like a function. When a table is followed by |
| 1368 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1369 | * a __call key pointing to a function, that function is invoked |
| 1370 | * (passing any specified arguments) and the return value is |
| 1371 | * returned. |
| 1372 | * |
| 1373 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1374 | * called, if the metatable for myTable has a __metatable |
| 1375 | * key, the value of that key is returned instead of the |
| 1376 | * actual metatable. |
| 1377 | * |
| 1378 | * __tostring - Control string representation. When the builtin |
| 1379 | * "tostring( myTable )" function is called, if the metatable |
| 1380 | * for myTable has a __tostring property set to a function, |
| 1381 | * that function is invoked (passing myTable to it) and the |
| 1382 | * return value is used as the string representation. |
| 1383 | * |
| 1384 | * __len - Control table length. When the table length is requested using |
| 1385 | * the length operator ( '#' ), if the metatable for myTable has |
| 1386 | * a __len key pointing to a function, that function is invoked |
| 1387 | * (passing myTable to it) and the return value used as the value |
| 1388 | * of "#myTable". |
| 1389 | * |
| 1390 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1391 | * collected, if the metatable has a __gc field pointing to a |
| 1392 | * function, that function is first invoked, passing the userdata |
| 1393 | * to it. The __gc metamethod is not called for tables. |
| 1394 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1395 | * |
| 1396 | * Special metamethods for redefining standard operators: |
| 1397 | * http://www.lua.org/pil/13.1.html |
| 1398 | * |
| 1399 | * __add "+" |
| 1400 | * __sub "-" |
| 1401 | * __mul "*" |
| 1402 | * __div "/" |
| 1403 | * __unm "!" |
| 1404 | * __pow "^" |
| 1405 | * __concat ".." |
| 1406 | * |
| 1407 | * Special methods for redfining standar relations |
| 1408 | * http://www.lua.org/pil/13.2.html |
| 1409 | * |
| 1410 | * __eq "==" |
| 1411 | * __lt "<" |
| 1412 | * __le "<=" |
| 1413 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1414 | |
| 1415 | /* |
| 1416 | * |
| 1417 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1418 | * Class Map |
| 1419 | * |
| 1420 | * |
| 1421 | */ |
| 1422 | |
| 1423 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1424 | * a class session, otherwise it throws an error. |
| 1425 | */ |
| 1426 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1427 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1428 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | /* This function is the map constructor. It don't need |
| 1432 | * the class Map object. It creates and return a new Map |
| 1433 | * object. It must be called only during "body" or "init" |
| 1434 | * context because it process some filesystem accesses. |
| 1435 | */ |
| 1436 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1437 | { |
| 1438 | const char *fn; |
| 1439 | int match = PAT_MATCH_STR; |
| 1440 | struct sample_conv conv; |
| 1441 | const char *file = ""; |
| 1442 | int line = 0; |
| 1443 | lua_Debug ar; |
| 1444 | char *err = NULL; |
| 1445 | struct arg args[2]; |
| 1446 | |
| 1447 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1448 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1449 | |
| 1450 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1451 | |
| 1452 | if (lua_gettop(L) >= 2) { |
| 1453 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1454 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1455 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1456 | } |
| 1457 | |
| 1458 | /* Get Lua filename and line number. */ |
| 1459 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1460 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1461 | if (ar.currentline > 0) { /* is there info? */ |
| 1462 | file = ar.short_src; |
| 1463 | line = ar.currentline; |
| 1464 | } |
| 1465 | } |
| 1466 | |
| 1467 | /* fill fake sample_conv struct. */ |
| 1468 | conv.kw = ""; /* unused. */ |
| 1469 | conv.process = NULL; /* unused. */ |
| 1470 | conv.arg_mask = 0; /* unused. */ |
| 1471 | conv.val_args = NULL; /* unused. */ |
| 1472 | conv.out_type = SMP_T_STR; |
| 1473 | conv.private = (void *)(long)match; |
| 1474 | switch (match) { |
| 1475 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1476 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1477 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1478 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1479 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1480 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1481 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1482 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1483 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1484 | default: |
| 1485 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1486 | } |
| 1487 | |
| 1488 | /* fill fake args. */ |
| 1489 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1490 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1491 | args[1].type = ARGT_STOP; |
| 1492 | |
| 1493 | /* load the map. */ |
| 1494 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1495 | /* error case: we cant use luaL_error because we must |
| 1496 | * free the err variable. |
| 1497 | */ |
| 1498 | luaL_where(L, 1); |
| 1499 | lua_pushfstring(L, "'new': %s.", err); |
| 1500 | lua_concat(L, 2); |
| 1501 | free(err); |
| 1502 | WILL_LJMP(lua_error(L)); |
| 1503 | } |
| 1504 | |
| 1505 | /* create the lua object. */ |
| 1506 | lua_newtable(L); |
| 1507 | lua_pushlightuserdata(L, args[0].data.map); |
| 1508 | lua_rawseti(L, -2, 0); |
| 1509 | |
| 1510 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1511 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1512 | lua_setmetatable(L, -2); |
| 1513 | |
| 1514 | |
| 1515 | return 1; |
| 1516 | } |
| 1517 | |
| 1518 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1519 | { |
| 1520 | struct map_descriptor *desc; |
| 1521 | struct pattern *pat; |
| 1522 | struct sample smp; |
| 1523 | |
| 1524 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1525 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1526 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1527 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1528 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1529 | } |
| 1530 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1531 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1532 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1533 | 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] | 1534 | } |
| 1535 | |
| 1536 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1537 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1538 | if (str) |
| 1539 | lua_pushstring(L, ""); |
| 1540 | else |
| 1541 | lua_pushnil(L); |
| 1542 | return 1; |
| 1543 | } |
| 1544 | |
| 1545 | /* 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] | 1546 | 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] | 1547 | return 1; |
| 1548 | } |
| 1549 | |
| 1550 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1551 | { |
| 1552 | return _hlua_map_lookup(L, 0); |
| 1553 | } |
| 1554 | |
| 1555 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1556 | { |
| 1557 | return _hlua_map_lookup(L, 1); |
| 1558 | } |
| 1559 | |
| 1560 | /* |
| 1561 | * |
| 1562 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1563 | * Class Socket |
| 1564 | * |
| 1565 | * |
| 1566 | */ |
| 1567 | |
| 1568 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1569 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1570 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1571 | } |
| 1572 | |
| 1573 | /* This function is the handler called for each I/O on the established |
| 1574 | * connection. It is used for notify space avalaible to send or data |
| 1575 | * received. |
| 1576 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1577 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1578 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1579 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1580 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1581 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1582 | if (appctx->ctx.hlua_cosocket.die) { |
| 1583 | si_shutw(si); |
| 1584 | si_shutr(si); |
| 1585 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1586 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1587 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1588 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1589 | } |
| 1590 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1591 | /* If the connection object is not avalaible, close all the |
| 1592 | * streams and wakeup everithing waiting for. |
| 1593 | */ |
| 1594 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1595 | si_shutw(si); |
| 1596 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1597 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1598 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1599 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1600 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1601 | } |
| 1602 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1603 | /* If we cant write, wakeup the pending write signals. */ |
| 1604 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1605 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1606 | |
| 1607 | /* If we cant read, wakeup the pending read signals. */ |
| 1608 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1609 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1610 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1611 | /* if the connection is not estabkished, inform the stream that we want |
| 1612 | * to be notified whenever the connection completes. |
| 1613 | */ |
| 1614 | if (!(c->flags & CO_FL_CONNECTED)) { |
| 1615 | si_applet_cant_get(si); |
| 1616 | si_applet_cant_put(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1617 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1618 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1619 | |
| 1620 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1621 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1622 | |
| 1623 | /* Wake the tasks which wants to write if the buffer have avalaible space. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1624 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1625 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1626 | |
| 1627 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1628 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1629 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1630 | |
| 1631 | /* Some data were injected in the buffer, notify the stream |
| 1632 | * interface. |
| 1633 | */ |
| 1634 | if (!channel_is_empty(si_ic(si))) |
| 1635 | stream_int_update(si); |
| 1636 | |
| 1637 | /* If write notifications are registered, we considers we want |
| 1638 | * to write, so we set the flag cant put |
| 1639 | */ |
| 1640 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
| 1641 | si_applet_cant_put(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1642 | } |
| 1643 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1644 | /* This function is called when the "struct stream" is destroyed. |
| 1645 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1646 | * Wake all the pending signals. |
| 1647 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1648 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1649 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1650 | struct xref *peer; |
| 1651 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1652 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1653 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1654 | if (peer) |
| 1655 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1656 | |
| 1657 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1658 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1659 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1663 | * uses this object. If the stream does not exists, just quit. |
| 1664 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1665 | * pending signal can rest in the read and write lists. destroy |
| 1666 | * it. |
| 1667 | */ |
| 1668 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1669 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1670 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1671 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1672 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1673 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1674 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1675 | |
| 1676 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1677 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1678 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1679 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1680 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1681 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1682 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1683 | appctx->ctx.hlua_cosocket.die = 1; |
| 1684 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1685 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1686 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1687 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1688 | return 0; |
| 1689 | } |
| 1690 | |
| 1691 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1692 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1693 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1694 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1695 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1696 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1697 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1698 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1699 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1700 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1701 | |
| 1702 | /* Check if we run on the same thread than the xreator thread. |
| 1703 | * We cannot access to the socket if the thread is different. |
| 1704 | */ |
| 1705 | if (socket->tid != tid) |
| 1706 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1707 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1708 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1709 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1710 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1711 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1712 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1713 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1714 | appctx->ctx.hlua_cosocket.die = 1; |
| 1715 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1716 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1717 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1718 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1719 | return 0; |
| 1720 | } |
| 1721 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1722 | /* The close function calls close_helper. |
| 1723 | */ |
| 1724 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1725 | { |
| 1726 | MAY_LJMP(check_args(L, 1, "close")); |
| 1727 | return hlua_socket_close_helper(L); |
| 1728 | } |
| 1729 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1730 | /* This Lua function assumes that the stack contain three parameters. |
| 1731 | * 1 - USERDATA containing a struct socket |
| 1732 | * 2 - INTEGER with values of the macro defined below |
| 1733 | * If the integer is -1, we must read at most one line. |
| 1734 | * If the integer is -2, we ust read all the data until the |
| 1735 | * end of the stream. |
| 1736 | * If the integer is positive value, we must read a number of |
| 1737 | * bytes corresponding to this value. |
| 1738 | */ |
| 1739 | #define HLSR_READ_LINE (-1) |
| 1740 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1741 | __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] | 1742 | { |
| 1743 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1744 | int wanted = lua_tointeger(L, 2); |
| 1745 | struct hlua *hlua = hlua_gethlua(L); |
| 1746 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1747 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1748 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1749 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1750 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1751 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1752 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1753 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1754 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1755 | struct stream_interface *si; |
| 1756 | struct stream *s; |
| 1757 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1758 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1759 | |
| 1760 | /* Check if this lua stack is schedulable. */ |
| 1761 | if (!hlua || !hlua->task) |
| 1762 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1763 | "'frontend', 'backend' or 'task'")); |
| 1764 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1765 | /* Check if we run on the same thread than the xreator thread. |
| 1766 | * We cannot access to the socket if the thread is different. |
| 1767 | */ |
| 1768 | if (socket->tid != tid) |
| 1769 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1770 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1771 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1772 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1773 | if (!peer) |
| 1774 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1775 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1776 | si = appctx->owner; |
| 1777 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1778 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1779 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1780 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1781 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1782 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1783 | if (nblk < 0) /* Connection close. */ |
| 1784 | goto connection_closed; |
| 1785 | if (nblk == 0) /* No data avalaible. */ |
| 1786 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1787 | |
| 1788 | /* remove final \r\n. */ |
| 1789 | if (nblk == 1) { |
| 1790 | if (blk1[len1-1] == '\n') { |
| 1791 | len1--; |
| 1792 | skip_at_end++; |
| 1793 | if (blk1[len1-1] == '\r') { |
| 1794 | len1--; |
| 1795 | skip_at_end++; |
| 1796 | } |
| 1797 | } |
| 1798 | } |
| 1799 | else { |
| 1800 | if (blk2[len2-1] == '\n') { |
| 1801 | len2--; |
| 1802 | skip_at_end++; |
| 1803 | if (blk2[len2-1] == '\r') { |
| 1804 | len2--; |
| 1805 | skip_at_end++; |
| 1806 | } |
| 1807 | } |
| 1808 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1812 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1813 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1814 | if (nblk < 0) /* Connection close. */ |
| 1815 | goto connection_closed; |
| 1816 | if (nblk == 0) /* No data avalaible. */ |
| 1817 | goto connection_empty; |
| 1818 | } |
| 1819 | |
| 1820 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1821 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1822 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1823 | if (nblk < 0) /* Connection close. */ |
| 1824 | goto connection_closed; |
| 1825 | if (nblk == 0) /* No data avalaible. */ |
| 1826 | goto connection_empty; |
| 1827 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1828 | missing_bytes = wanted - socket->b.n; |
| 1829 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1830 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1831 | len1 = missing_bytes; |
| 1832 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1833 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1834 | } |
| 1835 | |
| 1836 | len = len1; |
| 1837 | |
| 1838 | luaL_addlstring(&socket->b, blk1, len1); |
| 1839 | if (nblk == 2) { |
| 1840 | len += len2; |
| 1841 | luaL_addlstring(&socket->b, blk2, len2); |
| 1842 | } |
| 1843 | |
| 1844 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1845 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1846 | |
| 1847 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1848 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1849 | |
| 1850 | /* If the pattern reclaim to read all the data |
| 1851 | * in the connection, got out. |
| 1852 | */ |
| 1853 | if (wanted == HLSR_READ_ALL) |
| 1854 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1855 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1856 | goto connection_empty; |
| 1857 | |
| 1858 | /* Return result. */ |
| 1859 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1860 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1861 | return 1; |
| 1862 | |
| 1863 | connection_closed: |
| 1864 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1865 | xref_unlock(&socket->xref, peer); |
| 1866 | |
| 1867 | no_peer: |
| 1868 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1869 | /* If the buffer containds data. */ |
| 1870 | if (socket->b.n > 0) { |
| 1871 | luaL_pushresult(&socket->b); |
| 1872 | return 1; |
| 1873 | } |
| 1874 | lua_pushnil(L); |
| 1875 | lua_pushstring(L, "connection closed."); |
| 1876 | return 2; |
| 1877 | |
| 1878 | connection_empty: |
| 1879 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1880 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1881 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1882 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1883 | } |
| 1884 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1885 | WILL_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] | 1886 | return 0; |
| 1887 | } |
| 1888 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1889 | /* This Lua function gets two parameters. The first one can be string |
| 1890 | * or a number. If the string is "*l", the user requires one line. If |
| 1891 | * 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] | 1892 | * If the value is a number, the user require a number of bytes equal |
| 1893 | * to the value. The default value is "*l" (a line). |
| 1894 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1895 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1896 | * integer takes this values: |
| 1897 | * -1 : read a line |
| 1898 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1899 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1900 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1901 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1902 | * concatenated with the read data. |
| 1903 | */ |
| 1904 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1905 | { |
| 1906 | int wanted = HLSR_READ_LINE; |
| 1907 | const char *pattern; |
| 1908 | int type; |
| 1909 | char *error; |
| 1910 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1911 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1912 | |
| 1913 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1914 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1915 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1916 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1917 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1918 | /* Check if we run on the same thread than the xreator thread. |
| 1919 | * We cannot access to the socket if the thread is different. |
| 1920 | */ |
| 1921 | if (socket->tid != tid) |
| 1922 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1923 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1924 | /* check for pattern. */ |
| 1925 | if (lua_gettop(L) >= 2) { |
| 1926 | type = lua_type(L, 2); |
| 1927 | if (type == LUA_TSTRING) { |
| 1928 | pattern = lua_tostring(L, 2); |
| 1929 | if (strcmp(pattern, "*a") == 0) |
| 1930 | wanted = HLSR_READ_ALL; |
| 1931 | else if (strcmp(pattern, "*l") == 0) |
| 1932 | wanted = HLSR_READ_LINE; |
| 1933 | else { |
| 1934 | wanted = strtoll(pattern, &error, 10); |
| 1935 | if (*error != '\0') |
| 1936 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1937 | } |
| 1938 | } |
| 1939 | else if (type == LUA_TNUMBER) { |
| 1940 | wanted = lua_tointeger(L, 2); |
| 1941 | if (wanted < 0) |
| 1942 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1943 | } |
| 1944 | } |
| 1945 | |
| 1946 | /* Set pattern. */ |
| 1947 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1948 | |
| 1949 | /* Check if we would replace the top by itself. */ |
| 1950 | if (lua_gettop(L) != 2) |
| 1951 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1952 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1953 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1954 | luaL_buffinit(L, &socket->b); |
| 1955 | |
| 1956 | /* Check prefix. */ |
| 1957 | if (lua_gettop(L) >= 3) { |
| 1958 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1959 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1960 | pattern = lua_tolstring(L, 3, &len); |
| 1961 | luaL_addlstring(&socket->b, pattern, len); |
| 1962 | } |
| 1963 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1964 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1965 | } |
| 1966 | |
| 1967 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1968 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1969 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1970 | 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] | 1971 | { |
| 1972 | struct hlua_socket *socket; |
| 1973 | struct hlua *hlua = hlua_gethlua(L); |
| 1974 | struct appctx *appctx; |
| 1975 | size_t buf_len; |
| 1976 | const char *buf; |
| 1977 | int len; |
| 1978 | int send_len; |
| 1979 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1980 | struct xref *peer; |
| 1981 | struct stream_interface *si; |
| 1982 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1983 | |
| 1984 | /* Check if this lua stack is schedulable. */ |
| 1985 | if (!hlua || !hlua->task) |
| 1986 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1987 | "'frontend', 'backend' or 'task'")); |
| 1988 | |
| 1989 | /* Get object */ |
| 1990 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1991 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1992 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1993 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1994 | /* Check if we run on the same thread than the xreator thread. |
| 1995 | * We cannot access to the socket if the thread is different. |
| 1996 | */ |
| 1997 | if (socket->tid != tid) |
| 1998 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1999 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2000 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2001 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2002 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2003 | lua_pushinteger(L, -1); |
| 2004 | return 1; |
| 2005 | } |
| 2006 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2007 | si = appctx->owner; |
| 2008 | s = si_strm(si); |
| 2009 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2010 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2011 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2012 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2013 | lua_pushinteger(L, -1); |
| 2014 | return 1; |
| 2015 | } |
| 2016 | |
| 2017 | /* Update the input buffer data. */ |
| 2018 | buf += sent; |
| 2019 | send_len = buf_len - sent; |
| 2020 | |
| 2021 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2022 | if (sent >= buf_len) { |
| 2023 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2024 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2025 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2026 | |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2027 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2028 | * the request buffer if its not required. |
| 2029 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2030 | if (s->req.buf.size == 0) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2031 | if (!channel_alloc_buffer(&s->req, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2032 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2033 | } |
| 2034 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2035 | /* Check for avalaible space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2036 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2037 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2038 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2039 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2040 | |
| 2041 | /* send data */ |
| 2042 | if (len < send_len) |
| 2043 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2044 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2045 | |
| 2046 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2047 | * the data" (-2) are not expected because the available length |
| 2048 | * is tested. |
| 2049 | * Other unknown error are also not expected. |
| 2050 | */ |
| 2051 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2052 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2053 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2054 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2055 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2056 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2057 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2058 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2059 | return 1; |
| 2060 | } |
| 2061 | |
| 2062 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2063 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2064 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2065 | s->req.rex = TICK_ETERNITY; |
| 2066 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2067 | |
| 2068 | /* Update length sent. */ |
| 2069 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2070 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2071 | |
| 2072 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2073 | if (sent + len >= buf_len) { |
| 2074 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2075 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2076 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2077 | |
| 2078 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2079 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2080 | xref_unlock(&socket->xref, peer); |
| 2081 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2082 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2083 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2084 | WILL_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] | 2085 | return 0; |
| 2086 | } |
| 2087 | |
| 2088 | /* This function initiate the send of data. It just check the input |
| 2089 | * parameters and push an integer in the Lua stack that contain the |
| 2090 | * amount of data writed in the buffer. This is used by the function |
| 2091 | * "hlua_socket_write_yield" that can yield. |
| 2092 | * |
| 2093 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2094 | * the associated object. The second is a string buffer. The third is |
| 2095 | * a facultative integer that represents where is the buffer position |
| 2096 | * of the start of the data that can send. The first byte is the |
| 2097 | * position "1". The default value is "1". The fourth argument is a |
| 2098 | * facultative integer that represents where is the buffer position |
| 2099 | * of the end of the data that can send. The default is the last byte. |
| 2100 | */ |
| 2101 | static int hlua_socket_send(struct lua_State *L) |
| 2102 | { |
| 2103 | int i; |
| 2104 | int j; |
| 2105 | const char *buf; |
| 2106 | size_t buf_len; |
| 2107 | |
| 2108 | /* Check number of arguments. */ |
| 2109 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2110 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2111 | |
| 2112 | /* Get the string. */ |
| 2113 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2114 | |
| 2115 | /* Get and check j. */ |
| 2116 | if (lua_gettop(L) == 4) { |
| 2117 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2118 | if (j < 0) |
| 2119 | j = buf_len + j + 1; |
| 2120 | if (j > buf_len) |
| 2121 | j = buf_len + 1; |
| 2122 | lua_pop(L, 1); |
| 2123 | } |
| 2124 | else |
| 2125 | j = buf_len; |
| 2126 | |
| 2127 | /* Get and check i. */ |
| 2128 | if (lua_gettop(L) == 3) { |
| 2129 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2130 | if (i < 0) |
| 2131 | i = buf_len + i + 1; |
| 2132 | if (i > buf_len) |
| 2133 | i = buf_len + 1; |
| 2134 | lua_pop(L, 1); |
| 2135 | } else |
| 2136 | i = 1; |
| 2137 | |
| 2138 | /* Check bth i and j. */ |
| 2139 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2140 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2141 | return 1; |
| 2142 | } |
| 2143 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2144 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2145 | return 1; |
| 2146 | } |
| 2147 | if (i == 0) |
| 2148 | i = 1; |
| 2149 | if (j == 0) |
| 2150 | j = 1; |
| 2151 | |
| 2152 | /* Pop the string. */ |
| 2153 | lua_pop(L, 1); |
| 2154 | |
| 2155 | /* Update the buffer length. */ |
| 2156 | buf += i - 1; |
| 2157 | buf_len = j - i + 1; |
| 2158 | lua_pushlstring(L, buf, buf_len); |
| 2159 | |
| 2160 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2161 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2162 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2163 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2164 | } |
| 2165 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2166 | #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] | 2167 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2168 | { |
| 2169 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2170 | int ret; |
| 2171 | int len; |
| 2172 | char *p; |
| 2173 | |
| 2174 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2175 | if (ret <= 0) { |
| 2176 | lua_pushnil(L); |
| 2177 | return 1; |
| 2178 | } |
| 2179 | |
| 2180 | if (ret == AF_UNIX) { |
| 2181 | lua_pushstring(L, buffer+1); |
| 2182 | return 1; |
| 2183 | } |
| 2184 | else if (ret == AF_INET6) { |
| 2185 | buffer[0] = '['; |
| 2186 | len = strlen(buffer); |
| 2187 | buffer[len] = ']'; |
| 2188 | len++; |
| 2189 | buffer[len] = ':'; |
| 2190 | len++; |
| 2191 | p = buffer; |
| 2192 | } |
| 2193 | else if (ret == AF_INET) { |
| 2194 | p = buffer + 1; |
| 2195 | len = strlen(p); |
| 2196 | p[len] = ':'; |
| 2197 | len++; |
| 2198 | } |
| 2199 | else { |
| 2200 | lua_pushnil(L); |
| 2201 | return 1; |
| 2202 | } |
| 2203 | |
| 2204 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2205 | lua_pushnil(L); |
| 2206 | return 1; |
| 2207 | } |
| 2208 | |
| 2209 | lua_pushstring(L, p); |
| 2210 | return 1; |
| 2211 | } |
| 2212 | |
| 2213 | /* Returns information about the peer of the connection. */ |
| 2214 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2215 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2216 | struct hlua_socket *socket; |
| 2217 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2218 | struct xref *peer; |
| 2219 | struct appctx *appctx; |
| 2220 | struct stream_interface *si; |
| 2221 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2222 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2223 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2224 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2225 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2226 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2227 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2228 | /* Check if we run on the same thread than the xreator thread. |
| 2229 | * We cannot access to the socket if the thread is different. |
| 2230 | */ |
| 2231 | if (socket->tid != tid) |
| 2232 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2233 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2234 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2235 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2236 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2237 | lua_pushnil(L); |
| 2238 | return 1; |
| 2239 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2240 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2241 | si = appctx->owner; |
| 2242 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2243 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2244 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2245 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2246 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2247 | lua_pushnil(L); |
| 2248 | return 1; |
| 2249 | } |
| 2250 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2251 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2252 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2253 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2254 | lua_pushnil(L); |
| 2255 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2256 | } |
| 2257 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2258 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2259 | xref_unlock(&socket->xref, peer); |
| 2260 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2261 | } |
| 2262 | |
| 2263 | /* Returns information about my connection side. */ |
| 2264 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2265 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2266 | struct hlua_socket *socket; |
| 2267 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2268 | struct appctx *appctx; |
| 2269 | struct xref *peer; |
| 2270 | struct stream_interface *si; |
| 2271 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2272 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2273 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2274 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2275 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2276 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2277 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2278 | /* Check if we run on the same thread than the xreator thread. |
| 2279 | * We cannot access to the socket if the thread is different. |
| 2280 | */ |
| 2281 | if (socket->tid != tid) |
| 2282 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2283 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2284 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2285 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2286 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2287 | lua_pushnil(L); |
| 2288 | return 1; |
| 2289 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2290 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2291 | si = appctx->owner; |
| 2292 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2294 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2295 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2296 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | lua_pushnil(L); |
| 2298 | return 1; |
| 2299 | } |
| 2300 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2301 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2302 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2303 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2304 | lua_pushnil(L); |
| 2305 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | } |
| 2307 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2308 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2309 | xref_unlock(&socket->xref, peer); |
| 2310 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2311 | } |
| 2312 | |
| 2313 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2314 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2315 | .obj_type = OBJ_TYPE_APPLET, |
| 2316 | .name = "<LUA_TCP>", |
| 2317 | .fct = hlua_socket_handler, |
| 2318 | .release = hlua_socket_release, |
| 2319 | }; |
| 2320 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2321 | __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] | 2322 | { |
| 2323 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2324 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2325 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2326 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2327 | struct stream_interface *si; |
| 2328 | struct stream *s; |
| 2329 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2330 | /* Check if we run on the same thread than the xreator thread. |
| 2331 | * We cannot access to the socket if the thread is different. |
| 2332 | */ |
| 2333 | if (socket->tid != tid) |
| 2334 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2335 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2336 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2337 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2338 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2339 | lua_pushnil(L); |
| 2340 | lua_pushstring(L, "Can't connect"); |
| 2341 | return 2; |
| 2342 | } |
| 2343 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2344 | si = appctx->owner; |
| 2345 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2346 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2347 | /* Check if we run on the same thread than the xreator thread. |
| 2348 | * We cannot access to the socket if the thread is different. |
| 2349 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2350 | if (socket->tid != tid) { |
| 2351 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2352 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2353 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2354 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2355 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2356 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2357 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2358 | lua_pushnil(L); |
| 2359 | lua_pushstring(L, "Can't connect"); |
| 2360 | return 2; |
| 2361 | } |
| 2362 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2363 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2364 | |
| 2365 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2366 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2367 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2368 | lua_pushinteger(L, 1); |
| 2369 | return 1; |
| 2370 | } |
| 2371 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2372 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2373 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2374 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2375 | } |
| 2376 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2377 | WILL_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] | 2378 | return 0; |
| 2379 | } |
| 2380 | |
| 2381 | /* This function fail or initite the connection. */ |
| 2382 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2383 | { |
| 2384 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2385 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2386 | const char *ip; |
| 2387 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2388 | struct hlua *hlua; |
| 2389 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2390 | int low, high; |
| 2391 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2392 | struct xref *peer; |
| 2393 | struct stream_interface *si; |
| 2394 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2395 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2396 | if (lua_gettop(L) < 2) |
| 2397 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2398 | |
| 2399 | /* Get args. */ |
| 2400 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2401 | |
| 2402 | /* Check if we run on the same thread than the xreator thread. |
| 2403 | * We cannot access to the socket if the thread is different. |
| 2404 | */ |
| 2405 | if (socket->tid != tid) |
| 2406 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2407 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2408 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2409 | if (lua_gettop(L) >= 3) { |
| 2410 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2411 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2412 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2413 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2414 | * that are not enclosed within square brackets. |
| 2415 | */ |
| 2416 | if (port > 0) { |
| 2417 | luaL_buffinit(L, &b); |
| 2418 | luaL_addstring(&b, ip); |
| 2419 | luaL_addchar(&b, ':'); |
| 2420 | luaL_pushresult(&b); |
| 2421 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2422 | } |
| 2423 | } |
| 2424 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2425 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2426 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2427 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2428 | lua_pushnil(L); |
| 2429 | return 1; |
| 2430 | } |
| 2431 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2432 | si = appctx->owner; |
| 2433 | s = si_strm(si); |
| 2434 | |
| 2435 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2436 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2437 | if (!conn) { |
| 2438 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2439 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2440 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2441 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2442 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2443 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2444 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2445 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2446 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2447 | if (!addr) { |
| 2448 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2449 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2450 | } |
| 2451 | if (low != high) { |
| 2452 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2453 | 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] | 2454 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2455 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2456 | |
| 2457 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2458 | if (low == 0) { |
| 2459 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2460 | if (port == -1) { |
| 2461 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2462 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2463 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2464 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2465 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2466 | if (port == -1) { |
| 2467 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2468 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2469 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2470 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2471 | } |
| 2472 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2473 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2474 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2475 | appctx = objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2476 | |
| 2477 | /* inform the stream that we want to be notified whenever the |
| 2478 | * connection completes. |
| 2479 | */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2480 | si_applet_cant_get(&s->si[0]); |
| 2481 | si_applet_cant_put(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2482 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2483 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2484 | hlua->flags |= HLUA_MUST_GC; |
| 2485 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2486 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2487 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2488 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2489 | } |
| 2490 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2491 | |
| 2492 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2493 | /* Return yield waiting for connection. */ |
| 2494 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2495 | WILL_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] | 2496 | |
| 2497 | return 0; |
| 2498 | } |
| 2499 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2500 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2501 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2502 | { |
| 2503 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2504 | struct xref *peer; |
| 2505 | struct appctx *appctx; |
| 2506 | struct stream_interface *si; |
| 2507 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2508 | |
| 2509 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2510 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2511 | |
| 2512 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2513 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2514 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2515 | lua_pushnil(L); |
| 2516 | return 1; |
| 2517 | } |
| 2518 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2519 | si = appctx->owner; |
| 2520 | s = si_strm(si); |
| 2521 | |
| 2522 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2523 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2524 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2525 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2526 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2527 | |
| 2528 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2529 | { |
| 2530 | return 0; |
| 2531 | } |
| 2532 | |
| 2533 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2534 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2535 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2536 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2537 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2538 | struct xref *peer; |
| 2539 | struct appctx *appctx; |
| 2540 | struct stream_interface *si; |
| 2541 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2542 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2543 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2544 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2545 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2546 | |
| 2547 | /* round up for inputs that are fractions and convert to millis */ |
| 2548 | dtmout = (0.5 + MAY_LJMP(luaL_checknumber(L, 2))) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2549 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2550 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2551 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2552 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2553 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2554 | if (dtmout > INT_MAX) /* overflow check */ |
| 2555 | WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d", INT_MAX)); |
| 2556 | |
| 2557 | tmout = MS_TO_TICKS((int)dtmout); |
| 2558 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2559 | /* Check if we run on the same thread than the xreator thread. |
| 2560 | * We cannot access to the socket if the thread is different. |
| 2561 | */ |
| 2562 | if (socket->tid != tid) |
| 2563 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2564 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2565 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2566 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2567 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2568 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2569 | WILL_LJMP(lua_error(L)); |
| 2570 | return 0; |
| 2571 | } |
| 2572 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2573 | si = appctx->owner; |
| 2574 | s = si_strm(si); |
| 2575 | |
| 2576 | s->req.rto = tmout; |
| 2577 | s->req.wto = tmout; |
| 2578 | s->res.rto = tmout; |
| 2579 | s->res.wto = tmout; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2580 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2581 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2582 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2583 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2587 | { |
| 2588 | struct hlua_socket *socket; |
| 2589 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2590 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2591 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2592 | |
| 2593 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2594 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2595 | hlua_pusherror(L, "socket: full stack"); |
| 2596 | goto out_fail_conf; |
| 2597 | } |
| 2598 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2599 | /* Create the object: obj[0] = userdata. */ |
| 2600 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2601 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2602 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2603 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2604 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2605 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2606 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2607 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2608 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2609 | goto out_fail_conf; |
| 2610 | } |
| 2611 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2612 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2613 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2614 | lua_setmetatable(L, -2); |
| 2615 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2616 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2617 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2618 | if (!appctx) { |
| 2619 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2620 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2621 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2622 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2623 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2624 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2625 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2626 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2627 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2628 | /* Now create a session, task and stream for this applet */ |
| 2629 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2630 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2631 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2632 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2633 | } |
| 2634 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2635 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2636 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2637 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2638 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2639 | } |
| 2640 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2641 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2642 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2643 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2644 | /* Configure "right" stream interface. this "si" is used to connect |
| 2645 | * and retrieve data from the server. The connection is initialized |
| 2646 | * with the "struct server". |
| 2647 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2648 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2649 | |
| 2650 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2651 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2652 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2653 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2654 | return 1; |
| 2655 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2656 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2657 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2658 | out_fail_sess: |
| 2659 | appctx_free(appctx); |
| 2660 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2661 | WILL_LJMP(lua_error(L)); |
| 2662 | return 0; |
| 2663 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2664 | |
| 2665 | /* |
| 2666 | * |
| 2667 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2668 | * Class Channel |
| 2669 | * |
| 2670 | * |
| 2671 | */ |
| 2672 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2673 | /* The state between the channel data and the HTTP parser state can be |
| 2674 | * unconsistent, so reset the parser and call it again. Warning, this |
| 2675 | * action not revalidate the request and not send a 400 if the modified |
| 2676 | * resuest is not valid. |
| 2677 | * |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2678 | * This function never fails. The direction is set using dir, which equals |
| 2679 | * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2680 | */ |
| 2681 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2682 | { |
| 2683 | /* Protocol HTTP. */ |
| 2684 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2685 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2686 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2687 | http_txn_reset_req(stream->txn); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2688 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2689 | http_txn_reset_res(stream->txn); |
| 2690 | |
| 2691 | if (stream->txn->hdr_idx.v) |
| 2692 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2693 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2694 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2695 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2696 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2697 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2698 | } |
| 2699 | } |
| 2700 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2701 | /* This function is called before the Lua execution. It stores |
| 2702 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2703 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2704 | 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] | 2705 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2706 | c->mode = stream->be->mode; |
| 2707 | switch (c->mode) { |
| 2708 | case PR_MODE_HTTP: |
| 2709 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2710 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2711 | c->data.http.state = stream->txn->req.msg_state; |
| 2712 | else |
| 2713 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2714 | break; |
| 2715 | default: |
| 2716 | break; |
| 2717 | } |
| 2718 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2719 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2720 | /* This function is called after the Lua execution. it |
| 2721 | * returns true if the parser state is consistent, otherwise, |
| 2722 | * it return false. |
| 2723 | * |
| 2724 | * In HTTP mode, the parser state must be in the same state |
| 2725 | * or greater when we exit the function. Even if we do a |
| 2726 | * control yield. This prevent to break the HTTP message |
| 2727 | * from the Lua code. |
| 2728 | */ |
| 2729 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2730 | { |
| 2731 | if (c->mode != stream->be->mode) |
| 2732 | return 0; |
| 2733 | |
| 2734 | switch (c->mode) { |
| 2735 | case PR_MODE_HTTP: |
| 2736 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2737 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2738 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2739 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2740 | else |
| 2741 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2742 | default: |
| 2743 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2744 | } |
| 2745 | return 1; |
| 2746 | } |
| 2747 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2748 | /* Returns the struct hlua_channel join to the class channel in the |
| 2749 | * stack entry "ud" or throws an argument error. |
| 2750 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2751 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2752 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2753 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2754 | } |
| 2755 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2756 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2757 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2758 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2759 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2760 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2761 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2762 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2763 | return 0; |
| 2764 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2765 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2766 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2767 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2768 | |
| 2769 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2770 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2771 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2772 | return 1; |
| 2773 | } |
| 2774 | |
| 2775 | /* Duplicate all the data present in the input channel and put it |
| 2776 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2777 | * the stack if the channel is closed and all the data are consumed, |
| 2778 | * returns 0 if no data are available, otherwise it returns the length |
| 2779 | * of the builded string. |
| 2780 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2781 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2782 | { |
| 2783 | char *blk1; |
| 2784 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2785 | size_t len1; |
| 2786 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2787 | int ret; |
| 2788 | luaL_Buffer b; |
| 2789 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2790 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2791 | if (unlikely(ret == 0)) |
| 2792 | return 0; |
| 2793 | |
| 2794 | if (unlikely(ret < 0)) { |
| 2795 | lua_pushnil(L); |
| 2796 | return -1; |
| 2797 | } |
| 2798 | |
| 2799 | luaL_buffinit(L, &b); |
| 2800 | luaL_addlstring(&b, blk1, len1); |
| 2801 | if (unlikely(ret == 2)) |
| 2802 | luaL_addlstring(&b, blk2, len2); |
| 2803 | luaL_pushresult(&b); |
| 2804 | |
| 2805 | if (unlikely(ret == 2)) |
| 2806 | return len1 + len2; |
| 2807 | return len1; |
| 2808 | } |
| 2809 | |
| 2810 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2811 | * a yield. This function keep the data in the buffer. |
| 2812 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2813 | __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] | 2814 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2815 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2816 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2817 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2818 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2819 | if (_hlua_channel_dup(chn, L) == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2820 | WILL_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] | 2821 | return 1; |
| 2822 | } |
| 2823 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2824 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2825 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2826 | { |
| 2827 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2828 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2829 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2830 | } |
| 2831 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2832 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2833 | * a yield. This function consumes the data in the buffer. It returns |
| 2834 | * a string containing the data or a nil pointer if no data are available |
| 2835 | * and the channel is closed. |
| 2836 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2837 | __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] | 2838 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2839 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2840 | int ret; |
| 2841 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2842 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2843 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2844 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2845 | if (unlikely(ret == 0)) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2846 | WILL_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] | 2847 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2848 | if (unlikely(ret == -1)) |
| 2849 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2850 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2851 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2852 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2853 | return 1; |
| 2854 | } |
| 2855 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2856 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 2857 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2858 | { |
| 2859 | MAY_LJMP(check_args(L, 1, "get")); |
| 2860 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2861 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2862 | } |
| 2863 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2864 | /* This functions consumes and returns one line. If the channel is closed, |
| 2865 | * and the last data does not contains a final '\n', the data are returned |
| 2866 | * without the final '\n'. When no more data are avalaible, it returns nil |
| 2867 | * value. |
| 2868 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2869 | __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] | 2870 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2871 | char *blk1; |
| 2872 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2873 | size_t len1; |
| 2874 | size_t len2; |
| 2875 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2876 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2877 | int ret; |
| 2878 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2879 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2880 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2881 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2882 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2883 | if (ret == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2884 | WILL_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] | 2885 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2886 | if (ret == -1) { |
| 2887 | lua_pushnil(L); |
| 2888 | return 1; |
| 2889 | } |
| 2890 | |
| 2891 | luaL_buffinit(L, &b); |
| 2892 | luaL_addlstring(&b, blk1, len1); |
| 2893 | len = len1; |
| 2894 | if (unlikely(ret == 2)) { |
| 2895 | luaL_addlstring(&b, blk2, len2); |
| 2896 | len += len2; |
| 2897 | } |
| 2898 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2899 | b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2900 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2901 | return 1; |
| 2902 | } |
| 2903 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2904 | /* Check arguments for the fucntion "hlua_channel_getline_yield". */ |
| 2905 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2906 | { |
| 2907 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2908 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2909 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2910 | } |
| 2911 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2912 | /* This function takes a string as input, and append it at the |
| 2913 | * input side of channel. If the data is too big, but a space |
| 2914 | * is probably available after sending some data, the function |
| 2915 | * yield. If the data is bigger than the buffer, or if the |
| 2916 | * channel is closed, it returns -1. otherwise, it returns the |
| 2917 | * amount of data writed. |
| 2918 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2919 | __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] | 2920 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2921 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2922 | size_t len; |
| 2923 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2924 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2925 | int ret; |
| 2926 | int max; |
| 2927 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2928 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2929 | * the request buffer if its not required. |
| 2930 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2931 | if (chn->buf.size == 0) { |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2932 | si_applet_cant_put(chn_prod(chn)); |
| 2933 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
| 2934 | } |
| 2935 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2936 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2937 | if (max > len - l) |
| 2938 | max = len - l; |
| 2939 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2940 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2941 | if (ret == -2 || ret == -3) { |
| 2942 | lua_pushinteger(L, -1); |
| 2943 | return 1; |
| 2944 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2945 | if (ret == -1) { |
| 2946 | chn->flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2947 | WILL_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] | 2948 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2949 | l += ret; |
| 2950 | lua_pop(L, 1); |
| 2951 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2952 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2953 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2954 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2955 | if (max == 0 && co_data(chn) == 0) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2956 | /* There are no space avalaible, and the output buffer is empty. |
| 2957 | * in this case, we cannot add more data, so we cannot yield, |
| 2958 | * we return the amount of copyied data. |
| 2959 | */ |
| 2960 | return 1; |
| 2961 | } |
| 2962 | if (l < len) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2963 | WILL_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] | 2964 | return 1; |
| 2965 | } |
| 2966 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2967 | /* just a wrapper of "hlua_channel_append_yield". It returns the length |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2968 | * of the writed string, or -1 if the channel is closed or if the |
| 2969 | * buffer size is too little for the data. |
| 2970 | */ |
| 2971 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2972 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2973 | size_t len; |
| 2974 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2975 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2976 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2977 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2978 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2979 | lua_pushinteger(L, 0); |
| 2980 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2981 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2982 | } |
| 2983 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2984 | /* just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2985 | * his process by cleaning the buffer. The result is a replacement |
| 2986 | * of the current data. It returns the length of the writed string, |
| 2987 | * or -1 if the channel is closed or if the buffer size is too |
| 2988 | * little for the data. |
| 2989 | */ |
| 2990 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2991 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2992 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2993 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2994 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2995 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2996 | lua_pushinteger(L, 0); |
| 2997 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2998 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2999 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3000 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | /* Append data in the output side of the buffer. This data is immediatly |
| 3004 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3005 | * cannot contains the data, the function yield. The function returns -1 |
| 3006 | * if the channel is closed. |
| 3007 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3008 | __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] | 3009 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3010 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3011 | size_t len; |
| 3012 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3013 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3014 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3015 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3016 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3017 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3018 | lua_pushinteger(L, -1); |
| 3019 | return 1; |
| 3020 | } |
| 3021 | |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3022 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 3023 | * the request buffer if its not required. |
| 3024 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3025 | if (chn->buf.size == 0) { |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3026 | si_applet_cant_put(chn_prod(chn)); |
| 3027 | WILL_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] | 3028 | } |
| 3029 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3030 | /* the writed data will be immediatly sent, so we can check |
| 3031 | * the avalaible space without taking in account the reserve. |
| 3032 | * The reserve is guaranted for the processing of incoming |
| 3033 | * data, because the buffer will be flushed. |
| 3034 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3035 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3036 | |
| 3037 | /* If there are no space avalaible, and the output buffer is empty. |
| 3038 | * in this case, we cannot add more data, so we cannot yield, |
| 3039 | * we return the amount of copyied data. |
| 3040 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3041 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3042 | return 1; |
| 3043 | |
| 3044 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3045 | if (max > len - l) |
| 3046 | max = len - l; |
| 3047 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3048 | /* The buffer avalaible size may be not contiguous. This test |
| 3049 | * detects a non contiguous buffer and realign it. |
| 3050 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3051 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3052 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3053 | |
| 3054 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3055 | 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] | 3056 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3057 | /* buffer replace considers that the input part is filled. |
| 3058 | * so, I must forward these new data in the output part. |
| 3059 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3060 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3061 | |
| 3062 | l += max; |
| 3063 | lua_pop(L, 1); |
| 3064 | lua_pushinteger(L, l); |
| 3065 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3066 | /* If there are no space avalaible, and the output buffer is empty. |
| 3067 | * in this case, we cannot add more data, so we cannot yield, |
| 3068 | * we return the amount of copyied data. |
| 3069 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3070 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3071 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3072 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3073 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3074 | if (l < len) { |
| 3075 | /* If we are waiting for space in the response buffer, we |
| 3076 | * must set the flag WAKERESWR. This flag required the task |
| 3077 | * wake up if any activity is detected on the response buffer. |
| 3078 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3079 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3080 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3081 | else |
| 3082 | HLUA_SET_WAKEREQWR(hlua); |
| 3083 | WILL_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] | 3084 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3085 | |
| 3086 | return 1; |
| 3087 | } |
| 3088 | |
| 3089 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3090 | * yield the LUA process, and resume it without checking the |
| 3091 | * input arguments. |
| 3092 | */ |
| 3093 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3094 | { |
| 3095 | MAY_LJMP(check_args(L, 2, "send")); |
| 3096 | lua_pushinteger(L, 0); |
| 3097 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3098 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | /* This function forward and amount of butes. The data pass from |
| 3102 | * the input side of the buffer to the output side, and can be |
| 3103 | * forwarded. This function never fails. |
| 3104 | * |
| 3105 | * The Lua function takes an amount of bytes to be forwarded in |
| 3106 | * imput. It returns the number of bytes forwarded. |
| 3107 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3108 | __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] | 3109 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3110 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3111 | int len; |
| 3112 | int l; |
| 3113 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3114 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3115 | |
| 3116 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3117 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3118 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3119 | |
| 3120 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3121 | if (max > ci_data(chn)) |
| 3122 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3123 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3124 | l += max; |
| 3125 | |
| 3126 | lua_pop(L, 1); |
| 3127 | lua_pushinteger(L, l); |
| 3128 | |
| 3129 | /* Check if it miss bytes to forward. */ |
| 3130 | if (l < len) { |
| 3131 | /* The the input channel or the output channel are closed, we |
| 3132 | * must return the amount of data forwarded. |
| 3133 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3134 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3135 | return 1; |
| 3136 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3137 | /* If we are waiting for space data in the response buffer, we |
| 3138 | * must set the flag WAKERESWR. This flag required the task |
| 3139 | * wake up if any activity is detected on the response buffer. |
| 3140 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3141 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3142 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3143 | else |
| 3144 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3145 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3146 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 3147 | WILL_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] | 3148 | } |
| 3149 | |
| 3150 | return 1; |
| 3151 | } |
| 3152 | |
| 3153 | /* Just check the input and prepare the stack for the previous |
| 3154 | * function "hlua_channel_forward_yield" |
| 3155 | */ |
| 3156 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3157 | { |
| 3158 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3159 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3160 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3161 | |
| 3162 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3163 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | /* Just returns the number of bytes available in the input |
| 3167 | * side of the buffer. This function never fails. |
| 3168 | */ |
| 3169 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3170 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3171 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3172 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3173 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3174 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3175 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3176 | return 1; |
| 3177 | } |
| 3178 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3179 | /* Returns true if the channel is full. */ |
| 3180 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3181 | { |
| 3182 | struct channel *chn; |
| 3183 | int rem; |
| 3184 | |
| 3185 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3186 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3187 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3188 | rem = b_room(&chn->buf); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3189 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3190 | |
| 3191 | lua_pushboolean(L, rem <= 0); |
| 3192 | return 1; |
| 3193 | } |
| 3194 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3195 | /* Just returns the number of bytes available in the output |
| 3196 | * side of the buffer. This function never fails. |
| 3197 | */ |
| 3198 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3199 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3200 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3201 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3202 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3203 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3204 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3205 | return 1; |
| 3206 | } |
| 3207 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3208 | /* |
| 3209 | * |
| 3210 | * |
| 3211 | * Class Fetches |
| 3212 | * |
| 3213 | * |
| 3214 | */ |
| 3215 | |
| 3216 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3217 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3218 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3219 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3220 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3221 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3222 | } |
| 3223 | |
| 3224 | /* This function creates and push in the stack a fetch object according |
| 3225 | * with a current TXN. |
| 3226 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3227 | 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] | 3228 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3229 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3230 | |
| 3231 | /* Check stack size. */ |
| 3232 | if (!lua_checkstack(L, 3)) |
| 3233 | return 0; |
| 3234 | |
| 3235 | /* Create the object: obj[0] = userdata. |
| 3236 | * Note that the base of the Fetches object is the |
| 3237 | * transaction object. |
| 3238 | */ |
| 3239 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3240 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3241 | lua_rawseti(L, -2, 0); |
| 3242 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3243 | hsmp->s = txn->s; |
| 3244 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3245 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3246 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3247 | |
| 3248 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3249 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3250 | lua_setmetatable(L, -2); |
| 3251 | |
| 3252 | return 1; |
| 3253 | } |
| 3254 | |
| 3255 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3256 | * It uses closure argument to store the associated sample-fetch. It |
| 3257 | * returns only one argument or throws an error. An error is thrown |
| 3258 | * only if an error is encountered during the argument parsing. If |
| 3259 | * the "sample-fetch" function fails, nil is returned. |
| 3260 | */ |
| 3261 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3262 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3263 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3264 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3265 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3266 | int i; |
| 3267 | struct sample smp; |
| 3268 | |
| 3269 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3270 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3271 | |
| 3272 | /* Get traditionnal arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3273 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3274 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3275 | /* Check execution authorization. */ |
| 3276 | if (f->use & SMP_USE_HTTP_ANY && |
| 3277 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3278 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3279 | "is not available in Lua services", f->kw); |
| 3280 | WILL_LJMP(lua_error(L)); |
| 3281 | } |
| 3282 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3283 | /* Get extra arguments. */ |
| 3284 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3285 | if (i >= ARGM_NBARGS) |
| 3286 | break; |
| 3287 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3288 | } |
| 3289 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3290 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3291 | |
| 3292 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3293 | 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] | 3294 | |
| 3295 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3296 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3297 | lua_pushfstring(L, "error in arguments"); |
| 3298 | WILL_LJMP(lua_error(L)); |
| 3299 | } |
| 3300 | |
| 3301 | /* Initialise the sample. */ |
| 3302 | memset(&smp, 0, sizeof(smp)); |
| 3303 | |
| 3304 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3305 | 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] | 3306 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3307 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3308 | lua_pushstring(L, ""); |
| 3309 | else |
| 3310 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3311 | return 1; |
| 3312 | } |
| 3313 | |
| 3314 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3315 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3316 | hlua_smp2lua_str(L, &smp); |
| 3317 | else |
| 3318 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3319 | return 1; |
| 3320 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3321 | |
| 3322 | /* |
| 3323 | * |
| 3324 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3325 | * Class Converters |
| 3326 | * |
| 3327 | * |
| 3328 | */ |
| 3329 | |
| 3330 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3331 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3332 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3333 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3334 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3335 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3336 | } |
| 3337 | |
| 3338 | /* This function creates and push in the stack a Converters object |
| 3339 | * according with a current TXN. |
| 3340 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3341 | 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] | 3342 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3343 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3344 | |
| 3345 | /* Check stack size. */ |
| 3346 | if (!lua_checkstack(L, 3)) |
| 3347 | return 0; |
| 3348 | |
| 3349 | /* Create the object: obj[0] = userdata. |
| 3350 | * Note that the base of the Converters object is the |
| 3351 | * same than the TXN object. |
| 3352 | */ |
| 3353 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3354 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3355 | lua_rawseti(L, -2, 0); |
| 3356 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3357 | hsmp->s = txn->s; |
| 3358 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3359 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3360 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3361 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3362 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3363 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3364 | lua_setmetatable(L, -2); |
| 3365 | |
| 3366 | return 1; |
| 3367 | } |
| 3368 | |
| 3369 | /* This function is an LUA binding. It is called with each converter. |
| 3370 | * It uses closure argument to store the associated converter. It |
| 3371 | * returns only one argument or throws an error. An error is thrown |
| 3372 | * only if an error is encountered during the argument parsing. If |
| 3373 | * the converter function function fails, nil is returned. |
| 3374 | */ |
| 3375 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3376 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3377 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3378 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3379 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3380 | int i; |
| 3381 | struct sample smp; |
| 3382 | |
| 3383 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3384 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3385 | |
| 3386 | /* Get traditionnal arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3387 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3388 | |
| 3389 | /* Get extra arguments. */ |
| 3390 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3391 | if (i >= ARGM_NBARGS) |
| 3392 | break; |
| 3393 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3394 | } |
| 3395 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3396 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3397 | |
| 3398 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3399 | 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] | 3400 | |
| 3401 | /* Run the special args checker. */ |
| 3402 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3403 | hlua_pusherror(L, "error in arguments"); |
| 3404 | WILL_LJMP(lua_error(L)); |
| 3405 | } |
| 3406 | |
| 3407 | /* Initialise the sample. */ |
| 3408 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3409 | hlua_pusherror(L, "error in the input argument"); |
| 3410 | WILL_LJMP(lua_error(L)); |
| 3411 | } |
| 3412 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3413 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3414 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3415 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3416 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3417 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3418 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3419 | WILL_LJMP(lua_error(L)); |
| 3420 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3421 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3422 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3423 | hlua_pusherror(L, "error during the input argument casting"); |
| 3424 | WILL_LJMP(lua_error(L)); |
| 3425 | } |
| 3426 | |
| 3427 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3428 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3429 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3430 | lua_pushstring(L, ""); |
| 3431 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3432 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3433 | return 1; |
| 3434 | } |
| 3435 | |
| 3436 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3437 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3438 | hlua_smp2lua_str(L, &smp); |
| 3439 | else |
| 3440 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3441 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3442 | } |
| 3443 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3444 | /* |
| 3445 | * |
| 3446 | * |
| 3447 | * Class AppletTCP |
| 3448 | * |
| 3449 | * |
| 3450 | */ |
| 3451 | |
| 3452 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3453 | * a class stream, otherwise it throws an error. |
| 3454 | */ |
| 3455 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3456 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3457 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3458 | } |
| 3459 | |
| 3460 | /* This function creates and push in the stack an Applet object |
| 3461 | * according with a current TXN. |
| 3462 | */ |
| 3463 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3464 | { |
| 3465 | struct hlua_appctx *appctx; |
| 3466 | struct stream_interface *si = ctx->owner; |
| 3467 | struct stream *s = si_strm(si); |
| 3468 | struct proxy *p = s->be; |
| 3469 | |
| 3470 | /* Check stack size. */ |
| 3471 | if (!lua_checkstack(L, 3)) |
| 3472 | return 0; |
| 3473 | |
| 3474 | /* Create the object: obj[0] = userdata. |
| 3475 | * Note that the base of the Converters object is the |
| 3476 | * same than the TXN object. |
| 3477 | */ |
| 3478 | lua_newtable(L); |
| 3479 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3480 | lua_rawseti(L, -2, 0); |
| 3481 | appctx->appctx = ctx; |
| 3482 | appctx->htxn.s = s; |
| 3483 | appctx->htxn.p = p; |
| 3484 | |
| 3485 | /* Create the "f" field that contains a list of fetches. */ |
| 3486 | lua_pushstring(L, "f"); |
| 3487 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3488 | return 0; |
| 3489 | lua_settable(L, -3); |
| 3490 | |
| 3491 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3492 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3493 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3494 | return 0; |
| 3495 | lua_settable(L, -3); |
| 3496 | |
| 3497 | /* Create the "c" field that contains a list of converters. */ |
| 3498 | lua_pushstring(L, "c"); |
| 3499 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3500 | return 0; |
| 3501 | lua_settable(L, -3); |
| 3502 | |
| 3503 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3504 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3505 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3506 | return 0; |
| 3507 | lua_settable(L, -3); |
| 3508 | |
| 3509 | /* Pop a class stream metatable and affect it to the table. */ |
| 3510 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3511 | lua_setmetatable(L, -2); |
| 3512 | |
| 3513 | return 1; |
| 3514 | } |
| 3515 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3516 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3517 | { |
| 3518 | struct hlua_appctx *appctx; |
| 3519 | struct stream *s; |
| 3520 | const char *name; |
| 3521 | size_t len; |
| 3522 | struct sample smp; |
| 3523 | |
| 3524 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3525 | |
| 3526 | /* It is useles to retrieve the stream, but this function |
| 3527 | * runs only in a stream context. |
| 3528 | */ |
| 3529 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3530 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3531 | s = appctx->htxn.s; |
| 3532 | |
| 3533 | /* Converts the third argument in a sample. */ |
| 3534 | hlua_lua2smp(L, 3, &smp); |
| 3535 | |
| 3536 | /* Store the sample in a variable. */ |
| 3537 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3538 | vars_set_by_name(name, len, &smp); |
| 3539 | return 0; |
| 3540 | } |
| 3541 | |
| 3542 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3543 | { |
| 3544 | struct hlua_appctx *appctx; |
| 3545 | struct stream *s; |
| 3546 | const char *name; |
| 3547 | size_t len; |
| 3548 | struct sample smp; |
| 3549 | |
| 3550 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3551 | |
| 3552 | /* It is useles to retrieve the stream, but this function |
| 3553 | * runs only in a stream context. |
| 3554 | */ |
| 3555 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3556 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3557 | s = appctx->htxn.s; |
| 3558 | |
| 3559 | /* Unset the variable. */ |
| 3560 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3561 | vars_unset_by_name(name, len, &smp); |
| 3562 | return 0; |
| 3563 | } |
| 3564 | |
| 3565 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3566 | { |
| 3567 | struct hlua_appctx *appctx; |
| 3568 | struct stream *s; |
| 3569 | const char *name; |
| 3570 | size_t len; |
| 3571 | struct sample smp; |
| 3572 | |
| 3573 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3574 | |
| 3575 | /* It is useles to retrieve the stream, but this function |
| 3576 | * runs only in a stream context. |
| 3577 | */ |
| 3578 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3579 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3580 | s = appctx->htxn.s; |
| 3581 | |
| 3582 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3583 | if (!vars_get_by_name(name, len, &smp)) { |
| 3584 | lua_pushnil(L); |
| 3585 | return 1; |
| 3586 | } |
| 3587 | |
| 3588 | return hlua_smp2lua(L, &smp); |
| 3589 | } |
| 3590 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3591 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3592 | { |
| 3593 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3594 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3595 | struct hlua *hlua; |
| 3596 | |
| 3597 | /* 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] | 3598 | if (!s->hlua) |
| 3599 | return 0; |
| 3600 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3601 | |
| 3602 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3603 | |
| 3604 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3605 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3606 | |
| 3607 | /* Get and store new value. */ |
| 3608 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3609 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3610 | |
| 3611 | return 0; |
| 3612 | } |
| 3613 | |
| 3614 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3615 | { |
| 3616 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3617 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3618 | struct hlua *hlua; |
| 3619 | |
| 3620 | /* 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] | 3621 | if (!s->hlua) { |
| 3622 | lua_pushnil(L); |
| 3623 | return 1; |
| 3624 | } |
| 3625 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3626 | |
| 3627 | /* Push configuration index in the stack. */ |
| 3628 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3629 | |
| 3630 | return 1; |
| 3631 | } |
| 3632 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3633 | /* If expected data not yet available, it returns a yield. This function |
| 3634 | * consumes the data in the buffer. It returns a string containing the |
| 3635 | * data. This string can be empty. |
| 3636 | */ |
| 3637 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3638 | { |
| 3639 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3640 | struct stream_interface *si = appctx->appctx->owner; |
| 3641 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3642 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3643 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3644 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3645 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3646 | |
| 3647 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3648 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3649 | |
| 3650 | /* Data not yet avalaible. return yield. */ |
| 3651 | if (ret == 0) { |
| 3652 | si_applet_cant_get(si); |
| 3653 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
| 3654 | } |
| 3655 | |
| 3656 | /* End of data: commit the total strings and return. */ |
| 3657 | if (ret < 0) { |
| 3658 | luaL_pushresult(&appctx->b); |
| 3659 | return 1; |
| 3660 | } |
| 3661 | |
| 3662 | /* Ensure that the block 2 length is usable. */ |
| 3663 | if (ret == 1) |
| 3664 | len2 = 0; |
| 3665 | |
| 3666 | /* dont check the max length read and dont check. */ |
| 3667 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3668 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3669 | |
| 3670 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3671 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3672 | luaL_pushresult(&appctx->b); |
| 3673 | return 1; |
| 3674 | } |
| 3675 | |
| 3676 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3677 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3678 | { |
| 3679 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3680 | |
| 3681 | /* Initialise the string catenation. */ |
| 3682 | luaL_buffinit(L, &appctx->b); |
| 3683 | |
| 3684 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3685 | } |
| 3686 | |
| 3687 | /* If expected data not yet available, it returns a yield. This function |
| 3688 | * consumes the data in the buffer. It returns a string containing the |
| 3689 | * data. This string can be empty. |
| 3690 | */ |
| 3691 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3692 | { |
| 3693 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3694 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3695 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3696 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3697 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3698 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3699 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3700 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3701 | |
| 3702 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3703 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3704 | |
| 3705 | /* Data not yet avalaible. return yield. */ |
| 3706 | if (ret == 0) { |
| 3707 | si_applet_cant_get(si); |
| 3708 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3709 | } |
| 3710 | |
| 3711 | /* End of data: commit the total strings and return. */ |
| 3712 | if (ret < 0) { |
| 3713 | luaL_pushresult(&appctx->b); |
| 3714 | return 1; |
| 3715 | } |
| 3716 | |
| 3717 | /* Ensure that the block 2 length is usable. */ |
| 3718 | if (ret == 1) |
| 3719 | len2 = 0; |
| 3720 | |
| 3721 | if (len == -1) { |
| 3722 | |
| 3723 | /* If len == -1, catenate all the data avalaile and |
| 3724 | * yield because we want to get all the data until |
| 3725 | * the end of data stream. |
| 3726 | */ |
| 3727 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3728 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3729 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3730 | si_applet_cant_get(si); |
| 3731 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3732 | |
| 3733 | } else { |
| 3734 | |
| 3735 | /* Copy the fisrt block caping to the length required. */ |
| 3736 | if (len1 > len) |
| 3737 | len1 = len; |
| 3738 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3739 | len -= len1; |
| 3740 | |
| 3741 | /* Copy the second block. */ |
| 3742 | if (len2 > len) |
| 3743 | len2 = len; |
| 3744 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3745 | len -= len2; |
| 3746 | |
| 3747 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3748 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3749 | |
| 3750 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3751 | if (len > 0) { |
| 3752 | lua_pushinteger(L, len); |
| 3753 | lua_replace(L, 2); |
| 3754 | si_applet_cant_get(si); |
| 3755 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3756 | } |
| 3757 | |
| 3758 | /* return the result. */ |
| 3759 | luaL_pushresult(&appctx->b); |
| 3760 | return 1; |
| 3761 | } |
| 3762 | |
| 3763 | /* we never executes this */ |
| 3764 | hlua_pusherror(L, "Lua: internal error"); |
| 3765 | WILL_LJMP(lua_error(L)); |
| 3766 | return 0; |
| 3767 | } |
| 3768 | |
| 3769 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3770 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3771 | { |
| 3772 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3773 | int len = -1; |
| 3774 | |
| 3775 | if (lua_gettop(L) > 2) |
| 3776 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3777 | if (lua_gettop(L) >= 2) { |
| 3778 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3779 | lua_pop(L, 1); |
| 3780 | } |
| 3781 | |
| 3782 | /* Confirm or set the required length */ |
| 3783 | lua_pushinteger(L, len); |
| 3784 | |
| 3785 | /* Initialise the string catenation. */ |
| 3786 | luaL_buffinit(L, &appctx->b); |
| 3787 | |
| 3788 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3789 | } |
| 3790 | |
| 3791 | /* Append data in the output side of the buffer. This data is immediatly |
| 3792 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3793 | * cannot contains the data, the function yield. The function returns -1 |
| 3794 | * if the channel is closed. |
| 3795 | */ |
| 3796 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3797 | { |
| 3798 | size_t len; |
| 3799 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3800 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3801 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3802 | struct stream_interface *si = appctx->appctx->owner; |
| 3803 | struct channel *chn = si_ic(si); |
| 3804 | int max; |
| 3805 | |
| 3806 | /* Get the max amount of data which can write as input in the channel. */ |
| 3807 | max = channel_recv_max(chn); |
| 3808 | if (max > (len - l)) |
| 3809 | max = len - l; |
| 3810 | |
| 3811 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3812 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3813 | |
| 3814 | /* update counters. */ |
| 3815 | l += max; |
| 3816 | lua_pop(L, 1); |
| 3817 | lua_pushinteger(L, l); |
| 3818 | |
| 3819 | /* If some data is not send, declares the situation to the |
| 3820 | * applet, and returns a yield. |
| 3821 | */ |
| 3822 | if (l < len) { |
| 3823 | si_applet_cant_put(si); |
| 3824 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
| 3825 | } |
| 3826 | |
| 3827 | return 1; |
| 3828 | } |
| 3829 | |
| 3830 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3831 | * yield the LUA process, and resume it without checking the |
| 3832 | * input arguments. |
| 3833 | */ |
| 3834 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3835 | { |
| 3836 | MAY_LJMP(check_args(L, 2, "send")); |
| 3837 | lua_pushinteger(L, 0); |
| 3838 | |
| 3839 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3840 | } |
| 3841 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3842 | /* |
| 3843 | * |
| 3844 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3845 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3846 | * |
| 3847 | * |
| 3848 | */ |
| 3849 | |
| 3850 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3851 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3852 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3853 | __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] | 3854 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3855 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3856 | } |
| 3857 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3858 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3859 | * according with a current TXN. |
| 3860 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3861 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3862 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3863 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3864 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3865 | struct stream_interface *si = ctx->owner; |
| 3866 | struct stream *s = si_strm(si); |
| 3867 | struct proxy *px = s->be; |
| 3868 | struct http_txn *txn = s->txn; |
| 3869 | const char *path; |
| 3870 | const char *end; |
| 3871 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3872 | |
| 3873 | /* Check stack size. */ |
| 3874 | if (!lua_checkstack(L, 3)) |
| 3875 | return 0; |
| 3876 | |
| 3877 | /* Create the object: obj[0] = userdata. |
| 3878 | * Note that the base of the Converters object is the |
| 3879 | * same than the TXN object. |
| 3880 | */ |
| 3881 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3882 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3883 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3884 | appctx->appctx = ctx; |
| 3885 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3886 | 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] | 3887 | appctx->htxn.s = s; |
| 3888 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3889 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3890 | /* Create the "f" field that contains a list of fetches. */ |
| 3891 | lua_pushstring(L, "f"); |
| 3892 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3893 | return 0; |
| 3894 | lua_settable(L, -3); |
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 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3897 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3898 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3899 | return 0; |
| 3900 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3901 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3902 | /* Create the "c" field that contains a list of converters. */ |
| 3903 | lua_pushstring(L, "c"); |
| 3904 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3905 | return 0; |
| 3906 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3907 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3908 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3909 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3910 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3911 | return 0; |
| 3912 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3913 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3914 | /* Stores the request method. */ |
| 3915 | lua_pushstring(L, "method"); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3916 | lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3917 | lua_settable(L, -3); |
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 | /* Stores the http version. */ |
| 3920 | lua_pushstring(L, "version"); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3921 | lua_pushlstring(L, ci_head(txn->req.chn) + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3922 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3923 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3924 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3925 | * the array on the top of the stack. |
| 3926 | */ |
| 3927 | lua_pushstring(L, "headers"); |
| 3928 | htxn.s = s; |
| 3929 | htxn.p = px; |
| 3930 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3931 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3932 | return 0; |
| 3933 | lua_settable(L, -3); |
| 3934 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3935 | /* Get path and qs */ |
| 3936 | path = http_get_path(txn); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3937 | if (path) { |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3938 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3939 | p = path; |
| 3940 | while (p < end && *p != '?') |
| 3941 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3942 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3943 | /* Stores the request path. */ |
| 3944 | lua_pushstring(L, "path"); |
| 3945 | lua_pushlstring(L, path, p - path); |
| 3946 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3947 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3948 | /* Stores the query string. */ |
| 3949 | lua_pushstring(L, "qs"); |
| 3950 | if (*p == '?') |
| 3951 | p++; |
| 3952 | lua_pushlstring(L, p, end - p); |
| 3953 | lua_settable(L, -3); |
| 3954 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3955 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3956 | /* Stores the request path. */ |
| 3957 | lua_pushstring(L, "length"); |
| 3958 | lua_pushinteger(L, txn->req.body_len); |
| 3959 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3960 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3961 | /* Create an array of HTTP request headers. */ |
| 3962 | lua_pushstring(L, "headers"); |
| 3963 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3964 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3965 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3966 | /* Create an empty array of HTTP request headers. */ |
| 3967 | lua_pushstring(L, "response"); |
| 3968 | lua_newtable(L); |
| 3969 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3970 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3971 | /* Pop a class stream metatable and affect it to the table. */ |
| 3972 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3973 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3974 | |
| 3975 | return 1; |
| 3976 | } |
| 3977 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3978 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3979 | { |
| 3980 | struct hlua_appctx *appctx; |
| 3981 | struct stream *s; |
| 3982 | const char *name; |
| 3983 | size_t len; |
| 3984 | struct sample smp; |
| 3985 | |
| 3986 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3987 | |
| 3988 | /* It is useles to retrieve the stream, but this function |
| 3989 | * runs only in a stream context. |
| 3990 | */ |
| 3991 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3992 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3993 | s = appctx->htxn.s; |
| 3994 | |
| 3995 | /* Converts the third argument in a sample. */ |
| 3996 | hlua_lua2smp(L, 3, &smp); |
| 3997 | |
| 3998 | /* Store the sample in a variable. */ |
| 3999 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4000 | vars_set_by_name(name, len, &smp); |
| 4001 | return 0; |
| 4002 | } |
| 4003 | |
| 4004 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4005 | { |
| 4006 | struct hlua_appctx *appctx; |
| 4007 | struct stream *s; |
| 4008 | const char *name; |
| 4009 | size_t len; |
| 4010 | struct sample smp; |
| 4011 | |
| 4012 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4013 | |
| 4014 | /* It is useles to retrieve the stream, but this function |
| 4015 | * runs only in a stream context. |
| 4016 | */ |
| 4017 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4018 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4019 | s = appctx->htxn.s; |
| 4020 | |
| 4021 | /* Unset the variable. */ |
| 4022 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4023 | vars_unset_by_name(name, len, &smp); |
| 4024 | return 0; |
| 4025 | } |
| 4026 | |
| 4027 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4028 | { |
| 4029 | struct hlua_appctx *appctx; |
| 4030 | struct stream *s; |
| 4031 | const char *name; |
| 4032 | size_t len; |
| 4033 | struct sample smp; |
| 4034 | |
| 4035 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4036 | |
| 4037 | /* It is useles to retrieve the stream, but this function |
| 4038 | * runs only in a stream context. |
| 4039 | */ |
| 4040 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4041 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4042 | s = appctx->htxn.s; |
| 4043 | |
| 4044 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4045 | if (!vars_get_by_name(name, len, &smp)) { |
| 4046 | lua_pushnil(L); |
| 4047 | return 1; |
| 4048 | } |
| 4049 | |
| 4050 | return hlua_smp2lua(L, &smp); |
| 4051 | } |
| 4052 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4053 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4054 | { |
| 4055 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4056 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4057 | struct hlua *hlua; |
| 4058 | |
| 4059 | /* 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] | 4060 | if (!s->hlua) |
| 4061 | return 0; |
| 4062 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4063 | |
| 4064 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4065 | |
| 4066 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4067 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4068 | |
| 4069 | /* Get and store new value. */ |
| 4070 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4071 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4072 | |
| 4073 | return 0; |
| 4074 | } |
| 4075 | |
| 4076 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4077 | { |
| 4078 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4079 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4080 | struct hlua *hlua; |
| 4081 | |
| 4082 | /* 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] | 4083 | if (!s->hlua) { |
| 4084 | lua_pushnil(L); |
| 4085 | return 1; |
| 4086 | } |
| 4087 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4088 | |
| 4089 | /* Push configuration index in the stack. */ |
| 4090 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4091 | |
| 4092 | return 1; |
| 4093 | } |
| 4094 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4095 | /* If expected data not yet available, it returns a yield. This function |
| 4096 | * consumes the data in the buffer. It returns a string containing the |
| 4097 | * data. This string can be empty. |
| 4098 | */ |
| 4099 | __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] | 4100 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4101 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4102 | struct stream_interface *si = appctx->appctx->owner; |
| 4103 | struct channel *chn = si_ic(si); |
| 4104 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4105 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4106 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4107 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4108 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4109 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4110 | /* Maybe we cant send a 100-continue ? */ |
| 4111 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4112 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4113 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4114 | * big for the buffers. We cant send anything. So, we ignoring |
| 4115 | * the error, considers that the 100-continue is sent, and try |
| 4116 | * to receive. |
| 4117 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4118 | */ |
| 4119 | if (ret == -1) { |
| 4120 | si_applet_cant_put(si); |
| 4121 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 4122 | } |
| 4123 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4124 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4125 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4126 | /* Check for the end of the data. */ |
| 4127 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4128 | luaL_pushresult(&appctx->b); |
| 4129 | return 1; |
| 4130 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4131 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4132 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4133 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4134 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4135 | /* Data not yet avalaible. return yield. */ |
| 4136 | if (ret == 0) { |
| 4137 | si_applet_cant_get(si); |
| 4138 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 4139 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4140 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4141 | /* End of data: commit the total strings and return. */ |
| 4142 | if (ret < 0) { |
| 4143 | luaL_pushresult(&appctx->b); |
| 4144 | return 1; |
| 4145 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4146 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4147 | /* Ensure that the block 2 length is usable. */ |
| 4148 | if (ret == 1) |
| 4149 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4150 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4151 | /* Copy the fisrt block caping to the length required. */ |
| 4152 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4153 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4154 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4155 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4156 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4157 | /* Copy the second block. */ |
| 4158 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4159 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4160 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4161 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4162 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4163 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4164 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4165 | luaL_pushresult(&appctx->b); |
| 4166 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4167 | } |
| 4168 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4169 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 4170 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4171 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4172 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4173 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4174 | /* Initialise the string catenation. */ |
| 4175 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4176 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4177 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4178 | } |
| 4179 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4180 | /* If expected data not yet available, it returns a yield. This function |
| 4181 | * consumes the data in the buffer. It returns a string containing the |
| 4182 | * data. This string can be empty. |
| 4183 | */ |
| 4184 | __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] | 4185 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4186 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4187 | struct stream_interface *si = appctx->appctx->owner; |
| 4188 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4189 | struct channel *chn = si_ic(si); |
| 4190 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4191 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4192 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4193 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4194 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4195 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4196 | /* Maybe we cant send a 100-continue ? */ |
| 4197 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4198 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4199 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4200 | * big for the buffers. We cant send anything. So, we ignoring |
| 4201 | * the error, considers that the 100-continue is sent, and try |
| 4202 | * to receive. |
| 4203 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4204 | */ |
| 4205 | if (ret == -1) { |
| 4206 | si_applet_cant_put(si); |
| 4207 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4208 | } |
| 4209 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4210 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4211 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4212 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4213 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4214 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4215 | /* Data not yet avalaible. return yield. */ |
| 4216 | if (ret == 0) { |
| 4217 | si_applet_cant_get(si); |
| 4218 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4219 | } |
| 4220 | |
| 4221 | /* End of data: commit the total strings and return. */ |
| 4222 | if (ret < 0) { |
| 4223 | luaL_pushresult(&appctx->b); |
| 4224 | return 1; |
| 4225 | } |
| 4226 | |
| 4227 | /* Ensure that the block 2 length is usable. */ |
| 4228 | if (ret == 1) |
| 4229 | len2 = 0; |
| 4230 | |
| 4231 | /* Copy the fisrt block caping to the length required. */ |
| 4232 | if (len1 > len) |
| 4233 | len1 = len; |
| 4234 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4235 | len -= len1; |
| 4236 | |
| 4237 | /* Copy the second block. */ |
| 4238 | if (len2 > len) |
| 4239 | len2 = len; |
| 4240 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4241 | len -= len2; |
| 4242 | |
| 4243 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4244 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4245 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4246 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4247 | |
| 4248 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 4249 | if (len > 0) { |
| 4250 | lua_pushinteger(L, len); |
| 4251 | lua_replace(L, 2); |
| 4252 | si_applet_cant_get(si); |
| 4253 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4254 | } |
| 4255 | |
| 4256 | /* return the result. */ |
| 4257 | luaL_pushresult(&appctx->b); |
| 4258 | return 1; |
| 4259 | } |
| 4260 | |
| 4261 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 4262 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4263 | { |
| 4264 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4265 | int len = -1; |
| 4266 | |
| 4267 | /* Check arguments. */ |
| 4268 | if (lua_gettop(L) > 2) |
| 4269 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4270 | if (lua_gettop(L) >= 2) { |
| 4271 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4272 | lua_pop(L, 1); |
| 4273 | } |
| 4274 | |
| 4275 | /* Check the required length */ |
| 4276 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4277 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4278 | lua_pushinteger(L, len); |
| 4279 | |
| 4280 | /* Initialise the string catenation. */ |
| 4281 | luaL_buffinit(L, &appctx->b); |
| 4282 | |
| 4283 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4284 | } |
| 4285 | |
| 4286 | /* Append data in the output side of the buffer. This data is immediatly |
| 4287 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 4288 | * cannot contains the data, the function yield. The function returns -1 |
| 4289 | * if the channel is closed. |
| 4290 | */ |
| 4291 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4292 | { |
| 4293 | size_t len; |
| 4294 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4295 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4296 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4297 | struct stream_interface *si = appctx->appctx->owner; |
| 4298 | struct channel *chn = si_ic(si); |
| 4299 | int max; |
| 4300 | |
| 4301 | /* Get the max amount of data which can write as input in the channel. */ |
| 4302 | max = channel_recv_max(chn); |
| 4303 | if (max > (len - l)) |
| 4304 | max = len - l; |
| 4305 | |
| 4306 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4307 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4308 | |
| 4309 | /* update counters. */ |
| 4310 | l += max; |
| 4311 | lua_pop(L, 1); |
| 4312 | lua_pushinteger(L, l); |
| 4313 | |
| 4314 | /* If some data is not send, declares the situation to the |
| 4315 | * applet, and returns a yield. |
| 4316 | */ |
| 4317 | if (l < len) { |
| 4318 | si_applet_cant_put(si); |
| 4319 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
| 4320 | } |
| 4321 | |
| 4322 | return 1; |
| 4323 | } |
| 4324 | |
| 4325 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4326 | * yield the LUA process, and resume it without checking the |
| 4327 | * input arguments. |
| 4328 | */ |
| 4329 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4330 | { |
| 4331 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4332 | size_t len; |
| 4333 | char hex[10]; |
| 4334 | |
| 4335 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4336 | |
| 4337 | /* If transfer encoding chunked is selected, we surround the data |
| 4338 | * by chunk data. |
| 4339 | */ |
| 4340 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4341 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4342 | lua_pushfstring(L, "%s\r\n", hex); |
| 4343 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4344 | lua_pushstring(L, "\r\n"); |
| 4345 | lua_concat(L, 3); |
| 4346 | } |
| 4347 | |
| 4348 | /* This interger is used for followinf the amount of data sent. */ |
| 4349 | lua_pushinteger(L, 0); |
| 4350 | |
| 4351 | /* We want to send some data. Headers must be sent. */ |
| 4352 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4353 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4354 | WILL_LJMP(lua_error(L)); |
| 4355 | } |
| 4356 | |
| 4357 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4358 | } |
| 4359 | |
| 4360 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4361 | { |
| 4362 | const char *name; |
| 4363 | int ret; |
| 4364 | |
| 4365 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4366 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4367 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4368 | |
| 4369 | /* Push in the stack the "response" entry. */ |
| 4370 | ret = lua_getfield(L, 1, "response"); |
| 4371 | if (ret != LUA_TTABLE) { |
| 4372 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4373 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4374 | WILL_LJMP(lua_error(L)); |
| 4375 | } |
| 4376 | |
| 4377 | /* check if the header is already registered if it is not |
| 4378 | * the case, register it. |
| 4379 | */ |
| 4380 | ret = lua_getfield(L, -1, name); |
| 4381 | if (ret == LUA_TNIL) { |
| 4382 | |
| 4383 | /* Entry not found. */ |
| 4384 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4385 | |
| 4386 | /* Insert the new header name in the array in the top of the stack. |
| 4387 | * It left the new array in the top of the stack. |
| 4388 | */ |
| 4389 | lua_newtable(L); |
| 4390 | lua_pushvalue(L, 2); |
| 4391 | lua_pushvalue(L, -2); |
| 4392 | lua_settable(L, -4); |
| 4393 | |
| 4394 | } else if (ret != LUA_TTABLE) { |
| 4395 | |
| 4396 | /* corruption error. */ |
| 4397 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4398 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4399 | WILL_LJMP(lua_error(L)); |
| 4400 | } |
| 4401 | |
| 4402 | /* Now the top od thestack is an array of values. We push |
| 4403 | * the header value as new entry. |
| 4404 | */ |
| 4405 | lua_pushvalue(L, 3); |
| 4406 | ret = lua_rawlen(L, -2); |
| 4407 | lua_rawseti(L, -2, ret + 1); |
| 4408 | lua_pushboolean(L, 1); |
| 4409 | return 1; |
| 4410 | } |
| 4411 | |
| 4412 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4413 | { |
| 4414 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4415 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4416 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4417 | |
| 4418 | if (status < 100 || status > 599) { |
| 4419 | lua_pushboolean(L, 0); |
| 4420 | return 1; |
| 4421 | } |
| 4422 | |
| 4423 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4424 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4425 | lua_pushboolean(L, 1); |
| 4426 | return 1; |
| 4427 | } |
| 4428 | |
| 4429 | /* We will build the status line and the headers of the HTTP response. |
| 4430 | * We will try send at once if its not possible, we give back the hand |
| 4431 | * waiting for more room. |
| 4432 | */ |
| 4433 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4434 | { |
| 4435 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4436 | struct stream_interface *si = appctx->appctx->owner; |
| 4437 | struct channel *chn = si_ic(si); |
| 4438 | int ret; |
| 4439 | size_t len; |
| 4440 | const char *msg; |
| 4441 | |
| 4442 | /* Get the message as the first argument on the stack. */ |
| 4443 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4444 | |
| 4445 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4446 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4447 | |
| 4448 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4449 | * big for the buffers. |
| 4450 | */ |
| 4451 | if (ret == -2 || ret == -3) { |
| 4452 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4453 | WILL_LJMP(lua_error(L)); |
| 4454 | } |
| 4455 | |
| 4456 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4457 | if (ret == -1) { |
| 4458 | si_applet_cant_put(si); |
| 4459 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
| 4460 | } |
| 4461 | |
| 4462 | /* Headers sent, set the flag. */ |
| 4463 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4464 | return 0; |
| 4465 | } |
| 4466 | |
| 4467 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4468 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4469 | struct buffer *tmp = get_trash_chunk(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4470 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4471 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4472 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4473 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4474 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4475 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4476 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4477 | int hdr_chunked = 0; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4478 | const char *reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4479 | |
| 4480 | if (reason == NULL) |
| 4481 | reason = get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4482 | |
| 4483 | /* Use the same http version than the request. */ |
| 4484 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4485 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4486 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4487 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4488 | |
| 4489 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4490 | lua_pushvalue(L, 0); |
| 4491 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4492 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4493 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4494 | WILL_LJMP(lua_error(L)); |
| 4495 | } |
| 4496 | |
| 4497 | /* Browse the list of headers. */ |
| 4498 | lua_pushnil(L); |
| 4499 | while(lua_next(L, -2) != 0) { |
| 4500 | |
| 4501 | /* We expect a string as -2. */ |
| 4502 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4503 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4504 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4505 | lua_typename(L, lua_type(L, -2))); |
| 4506 | WILL_LJMP(lua_error(L)); |
| 4507 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4508 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4509 | |
| 4510 | /* We expect an array as -1. */ |
| 4511 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4512 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4513 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4514 | name, |
| 4515 | lua_typename(L, lua_type(L, -1))); |
| 4516 | WILL_LJMP(lua_error(L)); |
| 4517 | } |
| 4518 | |
| 4519 | /* Browse the table who is on the top of the stack. */ |
| 4520 | lua_pushnil(L); |
| 4521 | while(lua_next(L, -2) != 0) { |
| 4522 | |
| 4523 | /* We expect a number as -2. */ |
| 4524 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4525 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4526 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4527 | name, |
| 4528 | lua_typename(L, lua_type(L, -2))); |
| 4529 | WILL_LJMP(lua_error(L)); |
| 4530 | } |
| 4531 | id = lua_tointeger(L, -2); |
| 4532 | |
| 4533 | /* We expect a string as -2. */ |
| 4534 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4535 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4536 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4537 | name, id, |
| 4538 | lua_typename(L, lua_type(L, -1))); |
| 4539 | WILL_LJMP(lua_error(L)); |
| 4540 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4541 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4542 | |
| 4543 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4544 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 4545 | memcpy(tmp->area + tmp->data, name, name_len); |
| 4546 | tmp->data += name_len; |
| 4547 | tmp->area[tmp->data++] = ':'; |
| 4548 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4549 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4550 | memcpy(tmp->area + tmp->data, value, |
| 4551 | value_len); |
| 4552 | tmp->data += value_len; |
| 4553 | tmp->area[tmp->data++] = '\r'; |
| 4554 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4555 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4556 | |
| 4557 | /* Protocol checks. */ |
| 4558 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4559 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4560 | * is done without control. If it contains a bad value, |
| 4561 | * the content-length remains negative so that we can |
| 4562 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4563 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4564 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4565 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4566 | |
| 4567 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4568 | if (name_len == 17 && value_len == 7 && |
| 4569 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4570 | strcasecmp("chunked", value) == 0) |
| 4571 | hdr_chunked = 1; |
| 4572 | |
| 4573 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4574 | lua_pop(L, 1); |
| 4575 | } |
| 4576 | |
| 4577 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4578 | lua_pop(L, 1); |
| 4579 | } |
| 4580 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4581 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4582 | * and the status code implies the presence of a message body, we must |
| 4583 | * announce a transfer encoding chunked. This is required by haproxy |
| 4584 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4585 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4586 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4587 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4588 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 4589 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4590 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4591 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4592 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4593 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4594 | } |
| 4595 | |
| 4596 | /* Finalize headers. */ |
| 4597 | chunk_appendf(tmp, "\r\n"); |
| 4598 | |
| 4599 | /* Remove the last entry and the array of headers */ |
| 4600 | lua_pop(L, 2); |
| 4601 | |
| 4602 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4603 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4604 | |
| 4605 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4606 | } |
| 4607 | |
| 4608 | /* |
| 4609 | * |
| 4610 | * |
| 4611 | * Class HTTP |
| 4612 | * |
| 4613 | * |
| 4614 | */ |
| 4615 | |
| 4616 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4617 | * a class stream, otherwise it throws an error. |
| 4618 | */ |
| 4619 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4620 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4621 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4622 | } |
| 4623 | |
| 4624 | /* This function creates and push in the stack a HTTP object |
| 4625 | * according with a current TXN. |
| 4626 | */ |
| 4627 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4628 | { |
| 4629 | struct hlua_txn *htxn; |
| 4630 | |
| 4631 | /* Check stack size. */ |
| 4632 | if (!lua_checkstack(L, 3)) |
| 4633 | return 0; |
| 4634 | |
| 4635 | /* Create the object: obj[0] = userdata. |
| 4636 | * Note that the base of the Converters object is the |
| 4637 | * same than the TXN object. |
| 4638 | */ |
| 4639 | lua_newtable(L); |
| 4640 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4641 | lua_rawseti(L, -2, 0); |
| 4642 | |
| 4643 | htxn->s = txn->s; |
| 4644 | htxn->p = txn->p; |
| 4645 | |
| 4646 | /* Pop a class stream metatable and affect it to the table. */ |
| 4647 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4648 | lua_setmetatable(L, -2); |
| 4649 | |
| 4650 | return 1; |
| 4651 | } |
| 4652 | |
| 4653 | /* This function creates ans returns an array of HTTP headers. |
| 4654 | * This function does not fails. It is used as wrapper with the |
| 4655 | * 2 following functions. |
| 4656 | */ |
| 4657 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4658 | { |
| 4659 | const char *cur_ptr, *cur_next, *p; |
| 4660 | int old_idx, cur_idx; |
| 4661 | struct hdr_idx_elem *cur_hdr; |
| 4662 | const char *hn, *hv; |
| 4663 | int hnl, hvl; |
| 4664 | int type; |
| 4665 | const char *in; |
| 4666 | char *out; |
| 4667 | int len; |
| 4668 | |
| 4669 | /* Create the table. */ |
| 4670 | lua_newtable(L); |
| 4671 | |
| 4672 | if (!htxn->s->txn) |
| 4673 | return 1; |
| 4674 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4675 | /* Check if a valid response is parsed */ |
| 4676 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4677 | return 1; |
| 4678 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4679 | /* Build array of headers. */ |
| 4680 | old_idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 4681 | cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4682 | |
| 4683 | while (1) { |
| 4684 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4685 | if (!cur_idx) |
| 4686 | break; |
| 4687 | old_idx = cur_idx; |
| 4688 | |
| 4689 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4690 | cur_ptr = cur_next; |
| 4691 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4692 | |
| 4693 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4694 | * and the next header starts at cur_next. We'll check |
| 4695 | * this header in the list as well as against the default |
| 4696 | * rule. |
| 4697 | */ |
| 4698 | |
| 4699 | /* look for ': *'. */ |
| 4700 | hn = cur_ptr; |
| 4701 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4702 | if (p >= cur_ptr+cur_hdr->len) |
| 4703 | continue; |
| 4704 | hnl = p - hn; |
| 4705 | p++; |
| 4706 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4707 | p++; |
| 4708 | if (p >= cur_ptr+cur_hdr->len) |
| 4709 | continue; |
| 4710 | hv = p; |
| 4711 | hvl = cur_ptr+cur_hdr->len-p; |
| 4712 | |
| 4713 | /* Lowercase the key. Don't check the size of trash, it have |
| 4714 | * the size of one buffer and the input data contains in one |
| 4715 | * buffer. |
| 4716 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4717 | out = trash.area; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4718 | for (in=hn; in<hn+hnl; in++, out++) |
| 4719 | *out = tolower(*in); |
| 4720 | *out = '\0'; |
| 4721 | |
| 4722 | /* Check for existing entry: |
| 4723 | * assume that the table is on the top of the stack, and |
| 4724 | * push the key in the stack, the function lua_gettable() |
| 4725 | * perform the lookup. |
| 4726 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4727 | lua_pushlstring(L, trash.area, hnl); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4728 | lua_gettable(L, -2); |
| 4729 | type = lua_type(L, -1); |
| 4730 | |
| 4731 | switch (type) { |
| 4732 | case LUA_TNIL: |
| 4733 | /* Table not found, create it. */ |
| 4734 | lua_pop(L, 1); /* remove the nil value. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4735 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4736 | lua_newtable(L); /* create and push empty table. */ |
| 4737 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4738 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4739 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4740 | break; |
| 4741 | |
| 4742 | case LUA_TTABLE: |
| 4743 | /* Entry found: push the value in the table. */ |
| 4744 | len = lua_rawlen(L, -1); |
| 4745 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4746 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4747 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4748 | break; |
| 4749 | |
| 4750 | default: |
| 4751 | /* Other cases are errors. */ |
| 4752 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4753 | WILL_LJMP(lua_error(L)); |
| 4754 | } |
| 4755 | } |
| 4756 | |
| 4757 | return 1; |
| 4758 | } |
| 4759 | |
| 4760 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4761 | { |
| 4762 | struct hlua_txn *htxn; |
| 4763 | |
| 4764 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4765 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4766 | |
| 4767 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4768 | } |
| 4769 | |
| 4770 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4771 | { |
| 4772 | struct hlua_txn *htxn; |
| 4773 | |
| 4774 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4775 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4776 | |
| 4777 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4778 | } |
| 4779 | |
| 4780 | /* This function replace full header, or just a value in |
| 4781 | * the request or in the response. It is a wrapper fir the |
| 4782 | * 4 following functions. |
| 4783 | */ |
| 4784 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4785 | struct http_msg *msg, int action) |
| 4786 | { |
| 4787 | size_t name_len; |
| 4788 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4789 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4790 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4791 | struct my_regex re; |
| 4792 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4793 | /* Check if a valid response is parsed */ |
| 4794 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4795 | return 0; |
| 4796 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4797 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4798 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4799 | |
| 4800 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4801 | regex_free(&re); |
| 4802 | return 0; |
| 4803 | } |
| 4804 | |
| 4805 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4806 | { |
| 4807 | struct hlua_txn *htxn; |
| 4808 | |
| 4809 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4810 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4811 | |
| 4812 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4813 | } |
| 4814 | |
| 4815 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4816 | { |
| 4817 | struct hlua_txn *htxn; |
| 4818 | |
| 4819 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4820 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4821 | |
| 4822 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4823 | } |
| 4824 | |
| 4825 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4826 | { |
| 4827 | struct hlua_txn *htxn; |
| 4828 | |
| 4829 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4830 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4831 | |
| 4832 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4833 | } |
| 4834 | |
| 4835 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4836 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4837 | struct hlua_txn *htxn; |
| 4838 | |
| 4839 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4840 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4841 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4842 | 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] | 4843 | } |
| 4844 | |
| 4845 | /* This function deletes all the occurences of an header. |
| 4846 | * It is a wrapper for the 2 following functions. |
| 4847 | */ |
| 4848 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4849 | { |
| 4850 | size_t len; |
| 4851 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4852 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4853 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4854 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4855 | /* Check if a valid response is parsed */ |
| 4856 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4857 | return 0; |
| 4858 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4859 | ctx.idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 4860 | while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4861 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4862 | return 0; |
| 4863 | } |
| 4864 | |
| 4865 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4866 | { |
| 4867 | struct hlua_txn *htxn; |
| 4868 | |
| 4869 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4870 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4871 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4872 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4873 | } |
| 4874 | |
| 4875 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4876 | { |
| 4877 | struct hlua_txn *htxn; |
| 4878 | |
| 4879 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4880 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4881 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4882 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4883 | } |
| 4884 | |
| 4885 | /* This function adds an header. It is a wrapper used by |
| 4886 | * the 2 following functions. |
| 4887 | */ |
| 4888 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4889 | { |
| 4890 | size_t name_len; |
| 4891 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4892 | size_t value_len; |
| 4893 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4894 | char *p; |
| 4895 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4896 | /* Check if a valid message is parsed */ |
| 4897 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4898 | return 0; |
| 4899 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4900 | /* Check length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4901 | trash.data = value_len + name_len + 2; |
| 4902 | if (trash.data > trash.size) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4903 | return 0; |
| 4904 | |
| 4905 | /* Creates the header string. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4906 | p = trash.area; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4907 | memcpy(p, name, name_len); |
| 4908 | p += name_len; |
| 4909 | *p = ':'; |
| 4910 | p++; |
| 4911 | *p = ' '; |
| 4912 | p++; |
| 4913 | memcpy(p, value, value_len); |
| 4914 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4915 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4916 | trash.area, trash.data) != 0); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4917 | |
| 4918 | return 0; |
| 4919 | } |
| 4920 | |
| 4921 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4922 | { |
| 4923 | struct hlua_txn *htxn; |
| 4924 | |
| 4925 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4926 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4927 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4928 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4929 | } |
| 4930 | |
| 4931 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4932 | { |
| 4933 | struct hlua_txn *htxn; |
| 4934 | |
| 4935 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4936 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4937 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4938 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4939 | } |
| 4940 | |
| 4941 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4942 | { |
| 4943 | struct hlua_txn *htxn; |
| 4944 | |
| 4945 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4946 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4947 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4948 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4949 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4950 | } |
| 4951 | |
| 4952 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4953 | { |
| 4954 | struct hlua_txn *htxn; |
| 4955 | |
| 4956 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4957 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4958 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4959 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4960 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4961 | } |
| 4962 | |
| 4963 | /* This function set the method. */ |
| 4964 | static int hlua_http_req_set_meth(lua_State *L) |
| 4965 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4966 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4967 | size_t name_len; |
| 4968 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4969 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4970 | /* Check if a valid request is parsed */ |
| 4971 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4972 | lua_pushboolean(L, 0); |
| 4973 | return 1; |
| 4974 | } |
| 4975 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4976 | 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] | 4977 | return 1; |
| 4978 | } |
| 4979 | |
| 4980 | /* This function set the method. */ |
| 4981 | static int hlua_http_req_set_path(lua_State *L) |
| 4982 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4983 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4984 | size_t name_len; |
| 4985 | 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] | 4986 | |
| 4987 | /* Check if a valid request is parsed */ |
| 4988 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4989 | lua_pushboolean(L, 0); |
| 4990 | return 1; |
| 4991 | } |
| 4992 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4993 | 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] | 4994 | return 1; |
| 4995 | } |
| 4996 | |
| 4997 | /* This function set the query-string. */ |
| 4998 | static int hlua_http_req_set_query(lua_State *L) |
| 4999 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5000 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5001 | size_t name_len; |
| 5002 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5003 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5004 | /* Check if a valid request is parsed */ |
| 5005 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5006 | lua_pushboolean(L, 0); |
| 5007 | return 1; |
| 5008 | } |
| 5009 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5010 | /* Check length. */ |
| 5011 | if (name_len > trash.size - 1) { |
| 5012 | lua_pushboolean(L, 0); |
| 5013 | return 1; |
| 5014 | } |
| 5015 | |
| 5016 | /* Add the mark question as prefix. */ |
| 5017 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5018 | trash.area[trash.data++] = '?'; |
| 5019 | memcpy(trash.area + trash.data, name, name_len); |
| 5020 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5021 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5022 | lua_pushboolean(L, |
| 5023 | 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] | 5024 | return 1; |
| 5025 | } |
| 5026 | |
| 5027 | /* This function set the uri. */ |
| 5028 | static int hlua_http_req_set_uri(lua_State *L) |
| 5029 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5030 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5031 | size_t name_len; |
| 5032 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5033 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5034 | /* Check if a valid request is parsed */ |
| 5035 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5036 | lua_pushboolean(L, 0); |
| 5037 | return 1; |
| 5038 | } |
| 5039 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5040 | 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] | 5041 | return 1; |
| 5042 | } |
| 5043 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5044 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5045 | static int hlua_http_res_set_status(lua_State *L) |
| 5046 | { |
| 5047 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5048 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5049 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5050 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5051 | /* Check if a valid response is parsed */ |
| 5052 | if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5053 | return 0; |
| 5054 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5055 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5056 | return 0; |
| 5057 | } |
| 5058 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5059 | /* |
| 5060 | * |
| 5061 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5062 | * Class TXN |
| 5063 | * |
| 5064 | * |
| 5065 | */ |
| 5066 | |
| 5067 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5068 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5069 | */ |
| 5070 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5071 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5072 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5073 | } |
| 5074 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5075 | __LJMP static int hlua_set_var(lua_State *L) |
| 5076 | { |
| 5077 | struct hlua_txn *htxn; |
| 5078 | const char *name; |
| 5079 | size_t len; |
| 5080 | struct sample smp; |
| 5081 | |
| 5082 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5083 | |
| 5084 | /* It is useles to retrieve the stream, but this function |
| 5085 | * runs only in a stream context. |
| 5086 | */ |
| 5087 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5088 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5089 | |
| 5090 | /* Converts the third argument in a sample. */ |
| 5091 | hlua_lua2smp(L, 3, &smp); |
| 5092 | |
| 5093 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5094 | 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] | 5095 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5096 | return 0; |
| 5097 | } |
| 5098 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5099 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5100 | { |
| 5101 | struct hlua_txn *htxn; |
| 5102 | const char *name; |
| 5103 | size_t len; |
| 5104 | struct sample smp; |
| 5105 | |
| 5106 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5107 | |
| 5108 | /* It is useles to retrieve the stream, but this function |
| 5109 | * runs only in a stream context. |
| 5110 | */ |
| 5111 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5112 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5113 | |
| 5114 | /* Unset the variable. */ |
| 5115 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5116 | vars_unset_by_name(name, len, &smp); |
| 5117 | return 0; |
| 5118 | } |
| 5119 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5120 | __LJMP static int hlua_get_var(lua_State *L) |
| 5121 | { |
| 5122 | struct hlua_txn *htxn; |
| 5123 | const char *name; |
| 5124 | size_t len; |
| 5125 | struct sample smp; |
| 5126 | |
| 5127 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5128 | |
| 5129 | /* It is useles to retrieve the stream, but this function |
| 5130 | * runs only in a stream context. |
| 5131 | */ |
| 5132 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5133 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5134 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5135 | 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] | 5136 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5137 | lua_pushnil(L); |
| 5138 | return 1; |
| 5139 | } |
| 5140 | |
| 5141 | return hlua_smp2lua(L, &smp); |
| 5142 | } |
| 5143 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5144 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5145 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5146 | struct hlua *hlua; |
| 5147 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5148 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5149 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5150 | /* It is useles to retrieve the stream, but this function |
| 5151 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5152 | */ |
| 5153 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5154 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5155 | |
| 5156 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5157 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5158 | |
| 5159 | /* Get and store new value. */ |
| 5160 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5161 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5162 | |
| 5163 | return 0; |
| 5164 | } |
| 5165 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5166 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5167 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5168 | struct hlua *hlua; |
| 5169 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5170 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5171 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5172 | /* It is useles to retrieve the stream, but this function |
| 5173 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5174 | */ |
| 5175 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5176 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5177 | |
| 5178 | /* Push configuration index in the stack. */ |
| 5179 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5180 | |
| 5181 | return 1; |
| 5182 | } |
| 5183 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5184 | /* Create stack entry containing a class TXN. This function |
| 5185 | * return 0 if the stack does not contains free slots, |
| 5186 | * otherwise it returns 1. |
| 5187 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5188 | 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] | 5189 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5190 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5191 | |
| 5192 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5193 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5194 | return 0; |
| 5195 | |
| 5196 | /* NOTE: The allocation never fails. The failure |
| 5197 | * throw an error, and the function never returns. |
| 5198 | * if the throw is not avalaible, the process is aborted. |
| 5199 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5200 | /* Create the object: obj[0] = userdata. */ |
| 5201 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5202 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5203 | lua_rawseti(L, -2, 0); |
| 5204 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5205 | htxn->s = s; |
| 5206 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5207 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5208 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5209 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5210 | /* Create the "f" field that contains a list of fetches. */ |
| 5211 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5212 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5213 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5214 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5215 | |
| 5216 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5217 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5218 | 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] | 5219 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5220 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5221 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5222 | /* Create the "c" field that contains a list of converters. */ |
| 5223 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5224 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5225 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5226 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5227 | |
| 5228 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5229 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5230 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5231 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5232 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5233 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5234 | /* Create the "req" field that contains the request channel object. */ |
| 5235 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5236 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5237 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5238 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5239 | |
| 5240 | /* Create the "res" field that contains the response channel object. */ |
| 5241 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5242 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5243 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5244 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5245 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5246 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5247 | lua_pushstring(L, "http"); |
| 5248 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5249 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5250 | return 0; |
| 5251 | } |
| 5252 | else |
| 5253 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5254 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5255 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5256 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5257 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5258 | lua_setmetatable(L, -2); |
| 5259 | |
| 5260 | return 1; |
| 5261 | } |
| 5262 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5263 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5264 | { |
| 5265 | const char *msg; |
| 5266 | struct hlua_txn *htxn; |
| 5267 | |
| 5268 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5269 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5270 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5271 | |
| 5272 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5273 | return 0; |
| 5274 | } |
| 5275 | |
| 5276 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5277 | { |
| 5278 | int level; |
| 5279 | const char *msg; |
| 5280 | struct hlua_txn *htxn; |
| 5281 | |
| 5282 | MAY_LJMP(check_args(L, 3, "log")); |
| 5283 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5284 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5285 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5286 | |
| 5287 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5288 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5289 | |
| 5290 | hlua_sendlog(htxn->s->be, level, msg); |
| 5291 | return 0; |
| 5292 | } |
| 5293 | |
| 5294 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5295 | { |
| 5296 | const char *msg; |
| 5297 | struct hlua_txn *htxn; |
| 5298 | |
| 5299 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5300 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5301 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5302 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5303 | return 0; |
| 5304 | } |
| 5305 | |
| 5306 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5307 | { |
| 5308 | const char *msg; |
| 5309 | struct hlua_txn *htxn; |
| 5310 | |
| 5311 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5312 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5313 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5314 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5315 | return 0; |
| 5316 | } |
| 5317 | |
| 5318 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5319 | { |
| 5320 | const char *msg; |
| 5321 | struct hlua_txn *htxn; |
| 5322 | |
| 5323 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5324 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5325 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5326 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5327 | return 0; |
| 5328 | } |
| 5329 | |
| 5330 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5331 | { |
| 5332 | const char *msg; |
| 5333 | struct hlua_txn *htxn; |
| 5334 | |
| 5335 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5336 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5337 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5338 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5339 | return 0; |
| 5340 | } |
| 5341 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5342 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5343 | { |
| 5344 | struct hlua_txn *htxn; |
| 5345 | int ll; |
| 5346 | |
| 5347 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5348 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5349 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5350 | |
| 5351 | if (ll < 0 || ll > 7) |
| 5352 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5353 | |
| 5354 | htxn->s->logs.level = ll; |
| 5355 | return 0; |
| 5356 | } |
| 5357 | |
| 5358 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5359 | { |
| 5360 | struct hlua_txn *htxn; |
| 5361 | struct connection *cli_conn; |
| 5362 | int tos; |
| 5363 | |
| 5364 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5365 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5366 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5367 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5368 | if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5369 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5370 | |
| 5371 | return 0; |
| 5372 | } |
| 5373 | |
| 5374 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5375 | { |
| 5376 | #ifdef SO_MARK |
| 5377 | struct hlua_txn *htxn; |
| 5378 | struct connection *cli_conn; |
| 5379 | int mark; |
| 5380 | |
| 5381 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5382 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5383 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5384 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5385 | if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5386 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5387 | #endif |
| 5388 | return 0; |
| 5389 | } |
| 5390 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5391 | /* This function is an Lua binding that send pending data |
| 5392 | * to the client, and close the stream interface. |
| 5393 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5394 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5395 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5396 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5397 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5398 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5399 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5400 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5401 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5402 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5403 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5404 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5405 | * session, so we just end the execution of the current |
| 5406 | * lua code. |
| 5407 | */ |
| 5408 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5409 | WILL_LJMP(hlua_done(L)); |
| 5410 | return 0; |
| 5411 | } |
| 5412 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5413 | ic = &htxn->s->req; |
| 5414 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5415 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5416 | if (htxn->s->txn) { |
| 5417 | /* HTTP mode, let's stay in sync with the stream */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5418 | b_del(&ic->buf, htxn->s->txn->req.sov); |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5419 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 5420 | htxn->s->txn->req.sov = 0; |
| 5421 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 5422 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 5423 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5424 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 5425 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5426 | /* Note that if we want to support keep-alive, we need |
| 5427 | * to bypass the close/shutr_now calls below, but that |
| 5428 | * may only be done if the HTTP request was already |
| 5429 | * processed and the connection header is known (ie |
| 5430 | * not during TCP rules). |
| 5431 | */ |
| 5432 | } |
| 5433 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5434 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5435 | channel_abort(ic); |
| 5436 | channel_auto_close(ic); |
| 5437 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5438 | |
| 5439 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5440 | channel_auto_read(oc); |
| 5441 | channel_auto_close(oc); |
| 5442 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5443 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 5444 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5445 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5446 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5447 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5448 | return 0; |
| 5449 | } |
| 5450 | |
| 5451 | __LJMP static int hlua_log(lua_State *L) |
| 5452 | { |
| 5453 | int level; |
| 5454 | const char *msg; |
| 5455 | |
| 5456 | MAY_LJMP(check_args(L, 2, "log")); |
| 5457 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5458 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5459 | |
| 5460 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5461 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5462 | |
| 5463 | hlua_sendlog(NULL, level, msg); |
| 5464 | return 0; |
| 5465 | } |
| 5466 | |
| 5467 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5468 | { |
| 5469 | const char *msg; |
| 5470 | |
| 5471 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5472 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5473 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5474 | return 0; |
| 5475 | } |
| 5476 | |
| 5477 | __LJMP static int hlua_log_info(lua_State *L) |
| 5478 | { |
| 5479 | const char *msg; |
| 5480 | |
| 5481 | MAY_LJMP(check_args(L, 1, "info")); |
| 5482 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5483 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5484 | return 0; |
| 5485 | } |
| 5486 | |
| 5487 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5488 | { |
| 5489 | const char *msg; |
| 5490 | |
| 5491 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5492 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5493 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5494 | return 0; |
| 5495 | } |
| 5496 | |
| 5497 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5498 | { |
| 5499 | const char *msg; |
| 5500 | |
| 5501 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5502 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5503 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5504 | return 0; |
| 5505 | } |
| 5506 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5507 | __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] | 5508 | { |
| 5509 | int wakeup_ms = lua_tointeger(L, -1); |
| 5510 | if (now_ms < wakeup_ms) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5511 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5512 | return 0; |
| 5513 | } |
| 5514 | |
| 5515 | __LJMP static int hlua_sleep(lua_State *L) |
| 5516 | { |
| 5517 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5518 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5519 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5520 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5521 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5522 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5523 | wakeup_ms = tick_add(now_ms, delay); |
| 5524 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5525 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5526 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5527 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5528 | } |
| 5529 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5530 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5531 | { |
| 5532 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5533 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5534 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5535 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5536 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5537 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5538 | wakeup_ms = tick_add(now_ms, delay); |
| 5539 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5540 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5541 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5542 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5543 | } |
| 5544 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5545 | /* This functionis an LUA binding. it permits to give back |
| 5546 | * the hand at the HAProxy scheduler. It is used when the |
| 5547 | * LUA processing consumes a lot of time. |
| 5548 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5549 | __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] | 5550 | { |
| 5551 | return 0; |
| 5552 | } |
| 5553 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5554 | __LJMP static int hlua_yield(lua_State *L) |
| 5555 | { |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5556 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 5557 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5558 | } |
| 5559 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5560 | /* This function change the nice of the currently executed |
| 5561 | * task. It is used set low or high priority at the current |
| 5562 | * task. |
| 5563 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5564 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5565 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5566 | struct hlua *hlua; |
| 5567 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5568 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5569 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5570 | hlua = hlua_gethlua(L); |
| 5571 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5572 | |
| 5573 | /* If he task is not set, I'm in a start mode. */ |
| 5574 | if (!hlua || !hlua->task) |
| 5575 | return 0; |
| 5576 | |
| 5577 | if (nice < -1024) |
| 5578 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5579 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5580 | nice = 1024; |
| 5581 | |
| 5582 | hlua->task->nice = nice; |
| 5583 | return 0; |
| 5584 | } |
| 5585 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5586 | /* This function is used as a calback of a task. It is called by the |
| 5587 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5588 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5589 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5590 | * |
| 5591 | * Task wrapper are longjmp safe because the only one Lua code |
| 5592 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5593 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5594 | 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] | 5595 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5596 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5597 | enum hlua_exec status; |
| 5598 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 5599 | if (task->thread_mask == MAX_THREADS_MASK) |
| 5600 | task_set_affinity(task, tid_bit); |
| 5601 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5602 | /* If it is the first call to the task, we must initialize the |
| 5603 | * execution timeouts. |
| 5604 | */ |
| 5605 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5606 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5607 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5608 | /* Execute the Lua code. */ |
| 5609 | status = hlua_ctx_resume(hlua, 1); |
| 5610 | |
| 5611 | switch (status) { |
| 5612 | /* finished or yield */ |
| 5613 | case HLUA_E_OK: |
| 5614 | hlua_ctx_destroy(hlua); |
| 5615 | task_delete(task); |
| 5616 | task_free(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 5617 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5618 | break; |
| 5619 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5620 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 5621 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 5622 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5623 | break; |
| 5624 | |
| 5625 | /* finished with error. */ |
| 5626 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5627 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5628 | hlua_ctx_destroy(hlua); |
| 5629 | task_delete(task); |
| 5630 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5631 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5632 | break; |
| 5633 | |
| 5634 | case HLUA_E_ERR: |
| 5635 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5636 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5637 | hlua_ctx_destroy(hlua); |
| 5638 | task_delete(task); |
| 5639 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5640 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5641 | break; |
| 5642 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5643 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5644 | } |
| 5645 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5646 | /* This function is an LUA binding that register LUA function to be |
| 5647 | * executed after the HAProxy configuration parsing and before the |
| 5648 | * HAProxy scheduler starts. This function expect only one LUA |
| 5649 | * argument that is a function. This function returns nothing, but |
| 5650 | * throws if an error is encountered. |
| 5651 | */ |
| 5652 | __LJMP static int hlua_register_init(lua_State *L) |
| 5653 | { |
| 5654 | struct hlua_init_function *init; |
| 5655 | int ref; |
| 5656 | |
| 5657 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5658 | |
| 5659 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5660 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5661 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5662 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5663 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5664 | |
| 5665 | init->function_ref = ref; |
| 5666 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5667 | return 0; |
| 5668 | } |
| 5669 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5670 | /* This functio is an LUA binding. It permits to register a task |
| 5671 | * executed in parallel of the main HAroxy activity. The task is |
| 5672 | * created and it is set in the HAProxy scheduler. It can be called |
| 5673 | * from the "init" section, "post init" or during the runtime. |
| 5674 | * |
| 5675 | * Lua prototype: |
| 5676 | * |
| 5677 | * <none> core.register_task(<function>) |
| 5678 | */ |
| 5679 | static int hlua_register_task(lua_State *L) |
| 5680 | { |
| 5681 | struct hlua *hlua; |
| 5682 | struct task *task; |
| 5683 | int ref; |
| 5684 | |
| 5685 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5686 | |
| 5687 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5688 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5689 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5690 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5691 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5692 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 5693 | task = task_new(MAX_THREADS_MASK); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5694 | task->context = hlua; |
| 5695 | task->process = hlua_process_task; |
| 5696 | |
| 5697 | if (!hlua_ctx_init(hlua, task)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5698 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5699 | |
| 5700 | /* Restore the function in the stack. */ |
| 5701 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5702 | hlua->nargs = 0; |
| 5703 | |
| 5704 | /* Schedule task. */ |
| 5705 | task_schedule(task, now_ms); |
| 5706 | |
| 5707 | return 0; |
| 5708 | } |
| 5709 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5710 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5711 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5712 | * resume converters. |
| 5713 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5714 | 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] | 5715 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5716 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5717 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5718 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5719 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5720 | if (!stream) |
| 5721 | return 0; |
| 5722 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5723 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5724 | * Lua context can be not initialized. This behavior |
| 5725 | * permits to save performances because a systematic |
| 5726 | * Lua initialization cause 5% performances loss. |
| 5727 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5728 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5729 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5730 | if (!stream->hlua) { |
| 5731 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5732 | return 0; |
| 5733 | } |
| 5734 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5735 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5736 | return 0; |
| 5737 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5738 | } |
| 5739 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5740 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5741 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5742 | |
| 5743 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5744 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5745 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5746 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5747 | else |
| 5748 | error = "critical error"; |
| 5749 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5750 | return 0; |
| 5751 | } |
| 5752 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5753 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5754 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5755 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5756 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5757 | return 0; |
| 5758 | } |
| 5759 | |
| 5760 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5761 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5762 | |
| 5763 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5764 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5765 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5766 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5767 | return 0; |
| 5768 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5769 | hlua_smp2lua(stream->hlua->T, smp); |
| 5770 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5771 | |
| 5772 | /* push keywords in the stack. */ |
| 5773 | if (arg_p) { |
| 5774 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5775 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5776 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5777 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5778 | return 0; |
| 5779 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5780 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5781 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5782 | } |
| 5783 | } |
| 5784 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5785 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5786 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5787 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5788 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5789 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5790 | } |
| 5791 | |
| 5792 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5793 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5794 | /* finished. */ |
| 5795 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5796 | /* If the stack is empty, the function fails. */ |
| 5797 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5798 | return 0; |
| 5799 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5800 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5801 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5802 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5803 | return 1; |
| 5804 | |
| 5805 | /* yield. */ |
| 5806 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5807 | 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] | 5808 | return 0; |
| 5809 | |
| 5810 | /* finished with error. */ |
| 5811 | case HLUA_E_ERRMSG: |
| 5812 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5813 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5814 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5815 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5816 | return 0; |
| 5817 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5818 | case HLUA_E_ETMOUT: |
| 5819 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 5820 | return 0; |
| 5821 | |
| 5822 | case HLUA_E_NOMEM: |
| 5823 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 5824 | return 0; |
| 5825 | |
| 5826 | case HLUA_E_YIELD: |
| 5827 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 5828 | return 0; |
| 5829 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5830 | case HLUA_E_ERR: |
| 5831 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5832 | 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] | 5833 | |
| 5834 | default: |
| 5835 | return 0; |
| 5836 | } |
| 5837 | } |
| 5838 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5839 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5840 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5841 | * resume sample-fetches. This function will be called by the sample |
| 5842 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5843 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5844 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5845 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5846 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5847 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5848 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5849 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5850 | const struct buffer msg = { }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5851 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5852 | if (!stream) |
| 5853 | return 0; |
| 5854 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5855 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5856 | * Lua context can be not initialized. This behavior |
| 5857 | * permits to save performances because a systematic |
| 5858 | * Lua initialization cause 5% performances loss. |
| 5859 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5860 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5861 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5862 | if (!stream->hlua) { |
| 5863 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5864 | return 0; |
| 5865 | } |
| 5866 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5867 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5868 | return 0; |
| 5869 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5870 | } |
| 5871 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5872 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5873 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5874 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5875 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5876 | |
| 5877 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5878 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5879 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5880 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5881 | else |
| 5882 | error = "critical error"; |
| 5883 | 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] | 5884 | return 0; |
| 5885 | } |
| 5886 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5887 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5888 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5889 | 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] | 5890 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5891 | return 0; |
| 5892 | } |
| 5893 | |
| 5894 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5895 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5896 | |
| 5897 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5898 | 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] | 5899 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5900 | 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] | 5901 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5902 | return 0; |
| 5903 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5904 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5905 | |
| 5906 | /* push keywords in the stack. */ |
| 5907 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5908 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5909 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5910 | 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] | 5911 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5912 | return 0; |
| 5913 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5914 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5915 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5916 | } |
| 5917 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5918 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5919 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5920 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5921 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5922 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5923 | } |
| 5924 | |
| 5925 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5926 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5927 | /* finished. */ |
| 5928 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5929 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5930 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5931 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5932 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5933 | /* If the stack is empty, the function fails. */ |
| 5934 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5935 | return 0; |
| 5936 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5937 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5938 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5939 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5940 | |
| 5941 | /* Set the end of execution flag. */ |
| 5942 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5943 | return 1; |
| 5944 | |
| 5945 | /* yield. */ |
| 5946 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5947 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5948 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5949 | 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] | 5950 | return 0; |
| 5951 | |
| 5952 | /* finished with error. */ |
| 5953 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5954 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5955 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5956 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5957 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5958 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5959 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5960 | return 0; |
| 5961 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5962 | case HLUA_E_ETMOUT: |
| 5963 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 5964 | stream_int_retnclose(&stream->si[0], &msg); |
| 5965 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 5966 | return 0; |
| 5967 | |
| 5968 | case HLUA_E_NOMEM: |
| 5969 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 5970 | stream_int_retnclose(&stream->si[0], &msg); |
| 5971 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 5972 | return 0; |
| 5973 | |
| 5974 | case HLUA_E_YIELD: |
| 5975 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 5976 | stream_int_retnclose(&stream->si[0], &msg); |
| 5977 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 5978 | return 0; |
| 5979 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5980 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5981 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5982 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5983 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5984 | 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] | 5985 | |
| 5986 | default: |
| 5987 | return 0; |
| 5988 | } |
| 5989 | } |
| 5990 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5991 | /* This function is an LUA binding used for registering |
| 5992 | * "sample-conv" functions. It expects a converter name used |
| 5993 | * in the haproxy configuration file, and an LUA function. |
| 5994 | */ |
| 5995 | __LJMP static int hlua_register_converters(lua_State *L) |
| 5996 | { |
| 5997 | struct sample_conv_kw_list *sck; |
| 5998 | const char *name; |
| 5999 | int ref; |
| 6000 | int len; |
| 6001 | struct hlua_function *fcn; |
| 6002 | |
| 6003 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6004 | |
| 6005 | /* First argument : converter name. */ |
| 6006 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6007 | |
| 6008 | /* Second argument : lua function. */ |
| 6009 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6010 | |
| 6011 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6012 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6013 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6014 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6015 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6016 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6017 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6018 | |
| 6019 | /* Fill fcn. */ |
| 6020 | fcn->name = strdup(name); |
| 6021 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6022 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6023 | fcn->function_ref = ref; |
| 6024 | |
| 6025 | /* List head */ |
| 6026 | sck->list.n = sck->list.p = NULL; |
| 6027 | |
| 6028 | /* converter keyword. */ |
| 6029 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6030 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6031 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6032 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6033 | |
| 6034 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6035 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6036 | 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] | 6037 | sck->kw[0].val_args = NULL; |
| 6038 | sck->kw[0].in_type = SMP_T_STR; |
| 6039 | sck->kw[0].out_type = SMP_T_STR; |
| 6040 | sck->kw[0].private = fcn; |
| 6041 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6042 | /* Register this new converter */ |
| 6043 | sample_register_convs(sck); |
| 6044 | |
| 6045 | return 0; |
| 6046 | } |
| 6047 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6048 | /* This fucntion is an LUA binding used for registering |
| 6049 | * "sample-fetch" functions. It expects a converter name used |
| 6050 | * in the haproxy configuration file, and an LUA function. |
| 6051 | */ |
| 6052 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6053 | { |
| 6054 | const char *name; |
| 6055 | int ref; |
| 6056 | int len; |
| 6057 | struct sample_fetch_kw_list *sfk; |
| 6058 | struct hlua_function *fcn; |
| 6059 | |
| 6060 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6061 | |
| 6062 | /* First argument : sample-fetch name. */ |
| 6063 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6064 | |
| 6065 | /* Second argument : lua function. */ |
| 6066 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6067 | |
| 6068 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6069 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6070 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6071 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6072 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6073 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6074 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6075 | |
| 6076 | /* Fill fcn. */ |
| 6077 | fcn->name = strdup(name); |
| 6078 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6079 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6080 | fcn->function_ref = ref; |
| 6081 | |
| 6082 | /* List head */ |
| 6083 | sfk->list.n = sfk->list.p = NULL; |
| 6084 | |
| 6085 | /* sample-fetch keyword. */ |
| 6086 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6087 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6088 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6089 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6090 | |
| 6091 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6092 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6093 | 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] | 6094 | sfk->kw[0].val_args = NULL; |
| 6095 | sfk->kw[0].out_type = SMP_T_STR; |
| 6096 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6097 | sfk->kw[0].val = 0; |
| 6098 | sfk->kw[0].private = fcn; |
| 6099 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6100 | /* Register this new fetch. */ |
| 6101 | sample_register_fetches(sfk); |
| 6102 | |
| 6103 | return 0; |
| 6104 | } |
| 6105 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6106 | /* This function is a wrapper to execute each LUA function declared |
| 6107 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6108 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6109 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6110 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6111 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6112 | 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] | 6113 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6114 | { |
| 6115 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6116 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6117 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6118 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6119 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6120 | |
| 6121 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 6122 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 6123 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 6124 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 6125 | 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] | 6126 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6127 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6128 | return ACT_RET_CONT; |
| 6129 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6130 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6131 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6132 | * Lua context can be not initialized. This behavior |
| 6133 | * permits to save performances because a systematic |
| 6134 | * Lua initialization cause 5% performances loss. |
| 6135 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6136 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6137 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6138 | if (!s->hlua) { |
| 6139 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6140 | rule->arg.hlua_rule->fcn.name); |
| 6141 | return ACT_RET_CONT; |
| 6142 | } |
| 6143 | if (!hlua_ctx_init(s->hlua, s->task)) { |
| 6144 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6145 | rule->arg.hlua_rule->fcn.name); |
| 6146 | return ACT_RET_CONT; |
| 6147 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6148 | } |
| 6149 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6150 | consistency_set(s, dir, &s->hlua->cons); |
| 6151 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6152 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6153 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6154 | |
| 6155 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6156 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6157 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6158 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6159 | else |
| 6160 | error = "critical error"; |
| 6161 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6162 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6163 | return ACT_RET_CONT; |
| 6164 | } |
| 6165 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6166 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6167 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6168 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6169 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6170 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6171 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6172 | } |
| 6173 | |
| 6174 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6175 | 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] | 6176 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6177 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6178 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6179 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6180 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6181 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6182 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6183 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6184 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6185 | |
| 6186 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6187 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6188 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6189 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6190 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6191 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6192 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6193 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6194 | lua_pushstring(s->hlua->T, *arg); |
| 6195 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6196 | } |
| 6197 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6198 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6199 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6200 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6201 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6202 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6203 | } |
| 6204 | |
| 6205 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6206 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6207 | /* finished. */ |
| 6208 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6209 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6210 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6211 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6212 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6213 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6214 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6215 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6216 | |
| 6217 | /* yield. */ |
| 6218 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6219 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6220 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6221 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6222 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6223 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6224 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6225 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6226 | /* Some actions can be wake up when a "write" event |
| 6227 | * is detected on a response channel. This is useful |
| 6228 | * only for actions targetted on the requests. |
| 6229 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6230 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6231 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6232 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6233 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6234 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6235 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6236 | s->req.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6237 | /* We can quit the fcuntion without consistency check |
| 6238 | * because HAProxy is not able to manipulate data, it |
| 6239 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6240 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6241 | |
| 6242 | /* finished with error. */ |
| 6243 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6244 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6245 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6246 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6247 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6248 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6249 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6250 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6251 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6252 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6253 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6254 | case HLUA_E_ETMOUT: |
| 6255 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6256 | stream_int_retnclose(&s->si[0], &msg); |
| 6257 | return ACT_RET_ERR; |
| 6258 | } |
| 6259 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6260 | return 0; |
| 6261 | |
| 6262 | case HLUA_E_NOMEM: |
| 6263 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6264 | stream_int_retnclose(&s->si[0], &msg); |
| 6265 | return ACT_RET_ERR; |
| 6266 | } |
| 6267 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6268 | return 0; |
| 6269 | |
| 6270 | case HLUA_E_YIELD: |
| 6271 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6272 | stream_int_retnclose(&s->si[0], &msg); |
| 6273 | return ACT_RET_ERR; |
| 6274 | } |
| 6275 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6276 | rule->arg.hlua_rule->fcn.name); |
| 6277 | return 0; |
| 6278 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6279 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6280 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6281 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6282 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6283 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6284 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6285 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6286 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6287 | |
| 6288 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6289 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6290 | } |
| 6291 | } |
| 6292 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6293 | 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] | 6294 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6295 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6296 | struct stream_interface *si = ctx->owner; |
| 6297 | |
| 6298 | /* If the applet is wake up without any expected work, the sheduler |
| 6299 | * remove it from the run queue. This flag indicate that the applet |
| 6300 | * is waiting for write. If the buffer is full, the main processing |
| 6301 | * will send some data and after call the applet, otherwise it call |
| 6302 | * the applet ASAP. |
| 6303 | */ |
| 6304 | si_applet_cant_put(si); |
| 6305 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6306 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6307 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6308 | } |
| 6309 | |
| 6310 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6311 | { |
| 6312 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6313 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6314 | struct task *task; |
| 6315 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6316 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6317 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6318 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6319 | if (!hlua) { |
| 6320 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6321 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6322 | return 0; |
| 6323 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6324 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6325 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6326 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6327 | |
| 6328 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6329 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6330 | if (!task) { |
| 6331 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6332 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6333 | return 0; |
| 6334 | } |
| 6335 | task->nice = 0; |
| 6336 | task->context = ctx; |
| 6337 | task->process = hlua_applet_wakeup; |
| 6338 | ctx->ctx.hlua_apptcp.task = task; |
| 6339 | |
| 6340 | /* In the execution wrappers linked with a stream, the |
| 6341 | * Lua context can be not initialized. This behavior |
| 6342 | * permits to save performances because a systematic |
| 6343 | * Lua initialization cause 5% performances loss. |
| 6344 | */ |
| 6345 | if (!hlua_ctx_init(hlua, task)) { |
| 6346 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6347 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6348 | return 0; |
| 6349 | } |
| 6350 | |
| 6351 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6352 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6353 | |
| 6354 | /* The following Lua calls can fail. */ |
| 6355 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6356 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6357 | error = lua_tostring(hlua->T, -1); |
| 6358 | else |
| 6359 | error = "critical error"; |
| 6360 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6361 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6362 | RESET_SAFE_LJMP(hlua->T); |
| 6363 | return 0; |
| 6364 | } |
| 6365 | |
| 6366 | /* Check stack available size. */ |
| 6367 | if (!lua_checkstack(hlua->T, 1)) { |
| 6368 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6369 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6370 | RESET_SAFE_LJMP(hlua->T); |
| 6371 | return 0; |
| 6372 | } |
| 6373 | |
| 6374 | /* Restore the function in the stack. */ |
| 6375 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6376 | |
| 6377 | /* Create and and push object stream in the stack. */ |
| 6378 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6379 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6380 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6381 | RESET_SAFE_LJMP(hlua->T); |
| 6382 | return 0; |
| 6383 | } |
| 6384 | hlua->nargs = 1; |
| 6385 | |
| 6386 | /* push keywords in the stack. */ |
| 6387 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6388 | if (!lua_checkstack(hlua->T, 1)) { |
| 6389 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6390 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6391 | RESET_SAFE_LJMP(hlua->T); |
| 6392 | return 0; |
| 6393 | } |
| 6394 | lua_pushstring(hlua->T, *arg); |
| 6395 | hlua->nargs++; |
| 6396 | } |
| 6397 | |
| 6398 | RESET_SAFE_LJMP(hlua->T); |
| 6399 | |
| 6400 | /* Wakeup the applet ASAP. */ |
| 6401 | si_applet_cant_get(si); |
| 6402 | si_applet_cant_put(si); |
| 6403 | |
| 6404 | return 1; |
| 6405 | } |
| 6406 | |
| 6407 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6408 | { |
| 6409 | struct stream_interface *si = ctx->owner; |
| 6410 | struct stream *strm = si_strm(si); |
| 6411 | struct channel *res = si_ic(si); |
| 6412 | struct act_rule *rule = ctx->rule; |
| 6413 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6414 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6415 | |
| 6416 | /* The applet execution is already done. */ |
| 6417 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) |
| 6418 | return; |
| 6419 | |
| 6420 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6421 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6422 | return; |
| 6423 | |
| 6424 | /* Execute the function. */ |
| 6425 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6426 | /* finished. */ |
| 6427 | case HLUA_E_OK: |
| 6428 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6429 | |
| 6430 | /* log time */ |
| 6431 | strm->logs.tv_request = now; |
| 6432 | |
| 6433 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6434 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6435 | res->flags |= CF_READ_NULL; |
| 6436 | si_shutr(si); |
| 6437 | return; |
| 6438 | |
| 6439 | /* yield. */ |
| 6440 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6441 | if (hlua->wake_time != TICK_ETERNITY) |
| 6442 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6443 | return; |
| 6444 | |
| 6445 | /* finished with error. */ |
| 6446 | case HLUA_E_ERRMSG: |
| 6447 | /* Display log. */ |
| 6448 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6449 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6450 | lua_pop(hlua->T, 1); |
| 6451 | goto error; |
| 6452 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6453 | case HLUA_E_ETMOUT: |
| 6454 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6455 | rule->arg.hlua_rule->fcn.name); |
| 6456 | goto error; |
| 6457 | |
| 6458 | case HLUA_E_NOMEM: |
| 6459 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6460 | rule->arg.hlua_rule->fcn.name); |
| 6461 | goto error; |
| 6462 | |
| 6463 | case HLUA_E_YIELD: /* unexpected */ |
| 6464 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6465 | rule->arg.hlua_rule->fcn.name); |
| 6466 | goto error; |
| 6467 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6468 | case HLUA_E_ERR: |
| 6469 | /* Display log. */ |
| 6470 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6471 | rule->arg.hlua_rule->fcn.name); |
| 6472 | goto error; |
| 6473 | |
| 6474 | default: |
| 6475 | goto error; |
| 6476 | } |
| 6477 | |
| 6478 | error: |
| 6479 | |
| 6480 | /* For all other cases, just close the stream. */ |
| 6481 | si_shutw(si); |
| 6482 | si_shutr(si); |
| 6483 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6484 | } |
| 6485 | |
| 6486 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6487 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6488 | task_delete(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6489 | task_free(ctx->ctx.hlua_apptcp.task); |
| 6490 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6491 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6492 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6493 | } |
| 6494 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6495 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6496 | * an errors occurs and -1 if more data are required for initializing |
| 6497 | * the applet. |
| 6498 | */ |
| 6499 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6500 | { |
| 6501 | struct stream_interface *si = ctx->owner; |
| 6502 | struct channel *req = si_oc(si); |
| 6503 | struct http_msg *msg; |
| 6504 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6505 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6506 | char **arg; |
| 6507 | struct hdr_ctx hdr; |
| 6508 | struct task *task; |
| 6509 | struct sample smp; /* just used for a valid call to smp_prefetch_http. */ |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6510 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6511 | |
| 6512 | /* Wait for a full HTTP request. */ |
| 6513 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 6514 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 6515 | return -1; |
| 6516 | return 0; |
| 6517 | } |
| 6518 | txn = strm->txn; |
| 6519 | msg = &txn->req; |
| 6520 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6521 | /* We want two things in HTTP mode : |
| 6522 | * - enforce server-close mode if we were in keep-alive, so that the |
| 6523 | * applet is released after each response ; |
| 6524 | * - enable request body transfer to the applet in order to resync |
| 6525 | * with the response body. |
| 6526 | */ |
| 6527 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 6528 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6529 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6530 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6531 | if (!hlua) { |
| 6532 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6533 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6534 | return 0; |
| 6535 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6536 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6537 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6538 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6539 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6540 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6541 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6542 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6543 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6544 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6545 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6546 | if (!task) { |
| 6547 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6548 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6549 | return 0; |
| 6550 | } |
| 6551 | task->nice = 0; |
| 6552 | task->context = ctx; |
| 6553 | task->process = hlua_applet_wakeup; |
| 6554 | ctx->ctx.hlua_apphttp.task = task; |
| 6555 | |
| 6556 | /* In the execution wrappers linked with a stream, the |
| 6557 | * Lua context can be not initialized. This behavior |
| 6558 | * permits to save performances because a systematic |
| 6559 | * Lua initialization cause 5% performances loss. |
| 6560 | */ |
| 6561 | if (!hlua_ctx_init(hlua, task)) { |
| 6562 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6563 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6564 | return 0; |
| 6565 | } |
| 6566 | |
| 6567 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6568 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6569 | |
| 6570 | /* The following Lua calls can fail. */ |
| 6571 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6572 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6573 | error = lua_tostring(hlua->T, -1); |
| 6574 | else |
| 6575 | error = "critical error"; |
| 6576 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6577 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6578 | return 0; |
| 6579 | } |
| 6580 | |
| 6581 | /* Check stack available size. */ |
| 6582 | if (!lua_checkstack(hlua->T, 1)) { |
| 6583 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6584 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6585 | RESET_SAFE_LJMP(hlua->T); |
| 6586 | return 0; |
| 6587 | } |
| 6588 | |
| 6589 | /* Restore the function in the stack. */ |
| 6590 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6591 | |
| 6592 | /* Create and and push object stream in the stack. */ |
| 6593 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6594 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6595 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6596 | RESET_SAFE_LJMP(hlua->T); |
| 6597 | return 0; |
| 6598 | } |
| 6599 | hlua->nargs = 1; |
| 6600 | |
| 6601 | /* Look for a 100-continue expected. */ |
| 6602 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 6603 | hdr.idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6604 | if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &hdr) && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6605 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 6606 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 6607 | } |
| 6608 | |
| 6609 | /* push keywords in the stack. */ |
| 6610 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6611 | if (!lua_checkstack(hlua->T, 1)) { |
| 6612 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6613 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6614 | RESET_SAFE_LJMP(hlua->T); |
| 6615 | return 0; |
| 6616 | } |
| 6617 | lua_pushstring(hlua->T, *arg); |
| 6618 | hlua->nargs++; |
| 6619 | } |
| 6620 | |
| 6621 | RESET_SAFE_LJMP(hlua->T); |
| 6622 | |
| 6623 | /* Wakeup the applet when data is ready for read. */ |
| 6624 | si_applet_cant_get(si); |
| 6625 | |
| 6626 | return 1; |
| 6627 | } |
| 6628 | |
| 6629 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 6630 | { |
| 6631 | struct stream_interface *si = ctx->owner; |
| 6632 | struct stream *strm = si_strm(si); |
| 6633 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6634 | struct act_rule *rule = ctx->rule; |
| 6635 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6636 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 6637 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 6638 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 6639 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 6640 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6641 | int ret; |
| 6642 | |
| 6643 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6644 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6645 | return; |
| 6646 | |
| 6647 | /* Set the currently running flag. */ |
| 6648 | if (!HLUA_IS_RUNNING(hlua) && |
| 6649 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6650 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6651 | /* Wait for full HTTP analysys. */ |
| 6652 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 6653 | si_applet_cant_get(si); |
| 6654 | return; |
| 6655 | } |
| 6656 | |
| 6657 | /* Store the max amount of bytes that we can read. */ |
| 6658 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 6659 | |
| 6660 | /* We need to flush the request header. This left the body |
| 6661 | * for the Lua. |
| 6662 | */ |
| 6663 | |
| 6664 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6665 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6666 | if (ret == -1) |
| 6667 | return; |
| 6668 | |
| 6669 | /* No data available, ask for more data. */ |
| 6670 | if (ret == 1) |
| 6671 | len2 = 0; |
| 6672 | if (ret == 0) |
| 6673 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 6674 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6675 | si_applet_cant_get(si); |
| 6676 | return; |
| 6677 | } |
| 6678 | |
| 6679 | /* skip the requests bytes. */ |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 6680 | 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] | 6681 | } |
| 6682 | |
| 6683 | /* Executes The applet if it is not done. */ |
| 6684 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6685 | |
| 6686 | /* Execute the function. */ |
| 6687 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6688 | /* finished. */ |
| 6689 | case HLUA_E_OK: |
| 6690 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6691 | break; |
| 6692 | |
| 6693 | /* yield. */ |
| 6694 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6695 | if (hlua->wake_time != TICK_ETERNITY) |
| 6696 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6697 | return; |
| 6698 | |
| 6699 | /* finished with error. */ |
| 6700 | case HLUA_E_ERRMSG: |
| 6701 | /* Display log. */ |
| 6702 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6703 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6704 | lua_pop(hlua->T, 1); |
| 6705 | goto error; |
| 6706 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6707 | case HLUA_E_ETMOUT: |
| 6708 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 6709 | rule->arg.hlua_rule->fcn.name); |
| 6710 | goto error; |
| 6711 | |
| 6712 | case HLUA_E_NOMEM: |
| 6713 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 6714 | rule->arg.hlua_rule->fcn.name); |
| 6715 | goto error; |
| 6716 | |
| 6717 | case HLUA_E_YIELD: /* unexpected */ |
| 6718 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 6719 | rule->arg.hlua_rule->fcn.name); |
| 6720 | goto error; |
| 6721 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6722 | case HLUA_E_ERR: |
| 6723 | /* Display log. */ |
| 6724 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6725 | rule->arg.hlua_rule->fcn.name); |
| 6726 | goto error; |
| 6727 | |
| 6728 | default: |
| 6729 | goto error; |
| 6730 | } |
| 6731 | } |
| 6732 | |
| 6733 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 6734 | |
| 6735 | /* We must send the final chunk. */ |
| 6736 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 6737 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 6738 | |
| 6739 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6740 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6741 | |
| 6742 | /* critical error. */ |
| 6743 | if (ret == -2 || ret == -3) { |
| 6744 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 6745 | rule->arg.hlua_rule->fcn.name); |
| 6746 | goto error; |
| 6747 | } |
| 6748 | |
| 6749 | /* no enough space error. */ |
| 6750 | if (ret == -1) { |
| 6751 | si_applet_cant_put(si); |
| 6752 | return; |
| 6753 | } |
| 6754 | |
| 6755 | /* set the last chunk sent. */ |
| 6756 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6757 | } |
| 6758 | |
| 6759 | /* close the connection. */ |
| 6760 | |
| 6761 | /* status / log */ |
| 6762 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6763 | strm->logs.tv_request = now; |
| 6764 | |
| 6765 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6766 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6767 | res->flags |= CF_READ_NULL; |
| 6768 | si_shutr(si); |
| 6769 | |
| 6770 | return; |
| 6771 | } |
| 6772 | |
| 6773 | error: |
| 6774 | |
| 6775 | /* If we are in HTTP mode, and we are not send any |
| 6776 | * data, return a 500 server error in best effort: |
| 6777 | * if there are no room avalaible in the buffer, |
| 6778 | * just close the connection. |
| 6779 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6780 | ci_putblk(res, error_500, strlen(error_500)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6781 | if (!(strm->flags & SF_ERR_MASK)) |
| 6782 | strm->flags |= SF_ERR_RESOURCE; |
| 6783 | si_shutw(si); |
| 6784 | si_shutr(si); |
| 6785 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6786 | } |
| 6787 | |
| 6788 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6789 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6790 | task_delete(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6791 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6792 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6793 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6794 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6795 | } |
| 6796 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6797 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6798 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6799 | * |
| 6800 | * This function can fail with an abort() due to an Lua critical error. |
| 6801 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6802 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6803 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6804 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6805 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6806 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6807 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6808 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6809 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6810 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6811 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6812 | if (!rule->arg.hlua_rule) { |
| 6813 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6814 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6815 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6816 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6817 | /* Memory for arguments. */ |
| 6818 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6819 | if (!rule->arg.hlua_rule->args) { |
| 6820 | memprintf(err, "out of memory error"); |
| 6821 | return ACT_RET_PRS_ERR; |
| 6822 | } |
| 6823 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6824 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6825 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6826 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6827 | /* Expect some arguments */ |
| 6828 | for (i = 0; i < fcn->nargs; i++) { |
| 6829 | if (*args[i+1] == '\0') { |
| 6830 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6831 | return ACT_RET_PRS_ERR; |
| 6832 | } |
| 6833 | rule->arg.hlua_rule->args[i] = strdup(args[i + 1]); |
| 6834 | if (!rule->arg.hlua_rule->args[i]) { |
| 6835 | memprintf(err, "out of memory error"); |
| 6836 | return ACT_RET_PRS_ERR; |
| 6837 | } |
| 6838 | (*cur_arg)++; |
| 6839 | } |
| 6840 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6841 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6842 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6843 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6844 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6845 | } |
| 6846 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6847 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6848 | struct act_rule *rule, char **err) |
| 6849 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6850 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6851 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6852 | /* HTTP applets are forbidden in tcp-request rules. |
| 6853 | * HTTP applet request requires everything initilized by |
| 6854 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6855 | * The applet will be immediately initilized, but its before |
| 6856 | * the call of this analyzer. |
| 6857 | */ |
| 6858 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6859 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6860 | return ACT_RET_PRS_ERR; |
| 6861 | } |
| 6862 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6863 | /* Memory for the rule. */ |
| 6864 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6865 | if (!rule->arg.hlua_rule) { |
| 6866 | memprintf(err, "out of memory error"); |
| 6867 | return ACT_RET_PRS_ERR; |
| 6868 | } |
| 6869 | |
| 6870 | /* Reference the Lua function and store the reference. */ |
| 6871 | rule->arg.hlua_rule->fcn = *fcn; |
| 6872 | |
| 6873 | /* TODO: later accept arguments. */ |
| 6874 | rule->arg.hlua_rule->args = NULL; |
| 6875 | |
| 6876 | /* Add applet pointer in the rule. */ |
| 6877 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6878 | rule->applet.name = fcn->name; |
| 6879 | rule->applet.init = hlua_applet_http_init; |
| 6880 | rule->applet.fct = hlua_applet_http_fct; |
| 6881 | rule->applet.release = hlua_applet_http_release; |
| 6882 | rule->applet.timeout = hlua_timeout_applet; |
| 6883 | |
| 6884 | return ACT_RET_PRS_OK; |
| 6885 | } |
| 6886 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6887 | /* This function is an LUA binding used for registering |
| 6888 | * "sample-conv" functions. It expects a converter name used |
| 6889 | * in the haproxy configuration file, and an LUA function. |
| 6890 | */ |
| 6891 | __LJMP static int hlua_register_action(lua_State *L) |
| 6892 | { |
| 6893 | struct action_kw_list *akl; |
| 6894 | const char *name; |
| 6895 | int ref; |
| 6896 | int len; |
| 6897 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6898 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6899 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6900 | /* Initialise the number of expected arguments at 0. */ |
| 6901 | nargs = 0; |
| 6902 | |
| 6903 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6904 | 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] | 6905 | |
| 6906 | /* First argument : converter name. */ |
| 6907 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6908 | |
| 6909 | /* Second argument : environment. */ |
| 6910 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6911 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6912 | |
| 6913 | /* Third argument : lua function. */ |
| 6914 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6915 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6916 | /* Fouth argument : number of mandatories arguments expected on the configuration line. */ |
| 6917 | if (lua_gettop(L) >= 4) |
| 6918 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6919 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6920 | /* browse the second argulent as an array. */ |
| 6921 | lua_pushnil(L); |
| 6922 | while (lua_next(L, 2) != 0) { |
| 6923 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6924 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6925 | |
| 6926 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6927 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6928 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6929 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6930 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6931 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6932 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6933 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6934 | |
| 6935 | /* Fill fcn. */ |
| 6936 | fcn->name = strdup(name); |
| 6937 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6938 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6939 | fcn->function_ref = ref; |
| 6940 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6941 | /* Set the expected number od arguments. */ |
| 6942 | fcn->nargs = nargs; |
| 6943 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6944 | /* List head */ |
| 6945 | akl->list.n = akl->list.p = NULL; |
| 6946 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6947 | /* action keyword. */ |
| 6948 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6949 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6950 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6951 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6952 | |
| 6953 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6954 | |
| 6955 | akl->kw[0].match_pfx = 0; |
| 6956 | akl->kw[0].private = fcn; |
| 6957 | akl->kw[0].parse = action_register_lua; |
| 6958 | |
| 6959 | /* select the action registering point. */ |
| 6960 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6961 | tcp_req_cont_keywords_register(akl); |
| 6962 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6963 | tcp_res_cont_keywords_register(akl); |
| 6964 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6965 | http_req_keywords_register(akl); |
| 6966 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6967 | http_res_keywords_register(akl); |
| 6968 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6969 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6970 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6971 | "are expected.", lua_tostring(L, -1))); |
| 6972 | |
| 6973 | /* pop the environment string. */ |
| 6974 | lua_pop(L, 1); |
| 6975 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6976 | return ACT_RET_PRS_OK; |
| 6977 | } |
| 6978 | |
| 6979 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6980 | struct act_rule *rule, char **err) |
| 6981 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6982 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6983 | |
| 6984 | /* Memory for the rule. */ |
| 6985 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6986 | if (!rule->arg.hlua_rule) { |
| 6987 | memprintf(err, "out of memory error"); |
| 6988 | return ACT_RET_PRS_ERR; |
| 6989 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6990 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6991 | /* Reference the Lua function and store the reference. */ |
| 6992 | rule->arg.hlua_rule->fcn = *fcn; |
| 6993 | |
| 6994 | /* TODO: later accept arguments. */ |
| 6995 | rule->arg.hlua_rule->args = NULL; |
| 6996 | |
| 6997 | /* Add applet pointer in the rule. */ |
| 6998 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6999 | rule->applet.name = fcn->name; |
| 7000 | rule->applet.init = hlua_applet_tcp_init; |
| 7001 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7002 | rule->applet.release = hlua_applet_tcp_release; |
| 7003 | rule->applet.timeout = hlua_timeout_applet; |
| 7004 | |
| 7005 | return 0; |
| 7006 | } |
| 7007 | |
| 7008 | /* This function is an LUA binding used for registering |
| 7009 | * "sample-conv" functions. It expects a converter name used |
| 7010 | * in the haproxy configuration file, and an LUA function. |
| 7011 | */ |
| 7012 | __LJMP static int hlua_register_service(lua_State *L) |
| 7013 | { |
| 7014 | struct action_kw_list *akl; |
| 7015 | const char *name; |
| 7016 | const char *env; |
| 7017 | int ref; |
| 7018 | int len; |
| 7019 | struct hlua_function *fcn; |
| 7020 | |
| 7021 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7022 | |
| 7023 | /* First argument : converter name. */ |
| 7024 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7025 | |
| 7026 | /* Second argument : environment. */ |
| 7027 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7028 | |
| 7029 | /* Third argument : lua function. */ |
| 7030 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7031 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7032 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7033 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7034 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7035 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7036 | fcn = calloc(1, sizeof(*fcn)); |
| 7037 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7038 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7039 | |
| 7040 | /* Fill fcn. */ |
| 7041 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7042 | fcn->name = calloc(1, len); |
| 7043 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7044 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7045 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7046 | fcn->function_ref = ref; |
| 7047 | |
| 7048 | /* List head */ |
| 7049 | akl->list.n = akl->list.p = NULL; |
| 7050 | |
| 7051 | /* converter keyword. */ |
| 7052 | len = strlen("lua.") + strlen(name) + 1; |
| 7053 | akl->kw[0].kw = calloc(1, len); |
| 7054 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7055 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7056 | |
| 7057 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7058 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7059 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7060 | if (strcmp(env, "tcp") == 0) |
| 7061 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7062 | else if (strcmp(env, "http") == 0) |
| 7063 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7064 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7065 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7066 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7067 | |
| 7068 | akl->kw[0].match_pfx = 0; |
| 7069 | akl->kw[0].private = fcn; |
| 7070 | |
| 7071 | /* End of array. */ |
| 7072 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7073 | |
| 7074 | /* Register this new converter */ |
| 7075 | service_keywords_register(akl); |
| 7076 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7077 | return 0; |
| 7078 | } |
| 7079 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7080 | /* This function initialises Lua cli handler. It copies the |
| 7081 | * arguments in the Lua stack and create channel IO objects. |
| 7082 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7083 | 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] | 7084 | { |
| 7085 | struct hlua *hlua; |
| 7086 | struct hlua_function *fcn; |
| 7087 | int i; |
| 7088 | const char *error; |
| 7089 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7090 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7091 | appctx->ctx.hlua_cli.fcn = private; |
| 7092 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7093 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7094 | if (!hlua) { |
| 7095 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7096 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7097 | } |
| 7098 | HLUA_INIT(hlua); |
| 7099 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7100 | |
| 7101 | /* Create task used by signal to wakeup applets. |
| 7102 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7103 | * applet_http. It is absolutely compatible. |
| 7104 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7105 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7106 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7107 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7108 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7109 | } |
| 7110 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7111 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7112 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7113 | |
| 7114 | /* Initialises the Lua context */ |
| 7115 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 7116 | 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] | 7117 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7118 | } |
| 7119 | |
| 7120 | /* The following Lua calls can fail. */ |
| 7121 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7122 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7123 | error = lua_tostring(hlua->T, -1); |
| 7124 | else |
| 7125 | error = "critical error"; |
| 7126 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7127 | goto error; |
| 7128 | } |
| 7129 | |
| 7130 | /* Check stack available size. */ |
| 7131 | if (!lua_checkstack(hlua->T, 2)) { |
| 7132 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7133 | goto error; |
| 7134 | } |
| 7135 | |
| 7136 | /* Restore the function in the stack. */ |
| 7137 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7138 | |
| 7139 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7140 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7141 | */ |
| 7142 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7143 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7144 | goto error; |
| 7145 | } |
| 7146 | hlua->nargs = 1; |
| 7147 | |
| 7148 | /* push keywords in the stack. */ |
| 7149 | for (i = 0; *args[i]; i++) { |
| 7150 | /* Check stack available size. */ |
| 7151 | if (!lua_checkstack(hlua->T, 1)) { |
| 7152 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7153 | goto error; |
| 7154 | } |
| 7155 | lua_pushstring(hlua->T, args[i]); |
| 7156 | hlua->nargs++; |
| 7157 | } |
| 7158 | |
| 7159 | /* We must initialize the execution timeouts. */ |
| 7160 | hlua->max_time = hlua_timeout_session; |
| 7161 | |
| 7162 | /* At this point the execution is safe. */ |
| 7163 | RESET_SAFE_LJMP(hlua->T); |
| 7164 | |
| 7165 | /* It's ok */ |
| 7166 | return 0; |
| 7167 | |
| 7168 | /* It's not ok. */ |
| 7169 | error: |
| 7170 | RESET_SAFE_LJMP(hlua->T); |
| 7171 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7172 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7173 | return 1; |
| 7174 | } |
| 7175 | |
| 7176 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7177 | { |
| 7178 | struct hlua *hlua; |
| 7179 | struct stream_interface *si; |
| 7180 | struct hlua_function *fcn; |
| 7181 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7182 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7183 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7184 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7185 | |
| 7186 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7187 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7188 | return 1; |
| 7189 | |
| 7190 | /* Execute the function. */ |
| 7191 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7192 | |
| 7193 | /* finished. */ |
| 7194 | case HLUA_E_OK: |
| 7195 | return 1; |
| 7196 | |
| 7197 | /* yield. */ |
| 7198 | case HLUA_E_AGAIN: |
| 7199 | /* We want write. */ |
| 7200 | if (HLUA_IS_WAKERESWR(hlua)) |
| 7201 | si_applet_cant_put(si); |
| 7202 | /* Set the timeout. */ |
| 7203 | if (hlua->wake_time != TICK_ETERNITY) |
| 7204 | task_schedule(hlua->task, hlua->wake_time); |
| 7205 | return 0; |
| 7206 | |
| 7207 | /* finished with error. */ |
| 7208 | case HLUA_E_ERRMSG: |
| 7209 | /* Display log. */ |
| 7210 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7211 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7212 | lua_pop(hlua->T, 1); |
| 7213 | return 1; |
| 7214 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7215 | case HLUA_E_ETMOUT: |
| 7216 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7217 | fcn->name); |
| 7218 | return 1; |
| 7219 | |
| 7220 | case HLUA_E_NOMEM: |
| 7221 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7222 | fcn->name); |
| 7223 | return 1; |
| 7224 | |
| 7225 | case HLUA_E_YIELD: /* unexpected */ |
| 7226 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7227 | fcn->name); |
| 7228 | return 1; |
| 7229 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7230 | case HLUA_E_ERR: |
| 7231 | /* Display log. */ |
| 7232 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7233 | fcn->name); |
| 7234 | return 1; |
| 7235 | |
| 7236 | default: |
| 7237 | return 1; |
| 7238 | } |
| 7239 | |
| 7240 | return 1; |
| 7241 | } |
| 7242 | |
| 7243 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7244 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7245 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7246 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7247 | } |
| 7248 | |
| 7249 | /* This function is an LUA binding used for registering |
| 7250 | * new keywords in the cli. It expects a list of keywords |
| 7251 | * which are the "path". It is limited to 5 keywords. A |
| 7252 | * description of the command, a function to be executed |
| 7253 | * for the parsing and a function for io handlers. |
| 7254 | */ |
| 7255 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7256 | { |
| 7257 | struct cli_kw_list *cli_kws; |
| 7258 | const char *message; |
| 7259 | int ref_io; |
| 7260 | int len; |
| 7261 | struct hlua_function *fcn; |
| 7262 | int index; |
| 7263 | int i; |
| 7264 | |
| 7265 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7266 | |
| 7267 | /* First argument : an array of maximum 5 keywords. */ |
| 7268 | if (!lua_istable(L, 1)) |
| 7269 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7270 | |
| 7271 | /* Second argument : string with contextual message. */ |
| 7272 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7273 | |
| 7274 | /* Third and fourth argument : lua function. */ |
| 7275 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7276 | |
| 7277 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7278 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7279 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7280 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7281 | fcn = calloc(1, sizeof(*fcn)); |
| 7282 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7283 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7284 | |
| 7285 | /* Fill path. */ |
| 7286 | index = 0; |
| 7287 | lua_pushnil(L); |
| 7288 | while(lua_next(L, 1) != 0) { |
| 7289 | if (index >= 5) |
| 7290 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7291 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7292 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7293 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7294 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7295 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7296 | index++; |
| 7297 | lua_pop(L, 1); |
| 7298 | } |
| 7299 | |
| 7300 | /* Copy help message. */ |
| 7301 | cli_kws->kw[0].usage = strdup(message); |
| 7302 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7303 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7304 | |
| 7305 | /* Fill fcn io handler. */ |
| 7306 | len = strlen("<lua.cli>") + 1; |
| 7307 | for (i = 0; i < index; i++) |
| 7308 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7309 | fcn->name = calloc(1, len); |
| 7310 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7311 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7312 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7313 | for (i = 0; i < index; i++) { |
| 7314 | strncat((char *)fcn->name, ".", len); |
| 7315 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7316 | } |
| 7317 | strncat((char *)fcn->name, ">", len); |
| 7318 | fcn->function_ref = ref_io; |
| 7319 | |
| 7320 | /* Fill last entries. */ |
| 7321 | cli_kws->kw[0].private = fcn; |
| 7322 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7323 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7324 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7325 | |
| 7326 | /* Register this new converter */ |
| 7327 | cli_register_kw(cli_kws); |
| 7328 | |
| 7329 | return 0; |
| 7330 | } |
| 7331 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7332 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7333 | struct proxy *defpx, const char *file, int line, |
| 7334 | char **err, unsigned int *timeout) |
| 7335 | { |
| 7336 | const char *error; |
| 7337 | |
| 7338 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 7339 | if (error && *error != '\0') { |
| 7340 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7341 | return -1; |
| 7342 | } |
| 7343 | return 0; |
| 7344 | } |
| 7345 | |
| 7346 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7347 | struct proxy *defpx, const char *file, int line, |
| 7348 | char **err) |
| 7349 | { |
| 7350 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7351 | file, line, err, &hlua_timeout_session); |
| 7352 | } |
| 7353 | |
| 7354 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7355 | struct proxy *defpx, const char *file, int line, |
| 7356 | char **err) |
| 7357 | { |
| 7358 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7359 | file, line, err, &hlua_timeout_task); |
| 7360 | } |
| 7361 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7362 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7363 | struct proxy *defpx, const char *file, int line, |
| 7364 | char **err) |
| 7365 | { |
| 7366 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7367 | file, line, err, &hlua_timeout_applet); |
| 7368 | } |
| 7369 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7370 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7371 | struct proxy *defpx, const char *file, int line, |
| 7372 | char **err) |
| 7373 | { |
| 7374 | char *error; |
| 7375 | |
| 7376 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7377 | if (*error != '\0') { |
| 7378 | memprintf(err, "%s: invalid number", args[0]); |
| 7379 | return -1; |
| 7380 | } |
| 7381 | return 0; |
| 7382 | } |
| 7383 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7384 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7385 | struct proxy *defpx, const char *file, int line, |
| 7386 | char **err) |
| 7387 | { |
| 7388 | char *error; |
| 7389 | |
| 7390 | if (*(args[1]) == 0) { |
| 7391 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7392 | return -1; |
| 7393 | } |
| 7394 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7395 | if (*error != '\0') { |
| 7396 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7397 | return -1; |
| 7398 | } |
| 7399 | return 0; |
| 7400 | } |
| 7401 | |
| 7402 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7403 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7404 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7405 | * the main lua entry point. |
| 7406 | * |
| 7407 | * This funtion runs with the HAProxy keywords API. It returns -1 if an error is |
| 7408 | * occured, otherwise it returns 0. |
| 7409 | * |
| 7410 | * In some error case, LUA set an error message in top of the stack. This function |
| 7411 | * 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] | 7412 | * |
| 7413 | * This function can fail with an abort() due to an Lua critical error. |
| 7414 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7415 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7416 | */ |
| 7417 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7418 | struct proxy *defpx, const char *file, int line, |
| 7419 | char **err) |
| 7420 | { |
| 7421 | int error; |
| 7422 | |
| 7423 | /* Just load and compile the file. */ |
| 7424 | error = luaL_loadfile(gL.T, args[1]); |
| 7425 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7426 | 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] | 7427 | lua_pop(gL.T, 1); |
| 7428 | return -1; |
| 7429 | } |
| 7430 | |
| 7431 | /* If no syntax error where detected, execute the code. */ |
| 7432 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7433 | switch (error) { |
| 7434 | case LUA_OK: |
| 7435 | break; |
| 7436 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7437 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7438 | lua_pop(gL.T, 1); |
| 7439 | return -1; |
| 7440 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7441 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7442 | return -1; |
| 7443 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7444 | 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] | 7445 | lua_pop(gL.T, 1); |
| 7446 | return -1; |
| 7447 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7448 | 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] | 7449 | lua_pop(gL.T, 1); |
| 7450 | return -1; |
| 7451 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7452 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7453 | lua_pop(gL.T, 1); |
| 7454 | return -1; |
| 7455 | } |
| 7456 | |
| 7457 | return 0; |
| 7458 | } |
| 7459 | |
| 7460 | /* configuration keywords declaration */ |
| 7461 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7462 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7463 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7464 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7465 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7466 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7467 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7468 | { 0, NULL, NULL }, |
| 7469 | }}; |
| 7470 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7471 | /* This function can fail with an abort() due to an Lua critical error. |
| 7472 | * We are in the initialisation process of HAProxy, this abort() is |
| 7473 | * tolerated. |
| 7474 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7475 | int hlua_post_init() |
| 7476 | { |
| 7477 | struct hlua_init_function *init; |
| 7478 | const char *msg; |
| 7479 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7480 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7481 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7482 | /* Call post initialisation function in safe environement. */ |
| 7483 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7484 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7485 | error = lua_tostring(gL.T, -1); |
| 7486 | else |
| 7487 | error = "critical error"; |
| 7488 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7489 | exit(1); |
| 7490 | } |
| 7491 | hlua_fcn_post_init(gL.T); |
| 7492 | RESET_SAFE_LJMP(gL.T); |
| 7493 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7494 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7495 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7496 | ret = hlua_ctx_resume(&gL, 0); |
| 7497 | switch (ret) { |
| 7498 | case HLUA_E_OK: |
| 7499 | lua_pop(gL.T, -1); |
| 7500 | return 1; |
| 7501 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7502 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7503 | return 0; |
| 7504 | case HLUA_E_ERRMSG: |
| 7505 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7506 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7507 | return 0; |
| 7508 | case HLUA_E_ERR: |
| 7509 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7510 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7511 | return 0; |
| 7512 | } |
| 7513 | } |
| 7514 | return 1; |
| 7515 | } |
| 7516 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7517 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7518 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7519 | * is the previously allocated size or the kind of object in case of a new |
| 7520 | * allocation. <nsize> is the requested new size. |
| 7521 | */ |
| 7522 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7523 | { |
| 7524 | struct hlua_mem_allocator *zone = ud; |
| 7525 | |
| 7526 | if (nsize == 0) { |
| 7527 | /* it's a free */ |
| 7528 | if (ptr) |
| 7529 | zone->allocated -= osize; |
| 7530 | free(ptr); |
| 7531 | return NULL; |
| 7532 | } |
| 7533 | |
| 7534 | if (!ptr) { |
| 7535 | /* it's a new allocation */ |
| 7536 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7537 | return NULL; |
| 7538 | |
| 7539 | ptr = malloc(nsize); |
| 7540 | if (ptr) |
| 7541 | zone->allocated += nsize; |
| 7542 | return ptr; |
| 7543 | } |
| 7544 | |
| 7545 | /* it's a realloc */ |
| 7546 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7547 | return NULL; |
| 7548 | |
| 7549 | ptr = realloc(ptr, nsize); |
| 7550 | if (ptr) |
| 7551 | zone->allocated += nsize - osize; |
| 7552 | return ptr; |
| 7553 | } |
| 7554 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7555 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7556 | * We are in the initialisation process of HAProxy, this abort() is |
| 7557 | * tolerated. |
| 7558 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7559 | void hlua_init(void) |
| 7560 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7561 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7562 | int idx; |
| 7563 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7564 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7565 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7566 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7567 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7568 | struct srv_kw *kw; |
| 7569 | int tmp_error; |
| 7570 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7571 | char *args[] = { /* SSL client configuration. */ |
| 7572 | "ssl", |
| 7573 | "verify", |
| 7574 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7575 | NULL |
| 7576 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7577 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7578 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7579 | HA_SPIN_INIT(&hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 7580 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7581 | /* Initialise struct hlua and com signals pool */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7582 | pool_head_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7583 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7584 | /* Register configuration keywords. */ |
| 7585 | cfg_register_keywords(&cfg_kws); |
| 7586 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7587 | /* Init main lua stack. */ |
| 7588 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7589 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7590 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7591 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7592 | hlua_sethlua(&gL); |
| 7593 | gL.Tref = LUA_REFNIL; |
| 7594 | gL.task = NULL; |
| 7595 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7596 | /* From this point, until the end of the initialisation fucntion, |
| 7597 | * the Lua function can fail with an abort. We are in the initialisation |
| 7598 | * process of HAProxy, this abort() is tolerated. |
| 7599 | */ |
| 7600 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7601 | /* Initialise lua. */ |
| 7602 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7603 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7604 | /* Set safe environment for the initialisation. */ |
| 7605 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7606 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7607 | error_msg = lua_tostring(gL.T, -1); |
| 7608 | else |
| 7609 | error_msg = "critical error"; |
| 7610 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7611 | exit(1); |
| 7612 | } |
| 7613 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7614 | /* |
| 7615 | * |
| 7616 | * Create "core" object. |
| 7617 | * |
| 7618 | */ |
| 7619 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7620 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7621 | lua_newtable(gL.T); |
| 7622 | |
| 7623 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7624 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7625 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7626 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7627 | /* Register special functions. */ |
| 7628 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7629 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7630 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7631 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7632 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7633 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7634 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7635 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7636 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7637 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7638 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7639 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7640 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7641 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7642 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7643 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7644 | hlua_class_function(gL.T, "log", hlua_log); |
| 7645 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7646 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7647 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7648 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7649 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7650 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7651 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7652 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7653 | |
| 7654 | /* |
| 7655 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7656 | * Register class Map |
| 7657 | * |
| 7658 | */ |
| 7659 | |
| 7660 | /* This table entry is the object "Map" base. */ |
| 7661 | lua_newtable(gL.T); |
| 7662 | |
| 7663 | /* register pattern types. */ |
| 7664 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7665 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7666 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7667 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 7668 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7669 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7670 | |
| 7671 | /* register constructor. */ |
| 7672 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7673 | |
| 7674 | /* Create and fill the metatable. */ |
| 7675 | lua_newtable(gL.T); |
| 7676 | |
| 7677 | /* Create and fille the __index entry. */ |
| 7678 | lua_pushstring(gL.T, "__index"); |
| 7679 | lua_newtable(gL.T); |
| 7680 | |
| 7681 | /* Register . */ |
| 7682 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7683 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7684 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7685 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7686 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7687 | /* Register previous table in the registry with reference and named entry. |
| 7688 | * The function hlua_register_metatable() pops the stack, so we |
| 7689 | * previously create a copy of the table. |
| 7690 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7691 | 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] | 7692 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7693 | |
| 7694 | /* Assign the metatable to the mai Map object. */ |
| 7695 | lua_setmetatable(gL.T, -2); |
| 7696 | |
| 7697 | /* Set a name to the table. */ |
| 7698 | lua_setglobal(gL.T, "Map"); |
| 7699 | |
| 7700 | /* |
| 7701 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7702 | * Register class Channel |
| 7703 | * |
| 7704 | */ |
| 7705 | |
| 7706 | /* Create and fill the metatable. */ |
| 7707 | lua_newtable(gL.T); |
| 7708 | |
| 7709 | /* Create and fille the __index entry. */ |
| 7710 | lua_pushstring(gL.T, "__index"); |
| 7711 | lua_newtable(gL.T); |
| 7712 | |
| 7713 | /* Register . */ |
| 7714 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7715 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7716 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7717 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7718 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7719 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7720 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7721 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7722 | 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] | 7723 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7724 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7725 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7726 | |
| 7727 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7728 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7729 | |
| 7730 | /* |
| 7731 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7732 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7733 | * |
| 7734 | */ |
| 7735 | |
| 7736 | /* Create and fill the metatable. */ |
| 7737 | lua_newtable(gL.T); |
| 7738 | |
| 7739 | /* Create and fille the __index entry. */ |
| 7740 | lua_pushstring(gL.T, "__index"); |
| 7741 | lua_newtable(gL.T); |
| 7742 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7743 | /* Browse existing fetches and create the associated |
| 7744 | * object method. |
| 7745 | */ |
| 7746 | sf = NULL; |
| 7747 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7748 | |
| 7749 | /* Dont register the keywork if the arguments check function are |
| 7750 | * not safe during the runtime. |
| 7751 | */ |
| 7752 | if ((sf->val_args != NULL) && |
| 7753 | (sf->val_args != val_payload_lv) && |
| 7754 | (sf->val_args != val_hdr)) |
| 7755 | continue; |
| 7756 | |
| 7757 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7758 | * by an underscore. |
| 7759 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7760 | strncpy(trash.area, sf->kw, trash.size); |
| 7761 | trash.area[trash.size - 1] = '\0'; |
| 7762 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7763 | if (*p == '.' || *p == '-' || *p == '+') |
| 7764 | *p = '_'; |
| 7765 | |
| 7766 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7767 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7768 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7769 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7770 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7771 | } |
| 7772 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7773 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7774 | |
| 7775 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7776 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7777 | |
| 7778 | /* |
| 7779 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7780 | * Register class Converters |
| 7781 | * |
| 7782 | */ |
| 7783 | |
| 7784 | /* Create and fill the metatable. */ |
| 7785 | lua_newtable(gL.T); |
| 7786 | |
| 7787 | /* Create and fill the __index entry. */ |
| 7788 | lua_pushstring(gL.T, "__index"); |
| 7789 | lua_newtable(gL.T); |
| 7790 | |
| 7791 | /* Browse existing converters and create the associated |
| 7792 | * object method. |
| 7793 | */ |
| 7794 | sc = NULL; |
| 7795 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7796 | /* Dont register the keywork if the arguments check function are |
| 7797 | * not safe during the runtime. |
| 7798 | */ |
| 7799 | if (sc->val_args != NULL) |
| 7800 | continue; |
| 7801 | |
| 7802 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7803 | * by an underscore. |
| 7804 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7805 | strncpy(trash.area, sc->kw, trash.size); |
| 7806 | trash.area[trash.size - 1] = '\0'; |
| 7807 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7808 | if (*p == '.' || *p == '-' || *p == '+') |
| 7809 | *p = '_'; |
| 7810 | |
| 7811 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7812 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7813 | lua_pushlightuserdata(gL.T, sc); |
| 7814 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7815 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7816 | } |
| 7817 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7818 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7819 | |
| 7820 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7821 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7822 | |
| 7823 | /* |
| 7824 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7825 | * Register class HTTP |
| 7826 | * |
| 7827 | */ |
| 7828 | |
| 7829 | /* Create and fill the metatable. */ |
| 7830 | lua_newtable(gL.T); |
| 7831 | |
| 7832 | /* Create and fille the __index entry. */ |
| 7833 | lua_pushstring(gL.T, "__index"); |
| 7834 | lua_newtable(gL.T); |
| 7835 | |
| 7836 | /* Register Lua functions. */ |
| 7837 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7838 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7839 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7840 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7841 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7842 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7843 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7844 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7845 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7846 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7847 | |
| 7848 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7849 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7850 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7851 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7852 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7853 | 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] | 7854 | 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] | 7855 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7856 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7857 | |
| 7858 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7859 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7860 | |
| 7861 | /* |
| 7862 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7863 | * Register class AppletTCP |
| 7864 | * |
| 7865 | */ |
| 7866 | |
| 7867 | /* Create and fill the metatable. */ |
| 7868 | lua_newtable(gL.T); |
| 7869 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7870 | /* Create and fille the __index entry. */ |
| 7871 | lua_pushstring(gL.T, "__index"); |
| 7872 | lua_newtable(gL.T); |
| 7873 | |
| 7874 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7875 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7876 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7877 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7878 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7879 | 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] | 7880 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7881 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7882 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7883 | |
| 7884 | lua_settable(gL.T, -3); |
| 7885 | |
| 7886 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7887 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7888 | |
| 7889 | /* |
| 7890 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7891 | * Register class AppletHTTP |
| 7892 | * |
| 7893 | */ |
| 7894 | |
| 7895 | /* Create and fill the metatable. */ |
| 7896 | lua_newtable(gL.T); |
| 7897 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7898 | /* Create and fille the __index entry. */ |
| 7899 | lua_pushstring(gL.T, "__index"); |
| 7900 | lua_newtable(gL.T); |
| 7901 | |
| 7902 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7903 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7904 | 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] | 7905 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7906 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7907 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7908 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7909 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7910 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7911 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7912 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7913 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 7914 | |
| 7915 | lua_settable(gL.T, -3); |
| 7916 | |
| 7917 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7918 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7919 | |
| 7920 | /* |
| 7921 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7922 | * Register class TXN |
| 7923 | * |
| 7924 | */ |
| 7925 | |
| 7926 | /* Create and fill the metatable. */ |
| 7927 | lua_newtable(gL.T); |
| 7928 | |
| 7929 | /* Create and fille the __index entry. */ |
| 7930 | lua_pushstring(gL.T, "__index"); |
| 7931 | lua_newtable(gL.T); |
| 7932 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7933 | /* Register Lua functions. */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7934 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 7935 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7936 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7937 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7938 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7939 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7940 | hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel); |
| 7941 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 7942 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7943 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 7944 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 7945 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 7946 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 7947 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 7948 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7949 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7950 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7951 | |
| 7952 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7953 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7954 | |
| 7955 | /* |
| 7956 | * |
| 7957 | * Register class Socket |
| 7958 | * |
| 7959 | */ |
| 7960 | |
| 7961 | /* Create and fill the metatable. */ |
| 7962 | lua_newtable(gL.T); |
| 7963 | |
| 7964 | /* Create and fille the __index entry. */ |
| 7965 | lua_pushstring(gL.T, "__index"); |
| 7966 | lua_newtable(gL.T); |
| 7967 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7968 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7969 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7970 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7971 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 7972 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 7973 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 7974 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 7975 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 7976 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 7977 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 7978 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 7979 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7980 | 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] | 7981 | |
| 7982 | /* Register the garbage collector entry. */ |
| 7983 | lua_pushstring(gL.T, "__gc"); |
| 7984 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7985 | 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] | 7986 | |
| 7987 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7988 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7989 | |
| 7990 | /* Proxy and server configuration initialisation. */ |
| 7991 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 7992 | init_new_proxy(&socket_proxy); |
| 7993 | socket_proxy.parent = NULL; |
| 7994 | socket_proxy.last_change = now.tv_sec; |
| 7995 | socket_proxy.id = "LUA-SOCKET"; |
| 7996 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 7997 | socket_proxy.maxconn = 0; |
| 7998 | socket_proxy.accept = NULL; |
| 7999 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8000 | socket_proxy.srv = NULL; |
| 8001 | socket_proxy.conn_retries = 0; |
| 8002 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8003 | |
| 8004 | /* Init TCP server: unchanged parameters */ |
| 8005 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8006 | socket_tcp.next = NULL; |
| 8007 | socket_tcp.proxy = &socket_proxy; |
| 8008 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8009 | LIST_INIT(&socket_tcp.actconns); |
| 8010 | LIST_INIT(&socket_tcp.pendconns); |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8011 | socket_tcp.priv_conns = NULL; |
| 8012 | socket_tcp.idle_conns = NULL; |
| 8013 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8014 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8015 | socket_tcp.last_change = 0; |
| 8016 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8017 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8018 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8019 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8020 | |
| 8021 | /* XXX: Copy default parameter from default server, |
| 8022 | * but the default server is not initialized. |
| 8023 | */ |
| 8024 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8025 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8026 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8027 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8028 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8029 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8030 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8031 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8032 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8033 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8034 | |
| 8035 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8036 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8037 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8038 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8039 | socket_tcp.check.server = &socket_tcp; |
| 8040 | |
| 8041 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8042 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8043 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8044 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8045 | socket_tcp.agent.server = &socket_tcp; |
| 8046 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8047 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8048 | |
| 8049 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8050 | /* Init TCP server: unchanged parameters */ |
| 8051 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8052 | socket_ssl.next = NULL; |
| 8053 | socket_ssl.proxy = &socket_proxy; |
| 8054 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8055 | LIST_INIT(&socket_ssl.actconns); |
| 8056 | LIST_INIT(&socket_ssl.pendconns); |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8057 | socket_tcp.priv_conns = NULL; |
| 8058 | socket_tcp.idle_conns = NULL; |
| 8059 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8060 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8061 | socket_ssl.last_change = 0; |
| 8062 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8063 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8064 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8065 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8066 | |
| 8067 | /* XXX: Copy default parameter from default server, |
| 8068 | * but the default server is not initialized. |
| 8069 | */ |
| 8070 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8071 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8072 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8073 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8074 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8075 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8076 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8077 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8078 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8079 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8080 | |
| 8081 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8082 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8083 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8084 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8085 | socket_ssl.check.server = &socket_ssl; |
| 8086 | |
| 8087 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8088 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8089 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8090 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8091 | socket_ssl.agent.server = &socket_ssl; |
| 8092 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8093 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8094 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8095 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8096 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8097 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8098 | /* |
| 8099 | * |
| 8100 | * If the keyword is not known, we can search in the registered |
| 8101 | * server keywords. This is usefull to configure special SSL |
| 8102 | * features like client certificates and ssl_verify. |
| 8103 | * |
| 8104 | */ |
| 8105 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8106 | if (tmp_error != 0) { |
| 8107 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8108 | abort(); /* This must be never arrives because the command line |
| 8109 | not editable by the user. */ |
| 8110 | } |
| 8111 | idx += kw->skip; |
| 8112 | } |
| 8113 | } |
| 8114 | |
| 8115 | /* Initialize SSL server. */ |
Willy Tarreau | cbe6da5 | 2018-05-18 17:08:28 +0200 | [diff] [blame] | 8116 | if (socket_ssl.xprt->prepare_srv) { |
| 8117 | int saved_used_backed = global.ssl_used_backend; |
| 8118 | // don't affect maxconn automatic computation |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 8119 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
Willy Tarreau | cbe6da5 | 2018-05-18 17:08:28 +0200 | [diff] [blame] | 8120 | global.ssl_used_backend = saved_used_backed; |
| 8121 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8122 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8123 | |
| 8124 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8125 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8126 | |
| 8127 | __attribute__((constructor)) |
| 8128 | static void __hlua_init(void) |
| 8129 | { |
| 8130 | char *ptr = NULL; |
| 8131 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8132 | hap_register_build_opts(ptr, 1); |
| 8133 | } |