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 | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 290 | /* This function check the number of arguments available in the |
| 291 | * stack. If the number of arguments available is not the same |
| 292 | * then <nb> an error is throwed. |
| 293 | */ |
| 294 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 295 | { |
| 296 | if (lua_gettop(L) == nb) |
| 297 | return; |
| 298 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 299 | } |
| 300 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 301 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 302 | * and the line number where the error is encountered. |
| 303 | */ |
| 304 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 305 | { |
| 306 | va_list argp; |
| 307 | va_start(argp, fmt); |
| 308 | luaL_where(L, 1); |
| 309 | lua_pushvfstring(L, fmt, argp); |
| 310 | va_end(argp); |
| 311 | lua_concat(L, 2); |
| 312 | return 1; |
| 313 | } |
| 314 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 315 | /* This functions is used with sample fetch and converters. It |
| 316 | * converts the HAProxy configuration argument in a lua stack |
| 317 | * values. |
| 318 | * |
| 319 | * It takes an array of "arg", and each entry of the array is |
| 320 | * converted and pushed in the LUA stack. |
| 321 | */ |
| 322 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 323 | { |
| 324 | switch (arg->type) { |
| 325 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 326 | case ARGT_TIME: |
| 327 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 328 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 329 | break; |
| 330 | |
| 331 | case ARGT_STR: |
| 332 | lua_pushlstring(L, arg->data.str.str, arg->data.str.len); |
| 333 | break; |
| 334 | |
| 335 | case ARGT_IPV4: |
| 336 | case ARGT_IPV6: |
| 337 | case ARGT_MSK4: |
| 338 | case ARGT_MSK6: |
| 339 | case ARGT_FE: |
| 340 | case ARGT_BE: |
| 341 | case ARGT_TAB: |
| 342 | case ARGT_SRV: |
| 343 | case ARGT_USR: |
| 344 | case ARGT_MAP: |
| 345 | default: |
| 346 | lua_pushnil(L); |
| 347 | break; |
| 348 | } |
| 349 | return 1; |
| 350 | } |
| 351 | |
| 352 | /* This function take one entrie in an LUA stack at the index "ud", |
| 353 | * and try to convert it in an HAProxy argument entry. This is useful |
| 354 | * with sample fetch wrappers. The input arguments are gived to the |
| 355 | * lua wrapper and converted as arg list by thi function. |
| 356 | */ |
| 357 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 358 | { |
| 359 | switch (lua_type(L, ud)) { |
| 360 | |
| 361 | case LUA_TNUMBER: |
| 362 | case LUA_TBOOLEAN: |
| 363 | arg->type = ARGT_SINT; |
| 364 | arg->data.sint = lua_tointeger(L, ud); |
| 365 | break; |
| 366 | |
| 367 | case LUA_TSTRING: |
| 368 | arg->type = ARGT_STR; |
| 369 | arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len); |
| 370 | break; |
| 371 | |
| 372 | case LUA_TUSERDATA: |
| 373 | case LUA_TNIL: |
| 374 | case LUA_TTABLE: |
| 375 | case LUA_TFUNCTION: |
| 376 | case LUA_TTHREAD: |
| 377 | case LUA_TLIGHTUSERDATA: |
| 378 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 379 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 380 | break; |
| 381 | } |
| 382 | return 1; |
| 383 | } |
| 384 | |
| 385 | /* the following functions are used to convert a struct sample |
| 386 | * in Lua type. This useful to convert the return of the |
| 387 | * fetchs or converters. |
| 388 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 389 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 390 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 391 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 392 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 393 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 394 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 395 | break; |
| 396 | |
| 397 | case SMP_T_BIN: |
| 398 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 399 | lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 400 | break; |
| 401 | |
| 402 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 403 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 404 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 405 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 406 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 407 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 408 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 409 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 410 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 411 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 412 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 413 | lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 414 | break; |
| 415 | default: |
| 416 | lua_pushnil(L); |
| 417 | break; |
| 418 | } |
| 419 | break; |
| 420 | |
| 421 | case SMP_T_IPV4: |
| 422 | case SMP_T_IPV6: |
| 423 | 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] | 424 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 425 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 426 | lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 427 | else |
| 428 | lua_pushnil(L); |
| 429 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 430 | default: |
| 431 | lua_pushnil(L); |
| 432 | break; |
| 433 | } |
| 434 | return 1; |
| 435 | } |
| 436 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 437 | /* the following functions are used to convert a struct sample |
| 438 | * in Lua strings. This is useful to convert the return of the |
| 439 | * fetchs or converters. |
| 440 | */ |
| 441 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 442 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 443 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 444 | |
| 445 | case SMP_T_BIN: |
| 446 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 447 | lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 448 | break; |
| 449 | |
| 450 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 451 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 452 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 453 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 454 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 455 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 456 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 457 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 458 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 459 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 460 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 461 | lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 462 | break; |
| 463 | default: |
| 464 | lua_pushstring(L, ""); |
| 465 | break; |
| 466 | } |
| 467 | break; |
| 468 | |
| 469 | case SMP_T_SINT: |
| 470 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 471 | case SMP_T_IPV4: |
| 472 | case SMP_T_IPV6: |
| 473 | 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] | 474 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 475 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 476 | lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 477 | else |
| 478 | lua_pushstring(L, ""); |
| 479 | break; |
| 480 | default: |
| 481 | lua_pushstring(L, ""); |
| 482 | break; |
| 483 | } |
| 484 | return 1; |
| 485 | } |
| 486 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 487 | /* the following functions are used to convert an Lua type in a |
| 488 | * struct sample. This is useful to provide data from a converter |
| 489 | * to the LUA code. |
| 490 | */ |
| 491 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 492 | { |
| 493 | switch (lua_type(L, ud)) { |
| 494 | |
| 495 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 496 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 497 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 498 | break; |
| 499 | |
| 500 | |
| 501 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 502 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 503 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 504 | break; |
| 505 | |
| 506 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 507 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 508 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 509 | smp->data.u.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.len); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 510 | break; |
| 511 | |
| 512 | case LUA_TUSERDATA: |
| 513 | case LUA_TNIL: |
| 514 | case LUA_TTABLE: |
| 515 | case LUA_TFUNCTION: |
| 516 | case LUA_TTHREAD: |
| 517 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 518 | case LUA_TNONE: |
| 519 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 520 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 521 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 522 | break; |
| 523 | } |
| 524 | return 1; |
| 525 | } |
| 526 | |
| 527 | /* This function check the "argp" builded by another conversion function |
| 528 | * is in accord with the expected argp defined by the "mask". The fucntion |
| 529 | * 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] | 530 | * |
| 531 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 532 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 533 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 534 | __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] | 535 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 536 | { |
| 537 | int min_arg; |
| 538 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 539 | struct proxy *px; |
| 540 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 541 | |
| 542 | idx = 0; |
| 543 | min_arg = ARGM(mask); |
| 544 | mask >>= ARGM_BITS; |
| 545 | |
| 546 | while (1) { |
| 547 | |
| 548 | /* Check oversize. */ |
| 549 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 550 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | /* Check for mandatory arguments. */ |
| 554 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 555 | if (idx < min_arg) { |
| 556 | |
| 557 | /* If miss other argument than the first one, we return an error. */ |
| 558 | if (idx > 0) |
| 559 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 560 | |
| 561 | /* If first argument have a certain type, some default values |
| 562 | * may be used. See the function smp_resolve_args(). |
| 563 | */ |
| 564 | switch (mask & ARGT_MASK) { |
| 565 | |
| 566 | case ARGT_FE: |
| 567 | if (!(p->cap & PR_CAP_FE)) |
| 568 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 569 | argp[idx].data.prx = p; |
| 570 | argp[idx].type = ARGT_FE; |
| 571 | argp[idx+1].type = ARGT_STOP; |
| 572 | break; |
| 573 | |
| 574 | case ARGT_BE: |
| 575 | if (!(p->cap & PR_CAP_BE)) |
| 576 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 577 | argp[idx].data.prx = p; |
| 578 | argp[idx].type = ARGT_BE; |
| 579 | argp[idx+1].type = ARGT_STOP; |
| 580 | break; |
| 581 | |
| 582 | case ARGT_TAB: |
| 583 | argp[idx].data.prx = p; |
| 584 | argp[idx].type = ARGT_TAB; |
| 585 | argp[idx+1].type = ARGT_STOP; |
| 586 | break; |
| 587 | |
| 588 | default: |
| 589 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 590 | break; |
| 591 | } |
| 592 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | /* Check for exceed the number of requiered argument. */ |
| 597 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 598 | argp[idx].type != ARGT_STOP) { |
| 599 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 600 | } |
| 601 | |
| 602 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 603 | argp[idx].type == ARGT_STOP) { |
| 604 | return 0; |
| 605 | } |
| 606 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 607 | /* Convert some argument types. */ |
| 608 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 609 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 610 | if (argp[idx].type != ARGT_SINT) |
| 611 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 612 | argp[idx].type = ARGT_SINT; |
| 613 | break; |
| 614 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 615 | case ARGT_TIME: |
| 616 | if (argp[idx].type != ARGT_SINT) |
| 617 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 618 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 619 | break; |
| 620 | |
| 621 | case ARGT_SIZE: |
| 622 | if (argp[idx].type != ARGT_SINT) |
| 623 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 624 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 625 | break; |
| 626 | |
| 627 | case ARGT_FE: |
| 628 | if (argp[idx].type != ARGT_STR) |
| 629 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 630 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 631 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 632 | argp[idx].data.prx = proxy_fe_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 633 | if (!argp[idx].data.prx) |
| 634 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 635 | argp[idx].type = ARGT_FE; |
| 636 | break; |
| 637 | |
| 638 | case ARGT_BE: |
| 639 | if (argp[idx].type != ARGT_STR) |
| 640 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 641 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 642 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 643 | argp[idx].data.prx = proxy_be_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 644 | if (!argp[idx].data.prx) |
| 645 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 646 | argp[idx].type = ARGT_BE; |
| 647 | break; |
| 648 | |
| 649 | case ARGT_TAB: |
| 650 | if (argp[idx].type != ARGT_STR) |
| 651 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 652 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 653 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | e2dc1fa | 2015-05-26 12:08:07 +0200 | [diff] [blame] | 654 | argp[idx].data.prx = proxy_tbl_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 655 | if (!argp[idx].data.prx) |
| 656 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 657 | argp[idx].type = ARGT_TAB; |
| 658 | break; |
| 659 | |
| 660 | case ARGT_SRV: |
| 661 | if (argp[idx].type != ARGT_STR) |
| 662 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 663 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 664 | trash.str[argp[idx].data.str.len] = 0; |
| 665 | sname = strrchr(trash.str, '/'); |
| 666 | if (sname) { |
| 667 | *sname++ = '\0'; |
| 668 | pname = trash.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 669 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 670 | if (!px) |
| 671 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 672 | } |
| 673 | else { |
| 674 | sname = trash.str; |
| 675 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 676 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 677 | argp[idx].data.srv = findserver(px, sname); |
| 678 | if (!argp[idx].data.srv) |
| 679 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 680 | argp[idx].type = ARGT_SRV; |
| 681 | break; |
| 682 | |
| 683 | case ARGT_IPV4: |
| 684 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 685 | trash.str[argp[idx].data.str.len] = 0; |
| 686 | if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4)) |
| 687 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 688 | argp[idx].type = ARGT_IPV4; |
| 689 | break; |
| 690 | |
| 691 | case ARGT_MSK4: |
| 692 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 693 | trash.str[argp[idx].data.str.len] = 0; |
| 694 | if (!str2mask(trash.str, &argp[idx].data.ipv4)) |
| 695 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 696 | argp[idx].type = ARGT_MSK4; |
| 697 | break; |
| 698 | |
| 699 | case ARGT_IPV6: |
| 700 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 701 | trash.str[argp[idx].data.str.len] = 0; |
| 702 | if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6)) |
| 703 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 704 | argp[idx].type = ARGT_IPV6; |
| 705 | break; |
| 706 | |
| 707 | case ARGT_MSK6: |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 708 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 709 | trash.str[argp[idx].data.str.len] = 0; |
| 710 | if (!str2mask6(trash.str, &argp[idx].data.ipv6)) |
| 711 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 712 | argp[idx].type = ARGT_MSK6; |
| 713 | break; |
| 714 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 715 | case ARGT_MAP: |
| 716 | case ARGT_REG: |
| 717 | case ARGT_USR: |
| 718 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 719 | break; |
| 720 | } |
| 721 | |
| 722 | /* Check for type of argument. */ |
| 723 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 724 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 725 | arg_type_names[(mask & ARGT_MASK)], |
| 726 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 727 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 728 | } |
| 729 | |
| 730 | /* Next argument. */ |
| 731 | mask >>= ARGT_BITS; |
| 732 | idx++; |
| 733 | } |
| 734 | } |
| 735 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 736 | /* |
| 737 | * The following functions are used to make correspondance between the the |
| 738 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 739 | * |
| 740 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 741 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 742 | */ |
| 743 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 744 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 745 | struct hlua **hlua = lua_getextraspace(L); |
| 746 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 747 | } |
| 748 | static inline void hlua_sethlua(struct hlua *hlua) |
| 749 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 750 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 751 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 752 | } |
| 753 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 754 | /* This function is used to send logs. It try to send on screen (stderr) |
| 755 | * and on the default syslog server. |
| 756 | */ |
| 757 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 758 | { |
| 759 | struct tm tm; |
| 760 | char *p; |
| 761 | |
| 762 | /* Cleanup the log message. */ |
| 763 | p = trash.str; |
| 764 | for (; *msg != '\0'; msg++, p++) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 765 | if (p >= trash.str + trash.size - 1) { |
| 766 | /* Break the message if exceed the buffer size. */ |
| 767 | *(p-4) = ' '; |
| 768 | *(p-3) = '.'; |
| 769 | *(p-2) = '.'; |
| 770 | *(p-1) = '.'; |
| 771 | break; |
| 772 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 773 | if (isprint(*msg)) |
| 774 | *p = *msg; |
| 775 | else |
| 776 | *p = '.'; |
| 777 | } |
| 778 | *p = '\0'; |
| 779 | |
Thierry FOURNIER | 5554e29 | 2015-09-09 11:21:37 +0200 | [diff] [blame] | 780 | send_log(px, level, "%s\n", trash.str); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 781 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 782 | get_localtime(date.tv_sec, &tm); |
| 783 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 784 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 785 | (int)getpid(), trash.str); |
| 786 | fflush(stderr); |
| 787 | } |
| 788 | } |
| 789 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 790 | /* This function just ensure that the yield will be always |
| 791 | * returned with a timeout and permit to set some flags |
| 792 | */ |
| 793 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 794 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 795 | { |
| 796 | struct hlua *hlua = hlua_gethlua(L); |
| 797 | |
| 798 | /* Set the wake timeout. If timeout is required, we set |
| 799 | * the expiration time. |
| 800 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 801 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 802 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 803 | hlua->flags |= flags; |
| 804 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 805 | /* Process the yield. */ |
| 806 | WILL_LJMP(lua_yieldk(L, nresults, ctx, k)); |
| 807 | } |
| 808 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 809 | /* This function initialises the Lua environment stored in the stream. |
| 810 | * 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] | 811 | * 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] | 812 | * |
| 813 | * This function is particular. it initialises a new Lua thread. If the |
| 814 | * initialisation fails (example: out of memory error), the lua function |
| 815 | * throws an error (longjmp). |
| 816 | * |
| 817 | * This function manipulates two Lua stack: the main and the thread. Only |
| 818 | * the main stack can fail. The thread is not manipulated. This function |
| 819 | * MUST NOT manipulate the created thread stack state, because is not |
| 820 | * proctected agains error throwed by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 821 | */ |
| 822 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 823 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 824 | if (!SET_SAFE_LJMP(gL.T)) { |
| 825 | lua->Tref = LUA_REFNIL; |
| 826 | return 0; |
| 827 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 828 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 829 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 830 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 831 | lua->T = lua_newthread(gL.T); |
| 832 | if (!lua->T) { |
| 833 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | 0a97620 | 2017-07-12 11:18:00 +0200 | [diff] [blame] | 834 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 835 | return 0; |
| 836 | } |
| 837 | hlua_sethlua(lua); |
| 838 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 839 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 840 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 841 | return 1; |
| 842 | } |
| 843 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 844 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 845 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 846 | * is not freed. |
| 847 | */ |
| 848 | void hlua_ctx_destroy(struct hlua *lua) |
| 849 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 850 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 851 | return; |
| 852 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 853 | if (!lua->T) |
| 854 | goto end; |
| 855 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 856 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 857 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 858 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 859 | if (!SET_SAFE_LJMP(lua->T)) |
| 860 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 861 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 862 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 863 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 864 | if (!SET_SAFE_LJMP(gL.T)) |
| 865 | return; |
| 866 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 867 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 868 | /* Forces a garbage collecting process. If the Lua program is finished |
| 869 | * without error, we run the GC on the thread pointer. Its freed all |
| 870 | * the unused memory. |
| 871 | * If the thread is finnish with an error or is currently yielded, |
| 872 | * it seems that the GC applied on the thread doesn't clean anything, |
| 873 | * so e run the GC on the main thread. |
| 874 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 875 | * the garbage collection. |
| 876 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 877 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 878 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 879 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 880 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 881 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 882 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 883 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 884 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 885 | |
| 886 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 887 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 888 | } |
| 889 | |
| 890 | /* This function is used to restore the Lua context when a coroutine |
| 891 | * fails. This function copy the common memory between old coroutine |
| 892 | * and the new coroutine. The old coroutine is destroyed, and its |
| 893 | * replaced by the new coroutine. |
| 894 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 895 | * as string error message and it is copied in the new stack. |
| 896 | */ |
| 897 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 898 | { |
| 899 | lua_State *T; |
| 900 | int new_ref; |
| 901 | |
| 902 | /* Renew the main LUA stack doesn't have sense. */ |
| 903 | if (lua == &gL) |
| 904 | return 0; |
| 905 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 906 | /* New Lua coroutine. */ |
| 907 | T = lua_newthread(gL.T); |
| 908 | if (!T) |
| 909 | return 0; |
| 910 | |
| 911 | /* Copy last error message. */ |
| 912 | if (keep_msg) |
| 913 | lua_xmove(lua->T, T, 1); |
| 914 | |
| 915 | /* Copy data between the coroutines. */ |
| 916 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 917 | lua_xmove(lua->T, T, 1); |
| 918 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 919 | |
| 920 | /* Destroy old data. */ |
| 921 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 922 | |
| 923 | /* The thread is garbage collected by Lua. */ |
| 924 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 925 | |
| 926 | /* Fill the struct with the new coroutine values. */ |
| 927 | lua->Mref = new_ref; |
| 928 | lua->T = T; |
| 929 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 930 | |
| 931 | /* Set context. */ |
| 932 | hlua_sethlua(lua); |
| 933 | |
| 934 | return 1; |
| 935 | } |
| 936 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 937 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 938 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 939 | struct hlua *hlua = hlua_gethlua(L); |
| 940 | |
| 941 | /* Lua cannot yield when its returning from a function, |
| 942 | * so, we can fix the interrupt hook to 1 instruction, |
| 943 | * expecting that the function is finnished. |
| 944 | */ |
| 945 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 946 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 947 | return; |
| 948 | } |
| 949 | |
| 950 | /* restore the interrupt condition. */ |
| 951 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 952 | |
| 953 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 954 | * If the state is not yieldable, trying yield causes an error. |
| 955 | */ |
| 956 | if (lua_isyieldable(L)) |
| 957 | WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 958 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 959 | /* If we cannot yield, update the clock and check the timeout. */ |
| 960 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 961 | hlua->run_time += now_ms - hlua->start_time; |
| 962 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 963 | lua_pushfstring(L, "execution timeout"); |
| 964 | WILL_LJMP(lua_error(L)); |
| 965 | } |
| 966 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 967 | /* Update the start time. */ |
| 968 | hlua->start_time = now_ms; |
| 969 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 970 | /* Try to interrupt the process at the end of the current |
| 971 | * unyieldable function. |
| 972 | */ |
| 973 | 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] | 974 | } |
| 975 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 976 | /* This function start or resumes the Lua stack execution. If the flag |
| 977 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 978 | * The function return an error. |
| 979 | * |
| 980 | * The function can returns 4 values: |
| 981 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 982 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 983 | * task wakeup. |
| 984 | * - HLUA_E_ERRMSG : An error has occured, an error message is set in |
| 985 | * the top of the stack. |
| 986 | * - HLUA_E_ERR : An error has occured without error message. |
| 987 | * |
| 988 | * If an error occured, the stack is renewed and it is ready to run new |
| 989 | * LUA code. |
| 990 | */ |
| 991 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 992 | { |
| 993 | int ret; |
| 994 | const char *msg; |
| 995 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 996 | /* Initialise run time counter. */ |
| 997 | if (!HLUA_IS_RUNNING(lua)) |
| 998 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 999 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1000 | /* Lock the whole Lua execution. This lock must be before the |
| 1001 | * label "resume_execution". |
| 1002 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1003 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1004 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1005 | resume_execution: |
| 1006 | |
| 1007 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1008 | * instructions. it is used for preventing infinite loops. |
| 1009 | */ |
| 1010 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1011 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1012 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1013 | HLUA_SET_RUN(lua); |
| 1014 | HLUA_CLR_CTRLYIELD(lua); |
| 1015 | HLUA_CLR_WAKERESWR(lua); |
| 1016 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1017 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1018 | /* Update the start time. */ |
| 1019 | lua->start_time = now_ms; |
| 1020 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1021 | /* Call the function. */ |
| 1022 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1023 | switch (ret) { |
| 1024 | |
| 1025 | case LUA_OK: |
| 1026 | ret = HLUA_E_OK; |
| 1027 | break; |
| 1028 | |
| 1029 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1030 | /* Check if the execution timeout is expired. It it is the case, we |
| 1031 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1032 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1033 | tv_update_date(0, 1); |
| 1034 | lua->run_time += now_ms - lua->start_time; |
| 1035 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1036 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1037 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1038 | break; |
| 1039 | } |
| 1040 | /* Process the forced yield. if the general yield is not allowed or |
| 1041 | * if no task were associated this the current Lua execution |
| 1042 | * coroutine, we resume the execution. Else we want to return in the |
| 1043 | * scheduler and we want to be waked up again, to continue the |
| 1044 | * current Lua execution. So we schedule our own task. |
| 1045 | */ |
| 1046 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1047 | if (!yield_allowed || !lua->task) |
| 1048 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1049 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1050 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1051 | if (!yield_allowed) { |
| 1052 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1053 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1054 | break; |
| 1055 | } |
| 1056 | ret = HLUA_E_AGAIN; |
| 1057 | break; |
| 1058 | |
| 1059 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1060 | |
| 1061 | /* Special exit case. The traditionnal exit is returned as an error |
| 1062 | * because the errors ares the only one mean to return immediately |
| 1063 | * from and lua execution. |
| 1064 | */ |
| 1065 | if (lua->flags & HLUA_EXIT) { |
| 1066 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1067 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1068 | break; |
| 1069 | } |
| 1070 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1071 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1072 | if (!lua_checkstack(lua->T, 1)) { |
| 1073 | ret = HLUA_E_ERR; |
| 1074 | break; |
| 1075 | } |
| 1076 | msg = lua_tostring(lua->T, -1); |
| 1077 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1078 | lua_pop(lua->T, 1); |
| 1079 | if (msg) |
| 1080 | lua_pushfstring(lua->T, "runtime error: %s", msg); |
| 1081 | else |
| 1082 | lua_pushfstring(lua->T, "unknown runtime error"); |
| 1083 | ret = HLUA_E_ERRMSG; |
| 1084 | break; |
| 1085 | |
| 1086 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1087 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1088 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1089 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1090 | break; |
| 1091 | |
| 1092 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1093 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1094 | if (!lua_checkstack(lua->T, 1)) { |
| 1095 | ret = HLUA_E_ERR; |
| 1096 | break; |
| 1097 | } |
| 1098 | msg = lua_tostring(lua->T, -1); |
| 1099 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1100 | lua_pop(lua->T, 1); |
| 1101 | if (msg) |
| 1102 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1103 | else |
| 1104 | lua_pushfstring(lua->T, "message handler error"); |
| 1105 | ret = HLUA_E_ERRMSG; |
| 1106 | break; |
| 1107 | |
| 1108 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1109 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1110 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1111 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1112 | break; |
| 1113 | } |
| 1114 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1115 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1116 | if (lua->flags & HLUA_MUST_GC && |
| 1117 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1118 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1119 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1120 | switch (ret) { |
| 1121 | case HLUA_E_AGAIN: |
| 1122 | break; |
| 1123 | |
| 1124 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1125 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1126 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1127 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1128 | break; |
| 1129 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1130 | case HLUA_E_ETMOUT: |
| 1131 | case HLUA_E_NOMEM: |
| 1132 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1133 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1134 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1135 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1136 | hlua_ctx_renew(lua, 0); |
| 1137 | break; |
| 1138 | |
| 1139 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1140 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1141 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1142 | break; |
| 1143 | } |
| 1144 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1145 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1146 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1147 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1148 | return ret; |
| 1149 | } |
| 1150 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1151 | /* This function exit the current code. */ |
| 1152 | __LJMP static int hlua_done(lua_State *L) |
| 1153 | { |
| 1154 | struct hlua *hlua = hlua_gethlua(L); |
| 1155 | |
| 1156 | hlua->flags |= HLUA_EXIT; |
| 1157 | WILL_LJMP(lua_error(L)); |
| 1158 | |
| 1159 | return 0; |
| 1160 | } |
| 1161 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1162 | /* This function is an LUA binding. It provides a function |
| 1163 | * for deleting ACL from a referenced ACL file. |
| 1164 | */ |
| 1165 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1166 | { |
| 1167 | const char *name; |
| 1168 | const char *key; |
| 1169 | struct pat_ref *ref; |
| 1170 | |
| 1171 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1172 | |
| 1173 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1174 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1175 | |
| 1176 | ref = pat_ref_lookup(name); |
| 1177 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1178 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1179 | |
| 1180 | pat_ref_delete(ref, key); |
| 1181 | return 0; |
| 1182 | } |
| 1183 | |
| 1184 | /* This function is an LUA binding. It provides a function |
| 1185 | * for deleting map entry from a referenced map file. |
| 1186 | */ |
| 1187 | static int hlua_del_map(lua_State *L) |
| 1188 | { |
| 1189 | const char *name; |
| 1190 | const char *key; |
| 1191 | struct pat_ref *ref; |
| 1192 | |
| 1193 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1194 | |
| 1195 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1196 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1197 | |
| 1198 | ref = pat_ref_lookup(name); |
| 1199 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1200 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1201 | |
| 1202 | pat_ref_delete(ref, key); |
| 1203 | return 0; |
| 1204 | } |
| 1205 | |
| 1206 | /* This function is an LUA binding. It provides a function |
| 1207 | * for adding ACL pattern from a referenced ACL file. |
| 1208 | */ |
| 1209 | static int hlua_add_acl(lua_State *L) |
| 1210 | { |
| 1211 | const char *name; |
| 1212 | const char *key; |
| 1213 | struct pat_ref *ref; |
| 1214 | |
| 1215 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1216 | |
| 1217 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1218 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1219 | |
| 1220 | ref = pat_ref_lookup(name); |
| 1221 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1222 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1223 | |
| 1224 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1225 | pat_ref_add(ref, key, NULL, NULL); |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
| 1229 | /* This function is an LUA binding. It provides a function |
| 1230 | * for setting map pattern and sample from a referenced map |
| 1231 | * file. |
| 1232 | */ |
| 1233 | static int hlua_set_map(lua_State *L) |
| 1234 | { |
| 1235 | const char *name; |
| 1236 | const char *key; |
| 1237 | const char *value; |
| 1238 | struct pat_ref *ref; |
| 1239 | |
| 1240 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1241 | |
| 1242 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1243 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1244 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1245 | |
| 1246 | ref = pat_ref_lookup(name); |
| 1247 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1248 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1249 | |
| 1250 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1251 | pat_ref_set(ref, key, value, NULL); |
| 1252 | else |
| 1253 | pat_ref_add(ref, key, value, NULL); |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1257 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1258 | * an integer or user data. This data is associated with a metatable. This |
| 1259 | * metatable have an original version registred in the global context with |
| 1260 | * the name of the object (_G[<name>] = <metable> ). |
| 1261 | * |
| 1262 | * A metable is a table that modify the standard behavior of a standard |
| 1263 | * access to the associated data. The entries of this new metatable are |
| 1264 | * defined as is: |
| 1265 | * |
| 1266 | * http://lua-users.org/wiki/MetatableEvents |
| 1267 | * |
| 1268 | * __index |
| 1269 | * |
| 1270 | * we access an absent field in a table, the result is nil. This is |
| 1271 | * true, but it is not the whole truth. Actually, such access triggers |
| 1272 | * the interpreter to look for an __index metamethod: If there is no |
| 1273 | * such method, as usually happens, then the access results in nil; |
| 1274 | * otherwise, the metamethod will provide the result. |
| 1275 | * |
| 1276 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1277 | * the key does not appear in the table, but the metatable has an __index |
| 1278 | * property: |
| 1279 | * |
| 1280 | * - if the value is a function, the function is called, passing in the |
| 1281 | * table and the key; the return value of that function is returned as |
| 1282 | * the result. |
| 1283 | * |
| 1284 | * - if the value is another table, the value of the key in that table is |
| 1285 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1286 | * table's metatable has an __index property, then it continues on up) |
| 1287 | * |
| 1288 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1289 | * |
| 1290 | * http://www.lua.org/pil/13.4.1.html |
| 1291 | * |
| 1292 | * __newindex |
| 1293 | * |
| 1294 | * Like __index, but control property assignment. |
| 1295 | * |
| 1296 | * __mode - Control weak references. A string value with one or both |
| 1297 | * of the characters 'k' and 'v' which specifies that the the |
| 1298 | * keys and/or values in the table are weak references. |
| 1299 | * |
| 1300 | * __call - Treat a table like a function. When a table is followed by |
| 1301 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1302 | * a __call key pointing to a function, that function is invoked |
| 1303 | * (passing any specified arguments) and the return value is |
| 1304 | * returned. |
| 1305 | * |
| 1306 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1307 | * called, if the metatable for myTable has a __metatable |
| 1308 | * key, the value of that key is returned instead of the |
| 1309 | * actual metatable. |
| 1310 | * |
| 1311 | * __tostring - Control string representation. When the builtin |
| 1312 | * "tostring( myTable )" function is called, if the metatable |
| 1313 | * for myTable has a __tostring property set to a function, |
| 1314 | * that function is invoked (passing myTable to it) and the |
| 1315 | * return value is used as the string representation. |
| 1316 | * |
| 1317 | * __len - Control table length. When the table length is requested using |
| 1318 | * the length operator ( '#' ), if the metatable for myTable has |
| 1319 | * a __len key pointing to a function, that function is invoked |
| 1320 | * (passing myTable to it) and the return value used as the value |
| 1321 | * of "#myTable". |
| 1322 | * |
| 1323 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1324 | * collected, if the metatable has a __gc field pointing to a |
| 1325 | * function, that function is first invoked, passing the userdata |
| 1326 | * to it. The __gc metamethod is not called for tables. |
| 1327 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1328 | * |
| 1329 | * Special metamethods for redefining standard operators: |
| 1330 | * http://www.lua.org/pil/13.1.html |
| 1331 | * |
| 1332 | * __add "+" |
| 1333 | * __sub "-" |
| 1334 | * __mul "*" |
| 1335 | * __div "/" |
| 1336 | * __unm "!" |
| 1337 | * __pow "^" |
| 1338 | * __concat ".." |
| 1339 | * |
| 1340 | * Special methods for redfining standar relations |
| 1341 | * http://www.lua.org/pil/13.2.html |
| 1342 | * |
| 1343 | * __eq "==" |
| 1344 | * __lt "<" |
| 1345 | * __le "<=" |
| 1346 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1347 | |
| 1348 | /* |
| 1349 | * |
| 1350 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1351 | * Class Map |
| 1352 | * |
| 1353 | * |
| 1354 | */ |
| 1355 | |
| 1356 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1357 | * a class session, otherwise it throws an error. |
| 1358 | */ |
| 1359 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1360 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1361 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1362 | } |
| 1363 | |
| 1364 | /* This function is the map constructor. It don't need |
| 1365 | * the class Map object. It creates and return a new Map |
| 1366 | * object. It must be called only during "body" or "init" |
| 1367 | * context because it process some filesystem accesses. |
| 1368 | */ |
| 1369 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1370 | { |
| 1371 | const char *fn; |
| 1372 | int match = PAT_MATCH_STR; |
| 1373 | struct sample_conv conv; |
| 1374 | const char *file = ""; |
| 1375 | int line = 0; |
| 1376 | lua_Debug ar; |
| 1377 | char *err = NULL; |
| 1378 | struct arg args[2]; |
| 1379 | |
| 1380 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1381 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1382 | |
| 1383 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1384 | |
| 1385 | if (lua_gettop(L) >= 2) { |
| 1386 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1387 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1388 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1389 | } |
| 1390 | |
| 1391 | /* Get Lua filename and line number. */ |
| 1392 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1393 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1394 | if (ar.currentline > 0) { /* is there info? */ |
| 1395 | file = ar.short_src; |
| 1396 | line = ar.currentline; |
| 1397 | } |
| 1398 | } |
| 1399 | |
| 1400 | /* fill fake sample_conv struct. */ |
| 1401 | conv.kw = ""; /* unused. */ |
| 1402 | conv.process = NULL; /* unused. */ |
| 1403 | conv.arg_mask = 0; /* unused. */ |
| 1404 | conv.val_args = NULL; /* unused. */ |
| 1405 | conv.out_type = SMP_T_STR; |
| 1406 | conv.private = (void *)(long)match; |
| 1407 | switch (match) { |
| 1408 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1409 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1410 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1411 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1412 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1413 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1414 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1415 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1416 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1417 | default: |
| 1418 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1419 | } |
| 1420 | |
| 1421 | /* fill fake args. */ |
| 1422 | args[0].type = ARGT_STR; |
| 1423 | args[0].data.str.str = (char *)fn; |
| 1424 | args[1].type = ARGT_STOP; |
| 1425 | |
| 1426 | /* load the map. */ |
| 1427 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1428 | /* error case: we cant use luaL_error because we must |
| 1429 | * free the err variable. |
| 1430 | */ |
| 1431 | luaL_where(L, 1); |
| 1432 | lua_pushfstring(L, "'new': %s.", err); |
| 1433 | lua_concat(L, 2); |
| 1434 | free(err); |
| 1435 | WILL_LJMP(lua_error(L)); |
| 1436 | } |
| 1437 | |
| 1438 | /* create the lua object. */ |
| 1439 | lua_newtable(L); |
| 1440 | lua_pushlightuserdata(L, args[0].data.map); |
| 1441 | lua_rawseti(L, -2, 0); |
| 1442 | |
| 1443 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1444 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1445 | lua_setmetatable(L, -2); |
| 1446 | |
| 1447 | |
| 1448 | return 1; |
| 1449 | } |
| 1450 | |
| 1451 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1452 | { |
| 1453 | struct map_descriptor *desc; |
| 1454 | struct pattern *pat; |
| 1455 | struct sample smp; |
| 1456 | |
| 1457 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1458 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1459 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1460 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1461 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1462 | } |
| 1463 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1464 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1465 | smp.flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1466 | smp.data.u.str.str = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.len)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1470 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1471 | if (str) |
| 1472 | lua_pushstring(L, ""); |
| 1473 | else |
| 1474 | lua_pushnil(L); |
| 1475 | return 1; |
| 1476 | } |
| 1477 | |
| 1478 | /* The Lua pattern must return a string, so we can't check the returned type */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1479 | lua_pushlstring(L, pat->data->u.str.str, pat->data->u.str.len); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1480 | return 1; |
| 1481 | } |
| 1482 | |
| 1483 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1484 | { |
| 1485 | return _hlua_map_lookup(L, 0); |
| 1486 | } |
| 1487 | |
| 1488 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1489 | { |
| 1490 | return _hlua_map_lookup(L, 1); |
| 1491 | } |
| 1492 | |
| 1493 | /* |
| 1494 | * |
| 1495 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1496 | * Class Socket |
| 1497 | * |
| 1498 | * |
| 1499 | */ |
| 1500 | |
| 1501 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1502 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1503 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | /* This function is the handler called for each I/O on the established |
| 1507 | * connection. It is used for notify space avalaible to send or data |
| 1508 | * received. |
| 1509 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1510 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1511 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1512 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1513 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1514 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1515 | if (appctx->ctx.hlua_cosocket.die) { |
| 1516 | si_shutw(si); |
| 1517 | si_shutr(si); |
| 1518 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1519 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1520 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1521 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1522 | } |
| 1523 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1524 | /* If the connection object is not avalaible, close all the |
| 1525 | * streams and wakeup everithing waiting for. |
| 1526 | */ |
| 1527 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1528 | si_shutw(si); |
| 1529 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1530 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1531 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1532 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1533 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1534 | } |
| 1535 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1536 | /* If we cant write, wakeup the pending write signals. */ |
| 1537 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1538 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1539 | |
| 1540 | /* If we cant read, wakeup the pending read signals. */ |
| 1541 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1542 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1543 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1544 | /* if the connection is not estabkished, inform the stream that we want |
| 1545 | * to be notified whenever the connection completes. |
| 1546 | */ |
| 1547 | if (!(c->flags & CO_FL_CONNECTED)) { |
| 1548 | si_applet_cant_get(si); |
| 1549 | si_applet_cant_put(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1550 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1551 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1552 | |
| 1553 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1554 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1555 | |
| 1556 | /* 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] | 1557 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1558 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1559 | |
| 1560 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1561 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1562 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1563 | |
| 1564 | /* Some data were injected in the buffer, notify the stream |
| 1565 | * interface. |
| 1566 | */ |
| 1567 | if (!channel_is_empty(si_ic(si))) |
| 1568 | stream_int_update(si); |
| 1569 | |
| 1570 | /* If write notifications are registered, we considers we want |
| 1571 | * to write, so we set the flag cant put |
| 1572 | */ |
| 1573 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
| 1574 | si_applet_cant_put(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1575 | } |
| 1576 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1577 | /* This function is called when the "struct stream" is destroyed. |
| 1578 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1579 | * Wake all the pending signals. |
| 1580 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1581 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1582 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1583 | struct xref *peer; |
| 1584 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1585 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1586 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1587 | if (peer) |
| 1588 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1589 | |
| 1590 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1591 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1592 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1596 | * uses this object. If the stream does not exists, just quit. |
| 1597 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1598 | * pending signal can rest in the read and write lists. destroy |
| 1599 | * it. |
| 1600 | */ |
| 1601 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1602 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1603 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1604 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1605 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1606 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1607 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1608 | |
| 1609 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1610 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1611 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1612 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1613 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1614 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1615 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1616 | appctx->ctx.hlua_cosocket.die = 1; |
| 1617 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1618 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1619 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1620 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1621 | return 0; |
| 1622 | } |
| 1623 | |
| 1624 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1625 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1626 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1627 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1628 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1629 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1630 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1631 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1632 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1633 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1634 | |
| 1635 | /* Check if we run on the same thread than the xreator thread. |
| 1636 | * We cannot access to the socket if the thread is different. |
| 1637 | */ |
| 1638 | if (socket->tid != tid) |
| 1639 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1640 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1641 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1642 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1643 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1644 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1645 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1646 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1647 | appctx->ctx.hlua_cosocket.die = 1; |
| 1648 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1649 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1650 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1651 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1652 | return 0; |
| 1653 | } |
| 1654 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1655 | /* The close function calls close_helper. |
| 1656 | */ |
| 1657 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1658 | { |
| 1659 | MAY_LJMP(check_args(L, 1, "close")); |
| 1660 | return hlua_socket_close_helper(L); |
| 1661 | } |
| 1662 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1663 | /* This Lua function assumes that the stack contain three parameters. |
| 1664 | * 1 - USERDATA containing a struct socket |
| 1665 | * 2 - INTEGER with values of the macro defined below |
| 1666 | * If the integer is -1, we must read at most one line. |
| 1667 | * If the integer is -2, we ust read all the data until the |
| 1668 | * end of the stream. |
| 1669 | * If the integer is positive value, we must read a number of |
| 1670 | * bytes corresponding to this value. |
| 1671 | */ |
| 1672 | #define HLSR_READ_LINE (-1) |
| 1673 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1674 | __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] | 1675 | { |
| 1676 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1677 | int wanted = lua_tointeger(L, 2); |
| 1678 | struct hlua *hlua = hlua_gethlua(L); |
| 1679 | struct appctx *appctx; |
| 1680 | int len; |
| 1681 | int nblk; |
| 1682 | char *blk1; |
| 1683 | int len1; |
| 1684 | char *blk2; |
| 1685 | int len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1686 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1687 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1688 | struct stream_interface *si; |
| 1689 | struct stream *s; |
| 1690 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1691 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1692 | |
| 1693 | /* Check if this lua stack is schedulable. */ |
| 1694 | if (!hlua || !hlua->task) |
| 1695 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1696 | "'frontend', 'backend' or 'task'")); |
| 1697 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1698 | /* Check if we run on the same thread than the xreator thread. |
| 1699 | * We cannot access to the socket if the thread is different. |
| 1700 | */ |
| 1701 | if (socket->tid != tid) |
| 1702 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1703 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1704 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1705 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1706 | if (!peer) |
| 1707 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1708 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1709 | si = appctx->owner; |
| 1710 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1711 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1712 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1713 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1714 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1715 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1716 | if (nblk < 0) /* Connection close. */ |
| 1717 | goto connection_closed; |
| 1718 | if (nblk == 0) /* No data avalaible. */ |
| 1719 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1720 | |
| 1721 | /* remove final \r\n. */ |
| 1722 | if (nblk == 1) { |
| 1723 | if (blk1[len1-1] == '\n') { |
| 1724 | len1--; |
| 1725 | skip_at_end++; |
| 1726 | if (blk1[len1-1] == '\r') { |
| 1727 | len1--; |
| 1728 | skip_at_end++; |
| 1729 | } |
| 1730 | } |
| 1731 | } |
| 1732 | else { |
| 1733 | if (blk2[len2-1] == '\n') { |
| 1734 | len2--; |
| 1735 | skip_at_end++; |
| 1736 | if (blk2[len2-1] == '\r') { |
| 1737 | len2--; |
| 1738 | skip_at_end++; |
| 1739 | } |
| 1740 | } |
| 1741 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1745 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1746 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1747 | if (nblk < 0) /* Connection close. */ |
| 1748 | goto connection_closed; |
| 1749 | if (nblk == 0) /* No data avalaible. */ |
| 1750 | goto connection_empty; |
| 1751 | } |
| 1752 | |
| 1753 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1754 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1755 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1756 | if (nblk < 0) /* Connection close. */ |
| 1757 | goto connection_closed; |
| 1758 | if (nblk == 0) /* No data avalaible. */ |
| 1759 | goto connection_empty; |
| 1760 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1761 | missing_bytes = wanted - socket->b.n; |
| 1762 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1763 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1764 | len1 = missing_bytes; |
| 1765 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1766 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1767 | } |
| 1768 | |
| 1769 | len = len1; |
| 1770 | |
| 1771 | luaL_addlstring(&socket->b, blk1, len1); |
| 1772 | if (nblk == 2) { |
| 1773 | len += len2; |
| 1774 | luaL_addlstring(&socket->b, blk2, len2); |
| 1775 | } |
| 1776 | |
| 1777 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1778 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1779 | |
| 1780 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1781 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1782 | |
| 1783 | /* If the pattern reclaim to read all the data |
| 1784 | * in the connection, got out. |
| 1785 | */ |
| 1786 | if (wanted == HLSR_READ_ALL) |
| 1787 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1788 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1789 | goto connection_empty; |
| 1790 | |
| 1791 | /* Return result. */ |
| 1792 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1793 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1794 | return 1; |
| 1795 | |
| 1796 | connection_closed: |
| 1797 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1798 | xref_unlock(&socket->xref, peer); |
| 1799 | |
| 1800 | no_peer: |
| 1801 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1802 | /* If the buffer containds data. */ |
| 1803 | if (socket->b.n > 0) { |
| 1804 | luaL_pushresult(&socket->b); |
| 1805 | return 1; |
| 1806 | } |
| 1807 | lua_pushnil(L); |
| 1808 | lua_pushstring(L, "connection closed."); |
| 1809 | return 2; |
| 1810 | |
| 1811 | connection_empty: |
| 1812 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1813 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1814 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1815 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1816 | } |
| 1817 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1818 | 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] | 1819 | return 0; |
| 1820 | } |
| 1821 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1822 | /* This Lua function gets two parameters. The first one can be string |
| 1823 | * or a number. If the string is "*l", the user requires one line. If |
| 1824 | * 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] | 1825 | * If the value is a number, the user require a number of bytes equal |
| 1826 | * to the value. The default value is "*l" (a line). |
| 1827 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1828 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1829 | * integer takes this values: |
| 1830 | * -1 : read a line |
| 1831 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1832 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1833 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1834 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1835 | * concatenated with the read data. |
| 1836 | */ |
| 1837 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1838 | { |
| 1839 | int wanted = HLSR_READ_LINE; |
| 1840 | const char *pattern; |
| 1841 | int type; |
| 1842 | char *error; |
| 1843 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1844 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1845 | |
| 1846 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1847 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1848 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1849 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1850 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1851 | /* Check if we run on the same thread than the xreator thread. |
| 1852 | * We cannot access to the socket if the thread is different. |
| 1853 | */ |
| 1854 | if (socket->tid != tid) |
| 1855 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1856 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1857 | /* check for pattern. */ |
| 1858 | if (lua_gettop(L) >= 2) { |
| 1859 | type = lua_type(L, 2); |
| 1860 | if (type == LUA_TSTRING) { |
| 1861 | pattern = lua_tostring(L, 2); |
| 1862 | if (strcmp(pattern, "*a") == 0) |
| 1863 | wanted = HLSR_READ_ALL; |
| 1864 | else if (strcmp(pattern, "*l") == 0) |
| 1865 | wanted = HLSR_READ_LINE; |
| 1866 | else { |
| 1867 | wanted = strtoll(pattern, &error, 10); |
| 1868 | if (*error != '\0') |
| 1869 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1870 | } |
| 1871 | } |
| 1872 | else if (type == LUA_TNUMBER) { |
| 1873 | wanted = lua_tointeger(L, 2); |
| 1874 | if (wanted < 0) |
| 1875 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | /* Set pattern. */ |
| 1880 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1881 | |
| 1882 | /* Check if we would replace the top by itself. */ |
| 1883 | if (lua_gettop(L) != 2) |
| 1884 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1885 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1886 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1887 | luaL_buffinit(L, &socket->b); |
| 1888 | |
| 1889 | /* Check prefix. */ |
| 1890 | if (lua_gettop(L) >= 3) { |
| 1891 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1892 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1893 | pattern = lua_tolstring(L, 3, &len); |
| 1894 | luaL_addlstring(&socket->b, pattern, len); |
| 1895 | } |
| 1896 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1897 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1898 | } |
| 1899 | |
| 1900 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1901 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1902 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1903 | 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] | 1904 | { |
| 1905 | struct hlua_socket *socket; |
| 1906 | struct hlua *hlua = hlua_gethlua(L); |
| 1907 | struct appctx *appctx; |
| 1908 | size_t buf_len; |
| 1909 | const char *buf; |
| 1910 | int len; |
| 1911 | int send_len; |
| 1912 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1913 | struct xref *peer; |
| 1914 | struct stream_interface *si; |
| 1915 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1916 | |
| 1917 | /* Check if this lua stack is schedulable. */ |
| 1918 | if (!hlua || !hlua->task) |
| 1919 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1920 | "'frontend', 'backend' or 'task'")); |
| 1921 | |
| 1922 | /* Get object */ |
| 1923 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1924 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1925 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1926 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1927 | /* Check if we run on the same thread than the xreator thread. |
| 1928 | * We cannot access to the socket if the thread is different. |
| 1929 | */ |
| 1930 | if (socket->tid != tid) |
| 1931 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1932 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1933 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1934 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1935 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1936 | lua_pushinteger(L, -1); |
| 1937 | return 1; |
| 1938 | } |
| 1939 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1940 | si = appctx->owner; |
| 1941 | s = si_strm(si); |
| 1942 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1943 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1944 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1945 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1946 | lua_pushinteger(L, -1); |
| 1947 | return 1; |
| 1948 | } |
| 1949 | |
| 1950 | /* Update the input buffer data. */ |
| 1951 | buf += sent; |
| 1952 | send_len = buf_len - sent; |
| 1953 | |
| 1954 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1955 | if (sent >= buf_len) { |
| 1956 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1957 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1958 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1959 | |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1960 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 1961 | * the request buffer if its not required. |
| 1962 | */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1963 | if (s->req.buf->size == 0) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1964 | if (!channel_alloc_buffer(&s->req, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1965 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1966 | } |
| 1967 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1968 | /* Check for avalaible space. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1969 | len = buffer_total_space(s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1970 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1971 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1972 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1973 | |
| 1974 | /* send data */ |
| 1975 | if (len < send_len) |
| 1976 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 1977 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1978 | |
| 1979 | /* "Not enough space" (-1), "Buffer too little to contain |
| 1980 | * the data" (-2) are not expected because the available length |
| 1981 | * is tested. |
| 1982 | * Other unknown error are also not expected. |
| 1983 | */ |
| 1984 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1985 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1986 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1987 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1988 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1989 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1990 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1991 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1992 | return 1; |
| 1993 | } |
| 1994 | |
| 1995 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1996 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 1997 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1998 | s->req.rex = TICK_ETERNITY; |
| 1999 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2000 | |
| 2001 | /* Update length sent. */ |
| 2002 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2003 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2004 | |
| 2005 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2006 | if (sent + len >= buf_len) { |
| 2007 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2008 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2009 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2010 | |
| 2011 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2012 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2013 | xref_unlock(&socket->xref, peer); |
| 2014 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2015 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2016 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2017 | 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] | 2018 | return 0; |
| 2019 | } |
| 2020 | |
| 2021 | /* This function initiate the send of data. It just check the input |
| 2022 | * parameters and push an integer in the Lua stack that contain the |
| 2023 | * amount of data writed in the buffer. This is used by the function |
| 2024 | * "hlua_socket_write_yield" that can yield. |
| 2025 | * |
| 2026 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2027 | * the associated object. The second is a string buffer. The third is |
| 2028 | * a facultative integer that represents where is the buffer position |
| 2029 | * of the start of the data that can send. The first byte is the |
| 2030 | * position "1". The default value is "1". The fourth argument is a |
| 2031 | * facultative integer that represents where is the buffer position |
| 2032 | * of the end of the data that can send. The default is the last byte. |
| 2033 | */ |
| 2034 | static int hlua_socket_send(struct lua_State *L) |
| 2035 | { |
| 2036 | int i; |
| 2037 | int j; |
| 2038 | const char *buf; |
| 2039 | size_t buf_len; |
| 2040 | |
| 2041 | /* Check number of arguments. */ |
| 2042 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2043 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2044 | |
| 2045 | /* Get the string. */ |
| 2046 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2047 | |
| 2048 | /* Get and check j. */ |
| 2049 | if (lua_gettop(L) == 4) { |
| 2050 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2051 | if (j < 0) |
| 2052 | j = buf_len + j + 1; |
| 2053 | if (j > buf_len) |
| 2054 | j = buf_len + 1; |
| 2055 | lua_pop(L, 1); |
| 2056 | } |
| 2057 | else |
| 2058 | j = buf_len; |
| 2059 | |
| 2060 | /* Get and check i. */ |
| 2061 | if (lua_gettop(L) == 3) { |
| 2062 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2063 | if (i < 0) |
| 2064 | i = buf_len + i + 1; |
| 2065 | if (i > buf_len) |
| 2066 | i = buf_len + 1; |
| 2067 | lua_pop(L, 1); |
| 2068 | } else |
| 2069 | i = 1; |
| 2070 | |
| 2071 | /* Check bth i and j. */ |
| 2072 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2073 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2074 | return 1; |
| 2075 | } |
| 2076 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2077 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2078 | return 1; |
| 2079 | } |
| 2080 | if (i == 0) |
| 2081 | i = 1; |
| 2082 | if (j == 0) |
| 2083 | j = 1; |
| 2084 | |
| 2085 | /* Pop the string. */ |
| 2086 | lua_pop(L, 1); |
| 2087 | |
| 2088 | /* Update the buffer length. */ |
| 2089 | buf += i - 1; |
| 2090 | buf_len = j - i + 1; |
| 2091 | lua_pushlstring(L, buf, buf_len); |
| 2092 | |
| 2093 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2094 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2095 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2096 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2097 | } |
| 2098 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2099 | #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] | 2100 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2101 | { |
| 2102 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2103 | int ret; |
| 2104 | int len; |
| 2105 | char *p; |
| 2106 | |
| 2107 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2108 | if (ret <= 0) { |
| 2109 | lua_pushnil(L); |
| 2110 | return 1; |
| 2111 | } |
| 2112 | |
| 2113 | if (ret == AF_UNIX) { |
| 2114 | lua_pushstring(L, buffer+1); |
| 2115 | return 1; |
| 2116 | } |
| 2117 | else if (ret == AF_INET6) { |
| 2118 | buffer[0] = '['; |
| 2119 | len = strlen(buffer); |
| 2120 | buffer[len] = ']'; |
| 2121 | len++; |
| 2122 | buffer[len] = ':'; |
| 2123 | len++; |
| 2124 | p = buffer; |
| 2125 | } |
| 2126 | else if (ret == AF_INET) { |
| 2127 | p = buffer + 1; |
| 2128 | len = strlen(p); |
| 2129 | p[len] = ':'; |
| 2130 | len++; |
| 2131 | } |
| 2132 | else { |
| 2133 | lua_pushnil(L); |
| 2134 | return 1; |
| 2135 | } |
| 2136 | |
| 2137 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2138 | lua_pushnil(L); |
| 2139 | return 1; |
| 2140 | } |
| 2141 | |
| 2142 | lua_pushstring(L, p); |
| 2143 | return 1; |
| 2144 | } |
| 2145 | |
| 2146 | /* Returns information about the peer of the connection. */ |
| 2147 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2148 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2149 | struct hlua_socket *socket; |
| 2150 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2151 | struct xref *peer; |
| 2152 | struct appctx *appctx; |
| 2153 | struct stream_interface *si; |
| 2154 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2155 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2156 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2157 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2158 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2159 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2160 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2161 | /* Check if we run on the same thread than the xreator thread. |
| 2162 | * We cannot access to the socket if the thread is different. |
| 2163 | */ |
| 2164 | if (socket->tid != tid) |
| 2165 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2166 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2167 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2168 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2169 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2170 | lua_pushnil(L); |
| 2171 | return 1; |
| 2172 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2173 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2174 | si = appctx->owner; |
| 2175 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2176 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2177 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2178 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2179 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2180 | lua_pushnil(L); |
| 2181 | return 1; |
| 2182 | } |
| 2183 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2184 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2185 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2186 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2187 | lua_pushnil(L); |
| 2188 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2189 | } |
| 2190 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2191 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2192 | xref_unlock(&socket->xref, peer); |
| 2193 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | /* Returns information about my connection side. */ |
| 2197 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2198 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2199 | struct hlua_socket *socket; |
| 2200 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2201 | struct appctx *appctx; |
| 2202 | struct xref *peer; |
| 2203 | struct stream_interface *si; |
| 2204 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2205 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2206 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2207 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2208 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2209 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2210 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2211 | /* Check if we run on the same thread than the xreator thread. |
| 2212 | * We cannot access to the socket if the thread is different. |
| 2213 | */ |
| 2214 | if (socket->tid != tid) |
| 2215 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2216 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2217 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2218 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2219 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2220 | lua_pushnil(L); |
| 2221 | return 1; |
| 2222 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2223 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2224 | si = appctx->owner; |
| 2225 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2226 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2227 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2228 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2229 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2230 | lua_pushnil(L); |
| 2231 | return 1; |
| 2232 | } |
| 2233 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2234 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2235 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2236 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2237 | lua_pushnil(L); |
| 2238 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2239 | } |
| 2240 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2241 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2242 | xref_unlock(&socket->xref, peer); |
| 2243 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2244 | } |
| 2245 | |
| 2246 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2247 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2248 | .obj_type = OBJ_TYPE_APPLET, |
| 2249 | .name = "<LUA_TCP>", |
| 2250 | .fct = hlua_socket_handler, |
| 2251 | .release = hlua_socket_release, |
| 2252 | }; |
| 2253 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2254 | __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] | 2255 | { |
| 2256 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2257 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2258 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2259 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2260 | struct stream_interface *si; |
| 2261 | struct stream *s; |
| 2262 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2263 | /* Check if we run on the same thread than the xreator thread. |
| 2264 | * We cannot access to the socket if the thread is different. |
| 2265 | */ |
| 2266 | if (socket->tid != tid) |
| 2267 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2268 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2269 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2270 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2271 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2272 | lua_pushnil(L); |
| 2273 | lua_pushstring(L, "Can't connect"); |
| 2274 | return 2; |
| 2275 | } |
| 2276 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2277 | si = appctx->owner; |
| 2278 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2279 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2280 | /* Check if we run on the same thread than the xreator thread. |
| 2281 | * We cannot access to the socket if the thread is different. |
| 2282 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2283 | if (socket->tid != tid) { |
| 2284 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2285 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2286 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2287 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2289 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2290 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2291 | lua_pushnil(L); |
| 2292 | lua_pushstring(L, "Can't connect"); |
| 2293 | return 2; |
| 2294 | } |
| 2295 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2296 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | |
| 2298 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2299 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2300 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2301 | lua_pushinteger(L, 1); |
| 2302 | return 1; |
| 2303 | } |
| 2304 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2305 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2306 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2307 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2308 | } |
| 2309 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2310 | 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] | 2311 | return 0; |
| 2312 | } |
| 2313 | |
| 2314 | /* This function fail or initite the connection. */ |
| 2315 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2316 | { |
| 2317 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2318 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2319 | const char *ip; |
| 2320 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2321 | struct hlua *hlua; |
| 2322 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2323 | int low, high; |
| 2324 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2325 | struct xref *peer; |
| 2326 | struct stream_interface *si; |
| 2327 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2328 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2329 | if (lua_gettop(L) < 2) |
| 2330 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2331 | |
| 2332 | /* Get args. */ |
| 2333 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2334 | |
| 2335 | /* Check if we run on the same thread than the xreator thread. |
| 2336 | * We cannot access to the socket if the thread is different. |
| 2337 | */ |
| 2338 | if (socket->tid != tid) |
| 2339 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2340 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2341 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2342 | if (lua_gettop(L) >= 3) { |
| 2343 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2344 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2345 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2346 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2347 | * that are not enclosed within square brackets. |
| 2348 | */ |
| 2349 | if (port > 0) { |
| 2350 | luaL_buffinit(L, &b); |
| 2351 | luaL_addstring(&b, ip); |
| 2352 | luaL_addchar(&b, ':'); |
| 2353 | luaL_pushresult(&b); |
| 2354 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2355 | } |
| 2356 | } |
| 2357 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2358 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2359 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2360 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2361 | lua_pushnil(L); |
| 2362 | return 1; |
| 2363 | } |
| 2364 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2365 | si = appctx->owner; |
| 2366 | s = si_strm(si); |
| 2367 | |
| 2368 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2369 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2370 | if (!conn) { |
| 2371 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2372 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2373 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2374 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2375 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2376 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2377 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2378 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2379 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2380 | if (!addr) { |
| 2381 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2382 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2383 | } |
| 2384 | if (low != high) { |
| 2385 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2386 | 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] | 2387 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2388 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | |
| 2390 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2391 | if (low == 0) { |
| 2392 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2393 | if (port == -1) { |
| 2394 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2395 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2396 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2397 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2398 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2399 | if (port == -1) { |
| 2400 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2401 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2402 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2403 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2404 | } |
| 2405 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2406 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2407 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2408 | appctx = objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2409 | |
| 2410 | /* inform the stream that we want to be notified whenever the |
| 2411 | * connection completes. |
| 2412 | */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2413 | si_applet_cant_get(&s->si[0]); |
| 2414 | si_applet_cant_put(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2415 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2416 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2417 | hlua->flags |= HLUA_MUST_GC; |
| 2418 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2419 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2420 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2421 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2422 | } |
| 2423 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2424 | |
| 2425 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2426 | /* Return yield waiting for connection. */ |
| 2427 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2428 | 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] | 2429 | |
| 2430 | return 0; |
| 2431 | } |
| 2432 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2433 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2434 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2435 | { |
| 2436 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2437 | struct xref *peer; |
| 2438 | struct appctx *appctx; |
| 2439 | struct stream_interface *si; |
| 2440 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2441 | |
| 2442 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2443 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2444 | |
| 2445 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2446 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2447 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2448 | lua_pushnil(L); |
| 2449 | return 1; |
| 2450 | } |
| 2451 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2452 | si = appctx->owner; |
| 2453 | s = si_strm(si); |
| 2454 | |
| 2455 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2456 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2457 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2458 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2459 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2460 | |
| 2461 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2462 | { |
| 2463 | return 0; |
| 2464 | } |
| 2465 | |
| 2466 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2467 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2468 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2469 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2470 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2471 | struct xref *peer; |
| 2472 | struct appctx *appctx; |
| 2473 | struct stream_interface *si; |
| 2474 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2475 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2476 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2477 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2478 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2479 | |
| 2480 | /* round up for inputs that are fractions and convert to millis */ |
| 2481 | dtmout = (0.5 + MAY_LJMP(luaL_checknumber(L, 2))) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2482 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2483 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2484 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2485 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2486 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2487 | if (dtmout > INT_MAX) /* overflow check */ |
| 2488 | WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d", INT_MAX)); |
| 2489 | |
| 2490 | tmout = MS_TO_TICKS((int)dtmout); |
| 2491 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2492 | /* Check if we run on the same thread than the xreator thread. |
| 2493 | * We cannot access to the socket if the thread is different. |
| 2494 | */ |
| 2495 | if (socket->tid != tid) |
| 2496 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2497 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2498 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2499 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2500 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2501 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2502 | WILL_LJMP(lua_error(L)); |
| 2503 | return 0; |
| 2504 | } |
| 2505 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2506 | si = appctx->owner; |
| 2507 | s = si_strm(si); |
| 2508 | |
| 2509 | s->req.rto = tmout; |
| 2510 | s->req.wto = tmout; |
| 2511 | s->res.rto = tmout; |
| 2512 | s->res.wto = tmout; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2513 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2514 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2515 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2516 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2520 | { |
| 2521 | struct hlua_socket *socket; |
| 2522 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2523 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2524 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2525 | |
| 2526 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2527 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2528 | hlua_pusherror(L, "socket: full stack"); |
| 2529 | goto out_fail_conf; |
| 2530 | } |
| 2531 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2532 | /* Create the object: obj[0] = userdata. */ |
| 2533 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2534 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2535 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2536 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2537 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2538 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2539 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2540 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2541 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2542 | goto out_fail_conf; |
| 2543 | } |
| 2544 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2545 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2546 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2547 | lua_setmetatable(L, -2); |
| 2548 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2549 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2550 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2551 | if (!appctx) { |
| 2552 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2553 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2554 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2555 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2556 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2557 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2558 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2559 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2560 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2561 | /* Now create a session, task and stream for this applet */ |
| 2562 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2563 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2564 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2565 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2566 | } |
| 2567 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2568 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2569 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2570 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2571 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2572 | } |
| 2573 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2574 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2575 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2576 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2577 | /* Configure "right" stream interface. this "si" is used to connect |
| 2578 | * and retrieve data from the server. The connection is initialized |
| 2579 | * with the "struct server". |
| 2580 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2581 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2582 | |
| 2583 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2584 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2585 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2586 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2587 | return 1; |
| 2588 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2589 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2590 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2591 | out_fail_sess: |
| 2592 | appctx_free(appctx); |
| 2593 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2594 | WILL_LJMP(lua_error(L)); |
| 2595 | return 0; |
| 2596 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2597 | |
| 2598 | /* |
| 2599 | * |
| 2600 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2601 | * Class Channel |
| 2602 | * |
| 2603 | * |
| 2604 | */ |
| 2605 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2606 | /* The state between the channel data and the HTTP parser state can be |
| 2607 | * unconsistent, so reset the parser and call it again. Warning, this |
| 2608 | * action not revalidate the request and not send a 400 if the modified |
| 2609 | * resuest is not valid. |
| 2610 | * |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2611 | * This function never fails. The direction is set using dir, which equals |
| 2612 | * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2613 | */ |
| 2614 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2615 | { |
| 2616 | /* Protocol HTTP. */ |
| 2617 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2618 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2619 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2620 | http_txn_reset_req(stream->txn); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2621 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2622 | http_txn_reset_res(stream->txn); |
| 2623 | |
| 2624 | if (stream->txn->hdr_idx.v) |
| 2625 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2626 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2627 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2628 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2629 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2630 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2631 | } |
| 2632 | } |
| 2633 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2634 | /* This function is called before the Lua execution. It stores |
| 2635 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2636 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2637 | 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] | 2638 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2639 | c->mode = stream->be->mode; |
| 2640 | switch (c->mode) { |
| 2641 | case PR_MODE_HTTP: |
| 2642 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2643 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2644 | c->data.http.state = stream->txn->req.msg_state; |
| 2645 | else |
| 2646 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2647 | break; |
| 2648 | default: |
| 2649 | break; |
| 2650 | } |
| 2651 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2652 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2653 | /* This function is called after the Lua execution. it |
| 2654 | * returns true if the parser state is consistent, otherwise, |
| 2655 | * it return false. |
| 2656 | * |
| 2657 | * In HTTP mode, the parser state must be in the same state |
| 2658 | * or greater when we exit the function. Even if we do a |
| 2659 | * control yield. This prevent to break the HTTP message |
| 2660 | * from the Lua code. |
| 2661 | */ |
| 2662 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2663 | { |
| 2664 | if (c->mode != stream->be->mode) |
| 2665 | return 0; |
| 2666 | |
| 2667 | switch (c->mode) { |
| 2668 | case PR_MODE_HTTP: |
| 2669 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2670 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2671 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2672 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2673 | else |
| 2674 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2675 | default: |
| 2676 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2677 | } |
| 2678 | return 1; |
| 2679 | } |
| 2680 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2681 | /* Returns the struct hlua_channel join to the class channel in the |
| 2682 | * stack entry "ud" or throws an argument error. |
| 2683 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2684 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2685 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2686 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2687 | } |
| 2688 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2689 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2690 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2691 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2692 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2693 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2694 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2695 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2696 | return 0; |
| 2697 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2698 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2699 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2700 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2701 | |
| 2702 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2703 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2704 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2705 | return 1; |
| 2706 | } |
| 2707 | |
| 2708 | /* Duplicate all the data present in the input channel and put it |
| 2709 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2710 | * the stack if the channel is closed and all the data are consumed, |
| 2711 | * returns 0 if no data are available, otherwise it returns the length |
| 2712 | * of the builded string. |
| 2713 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2714 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2715 | { |
| 2716 | char *blk1; |
| 2717 | char *blk2; |
| 2718 | int len1; |
| 2719 | int len2; |
| 2720 | int ret; |
| 2721 | luaL_Buffer b; |
| 2722 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2723 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2724 | if (unlikely(ret == 0)) |
| 2725 | return 0; |
| 2726 | |
| 2727 | if (unlikely(ret < 0)) { |
| 2728 | lua_pushnil(L); |
| 2729 | return -1; |
| 2730 | } |
| 2731 | |
| 2732 | luaL_buffinit(L, &b); |
| 2733 | luaL_addlstring(&b, blk1, len1); |
| 2734 | if (unlikely(ret == 2)) |
| 2735 | luaL_addlstring(&b, blk2, len2); |
| 2736 | luaL_pushresult(&b); |
| 2737 | |
| 2738 | if (unlikely(ret == 2)) |
| 2739 | return len1 + len2; |
| 2740 | return len1; |
| 2741 | } |
| 2742 | |
| 2743 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2744 | * a yield. This function keep the data in the buffer. |
| 2745 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2746 | __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] | 2747 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2748 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2749 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2750 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2751 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2752 | if (_hlua_channel_dup(chn, L) == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2753 | 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] | 2754 | return 1; |
| 2755 | } |
| 2756 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2757 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2758 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2759 | { |
| 2760 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2761 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2762 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2763 | } |
| 2764 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2765 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2766 | * a yield. This function consumes the data in the buffer. It returns |
| 2767 | * a string containing the data or a nil pointer if no data are available |
| 2768 | * and the channel is closed. |
| 2769 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2770 | __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] | 2771 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2772 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2773 | int ret; |
| 2774 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2775 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2776 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2777 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2778 | if (unlikely(ret == 0)) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2779 | 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] | 2780 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2781 | if (unlikely(ret == -1)) |
| 2782 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2783 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2784 | chn->buf->i -= ret; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2785 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2786 | return 1; |
| 2787 | } |
| 2788 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2789 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 2790 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2791 | { |
| 2792 | MAY_LJMP(check_args(L, 1, "get")); |
| 2793 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2794 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2795 | } |
| 2796 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2797 | /* This functions consumes and returns one line. If the channel is closed, |
| 2798 | * and the last data does not contains a final '\n', the data are returned |
| 2799 | * without the final '\n'. When no more data are avalaible, it returns nil |
| 2800 | * value. |
| 2801 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2802 | __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] | 2803 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2804 | char *blk1; |
| 2805 | char *blk2; |
| 2806 | int len1; |
| 2807 | int len2; |
| 2808 | int len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2809 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2810 | int ret; |
| 2811 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2812 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2813 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2814 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2815 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2816 | if (ret == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2817 | 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] | 2818 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2819 | if (ret == -1) { |
| 2820 | lua_pushnil(L); |
| 2821 | return 1; |
| 2822 | } |
| 2823 | |
| 2824 | luaL_buffinit(L, &b); |
| 2825 | luaL_addlstring(&b, blk1, len1); |
| 2826 | len = len1; |
| 2827 | if (unlikely(ret == 2)) { |
| 2828 | luaL_addlstring(&b, blk2, len2); |
| 2829 | len += len2; |
| 2830 | } |
| 2831 | luaL_pushresult(&b); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2832 | buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2833 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2834 | return 1; |
| 2835 | } |
| 2836 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2837 | /* Check arguments for the fucntion "hlua_channel_getline_yield". */ |
| 2838 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2839 | { |
| 2840 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2841 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2842 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2843 | } |
| 2844 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2845 | /* This function takes a string as input, and append it at the |
| 2846 | * input side of channel. If the data is too big, but a space |
| 2847 | * is probably available after sending some data, the function |
| 2848 | * yield. If the data is bigger than the buffer, or if the |
| 2849 | * channel is closed, it returns -1. otherwise, it returns the |
| 2850 | * amount of data writed. |
| 2851 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2852 | __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] | 2853 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2854 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2855 | size_t len; |
| 2856 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2857 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2858 | int ret; |
| 2859 | int max; |
| 2860 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2861 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2862 | * the request buffer if its not required. |
| 2863 | */ |
| 2864 | if (chn->buf->size == 0) { |
| 2865 | si_applet_cant_put(chn_prod(chn)); |
| 2866 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
| 2867 | } |
| 2868 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2869 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2870 | if (max > len - l) |
| 2871 | max = len - l; |
| 2872 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2873 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2874 | if (ret == -2 || ret == -3) { |
| 2875 | lua_pushinteger(L, -1); |
| 2876 | return 1; |
| 2877 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2878 | if (ret == -1) { |
| 2879 | chn->flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2880 | 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] | 2881 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2882 | l += ret; |
| 2883 | lua_pop(L, 1); |
| 2884 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2885 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2886 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2887 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
| 2888 | if (max == 0 && chn->buf->o == 0) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2889 | /* There are no space avalaible, and the output buffer is empty. |
| 2890 | * in this case, we cannot add more data, so we cannot yield, |
| 2891 | * we return the amount of copyied data. |
| 2892 | */ |
| 2893 | return 1; |
| 2894 | } |
| 2895 | if (l < len) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2896 | 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] | 2897 | return 1; |
| 2898 | } |
| 2899 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2900 | /* just a wrapper of "hlua_channel_append_yield". It returns the length |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2901 | * of the writed string, or -1 if the channel is closed or if the |
| 2902 | * buffer size is too little for the data. |
| 2903 | */ |
| 2904 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2905 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2906 | size_t len; |
| 2907 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2908 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2909 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2910 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2911 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2912 | lua_pushinteger(L, 0); |
| 2913 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2914 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2915 | } |
| 2916 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2917 | /* just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2918 | * his process by cleaning the buffer. The result is a replacement |
| 2919 | * of the current data. It returns the length of the writed string, |
| 2920 | * or -1 if the channel is closed or if the buffer size is too |
| 2921 | * little for the data. |
| 2922 | */ |
| 2923 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2924 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2925 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2926 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2927 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2928 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2929 | lua_pushinteger(L, 0); |
| 2930 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2931 | chn->buf->i = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2932 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2933 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2934 | } |
| 2935 | |
| 2936 | /* Append data in the output side of the buffer. This data is immediatly |
| 2937 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 2938 | * cannot contains the data, the function yield. The function returns -1 |
| 2939 | * if the channel is closed. |
| 2940 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2941 | __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] | 2942 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2943 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2944 | size_t len; |
| 2945 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2946 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2947 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2948 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2949 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2950 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2951 | lua_pushinteger(L, -1); |
| 2952 | return 1; |
| 2953 | } |
| 2954 | |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2955 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2956 | * the request buffer if its not required. |
| 2957 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2958 | if (chn->buf->size == 0) { |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2959 | si_applet_cant_put(chn_prod(chn)); |
| 2960 | 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] | 2961 | } |
| 2962 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2963 | /* the writed data will be immediatly sent, so we can check |
| 2964 | * the avalaible space without taking in account the reserve. |
| 2965 | * The reserve is guaranted for the processing of incoming |
| 2966 | * data, because the buffer will be flushed. |
| 2967 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2968 | max = chn->buf->size - buffer_len(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2969 | |
| 2970 | /* If there are no space avalaible, and the output buffer is empty. |
| 2971 | * in this case, we cannot add more data, so we cannot yield, |
| 2972 | * we return the amount of copyied data. |
| 2973 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2974 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2975 | return 1; |
| 2976 | |
| 2977 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2978 | if (max > len - l) |
| 2979 | max = len - l; |
| 2980 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2981 | /* The buffer avalaible size may be not contiguous. This test |
| 2982 | * detects a non contiguous buffer and realign it. |
| 2983 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2984 | if (bi_space_for_replace(chn->buf) < max) |
| 2985 | buffer_slow_realign(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2986 | |
| 2987 | /* Copy input data in the buffer. */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2988 | max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2989 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2990 | /* buffer replace considers that the input part is filled. |
| 2991 | * so, I must forward these new data in the output part. |
| 2992 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2993 | b_adv(chn->buf, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2994 | |
| 2995 | l += max; |
| 2996 | lua_pop(L, 1); |
| 2997 | lua_pushinteger(L, l); |
| 2998 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2999 | /* If there are no space avalaible, and the output buffer is empty. |
| 3000 | * in this case, we cannot add more data, so we cannot yield, |
| 3001 | * we return the amount of copyied data. |
| 3002 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3003 | max = chn->buf->size - buffer_len(chn->buf); |
| 3004 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3005 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3006 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3007 | if (l < len) { |
| 3008 | /* If we are waiting for space in the response buffer, we |
| 3009 | * must set the flag WAKERESWR. This flag required the task |
| 3010 | * wake up if any activity is detected on the response buffer. |
| 3011 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3012 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3013 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3014 | else |
| 3015 | HLUA_SET_WAKEREQWR(hlua); |
| 3016 | 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] | 3017 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3018 | |
| 3019 | return 1; |
| 3020 | } |
| 3021 | |
| 3022 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3023 | * yield the LUA process, and resume it without checking the |
| 3024 | * input arguments. |
| 3025 | */ |
| 3026 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3027 | { |
| 3028 | MAY_LJMP(check_args(L, 2, "send")); |
| 3029 | lua_pushinteger(L, 0); |
| 3030 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3031 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | /* This function forward and amount of butes. The data pass from |
| 3035 | * the input side of the buffer to the output side, and can be |
| 3036 | * forwarded. This function never fails. |
| 3037 | * |
| 3038 | * The Lua function takes an amount of bytes to be forwarded in |
| 3039 | * imput. It returns the number of bytes forwarded. |
| 3040 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3041 | __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] | 3042 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3043 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3044 | int len; |
| 3045 | int l; |
| 3046 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3047 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3048 | |
| 3049 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3050 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3051 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3052 | |
| 3053 | max = len - l; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3054 | if (max > chn->buf->i) |
| 3055 | max = chn->buf->i; |
| 3056 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3057 | l += max; |
| 3058 | |
| 3059 | lua_pop(L, 1); |
| 3060 | lua_pushinteger(L, l); |
| 3061 | |
| 3062 | /* Check if it miss bytes to forward. */ |
| 3063 | if (l < len) { |
| 3064 | /* The the input channel or the output channel are closed, we |
| 3065 | * must return the amount of data forwarded. |
| 3066 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3067 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3068 | return 1; |
| 3069 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3070 | /* If we are waiting for space data in the response buffer, we |
| 3071 | * must set the flag WAKERESWR. This flag required the task |
| 3072 | * wake up if any activity is detected on the response buffer. |
| 3073 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3074 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3075 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3076 | else |
| 3077 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3078 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3079 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 3080 | 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] | 3081 | } |
| 3082 | |
| 3083 | return 1; |
| 3084 | } |
| 3085 | |
| 3086 | /* Just check the input and prepare the stack for the previous |
| 3087 | * function "hlua_channel_forward_yield" |
| 3088 | */ |
| 3089 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3090 | { |
| 3091 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3092 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3093 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3094 | |
| 3095 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3096 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | /* Just returns the number of bytes available in the input |
| 3100 | * side of the buffer. This function never fails. |
| 3101 | */ |
| 3102 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3103 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3104 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3105 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3106 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3107 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3108 | lua_pushinteger(L, chn->buf->i); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3109 | return 1; |
| 3110 | } |
| 3111 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3112 | /* Returns true if the channel is full. */ |
| 3113 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3114 | { |
| 3115 | struct channel *chn; |
| 3116 | int rem; |
| 3117 | |
| 3118 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3119 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3120 | |
| 3121 | rem = chn->buf->size; |
| 3122 | rem -= chn->buf->o; /* Output size */ |
| 3123 | rem -= chn->buf->i; /* Input size */ |
| 3124 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3125 | |
| 3126 | lua_pushboolean(L, rem <= 0); |
| 3127 | return 1; |
| 3128 | } |
| 3129 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3130 | /* Just returns the number of bytes available in the output |
| 3131 | * side of the buffer. This function never fails. |
| 3132 | */ |
| 3133 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3134 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3135 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3136 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3137 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3138 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3139 | lua_pushinteger(L, chn->buf->o); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3140 | return 1; |
| 3141 | } |
| 3142 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3143 | /* |
| 3144 | * |
| 3145 | * |
| 3146 | * Class Fetches |
| 3147 | * |
| 3148 | * |
| 3149 | */ |
| 3150 | |
| 3151 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3152 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3153 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3154 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3155 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3156 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3157 | } |
| 3158 | |
| 3159 | /* This function creates and push in the stack a fetch object according |
| 3160 | * with a current TXN. |
| 3161 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3162 | 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] | 3163 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3164 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3165 | |
| 3166 | /* Check stack size. */ |
| 3167 | if (!lua_checkstack(L, 3)) |
| 3168 | return 0; |
| 3169 | |
| 3170 | /* Create the object: obj[0] = userdata. |
| 3171 | * Note that the base of the Fetches object is the |
| 3172 | * transaction object. |
| 3173 | */ |
| 3174 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3175 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3176 | lua_rawseti(L, -2, 0); |
| 3177 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3178 | hsmp->s = txn->s; |
| 3179 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3180 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3181 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3182 | |
| 3183 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3184 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3185 | lua_setmetatable(L, -2); |
| 3186 | |
| 3187 | return 1; |
| 3188 | } |
| 3189 | |
| 3190 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3191 | * It uses closure argument to store the associated sample-fetch. It |
| 3192 | * returns only one argument or throws an error. An error is thrown |
| 3193 | * only if an error is encountered during the argument parsing. If |
| 3194 | * the "sample-fetch" function fails, nil is returned. |
| 3195 | */ |
| 3196 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3197 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3198 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3199 | struct sample_fetch *f; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3200 | struct arg args[ARGM_NBARGS + 1]; |
| 3201 | int i; |
| 3202 | struct sample smp; |
| 3203 | |
| 3204 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3205 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3206 | |
| 3207 | /* Get traditionnal arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3208 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3209 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3210 | /* Check execution authorization. */ |
| 3211 | if (f->use & SMP_USE_HTTP_ANY && |
| 3212 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3213 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3214 | "is not available in Lua services", f->kw); |
| 3215 | WILL_LJMP(lua_error(L)); |
| 3216 | } |
| 3217 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3218 | /* Get extra arguments. */ |
| 3219 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3220 | if (i >= ARGM_NBARGS) |
| 3221 | break; |
| 3222 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3223 | } |
| 3224 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 3225 | args[i].data.str.str = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3226 | |
| 3227 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3228 | 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] | 3229 | |
| 3230 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3231 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3232 | lua_pushfstring(L, "error in arguments"); |
| 3233 | WILL_LJMP(lua_error(L)); |
| 3234 | } |
| 3235 | |
| 3236 | /* Initialise the sample. */ |
| 3237 | memset(&smp, 0, sizeof(smp)); |
| 3238 | |
| 3239 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3240 | 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] | 3241 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3242 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3243 | lua_pushstring(L, ""); |
| 3244 | else |
| 3245 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3246 | return 1; |
| 3247 | } |
| 3248 | |
| 3249 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3250 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3251 | hlua_smp2lua_str(L, &smp); |
| 3252 | else |
| 3253 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3254 | return 1; |
| 3255 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3256 | |
| 3257 | /* |
| 3258 | * |
| 3259 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3260 | * Class Converters |
| 3261 | * |
| 3262 | * |
| 3263 | */ |
| 3264 | |
| 3265 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3266 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3267 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3268 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3269 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3270 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3271 | } |
| 3272 | |
| 3273 | /* This function creates and push in the stack a Converters object |
| 3274 | * according with a current TXN. |
| 3275 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3276 | 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] | 3277 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3278 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3279 | |
| 3280 | /* Check stack size. */ |
| 3281 | if (!lua_checkstack(L, 3)) |
| 3282 | return 0; |
| 3283 | |
| 3284 | /* Create the object: obj[0] = userdata. |
| 3285 | * Note that the base of the Converters object is the |
| 3286 | * same than the TXN object. |
| 3287 | */ |
| 3288 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3289 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3290 | lua_rawseti(L, -2, 0); |
| 3291 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3292 | hsmp->s = txn->s; |
| 3293 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3294 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3295 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3296 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3297 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3298 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3299 | lua_setmetatable(L, -2); |
| 3300 | |
| 3301 | return 1; |
| 3302 | } |
| 3303 | |
| 3304 | /* This function is an LUA binding. It is called with each converter. |
| 3305 | * It uses closure argument to store the associated converter. It |
| 3306 | * returns only one argument or throws an error. An error is thrown |
| 3307 | * only if an error is encountered during the argument parsing. If |
| 3308 | * the converter function function fails, nil is returned. |
| 3309 | */ |
| 3310 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3311 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3312 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3313 | struct sample_conv *conv; |
| 3314 | struct arg args[ARGM_NBARGS + 1]; |
| 3315 | int i; |
| 3316 | struct sample smp; |
| 3317 | |
| 3318 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3319 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3320 | |
| 3321 | /* Get traditionnal arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3322 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3323 | |
| 3324 | /* Get extra arguments. */ |
| 3325 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3326 | if (i >= ARGM_NBARGS) |
| 3327 | break; |
| 3328 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3329 | } |
| 3330 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 3331 | args[i].data.str.str = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3332 | |
| 3333 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3334 | 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] | 3335 | |
| 3336 | /* Run the special args checker. */ |
| 3337 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3338 | hlua_pusherror(L, "error in arguments"); |
| 3339 | WILL_LJMP(lua_error(L)); |
| 3340 | } |
| 3341 | |
| 3342 | /* Initialise the sample. */ |
| 3343 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3344 | hlua_pusherror(L, "error in the input argument"); |
| 3345 | WILL_LJMP(lua_error(L)); |
| 3346 | } |
| 3347 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3348 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3349 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3350 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3351 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3352 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3353 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3354 | WILL_LJMP(lua_error(L)); |
| 3355 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3356 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3357 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3358 | hlua_pusherror(L, "error during the input argument casting"); |
| 3359 | WILL_LJMP(lua_error(L)); |
| 3360 | } |
| 3361 | |
| 3362 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3363 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3364 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3365 | lua_pushstring(L, ""); |
| 3366 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3367 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3368 | return 1; |
| 3369 | } |
| 3370 | |
| 3371 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3372 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3373 | hlua_smp2lua_str(L, &smp); |
| 3374 | else |
| 3375 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3376 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3377 | } |
| 3378 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3379 | /* |
| 3380 | * |
| 3381 | * |
| 3382 | * Class AppletTCP |
| 3383 | * |
| 3384 | * |
| 3385 | */ |
| 3386 | |
| 3387 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3388 | * a class stream, otherwise it throws an error. |
| 3389 | */ |
| 3390 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3391 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3392 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3393 | } |
| 3394 | |
| 3395 | /* This function creates and push in the stack an Applet object |
| 3396 | * according with a current TXN. |
| 3397 | */ |
| 3398 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3399 | { |
| 3400 | struct hlua_appctx *appctx; |
| 3401 | struct stream_interface *si = ctx->owner; |
| 3402 | struct stream *s = si_strm(si); |
| 3403 | struct proxy *p = s->be; |
| 3404 | |
| 3405 | /* Check stack size. */ |
| 3406 | if (!lua_checkstack(L, 3)) |
| 3407 | return 0; |
| 3408 | |
| 3409 | /* Create the object: obj[0] = userdata. |
| 3410 | * Note that the base of the Converters object is the |
| 3411 | * same than the TXN object. |
| 3412 | */ |
| 3413 | lua_newtable(L); |
| 3414 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3415 | lua_rawseti(L, -2, 0); |
| 3416 | appctx->appctx = ctx; |
| 3417 | appctx->htxn.s = s; |
| 3418 | appctx->htxn.p = p; |
| 3419 | |
| 3420 | /* Create the "f" field that contains a list of fetches. */ |
| 3421 | lua_pushstring(L, "f"); |
| 3422 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3423 | return 0; |
| 3424 | lua_settable(L, -3); |
| 3425 | |
| 3426 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3427 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3428 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3429 | return 0; |
| 3430 | lua_settable(L, -3); |
| 3431 | |
| 3432 | /* Create the "c" field that contains a list of converters. */ |
| 3433 | lua_pushstring(L, "c"); |
| 3434 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3435 | return 0; |
| 3436 | lua_settable(L, -3); |
| 3437 | |
| 3438 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3439 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3440 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3441 | return 0; |
| 3442 | lua_settable(L, -3); |
| 3443 | |
| 3444 | /* Pop a class stream metatable and affect it to the table. */ |
| 3445 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3446 | lua_setmetatable(L, -2); |
| 3447 | |
| 3448 | return 1; |
| 3449 | } |
| 3450 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3451 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3452 | { |
| 3453 | struct hlua_appctx *appctx; |
| 3454 | struct stream *s; |
| 3455 | const char *name; |
| 3456 | size_t len; |
| 3457 | struct sample smp; |
| 3458 | |
| 3459 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3460 | |
| 3461 | /* It is useles to retrieve the stream, but this function |
| 3462 | * runs only in a stream context. |
| 3463 | */ |
| 3464 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3465 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3466 | s = appctx->htxn.s; |
| 3467 | |
| 3468 | /* Converts the third argument in a sample. */ |
| 3469 | hlua_lua2smp(L, 3, &smp); |
| 3470 | |
| 3471 | /* Store the sample in a variable. */ |
| 3472 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3473 | vars_set_by_name(name, len, &smp); |
| 3474 | return 0; |
| 3475 | } |
| 3476 | |
| 3477 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3478 | { |
| 3479 | struct hlua_appctx *appctx; |
| 3480 | struct stream *s; |
| 3481 | const char *name; |
| 3482 | size_t len; |
| 3483 | struct sample smp; |
| 3484 | |
| 3485 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3486 | |
| 3487 | /* It is useles to retrieve the stream, but this function |
| 3488 | * runs only in a stream context. |
| 3489 | */ |
| 3490 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3491 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3492 | s = appctx->htxn.s; |
| 3493 | |
| 3494 | /* Unset the variable. */ |
| 3495 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3496 | vars_unset_by_name(name, len, &smp); |
| 3497 | return 0; |
| 3498 | } |
| 3499 | |
| 3500 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3501 | { |
| 3502 | struct hlua_appctx *appctx; |
| 3503 | struct stream *s; |
| 3504 | const char *name; |
| 3505 | size_t len; |
| 3506 | struct sample smp; |
| 3507 | |
| 3508 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3509 | |
| 3510 | /* It is useles to retrieve the stream, but this function |
| 3511 | * runs only in a stream context. |
| 3512 | */ |
| 3513 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3514 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3515 | s = appctx->htxn.s; |
| 3516 | |
| 3517 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3518 | if (!vars_get_by_name(name, len, &smp)) { |
| 3519 | lua_pushnil(L); |
| 3520 | return 1; |
| 3521 | } |
| 3522 | |
| 3523 | return hlua_smp2lua(L, &smp); |
| 3524 | } |
| 3525 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3526 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3527 | { |
| 3528 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3529 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3530 | struct hlua *hlua; |
| 3531 | |
| 3532 | /* 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] | 3533 | if (!s->hlua) |
| 3534 | return 0; |
| 3535 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3536 | |
| 3537 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3538 | |
| 3539 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3540 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3541 | |
| 3542 | /* Get and store new value. */ |
| 3543 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3544 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3545 | |
| 3546 | return 0; |
| 3547 | } |
| 3548 | |
| 3549 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3550 | { |
| 3551 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3552 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3553 | struct hlua *hlua; |
| 3554 | |
| 3555 | /* 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] | 3556 | if (!s->hlua) { |
| 3557 | lua_pushnil(L); |
| 3558 | return 1; |
| 3559 | } |
| 3560 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3561 | |
| 3562 | /* Push configuration index in the stack. */ |
| 3563 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3564 | |
| 3565 | return 1; |
| 3566 | } |
| 3567 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3568 | /* If expected data not yet available, it returns a yield. This function |
| 3569 | * consumes the data in the buffer. It returns a string containing the |
| 3570 | * data. This string can be empty. |
| 3571 | */ |
| 3572 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3573 | { |
| 3574 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3575 | struct stream_interface *si = appctx->appctx->owner; |
| 3576 | int ret; |
| 3577 | char *blk1; |
| 3578 | int len1; |
| 3579 | char *blk2; |
| 3580 | int len2; |
| 3581 | |
| 3582 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3583 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3584 | |
| 3585 | /* Data not yet avalaible. return yield. */ |
| 3586 | if (ret == 0) { |
| 3587 | si_applet_cant_get(si); |
| 3588 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
| 3589 | } |
| 3590 | |
| 3591 | /* End of data: commit the total strings and return. */ |
| 3592 | if (ret < 0) { |
| 3593 | luaL_pushresult(&appctx->b); |
| 3594 | return 1; |
| 3595 | } |
| 3596 | |
| 3597 | /* Ensure that the block 2 length is usable. */ |
| 3598 | if (ret == 1) |
| 3599 | len2 = 0; |
| 3600 | |
| 3601 | /* dont check the max length read and dont check. */ |
| 3602 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3603 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3604 | |
| 3605 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3606 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3607 | luaL_pushresult(&appctx->b); |
| 3608 | return 1; |
| 3609 | } |
| 3610 | |
| 3611 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3612 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3613 | { |
| 3614 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3615 | |
| 3616 | /* Initialise the string catenation. */ |
| 3617 | luaL_buffinit(L, &appctx->b); |
| 3618 | |
| 3619 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3620 | } |
| 3621 | |
| 3622 | /* If expected data not yet available, it returns a yield. This function |
| 3623 | * consumes the data in the buffer. It returns a string containing the |
| 3624 | * data. This string can be empty. |
| 3625 | */ |
| 3626 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3627 | { |
| 3628 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3629 | struct stream_interface *si = appctx->appctx->owner; |
| 3630 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3631 | int ret; |
| 3632 | char *blk1; |
| 3633 | int len1; |
| 3634 | char *blk2; |
| 3635 | int len2; |
| 3636 | |
| 3637 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3638 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3639 | |
| 3640 | /* Data not yet avalaible. return yield. */ |
| 3641 | if (ret == 0) { |
| 3642 | si_applet_cant_get(si); |
| 3643 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3644 | } |
| 3645 | |
| 3646 | /* End of data: commit the total strings and return. */ |
| 3647 | if (ret < 0) { |
| 3648 | luaL_pushresult(&appctx->b); |
| 3649 | return 1; |
| 3650 | } |
| 3651 | |
| 3652 | /* Ensure that the block 2 length is usable. */ |
| 3653 | if (ret == 1) |
| 3654 | len2 = 0; |
| 3655 | |
| 3656 | if (len == -1) { |
| 3657 | |
| 3658 | /* If len == -1, catenate all the data avalaile and |
| 3659 | * yield because we want to get all the data until |
| 3660 | * the end of data stream. |
| 3661 | */ |
| 3662 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3663 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3664 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3665 | si_applet_cant_get(si); |
| 3666 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3667 | |
| 3668 | } else { |
| 3669 | |
| 3670 | /* Copy the fisrt block caping to the length required. */ |
| 3671 | if (len1 > len) |
| 3672 | len1 = len; |
| 3673 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3674 | len -= len1; |
| 3675 | |
| 3676 | /* Copy the second block. */ |
| 3677 | if (len2 > len) |
| 3678 | len2 = len; |
| 3679 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3680 | len -= len2; |
| 3681 | |
| 3682 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3683 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3684 | |
| 3685 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3686 | if (len > 0) { |
| 3687 | lua_pushinteger(L, len); |
| 3688 | lua_replace(L, 2); |
| 3689 | si_applet_cant_get(si); |
| 3690 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3691 | } |
| 3692 | |
| 3693 | /* return the result. */ |
| 3694 | luaL_pushresult(&appctx->b); |
| 3695 | return 1; |
| 3696 | } |
| 3697 | |
| 3698 | /* we never executes this */ |
| 3699 | hlua_pusherror(L, "Lua: internal error"); |
| 3700 | WILL_LJMP(lua_error(L)); |
| 3701 | return 0; |
| 3702 | } |
| 3703 | |
| 3704 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3705 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3706 | { |
| 3707 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3708 | int len = -1; |
| 3709 | |
| 3710 | if (lua_gettop(L) > 2) |
| 3711 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3712 | if (lua_gettop(L) >= 2) { |
| 3713 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3714 | lua_pop(L, 1); |
| 3715 | } |
| 3716 | |
| 3717 | /* Confirm or set the required length */ |
| 3718 | lua_pushinteger(L, len); |
| 3719 | |
| 3720 | /* Initialise the string catenation. */ |
| 3721 | luaL_buffinit(L, &appctx->b); |
| 3722 | |
| 3723 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3724 | } |
| 3725 | |
| 3726 | /* Append data in the output side of the buffer. This data is immediatly |
| 3727 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3728 | * cannot contains the data, the function yield. The function returns -1 |
| 3729 | * if the channel is closed. |
| 3730 | */ |
| 3731 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3732 | { |
| 3733 | size_t len; |
| 3734 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3735 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3736 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3737 | struct stream_interface *si = appctx->appctx->owner; |
| 3738 | struct channel *chn = si_ic(si); |
| 3739 | int max; |
| 3740 | |
| 3741 | /* Get the max amount of data which can write as input in the channel. */ |
| 3742 | max = channel_recv_max(chn); |
| 3743 | if (max > (len - l)) |
| 3744 | max = len - l; |
| 3745 | |
| 3746 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3747 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3748 | |
| 3749 | /* update counters. */ |
| 3750 | l += max; |
| 3751 | lua_pop(L, 1); |
| 3752 | lua_pushinteger(L, l); |
| 3753 | |
| 3754 | /* If some data is not send, declares the situation to the |
| 3755 | * applet, and returns a yield. |
| 3756 | */ |
| 3757 | if (l < len) { |
| 3758 | si_applet_cant_put(si); |
| 3759 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
| 3760 | } |
| 3761 | |
| 3762 | return 1; |
| 3763 | } |
| 3764 | |
| 3765 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3766 | * yield the LUA process, and resume it without checking the |
| 3767 | * input arguments. |
| 3768 | */ |
| 3769 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3770 | { |
| 3771 | MAY_LJMP(check_args(L, 2, "send")); |
| 3772 | lua_pushinteger(L, 0); |
| 3773 | |
| 3774 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3775 | } |
| 3776 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3777 | /* |
| 3778 | * |
| 3779 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3780 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3781 | * |
| 3782 | * |
| 3783 | */ |
| 3784 | |
| 3785 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3786 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3787 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3788 | __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] | 3789 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3790 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3791 | } |
| 3792 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3793 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3794 | * according with a current TXN. |
| 3795 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3796 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3797 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3798 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3799 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3800 | struct stream_interface *si = ctx->owner; |
| 3801 | struct stream *s = si_strm(si); |
| 3802 | struct proxy *px = s->be; |
| 3803 | struct http_txn *txn = s->txn; |
| 3804 | const char *path; |
| 3805 | const char *end; |
| 3806 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3807 | |
| 3808 | /* Check stack size. */ |
| 3809 | if (!lua_checkstack(L, 3)) |
| 3810 | return 0; |
| 3811 | |
| 3812 | /* Create the object: obj[0] = userdata. |
| 3813 | * Note that the base of the Converters object is the |
| 3814 | * same than the TXN object. |
| 3815 | */ |
| 3816 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3817 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3818 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3819 | appctx->appctx = ctx; |
| 3820 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3821 | 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] | 3822 | appctx->htxn.s = s; |
| 3823 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3824 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3825 | /* Create the "f" field that contains a list of fetches. */ |
| 3826 | lua_pushstring(L, "f"); |
| 3827 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3828 | return 0; |
| 3829 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3830 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3831 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3832 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3833 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3834 | return 0; |
| 3835 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3836 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3837 | /* Create the "c" field that contains a list of converters. */ |
| 3838 | lua_pushstring(L, "c"); |
| 3839 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3840 | return 0; |
| 3841 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3842 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3843 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3844 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3845 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3846 | return 0; |
| 3847 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3848 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3849 | /* Stores the request method. */ |
| 3850 | lua_pushstring(L, "method"); |
| 3851 | lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l); |
| 3852 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3853 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3854 | /* Stores the http version. */ |
| 3855 | lua_pushstring(L, "version"); |
| 3856 | lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
| 3857 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3858 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3859 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3860 | * the array on the top of the stack. |
| 3861 | */ |
| 3862 | lua_pushstring(L, "headers"); |
| 3863 | htxn.s = s; |
| 3864 | htxn.p = px; |
| 3865 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3866 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3867 | return 0; |
| 3868 | lua_settable(L, -3); |
| 3869 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3870 | /* Get path and qs */ |
| 3871 | path = http_get_path(txn); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3872 | if (path) { |
| 3873 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 3874 | p = path; |
| 3875 | while (p < end && *p != '?') |
| 3876 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3877 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3878 | /* Stores the request path. */ |
| 3879 | lua_pushstring(L, "path"); |
| 3880 | lua_pushlstring(L, path, p - path); |
| 3881 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3882 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3883 | /* Stores the query string. */ |
| 3884 | lua_pushstring(L, "qs"); |
| 3885 | if (*p == '?') |
| 3886 | p++; |
| 3887 | lua_pushlstring(L, p, end - p); |
| 3888 | lua_settable(L, -3); |
| 3889 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3890 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3891 | /* Stores the request path. */ |
| 3892 | lua_pushstring(L, "length"); |
| 3893 | lua_pushinteger(L, txn->req.body_len); |
| 3894 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3895 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3896 | /* Create an array of HTTP request headers. */ |
| 3897 | lua_pushstring(L, "headers"); |
| 3898 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3899 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3900 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3901 | /* Create an empty array of HTTP request headers. */ |
| 3902 | lua_pushstring(L, "response"); |
| 3903 | lua_newtable(L); |
| 3904 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3905 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3906 | /* Pop a class stream metatable and affect it to the table. */ |
| 3907 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3908 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3909 | |
| 3910 | return 1; |
| 3911 | } |
| 3912 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3913 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3914 | { |
| 3915 | struct hlua_appctx *appctx; |
| 3916 | struct stream *s; |
| 3917 | const char *name; |
| 3918 | size_t len; |
| 3919 | struct sample smp; |
| 3920 | |
| 3921 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3922 | |
| 3923 | /* It is useles to retrieve the stream, but this function |
| 3924 | * runs only in a stream context. |
| 3925 | */ |
| 3926 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3927 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3928 | s = appctx->htxn.s; |
| 3929 | |
| 3930 | /* Converts the third argument in a sample. */ |
| 3931 | hlua_lua2smp(L, 3, &smp); |
| 3932 | |
| 3933 | /* Store the sample in a variable. */ |
| 3934 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3935 | vars_set_by_name(name, len, &smp); |
| 3936 | return 0; |
| 3937 | } |
| 3938 | |
| 3939 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 3940 | { |
| 3941 | struct hlua_appctx *appctx; |
| 3942 | struct stream *s; |
| 3943 | const char *name; |
| 3944 | size_t len; |
| 3945 | struct sample smp; |
| 3946 | |
| 3947 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3948 | |
| 3949 | /* It is useles to retrieve the stream, but this function |
| 3950 | * runs only in a stream context. |
| 3951 | */ |
| 3952 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3953 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3954 | s = appctx->htxn.s; |
| 3955 | |
| 3956 | /* Unset the variable. */ |
| 3957 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3958 | vars_unset_by_name(name, len, &smp); |
| 3959 | return 0; |
| 3960 | } |
| 3961 | |
| 3962 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 3963 | { |
| 3964 | struct hlua_appctx *appctx; |
| 3965 | struct stream *s; |
| 3966 | const char *name; |
| 3967 | size_t len; |
| 3968 | struct sample smp; |
| 3969 | |
| 3970 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3971 | |
| 3972 | /* It is useles to retrieve the stream, but this function |
| 3973 | * runs only in a stream context. |
| 3974 | */ |
| 3975 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3976 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3977 | s = appctx->htxn.s; |
| 3978 | |
| 3979 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3980 | if (!vars_get_by_name(name, len, &smp)) { |
| 3981 | lua_pushnil(L); |
| 3982 | return 1; |
| 3983 | } |
| 3984 | |
| 3985 | return hlua_smp2lua(L, &smp); |
| 3986 | } |
| 3987 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3988 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 3989 | { |
| 3990 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3991 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3992 | struct hlua *hlua; |
| 3993 | |
| 3994 | /* 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] | 3995 | if (!s->hlua) |
| 3996 | return 0; |
| 3997 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3998 | |
| 3999 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4000 | |
| 4001 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4002 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4003 | |
| 4004 | /* Get and store new value. */ |
| 4005 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4006 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4007 | |
| 4008 | return 0; |
| 4009 | } |
| 4010 | |
| 4011 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4012 | { |
| 4013 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4014 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4015 | struct hlua *hlua; |
| 4016 | |
| 4017 | /* 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] | 4018 | if (!s->hlua) { |
| 4019 | lua_pushnil(L); |
| 4020 | return 1; |
| 4021 | } |
| 4022 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4023 | |
| 4024 | /* Push configuration index in the stack. */ |
| 4025 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4026 | |
| 4027 | return 1; |
| 4028 | } |
| 4029 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4030 | /* If expected data not yet available, it returns a yield. This function |
| 4031 | * consumes the data in the buffer. It returns a string containing the |
| 4032 | * data. This string can be empty. |
| 4033 | */ |
| 4034 | __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] | 4035 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4036 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4037 | struct stream_interface *si = appctx->appctx->owner; |
| 4038 | struct channel *chn = si_ic(si); |
| 4039 | int ret; |
| 4040 | char *blk1; |
| 4041 | int len1; |
| 4042 | char *blk2; |
| 4043 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4044 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4045 | /* Maybe we cant send a 100-continue ? */ |
| 4046 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4047 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4048 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4049 | * big for the buffers. We cant send anything. So, we ignoring |
| 4050 | * the error, considers that the 100-continue is sent, and try |
| 4051 | * to receive. |
| 4052 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4053 | */ |
| 4054 | if (ret == -1) { |
| 4055 | si_applet_cant_put(si); |
| 4056 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 4057 | } |
| 4058 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4059 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4060 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4061 | /* Check for the end of the data. */ |
| 4062 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4063 | luaL_pushresult(&appctx->b); |
| 4064 | return 1; |
| 4065 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4066 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4067 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4068 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4069 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4070 | /* Data not yet avalaible. return yield. */ |
| 4071 | if (ret == 0) { |
| 4072 | si_applet_cant_get(si); |
| 4073 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 4074 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4075 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4076 | /* End of data: commit the total strings and return. */ |
| 4077 | if (ret < 0) { |
| 4078 | luaL_pushresult(&appctx->b); |
| 4079 | return 1; |
| 4080 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4081 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4082 | /* Ensure that the block 2 length is usable. */ |
| 4083 | if (ret == 1) |
| 4084 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4085 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4086 | /* Copy the fisrt block caping to the length required. */ |
| 4087 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4088 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4089 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4090 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4091 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4092 | /* Copy the second block. */ |
| 4093 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4094 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4095 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4096 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4097 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4098 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4099 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4100 | luaL_pushresult(&appctx->b); |
| 4101 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4102 | } |
| 4103 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4104 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 4105 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4106 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4107 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4108 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4109 | /* Initialise the string catenation. */ |
| 4110 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4111 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4112 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4113 | } |
| 4114 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4115 | /* If expected data not yet available, it returns a yield. This function |
| 4116 | * consumes the data in the buffer. It returns a string containing the |
| 4117 | * data. This string can be empty. |
| 4118 | */ |
| 4119 | __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] | 4120 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4121 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4122 | struct stream_interface *si = appctx->appctx->owner; |
| 4123 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4124 | struct channel *chn = si_ic(si); |
| 4125 | int ret; |
| 4126 | char *blk1; |
| 4127 | int len1; |
| 4128 | char *blk2; |
| 4129 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4130 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4131 | /* Maybe we cant send a 100-continue ? */ |
| 4132 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4133 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4134 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4135 | * big for the buffers. We cant send anything. So, we ignoring |
| 4136 | * the error, considers that the 100-continue is sent, and try |
| 4137 | * to receive. |
| 4138 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4139 | */ |
| 4140 | if (ret == -1) { |
| 4141 | si_applet_cant_put(si); |
| 4142 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4143 | } |
| 4144 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 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 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4148 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4149 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4150 | /* Data not yet avalaible. return yield. */ |
| 4151 | if (ret == 0) { |
| 4152 | si_applet_cant_get(si); |
| 4153 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4154 | } |
| 4155 | |
| 4156 | /* End of data: commit the total strings and return. */ |
| 4157 | if (ret < 0) { |
| 4158 | luaL_pushresult(&appctx->b); |
| 4159 | return 1; |
| 4160 | } |
| 4161 | |
| 4162 | /* Ensure that the block 2 length is usable. */ |
| 4163 | if (ret == 1) |
| 4164 | len2 = 0; |
| 4165 | |
| 4166 | /* Copy the fisrt block caping to the length required. */ |
| 4167 | if (len1 > len) |
| 4168 | len1 = len; |
| 4169 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4170 | len -= len1; |
| 4171 | |
| 4172 | /* Copy the second block. */ |
| 4173 | if (len2 > len) |
| 4174 | len2 = len; |
| 4175 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4176 | len -= len2; |
| 4177 | |
| 4178 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4179 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4180 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4181 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4182 | |
| 4183 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 4184 | if (len > 0) { |
| 4185 | lua_pushinteger(L, len); |
| 4186 | lua_replace(L, 2); |
| 4187 | si_applet_cant_get(si); |
| 4188 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4189 | } |
| 4190 | |
| 4191 | /* return the result. */ |
| 4192 | luaL_pushresult(&appctx->b); |
| 4193 | return 1; |
| 4194 | } |
| 4195 | |
| 4196 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 4197 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4198 | { |
| 4199 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4200 | int len = -1; |
| 4201 | |
| 4202 | /* Check arguments. */ |
| 4203 | if (lua_gettop(L) > 2) |
| 4204 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4205 | if (lua_gettop(L) >= 2) { |
| 4206 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4207 | lua_pop(L, 1); |
| 4208 | } |
| 4209 | |
| 4210 | /* Check the required length */ |
| 4211 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4212 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4213 | lua_pushinteger(L, len); |
| 4214 | |
| 4215 | /* Initialise the string catenation. */ |
| 4216 | luaL_buffinit(L, &appctx->b); |
| 4217 | |
| 4218 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4219 | } |
| 4220 | |
| 4221 | /* Append data in the output side of the buffer. This data is immediatly |
| 4222 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 4223 | * cannot contains the data, the function yield. The function returns -1 |
| 4224 | * if the channel is closed. |
| 4225 | */ |
| 4226 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4227 | { |
| 4228 | size_t len; |
| 4229 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4230 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4231 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4232 | struct stream_interface *si = appctx->appctx->owner; |
| 4233 | struct channel *chn = si_ic(si); |
| 4234 | int max; |
| 4235 | |
| 4236 | /* Get the max amount of data which can write as input in the channel. */ |
| 4237 | max = channel_recv_max(chn); |
| 4238 | if (max > (len - l)) |
| 4239 | max = len - l; |
| 4240 | |
| 4241 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4242 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4243 | |
| 4244 | /* update counters. */ |
| 4245 | l += max; |
| 4246 | lua_pop(L, 1); |
| 4247 | lua_pushinteger(L, l); |
| 4248 | |
| 4249 | /* If some data is not send, declares the situation to the |
| 4250 | * applet, and returns a yield. |
| 4251 | */ |
| 4252 | if (l < len) { |
| 4253 | si_applet_cant_put(si); |
| 4254 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
| 4255 | } |
| 4256 | |
| 4257 | return 1; |
| 4258 | } |
| 4259 | |
| 4260 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4261 | * yield the LUA process, and resume it without checking the |
| 4262 | * input arguments. |
| 4263 | */ |
| 4264 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4265 | { |
| 4266 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4267 | size_t len; |
| 4268 | char hex[10]; |
| 4269 | |
| 4270 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4271 | |
| 4272 | /* If transfer encoding chunked is selected, we surround the data |
| 4273 | * by chunk data. |
| 4274 | */ |
| 4275 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4276 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4277 | lua_pushfstring(L, "%s\r\n", hex); |
| 4278 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4279 | lua_pushstring(L, "\r\n"); |
| 4280 | lua_concat(L, 3); |
| 4281 | } |
| 4282 | |
| 4283 | /* This interger is used for followinf the amount of data sent. */ |
| 4284 | lua_pushinteger(L, 0); |
| 4285 | |
| 4286 | /* We want to send some data. Headers must be sent. */ |
| 4287 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4288 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4289 | WILL_LJMP(lua_error(L)); |
| 4290 | } |
| 4291 | |
| 4292 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4293 | } |
| 4294 | |
| 4295 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4296 | { |
| 4297 | const char *name; |
| 4298 | int ret; |
| 4299 | |
| 4300 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4301 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4302 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4303 | |
| 4304 | /* Push in the stack the "response" entry. */ |
| 4305 | ret = lua_getfield(L, 1, "response"); |
| 4306 | if (ret != LUA_TTABLE) { |
| 4307 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4308 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4309 | WILL_LJMP(lua_error(L)); |
| 4310 | } |
| 4311 | |
| 4312 | /* check if the header is already registered if it is not |
| 4313 | * the case, register it. |
| 4314 | */ |
| 4315 | ret = lua_getfield(L, -1, name); |
| 4316 | if (ret == LUA_TNIL) { |
| 4317 | |
| 4318 | /* Entry not found. */ |
| 4319 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4320 | |
| 4321 | /* Insert the new header name in the array in the top of the stack. |
| 4322 | * It left the new array in the top of the stack. |
| 4323 | */ |
| 4324 | lua_newtable(L); |
| 4325 | lua_pushvalue(L, 2); |
| 4326 | lua_pushvalue(L, -2); |
| 4327 | lua_settable(L, -4); |
| 4328 | |
| 4329 | } else if (ret != LUA_TTABLE) { |
| 4330 | |
| 4331 | /* corruption error. */ |
| 4332 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4333 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4334 | WILL_LJMP(lua_error(L)); |
| 4335 | } |
| 4336 | |
| 4337 | /* Now the top od thestack is an array of values. We push |
| 4338 | * the header value as new entry. |
| 4339 | */ |
| 4340 | lua_pushvalue(L, 3); |
| 4341 | ret = lua_rawlen(L, -2); |
| 4342 | lua_rawseti(L, -2, ret + 1); |
| 4343 | lua_pushboolean(L, 1); |
| 4344 | return 1; |
| 4345 | } |
| 4346 | |
| 4347 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4348 | { |
| 4349 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4350 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4351 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4352 | |
| 4353 | if (status < 100 || status > 599) { |
| 4354 | lua_pushboolean(L, 0); |
| 4355 | return 1; |
| 4356 | } |
| 4357 | |
| 4358 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4359 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4360 | lua_pushboolean(L, 1); |
| 4361 | return 1; |
| 4362 | } |
| 4363 | |
| 4364 | /* We will build the status line and the headers of the HTTP response. |
| 4365 | * We will try send at once if its not possible, we give back the hand |
| 4366 | * waiting for more room. |
| 4367 | */ |
| 4368 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4369 | { |
| 4370 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4371 | struct stream_interface *si = appctx->appctx->owner; |
| 4372 | struct channel *chn = si_ic(si); |
| 4373 | int ret; |
| 4374 | size_t len; |
| 4375 | const char *msg; |
| 4376 | |
| 4377 | /* Get the message as the first argument on the stack. */ |
| 4378 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4379 | |
| 4380 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4381 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4382 | |
| 4383 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4384 | * big for the buffers. |
| 4385 | */ |
| 4386 | if (ret == -2 || ret == -3) { |
| 4387 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4388 | WILL_LJMP(lua_error(L)); |
| 4389 | } |
| 4390 | |
| 4391 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4392 | if (ret == -1) { |
| 4393 | si_applet_cant_put(si); |
| 4394 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
| 4395 | } |
| 4396 | |
| 4397 | /* Headers sent, set the flag. */ |
| 4398 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4399 | return 0; |
| 4400 | } |
| 4401 | |
| 4402 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4403 | { |
| 4404 | struct chunk *tmp = get_trash_chunk(); |
| 4405 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4406 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4407 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4408 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4409 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4410 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4411 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4412 | int hdr_chunked = 0; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4413 | const char *reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4414 | |
| 4415 | if (reason == NULL) |
| 4416 | reason = get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4417 | |
| 4418 | /* Use the same http version than the request. */ |
| 4419 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4420 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4421 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4422 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4423 | |
| 4424 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4425 | lua_pushvalue(L, 0); |
| 4426 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4427 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4428 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4429 | WILL_LJMP(lua_error(L)); |
| 4430 | } |
| 4431 | |
| 4432 | /* Browse the list of headers. */ |
| 4433 | lua_pushnil(L); |
| 4434 | while(lua_next(L, -2) != 0) { |
| 4435 | |
| 4436 | /* We expect a string as -2. */ |
| 4437 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4438 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4439 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4440 | lua_typename(L, lua_type(L, -2))); |
| 4441 | WILL_LJMP(lua_error(L)); |
| 4442 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4443 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4444 | |
| 4445 | /* We expect an array as -1. */ |
| 4446 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4447 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4448 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4449 | name, |
| 4450 | lua_typename(L, lua_type(L, -1))); |
| 4451 | WILL_LJMP(lua_error(L)); |
| 4452 | } |
| 4453 | |
| 4454 | /* Browse the table who is on the top of the stack. */ |
| 4455 | lua_pushnil(L); |
| 4456 | while(lua_next(L, -2) != 0) { |
| 4457 | |
| 4458 | /* We expect a number as -2. */ |
| 4459 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4460 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4461 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4462 | name, |
| 4463 | lua_typename(L, lua_type(L, -2))); |
| 4464 | WILL_LJMP(lua_error(L)); |
| 4465 | } |
| 4466 | id = lua_tointeger(L, -2); |
| 4467 | |
| 4468 | /* We expect a string as -2. */ |
| 4469 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4470 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4471 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4472 | name, id, |
| 4473 | lua_typename(L, lua_type(L, -1))); |
| 4474 | WILL_LJMP(lua_error(L)); |
| 4475 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4476 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4477 | |
| 4478 | /* Catenate a new header. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4479 | if (tmp->len + name_len + 2 + value_len + 2 < tmp->size) { |
| 4480 | memcpy(tmp->str + tmp->len, name, name_len); |
| 4481 | tmp->len += name_len; |
| 4482 | tmp->str[tmp->len++] = ':'; |
| 4483 | tmp->str[tmp->len++] = ' '; |
| 4484 | |
| 4485 | memcpy(tmp->str + tmp->len, value, value_len); |
| 4486 | tmp->len += value_len; |
| 4487 | tmp->str[tmp->len++] = '\r'; |
| 4488 | tmp->str[tmp->len++] = '\n'; |
| 4489 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4490 | |
| 4491 | /* Protocol checks. */ |
| 4492 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4493 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4494 | * is done without control. If it contains a bad value, |
| 4495 | * the content-length remains negative so that we can |
| 4496 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4497 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4498 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4499 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4500 | |
| 4501 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4502 | if (name_len == 17 && value_len == 7 && |
| 4503 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4504 | strcasecmp("chunked", value) == 0) |
| 4505 | hdr_chunked = 1; |
| 4506 | |
| 4507 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4508 | lua_pop(L, 1); |
| 4509 | } |
| 4510 | |
| 4511 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4512 | lua_pop(L, 1); |
| 4513 | } |
| 4514 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4515 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4516 | * and the status code implies the presence of a message body, we must |
| 4517 | * announce a transfer encoding chunked. This is required by haproxy |
| 4518 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4519 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4520 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4521 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4522 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 4523 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4524 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4525 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4526 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4527 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4528 | } |
| 4529 | |
| 4530 | /* Finalize headers. */ |
| 4531 | chunk_appendf(tmp, "\r\n"); |
| 4532 | |
| 4533 | /* Remove the last entry and the array of headers */ |
| 4534 | lua_pop(L, 2); |
| 4535 | |
| 4536 | /* Push the headers block. */ |
| 4537 | lua_pushlstring(L, tmp->str, tmp->len); |
| 4538 | |
| 4539 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4540 | } |
| 4541 | |
| 4542 | /* |
| 4543 | * |
| 4544 | * |
| 4545 | * Class HTTP |
| 4546 | * |
| 4547 | * |
| 4548 | */ |
| 4549 | |
| 4550 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4551 | * a class stream, otherwise it throws an error. |
| 4552 | */ |
| 4553 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4554 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4555 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4556 | } |
| 4557 | |
| 4558 | /* This function creates and push in the stack a HTTP object |
| 4559 | * according with a current TXN. |
| 4560 | */ |
| 4561 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4562 | { |
| 4563 | struct hlua_txn *htxn; |
| 4564 | |
| 4565 | /* Check stack size. */ |
| 4566 | if (!lua_checkstack(L, 3)) |
| 4567 | return 0; |
| 4568 | |
| 4569 | /* Create the object: obj[0] = userdata. |
| 4570 | * Note that the base of the Converters object is the |
| 4571 | * same than the TXN object. |
| 4572 | */ |
| 4573 | lua_newtable(L); |
| 4574 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4575 | lua_rawseti(L, -2, 0); |
| 4576 | |
| 4577 | htxn->s = txn->s; |
| 4578 | htxn->p = txn->p; |
| 4579 | |
| 4580 | /* Pop a class stream metatable and affect it to the table. */ |
| 4581 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4582 | lua_setmetatable(L, -2); |
| 4583 | |
| 4584 | return 1; |
| 4585 | } |
| 4586 | |
| 4587 | /* This function creates ans returns an array of HTTP headers. |
| 4588 | * This function does not fails. It is used as wrapper with the |
| 4589 | * 2 following functions. |
| 4590 | */ |
| 4591 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4592 | { |
| 4593 | const char *cur_ptr, *cur_next, *p; |
| 4594 | int old_idx, cur_idx; |
| 4595 | struct hdr_idx_elem *cur_hdr; |
| 4596 | const char *hn, *hv; |
| 4597 | int hnl, hvl; |
| 4598 | int type; |
| 4599 | const char *in; |
| 4600 | char *out; |
| 4601 | int len; |
| 4602 | |
| 4603 | /* Create the table. */ |
| 4604 | lua_newtable(L); |
| 4605 | |
| 4606 | if (!htxn->s->txn) |
| 4607 | return 1; |
| 4608 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4609 | /* Check if a valid response is parsed */ |
| 4610 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4611 | return 1; |
| 4612 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4613 | /* Build array of headers. */ |
| 4614 | old_idx = 0; |
| 4615 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 4616 | |
| 4617 | while (1) { |
| 4618 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4619 | if (!cur_idx) |
| 4620 | break; |
| 4621 | old_idx = cur_idx; |
| 4622 | |
| 4623 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4624 | cur_ptr = cur_next; |
| 4625 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4626 | |
| 4627 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4628 | * and the next header starts at cur_next. We'll check |
| 4629 | * this header in the list as well as against the default |
| 4630 | * rule. |
| 4631 | */ |
| 4632 | |
| 4633 | /* look for ': *'. */ |
| 4634 | hn = cur_ptr; |
| 4635 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4636 | if (p >= cur_ptr+cur_hdr->len) |
| 4637 | continue; |
| 4638 | hnl = p - hn; |
| 4639 | p++; |
| 4640 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4641 | p++; |
| 4642 | if (p >= cur_ptr+cur_hdr->len) |
| 4643 | continue; |
| 4644 | hv = p; |
| 4645 | hvl = cur_ptr+cur_hdr->len-p; |
| 4646 | |
| 4647 | /* Lowercase the key. Don't check the size of trash, it have |
| 4648 | * the size of one buffer and the input data contains in one |
| 4649 | * buffer. |
| 4650 | */ |
| 4651 | out = trash.str; |
| 4652 | for (in=hn; in<hn+hnl; in++, out++) |
| 4653 | *out = tolower(*in); |
| 4654 | *out = '\0'; |
| 4655 | |
| 4656 | /* Check for existing entry: |
| 4657 | * assume that the table is on the top of the stack, and |
| 4658 | * push the key in the stack, the function lua_gettable() |
| 4659 | * perform the lookup. |
| 4660 | */ |
| 4661 | lua_pushlstring(L, trash.str, hnl); |
| 4662 | lua_gettable(L, -2); |
| 4663 | type = lua_type(L, -1); |
| 4664 | |
| 4665 | switch (type) { |
| 4666 | case LUA_TNIL: |
| 4667 | /* Table not found, create it. */ |
| 4668 | lua_pop(L, 1); /* remove the nil value. */ |
| 4669 | lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */ |
| 4670 | lua_newtable(L); /* create and push empty table. */ |
| 4671 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4672 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4673 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4674 | break; |
| 4675 | |
| 4676 | case LUA_TTABLE: |
| 4677 | /* Entry found: push the value in the table. */ |
| 4678 | len = lua_rawlen(L, -1); |
| 4679 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4680 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4681 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4682 | break; |
| 4683 | |
| 4684 | default: |
| 4685 | /* Other cases are errors. */ |
| 4686 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4687 | WILL_LJMP(lua_error(L)); |
| 4688 | } |
| 4689 | } |
| 4690 | |
| 4691 | return 1; |
| 4692 | } |
| 4693 | |
| 4694 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4695 | { |
| 4696 | struct hlua_txn *htxn; |
| 4697 | |
| 4698 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4699 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4700 | |
| 4701 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4702 | } |
| 4703 | |
| 4704 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4705 | { |
| 4706 | struct hlua_txn *htxn; |
| 4707 | |
| 4708 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4709 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4710 | |
| 4711 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4712 | } |
| 4713 | |
| 4714 | /* This function replace full header, or just a value in |
| 4715 | * the request or in the response. It is a wrapper fir the |
| 4716 | * 4 following functions. |
| 4717 | */ |
| 4718 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4719 | struct http_msg *msg, int action) |
| 4720 | { |
| 4721 | size_t name_len; |
| 4722 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4723 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4724 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4725 | struct my_regex re; |
| 4726 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4727 | /* Check if a valid response is parsed */ |
| 4728 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4729 | return 0; |
| 4730 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4731 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4732 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4733 | |
| 4734 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4735 | regex_free(&re); |
| 4736 | return 0; |
| 4737 | } |
| 4738 | |
| 4739 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4740 | { |
| 4741 | struct hlua_txn *htxn; |
| 4742 | |
| 4743 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4744 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4745 | |
| 4746 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4747 | } |
| 4748 | |
| 4749 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4750 | { |
| 4751 | struct hlua_txn *htxn; |
| 4752 | |
| 4753 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4754 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4755 | |
| 4756 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4757 | } |
| 4758 | |
| 4759 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4760 | { |
| 4761 | struct hlua_txn *htxn; |
| 4762 | |
| 4763 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4764 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4765 | |
| 4766 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4767 | } |
| 4768 | |
| 4769 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4770 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4771 | struct hlua_txn *htxn; |
| 4772 | |
| 4773 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4774 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4775 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4776 | 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] | 4777 | } |
| 4778 | |
| 4779 | /* This function deletes all the occurences of an header. |
| 4780 | * It is a wrapper for the 2 following functions. |
| 4781 | */ |
| 4782 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4783 | { |
| 4784 | size_t len; |
| 4785 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4786 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4787 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4788 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4789 | /* Check if a valid response is parsed */ |
| 4790 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4791 | return 0; |
| 4792 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4793 | ctx.idx = 0; |
| 4794 | while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) |
| 4795 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4796 | return 0; |
| 4797 | } |
| 4798 | |
| 4799 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4800 | { |
| 4801 | struct hlua_txn *htxn; |
| 4802 | |
| 4803 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4804 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4805 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4806 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4807 | } |
| 4808 | |
| 4809 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4810 | { |
| 4811 | struct hlua_txn *htxn; |
| 4812 | |
| 4813 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4814 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4815 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4816 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4817 | } |
| 4818 | |
| 4819 | /* This function adds an header. It is a wrapper used by |
| 4820 | * the 2 following functions. |
| 4821 | */ |
| 4822 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4823 | { |
| 4824 | size_t name_len; |
| 4825 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4826 | size_t value_len; |
| 4827 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4828 | char *p; |
| 4829 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4830 | /* Check if a valid message is parsed */ |
| 4831 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4832 | return 0; |
| 4833 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4834 | /* Check length. */ |
| 4835 | trash.len = value_len + name_len + 2; |
| 4836 | if (trash.len > trash.size) |
| 4837 | return 0; |
| 4838 | |
| 4839 | /* Creates the header string. */ |
| 4840 | p = trash.str; |
| 4841 | memcpy(p, name, name_len); |
| 4842 | p += name_len; |
| 4843 | *p = ':'; |
| 4844 | p++; |
| 4845 | *p = ' '; |
| 4846 | p++; |
| 4847 | memcpy(p, value, value_len); |
| 4848 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4849 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4850 | trash.str, trash.len) != 0); |
| 4851 | |
| 4852 | return 0; |
| 4853 | } |
| 4854 | |
| 4855 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4856 | { |
| 4857 | struct hlua_txn *htxn; |
| 4858 | |
| 4859 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4860 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4861 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4862 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4863 | } |
| 4864 | |
| 4865 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4866 | { |
| 4867 | struct hlua_txn *htxn; |
| 4868 | |
| 4869 | MAY_LJMP(check_args(L, 3, "res_add_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_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4873 | } |
| 4874 | |
| 4875 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4876 | { |
| 4877 | struct hlua_txn *htxn; |
| 4878 | |
| 4879 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4880 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4881 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4882 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4883 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4884 | } |
| 4885 | |
| 4886 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4887 | { |
| 4888 | struct hlua_txn *htxn; |
| 4889 | |
| 4890 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4891 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4892 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4893 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4894 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4895 | } |
| 4896 | |
| 4897 | /* This function set the method. */ |
| 4898 | static int hlua_http_req_set_meth(lua_State *L) |
| 4899 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4900 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4901 | size_t name_len; |
| 4902 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4903 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4904 | /* Check if a valid request is parsed */ |
| 4905 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4906 | lua_pushboolean(L, 0); |
| 4907 | return 1; |
| 4908 | } |
| 4909 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4910 | 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] | 4911 | return 1; |
| 4912 | } |
| 4913 | |
| 4914 | /* This function set the method. */ |
| 4915 | static int hlua_http_req_set_path(lua_State *L) |
| 4916 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4917 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4918 | size_t name_len; |
| 4919 | 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] | 4920 | |
| 4921 | /* Check if a valid request is parsed */ |
| 4922 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4923 | lua_pushboolean(L, 0); |
| 4924 | return 1; |
| 4925 | } |
| 4926 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4927 | 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] | 4928 | return 1; |
| 4929 | } |
| 4930 | |
| 4931 | /* This function set the query-string. */ |
| 4932 | static int hlua_http_req_set_query(lua_State *L) |
| 4933 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4934 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4935 | size_t name_len; |
| 4936 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4937 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4938 | /* Check if a valid request is parsed */ |
| 4939 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4940 | lua_pushboolean(L, 0); |
| 4941 | return 1; |
| 4942 | } |
| 4943 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4944 | /* Check length. */ |
| 4945 | if (name_len > trash.size - 1) { |
| 4946 | lua_pushboolean(L, 0); |
| 4947 | return 1; |
| 4948 | } |
| 4949 | |
| 4950 | /* Add the mark question as prefix. */ |
| 4951 | chunk_reset(&trash); |
| 4952 | trash.str[trash.len++] = '?'; |
| 4953 | memcpy(trash.str + trash.len, name, name_len); |
| 4954 | trash.len += name_len; |
| 4955 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4956 | lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4957 | return 1; |
| 4958 | } |
| 4959 | |
| 4960 | /* This function set the uri. */ |
| 4961 | static int hlua_http_req_set_uri(lua_State *L) |
| 4962 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4963 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4964 | size_t name_len; |
| 4965 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4966 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4967 | /* Check if a valid request is parsed */ |
| 4968 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4969 | lua_pushboolean(L, 0); |
| 4970 | return 1; |
| 4971 | } |
| 4972 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4973 | 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] | 4974 | return 1; |
| 4975 | } |
| 4976 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4977 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4978 | static int hlua_http_res_set_status(lua_State *L) |
| 4979 | { |
| 4980 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4981 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4982 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4983 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4984 | /* Check if a valid response is parsed */ |
| 4985 | if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4986 | return 0; |
| 4987 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4988 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4989 | return 0; |
| 4990 | } |
| 4991 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4992 | /* |
| 4993 | * |
| 4994 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4995 | * Class TXN |
| 4996 | * |
| 4997 | * |
| 4998 | */ |
| 4999 | |
| 5000 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5001 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5002 | */ |
| 5003 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5004 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5005 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5006 | } |
| 5007 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5008 | __LJMP static int hlua_set_var(lua_State *L) |
| 5009 | { |
| 5010 | struct hlua_txn *htxn; |
| 5011 | const char *name; |
| 5012 | size_t len; |
| 5013 | struct sample smp; |
| 5014 | |
| 5015 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5016 | |
| 5017 | /* It is useles to retrieve the stream, but this function |
| 5018 | * runs only in a stream context. |
| 5019 | */ |
| 5020 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5021 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5022 | |
| 5023 | /* Converts the third argument in a sample. */ |
| 5024 | hlua_lua2smp(L, 3, &smp); |
| 5025 | |
| 5026 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5027 | 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] | 5028 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5029 | return 0; |
| 5030 | } |
| 5031 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5032 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5033 | { |
| 5034 | struct hlua_txn *htxn; |
| 5035 | const char *name; |
| 5036 | size_t len; |
| 5037 | struct sample smp; |
| 5038 | |
| 5039 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5040 | |
| 5041 | /* It is useles to retrieve the stream, but this function |
| 5042 | * runs only in a stream context. |
| 5043 | */ |
| 5044 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5045 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5046 | |
| 5047 | /* Unset the variable. */ |
| 5048 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5049 | vars_unset_by_name(name, len, &smp); |
| 5050 | return 0; |
| 5051 | } |
| 5052 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5053 | __LJMP static int hlua_get_var(lua_State *L) |
| 5054 | { |
| 5055 | struct hlua_txn *htxn; |
| 5056 | const char *name; |
| 5057 | size_t len; |
| 5058 | struct sample smp; |
| 5059 | |
| 5060 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5061 | |
| 5062 | /* It is useles to retrieve the stream, but this function |
| 5063 | * runs only in a stream context. |
| 5064 | */ |
| 5065 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5066 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5067 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5068 | 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] | 5069 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5070 | lua_pushnil(L); |
| 5071 | return 1; |
| 5072 | } |
| 5073 | |
| 5074 | return hlua_smp2lua(L, &smp); |
| 5075 | } |
| 5076 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5077 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5078 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5079 | struct hlua *hlua; |
| 5080 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5081 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5082 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5083 | /* It is useles to retrieve the stream, but this function |
| 5084 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5085 | */ |
| 5086 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5087 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5088 | |
| 5089 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5090 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5091 | |
| 5092 | /* Get and store new value. */ |
| 5093 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5094 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5095 | |
| 5096 | return 0; |
| 5097 | } |
| 5098 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5099 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5100 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5101 | struct hlua *hlua; |
| 5102 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5103 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5104 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5105 | /* It is useles to retrieve the stream, but this function |
| 5106 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5107 | */ |
| 5108 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5109 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5110 | |
| 5111 | /* Push configuration index in the stack. */ |
| 5112 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5113 | |
| 5114 | return 1; |
| 5115 | } |
| 5116 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5117 | /* Create stack entry containing a class TXN. This function |
| 5118 | * return 0 if the stack does not contains free slots, |
| 5119 | * otherwise it returns 1. |
| 5120 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5121 | 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] | 5122 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5123 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5124 | |
| 5125 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5126 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5127 | return 0; |
| 5128 | |
| 5129 | /* NOTE: The allocation never fails. The failure |
| 5130 | * throw an error, and the function never returns. |
| 5131 | * if the throw is not avalaible, the process is aborted. |
| 5132 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5133 | /* Create the object: obj[0] = userdata. */ |
| 5134 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5135 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5136 | lua_rawseti(L, -2, 0); |
| 5137 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5138 | htxn->s = s; |
| 5139 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5140 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5141 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5142 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5143 | /* Create the "f" field that contains a list of fetches. */ |
| 5144 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5145 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5146 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5147 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5148 | |
| 5149 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5150 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5151 | 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] | 5152 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5153 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5154 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5155 | /* Create the "c" field that contains a list of converters. */ |
| 5156 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5157 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5158 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5159 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5160 | |
| 5161 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5162 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5163 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5164 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5165 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5166 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5167 | /* Create the "req" field that contains the request channel object. */ |
| 5168 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5169 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5170 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5171 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5172 | |
| 5173 | /* Create the "res" field that contains the response channel object. */ |
| 5174 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5175 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5176 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5177 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5178 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5179 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5180 | lua_pushstring(L, "http"); |
| 5181 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5182 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5183 | return 0; |
| 5184 | } |
| 5185 | else |
| 5186 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5187 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5188 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5189 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5190 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5191 | lua_setmetatable(L, -2); |
| 5192 | |
| 5193 | return 1; |
| 5194 | } |
| 5195 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5196 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5197 | { |
| 5198 | const char *msg; |
| 5199 | struct hlua_txn *htxn; |
| 5200 | |
| 5201 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5202 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5203 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5204 | |
| 5205 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5206 | return 0; |
| 5207 | } |
| 5208 | |
| 5209 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5210 | { |
| 5211 | int level; |
| 5212 | const char *msg; |
| 5213 | struct hlua_txn *htxn; |
| 5214 | |
| 5215 | MAY_LJMP(check_args(L, 3, "log")); |
| 5216 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5217 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5218 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5219 | |
| 5220 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5221 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5222 | |
| 5223 | hlua_sendlog(htxn->s->be, level, msg); |
| 5224 | return 0; |
| 5225 | } |
| 5226 | |
| 5227 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5228 | { |
| 5229 | const char *msg; |
| 5230 | struct hlua_txn *htxn; |
| 5231 | |
| 5232 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5233 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5234 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5235 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5236 | return 0; |
| 5237 | } |
| 5238 | |
| 5239 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5240 | { |
| 5241 | const char *msg; |
| 5242 | struct hlua_txn *htxn; |
| 5243 | |
| 5244 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5245 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5246 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5247 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5248 | return 0; |
| 5249 | } |
| 5250 | |
| 5251 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5252 | { |
| 5253 | const char *msg; |
| 5254 | struct hlua_txn *htxn; |
| 5255 | |
| 5256 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5257 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5258 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5259 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5260 | return 0; |
| 5261 | } |
| 5262 | |
| 5263 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5264 | { |
| 5265 | const char *msg; |
| 5266 | struct hlua_txn *htxn; |
| 5267 | |
| 5268 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5269 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5270 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5271 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5272 | return 0; |
| 5273 | } |
| 5274 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5275 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5276 | { |
| 5277 | struct hlua_txn *htxn; |
| 5278 | int ll; |
| 5279 | |
| 5280 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5281 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5282 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5283 | |
| 5284 | if (ll < 0 || ll > 7) |
| 5285 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5286 | |
| 5287 | htxn->s->logs.level = ll; |
| 5288 | return 0; |
| 5289 | } |
| 5290 | |
| 5291 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5292 | { |
| 5293 | struct hlua_txn *htxn; |
| 5294 | struct connection *cli_conn; |
| 5295 | int tos; |
| 5296 | |
| 5297 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5298 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5299 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5300 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5301 | 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] | 5302 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5303 | |
| 5304 | return 0; |
| 5305 | } |
| 5306 | |
| 5307 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5308 | { |
| 5309 | #ifdef SO_MARK |
| 5310 | struct hlua_txn *htxn; |
| 5311 | struct connection *cli_conn; |
| 5312 | int mark; |
| 5313 | |
| 5314 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5315 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5316 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5317 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5318 | 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] | 5319 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5320 | #endif |
| 5321 | return 0; |
| 5322 | } |
| 5323 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5324 | /* This function is an Lua binding that send pending data |
| 5325 | * to the client, and close the stream interface. |
| 5326 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5327 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5328 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5329 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5330 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5331 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5332 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5333 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5334 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5335 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5336 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5337 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5338 | * session, so we just end the execution of the current |
| 5339 | * lua code. |
| 5340 | */ |
| 5341 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5342 | WILL_LJMP(hlua_done(L)); |
| 5343 | return 0; |
| 5344 | } |
| 5345 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5346 | ic = &htxn->s->req; |
| 5347 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5348 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5349 | if (htxn->s->txn) { |
| 5350 | /* HTTP mode, let's stay in sync with the stream */ |
| 5351 | bi_fast_delete(ic->buf, htxn->s->txn->req.sov); |
| 5352 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 5353 | htxn->s->txn->req.sov = 0; |
| 5354 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 5355 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 5356 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5357 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 5358 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5359 | /* Note that if we want to support keep-alive, we need |
| 5360 | * to bypass the close/shutr_now calls below, but that |
| 5361 | * may only be done if the HTTP request was already |
| 5362 | * processed and the connection header is known (ie |
| 5363 | * not during TCP rules). |
| 5364 | */ |
| 5365 | } |
| 5366 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5367 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5368 | channel_abort(ic); |
| 5369 | channel_auto_close(ic); |
| 5370 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5371 | |
| 5372 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5373 | channel_auto_read(oc); |
| 5374 | channel_auto_close(oc); |
| 5375 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5376 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 5377 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5378 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5379 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5380 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5381 | return 0; |
| 5382 | } |
| 5383 | |
| 5384 | __LJMP static int hlua_log(lua_State *L) |
| 5385 | { |
| 5386 | int level; |
| 5387 | const char *msg; |
| 5388 | |
| 5389 | MAY_LJMP(check_args(L, 2, "log")); |
| 5390 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5391 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5392 | |
| 5393 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5394 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5395 | |
| 5396 | hlua_sendlog(NULL, level, msg); |
| 5397 | return 0; |
| 5398 | } |
| 5399 | |
| 5400 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5401 | { |
| 5402 | const char *msg; |
| 5403 | |
| 5404 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5405 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5406 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5407 | return 0; |
| 5408 | } |
| 5409 | |
| 5410 | __LJMP static int hlua_log_info(lua_State *L) |
| 5411 | { |
| 5412 | const char *msg; |
| 5413 | |
| 5414 | MAY_LJMP(check_args(L, 1, "info")); |
| 5415 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5416 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5417 | return 0; |
| 5418 | } |
| 5419 | |
| 5420 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5421 | { |
| 5422 | const char *msg; |
| 5423 | |
| 5424 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5425 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5426 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5427 | return 0; |
| 5428 | } |
| 5429 | |
| 5430 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5431 | { |
| 5432 | const char *msg; |
| 5433 | |
| 5434 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5435 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5436 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5437 | return 0; |
| 5438 | } |
| 5439 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5440 | __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] | 5441 | { |
| 5442 | int wakeup_ms = lua_tointeger(L, -1); |
| 5443 | if (now_ms < wakeup_ms) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5444 | 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] | 5445 | return 0; |
| 5446 | } |
| 5447 | |
| 5448 | __LJMP static int hlua_sleep(lua_State *L) |
| 5449 | { |
| 5450 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5451 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5452 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5453 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5454 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5455 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5456 | wakeup_ms = tick_add(now_ms, delay); |
| 5457 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5458 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5459 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5460 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5461 | } |
| 5462 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5463 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5464 | { |
| 5465 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5466 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5467 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5468 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5469 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5470 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5471 | wakeup_ms = tick_add(now_ms, delay); |
| 5472 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5473 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5474 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5475 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5476 | } |
| 5477 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5478 | /* This functionis an LUA binding. it permits to give back |
| 5479 | * the hand at the HAProxy scheduler. It is used when the |
| 5480 | * LUA processing consumes a lot of time. |
| 5481 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5482 | __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] | 5483 | { |
| 5484 | return 0; |
| 5485 | } |
| 5486 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5487 | __LJMP static int hlua_yield(lua_State *L) |
| 5488 | { |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5489 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 5490 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5491 | } |
| 5492 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5493 | /* This function change the nice of the currently executed |
| 5494 | * task. It is used set low or high priority at the current |
| 5495 | * task. |
| 5496 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5497 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5498 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5499 | struct hlua *hlua; |
| 5500 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5501 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5502 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5503 | hlua = hlua_gethlua(L); |
| 5504 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5505 | |
| 5506 | /* If he task is not set, I'm in a start mode. */ |
| 5507 | if (!hlua || !hlua->task) |
| 5508 | return 0; |
| 5509 | |
| 5510 | if (nice < -1024) |
| 5511 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5512 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5513 | nice = 1024; |
| 5514 | |
| 5515 | hlua->task->nice = nice; |
| 5516 | return 0; |
| 5517 | } |
| 5518 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5519 | /* This function is used as a calback of a task. It is called by the |
| 5520 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5521 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5522 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5523 | * |
| 5524 | * Task wrapper are longjmp safe because the only one Lua code |
| 5525 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5526 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5527 | 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] | 5528 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5529 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5530 | enum hlua_exec status; |
| 5531 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 5532 | if (task->thread_mask == MAX_THREADS_MASK) |
| 5533 | task_set_affinity(task, tid_bit); |
| 5534 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5535 | /* If it is the first call to the task, we must initialize the |
| 5536 | * execution timeouts. |
| 5537 | */ |
| 5538 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5539 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5540 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5541 | /* Execute the Lua code. */ |
| 5542 | status = hlua_ctx_resume(hlua, 1); |
| 5543 | |
| 5544 | switch (status) { |
| 5545 | /* finished or yield */ |
| 5546 | case HLUA_E_OK: |
| 5547 | hlua_ctx_destroy(hlua); |
| 5548 | task_delete(task); |
| 5549 | task_free(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 5550 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5551 | break; |
| 5552 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5553 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 5554 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 5555 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5556 | break; |
| 5557 | |
| 5558 | /* finished with error. */ |
| 5559 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5560 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5561 | hlua_ctx_destroy(hlua); |
| 5562 | task_delete(task); |
| 5563 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5564 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5565 | break; |
| 5566 | |
| 5567 | case HLUA_E_ERR: |
| 5568 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5569 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5570 | hlua_ctx_destroy(hlua); |
| 5571 | task_delete(task); |
| 5572 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5573 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5574 | break; |
| 5575 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5576 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5577 | } |
| 5578 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5579 | /* This function is an LUA binding that register LUA function to be |
| 5580 | * executed after the HAProxy configuration parsing and before the |
| 5581 | * HAProxy scheduler starts. This function expect only one LUA |
| 5582 | * argument that is a function. This function returns nothing, but |
| 5583 | * throws if an error is encountered. |
| 5584 | */ |
| 5585 | __LJMP static int hlua_register_init(lua_State *L) |
| 5586 | { |
| 5587 | struct hlua_init_function *init; |
| 5588 | int ref; |
| 5589 | |
| 5590 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5591 | |
| 5592 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5593 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5594 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5595 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5596 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5597 | |
| 5598 | init->function_ref = ref; |
| 5599 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5600 | return 0; |
| 5601 | } |
| 5602 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5603 | /* This functio is an LUA binding. It permits to register a task |
| 5604 | * executed in parallel of the main HAroxy activity. The task is |
| 5605 | * created and it is set in the HAProxy scheduler. It can be called |
| 5606 | * from the "init" section, "post init" or during the runtime. |
| 5607 | * |
| 5608 | * Lua prototype: |
| 5609 | * |
| 5610 | * <none> core.register_task(<function>) |
| 5611 | */ |
| 5612 | static int hlua_register_task(lua_State *L) |
| 5613 | { |
| 5614 | struct hlua *hlua; |
| 5615 | struct task *task; |
| 5616 | int ref; |
| 5617 | |
| 5618 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5619 | |
| 5620 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5621 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5622 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5623 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5624 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5625 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 5626 | task = task_new(MAX_THREADS_MASK); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5627 | task->context = hlua; |
| 5628 | task->process = hlua_process_task; |
| 5629 | |
| 5630 | if (!hlua_ctx_init(hlua, task)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5631 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5632 | |
| 5633 | /* Restore the function in the stack. */ |
| 5634 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5635 | hlua->nargs = 0; |
| 5636 | |
| 5637 | /* Schedule task. */ |
| 5638 | task_schedule(task, now_ms); |
| 5639 | |
| 5640 | return 0; |
| 5641 | } |
| 5642 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5643 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5644 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5645 | * resume converters. |
| 5646 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5647 | 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] | 5648 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5649 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5650 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5651 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5652 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5653 | if (!stream) |
| 5654 | return 0; |
| 5655 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5656 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5657 | * Lua context can be not initialized. This behavior |
| 5658 | * permits to save performances because a systematic |
| 5659 | * Lua initialization cause 5% performances loss. |
| 5660 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5661 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5662 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5663 | if (!stream->hlua) { |
| 5664 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5665 | return 0; |
| 5666 | } |
| 5667 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5668 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5669 | return 0; |
| 5670 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5671 | } |
| 5672 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5673 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5674 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5675 | |
| 5676 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5677 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5678 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5679 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5680 | else |
| 5681 | error = "critical error"; |
| 5682 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5683 | return 0; |
| 5684 | } |
| 5685 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5686 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5687 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5688 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5689 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5690 | return 0; |
| 5691 | } |
| 5692 | |
| 5693 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5694 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5695 | |
| 5696 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5697 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5698 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5699 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5700 | return 0; |
| 5701 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5702 | hlua_smp2lua(stream->hlua->T, smp); |
| 5703 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5704 | |
| 5705 | /* push keywords in the stack. */ |
| 5706 | if (arg_p) { |
| 5707 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5708 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5709 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5710 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5711 | return 0; |
| 5712 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5713 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5714 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5715 | } |
| 5716 | } |
| 5717 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5718 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5719 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5720 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5721 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5722 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5723 | } |
| 5724 | |
| 5725 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5726 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5727 | /* finished. */ |
| 5728 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5729 | /* If the stack is empty, the function fails. */ |
| 5730 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5731 | return 0; |
| 5732 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5733 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5734 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5735 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5736 | return 1; |
| 5737 | |
| 5738 | /* yield. */ |
| 5739 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5740 | 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] | 5741 | return 0; |
| 5742 | |
| 5743 | /* finished with error. */ |
| 5744 | case HLUA_E_ERRMSG: |
| 5745 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5746 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5747 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5748 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5749 | return 0; |
| 5750 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5751 | case HLUA_E_ETMOUT: |
| 5752 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 5753 | return 0; |
| 5754 | |
| 5755 | case HLUA_E_NOMEM: |
| 5756 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 5757 | return 0; |
| 5758 | |
| 5759 | case HLUA_E_YIELD: |
| 5760 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 5761 | return 0; |
| 5762 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5763 | case HLUA_E_ERR: |
| 5764 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5765 | 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] | 5766 | |
| 5767 | default: |
| 5768 | return 0; |
| 5769 | } |
| 5770 | } |
| 5771 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5772 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5773 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5774 | * resume sample-fetches. This function will be called by the sample |
| 5775 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5776 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5777 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5778 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5779 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5780 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5781 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5782 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5783 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5784 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5785 | if (!stream) |
| 5786 | return 0; |
| 5787 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5788 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5789 | * Lua context can be not initialized. This behavior |
| 5790 | * permits to save performances because a systematic |
| 5791 | * Lua initialization cause 5% performances loss. |
| 5792 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5793 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5794 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5795 | if (!stream->hlua) { |
| 5796 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5797 | return 0; |
| 5798 | } |
| 5799 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5800 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5801 | return 0; |
| 5802 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5803 | } |
| 5804 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5805 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5806 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5807 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5808 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5809 | |
| 5810 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5811 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5812 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5813 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5814 | else |
| 5815 | error = "critical error"; |
| 5816 | 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] | 5817 | return 0; |
| 5818 | } |
| 5819 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5820 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5821 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5822 | 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] | 5823 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5824 | return 0; |
| 5825 | } |
| 5826 | |
| 5827 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5828 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5829 | |
| 5830 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5831 | 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] | 5832 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5833 | 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] | 5834 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5835 | return 0; |
| 5836 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5837 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5838 | |
| 5839 | /* push keywords in the stack. */ |
| 5840 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5841 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5842 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5843 | 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] | 5844 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5845 | return 0; |
| 5846 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5847 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5848 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5849 | } |
| 5850 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5851 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5852 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5853 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5854 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5855 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5856 | } |
| 5857 | |
| 5858 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5859 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5860 | /* finished. */ |
| 5861 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5862 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5863 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5864 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5865 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5866 | /* If the stack is empty, the function fails. */ |
| 5867 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5868 | return 0; |
| 5869 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5870 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5871 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5872 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5873 | |
| 5874 | /* Set the end of execution flag. */ |
| 5875 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5876 | return 1; |
| 5877 | |
| 5878 | /* yield. */ |
| 5879 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5880 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5881 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5882 | 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] | 5883 | return 0; |
| 5884 | |
| 5885 | /* finished with error. */ |
| 5886 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5887 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5888 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5889 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5890 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5891 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5892 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5893 | return 0; |
| 5894 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5895 | case HLUA_E_ETMOUT: |
| 5896 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 5897 | stream_int_retnclose(&stream->si[0], &msg); |
| 5898 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 5899 | return 0; |
| 5900 | |
| 5901 | case HLUA_E_NOMEM: |
| 5902 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 5903 | stream_int_retnclose(&stream->si[0], &msg); |
| 5904 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 5905 | return 0; |
| 5906 | |
| 5907 | case HLUA_E_YIELD: |
| 5908 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 5909 | stream_int_retnclose(&stream->si[0], &msg); |
| 5910 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 5911 | return 0; |
| 5912 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5913 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5914 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5915 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5916 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5917 | 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] | 5918 | |
| 5919 | default: |
| 5920 | return 0; |
| 5921 | } |
| 5922 | } |
| 5923 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5924 | /* This function is an LUA binding used for registering |
| 5925 | * "sample-conv" functions. It expects a converter name used |
| 5926 | * in the haproxy configuration file, and an LUA function. |
| 5927 | */ |
| 5928 | __LJMP static int hlua_register_converters(lua_State *L) |
| 5929 | { |
| 5930 | struct sample_conv_kw_list *sck; |
| 5931 | const char *name; |
| 5932 | int ref; |
| 5933 | int len; |
| 5934 | struct hlua_function *fcn; |
| 5935 | |
| 5936 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 5937 | |
| 5938 | /* First argument : converter name. */ |
| 5939 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5940 | |
| 5941 | /* Second argument : lua function. */ |
| 5942 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5943 | |
| 5944 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5945 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5946 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5947 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5948 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5949 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5950 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5951 | |
| 5952 | /* Fill fcn. */ |
| 5953 | fcn->name = strdup(name); |
| 5954 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5955 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5956 | fcn->function_ref = ref; |
| 5957 | |
| 5958 | /* List head */ |
| 5959 | sck->list.n = sck->list.p = NULL; |
| 5960 | |
| 5961 | /* converter keyword. */ |
| 5962 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5963 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5964 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5965 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5966 | |
| 5967 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 5968 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5969 | 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] | 5970 | sck->kw[0].val_args = NULL; |
| 5971 | sck->kw[0].in_type = SMP_T_STR; |
| 5972 | sck->kw[0].out_type = SMP_T_STR; |
| 5973 | sck->kw[0].private = fcn; |
| 5974 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5975 | /* Register this new converter */ |
| 5976 | sample_register_convs(sck); |
| 5977 | |
| 5978 | return 0; |
| 5979 | } |
| 5980 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5981 | /* This fucntion is an LUA binding used for registering |
| 5982 | * "sample-fetch" functions. It expects a converter name used |
| 5983 | * in the haproxy configuration file, and an LUA function. |
| 5984 | */ |
| 5985 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 5986 | { |
| 5987 | const char *name; |
| 5988 | int ref; |
| 5989 | int len; |
| 5990 | struct sample_fetch_kw_list *sfk; |
| 5991 | struct hlua_function *fcn; |
| 5992 | |
| 5993 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 5994 | |
| 5995 | /* First argument : sample-fetch name. */ |
| 5996 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5997 | |
| 5998 | /* Second argument : lua function. */ |
| 5999 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6000 | |
| 6001 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6002 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6003 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6004 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6005 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6006 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6007 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6008 | |
| 6009 | /* Fill fcn. */ |
| 6010 | fcn->name = strdup(name); |
| 6011 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6012 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6013 | fcn->function_ref = ref; |
| 6014 | |
| 6015 | /* List head */ |
| 6016 | sfk->list.n = sfk->list.p = NULL; |
| 6017 | |
| 6018 | /* sample-fetch keyword. */ |
| 6019 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6020 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6021 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6022 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6023 | |
| 6024 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6025 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6026 | 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] | 6027 | sfk->kw[0].val_args = NULL; |
| 6028 | sfk->kw[0].out_type = SMP_T_STR; |
| 6029 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6030 | sfk->kw[0].val = 0; |
| 6031 | sfk->kw[0].private = fcn; |
| 6032 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6033 | /* Register this new fetch. */ |
| 6034 | sample_register_fetches(sfk); |
| 6035 | |
| 6036 | return 0; |
| 6037 | } |
| 6038 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6039 | /* This function is a wrapper to execute each LUA function declared |
| 6040 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6041 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6042 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6043 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6044 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6045 | 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] | 6046 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6047 | { |
| 6048 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6049 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6050 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6051 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6052 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6053 | |
| 6054 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 6055 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 6056 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 6057 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 6058 | 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] | 6059 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6060 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6061 | return ACT_RET_CONT; |
| 6062 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6063 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6064 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6065 | * Lua context can be not initialized. This behavior |
| 6066 | * permits to save performances because a systematic |
| 6067 | * Lua initialization cause 5% performances loss. |
| 6068 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6069 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6070 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6071 | if (!s->hlua) { |
| 6072 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6073 | rule->arg.hlua_rule->fcn.name); |
| 6074 | return ACT_RET_CONT; |
| 6075 | } |
| 6076 | if (!hlua_ctx_init(s->hlua, s->task)) { |
| 6077 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6078 | rule->arg.hlua_rule->fcn.name); |
| 6079 | return ACT_RET_CONT; |
| 6080 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6081 | } |
| 6082 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6083 | consistency_set(s, dir, &s->hlua->cons); |
| 6084 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6085 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6086 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6087 | |
| 6088 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6089 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6090 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6091 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6092 | else |
| 6093 | error = "critical error"; |
| 6094 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6095 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6096 | return ACT_RET_CONT; |
| 6097 | } |
| 6098 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6099 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6100 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6101 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6102 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6103 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6104 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6105 | } |
| 6106 | |
| 6107 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6108 | 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] | 6109 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6110 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6111 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6112 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6113 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6114 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6115 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6116 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6117 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6118 | |
| 6119 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6120 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6121 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6122 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6123 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6124 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6125 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6126 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6127 | lua_pushstring(s->hlua->T, *arg); |
| 6128 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6129 | } |
| 6130 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6131 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6132 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6133 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6134 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6135 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6136 | } |
| 6137 | |
| 6138 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6139 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6140 | /* finished. */ |
| 6141 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6142 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6143 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6144 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6145 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6146 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6147 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6148 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6149 | |
| 6150 | /* yield. */ |
| 6151 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6152 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6153 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6154 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6155 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6156 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6157 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6158 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6159 | /* Some actions can be wake up when a "write" event |
| 6160 | * is detected on a response channel. This is useful |
| 6161 | * only for actions targetted on the requests. |
| 6162 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6163 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6164 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6165 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6166 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6167 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6168 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6169 | s->req.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6170 | /* We can quit the fcuntion without consistency check |
| 6171 | * because HAProxy is not able to manipulate data, it |
| 6172 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6173 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6174 | |
| 6175 | /* finished with error. */ |
| 6176 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6177 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6178 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6179 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6180 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6181 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6182 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6183 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6184 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6185 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6186 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6187 | case HLUA_E_ETMOUT: |
| 6188 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6189 | stream_int_retnclose(&s->si[0], &msg); |
| 6190 | return ACT_RET_ERR; |
| 6191 | } |
| 6192 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6193 | return 0; |
| 6194 | |
| 6195 | case HLUA_E_NOMEM: |
| 6196 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6197 | stream_int_retnclose(&s->si[0], &msg); |
| 6198 | return ACT_RET_ERR; |
| 6199 | } |
| 6200 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6201 | return 0; |
| 6202 | |
| 6203 | case HLUA_E_YIELD: |
| 6204 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6205 | stream_int_retnclose(&s->si[0], &msg); |
| 6206 | return ACT_RET_ERR; |
| 6207 | } |
| 6208 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6209 | rule->arg.hlua_rule->fcn.name); |
| 6210 | return 0; |
| 6211 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6212 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6213 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6214 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6215 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6216 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6217 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6218 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6219 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6220 | |
| 6221 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6222 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6223 | } |
| 6224 | } |
| 6225 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6226 | 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] | 6227 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6228 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6229 | struct stream_interface *si = ctx->owner; |
| 6230 | |
| 6231 | /* If the applet is wake up without any expected work, the sheduler |
| 6232 | * remove it from the run queue. This flag indicate that the applet |
| 6233 | * is waiting for write. If the buffer is full, the main processing |
| 6234 | * will send some data and after call the applet, otherwise it call |
| 6235 | * the applet ASAP. |
| 6236 | */ |
| 6237 | si_applet_cant_put(si); |
| 6238 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6239 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6240 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6241 | } |
| 6242 | |
| 6243 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6244 | { |
| 6245 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6246 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6247 | struct task *task; |
| 6248 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6249 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6250 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6251 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6252 | if (!hlua) { |
| 6253 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6254 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6255 | return 0; |
| 6256 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6257 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6258 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6259 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6260 | |
| 6261 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6262 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6263 | if (!task) { |
| 6264 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6265 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6266 | return 0; |
| 6267 | } |
| 6268 | task->nice = 0; |
| 6269 | task->context = ctx; |
| 6270 | task->process = hlua_applet_wakeup; |
| 6271 | ctx->ctx.hlua_apptcp.task = task; |
| 6272 | |
| 6273 | /* In the execution wrappers linked with a stream, the |
| 6274 | * Lua context can be not initialized. This behavior |
| 6275 | * permits to save performances because a systematic |
| 6276 | * Lua initialization cause 5% performances loss. |
| 6277 | */ |
| 6278 | if (!hlua_ctx_init(hlua, task)) { |
| 6279 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6280 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6281 | return 0; |
| 6282 | } |
| 6283 | |
| 6284 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6285 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6286 | |
| 6287 | /* The following Lua calls can fail. */ |
| 6288 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6289 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6290 | error = lua_tostring(hlua->T, -1); |
| 6291 | else |
| 6292 | error = "critical error"; |
| 6293 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6294 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6295 | RESET_SAFE_LJMP(hlua->T); |
| 6296 | return 0; |
| 6297 | } |
| 6298 | |
| 6299 | /* Check stack available size. */ |
| 6300 | if (!lua_checkstack(hlua->T, 1)) { |
| 6301 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6302 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6303 | RESET_SAFE_LJMP(hlua->T); |
| 6304 | return 0; |
| 6305 | } |
| 6306 | |
| 6307 | /* Restore the function in the stack. */ |
| 6308 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6309 | |
| 6310 | /* Create and and push object stream in the stack. */ |
| 6311 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6312 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6313 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6314 | RESET_SAFE_LJMP(hlua->T); |
| 6315 | return 0; |
| 6316 | } |
| 6317 | hlua->nargs = 1; |
| 6318 | |
| 6319 | /* push keywords in the stack. */ |
| 6320 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6321 | if (!lua_checkstack(hlua->T, 1)) { |
| 6322 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6323 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6324 | RESET_SAFE_LJMP(hlua->T); |
| 6325 | return 0; |
| 6326 | } |
| 6327 | lua_pushstring(hlua->T, *arg); |
| 6328 | hlua->nargs++; |
| 6329 | } |
| 6330 | |
| 6331 | RESET_SAFE_LJMP(hlua->T); |
| 6332 | |
| 6333 | /* Wakeup the applet ASAP. */ |
| 6334 | si_applet_cant_get(si); |
| 6335 | si_applet_cant_put(si); |
| 6336 | |
| 6337 | return 1; |
| 6338 | } |
| 6339 | |
| 6340 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6341 | { |
| 6342 | struct stream_interface *si = ctx->owner; |
| 6343 | struct stream *strm = si_strm(si); |
| 6344 | struct channel *res = si_ic(si); |
| 6345 | struct act_rule *rule = ctx->rule; |
| 6346 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6347 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6348 | |
| 6349 | /* The applet execution is already done. */ |
| 6350 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) |
| 6351 | return; |
| 6352 | |
| 6353 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6354 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6355 | return; |
| 6356 | |
| 6357 | /* Execute the function. */ |
| 6358 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6359 | /* finished. */ |
| 6360 | case HLUA_E_OK: |
| 6361 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6362 | |
| 6363 | /* log time */ |
| 6364 | strm->logs.tv_request = now; |
| 6365 | |
| 6366 | /* eat the whole request */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6367 | co_skip(si_oc(si), si_ob(si)->o); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6368 | res->flags |= CF_READ_NULL; |
| 6369 | si_shutr(si); |
| 6370 | return; |
| 6371 | |
| 6372 | /* yield. */ |
| 6373 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6374 | if (hlua->wake_time != TICK_ETERNITY) |
| 6375 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6376 | return; |
| 6377 | |
| 6378 | /* finished with error. */ |
| 6379 | case HLUA_E_ERRMSG: |
| 6380 | /* Display log. */ |
| 6381 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6382 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6383 | lua_pop(hlua->T, 1); |
| 6384 | goto error; |
| 6385 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6386 | case HLUA_E_ETMOUT: |
| 6387 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6388 | rule->arg.hlua_rule->fcn.name); |
| 6389 | goto error; |
| 6390 | |
| 6391 | case HLUA_E_NOMEM: |
| 6392 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6393 | rule->arg.hlua_rule->fcn.name); |
| 6394 | goto error; |
| 6395 | |
| 6396 | case HLUA_E_YIELD: /* unexpected */ |
| 6397 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6398 | rule->arg.hlua_rule->fcn.name); |
| 6399 | goto error; |
| 6400 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6401 | case HLUA_E_ERR: |
| 6402 | /* Display log. */ |
| 6403 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6404 | rule->arg.hlua_rule->fcn.name); |
| 6405 | goto error; |
| 6406 | |
| 6407 | default: |
| 6408 | goto error; |
| 6409 | } |
| 6410 | |
| 6411 | error: |
| 6412 | |
| 6413 | /* For all other cases, just close the stream. */ |
| 6414 | si_shutw(si); |
| 6415 | si_shutr(si); |
| 6416 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6417 | } |
| 6418 | |
| 6419 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6420 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6421 | task_delete(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6422 | task_free(ctx->ctx.hlua_apptcp.task); |
| 6423 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6424 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6425 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6426 | } |
| 6427 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6428 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6429 | * an errors occurs and -1 if more data are required for initializing |
| 6430 | * the applet. |
| 6431 | */ |
| 6432 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6433 | { |
| 6434 | struct stream_interface *si = ctx->owner; |
| 6435 | struct channel *req = si_oc(si); |
| 6436 | struct http_msg *msg; |
| 6437 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6438 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6439 | char **arg; |
| 6440 | struct hdr_ctx hdr; |
| 6441 | struct task *task; |
| 6442 | 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] | 6443 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6444 | |
| 6445 | /* Wait for a full HTTP request. */ |
| 6446 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 6447 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 6448 | return -1; |
| 6449 | return 0; |
| 6450 | } |
| 6451 | txn = strm->txn; |
| 6452 | msg = &txn->req; |
| 6453 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6454 | /* We want two things in HTTP mode : |
| 6455 | * - enforce server-close mode if we were in keep-alive, so that the |
| 6456 | * applet is released after each response ; |
| 6457 | * - enable request body transfer to the applet in order to resync |
| 6458 | * with the response body. |
| 6459 | */ |
| 6460 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 6461 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6462 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6463 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6464 | if (!hlua) { |
| 6465 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6466 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6467 | return 0; |
| 6468 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6469 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6470 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6471 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6472 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6473 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6474 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6475 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6476 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6477 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6478 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6479 | if (!task) { |
| 6480 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6481 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6482 | return 0; |
| 6483 | } |
| 6484 | task->nice = 0; |
| 6485 | task->context = ctx; |
| 6486 | task->process = hlua_applet_wakeup; |
| 6487 | ctx->ctx.hlua_apphttp.task = task; |
| 6488 | |
| 6489 | /* In the execution wrappers linked with a stream, the |
| 6490 | * Lua context can be not initialized. This behavior |
| 6491 | * permits to save performances because a systematic |
| 6492 | * Lua initialization cause 5% performances loss. |
| 6493 | */ |
| 6494 | if (!hlua_ctx_init(hlua, task)) { |
| 6495 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6496 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6497 | return 0; |
| 6498 | } |
| 6499 | |
| 6500 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6501 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6502 | |
| 6503 | /* The following Lua calls can fail. */ |
| 6504 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6505 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6506 | error = lua_tostring(hlua->T, -1); |
| 6507 | else |
| 6508 | error = "critical error"; |
| 6509 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6510 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6511 | return 0; |
| 6512 | } |
| 6513 | |
| 6514 | /* Check stack available size. */ |
| 6515 | if (!lua_checkstack(hlua->T, 1)) { |
| 6516 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6517 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6518 | RESET_SAFE_LJMP(hlua->T); |
| 6519 | return 0; |
| 6520 | } |
| 6521 | |
| 6522 | /* Restore the function in the stack. */ |
| 6523 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6524 | |
| 6525 | /* Create and and push object stream in the stack. */ |
| 6526 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6527 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6528 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6529 | RESET_SAFE_LJMP(hlua->T); |
| 6530 | return 0; |
| 6531 | } |
| 6532 | hlua->nargs = 1; |
| 6533 | |
| 6534 | /* Look for a 100-continue expected. */ |
| 6535 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 6536 | hdr.idx = 0; |
| 6537 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) && |
| 6538 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 6539 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 6540 | } |
| 6541 | |
| 6542 | /* push keywords in the stack. */ |
| 6543 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6544 | if (!lua_checkstack(hlua->T, 1)) { |
| 6545 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6546 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6547 | RESET_SAFE_LJMP(hlua->T); |
| 6548 | return 0; |
| 6549 | } |
| 6550 | lua_pushstring(hlua->T, *arg); |
| 6551 | hlua->nargs++; |
| 6552 | } |
| 6553 | |
| 6554 | RESET_SAFE_LJMP(hlua->T); |
| 6555 | |
| 6556 | /* Wakeup the applet when data is ready for read. */ |
| 6557 | si_applet_cant_get(si); |
| 6558 | |
| 6559 | return 1; |
| 6560 | } |
| 6561 | |
| 6562 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 6563 | { |
| 6564 | struct stream_interface *si = ctx->owner; |
| 6565 | struct stream *strm = si_strm(si); |
| 6566 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6567 | struct act_rule *rule = ctx->rule; |
| 6568 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6569 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6570 | char *blk1; |
| 6571 | int len1; |
| 6572 | char *blk2; |
| 6573 | int len2; |
| 6574 | int ret; |
| 6575 | |
| 6576 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6577 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6578 | return; |
| 6579 | |
| 6580 | /* Set the currently running flag. */ |
| 6581 | if (!HLUA_IS_RUNNING(hlua) && |
| 6582 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6583 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6584 | /* Wait for full HTTP analysys. */ |
| 6585 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 6586 | si_applet_cant_get(si); |
| 6587 | return; |
| 6588 | } |
| 6589 | |
| 6590 | /* Store the max amount of bytes that we can read. */ |
| 6591 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 6592 | |
| 6593 | /* We need to flush the request header. This left the body |
| 6594 | * for the Lua. |
| 6595 | */ |
| 6596 | |
| 6597 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6598 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6599 | if (ret == -1) |
| 6600 | return; |
| 6601 | |
| 6602 | /* No data available, ask for more data. */ |
| 6603 | if (ret == 1) |
| 6604 | len2 = 0; |
| 6605 | if (ret == 0) |
| 6606 | len1 = 0; |
| 6607 | if (len1 + len2 < strm->txn->req.eoh + 2) { |
| 6608 | si_applet_cant_get(si); |
| 6609 | return; |
| 6610 | } |
| 6611 | |
| 6612 | /* skip the requests bytes. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6613 | co_skip(si_oc(si), strm->txn->req.eoh + 2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6614 | } |
| 6615 | |
| 6616 | /* Executes The applet if it is not done. */ |
| 6617 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6618 | |
| 6619 | /* Execute the function. */ |
| 6620 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6621 | /* finished. */ |
| 6622 | case HLUA_E_OK: |
| 6623 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6624 | break; |
| 6625 | |
| 6626 | /* yield. */ |
| 6627 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6628 | if (hlua->wake_time != TICK_ETERNITY) |
| 6629 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6630 | return; |
| 6631 | |
| 6632 | /* finished with error. */ |
| 6633 | case HLUA_E_ERRMSG: |
| 6634 | /* Display log. */ |
| 6635 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6636 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6637 | lua_pop(hlua->T, 1); |
| 6638 | goto error; |
| 6639 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6640 | case HLUA_E_ETMOUT: |
| 6641 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 6642 | rule->arg.hlua_rule->fcn.name); |
| 6643 | goto error; |
| 6644 | |
| 6645 | case HLUA_E_NOMEM: |
| 6646 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 6647 | rule->arg.hlua_rule->fcn.name); |
| 6648 | goto error; |
| 6649 | |
| 6650 | case HLUA_E_YIELD: /* unexpected */ |
| 6651 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 6652 | rule->arg.hlua_rule->fcn.name); |
| 6653 | goto error; |
| 6654 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6655 | case HLUA_E_ERR: |
| 6656 | /* Display log. */ |
| 6657 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6658 | rule->arg.hlua_rule->fcn.name); |
| 6659 | goto error; |
| 6660 | |
| 6661 | default: |
| 6662 | goto error; |
| 6663 | } |
| 6664 | } |
| 6665 | |
| 6666 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 6667 | |
| 6668 | /* We must send the final chunk. */ |
| 6669 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 6670 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 6671 | |
| 6672 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6673 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6674 | |
| 6675 | /* critical error. */ |
| 6676 | if (ret == -2 || ret == -3) { |
| 6677 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 6678 | rule->arg.hlua_rule->fcn.name); |
| 6679 | goto error; |
| 6680 | } |
| 6681 | |
| 6682 | /* no enough space error. */ |
| 6683 | if (ret == -1) { |
| 6684 | si_applet_cant_put(si); |
| 6685 | return; |
| 6686 | } |
| 6687 | |
| 6688 | /* set the last chunk sent. */ |
| 6689 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6690 | } |
| 6691 | |
| 6692 | /* close the connection. */ |
| 6693 | |
| 6694 | /* status / log */ |
| 6695 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6696 | strm->logs.tv_request = now; |
| 6697 | |
| 6698 | /* eat the whole request */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6699 | co_skip(si_oc(si), si_ob(si)->o); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6700 | res->flags |= CF_READ_NULL; |
| 6701 | si_shutr(si); |
| 6702 | |
| 6703 | return; |
| 6704 | } |
| 6705 | |
| 6706 | error: |
| 6707 | |
| 6708 | /* If we are in HTTP mode, and we are not send any |
| 6709 | * data, return a 500 server error in best effort: |
| 6710 | * if there are no room avalaible in the buffer, |
| 6711 | * just close the connection. |
| 6712 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6713 | ci_putblk(res, error_500, strlen(error_500)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6714 | if (!(strm->flags & SF_ERR_MASK)) |
| 6715 | strm->flags |= SF_ERR_RESOURCE; |
| 6716 | si_shutw(si); |
| 6717 | si_shutr(si); |
| 6718 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6719 | } |
| 6720 | |
| 6721 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6722 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6723 | task_delete(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6724 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6725 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6726 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6727 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6728 | } |
| 6729 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6730 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6731 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6732 | * |
| 6733 | * This function can fail with an abort() due to an Lua critical error. |
| 6734 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6735 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6736 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6737 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6738 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6739 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6740 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6741 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6742 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6743 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6744 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6745 | if (!rule->arg.hlua_rule) { |
| 6746 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6747 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6748 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6749 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6750 | /* Memory for arguments. */ |
| 6751 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6752 | if (!rule->arg.hlua_rule->args) { |
| 6753 | memprintf(err, "out of memory error"); |
| 6754 | return ACT_RET_PRS_ERR; |
| 6755 | } |
| 6756 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6757 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6758 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6759 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6760 | /* Expect some arguments */ |
| 6761 | for (i = 0; i < fcn->nargs; i++) { |
| 6762 | if (*args[i+1] == '\0') { |
| 6763 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6764 | return ACT_RET_PRS_ERR; |
| 6765 | } |
| 6766 | rule->arg.hlua_rule->args[i] = strdup(args[i + 1]); |
| 6767 | if (!rule->arg.hlua_rule->args[i]) { |
| 6768 | memprintf(err, "out of memory error"); |
| 6769 | return ACT_RET_PRS_ERR; |
| 6770 | } |
| 6771 | (*cur_arg)++; |
| 6772 | } |
| 6773 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6774 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6775 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6776 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6777 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6778 | } |
| 6779 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6780 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6781 | struct act_rule *rule, char **err) |
| 6782 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6783 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6784 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6785 | /* HTTP applets are forbidden in tcp-request rules. |
| 6786 | * HTTP applet request requires everything initilized by |
| 6787 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6788 | * The applet will be immediately initilized, but its before |
| 6789 | * the call of this analyzer. |
| 6790 | */ |
| 6791 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6792 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6793 | return ACT_RET_PRS_ERR; |
| 6794 | } |
| 6795 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6796 | /* Memory for the rule. */ |
| 6797 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6798 | if (!rule->arg.hlua_rule) { |
| 6799 | memprintf(err, "out of memory error"); |
| 6800 | return ACT_RET_PRS_ERR; |
| 6801 | } |
| 6802 | |
| 6803 | /* Reference the Lua function and store the reference. */ |
| 6804 | rule->arg.hlua_rule->fcn = *fcn; |
| 6805 | |
| 6806 | /* TODO: later accept arguments. */ |
| 6807 | rule->arg.hlua_rule->args = NULL; |
| 6808 | |
| 6809 | /* Add applet pointer in the rule. */ |
| 6810 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6811 | rule->applet.name = fcn->name; |
| 6812 | rule->applet.init = hlua_applet_http_init; |
| 6813 | rule->applet.fct = hlua_applet_http_fct; |
| 6814 | rule->applet.release = hlua_applet_http_release; |
| 6815 | rule->applet.timeout = hlua_timeout_applet; |
| 6816 | |
| 6817 | return ACT_RET_PRS_OK; |
| 6818 | } |
| 6819 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6820 | /* This function is an LUA binding used for registering |
| 6821 | * "sample-conv" functions. It expects a converter name used |
| 6822 | * in the haproxy configuration file, and an LUA function. |
| 6823 | */ |
| 6824 | __LJMP static int hlua_register_action(lua_State *L) |
| 6825 | { |
| 6826 | struct action_kw_list *akl; |
| 6827 | const char *name; |
| 6828 | int ref; |
| 6829 | int len; |
| 6830 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6831 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6832 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6833 | /* Initialise the number of expected arguments at 0. */ |
| 6834 | nargs = 0; |
| 6835 | |
| 6836 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6837 | 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] | 6838 | |
| 6839 | /* First argument : converter name. */ |
| 6840 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6841 | |
| 6842 | /* Second argument : environment. */ |
| 6843 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6844 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6845 | |
| 6846 | /* Third argument : lua function. */ |
| 6847 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6848 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6849 | /* Fouth argument : number of mandatories arguments expected on the configuration line. */ |
| 6850 | if (lua_gettop(L) >= 4) |
| 6851 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6852 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6853 | /* browse the second argulent as an array. */ |
| 6854 | lua_pushnil(L); |
| 6855 | while (lua_next(L, 2) != 0) { |
| 6856 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6857 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6858 | |
| 6859 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6860 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6861 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6862 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6863 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6864 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6865 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6866 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6867 | |
| 6868 | /* Fill fcn. */ |
| 6869 | fcn->name = strdup(name); |
| 6870 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6871 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6872 | fcn->function_ref = ref; |
| 6873 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6874 | /* Set the expected number od arguments. */ |
| 6875 | fcn->nargs = nargs; |
| 6876 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6877 | /* List head */ |
| 6878 | akl->list.n = akl->list.p = NULL; |
| 6879 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6880 | /* action keyword. */ |
| 6881 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6882 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6883 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6884 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6885 | |
| 6886 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6887 | |
| 6888 | akl->kw[0].match_pfx = 0; |
| 6889 | akl->kw[0].private = fcn; |
| 6890 | akl->kw[0].parse = action_register_lua; |
| 6891 | |
| 6892 | /* select the action registering point. */ |
| 6893 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6894 | tcp_req_cont_keywords_register(akl); |
| 6895 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6896 | tcp_res_cont_keywords_register(akl); |
| 6897 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6898 | http_req_keywords_register(akl); |
| 6899 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6900 | http_res_keywords_register(akl); |
| 6901 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6902 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6903 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6904 | "are expected.", lua_tostring(L, -1))); |
| 6905 | |
| 6906 | /* pop the environment string. */ |
| 6907 | lua_pop(L, 1); |
| 6908 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6909 | return ACT_RET_PRS_OK; |
| 6910 | } |
| 6911 | |
| 6912 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6913 | struct act_rule *rule, char **err) |
| 6914 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6915 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6916 | |
| 6917 | /* Memory for the rule. */ |
| 6918 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6919 | if (!rule->arg.hlua_rule) { |
| 6920 | memprintf(err, "out of memory error"); |
| 6921 | return ACT_RET_PRS_ERR; |
| 6922 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6923 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6924 | /* Reference the Lua function and store the reference. */ |
| 6925 | rule->arg.hlua_rule->fcn = *fcn; |
| 6926 | |
| 6927 | /* TODO: later accept arguments. */ |
| 6928 | rule->arg.hlua_rule->args = NULL; |
| 6929 | |
| 6930 | /* Add applet pointer in the rule. */ |
| 6931 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6932 | rule->applet.name = fcn->name; |
| 6933 | rule->applet.init = hlua_applet_tcp_init; |
| 6934 | rule->applet.fct = hlua_applet_tcp_fct; |
| 6935 | rule->applet.release = hlua_applet_tcp_release; |
| 6936 | rule->applet.timeout = hlua_timeout_applet; |
| 6937 | |
| 6938 | return 0; |
| 6939 | } |
| 6940 | |
| 6941 | /* This function is an LUA binding used for registering |
| 6942 | * "sample-conv" functions. It expects a converter name used |
| 6943 | * in the haproxy configuration file, and an LUA function. |
| 6944 | */ |
| 6945 | __LJMP static int hlua_register_service(lua_State *L) |
| 6946 | { |
| 6947 | struct action_kw_list *akl; |
| 6948 | const char *name; |
| 6949 | const char *env; |
| 6950 | int ref; |
| 6951 | int len; |
| 6952 | struct hlua_function *fcn; |
| 6953 | |
| 6954 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 6955 | |
| 6956 | /* First argument : converter name. */ |
| 6957 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6958 | |
| 6959 | /* Second argument : environment. */ |
| 6960 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6961 | |
| 6962 | /* Third argument : lua function. */ |
| 6963 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6964 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6965 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6966 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 6967 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6968 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6969 | fcn = calloc(1, sizeof(*fcn)); |
| 6970 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6971 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6972 | |
| 6973 | /* Fill fcn. */ |
| 6974 | len = strlen("<lua.>") + strlen(name) + 1; |
| 6975 | fcn->name = calloc(1, len); |
| 6976 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6977 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6978 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 6979 | fcn->function_ref = ref; |
| 6980 | |
| 6981 | /* List head */ |
| 6982 | akl->list.n = akl->list.p = NULL; |
| 6983 | |
| 6984 | /* converter keyword. */ |
| 6985 | len = strlen("lua.") + strlen(name) + 1; |
| 6986 | akl->kw[0].kw = calloc(1, len); |
| 6987 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6988 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6989 | |
| 6990 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6991 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 6992 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6993 | if (strcmp(env, "tcp") == 0) |
| 6994 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6995 | else if (strcmp(env, "http") == 0) |
| 6996 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6997 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6998 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 6999 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7000 | |
| 7001 | akl->kw[0].match_pfx = 0; |
| 7002 | akl->kw[0].private = fcn; |
| 7003 | |
| 7004 | /* End of array. */ |
| 7005 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7006 | |
| 7007 | /* Register this new converter */ |
| 7008 | service_keywords_register(akl); |
| 7009 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7010 | return 0; |
| 7011 | } |
| 7012 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7013 | /* This function initialises Lua cli handler. It copies the |
| 7014 | * arguments in the Lua stack and create channel IO objects. |
| 7015 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7016 | 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] | 7017 | { |
| 7018 | struct hlua *hlua; |
| 7019 | struct hlua_function *fcn; |
| 7020 | int i; |
| 7021 | const char *error; |
| 7022 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7023 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7024 | appctx->ctx.hlua_cli.fcn = private; |
| 7025 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7026 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7027 | if (!hlua) { |
| 7028 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7029 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7030 | } |
| 7031 | HLUA_INIT(hlua); |
| 7032 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7033 | |
| 7034 | /* Create task used by signal to wakeup applets. |
| 7035 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7036 | * applet_http. It is absolutely compatible. |
| 7037 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7038 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7039 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7040 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7041 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7042 | } |
| 7043 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7044 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7045 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7046 | |
| 7047 | /* Initialises the Lua context */ |
| 7048 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 7049 | 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] | 7050 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7051 | } |
| 7052 | |
| 7053 | /* The following Lua calls can fail. */ |
| 7054 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7055 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7056 | error = lua_tostring(hlua->T, -1); |
| 7057 | else |
| 7058 | error = "critical error"; |
| 7059 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7060 | goto error; |
| 7061 | } |
| 7062 | |
| 7063 | /* Check stack available size. */ |
| 7064 | if (!lua_checkstack(hlua->T, 2)) { |
| 7065 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7066 | goto error; |
| 7067 | } |
| 7068 | |
| 7069 | /* Restore the function in the stack. */ |
| 7070 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7071 | |
| 7072 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7073 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7074 | */ |
| 7075 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7076 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7077 | goto error; |
| 7078 | } |
| 7079 | hlua->nargs = 1; |
| 7080 | |
| 7081 | /* push keywords in the stack. */ |
| 7082 | for (i = 0; *args[i]; i++) { |
| 7083 | /* Check stack available size. */ |
| 7084 | if (!lua_checkstack(hlua->T, 1)) { |
| 7085 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7086 | goto error; |
| 7087 | } |
| 7088 | lua_pushstring(hlua->T, args[i]); |
| 7089 | hlua->nargs++; |
| 7090 | } |
| 7091 | |
| 7092 | /* We must initialize the execution timeouts. */ |
| 7093 | hlua->max_time = hlua_timeout_session; |
| 7094 | |
| 7095 | /* At this point the execution is safe. */ |
| 7096 | RESET_SAFE_LJMP(hlua->T); |
| 7097 | |
| 7098 | /* It's ok */ |
| 7099 | return 0; |
| 7100 | |
| 7101 | /* It's not ok. */ |
| 7102 | error: |
| 7103 | RESET_SAFE_LJMP(hlua->T); |
| 7104 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7105 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7106 | return 1; |
| 7107 | } |
| 7108 | |
| 7109 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7110 | { |
| 7111 | struct hlua *hlua; |
| 7112 | struct stream_interface *si; |
| 7113 | struct hlua_function *fcn; |
| 7114 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7115 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7116 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7117 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7118 | |
| 7119 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7120 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7121 | return 1; |
| 7122 | |
| 7123 | /* Execute the function. */ |
| 7124 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7125 | |
| 7126 | /* finished. */ |
| 7127 | case HLUA_E_OK: |
| 7128 | return 1; |
| 7129 | |
| 7130 | /* yield. */ |
| 7131 | case HLUA_E_AGAIN: |
| 7132 | /* We want write. */ |
| 7133 | if (HLUA_IS_WAKERESWR(hlua)) |
| 7134 | si_applet_cant_put(si); |
| 7135 | /* Set the timeout. */ |
| 7136 | if (hlua->wake_time != TICK_ETERNITY) |
| 7137 | task_schedule(hlua->task, hlua->wake_time); |
| 7138 | return 0; |
| 7139 | |
| 7140 | /* finished with error. */ |
| 7141 | case HLUA_E_ERRMSG: |
| 7142 | /* Display log. */ |
| 7143 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7144 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7145 | lua_pop(hlua->T, 1); |
| 7146 | return 1; |
| 7147 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7148 | case HLUA_E_ETMOUT: |
| 7149 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7150 | fcn->name); |
| 7151 | return 1; |
| 7152 | |
| 7153 | case HLUA_E_NOMEM: |
| 7154 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7155 | fcn->name); |
| 7156 | return 1; |
| 7157 | |
| 7158 | case HLUA_E_YIELD: /* unexpected */ |
| 7159 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7160 | fcn->name); |
| 7161 | return 1; |
| 7162 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7163 | case HLUA_E_ERR: |
| 7164 | /* Display log. */ |
| 7165 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7166 | fcn->name); |
| 7167 | return 1; |
| 7168 | |
| 7169 | default: |
| 7170 | return 1; |
| 7171 | } |
| 7172 | |
| 7173 | return 1; |
| 7174 | } |
| 7175 | |
| 7176 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7177 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7178 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7179 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7180 | } |
| 7181 | |
| 7182 | /* This function is an LUA binding used for registering |
| 7183 | * new keywords in the cli. It expects a list of keywords |
| 7184 | * which are the "path". It is limited to 5 keywords. A |
| 7185 | * description of the command, a function to be executed |
| 7186 | * for the parsing and a function for io handlers. |
| 7187 | */ |
| 7188 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7189 | { |
| 7190 | struct cli_kw_list *cli_kws; |
| 7191 | const char *message; |
| 7192 | int ref_io; |
| 7193 | int len; |
| 7194 | struct hlua_function *fcn; |
| 7195 | int index; |
| 7196 | int i; |
| 7197 | |
| 7198 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7199 | |
| 7200 | /* First argument : an array of maximum 5 keywords. */ |
| 7201 | if (!lua_istable(L, 1)) |
| 7202 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7203 | |
| 7204 | /* Second argument : string with contextual message. */ |
| 7205 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7206 | |
| 7207 | /* Third and fourth argument : lua function. */ |
| 7208 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7209 | |
| 7210 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7211 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7212 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7213 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7214 | fcn = calloc(1, sizeof(*fcn)); |
| 7215 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7216 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7217 | |
| 7218 | /* Fill path. */ |
| 7219 | index = 0; |
| 7220 | lua_pushnil(L); |
| 7221 | while(lua_next(L, 1) != 0) { |
| 7222 | if (index >= 5) |
| 7223 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7224 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7225 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7226 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7227 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7228 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7229 | index++; |
| 7230 | lua_pop(L, 1); |
| 7231 | } |
| 7232 | |
| 7233 | /* Copy help message. */ |
| 7234 | cli_kws->kw[0].usage = strdup(message); |
| 7235 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7236 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7237 | |
| 7238 | /* Fill fcn io handler. */ |
| 7239 | len = strlen("<lua.cli>") + 1; |
| 7240 | for (i = 0; i < index; i++) |
| 7241 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7242 | fcn->name = calloc(1, len); |
| 7243 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7244 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7245 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7246 | for (i = 0; i < index; i++) { |
| 7247 | strncat((char *)fcn->name, ".", len); |
| 7248 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7249 | } |
| 7250 | strncat((char *)fcn->name, ">", len); |
| 7251 | fcn->function_ref = ref_io; |
| 7252 | |
| 7253 | /* Fill last entries. */ |
| 7254 | cli_kws->kw[0].private = fcn; |
| 7255 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7256 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7257 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7258 | |
| 7259 | /* Register this new converter */ |
| 7260 | cli_register_kw(cli_kws); |
| 7261 | |
| 7262 | return 0; |
| 7263 | } |
| 7264 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7265 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7266 | struct proxy *defpx, const char *file, int line, |
| 7267 | char **err, unsigned int *timeout) |
| 7268 | { |
| 7269 | const char *error; |
| 7270 | |
| 7271 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 7272 | if (error && *error != '\0') { |
| 7273 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7274 | return -1; |
| 7275 | } |
| 7276 | return 0; |
| 7277 | } |
| 7278 | |
| 7279 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7280 | struct proxy *defpx, const char *file, int line, |
| 7281 | char **err) |
| 7282 | { |
| 7283 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7284 | file, line, err, &hlua_timeout_session); |
| 7285 | } |
| 7286 | |
| 7287 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7288 | struct proxy *defpx, const char *file, int line, |
| 7289 | char **err) |
| 7290 | { |
| 7291 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7292 | file, line, err, &hlua_timeout_task); |
| 7293 | } |
| 7294 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7295 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7296 | struct proxy *defpx, const char *file, int line, |
| 7297 | char **err) |
| 7298 | { |
| 7299 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7300 | file, line, err, &hlua_timeout_applet); |
| 7301 | } |
| 7302 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7303 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7304 | struct proxy *defpx, const char *file, int line, |
| 7305 | char **err) |
| 7306 | { |
| 7307 | char *error; |
| 7308 | |
| 7309 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7310 | if (*error != '\0') { |
| 7311 | memprintf(err, "%s: invalid number", args[0]); |
| 7312 | return -1; |
| 7313 | } |
| 7314 | return 0; |
| 7315 | } |
| 7316 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7317 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7318 | struct proxy *defpx, const char *file, int line, |
| 7319 | char **err) |
| 7320 | { |
| 7321 | char *error; |
| 7322 | |
| 7323 | if (*(args[1]) == 0) { |
| 7324 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7325 | return -1; |
| 7326 | } |
| 7327 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7328 | if (*error != '\0') { |
| 7329 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7330 | return -1; |
| 7331 | } |
| 7332 | return 0; |
| 7333 | } |
| 7334 | |
| 7335 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7336 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7337 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7338 | * the main lua entry point. |
| 7339 | * |
| 7340 | * This funtion runs with the HAProxy keywords API. It returns -1 if an error is |
| 7341 | * occured, otherwise it returns 0. |
| 7342 | * |
| 7343 | * In some error case, LUA set an error message in top of the stack. This function |
| 7344 | * 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] | 7345 | * |
| 7346 | * This function can fail with an abort() due to an Lua critical error. |
| 7347 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7348 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7349 | */ |
| 7350 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7351 | struct proxy *defpx, const char *file, int line, |
| 7352 | char **err) |
| 7353 | { |
| 7354 | int error; |
| 7355 | |
| 7356 | /* Just load and compile the file. */ |
| 7357 | error = luaL_loadfile(gL.T, args[1]); |
| 7358 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7359 | 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] | 7360 | lua_pop(gL.T, 1); |
| 7361 | return -1; |
| 7362 | } |
| 7363 | |
| 7364 | /* If no syntax error where detected, execute the code. */ |
| 7365 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7366 | switch (error) { |
| 7367 | case LUA_OK: |
| 7368 | break; |
| 7369 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7370 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7371 | lua_pop(gL.T, 1); |
| 7372 | return -1; |
| 7373 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7374 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7375 | return -1; |
| 7376 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7377 | 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] | 7378 | lua_pop(gL.T, 1); |
| 7379 | return -1; |
| 7380 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7381 | 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] | 7382 | lua_pop(gL.T, 1); |
| 7383 | return -1; |
| 7384 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7385 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7386 | lua_pop(gL.T, 1); |
| 7387 | return -1; |
| 7388 | } |
| 7389 | |
| 7390 | return 0; |
| 7391 | } |
| 7392 | |
| 7393 | /* configuration keywords declaration */ |
| 7394 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7395 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7396 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7397 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7398 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7399 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7400 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7401 | { 0, NULL, NULL }, |
| 7402 | }}; |
| 7403 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7404 | /* This function can fail with an abort() due to an Lua critical error. |
| 7405 | * We are in the initialisation process of HAProxy, this abort() is |
| 7406 | * tolerated. |
| 7407 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7408 | int hlua_post_init() |
| 7409 | { |
| 7410 | struct hlua_init_function *init; |
| 7411 | const char *msg; |
| 7412 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7413 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7414 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7415 | /* Call post initialisation function in safe environement. */ |
| 7416 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7417 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7418 | error = lua_tostring(gL.T, -1); |
| 7419 | else |
| 7420 | error = "critical error"; |
| 7421 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7422 | exit(1); |
| 7423 | } |
| 7424 | hlua_fcn_post_init(gL.T); |
| 7425 | RESET_SAFE_LJMP(gL.T); |
| 7426 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7427 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7428 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7429 | ret = hlua_ctx_resume(&gL, 0); |
| 7430 | switch (ret) { |
| 7431 | case HLUA_E_OK: |
| 7432 | lua_pop(gL.T, -1); |
| 7433 | return 1; |
| 7434 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7435 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7436 | return 0; |
| 7437 | case HLUA_E_ERRMSG: |
| 7438 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7439 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7440 | return 0; |
| 7441 | case HLUA_E_ERR: |
| 7442 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7443 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7444 | return 0; |
| 7445 | } |
| 7446 | } |
| 7447 | return 1; |
| 7448 | } |
| 7449 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7450 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7451 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7452 | * is the previously allocated size or the kind of object in case of a new |
| 7453 | * allocation. <nsize> is the requested new size. |
| 7454 | */ |
| 7455 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7456 | { |
| 7457 | struct hlua_mem_allocator *zone = ud; |
| 7458 | |
| 7459 | if (nsize == 0) { |
| 7460 | /* it's a free */ |
| 7461 | if (ptr) |
| 7462 | zone->allocated -= osize; |
| 7463 | free(ptr); |
| 7464 | return NULL; |
| 7465 | } |
| 7466 | |
| 7467 | if (!ptr) { |
| 7468 | /* it's a new allocation */ |
| 7469 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7470 | return NULL; |
| 7471 | |
| 7472 | ptr = malloc(nsize); |
| 7473 | if (ptr) |
| 7474 | zone->allocated += nsize; |
| 7475 | return ptr; |
| 7476 | } |
| 7477 | |
| 7478 | /* it's a realloc */ |
| 7479 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7480 | return NULL; |
| 7481 | |
| 7482 | ptr = realloc(ptr, nsize); |
| 7483 | if (ptr) |
| 7484 | zone->allocated += nsize - osize; |
| 7485 | return ptr; |
| 7486 | } |
| 7487 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7488 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7489 | * We are in the initialisation process of HAProxy, this abort() is |
| 7490 | * tolerated. |
| 7491 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7492 | void hlua_init(void) |
| 7493 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7494 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7495 | int idx; |
| 7496 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7497 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7498 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7499 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7500 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7501 | struct srv_kw *kw; |
| 7502 | int tmp_error; |
| 7503 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7504 | char *args[] = { /* SSL client configuration. */ |
| 7505 | "ssl", |
| 7506 | "verify", |
| 7507 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7508 | NULL |
| 7509 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7510 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7511 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7512 | HA_SPIN_INIT(&hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 7513 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7514 | /* Initialise struct hlua and com signals pool */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7515 | pool_head_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7516 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7517 | /* Register configuration keywords. */ |
| 7518 | cfg_register_keywords(&cfg_kws); |
| 7519 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7520 | /* Init main lua stack. */ |
| 7521 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7522 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7523 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7524 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7525 | hlua_sethlua(&gL); |
| 7526 | gL.Tref = LUA_REFNIL; |
| 7527 | gL.task = NULL; |
| 7528 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7529 | /* From this point, until the end of the initialisation fucntion, |
| 7530 | * the Lua function can fail with an abort. We are in the initialisation |
| 7531 | * process of HAProxy, this abort() is tolerated. |
| 7532 | */ |
| 7533 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7534 | /* Initialise lua. */ |
| 7535 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7536 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7537 | /* Set safe environment for the initialisation. */ |
| 7538 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7539 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7540 | error_msg = lua_tostring(gL.T, -1); |
| 7541 | else |
| 7542 | error_msg = "critical error"; |
| 7543 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7544 | exit(1); |
| 7545 | } |
| 7546 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7547 | /* |
| 7548 | * |
| 7549 | * Create "core" object. |
| 7550 | * |
| 7551 | */ |
| 7552 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7553 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7554 | lua_newtable(gL.T); |
| 7555 | |
| 7556 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7557 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7558 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7559 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7560 | /* Register special functions. */ |
| 7561 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7562 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7563 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7564 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7565 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7566 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7567 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7568 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7569 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7570 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7571 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7572 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7573 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7574 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7575 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7576 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7577 | hlua_class_function(gL.T, "log", hlua_log); |
| 7578 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7579 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7580 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7581 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7582 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7583 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7584 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7585 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7586 | |
| 7587 | /* |
| 7588 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7589 | * Register class Map |
| 7590 | * |
| 7591 | */ |
| 7592 | |
| 7593 | /* This table entry is the object "Map" base. */ |
| 7594 | lua_newtable(gL.T); |
| 7595 | |
| 7596 | /* register pattern types. */ |
| 7597 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7598 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7599 | for (i=0; i<PAT_MATCH_NUM; i++) { |
| 7600 | snprintf(trash.str, trash.size, "_%s", pat_match_names[i]); |
| 7601 | hlua_class_const_int(gL.T, trash.str, i); |
| 7602 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7603 | |
| 7604 | /* register constructor. */ |
| 7605 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7606 | |
| 7607 | /* Create and fill the metatable. */ |
| 7608 | lua_newtable(gL.T); |
| 7609 | |
| 7610 | /* Create and fille the __index entry. */ |
| 7611 | lua_pushstring(gL.T, "__index"); |
| 7612 | lua_newtable(gL.T); |
| 7613 | |
| 7614 | /* Register . */ |
| 7615 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7616 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7617 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7618 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7619 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7620 | /* Register previous table in the registry with reference and named entry. |
| 7621 | * The function hlua_register_metatable() pops the stack, so we |
| 7622 | * previously create a copy of the table. |
| 7623 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7624 | 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] | 7625 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7626 | |
| 7627 | /* Assign the metatable to the mai Map object. */ |
| 7628 | lua_setmetatable(gL.T, -2); |
| 7629 | |
| 7630 | /* Set a name to the table. */ |
| 7631 | lua_setglobal(gL.T, "Map"); |
| 7632 | |
| 7633 | /* |
| 7634 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7635 | * Register class Channel |
| 7636 | * |
| 7637 | */ |
| 7638 | |
| 7639 | /* Create and fill the metatable. */ |
| 7640 | lua_newtable(gL.T); |
| 7641 | |
| 7642 | /* Create and fille the __index entry. */ |
| 7643 | lua_pushstring(gL.T, "__index"); |
| 7644 | lua_newtable(gL.T); |
| 7645 | |
| 7646 | /* Register . */ |
| 7647 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7648 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7649 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7650 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7651 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7652 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7653 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7654 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7655 | 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] | 7656 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7657 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7658 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7659 | |
| 7660 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7661 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7662 | |
| 7663 | /* |
| 7664 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7665 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7666 | * |
| 7667 | */ |
| 7668 | |
| 7669 | /* Create and fill the metatable. */ |
| 7670 | lua_newtable(gL.T); |
| 7671 | |
| 7672 | /* Create and fille the __index entry. */ |
| 7673 | lua_pushstring(gL.T, "__index"); |
| 7674 | lua_newtable(gL.T); |
| 7675 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7676 | /* Browse existing fetches and create the associated |
| 7677 | * object method. |
| 7678 | */ |
| 7679 | sf = NULL; |
| 7680 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7681 | |
| 7682 | /* Dont register the keywork if the arguments check function are |
| 7683 | * not safe during the runtime. |
| 7684 | */ |
| 7685 | if ((sf->val_args != NULL) && |
| 7686 | (sf->val_args != val_payload_lv) && |
| 7687 | (sf->val_args != val_hdr)) |
| 7688 | continue; |
| 7689 | |
| 7690 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7691 | * by an underscore. |
| 7692 | */ |
| 7693 | strncpy(trash.str, sf->kw, trash.size); |
| 7694 | trash.str[trash.size - 1] = '\0'; |
| 7695 | for (p = trash.str; *p; p++) |
| 7696 | if (*p == '.' || *p == '-' || *p == '+') |
| 7697 | *p = '_'; |
| 7698 | |
| 7699 | /* Register the function. */ |
| 7700 | lua_pushstring(gL.T, trash.str); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7701 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7702 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7703 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7704 | } |
| 7705 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7706 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7707 | |
| 7708 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7709 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7710 | |
| 7711 | /* |
| 7712 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7713 | * Register class Converters |
| 7714 | * |
| 7715 | */ |
| 7716 | |
| 7717 | /* Create and fill the metatable. */ |
| 7718 | lua_newtable(gL.T); |
| 7719 | |
| 7720 | /* Create and fill the __index entry. */ |
| 7721 | lua_pushstring(gL.T, "__index"); |
| 7722 | lua_newtable(gL.T); |
| 7723 | |
| 7724 | /* Browse existing converters and create the associated |
| 7725 | * object method. |
| 7726 | */ |
| 7727 | sc = NULL; |
| 7728 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7729 | /* Dont register the keywork if the arguments check function are |
| 7730 | * not safe during the runtime. |
| 7731 | */ |
| 7732 | if (sc->val_args != NULL) |
| 7733 | continue; |
| 7734 | |
| 7735 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7736 | * by an underscore. |
| 7737 | */ |
| 7738 | strncpy(trash.str, sc->kw, trash.size); |
| 7739 | trash.str[trash.size - 1] = '\0'; |
| 7740 | for (p = trash.str; *p; p++) |
| 7741 | if (*p == '.' || *p == '-' || *p == '+') |
| 7742 | *p = '_'; |
| 7743 | |
| 7744 | /* Register the function. */ |
| 7745 | lua_pushstring(gL.T, trash.str); |
| 7746 | lua_pushlightuserdata(gL.T, sc); |
| 7747 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7748 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7749 | } |
| 7750 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7751 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7752 | |
| 7753 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7754 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7755 | |
| 7756 | /* |
| 7757 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7758 | * Register class HTTP |
| 7759 | * |
| 7760 | */ |
| 7761 | |
| 7762 | /* Create and fill the metatable. */ |
| 7763 | lua_newtable(gL.T); |
| 7764 | |
| 7765 | /* Create and fille the __index entry. */ |
| 7766 | lua_pushstring(gL.T, "__index"); |
| 7767 | lua_newtable(gL.T); |
| 7768 | |
| 7769 | /* Register Lua functions. */ |
| 7770 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7771 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7772 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7773 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7774 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7775 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7776 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7777 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7778 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7779 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7780 | |
| 7781 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7782 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7783 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7784 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7785 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7786 | 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] | 7787 | 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] | 7788 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7789 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7790 | |
| 7791 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7792 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7793 | |
| 7794 | /* |
| 7795 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7796 | * Register class AppletTCP |
| 7797 | * |
| 7798 | */ |
| 7799 | |
| 7800 | /* Create and fill the metatable. */ |
| 7801 | lua_newtable(gL.T); |
| 7802 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7803 | /* Create and fille the __index entry. */ |
| 7804 | lua_pushstring(gL.T, "__index"); |
| 7805 | lua_newtable(gL.T); |
| 7806 | |
| 7807 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7808 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7809 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7810 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7811 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7812 | 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] | 7813 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7814 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7815 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7816 | |
| 7817 | lua_settable(gL.T, -3); |
| 7818 | |
| 7819 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7820 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7821 | |
| 7822 | /* |
| 7823 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7824 | * Register class AppletHTTP |
| 7825 | * |
| 7826 | */ |
| 7827 | |
| 7828 | /* Create and fill the metatable. */ |
| 7829 | lua_newtable(gL.T); |
| 7830 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7831 | /* Create and fille the __index entry. */ |
| 7832 | lua_pushstring(gL.T, "__index"); |
| 7833 | lua_newtable(gL.T); |
| 7834 | |
| 7835 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7836 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7837 | 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] | 7838 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7839 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7840 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7841 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7842 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7843 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7844 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7845 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7846 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 7847 | |
| 7848 | lua_settable(gL.T, -3); |
| 7849 | |
| 7850 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7851 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7852 | |
| 7853 | /* |
| 7854 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7855 | * Register class TXN |
| 7856 | * |
| 7857 | */ |
| 7858 | |
| 7859 | /* Create and fill the metatable. */ |
| 7860 | lua_newtable(gL.T); |
| 7861 | |
| 7862 | /* Create and fille the __index entry. */ |
| 7863 | lua_pushstring(gL.T, "__index"); |
| 7864 | lua_newtable(gL.T); |
| 7865 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7866 | /* Register Lua functions. */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7867 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 7868 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7869 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7870 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7871 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7872 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7873 | hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel); |
| 7874 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 7875 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7876 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 7877 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 7878 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 7879 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 7880 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 7881 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7882 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7883 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7884 | |
| 7885 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7886 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7887 | |
| 7888 | /* |
| 7889 | * |
| 7890 | * Register class Socket |
| 7891 | * |
| 7892 | */ |
| 7893 | |
| 7894 | /* Create and fill the metatable. */ |
| 7895 | lua_newtable(gL.T); |
| 7896 | |
| 7897 | /* Create and fille the __index entry. */ |
| 7898 | lua_pushstring(gL.T, "__index"); |
| 7899 | lua_newtable(gL.T); |
| 7900 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7901 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7902 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7903 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7904 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 7905 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 7906 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 7907 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 7908 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 7909 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 7910 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 7911 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 7912 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7913 | 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] | 7914 | |
| 7915 | /* Register the garbage collector entry. */ |
| 7916 | lua_pushstring(gL.T, "__gc"); |
| 7917 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7918 | 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] | 7919 | |
| 7920 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7921 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7922 | |
| 7923 | /* Proxy and server configuration initialisation. */ |
| 7924 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 7925 | init_new_proxy(&socket_proxy); |
| 7926 | socket_proxy.parent = NULL; |
| 7927 | socket_proxy.last_change = now.tv_sec; |
| 7928 | socket_proxy.id = "LUA-SOCKET"; |
| 7929 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 7930 | socket_proxy.maxconn = 0; |
| 7931 | socket_proxy.accept = NULL; |
| 7932 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 7933 | socket_proxy.srv = NULL; |
| 7934 | socket_proxy.conn_retries = 0; |
| 7935 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 7936 | |
| 7937 | /* Init TCP server: unchanged parameters */ |
| 7938 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 7939 | socket_tcp.next = NULL; |
| 7940 | socket_tcp.proxy = &socket_proxy; |
| 7941 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 7942 | LIST_INIT(&socket_tcp.actconns); |
| 7943 | LIST_INIT(&socket_tcp.pendconns); |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 7944 | socket_tcp.priv_conns = NULL; |
| 7945 | socket_tcp.idle_conns = NULL; |
| 7946 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7947 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7948 | socket_tcp.last_change = 0; |
| 7949 | socket_tcp.id = "LUA-TCP-CONN"; |
| 7950 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7951 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7952 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 7953 | |
| 7954 | /* XXX: Copy default parameter from default server, |
| 7955 | * but the default server is not initialized. |
| 7956 | */ |
| 7957 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7958 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 7959 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 7960 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 7961 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 7962 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7963 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7964 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7965 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 7966 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 7967 | |
| 7968 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 7969 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 7970 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 7971 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 7972 | socket_tcp.check.server = &socket_tcp; |
| 7973 | |
| 7974 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 7975 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7976 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7977 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 7978 | socket_tcp.agent.server = &socket_tcp; |
| 7979 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 7980 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7981 | |
| 7982 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7983 | /* Init TCP server: unchanged parameters */ |
| 7984 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 7985 | socket_ssl.next = NULL; |
| 7986 | socket_ssl.proxy = &socket_proxy; |
| 7987 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 7988 | LIST_INIT(&socket_ssl.actconns); |
| 7989 | LIST_INIT(&socket_ssl.pendconns); |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 7990 | socket_tcp.priv_conns = NULL; |
| 7991 | socket_tcp.idle_conns = NULL; |
| 7992 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7993 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7994 | socket_ssl.last_change = 0; |
| 7995 | socket_ssl.id = "LUA-SSL-CONN"; |
| 7996 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7997 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7998 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 7999 | |
| 8000 | /* XXX: Copy default parameter from default server, |
| 8001 | * but the default server is not initialized. |
| 8002 | */ |
| 8003 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8004 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8005 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8006 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8007 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8008 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8009 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8010 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8011 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8012 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8013 | |
| 8014 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8015 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8016 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8017 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8018 | socket_ssl.check.server = &socket_ssl; |
| 8019 | |
| 8020 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8021 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8022 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8023 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8024 | socket_ssl.agent.server = &socket_ssl; |
| 8025 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8026 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8027 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8028 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8029 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8030 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8031 | /* |
| 8032 | * |
| 8033 | * If the keyword is not known, we can search in the registered |
| 8034 | * server keywords. This is usefull to configure special SSL |
| 8035 | * features like client certificates and ssl_verify. |
| 8036 | * |
| 8037 | */ |
| 8038 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8039 | if (tmp_error != 0) { |
| 8040 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8041 | abort(); /* This must be never arrives because the command line |
| 8042 | not editable by the user. */ |
| 8043 | } |
| 8044 | idx += kw->skip; |
| 8045 | } |
| 8046 | } |
| 8047 | |
| 8048 | /* Initialize SSL server. */ |
Willy Tarreau | cbe6da5 | 2018-05-18 17:08:28 +0200 | [diff] [blame] | 8049 | if (socket_ssl.xprt->prepare_srv) { |
| 8050 | int saved_used_backed = global.ssl_used_backend; |
| 8051 | // don't affect maxconn automatic computation |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 8052 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
Willy Tarreau | cbe6da5 | 2018-05-18 17:08:28 +0200 | [diff] [blame] | 8053 | global.ssl_used_backend = saved_used_backed; |
| 8054 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8055 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8056 | |
| 8057 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8058 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8059 | |
| 8060 | __attribute__((constructor)) |
| 8061 | static void __hlua_init(void) |
| 8062 | { |
| 8063 | char *ptr = NULL; |
| 8064 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8065 | hap_register_build_opts(ptr, 1); |
| 8066 | } |