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 | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1563 | } |
| 1564 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1565 | /* This function is called when the "struct stream" is destroyed. |
| 1566 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1567 | * Wake all the pending signals. |
| 1568 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1569 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1570 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1571 | struct xref *peer; |
| 1572 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1573 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1574 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1575 | if (peer) |
| 1576 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1577 | |
| 1578 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1579 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1580 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1581 | } |
| 1582 | |
| 1583 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1584 | * uses this object. If the stream does not exists, just quit. |
| 1585 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1586 | * pending signal can rest in the read and write lists. destroy |
| 1587 | * it. |
| 1588 | */ |
| 1589 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1590 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1591 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1592 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1593 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1594 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1595 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1596 | |
| 1597 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1598 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1599 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1600 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1601 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1602 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1603 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1604 | appctx->ctx.hlua_cosocket.die = 1; |
| 1605 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1606 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1607 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1608 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1609 | return 0; |
| 1610 | } |
| 1611 | |
| 1612 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1613 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1614 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1615 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1616 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1617 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1618 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1619 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1620 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1621 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1622 | |
| 1623 | /* Check if we run on the same thread than the xreator thread. |
| 1624 | * We cannot access to the socket if the thread is different. |
| 1625 | */ |
| 1626 | if (socket->tid != tid) |
| 1627 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1628 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1629 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1630 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1631 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1632 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1633 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1634 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1635 | appctx->ctx.hlua_cosocket.die = 1; |
| 1636 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1637 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1638 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1639 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1640 | return 0; |
| 1641 | } |
| 1642 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1643 | /* The close function calls close_helper. |
| 1644 | */ |
| 1645 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1646 | { |
| 1647 | MAY_LJMP(check_args(L, 1, "close")); |
| 1648 | return hlua_socket_close_helper(L); |
| 1649 | } |
| 1650 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1651 | /* This Lua function assumes that the stack contain three parameters. |
| 1652 | * 1 - USERDATA containing a struct socket |
| 1653 | * 2 - INTEGER with values of the macro defined below |
| 1654 | * If the integer is -1, we must read at most one line. |
| 1655 | * If the integer is -2, we ust read all the data until the |
| 1656 | * end of the stream. |
| 1657 | * If the integer is positive value, we must read a number of |
| 1658 | * bytes corresponding to this value. |
| 1659 | */ |
| 1660 | #define HLSR_READ_LINE (-1) |
| 1661 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1662 | __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] | 1663 | { |
| 1664 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1665 | int wanted = lua_tointeger(L, 2); |
| 1666 | struct hlua *hlua = hlua_gethlua(L); |
| 1667 | struct appctx *appctx; |
| 1668 | int len; |
| 1669 | int nblk; |
| 1670 | char *blk1; |
| 1671 | int len1; |
| 1672 | char *blk2; |
| 1673 | int len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1674 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1675 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1676 | struct stream_interface *si; |
| 1677 | struct stream *s; |
| 1678 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame^] | 1679 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1680 | |
| 1681 | /* Check if this lua stack is schedulable. */ |
| 1682 | if (!hlua || !hlua->task) |
| 1683 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1684 | "'frontend', 'backend' or 'task'")); |
| 1685 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1686 | /* Check if we run on the same thread than the xreator thread. |
| 1687 | * We cannot access to the socket if the thread is different. |
| 1688 | */ |
| 1689 | if (socket->tid != tid) |
| 1690 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1691 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1692 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1693 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1694 | if (!peer) |
| 1695 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1696 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1697 | si = appctx->owner; |
| 1698 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1699 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1700 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1701 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1702 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1703 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1704 | if (nblk < 0) /* Connection close. */ |
| 1705 | goto connection_closed; |
| 1706 | if (nblk == 0) /* No data avalaible. */ |
| 1707 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1708 | |
| 1709 | /* remove final \r\n. */ |
| 1710 | if (nblk == 1) { |
| 1711 | if (blk1[len1-1] == '\n') { |
| 1712 | len1--; |
| 1713 | skip_at_end++; |
| 1714 | if (blk1[len1-1] == '\r') { |
| 1715 | len1--; |
| 1716 | skip_at_end++; |
| 1717 | } |
| 1718 | } |
| 1719 | } |
| 1720 | else { |
| 1721 | if (blk2[len2-1] == '\n') { |
| 1722 | len2--; |
| 1723 | skip_at_end++; |
| 1724 | if (blk2[len2-1] == '\r') { |
| 1725 | len2--; |
| 1726 | skip_at_end++; |
| 1727 | } |
| 1728 | } |
| 1729 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1733 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1734 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1735 | if (nblk < 0) /* Connection close. */ |
| 1736 | goto connection_closed; |
| 1737 | if (nblk == 0) /* No data avalaible. */ |
| 1738 | goto connection_empty; |
| 1739 | } |
| 1740 | |
| 1741 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1742 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1743 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1744 | if (nblk < 0) /* Connection close. */ |
| 1745 | goto connection_closed; |
| 1746 | if (nblk == 0) /* No data avalaible. */ |
| 1747 | goto connection_empty; |
| 1748 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame^] | 1749 | missing_bytes = wanted - socket->b.n; |
| 1750 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1751 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame^] | 1752 | len1 = missing_bytes; |
| 1753 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1754 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1755 | } |
| 1756 | |
| 1757 | len = len1; |
| 1758 | |
| 1759 | luaL_addlstring(&socket->b, blk1, len1); |
| 1760 | if (nblk == 2) { |
| 1761 | len += len2; |
| 1762 | luaL_addlstring(&socket->b, blk2, len2); |
| 1763 | } |
| 1764 | |
| 1765 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1766 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1767 | |
| 1768 | /* Don't wait anything. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1769 | stream_int_notify(&s->si[0]); |
| 1770 | stream_int_update_applet(&s->si[0]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1771 | |
| 1772 | /* If the pattern reclaim to read all the data |
| 1773 | * in the connection, got out. |
| 1774 | */ |
| 1775 | if (wanted == HLSR_READ_ALL) |
| 1776 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame^] | 1777 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1778 | goto connection_empty; |
| 1779 | |
| 1780 | /* Return result. */ |
| 1781 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1782 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1783 | return 1; |
| 1784 | |
| 1785 | connection_closed: |
| 1786 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1787 | xref_unlock(&socket->xref, peer); |
| 1788 | |
| 1789 | no_peer: |
| 1790 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1791 | /* If the buffer containds data. */ |
| 1792 | if (socket->b.n > 0) { |
| 1793 | luaL_pushresult(&socket->b); |
| 1794 | return 1; |
| 1795 | } |
| 1796 | lua_pushnil(L); |
| 1797 | lua_pushstring(L, "connection closed."); |
| 1798 | return 2; |
| 1799 | |
| 1800 | connection_empty: |
| 1801 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1802 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1803 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1804 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1805 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1806 | } |
| 1807 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1808 | 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] | 1809 | return 0; |
| 1810 | } |
| 1811 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1812 | /* This Lua function gets two parameters. The first one can be string |
| 1813 | * or a number. If the string is "*l", the user requires one line. If |
| 1814 | * 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] | 1815 | * If the value is a number, the user require a number of bytes equal |
| 1816 | * to the value. The default value is "*l" (a line). |
| 1817 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1818 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1819 | * integer takes this values: |
| 1820 | * -1 : read a line |
| 1821 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1822 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1823 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1824 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1825 | * concatenated with the read data. |
| 1826 | */ |
| 1827 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1828 | { |
| 1829 | int wanted = HLSR_READ_LINE; |
| 1830 | const char *pattern; |
| 1831 | int type; |
| 1832 | char *error; |
| 1833 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1834 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1835 | |
| 1836 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1837 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1838 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1839 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1840 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1841 | /* Check if we run on the same thread than the xreator thread. |
| 1842 | * We cannot access to the socket if the thread is different. |
| 1843 | */ |
| 1844 | if (socket->tid != tid) |
| 1845 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1846 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1847 | /* check for pattern. */ |
| 1848 | if (lua_gettop(L) >= 2) { |
| 1849 | type = lua_type(L, 2); |
| 1850 | if (type == LUA_TSTRING) { |
| 1851 | pattern = lua_tostring(L, 2); |
| 1852 | if (strcmp(pattern, "*a") == 0) |
| 1853 | wanted = HLSR_READ_ALL; |
| 1854 | else if (strcmp(pattern, "*l") == 0) |
| 1855 | wanted = HLSR_READ_LINE; |
| 1856 | else { |
| 1857 | wanted = strtoll(pattern, &error, 10); |
| 1858 | if (*error != '\0') |
| 1859 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1860 | } |
| 1861 | } |
| 1862 | else if (type == LUA_TNUMBER) { |
| 1863 | wanted = lua_tointeger(L, 2); |
| 1864 | if (wanted < 0) |
| 1865 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1866 | } |
| 1867 | } |
| 1868 | |
| 1869 | /* Set pattern. */ |
| 1870 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1871 | |
| 1872 | /* Check if we would replace the top by itself. */ |
| 1873 | if (lua_gettop(L) != 2) |
| 1874 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1875 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1876 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1877 | luaL_buffinit(L, &socket->b); |
| 1878 | |
| 1879 | /* Check prefix. */ |
| 1880 | if (lua_gettop(L) >= 3) { |
| 1881 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1882 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1883 | pattern = lua_tolstring(L, 3, &len); |
| 1884 | luaL_addlstring(&socket->b, pattern, len); |
| 1885 | } |
| 1886 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1887 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1891 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1892 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1893 | 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] | 1894 | { |
| 1895 | struct hlua_socket *socket; |
| 1896 | struct hlua *hlua = hlua_gethlua(L); |
| 1897 | struct appctx *appctx; |
| 1898 | size_t buf_len; |
| 1899 | const char *buf; |
| 1900 | int len; |
| 1901 | int send_len; |
| 1902 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1903 | struct xref *peer; |
| 1904 | struct stream_interface *si; |
| 1905 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1906 | |
| 1907 | /* Check if this lua stack is schedulable. */ |
| 1908 | if (!hlua || !hlua->task) |
| 1909 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1910 | "'frontend', 'backend' or 'task'")); |
| 1911 | |
| 1912 | /* Get object */ |
| 1913 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1914 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1915 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1916 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1917 | /* Check if we run on the same thread than the xreator thread. |
| 1918 | * We cannot access to the socket if the thread is different. |
| 1919 | */ |
| 1920 | if (socket->tid != tid) |
| 1921 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1922 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1923 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1924 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1925 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1926 | lua_pushinteger(L, -1); |
| 1927 | return 1; |
| 1928 | } |
| 1929 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1930 | si = appctx->owner; |
| 1931 | s = si_strm(si); |
| 1932 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1933 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1934 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1935 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1936 | lua_pushinteger(L, -1); |
| 1937 | return 1; |
| 1938 | } |
| 1939 | |
| 1940 | /* Update the input buffer data. */ |
| 1941 | buf += sent; |
| 1942 | send_len = buf_len - sent; |
| 1943 | |
| 1944 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1945 | if (sent >= buf_len) { |
| 1946 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1947 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1948 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1949 | |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1950 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 1951 | * the request buffer if its not required. |
| 1952 | */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1953 | if (s->req.buf->size == 0) { |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1954 | appctx = hlua->task->context; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1955 | if (!channel_alloc_buffer(&s->req, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1956 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1957 | } |
| 1958 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1959 | /* Check for avalaible space. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1960 | len = buffer_total_space(s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1961 | if (len <= 0) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1962 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1963 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 1964 | xref_unlock(&socket->xref, peer); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1965 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1966 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1967 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1968 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1969 | |
| 1970 | /* send data */ |
| 1971 | if (len < send_len) |
| 1972 | send_len = len; |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1973 | len = ci_putblk(&s->req, buf+sent, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | |
| 1975 | /* "Not enough space" (-1), "Buffer too little to contain |
| 1976 | * the data" (-2) are not expected because the available length |
| 1977 | * is tested. |
| 1978 | * Other unknown error are also not expected. |
| 1979 | */ |
| 1980 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1981 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1982 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1983 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1984 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1985 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1986 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1987 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1988 | return 1; |
| 1989 | } |
| 1990 | |
| 1991 | /* update buffers. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1992 | stream_int_notify(&s->si[0]); |
| 1993 | stream_int_update_applet(&s->si[0]); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 1994 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1995 | s->req.rex = TICK_ETERNITY; |
| 1996 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1997 | |
| 1998 | /* Update length sent. */ |
| 1999 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2000 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2001 | |
| 2002 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2003 | if (sent + len >= buf_len) { |
| 2004 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2005 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2006 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2007 | |
| 2008 | hlua_socket_write_yield_return: |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2009 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2010 | 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] | 2011 | return 0; |
| 2012 | } |
| 2013 | |
| 2014 | /* This function initiate the send of data. It just check the input |
| 2015 | * parameters and push an integer in the Lua stack that contain the |
| 2016 | * amount of data writed in the buffer. This is used by the function |
| 2017 | * "hlua_socket_write_yield" that can yield. |
| 2018 | * |
| 2019 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2020 | * the associated object. The second is a string buffer. The third is |
| 2021 | * a facultative integer that represents where is the buffer position |
| 2022 | * of the start of the data that can send. The first byte is the |
| 2023 | * position "1". The default value is "1". The fourth argument is a |
| 2024 | * facultative integer that represents where is the buffer position |
| 2025 | * of the end of the data that can send. The default is the last byte. |
| 2026 | */ |
| 2027 | static int hlua_socket_send(struct lua_State *L) |
| 2028 | { |
| 2029 | int i; |
| 2030 | int j; |
| 2031 | const char *buf; |
| 2032 | size_t buf_len; |
| 2033 | |
| 2034 | /* Check number of arguments. */ |
| 2035 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2036 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2037 | |
| 2038 | /* Get the string. */ |
| 2039 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2040 | |
| 2041 | /* Get and check j. */ |
| 2042 | if (lua_gettop(L) == 4) { |
| 2043 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2044 | if (j < 0) |
| 2045 | j = buf_len + j + 1; |
| 2046 | if (j > buf_len) |
| 2047 | j = buf_len + 1; |
| 2048 | lua_pop(L, 1); |
| 2049 | } |
| 2050 | else |
| 2051 | j = buf_len; |
| 2052 | |
| 2053 | /* Get and check i. */ |
| 2054 | if (lua_gettop(L) == 3) { |
| 2055 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2056 | if (i < 0) |
| 2057 | i = buf_len + i + 1; |
| 2058 | if (i > buf_len) |
| 2059 | i = buf_len + 1; |
| 2060 | lua_pop(L, 1); |
| 2061 | } else |
| 2062 | i = 1; |
| 2063 | |
| 2064 | /* Check bth i and j. */ |
| 2065 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2066 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2067 | return 1; |
| 2068 | } |
| 2069 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2070 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2071 | return 1; |
| 2072 | } |
| 2073 | if (i == 0) |
| 2074 | i = 1; |
| 2075 | if (j == 0) |
| 2076 | j = 1; |
| 2077 | |
| 2078 | /* Pop the string. */ |
| 2079 | lua_pop(L, 1); |
| 2080 | |
| 2081 | /* Update the buffer length. */ |
| 2082 | buf += i - 1; |
| 2083 | buf_len = j - i + 1; |
| 2084 | lua_pushlstring(L, buf, buf_len); |
| 2085 | |
| 2086 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2087 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2088 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2089 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2090 | } |
| 2091 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2092 | #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] | 2093 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2094 | { |
| 2095 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2096 | int ret; |
| 2097 | int len; |
| 2098 | char *p; |
| 2099 | |
| 2100 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2101 | if (ret <= 0) { |
| 2102 | lua_pushnil(L); |
| 2103 | return 1; |
| 2104 | } |
| 2105 | |
| 2106 | if (ret == AF_UNIX) { |
| 2107 | lua_pushstring(L, buffer+1); |
| 2108 | return 1; |
| 2109 | } |
| 2110 | else if (ret == AF_INET6) { |
| 2111 | buffer[0] = '['; |
| 2112 | len = strlen(buffer); |
| 2113 | buffer[len] = ']'; |
| 2114 | len++; |
| 2115 | buffer[len] = ':'; |
| 2116 | len++; |
| 2117 | p = buffer; |
| 2118 | } |
| 2119 | else if (ret == AF_INET) { |
| 2120 | p = buffer + 1; |
| 2121 | len = strlen(p); |
| 2122 | p[len] = ':'; |
| 2123 | len++; |
| 2124 | } |
| 2125 | else { |
| 2126 | lua_pushnil(L); |
| 2127 | return 1; |
| 2128 | } |
| 2129 | |
| 2130 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2131 | lua_pushnil(L); |
| 2132 | return 1; |
| 2133 | } |
| 2134 | |
| 2135 | lua_pushstring(L, p); |
| 2136 | return 1; |
| 2137 | } |
| 2138 | |
| 2139 | /* Returns information about the peer of the connection. */ |
| 2140 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2141 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2142 | struct hlua_socket *socket; |
| 2143 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2144 | struct xref *peer; |
| 2145 | struct appctx *appctx; |
| 2146 | struct stream_interface *si; |
| 2147 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2148 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2149 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2150 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2151 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2152 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2153 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2154 | /* Check if we run on the same thread than the xreator thread. |
| 2155 | * We cannot access to the socket if the thread is different. |
| 2156 | */ |
| 2157 | if (socket->tid != tid) |
| 2158 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2159 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2160 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2161 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2162 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2163 | lua_pushnil(L); |
| 2164 | return 1; |
| 2165 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2166 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2167 | si = appctx->owner; |
| 2168 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2169 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2170 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2171 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2172 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2173 | lua_pushnil(L); |
| 2174 | return 1; |
| 2175 | } |
| 2176 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2177 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2178 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2179 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2180 | lua_pushnil(L); |
| 2181 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2182 | } |
| 2183 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2184 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2185 | xref_unlock(&socket->xref, peer); |
| 2186 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | /* Returns information about my connection side. */ |
| 2190 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2191 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2192 | struct hlua_socket *socket; |
| 2193 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2194 | struct appctx *appctx; |
| 2195 | struct xref *peer; |
| 2196 | struct stream_interface *si; |
| 2197 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2198 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2199 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2200 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2201 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2202 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2203 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2204 | /* Check if we run on the same thread than the xreator thread. |
| 2205 | * We cannot access to the socket if the thread is different. |
| 2206 | */ |
| 2207 | if (socket->tid != tid) |
| 2208 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2209 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2210 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2211 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2212 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2213 | lua_pushnil(L); |
| 2214 | return 1; |
| 2215 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2216 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2217 | si = appctx->owner; |
| 2218 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2219 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2220 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2221 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2222 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2223 | lua_pushnil(L); |
| 2224 | return 1; |
| 2225 | } |
| 2226 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2227 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2228 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2229 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2230 | lua_pushnil(L); |
| 2231 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2232 | } |
| 2233 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2234 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2235 | xref_unlock(&socket->xref, peer); |
| 2236 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2240 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2241 | .obj_type = OBJ_TYPE_APPLET, |
| 2242 | .name = "<LUA_TCP>", |
| 2243 | .fct = hlua_socket_handler, |
| 2244 | .release = hlua_socket_release, |
| 2245 | }; |
| 2246 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2247 | __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] | 2248 | { |
| 2249 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2250 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2251 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2252 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2253 | struct stream_interface *si; |
| 2254 | struct stream *s; |
| 2255 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2256 | /* Check if we run on the same thread than the xreator thread. |
| 2257 | * We cannot access to the socket if the thread is different. |
| 2258 | */ |
| 2259 | if (socket->tid != tid) |
| 2260 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2261 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2262 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2263 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2264 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2265 | lua_pushnil(L); |
| 2266 | lua_pushstring(L, "Can't connect"); |
| 2267 | return 2; |
| 2268 | } |
| 2269 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2270 | si = appctx->owner; |
| 2271 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2272 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2273 | /* Check if we run on the same thread than the xreator thread. |
| 2274 | * We cannot access to the socket if the thread is different. |
| 2275 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2276 | if (socket->tid != tid) { |
| 2277 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2278 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2279 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2280 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2281 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2282 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2283 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2284 | lua_pushnil(L); |
| 2285 | lua_pushstring(L, "Can't connect"); |
| 2286 | return 2; |
| 2287 | } |
| 2288 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2289 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2290 | |
| 2291 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2292 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2293 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2294 | lua_pushinteger(L, 1); |
| 2295 | return 1; |
| 2296 | } |
| 2297 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2298 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2299 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2301 | } |
| 2302 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2303 | 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] | 2304 | return 0; |
| 2305 | } |
| 2306 | |
| 2307 | /* This function fail or initite the connection. */ |
| 2308 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2309 | { |
| 2310 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2311 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2312 | const char *ip; |
| 2313 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2314 | struct hlua *hlua; |
| 2315 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2316 | int low, high; |
| 2317 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2318 | struct xref *peer; |
| 2319 | struct stream_interface *si; |
| 2320 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2322 | if (lua_gettop(L) < 2) |
| 2323 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2324 | |
| 2325 | /* Get args. */ |
| 2326 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2327 | |
| 2328 | /* Check if we run on the same thread than the xreator thread. |
| 2329 | * We cannot access to the socket if the thread is different. |
| 2330 | */ |
| 2331 | if (socket->tid != tid) |
| 2332 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2333 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2334 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2335 | if (lua_gettop(L) >= 3) { |
| 2336 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2337 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2338 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2339 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2340 | * that are not enclosed within square brackets. |
| 2341 | */ |
| 2342 | if (port > 0) { |
| 2343 | luaL_buffinit(L, &b); |
| 2344 | luaL_addstring(&b, ip); |
| 2345 | luaL_addchar(&b, ':'); |
| 2346 | luaL_pushresult(&b); |
| 2347 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2348 | } |
| 2349 | } |
| 2350 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2351 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2352 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2353 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2354 | lua_pushnil(L); |
| 2355 | return 1; |
| 2356 | } |
| 2357 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2358 | si = appctx->owner; |
| 2359 | s = si_strm(si); |
| 2360 | |
| 2361 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2362 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2363 | if (!conn) { |
| 2364 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2365 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2366 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2367 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2368 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2369 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2370 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2371 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2372 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2373 | if (!addr) { |
| 2374 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2375 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2376 | } |
| 2377 | if (low != high) { |
| 2378 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2379 | 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] | 2380 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2381 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2382 | |
| 2383 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2384 | if (low == 0) { |
| 2385 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2386 | if (port == -1) { |
| 2387 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2388 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2389 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2390 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2391 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2392 | if (port == -1) { |
| 2393 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2394 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2395 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2396 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2397 | } |
| 2398 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2399 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2400 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2401 | appctx = objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2402 | |
| 2403 | /* inform the stream that we want to be notified whenever the |
| 2404 | * connection completes. |
| 2405 | */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2406 | si_applet_cant_get(&s->si[0]); |
| 2407 | si_applet_cant_put(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2408 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2409 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2410 | hlua->flags |= HLUA_MUST_GC; |
| 2411 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2412 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2413 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2414 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2415 | } |
| 2416 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2417 | |
| 2418 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2419 | /* Return yield waiting for connection. */ |
| 2420 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2421 | 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] | 2422 | |
| 2423 | return 0; |
| 2424 | } |
| 2425 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2426 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2427 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2428 | { |
| 2429 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2430 | struct xref *peer; |
| 2431 | struct appctx *appctx; |
| 2432 | struct stream_interface *si; |
| 2433 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2434 | |
| 2435 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2436 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2437 | |
| 2438 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2439 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2440 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2441 | lua_pushnil(L); |
| 2442 | return 1; |
| 2443 | } |
| 2444 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2445 | si = appctx->owner; |
| 2446 | s = si_strm(si); |
| 2447 | |
| 2448 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2449 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2450 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2451 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2452 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2453 | |
| 2454 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2455 | { |
| 2456 | return 0; |
| 2457 | } |
| 2458 | |
| 2459 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2460 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2461 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2462 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2463 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2464 | struct xref *peer; |
| 2465 | struct appctx *appctx; |
| 2466 | struct stream_interface *si; |
| 2467 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2468 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2469 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2470 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2471 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2472 | |
| 2473 | /* round up for inputs that are fractions and convert to millis */ |
| 2474 | dtmout = (0.5 + MAY_LJMP(luaL_checknumber(L, 2))) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2475 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2476 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2477 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2478 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2479 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2480 | if (dtmout > INT_MAX) /* overflow check */ |
| 2481 | WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d", INT_MAX)); |
| 2482 | |
| 2483 | tmout = MS_TO_TICKS((int)dtmout); |
| 2484 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2485 | /* Check if we run on the same thread than the xreator thread. |
| 2486 | * We cannot access to the socket if the thread is different. |
| 2487 | */ |
| 2488 | if (socket->tid != tid) |
| 2489 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2490 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2491 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2492 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2493 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2494 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2495 | WILL_LJMP(lua_error(L)); |
| 2496 | return 0; |
| 2497 | } |
| 2498 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2499 | si = appctx->owner; |
| 2500 | s = si_strm(si); |
| 2501 | |
| 2502 | s->req.rto = tmout; |
| 2503 | s->req.wto = tmout; |
| 2504 | s->res.rto = tmout; |
| 2505 | s->res.wto = tmout; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2506 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2507 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2508 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2509 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2513 | { |
| 2514 | struct hlua_socket *socket; |
| 2515 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2516 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2517 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2518 | |
| 2519 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2520 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2521 | hlua_pusherror(L, "socket: full stack"); |
| 2522 | goto out_fail_conf; |
| 2523 | } |
| 2524 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2525 | /* Create the object: obj[0] = userdata. */ |
| 2526 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2527 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2528 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2529 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2530 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2531 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2532 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2533 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2534 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2535 | goto out_fail_conf; |
| 2536 | } |
| 2537 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2538 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2539 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2540 | lua_setmetatable(L, -2); |
| 2541 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2542 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2543 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2544 | if (!appctx) { |
| 2545 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2546 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2547 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2548 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2549 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2550 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2551 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2552 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2553 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2554 | /* Now create a session, task and stream for this applet */ |
| 2555 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2556 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2557 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2558 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2559 | } |
| 2560 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2561 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2562 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2563 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2564 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2565 | } |
| 2566 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2567 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2568 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2569 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2570 | /* Configure "right" stream interface. this "si" is used to connect |
| 2571 | * and retrieve data from the server. The connection is initialized |
| 2572 | * with the "struct server". |
| 2573 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2574 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2575 | |
| 2576 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2577 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2578 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2579 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2580 | return 1; |
| 2581 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2582 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2583 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2584 | out_fail_sess: |
| 2585 | appctx_free(appctx); |
| 2586 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2587 | WILL_LJMP(lua_error(L)); |
| 2588 | return 0; |
| 2589 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2590 | |
| 2591 | /* |
| 2592 | * |
| 2593 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2594 | * Class Channel |
| 2595 | * |
| 2596 | * |
| 2597 | */ |
| 2598 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2599 | /* The state between the channel data and the HTTP parser state can be |
| 2600 | * unconsistent, so reset the parser and call it again. Warning, this |
| 2601 | * action not revalidate the request and not send a 400 if the modified |
| 2602 | * resuest is not valid. |
| 2603 | * |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2604 | * This function never fails. The direction is set using dir, which equals |
| 2605 | * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2606 | */ |
| 2607 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2608 | { |
| 2609 | /* Protocol HTTP. */ |
| 2610 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2611 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2612 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2613 | http_txn_reset_req(stream->txn); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2614 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2615 | http_txn_reset_res(stream->txn); |
| 2616 | |
| 2617 | if (stream->txn->hdr_idx.v) |
| 2618 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2619 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2620 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2621 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2622 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2623 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2624 | } |
| 2625 | } |
| 2626 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2627 | /* This function is called before the Lua execution. It stores |
| 2628 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2629 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2630 | 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] | 2631 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2632 | c->mode = stream->be->mode; |
| 2633 | switch (c->mode) { |
| 2634 | case PR_MODE_HTTP: |
| 2635 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2636 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2637 | c->data.http.state = stream->txn->req.msg_state; |
| 2638 | else |
| 2639 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2640 | break; |
| 2641 | default: |
| 2642 | break; |
| 2643 | } |
| 2644 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2645 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2646 | /* This function is called after the Lua execution. it |
| 2647 | * returns true if the parser state is consistent, otherwise, |
| 2648 | * it return false. |
| 2649 | * |
| 2650 | * In HTTP mode, the parser state must be in the same state |
| 2651 | * or greater when we exit the function. Even if we do a |
| 2652 | * control yield. This prevent to break the HTTP message |
| 2653 | * from the Lua code. |
| 2654 | */ |
| 2655 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2656 | { |
| 2657 | if (c->mode != stream->be->mode) |
| 2658 | return 0; |
| 2659 | |
| 2660 | switch (c->mode) { |
| 2661 | case PR_MODE_HTTP: |
| 2662 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2663 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2664 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2665 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2666 | else |
| 2667 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2668 | default: |
| 2669 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2670 | } |
| 2671 | return 1; |
| 2672 | } |
| 2673 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2674 | /* Returns the struct hlua_channel join to the class channel in the |
| 2675 | * stack entry "ud" or throws an argument error. |
| 2676 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2677 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2678 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2679 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2680 | } |
| 2681 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2682 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2683 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2684 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2685 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2686 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2687 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2688 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2689 | return 0; |
| 2690 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2691 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2692 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2693 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2694 | |
| 2695 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2696 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2697 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2698 | return 1; |
| 2699 | } |
| 2700 | |
| 2701 | /* Duplicate all the data present in the input channel and put it |
| 2702 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2703 | * the stack if the channel is closed and all the data are consumed, |
| 2704 | * returns 0 if no data are available, otherwise it returns the length |
| 2705 | * of the builded string. |
| 2706 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2707 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2708 | { |
| 2709 | char *blk1; |
| 2710 | char *blk2; |
| 2711 | int len1; |
| 2712 | int len2; |
| 2713 | int ret; |
| 2714 | luaL_Buffer b; |
| 2715 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2716 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2717 | if (unlikely(ret == 0)) |
| 2718 | return 0; |
| 2719 | |
| 2720 | if (unlikely(ret < 0)) { |
| 2721 | lua_pushnil(L); |
| 2722 | return -1; |
| 2723 | } |
| 2724 | |
| 2725 | luaL_buffinit(L, &b); |
| 2726 | luaL_addlstring(&b, blk1, len1); |
| 2727 | if (unlikely(ret == 2)) |
| 2728 | luaL_addlstring(&b, blk2, len2); |
| 2729 | luaL_pushresult(&b); |
| 2730 | |
| 2731 | if (unlikely(ret == 2)) |
| 2732 | return len1 + len2; |
| 2733 | return len1; |
| 2734 | } |
| 2735 | |
| 2736 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2737 | * a yield. This function keep the data in the buffer. |
| 2738 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2739 | __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] | 2740 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2741 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2742 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2743 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2744 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2745 | if (_hlua_channel_dup(chn, L) == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2746 | 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] | 2747 | return 1; |
| 2748 | } |
| 2749 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2750 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2751 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2752 | { |
| 2753 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2754 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2755 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2756 | } |
| 2757 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2758 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2759 | * a yield. This function consumes the data in the buffer. It returns |
| 2760 | * a string containing the data or a nil pointer if no data are available |
| 2761 | * and the channel is closed. |
| 2762 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2763 | __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] | 2764 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2765 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2766 | int ret; |
| 2767 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2768 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2769 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2770 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2771 | if (unlikely(ret == 0)) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2772 | 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] | 2773 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2774 | if (unlikely(ret == -1)) |
| 2775 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2776 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2777 | chn->buf->i -= ret; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2778 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2779 | return 1; |
| 2780 | } |
| 2781 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2782 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 2783 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2784 | { |
| 2785 | MAY_LJMP(check_args(L, 1, "get")); |
| 2786 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2787 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2788 | } |
| 2789 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2790 | /* This functions consumes and returns one line. If the channel is closed, |
| 2791 | * and the last data does not contains a final '\n', the data are returned |
| 2792 | * without the final '\n'. When no more data are avalaible, it returns nil |
| 2793 | * value. |
| 2794 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2795 | __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] | 2796 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2797 | char *blk1; |
| 2798 | char *blk2; |
| 2799 | int len1; |
| 2800 | int len2; |
| 2801 | int len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2802 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2803 | int ret; |
| 2804 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2805 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2806 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2807 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2808 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2809 | if (ret == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2810 | 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] | 2811 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2812 | if (ret == -1) { |
| 2813 | lua_pushnil(L); |
| 2814 | return 1; |
| 2815 | } |
| 2816 | |
| 2817 | luaL_buffinit(L, &b); |
| 2818 | luaL_addlstring(&b, blk1, len1); |
| 2819 | len = len1; |
| 2820 | if (unlikely(ret == 2)) { |
| 2821 | luaL_addlstring(&b, blk2, len2); |
| 2822 | len += len2; |
| 2823 | } |
| 2824 | luaL_pushresult(&b); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2825 | 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] | 2826 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2827 | return 1; |
| 2828 | } |
| 2829 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2830 | /* Check arguments for the fucntion "hlua_channel_getline_yield". */ |
| 2831 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2832 | { |
| 2833 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2834 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2835 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2836 | } |
| 2837 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2838 | /* This function takes a string as input, and append it at the |
| 2839 | * input side of channel. If the data is too big, but a space |
| 2840 | * is probably available after sending some data, the function |
| 2841 | * yield. If the data is bigger than the buffer, or if the |
| 2842 | * channel is closed, it returns -1. otherwise, it returns the |
| 2843 | * amount of data writed. |
| 2844 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2845 | __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] | 2846 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2847 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2848 | size_t len; |
| 2849 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2850 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2851 | int ret; |
| 2852 | int max; |
| 2853 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2854 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2855 | * the request buffer if its not required. |
| 2856 | */ |
| 2857 | if (chn->buf->size == 0) { |
| 2858 | si_applet_cant_put(chn_prod(chn)); |
| 2859 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
| 2860 | } |
| 2861 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2862 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2863 | if (max > len - l) |
| 2864 | max = len - l; |
| 2865 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2866 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2867 | if (ret == -2 || ret == -3) { |
| 2868 | lua_pushinteger(L, -1); |
| 2869 | return 1; |
| 2870 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2871 | if (ret == -1) { |
| 2872 | chn->flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2873 | 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] | 2874 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2875 | l += ret; |
| 2876 | lua_pop(L, 1); |
| 2877 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2878 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2879 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2880 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
| 2881 | if (max == 0 && chn->buf->o == 0) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2882 | /* There are no space avalaible, and the output buffer is empty. |
| 2883 | * in this case, we cannot add more data, so we cannot yield, |
| 2884 | * we return the amount of copyied data. |
| 2885 | */ |
| 2886 | return 1; |
| 2887 | } |
| 2888 | if (l < len) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2889 | 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] | 2890 | return 1; |
| 2891 | } |
| 2892 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2893 | /* just a wrapper of "hlua_channel_append_yield". It returns the length |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2894 | * of the writed string, or -1 if the channel is closed or if the |
| 2895 | * buffer size is too little for the data. |
| 2896 | */ |
| 2897 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2898 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2899 | size_t len; |
| 2900 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2901 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2902 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2903 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2904 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2905 | lua_pushinteger(L, 0); |
| 2906 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2907 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2908 | } |
| 2909 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2910 | /* just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2911 | * his process by cleaning the buffer. The result is a replacement |
| 2912 | * of the current data. It returns the length of the writed string, |
| 2913 | * or -1 if the channel is closed or if the buffer size is too |
| 2914 | * little for the data. |
| 2915 | */ |
| 2916 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2917 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2918 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2919 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2920 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2921 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2922 | lua_pushinteger(L, 0); |
| 2923 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2924 | chn->buf->i = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2925 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2926 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2927 | } |
| 2928 | |
| 2929 | /* Append data in the output side of the buffer. This data is immediatly |
| 2930 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 2931 | * cannot contains the data, the function yield. The function returns -1 |
| 2932 | * if the channel is closed. |
| 2933 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2934 | __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] | 2935 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2936 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2937 | size_t len; |
| 2938 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2939 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2940 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2941 | struct hlua *hlua = hlua_gethlua(L); |
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 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2944 | lua_pushinteger(L, -1); |
| 2945 | return 1; |
| 2946 | } |
| 2947 | |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2948 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2949 | * the request buffer if its not required. |
| 2950 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2951 | if (chn->buf->size == 0) { |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2952 | si_applet_cant_put(chn_prod(chn)); |
| 2953 | 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] | 2954 | } |
| 2955 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2956 | /* the writed data will be immediatly sent, so we can check |
| 2957 | * the avalaible space without taking in account the reserve. |
| 2958 | * The reserve is guaranted for the processing of incoming |
| 2959 | * data, because the buffer will be flushed. |
| 2960 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2961 | max = chn->buf->size - buffer_len(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2962 | |
| 2963 | /* If there are no space avalaible, and the output buffer is empty. |
| 2964 | * in this case, we cannot add more data, so we cannot yield, |
| 2965 | * we return the amount of copyied data. |
| 2966 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2967 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2968 | return 1; |
| 2969 | |
| 2970 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2971 | if (max > len - l) |
| 2972 | max = len - l; |
| 2973 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2974 | /* The buffer avalaible size may be not contiguous. This test |
| 2975 | * detects a non contiguous buffer and realign it. |
| 2976 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2977 | if (bi_space_for_replace(chn->buf) < max) |
| 2978 | buffer_slow_realign(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2979 | |
| 2980 | /* Copy input data in the buffer. */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2981 | 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] | 2982 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2983 | /* buffer replace considers that the input part is filled. |
| 2984 | * so, I must forward these new data in the output part. |
| 2985 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2986 | b_adv(chn->buf, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2987 | |
| 2988 | l += max; |
| 2989 | lua_pop(L, 1); |
| 2990 | lua_pushinteger(L, l); |
| 2991 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2992 | /* If there are no space avalaible, and the output buffer is empty. |
| 2993 | * in this case, we cannot add more data, so we cannot yield, |
| 2994 | * we return the amount of copyied data. |
| 2995 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2996 | max = chn->buf->size - buffer_len(chn->buf); |
| 2997 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2998 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2999 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3000 | if (l < len) { |
| 3001 | /* If we are waiting for space in the response buffer, we |
| 3002 | * must set the flag WAKERESWR. This flag required the task |
| 3003 | * wake up if any activity is detected on the response buffer. |
| 3004 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3005 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3006 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3007 | else |
| 3008 | HLUA_SET_WAKEREQWR(hlua); |
| 3009 | 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] | 3010 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3011 | |
| 3012 | return 1; |
| 3013 | } |
| 3014 | |
| 3015 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3016 | * yield the LUA process, and resume it without checking the |
| 3017 | * input arguments. |
| 3018 | */ |
| 3019 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3020 | { |
| 3021 | MAY_LJMP(check_args(L, 2, "send")); |
| 3022 | lua_pushinteger(L, 0); |
| 3023 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3024 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | /* This function forward and amount of butes. The data pass from |
| 3028 | * the input side of the buffer to the output side, and can be |
| 3029 | * forwarded. This function never fails. |
| 3030 | * |
| 3031 | * The Lua function takes an amount of bytes to be forwarded in |
| 3032 | * imput. It returns the number of bytes forwarded. |
| 3033 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3034 | __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] | 3035 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3036 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3037 | int len; |
| 3038 | int l; |
| 3039 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3040 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3041 | |
| 3042 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3043 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3044 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3045 | |
| 3046 | max = len - l; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3047 | if (max > chn->buf->i) |
| 3048 | max = chn->buf->i; |
| 3049 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3050 | l += max; |
| 3051 | |
| 3052 | lua_pop(L, 1); |
| 3053 | lua_pushinteger(L, l); |
| 3054 | |
| 3055 | /* Check if it miss bytes to forward. */ |
| 3056 | if (l < len) { |
| 3057 | /* The the input channel or the output channel are closed, we |
| 3058 | * must return the amount of data forwarded. |
| 3059 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3060 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3061 | return 1; |
| 3062 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3063 | /* If we are waiting for space data in the response buffer, we |
| 3064 | * must set the flag WAKERESWR. This flag required the task |
| 3065 | * wake up if any activity is detected on the response buffer. |
| 3066 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3067 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3068 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3069 | else |
| 3070 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3071 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3072 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 3073 | 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] | 3074 | } |
| 3075 | |
| 3076 | return 1; |
| 3077 | } |
| 3078 | |
| 3079 | /* Just check the input and prepare the stack for the previous |
| 3080 | * function "hlua_channel_forward_yield" |
| 3081 | */ |
| 3082 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3083 | { |
| 3084 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3085 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3086 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3087 | |
| 3088 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3089 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | /* Just returns the number of bytes available in the input |
| 3093 | * side of the buffer. This function never fails. |
| 3094 | */ |
| 3095 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3096 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3097 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3098 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3099 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3100 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3101 | lua_pushinteger(L, chn->buf->i); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3102 | return 1; |
| 3103 | } |
| 3104 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3105 | /* Returns true if the channel is full. */ |
| 3106 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3107 | { |
| 3108 | struct channel *chn; |
| 3109 | int rem; |
| 3110 | |
| 3111 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3112 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3113 | |
| 3114 | rem = chn->buf->size; |
| 3115 | rem -= chn->buf->o; /* Output size */ |
| 3116 | rem -= chn->buf->i; /* Input size */ |
| 3117 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3118 | |
| 3119 | lua_pushboolean(L, rem <= 0); |
| 3120 | return 1; |
| 3121 | } |
| 3122 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3123 | /* Just returns the number of bytes available in the output |
| 3124 | * side of the buffer. This function never fails. |
| 3125 | */ |
| 3126 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3127 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3128 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3129 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3130 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3131 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3132 | lua_pushinteger(L, chn->buf->o); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3133 | return 1; |
| 3134 | } |
| 3135 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3136 | /* |
| 3137 | * |
| 3138 | * |
| 3139 | * Class Fetches |
| 3140 | * |
| 3141 | * |
| 3142 | */ |
| 3143 | |
| 3144 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3145 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3146 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3147 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3148 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3149 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | /* This function creates and push in the stack a fetch object according |
| 3153 | * with a current TXN. |
| 3154 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3155 | 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] | 3156 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3157 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3158 | |
| 3159 | /* Check stack size. */ |
| 3160 | if (!lua_checkstack(L, 3)) |
| 3161 | return 0; |
| 3162 | |
| 3163 | /* Create the object: obj[0] = userdata. |
| 3164 | * Note that the base of the Fetches object is the |
| 3165 | * transaction object. |
| 3166 | */ |
| 3167 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3168 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3169 | lua_rawseti(L, -2, 0); |
| 3170 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3171 | hsmp->s = txn->s; |
| 3172 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3173 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3174 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3175 | |
| 3176 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3177 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3178 | lua_setmetatable(L, -2); |
| 3179 | |
| 3180 | return 1; |
| 3181 | } |
| 3182 | |
| 3183 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3184 | * It uses closure argument to store the associated sample-fetch. It |
| 3185 | * returns only one argument or throws an error. An error is thrown |
| 3186 | * only if an error is encountered during the argument parsing. If |
| 3187 | * the "sample-fetch" function fails, nil is returned. |
| 3188 | */ |
| 3189 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3190 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3191 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3192 | struct sample_fetch *f; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3193 | struct arg args[ARGM_NBARGS + 1]; |
| 3194 | int i; |
| 3195 | struct sample smp; |
| 3196 | |
| 3197 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3198 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3199 | |
| 3200 | /* Get traditionnal arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3201 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3202 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3203 | /* Check execution authorization. */ |
| 3204 | if (f->use & SMP_USE_HTTP_ANY && |
| 3205 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3206 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3207 | "is not available in Lua services", f->kw); |
| 3208 | WILL_LJMP(lua_error(L)); |
| 3209 | } |
| 3210 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3211 | /* Get extra arguments. */ |
| 3212 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3213 | if (i >= ARGM_NBARGS) |
| 3214 | break; |
| 3215 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3216 | } |
| 3217 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 3218 | args[i].data.str.str = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3219 | |
| 3220 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3221 | 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] | 3222 | |
| 3223 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3224 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3225 | lua_pushfstring(L, "error in arguments"); |
| 3226 | WILL_LJMP(lua_error(L)); |
| 3227 | } |
| 3228 | |
| 3229 | /* Initialise the sample. */ |
| 3230 | memset(&smp, 0, sizeof(smp)); |
| 3231 | |
| 3232 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3233 | 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] | 3234 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3235 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3236 | lua_pushstring(L, ""); |
| 3237 | else |
| 3238 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3239 | return 1; |
| 3240 | } |
| 3241 | |
| 3242 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3243 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3244 | hlua_smp2lua_str(L, &smp); |
| 3245 | else |
| 3246 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3247 | return 1; |
| 3248 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3249 | |
| 3250 | /* |
| 3251 | * |
| 3252 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3253 | * Class Converters |
| 3254 | * |
| 3255 | * |
| 3256 | */ |
| 3257 | |
| 3258 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3259 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3260 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3261 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3262 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3263 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3264 | } |
| 3265 | |
| 3266 | /* This function creates and push in the stack a Converters object |
| 3267 | * according with a current TXN. |
| 3268 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3269 | 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] | 3270 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3271 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3272 | |
| 3273 | /* Check stack size. */ |
| 3274 | if (!lua_checkstack(L, 3)) |
| 3275 | return 0; |
| 3276 | |
| 3277 | /* Create the object: obj[0] = userdata. |
| 3278 | * Note that the base of the Converters object is the |
| 3279 | * same than the TXN object. |
| 3280 | */ |
| 3281 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3282 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3283 | lua_rawseti(L, -2, 0); |
| 3284 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3285 | hsmp->s = txn->s; |
| 3286 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3287 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3288 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3289 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3290 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3291 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3292 | lua_setmetatable(L, -2); |
| 3293 | |
| 3294 | return 1; |
| 3295 | } |
| 3296 | |
| 3297 | /* This function is an LUA binding. It is called with each converter. |
| 3298 | * It uses closure argument to store the associated converter. It |
| 3299 | * returns only one argument or throws an error. An error is thrown |
| 3300 | * only if an error is encountered during the argument parsing. If |
| 3301 | * the converter function function fails, nil is returned. |
| 3302 | */ |
| 3303 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3304 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3305 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3306 | struct sample_conv *conv; |
| 3307 | struct arg args[ARGM_NBARGS + 1]; |
| 3308 | int i; |
| 3309 | struct sample smp; |
| 3310 | |
| 3311 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3312 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3313 | |
| 3314 | /* Get traditionnal arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3315 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3316 | |
| 3317 | /* Get extra arguments. */ |
| 3318 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3319 | if (i >= ARGM_NBARGS) |
| 3320 | break; |
| 3321 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3322 | } |
| 3323 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 3324 | args[i].data.str.str = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3325 | |
| 3326 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3327 | 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] | 3328 | |
| 3329 | /* Run the special args checker. */ |
| 3330 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3331 | hlua_pusherror(L, "error in arguments"); |
| 3332 | WILL_LJMP(lua_error(L)); |
| 3333 | } |
| 3334 | |
| 3335 | /* Initialise the sample. */ |
| 3336 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3337 | hlua_pusherror(L, "error in the input argument"); |
| 3338 | WILL_LJMP(lua_error(L)); |
| 3339 | } |
| 3340 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3341 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3342 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3343 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3344 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3345 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3346 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3347 | WILL_LJMP(lua_error(L)); |
| 3348 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3349 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3350 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3351 | hlua_pusherror(L, "error during the input argument casting"); |
| 3352 | WILL_LJMP(lua_error(L)); |
| 3353 | } |
| 3354 | |
| 3355 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3356 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3357 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3358 | lua_pushstring(L, ""); |
| 3359 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3360 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3361 | return 1; |
| 3362 | } |
| 3363 | |
| 3364 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3365 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3366 | hlua_smp2lua_str(L, &smp); |
| 3367 | else |
| 3368 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3369 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3370 | } |
| 3371 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3372 | /* |
| 3373 | * |
| 3374 | * |
| 3375 | * Class AppletTCP |
| 3376 | * |
| 3377 | * |
| 3378 | */ |
| 3379 | |
| 3380 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3381 | * a class stream, otherwise it throws an error. |
| 3382 | */ |
| 3383 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3384 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3385 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3386 | } |
| 3387 | |
| 3388 | /* This function creates and push in the stack an Applet object |
| 3389 | * according with a current TXN. |
| 3390 | */ |
| 3391 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3392 | { |
| 3393 | struct hlua_appctx *appctx; |
| 3394 | struct stream_interface *si = ctx->owner; |
| 3395 | struct stream *s = si_strm(si); |
| 3396 | struct proxy *p = s->be; |
| 3397 | |
| 3398 | /* Check stack size. */ |
| 3399 | if (!lua_checkstack(L, 3)) |
| 3400 | return 0; |
| 3401 | |
| 3402 | /* Create the object: obj[0] = userdata. |
| 3403 | * Note that the base of the Converters object is the |
| 3404 | * same than the TXN object. |
| 3405 | */ |
| 3406 | lua_newtable(L); |
| 3407 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3408 | lua_rawseti(L, -2, 0); |
| 3409 | appctx->appctx = ctx; |
| 3410 | appctx->htxn.s = s; |
| 3411 | appctx->htxn.p = p; |
| 3412 | |
| 3413 | /* Create the "f" field that contains a list of fetches. */ |
| 3414 | lua_pushstring(L, "f"); |
| 3415 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3416 | return 0; |
| 3417 | lua_settable(L, -3); |
| 3418 | |
| 3419 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3420 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3421 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3422 | return 0; |
| 3423 | lua_settable(L, -3); |
| 3424 | |
| 3425 | /* Create the "c" field that contains a list of converters. */ |
| 3426 | lua_pushstring(L, "c"); |
| 3427 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3428 | return 0; |
| 3429 | lua_settable(L, -3); |
| 3430 | |
| 3431 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3432 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3433 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3434 | return 0; |
| 3435 | lua_settable(L, -3); |
| 3436 | |
| 3437 | /* Pop a class stream metatable and affect it to the table. */ |
| 3438 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3439 | lua_setmetatable(L, -2); |
| 3440 | |
| 3441 | return 1; |
| 3442 | } |
| 3443 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3444 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3445 | { |
| 3446 | struct hlua_appctx *appctx; |
| 3447 | struct stream *s; |
| 3448 | const char *name; |
| 3449 | size_t len; |
| 3450 | struct sample smp; |
| 3451 | |
| 3452 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3453 | |
| 3454 | /* It is useles to retrieve the stream, but this function |
| 3455 | * runs only in a stream context. |
| 3456 | */ |
| 3457 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3458 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3459 | s = appctx->htxn.s; |
| 3460 | |
| 3461 | /* Converts the third argument in a sample. */ |
| 3462 | hlua_lua2smp(L, 3, &smp); |
| 3463 | |
| 3464 | /* Store the sample in a variable. */ |
| 3465 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3466 | vars_set_by_name(name, len, &smp); |
| 3467 | return 0; |
| 3468 | } |
| 3469 | |
| 3470 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3471 | { |
| 3472 | struct hlua_appctx *appctx; |
| 3473 | struct stream *s; |
| 3474 | const char *name; |
| 3475 | size_t len; |
| 3476 | struct sample smp; |
| 3477 | |
| 3478 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3479 | |
| 3480 | /* It is useles to retrieve the stream, but this function |
| 3481 | * runs only in a stream context. |
| 3482 | */ |
| 3483 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3484 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3485 | s = appctx->htxn.s; |
| 3486 | |
| 3487 | /* Unset the variable. */ |
| 3488 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3489 | vars_unset_by_name(name, len, &smp); |
| 3490 | return 0; |
| 3491 | } |
| 3492 | |
| 3493 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3494 | { |
| 3495 | struct hlua_appctx *appctx; |
| 3496 | struct stream *s; |
| 3497 | const char *name; |
| 3498 | size_t len; |
| 3499 | struct sample smp; |
| 3500 | |
| 3501 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3502 | |
| 3503 | /* It is useles to retrieve the stream, but this function |
| 3504 | * runs only in a stream context. |
| 3505 | */ |
| 3506 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3507 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3508 | s = appctx->htxn.s; |
| 3509 | |
| 3510 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3511 | if (!vars_get_by_name(name, len, &smp)) { |
| 3512 | lua_pushnil(L); |
| 3513 | return 1; |
| 3514 | } |
| 3515 | |
| 3516 | return hlua_smp2lua(L, &smp); |
| 3517 | } |
| 3518 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3519 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3520 | { |
| 3521 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3522 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3523 | struct hlua *hlua; |
| 3524 | |
| 3525 | /* 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] | 3526 | if (!s->hlua) |
| 3527 | return 0; |
| 3528 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3529 | |
| 3530 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3531 | |
| 3532 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3533 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3534 | |
| 3535 | /* Get and store new value. */ |
| 3536 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3537 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3538 | |
| 3539 | return 0; |
| 3540 | } |
| 3541 | |
| 3542 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3543 | { |
| 3544 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3545 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3546 | struct hlua *hlua; |
| 3547 | |
| 3548 | /* 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] | 3549 | if (!s->hlua) { |
| 3550 | lua_pushnil(L); |
| 3551 | return 1; |
| 3552 | } |
| 3553 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3554 | |
| 3555 | /* Push configuration index in the stack. */ |
| 3556 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3557 | |
| 3558 | return 1; |
| 3559 | } |
| 3560 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3561 | /* If expected data not yet available, it returns a yield. This function |
| 3562 | * consumes the data in the buffer. It returns a string containing the |
| 3563 | * data. This string can be empty. |
| 3564 | */ |
| 3565 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3566 | { |
| 3567 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3568 | struct stream_interface *si = appctx->appctx->owner; |
| 3569 | int ret; |
| 3570 | char *blk1; |
| 3571 | int len1; |
| 3572 | char *blk2; |
| 3573 | int len2; |
| 3574 | |
| 3575 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3576 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3577 | |
| 3578 | /* Data not yet avalaible. return yield. */ |
| 3579 | if (ret == 0) { |
| 3580 | si_applet_cant_get(si); |
| 3581 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
| 3582 | } |
| 3583 | |
| 3584 | /* End of data: commit the total strings and return. */ |
| 3585 | if (ret < 0) { |
| 3586 | luaL_pushresult(&appctx->b); |
| 3587 | return 1; |
| 3588 | } |
| 3589 | |
| 3590 | /* Ensure that the block 2 length is usable. */ |
| 3591 | if (ret == 1) |
| 3592 | len2 = 0; |
| 3593 | |
| 3594 | /* dont check the max length read and dont check. */ |
| 3595 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3596 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3597 | |
| 3598 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3599 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3600 | luaL_pushresult(&appctx->b); |
| 3601 | return 1; |
| 3602 | } |
| 3603 | |
| 3604 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3605 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3606 | { |
| 3607 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3608 | |
| 3609 | /* Initialise the string catenation. */ |
| 3610 | luaL_buffinit(L, &appctx->b); |
| 3611 | |
| 3612 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3613 | } |
| 3614 | |
| 3615 | /* If expected data not yet available, it returns a yield. This function |
| 3616 | * consumes the data in the buffer. It returns a string containing the |
| 3617 | * data. This string can be empty. |
| 3618 | */ |
| 3619 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3620 | { |
| 3621 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3622 | struct stream_interface *si = appctx->appctx->owner; |
| 3623 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3624 | int ret; |
| 3625 | char *blk1; |
| 3626 | int len1; |
| 3627 | char *blk2; |
| 3628 | int len2; |
| 3629 | |
| 3630 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3631 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3632 | |
| 3633 | /* Data not yet avalaible. return yield. */ |
| 3634 | if (ret == 0) { |
| 3635 | si_applet_cant_get(si); |
| 3636 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3637 | } |
| 3638 | |
| 3639 | /* End of data: commit the total strings and return. */ |
| 3640 | if (ret < 0) { |
| 3641 | luaL_pushresult(&appctx->b); |
| 3642 | return 1; |
| 3643 | } |
| 3644 | |
| 3645 | /* Ensure that the block 2 length is usable. */ |
| 3646 | if (ret == 1) |
| 3647 | len2 = 0; |
| 3648 | |
| 3649 | if (len == -1) { |
| 3650 | |
| 3651 | /* If len == -1, catenate all the data avalaile and |
| 3652 | * yield because we want to get all the data until |
| 3653 | * the end of data stream. |
| 3654 | */ |
| 3655 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3656 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3657 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3658 | si_applet_cant_get(si); |
| 3659 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3660 | |
| 3661 | } else { |
| 3662 | |
| 3663 | /* Copy the fisrt block caping to the length required. */ |
| 3664 | if (len1 > len) |
| 3665 | len1 = len; |
| 3666 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3667 | len -= len1; |
| 3668 | |
| 3669 | /* Copy the second block. */ |
| 3670 | if (len2 > len) |
| 3671 | len2 = len; |
| 3672 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3673 | len -= len2; |
| 3674 | |
| 3675 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3676 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3677 | |
| 3678 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3679 | if (len > 0) { |
| 3680 | lua_pushinteger(L, len); |
| 3681 | lua_replace(L, 2); |
| 3682 | si_applet_cant_get(si); |
| 3683 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3684 | } |
| 3685 | |
| 3686 | /* return the result. */ |
| 3687 | luaL_pushresult(&appctx->b); |
| 3688 | return 1; |
| 3689 | } |
| 3690 | |
| 3691 | /* we never executes this */ |
| 3692 | hlua_pusherror(L, "Lua: internal error"); |
| 3693 | WILL_LJMP(lua_error(L)); |
| 3694 | return 0; |
| 3695 | } |
| 3696 | |
| 3697 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3698 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3699 | { |
| 3700 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3701 | int len = -1; |
| 3702 | |
| 3703 | if (lua_gettop(L) > 2) |
| 3704 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3705 | if (lua_gettop(L) >= 2) { |
| 3706 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3707 | lua_pop(L, 1); |
| 3708 | } |
| 3709 | |
| 3710 | /* Confirm or set the required length */ |
| 3711 | lua_pushinteger(L, len); |
| 3712 | |
| 3713 | /* Initialise the string catenation. */ |
| 3714 | luaL_buffinit(L, &appctx->b); |
| 3715 | |
| 3716 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3717 | } |
| 3718 | |
| 3719 | /* Append data in the output side of the buffer. This data is immediatly |
| 3720 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3721 | * cannot contains the data, the function yield. The function returns -1 |
| 3722 | * if the channel is closed. |
| 3723 | */ |
| 3724 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3725 | { |
| 3726 | size_t len; |
| 3727 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3728 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3729 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3730 | struct stream_interface *si = appctx->appctx->owner; |
| 3731 | struct channel *chn = si_ic(si); |
| 3732 | int max; |
| 3733 | |
| 3734 | /* Get the max amount of data which can write as input in the channel. */ |
| 3735 | max = channel_recv_max(chn); |
| 3736 | if (max > (len - l)) |
| 3737 | max = len - l; |
| 3738 | |
| 3739 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3740 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3741 | |
| 3742 | /* update counters. */ |
| 3743 | l += max; |
| 3744 | lua_pop(L, 1); |
| 3745 | lua_pushinteger(L, l); |
| 3746 | |
| 3747 | /* If some data is not send, declares the situation to the |
| 3748 | * applet, and returns a yield. |
| 3749 | */ |
| 3750 | if (l < len) { |
| 3751 | si_applet_cant_put(si); |
| 3752 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
| 3753 | } |
| 3754 | |
| 3755 | return 1; |
| 3756 | } |
| 3757 | |
| 3758 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3759 | * yield the LUA process, and resume it without checking the |
| 3760 | * input arguments. |
| 3761 | */ |
| 3762 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3763 | { |
| 3764 | MAY_LJMP(check_args(L, 2, "send")); |
| 3765 | lua_pushinteger(L, 0); |
| 3766 | |
| 3767 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3768 | } |
| 3769 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3770 | /* |
| 3771 | * |
| 3772 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3773 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3774 | * |
| 3775 | * |
| 3776 | */ |
| 3777 | |
| 3778 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3779 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3780 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3781 | __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] | 3782 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3783 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3784 | } |
| 3785 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3786 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3787 | * according with a current TXN. |
| 3788 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3789 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3790 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3791 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3792 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3793 | struct stream_interface *si = ctx->owner; |
| 3794 | struct stream *s = si_strm(si); |
| 3795 | struct proxy *px = s->be; |
| 3796 | struct http_txn *txn = s->txn; |
| 3797 | const char *path; |
| 3798 | const char *end; |
| 3799 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3800 | |
| 3801 | /* Check stack size. */ |
| 3802 | if (!lua_checkstack(L, 3)) |
| 3803 | return 0; |
| 3804 | |
| 3805 | /* Create the object: obj[0] = userdata. |
| 3806 | * Note that the base of the Converters object is the |
| 3807 | * same than the TXN object. |
| 3808 | */ |
| 3809 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3810 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3811 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3812 | appctx->appctx = ctx; |
| 3813 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3814 | 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] | 3815 | appctx->htxn.s = s; |
| 3816 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3817 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3818 | /* Create the "f" field that contains a list of fetches. */ |
| 3819 | lua_pushstring(L, "f"); |
| 3820 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3821 | return 0; |
| 3822 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3823 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3824 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3825 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3826 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3827 | return 0; |
| 3828 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3829 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3830 | /* Create the "c" field that contains a list of converters. */ |
| 3831 | lua_pushstring(L, "c"); |
| 3832 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3833 | return 0; |
| 3834 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3835 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3836 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3837 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3838 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3839 | return 0; |
| 3840 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3841 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3842 | /* Stores the request method. */ |
| 3843 | lua_pushstring(L, "method"); |
| 3844 | lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l); |
| 3845 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3846 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3847 | /* Stores the http version. */ |
| 3848 | lua_pushstring(L, "version"); |
| 3849 | lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
| 3850 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3851 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3852 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3853 | * the array on the top of the stack. |
| 3854 | */ |
| 3855 | lua_pushstring(L, "headers"); |
| 3856 | htxn.s = s; |
| 3857 | htxn.p = px; |
| 3858 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3859 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3860 | return 0; |
| 3861 | lua_settable(L, -3); |
| 3862 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3863 | /* Get path and qs */ |
| 3864 | path = http_get_path(txn); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3865 | if (path) { |
| 3866 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 3867 | p = path; |
| 3868 | while (p < end && *p != '?') |
| 3869 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3870 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3871 | /* Stores the request path. */ |
| 3872 | lua_pushstring(L, "path"); |
| 3873 | lua_pushlstring(L, path, p - path); |
| 3874 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3875 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3876 | /* Stores the query string. */ |
| 3877 | lua_pushstring(L, "qs"); |
| 3878 | if (*p == '?') |
| 3879 | p++; |
| 3880 | lua_pushlstring(L, p, end - p); |
| 3881 | lua_settable(L, -3); |
| 3882 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3883 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3884 | /* Stores the request path. */ |
| 3885 | lua_pushstring(L, "length"); |
| 3886 | lua_pushinteger(L, txn->req.body_len); |
| 3887 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3888 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3889 | /* Create an array of HTTP request headers. */ |
| 3890 | lua_pushstring(L, "headers"); |
| 3891 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3892 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3893 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3894 | /* Create an empty array of HTTP request headers. */ |
| 3895 | lua_pushstring(L, "response"); |
| 3896 | lua_newtable(L); |
| 3897 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3898 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3899 | /* Pop a class stream metatable and affect it to the table. */ |
| 3900 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3901 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3902 | |
| 3903 | return 1; |
| 3904 | } |
| 3905 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3906 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3907 | { |
| 3908 | struct hlua_appctx *appctx; |
| 3909 | struct stream *s; |
| 3910 | const char *name; |
| 3911 | size_t len; |
| 3912 | struct sample smp; |
| 3913 | |
| 3914 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3915 | |
| 3916 | /* It is useles to retrieve the stream, but this function |
| 3917 | * runs only in a stream context. |
| 3918 | */ |
| 3919 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3920 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3921 | s = appctx->htxn.s; |
| 3922 | |
| 3923 | /* Converts the third argument in a sample. */ |
| 3924 | hlua_lua2smp(L, 3, &smp); |
| 3925 | |
| 3926 | /* Store the sample in a variable. */ |
| 3927 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3928 | vars_set_by_name(name, len, &smp); |
| 3929 | return 0; |
| 3930 | } |
| 3931 | |
| 3932 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 3933 | { |
| 3934 | struct hlua_appctx *appctx; |
| 3935 | struct stream *s; |
| 3936 | const char *name; |
| 3937 | size_t len; |
| 3938 | struct sample smp; |
| 3939 | |
| 3940 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3941 | |
| 3942 | /* It is useles to retrieve the stream, but this function |
| 3943 | * runs only in a stream context. |
| 3944 | */ |
| 3945 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3946 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3947 | s = appctx->htxn.s; |
| 3948 | |
| 3949 | /* Unset the variable. */ |
| 3950 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3951 | vars_unset_by_name(name, len, &smp); |
| 3952 | return 0; |
| 3953 | } |
| 3954 | |
| 3955 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 3956 | { |
| 3957 | struct hlua_appctx *appctx; |
| 3958 | struct stream *s; |
| 3959 | const char *name; |
| 3960 | size_t len; |
| 3961 | struct sample smp; |
| 3962 | |
| 3963 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3964 | |
| 3965 | /* It is useles to retrieve the stream, but this function |
| 3966 | * runs only in a stream context. |
| 3967 | */ |
| 3968 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3969 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3970 | s = appctx->htxn.s; |
| 3971 | |
| 3972 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3973 | if (!vars_get_by_name(name, len, &smp)) { |
| 3974 | lua_pushnil(L); |
| 3975 | return 1; |
| 3976 | } |
| 3977 | |
| 3978 | return hlua_smp2lua(L, &smp); |
| 3979 | } |
| 3980 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3981 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 3982 | { |
| 3983 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3984 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3985 | struct hlua *hlua; |
| 3986 | |
| 3987 | /* 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] | 3988 | if (!s->hlua) |
| 3989 | return 0; |
| 3990 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3991 | |
| 3992 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3993 | |
| 3994 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3995 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3996 | |
| 3997 | /* Get and store new value. */ |
| 3998 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3999 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4000 | |
| 4001 | return 0; |
| 4002 | } |
| 4003 | |
| 4004 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4005 | { |
| 4006 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4007 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4008 | struct hlua *hlua; |
| 4009 | |
| 4010 | /* 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] | 4011 | if (!s->hlua) { |
| 4012 | lua_pushnil(L); |
| 4013 | return 1; |
| 4014 | } |
| 4015 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4016 | |
| 4017 | /* Push configuration index in the stack. */ |
| 4018 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4019 | |
| 4020 | return 1; |
| 4021 | } |
| 4022 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4023 | /* If expected data not yet available, it returns a yield. This function |
| 4024 | * consumes the data in the buffer. It returns a string containing the |
| 4025 | * data. This string can be empty. |
| 4026 | */ |
| 4027 | __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] | 4028 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4029 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4030 | struct stream_interface *si = appctx->appctx->owner; |
| 4031 | struct channel *chn = si_ic(si); |
| 4032 | int ret; |
| 4033 | char *blk1; |
| 4034 | int len1; |
| 4035 | char *blk2; |
| 4036 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4037 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4038 | /* Maybe we cant send a 100-continue ? */ |
| 4039 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4040 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4041 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4042 | * big for the buffers. We cant send anything. So, we ignoring |
| 4043 | * the error, considers that the 100-continue is sent, and try |
| 4044 | * to receive. |
| 4045 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4046 | */ |
| 4047 | if (ret == -1) { |
| 4048 | si_applet_cant_put(si); |
| 4049 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 4050 | } |
| 4051 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4052 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4053 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4054 | /* Check for the end of the data. */ |
| 4055 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4056 | luaL_pushresult(&appctx->b); |
| 4057 | return 1; |
| 4058 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4059 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4060 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4061 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4062 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4063 | /* Data not yet avalaible. return yield. */ |
| 4064 | if (ret == 0) { |
| 4065 | si_applet_cant_get(si); |
| 4066 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 4067 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4068 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4069 | /* End of data: commit the total strings and return. */ |
| 4070 | if (ret < 0) { |
| 4071 | luaL_pushresult(&appctx->b); |
| 4072 | return 1; |
| 4073 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4074 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4075 | /* Ensure that the block 2 length is usable. */ |
| 4076 | if (ret == 1) |
| 4077 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4078 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4079 | /* Copy the fisrt block caping to the length required. */ |
| 4080 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4081 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4082 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4083 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4084 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4085 | /* Copy the second block. */ |
| 4086 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4087 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4088 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4089 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4090 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4091 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4092 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4093 | luaL_pushresult(&appctx->b); |
| 4094 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4095 | } |
| 4096 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4097 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 4098 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4099 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4100 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4101 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4102 | /* Initialise the string catenation. */ |
| 4103 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4104 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4105 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4106 | } |
| 4107 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4108 | /* If expected data not yet available, it returns a yield. This function |
| 4109 | * consumes the data in the buffer. It returns a string containing the |
| 4110 | * data. This string can be empty. |
| 4111 | */ |
| 4112 | __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] | 4113 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4114 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4115 | struct stream_interface *si = appctx->appctx->owner; |
| 4116 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4117 | struct channel *chn = si_ic(si); |
| 4118 | int ret; |
| 4119 | char *blk1; |
| 4120 | int len1; |
| 4121 | char *blk2; |
| 4122 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4123 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4124 | /* Maybe we cant send a 100-continue ? */ |
| 4125 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4126 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4127 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4128 | * big for the buffers. We cant send anything. So, we ignoring |
| 4129 | * the error, considers that the 100-continue is sent, and try |
| 4130 | * to receive. |
| 4131 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4132 | */ |
| 4133 | if (ret == -1) { |
| 4134 | si_applet_cant_put(si); |
| 4135 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4136 | } |
| 4137 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4138 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4139 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4140 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4141 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4142 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4143 | /* Data not yet avalaible. return yield. */ |
| 4144 | if (ret == 0) { |
| 4145 | si_applet_cant_get(si); |
| 4146 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4147 | } |
| 4148 | |
| 4149 | /* End of data: commit the total strings and return. */ |
| 4150 | if (ret < 0) { |
| 4151 | luaL_pushresult(&appctx->b); |
| 4152 | return 1; |
| 4153 | } |
| 4154 | |
| 4155 | /* Ensure that the block 2 length is usable. */ |
| 4156 | if (ret == 1) |
| 4157 | len2 = 0; |
| 4158 | |
| 4159 | /* Copy the fisrt block caping to the length required. */ |
| 4160 | if (len1 > len) |
| 4161 | len1 = len; |
| 4162 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4163 | len -= len1; |
| 4164 | |
| 4165 | /* Copy the second block. */ |
| 4166 | if (len2 > len) |
| 4167 | len2 = len; |
| 4168 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4169 | len -= len2; |
| 4170 | |
| 4171 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4172 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4173 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4174 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4175 | |
| 4176 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 4177 | if (len > 0) { |
| 4178 | lua_pushinteger(L, len); |
| 4179 | lua_replace(L, 2); |
| 4180 | si_applet_cant_get(si); |
| 4181 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4182 | } |
| 4183 | |
| 4184 | /* return the result. */ |
| 4185 | luaL_pushresult(&appctx->b); |
| 4186 | return 1; |
| 4187 | } |
| 4188 | |
| 4189 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 4190 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4191 | { |
| 4192 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4193 | int len = -1; |
| 4194 | |
| 4195 | /* Check arguments. */ |
| 4196 | if (lua_gettop(L) > 2) |
| 4197 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4198 | if (lua_gettop(L) >= 2) { |
| 4199 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4200 | lua_pop(L, 1); |
| 4201 | } |
| 4202 | |
| 4203 | /* Check the required length */ |
| 4204 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4205 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4206 | lua_pushinteger(L, len); |
| 4207 | |
| 4208 | /* Initialise the string catenation. */ |
| 4209 | luaL_buffinit(L, &appctx->b); |
| 4210 | |
| 4211 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4212 | } |
| 4213 | |
| 4214 | /* Append data in the output side of the buffer. This data is immediatly |
| 4215 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 4216 | * cannot contains the data, the function yield. The function returns -1 |
| 4217 | * if the channel is closed. |
| 4218 | */ |
| 4219 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4220 | { |
| 4221 | size_t len; |
| 4222 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4223 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4224 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4225 | struct stream_interface *si = appctx->appctx->owner; |
| 4226 | struct channel *chn = si_ic(si); |
| 4227 | int max; |
| 4228 | |
| 4229 | /* Get the max amount of data which can write as input in the channel. */ |
| 4230 | max = channel_recv_max(chn); |
| 4231 | if (max > (len - l)) |
| 4232 | max = len - l; |
| 4233 | |
| 4234 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4235 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4236 | |
| 4237 | /* update counters. */ |
| 4238 | l += max; |
| 4239 | lua_pop(L, 1); |
| 4240 | lua_pushinteger(L, l); |
| 4241 | |
| 4242 | /* If some data is not send, declares the situation to the |
| 4243 | * applet, and returns a yield. |
| 4244 | */ |
| 4245 | if (l < len) { |
| 4246 | si_applet_cant_put(si); |
| 4247 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
| 4248 | } |
| 4249 | |
| 4250 | return 1; |
| 4251 | } |
| 4252 | |
| 4253 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4254 | * yield the LUA process, and resume it without checking the |
| 4255 | * input arguments. |
| 4256 | */ |
| 4257 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4258 | { |
| 4259 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4260 | size_t len; |
| 4261 | char hex[10]; |
| 4262 | |
| 4263 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4264 | |
| 4265 | /* If transfer encoding chunked is selected, we surround the data |
| 4266 | * by chunk data. |
| 4267 | */ |
| 4268 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4269 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4270 | lua_pushfstring(L, "%s\r\n", hex); |
| 4271 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4272 | lua_pushstring(L, "\r\n"); |
| 4273 | lua_concat(L, 3); |
| 4274 | } |
| 4275 | |
| 4276 | /* This interger is used for followinf the amount of data sent. */ |
| 4277 | lua_pushinteger(L, 0); |
| 4278 | |
| 4279 | /* We want to send some data. Headers must be sent. */ |
| 4280 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4281 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4282 | WILL_LJMP(lua_error(L)); |
| 4283 | } |
| 4284 | |
| 4285 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4286 | } |
| 4287 | |
| 4288 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4289 | { |
| 4290 | const char *name; |
| 4291 | int ret; |
| 4292 | |
| 4293 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4294 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4295 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4296 | |
| 4297 | /* Push in the stack the "response" entry. */ |
| 4298 | ret = lua_getfield(L, 1, "response"); |
| 4299 | if (ret != LUA_TTABLE) { |
| 4300 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4301 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4302 | WILL_LJMP(lua_error(L)); |
| 4303 | } |
| 4304 | |
| 4305 | /* check if the header is already registered if it is not |
| 4306 | * the case, register it. |
| 4307 | */ |
| 4308 | ret = lua_getfield(L, -1, name); |
| 4309 | if (ret == LUA_TNIL) { |
| 4310 | |
| 4311 | /* Entry not found. */ |
| 4312 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4313 | |
| 4314 | /* Insert the new header name in the array in the top of the stack. |
| 4315 | * It left the new array in the top of the stack. |
| 4316 | */ |
| 4317 | lua_newtable(L); |
| 4318 | lua_pushvalue(L, 2); |
| 4319 | lua_pushvalue(L, -2); |
| 4320 | lua_settable(L, -4); |
| 4321 | |
| 4322 | } else if (ret != LUA_TTABLE) { |
| 4323 | |
| 4324 | /* corruption error. */ |
| 4325 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4326 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4327 | WILL_LJMP(lua_error(L)); |
| 4328 | } |
| 4329 | |
| 4330 | /* Now the top od thestack is an array of values. We push |
| 4331 | * the header value as new entry. |
| 4332 | */ |
| 4333 | lua_pushvalue(L, 3); |
| 4334 | ret = lua_rawlen(L, -2); |
| 4335 | lua_rawseti(L, -2, ret + 1); |
| 4336 | lua_pushboolean(L, 1); |
| 4337 | return 1; |
| 4338 | } |
| 4339 | |
| 4340 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4341 | { |
| 4342 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4343 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4344 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4345 | |
| 4346 | if (status < 100 || status > 599) { |
| 4347 | lua_pushboolean(L, 0); |
| 4348 | return 1; |
| 4349 | } |
| 4350 | |
| 4351 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4352 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4353 | lua_pushboolean(L, 1); |
| 4354 | return 1; |
| 4355 | } |
| 4356 | |
| 4357 | /* We will build the status line and the headers of the HTTP response. |
| 4358 | * We will try send at once if its not possible, we give back the hand |
| 4359 | * waiting for more room. |
| 4360 | */ |
| 4361 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4362 | { |
| 4363 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4364 | struct stream_interface *si = appctx->appctx->owner; |
| 4365 | struct channel *chn = si_ic(si); |
| 4366 | int ret; |
| 4367 | size_t len; |
| 4368 | const char *msg; |
| 4369 | |
| 4370 | /* Get the message as the first argument on the stack. */ |
| 4371 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4372 | |
| 4373 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4374 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4375 | |
| 4376 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4377 | * big for the buffers. |
| 4378 | */ |
| 4379 | if (ret == -2 || ret == -3) { |
| 4380 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4381 | WILL_LJMP(lua_error(L)); |
| 4382 | } |
| 4383 | |
| 4384 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4385 | if (ret == -1) { |
| 4386 | si_applet_cant_put(si); |
| 4387 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
| 4388 | } |
| 4389 | |
| 4390 | /* Headers sent, set the flag. */ |
| 4391 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4392 | return 0; |
| 4393 | } |
| 4394 | |
| 4395 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4396 | { |
| 4397 | struct chunk *tmp = get_trash_chunk(); |
| 4398 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4399 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4400 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4401 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4402 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4403 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4404 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4405 | int hdr_chunked = 0; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4406 | const char *reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4407 | |
| 4408 | if (reason == NULL) |
| 4409 | reason = get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4410 | |
| 4411 | /* Use the same http version than the request. */ |
| 4412 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4413 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4414 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4415 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4416 | |
| 4417 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4418 | lua_pushvalue(L, 0); |
| 4419 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4420 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4421 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4422 | WILL_LJMP(lua_error(L)); |
| 4423 | } |
| 4424 | |
| 4425 | /* Browse the list of headers. */ |
| 4426 | lua_pushnil(L); |
| 4427 | while(lua_next(L, -2) != 0) { |
| 4428 | |
| 4429 | /* We expect a string as -2. */ |
| 4430 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4431 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4432 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4433 | lua_typename(L, lua_type(L, -2))); |
| 4434 | WILL_LJMP(lua_error(L)); |
| 4435 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4436 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4437 | |
| 4438 | /* We expect an array as -1. */ |
| 4439 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4440 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4441 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4442 | name, |
| 4443 | lua_typename(L, lua_type(L, -1))); |
| 4444 | WILL_LJMP(lua_error(L)); |
| 4445 | } |
| 4446 | |
| 4447 | /* Browse the table who is on the top of the stack. */ |
| 4448 | lua_pushnil(L); |
| 4449 | while(lua_next(L, -2) != 0) { |
| 4450 | |
| 4451 | /* We expect a number as -2. */ |
| 4452 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4453 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4454 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4455 | name, |
| 4456 | lua_typename(L, lua_type(L, -2))); |
| 4457 | WILL_LJMP(lua_error(L)); |
| 4458 | } |
| 4459 | id = lua_tointeger(L, -2); |
| 4460 | |
| 4461 | /* We expect a string as -2. */ |
| 4462 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4463 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4464 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4465 | name, id, |
| 4466 | lua_typename(L, lua_type(L, -1))); |
| 4467 | WILL_LJMP(lua_error(L)); |
| 4468 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4469 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4470 | |
| 4471 | /* Catenate a new header. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4472 | if (tmp->len + name_len + 2 + value_len + 2 < tmp->size) { |
| 4473 | memcpy(tmp->str + tmp->len, name, name_len); |
| 4474 | tmp->len += name_len; |
| 4475 | tmp->str[tmp->len++] = ':'; |
| 4476 | tmp->str[tmp->len++] = ' '; |
| 4477 | |
| 4478 | memcpy(tmp->str + tmp->len, value, value_len); |
| 4479 | tmp->len += value_len; |
| 4480 | tmp->str[tmp->len++] = '\r'; |
| 4481 | tmp->str[tmp->len++] = '\n'; |
| 4482 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4483 | |
| 4484 | /* Protocol checks. */ |
| 4485 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4486 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4487 | * is done without control. If it contains a bad value, |
| 4488 | * the content-length remains negative so that we can |
| 4489 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4490 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4491 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4492 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4493 | |
| 4494 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4495 | if (name_len == 17 && value_len == 7 && |
| 4496 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4497 | strcasecmp("chunked", value) == 0) |
| 4498 | hdr_chunked = 1; |
| 4499 | |
| 4500 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4501 | lua_pop(L, 1); |
| 4502 | } |
| 4503 | |
| 4504 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4505 | lua_pop(L, 1); |
| 4506 | } |
| 4507 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4508 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4509 | * and the status code implies the presence of a message body, we must |
| 4510 | * announce a transfer encoding chunked. This is required by haproxy |
| 4511 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4512 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4513 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4514 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4515 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 4516 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4517 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4518 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4519 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4520 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4521 | } |
| 4522 | |
| 4523 | /* Finalize headers. */ |
| 4524 | chunk_appendf(tmp, "\r\n"); |
| 4525 | |
| 4526 | /* Remove the last entry and the array of headers */ |
| 4527 | lua_pop(L, 2); |
| 4528 | |
| 4529 | /* Push the headers block. */ |
| 4530 | lua_pushlstring(L, tmp->str, tmp->len); |
| 4531 | |
| 4532 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4533 | } |
| 4534 | |
| 4535 | /* |
| 4536 | * |
| 4537 | * |
| 4538 | * Class HTTP |
| 4539 | * |
| 4540 | * |
| 4541 | */ |
| 4542 | |
| 4543 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4544 | * a class stream, otherwise it throws an error. |
| 4545 | */ |
| 4546 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4547 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4548 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4549 | } |
| 4550 | |
| 4551 | /* This function creates and push in the stack a HTTP object |
| 4552 | * according with a current TXN. |
| 4553 | */ |
| 4554 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4555 | { |
| 4556 | struct hlua_txn *htxn; |
| 4557 | |
| 4558 | /* Check stack size. */ |
| 4559 | if (!lua_checkstack(L, 3)) |
| 4560 | return 0; |
| 4561 | |
| 4562 | /* Create the object: obj[0] = userdata. |
| 4563 | * Note that the base of the Converters object is the |
| 4564 | * same than the TXN object. |
| 4565 | */ |
| 4566 | lua_newtable(L); |
| 4567 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4568 | lua_rawseti(L, -2, 0); |
| 4569 | |
| 4570 | htxn->s = txn->s; |
| 4571 | htxn->p = txn->p; |
| 4572 | |
| 4573 | /* Pop a class stream metatable and affect it to the table. */ |
| 4574 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4575 | lua_setmetatable(L, -2); |
| 4576 | |
| 4577 | return 1; |
| 4578 | } |
| 4579 | |
| 4580 | /* This function creates ans returns an array of HTTP headers. |
| 4581 | * This function does not fails. It is used as wrapper with the |
| 4582 | * 2 following functions. |
| 4583 | */ |
| 4584 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4585 | { |
| 4586 | const char *cur_ptr, *cur_next, *p; |
| 4587 | int old_idx, cur_idx; |
| 4588 | struct hdr_idx_elem *cur_hdr; |
| 4589 | const char *hn, *hv; |
| 4590 | int hnl, hvl; |
| 4591 | int type; |
| 4592 | const char *in; |
| 4593 | char *out; |
| 4594 | int len; |
| 4595 | |
| 4596 | /* Create the table. */ |
| 4597 | lua_newtable(L); |
| 4598 | |
| 4599 | if (!htxn->s->txn) |
| 4600 | return 1; |
| 4601 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4602 | /* Check if a valid response is parsed */ |
| 4603 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4604 | return 1; |
| 4605 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4606 | /* Build array of headers. */ |
| 4607 | old_idx = 0; |
| 4608 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 4609 | |
| 4610 | while (1) { |
| 4611 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4612 | if (!cur_idx) |
| 4613 | break; |
| 4614 | old_idx = cur_idx; |
| 4615 | |
| 4616 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4617 | cur_ptr = cur_next; |
| 4618 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4619 | |
| 4620 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4621 | * and the next header starts at cur_next. We'll check |
| 4622 | * this header in the list as well as against the default |
| 4623 | * rule. |
| 4624 | */ |
| 4625 | |
| 4626 | /* look for ': *'. */ |
| 4627 | hn = cur_ptr; |
| 4628 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4629 | if (p >= cur_ptr+cur_hdr->len) |
| 4630 | continue; |
| 4631 | hnl = p - hn; |
| 4632 | p++; |
| 4633 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4634 | p++; |
| 4635 | if (p >= cur_ptr+cur_hdr->len) |
| 4636 | continue; |
| 4637 | hv = p; |
| 4638 | hvl = cur_ptr+cur_hdr->len-p; |
| 4639 | |
| 4640 | /* Lowercase the key. Don't check the size of trash, it have |
| 4641 | * the size of one buffer and the input data contains in one |
| 4642 | * buffer. |
| 4643 | */ |
| 4644 | out = trash.str; |
| 4645 | for (in=hn; in<hn+hnl; in++, out++) |
| 4646 | *out = tolower(*in); |
| 4647 | *out = '\0'; |
| 4648 | |
| 4649 | /* Check for existing entry: |
| 4650 | * assume that the table is on the top of the stack, and |
| 4651 | * push the key in the stack, the function lua_gettable() |
| 4652 | * perform the lookup. |
| 4653 | */ |
| 4654 | lua_pushlstring(L, trash.str, hnl); |
| 4655 | lua_gettable(L, -2); |
| 4656 | type = lua_type(L, -1); |
| 4657 | |
| 4658 | switch (type) { |
| 4659 | case LUA_TNIL: |
| 4660 | /* Table not found, create it. */ |
| 4661 | lua_pop(L, 1); /* remove the nil value. */ |
| 4662 | lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */ |
| 4663 | lua_newtable(L); /* create and push empty table. */ |
| 4664 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4665 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4666 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4667 | break; |
| 4668 | |
| 4669 | case LUA_TTABLE: |
| 4670 | /* Entry found: push the value in the table. */ |
| 4671 | len = lua_rawlen(L, -1); |
| 4672 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4673 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4674 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4675 | break; |
| 4676 | |
| 4677 | default: |
| 4678 | /* Other cases are errors. */ |
| 4679 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4680 | WILL_LJMP(lua_error(L)); |
| 4681 | } |
| 4682 | } |
| 4683 | |
| 4684 | return 1; |
| 4685 | } |
| 4686 | |
| 4687 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4688 | { |
| 4689 | struct hlua_txn *htxn; |
| 4690 | |
| 4691 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4692 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4693 | |
| 4694 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4695 | } |
| 4696 | |
| 4697 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4698 | { |
| 4699 | struct hlua_txn *htxn; |
| 4700 | |
| 4701 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4702 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4703 | |
| 4704 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4705 | } |
| 4706 | |
| 4707 | /* This function replace full header, or just a value in |
| 4708 | * the request or in the response. It is a wrapper fir the |
| 4709 | * 4 following functions. |
| 4710 | */ |
| 4711 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4712 | struct http_msg *msg, int action) |
| 4713 | { |
| 4714 | size_t name_len; |
| 4715 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4716 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4717 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4718 | struct my_regex re; |
| 4719 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4720 | /* Check if a valid response is parsed */ |
| 4721 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4722 | return 0; |
| 4723 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4724 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4725 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4726 | |
| 4727 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4728 | regex_free(&re); |
| 4729 | return 0; |
| 4730 | } |
| 4731 | |
| 4732 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4733 | { |
| 4734 | struct hlua_txn *htxn; |
| 4735 | |
| 4736 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4737 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4738 | |
| 4739 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4740 | } |
| 4741 | |
| 4742 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4743 | { |
| 4744 | struct hlua_txn *htxn; |
| 4745 | |
| 4746 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4747 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4748 | |
| 4749 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4750 | } |
| 4751 | |
| 4752 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4753 | { |
| 4754 | struct hlua_txn *htxn; |
| 4755 | |
| 4756 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4757 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4758 | |
| 4759 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4760 | } |
| 4761 | |
| 4762 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4763 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4764 | struct hlua_txn *htxn; |
| 4765 | |
| 4766 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4767 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4768 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4769 | 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] | 4770 | } |
| 4771 | |
| 4772 | /* This function deletes all the occurences of an header. |
| 4773 | * It is a wrapper for the 2 following functions. |
| 4774 | */ |
| 4775 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4776 | { |
| 4777 | size_t len; |
| 4778 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4779 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4780 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4781 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4782 | /* Check if a valid response is parsed */ |
| 4783 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4784 | return 0; |
| 4785 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4786 | ctx.idx = 0; |
| 4787 | while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) |
| 4788 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4789 | return 0; |
| 4790 | } |
| 4791 | |
| 4792 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4793 | { |
| 4794 | struct hlua_txn *htxn; |
| 4795 | |
| 4796 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4797 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4798 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4799 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4800 | } |
| 4801 | |
| 4802 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4803 | { |
| 4804 | struct hlua_txn *htxn; |
| 4805 | |
| 4806 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4807 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4808 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4809 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4810 | } |
| 4811 | |
| 4812 | /* This function adds an header. It is a wrapper used by |
| 4813 | * the 2 following functions. |
| 4814 | */ |
| 4815 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4816 | { |
| 4817 | size_t name_len; |
| 4818 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4819 | size_t value_len; |
| 4820 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4821 | char *p; |
| 4822 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4823 | /* Check if a valid message is parsed */ |
| 4824 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4825 | return 0; |
| 4826 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4827 | /* Check length. */ |
| 4828 | trash.len = value_len + name_len + 2; |
| 4829 | if (trash.len > trash.size) |
| 4830 | return 0; |
| 4831 | |
| 4832 | /* Creates the header string. */ |
| 4833 | p = trash.str; |
| 4834 | memcpy(p, name, name_len); |
| 4835 | p += name_len; |
| 4836 | *p = ':'; |
| 4837 | p++; |
| 4838 | *p = ' '; |
| 4839 | p++; |
| 4840 | memcpy(p, value, value_len); |
| 4841 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4842 | 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] | 4843 | trash.str, trash.len) != 0); |
| 4844 | |
| 4845 | return 0; |
| 4846 | } |
| 4847 | |
| 4848 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4849 | { |
| 4850 | struct hlua_txn *htxn; |
| 4851 | |
| 4852 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4853 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4854 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4855 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4856 | } |
| 4857 | |
| 4858 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4859 | { |
| 4860 | struct hlua_txn *htxn; |
| 4861 | |
| 4862 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4863 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4864 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4865 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4866 | } |
| 4867 | |
| 4868 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4869 | { |
| 4870 | struct hlua_txn *htxn; |
| 4871 | |
| 4872 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4873 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4874 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4875 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4876 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4880 | { |
| 4881 | struct hlua_txn *htxn; |
| 4882 | |
| 4883 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4884 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4885 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4886 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4887 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4888 | } |
| 4889 | |
| 4890 | /* This function set the method. */ |
| 4891 | static int hlua_http_req_set_meth(lua_State *L) |
| 4892 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4893 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4894 | size_t name_len; |
| 4895 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4896 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4897 | /* Check if a valid request is parsed */ |
| 4898 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4899 | lua_pushboolean(L, 0); |
| 4900 | return 1; |
| 4901 | } |
| 4902 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4903 | 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] | 4904 | return 1; |
| 4905 | } |
| 4906 | |
| 4907 | /* This function set the method. */ |
| 4908 | static int hlua_http_req_set_path(lua_State *L) |
| 4909 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4910 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4911 | size_t name_len; |
| 4912 | 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] | 4913 | |
| 4914 | /* Check if a valid request is parsed */ |
| 4915 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4916 | lua_pushboolean(L, 0); |
| 4917 | return 1; |
| 4918 | } |
| 4919 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4920 | 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] | 4921 | return 1; |
| 4922 | } |
| 4923 | |
| 4924 | /* This function set the query-string. */ |
| 4925 | static int hlua_http_req_set_query(lua_State *L) |
| 4926 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4927 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4928 | size_t name_len; |
| 4929 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4930 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4931 | /* Check if a valid request is parsed */ |
| 4932 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4933 | lua_pushboolean(L, 0); |
| 4934 | return 1; |
| 4935 | } |
| 4936 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4937 | /* Check length. */ |
| 4938 | if (name_len > trash.size - 1) { |
| 4939 | lua_pushboolean(L, 0); |
| 4940 | return 1; |
| 4941 | } |
| 4942 | |
| 4943 | /* Add the mark question as prefix. */ |
| 4944 | chunk_reset(&trash); |
| 4945 | trash.str[trash.len++] = '?'; |
| 4946 | memcpy(trash.str + trash.len, name, name_len); |
| 4947 | trash.len += name_len; |
| 4948 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4949 | 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] | 4950 | return 1; |
| 4951 | } |
| 4952 | |
| 4953 | /* This function set the uri. */ |
| 4954 | static int hlua_http_req_set_uri(lua_State *L) |
| 4955 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4956 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4957 | size_t name_len; |
| 4958 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4959 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4960 | /* Check if a valid request is parsed */ |
| 4961 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4962 | lua_pushboolean(L, 0); |
| 4963 | return 1; |
| 4964 | } |
| 4965 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4966 | 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] | 4967 | return 1; |
| 4968 | } |
| 4969 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4970 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4971 | static int hlua_http_res_set_status(lua_State *L) |
| 4972 | { |
| 4973 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4974 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4975 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4976 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4977 | /* Check if a valid response is parsed */ |
| 4978 | if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4979 | return 0; |
| 4980 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4981 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4982 | return 0; |
| 4983 | } |
| 4984 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4985 | /* |
| 4986 | * |
| 4987 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4988 | * Class TXN |
| 4989 | * |
| 4990 | * |
| 4991 | */ |
| 4992 | |
| 4993 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4994 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4995 | */ |
| 4996 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 4997 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4998 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4999 | } |
| 5000 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5001 | __LJMP static int hlua_set_var(lua_State *L) |
| 5002 | { |
| 5003 | struct hlua_txn *htxn; |
| 5004 | const char *name; |
| 5005 | size_t len; |
| 5006 | struct sample smp; |
| 5007 | |
| 5008 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5009 | |
| 5010 | /* It is useles to retrieve the stream, but this function |
| 5011 | * runs only in a stream context. |
| 5012 | */ |
| 5013 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5014 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5015 | |
| 5016 | /* Converts the third argument in a sample. */ |
| 5017 | hlua_lua2smp(L, 3, &smp); |
| 5018 | |
| 5019 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5020 | 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] | 5021 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5022 | return 0; |
| 5023 | } |
| 5024 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5025 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5026 | { |
| 5027 | struct hlua_txn *htxn; |
| 5028 | const char *name; |
| 5029 | size_t len; |
| 5030 | struct sample smp; |
| 5031 | |
| 5032 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5033 | |
| 5034 | /* It is useles to retrieve the stream, but this function |
| 5035 | * runs only in a stream context. |
| 5036 | */ |
| 5037 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5038 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5039 | |
| 5040 | /* Unset the variable. */ |
| 5041 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5042 | vars_unset_by_name(name, len, &smp); |
| 5043 | return 0; |
| 5044 | } |
| 5045 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5046 | __LJMP static int hlua_get_var(lua_State *L) |
| 5047 | { |
| 5048 | struct hlua_txn *htxn; |
| 5049 | const char *name; |
| 5050 | size_t len; |
| 5051 | struct sample smp; |
| 5052 | |
| 5053 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5054 | |
| 5055 | /* It is useles to retrieve the stream, but this function |
| 5056 | * runs only in a stream context. |
| 5057 | */ |
| 5058 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5059 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5060 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5061 | 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] | 5062 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5063 | lua_pushnil(L); |
| 5064 | return 1; |
| 5065 | } |
| 5066 | |
| 5067 | return hlua_smp2lua(L, &smp); |
| 5068 | } |
| 5069 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5070 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5071 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5072 | struct hlua *hlua; |
| 5073 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5074 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5075 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5076 | /* It is useles to retrieve the stream, but this function |
| 5077 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5078 | */ |
| 5079 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5080 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5081 | |
| 5082 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5083 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5084 | |
| 5085 | /* Get and store new value. */ |
| 5086 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5087 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5088 | |
| 5089 | return 0; |
| 5090 | } |
| 5091 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5092 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5093 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5094 | struct hlua *hlua; |
| 5095 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5096 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5097 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5098 | /* It is useles to retrieve the stream, but this function |
| 5099 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5100 | */ |
| 5101 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5102 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5103 | |
| 5104 | /* Push configuration index in the stack. */ |
| 5105 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5106 | |
| 5107 | return 1; |
| 5108 | } |
| 5109 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5110 | /* Create stack entry containing a class TXN. This function |
| 5111 | * return 0 if the stack does not contains free slots, |
| 5112 | * otherwise it returns 1. |
| 5113 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5114 | 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] | 5115 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5116 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5117 | |
| 5118 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5119 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5120 | return 0; |
| 5121 | |
| 5122 | /* NOTE: The allocation never fails. The failure |
| 5123 | * throw an error, and the function never returns. |
| 5124 | * if the throw is not avalaible, the process is aborted. |
| 5125 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5126 | /* Create the object: obj[0] = userdata. */ |
| 5127 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5128 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5129 | lua_rawseti(L, -2, 0); |
| 5130 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5131 | htxn->s = s; |
| 5132 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5133 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5134 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5135 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5136 | /* Create the "f" field that contains a list of fetches. */ |
| 5137 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5138 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5139 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5140 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5141 | |
| 5142 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5143 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5144 | 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] | 5145 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5146 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5147 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5148 | /* Create the "c" field that contains a list of converters. */ |
| 5149 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5150 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5151 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5152 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5153 | |
| 5154 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5155 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5156 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5157 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5158 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5159 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5160 | /* Create the "req" field that contains the request channel object. */ |
| 5161 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5162 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5163 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5164 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5165 | |
| 5166 | /* Create the "res" field that contains the response channel object. */ |
| 5167 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5168 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5169 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5170 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5171 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5172 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5173 | lua_pushstring(L, "http"); |
| 5174 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5175 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5176 | return 0; |
| 5177 | } |
| 5178 | else |
| 5179 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5180 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5181 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5182 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5183 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5184 | lua_setmetatable(L, -2); |
| 5185 | |
| 5186 | return 1; |
| 5187 | } |
| 5188 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5189 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5190 | { |
| 5191 | const char *msg; |
| 5192 | struct hlua_txn *htxn; |
| 5193 | |
| 5194 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5195 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5196 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5197 | |
| 5198 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5199 | return 0; |
| 5200 | } |
| 5201 | |
| 5202 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5203 | { |
| 5204 | int level; |
| 5205 | const char *msg; |
| 5206 | struct hlua_txn *htxn; |
| 5207 | |
| 5208 | MAY_LJMP(check_args(L, 3, "log")); |
| 5209 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5210 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5211 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5212 | |
| 5213 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5214 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5215 | |
| 5216 | hlua_sendlog(htxn->s->be, level, msg); |
| 5217 | return 0; |
| 5218 | } |
| 5219 | |
| 5220 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5221 | { |
| 5222 | const char *msg; |
| 5223 | struct hlua_txn *htxn; |
| 5224 | |
| 5225 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5226 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5227 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5228 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5229 | return 0; |
| 5230 | } |
| 5231 | |
| 5232 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5233 | { |
| 5234 | const char *msg; |
| 5235 | struct hlua_txn *htxn; |
| 5236 | |
| 5237 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5238 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5239 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5240 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5241 | return 0; |
| 5242 | } |
| 5243 | |
| 5244 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5245 | { |
| 5246 | const char *msg; |
| 5247 | struct hlua_txn *htxn; |
| 5248 | |
| 5249 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5250 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5251 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5252 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5253 | return 0; |
| 5254 | } |
| 5255 | |
| 5256 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5257 | { |
| 5258 | const char *msg; |
| 5259 | struct hlua_txn *htxn; |
| 5260 | |
| 5261 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5262 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5263 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5264 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5265 | return 0; |
| 5266 | } |
| 5267 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5268 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5269 | { |
| 5270 | struct hlua_txn *htxn; |
| 5271 | int ll; |
| 5272 | |
| 5273 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5274 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5275 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5276 | |
| 5277 | if (ll < 0 || ll > 7) |
| 5278 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5279 | |
| 5280 | htxn->s->logs.level = ll; |
| 5281 | return 0; |
| 5282 | } |
| 5283 | |
| 5284 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5285 | { |
| 5286 | struct hlua_txn *htxn; |
| 5287 | struct connection *cli_conn; |
| 5288 | int tos; |
| 5289 | |
| 5290 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5291 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5292 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5293 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5294 | 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] | 5295 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5296 | |
| 5297 | return 0; |
| 5298 | } |
| 5299 | |
| 5300 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5301 | { |
| 5302 | #ifdef SO_MARK |
| 5303 | struct hlua_txn *htxn; |
| 5304 | struct connection *cli_conn; |
| 5305 | int mark; |
| 5306 | |
| 5307 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5308 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5309 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5310 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5311 | 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] | 5312 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5313 | #endif |
| 5314 | return 0; |
| 5315 | } |
| 5316 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5317 | /* This function is an Lua binding that send pending data |
| 5318 | * to the client, and close the stream interface. |
| 5319 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5320 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5321 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5322 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5323 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5324 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5325 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5326 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5327 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5328 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5329 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5330 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5331 | * session, so we just end the execution of the current |
| 5332 | * lua code. |
| 5333 | */ |
| 5334 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5335 | WILL_LJMP(hlua_done(L)); |
| 5336 | return 0; |
| 5337 | } |
| 5338 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5339 | ic = &htxn->s->req; |
| 5340 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5341 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5342 | if (htxn->s->txn) { |
| 5343 | /* HTTP mode, let's stay in sync with the stream */ |
| 5344 | bi_fast_delete(ic->buf, htxn->s->txn->req.sov); |
| 5345 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 5346 | htxn->s->txn->req.sov = 0; |
| 5347 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 5348 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 5349 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5350 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 5351 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5352 | /* Note that if we want to support keep-alive, we need |
| 5353 | * to bypass the close/shutr_now calls below, but that |
| 5354 | * may only be done if the HTTP request was already |
| 5355 | * processed and the connection header is known (ie |
| 5356 | * not during TCP rules). |
| 5357 | */ |
| 5358 | } |
| 5359 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5360 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5361 | channel_abort(ic); |
| 5362 | channel_auto_close(ic); |
| 5363 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5364 | |
| 5365 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5366 | channel_auto_read(oc); |
| 5367 | channel_auto_close(oc); |
| 5368 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5369 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 5370 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5371 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5372 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5373 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5374 | return 0; |
| 5375 | } |
| 5376 | |
| 5377 | __LJMP static int hlua_log(lua_State *L) |
| 5378 | { |
| 5379 | int level; |
| 5380 | const char *msg; |
| 5381 | |
| 5382 | MAY_LJMP(check_args(L, 2, "log")); |
| 5383 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5384 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5385 | |
| 5386 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5387 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5388 | |
| 5389 | hlua_sendlog(NULL, level, msg); |
| 5390 | return 0; |
| 5391 | } |
| 5392 | |
| 5393 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5394 | { |
| 5395 | const char *msg; |
| 5396 | |
| 5397 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5398 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5399 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5400 | return 0; |
| 5401 | } |
| 5402 | |
| 5403 | __LJMP static int hlua_log_info(lua_State *L) |
| 5404 | { |
| 5405 | const char *msg; |
| 5406 | |
| 5407 | MAY_LJMP(check_args(L, 1, "info")); |
| 5408 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5409 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5410 | return 0; |
| 5411 | } |
| 5412 | |
| 5413 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5414 | { |
| 5415 | const char *msg; |
| 5416 | |
| 5417 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5418 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5419 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5420 | return 0; |
| 5421 | } |
| 5422 | |
| 5423 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5424 | { |
| 5425 | const char *msg; |
| 5426 | |
| 5427 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5428 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5429 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5430 | return 0; |
| 5431 | } |
| 5432 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5433 | __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] | 5434 | { |
| 5435 | int wakeup_ms = lua_tointeger(L, -1); |
| 5436 | if (now_ms < wakeup_ms) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5437 | 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] | 5438 | return 0; |
| 5439 | } |
| 5440 | |
| 5441 | __LJMP static int hlua_sleep(lua_State *L) |
| 5442 | { |
| 5443 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5444 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5445 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5446 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5447 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5448 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5449 | wakeup_ms = tick_add(now_ms, delay); |
| 5450 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5451 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5452 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5453 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5454 | } |
| 5455 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5456 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5457 | { |
| 5458 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5459 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5460 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5461 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5462 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5463 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5464 | wakeup_ms = tick_add(now_ms, delay); |
| 5465 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5466 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5467 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5468 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5469 | } |
| 5470 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5471 | /* This functionis an LUA binding. it permits to give back |
| 5472 | * the hand at the HAProxy scheduler. It is used when the |
| 5473 | * LUA processing consumes a lot of time. |
| 5474 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5475 | __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] | 5476 | { |
| 5477 | return 0; |
| 5478 | } |
| 5479 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5480 | __LJMP static int hlua_yield(lua_State *L) |
| 5481 | { |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5482 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 5483 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5484 | } |
| 5485 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5486 | /* This function change the nice of the currently executed |
| 5487 | * task. It is used set low or high priority at the current |
| 5488 | * task. |
| 5489 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5490 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5491 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5492 | struct hlua *hlua; |
| 5493 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5494 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5495 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5496 | hlua = hlua_gethlua(L); |
| 5497 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5498 | |
| 5499 | /* If he task is not set, I'm in a start mode. */ |
| 5500 | if (!hlua || !hlua->task) |
| 5501 | return 0; |
| 5502 | |
| 5503 | if (nice < -1024) |
| 5504 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5505 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5506 | nice = 1024; |
| 5507 | |
| 5508 | hlua->task->nice = nice; |
| 5509 | return 0; |
| 5510 | } |
| 5511 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5512 | /* This function is used as a calback of a task. It is called by the |
| 5513 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5514 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5515 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5516 | * |
| 5517 | * Task wrapper are longjmp safe because the only one Lua code |
| 5518 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5519 | */ |
| 5520 | static struct task *hlua_process_task(struct task *task) |
| 5521 | { |
| 5522 | struct hlua *hlua = task->context; |
| 5523 | enum hlua_exec status; |
| 5524 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 5525 | if (task->thread_mask == MAX_THREADS_MASK) |
| 5526 | task_set_affinity(task, tid_bit); |
| 5527 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5528 | /* If it is the first call to the task, we must initialize the |
| 5529 | * execution timeouts. |
| 5530 | */ |
| 5531 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5532 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5533 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5534 | /* Execute the Lua code. */ |
| 5535 | status = hlua_ctx_resume(hlua, 1); |
| 5536 | |
| 5537 | switch (status) { |
| 5538 | /* finished or yield */ |
| 5539 | case HLUA_E_OK: |
| 5540 | hlua_ctx_destroy(hlua); |
| 5541 | task_delete(task); |
| 5542 | task_free(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 5543 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5544 | break; |
| 5545 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5546 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 5547 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 5548 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5549 | break; |
| 5550 | |
| 5551 | /* finished with error. */ |
| 5552 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5553 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5554 | hlua_ctx_destroy(hlua); |
| 5555 | task_delete(task); |
| 5556 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5557 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5558 | break; |
| 5559 | |
| 5560 | case HLUA_E_ERR: |
| 5561 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5562 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5563 | hlua_ctx_destroy(hlua); |
| 5564 | task_delete(task); |
| 5565 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5566 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5567 | break; |
| 5568 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5569 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5570 | } |
| 5571 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5572 | /* This function is an LUA binding that register LUA function to be |
| 5573 | * executed after the HAProxy configuration parsing and before the |
| 5574 | * HAProxy scheduler starts. This function expect only one LUA |
| 5575 | * argument that is a function. This function returns nothing, but |
| 5576 | * throws if an error is encountered. |
| 5577 | */ |
| 5578 | __LJMP static int hlua_register_init(lua_State *L) |
| 5579 | { |
| 5580 | struct hlua_init_function *init; |
| 5581 | int ref; |
| 5582 | |
| 5583 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5584 | |
| 5585 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5586 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5587 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5588 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5589 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5590 | |
| 5591 | init->function_ref = ref; |
| 5592 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5593 | return 0; |
| 5594 | } |
| 5595 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5596 | /* This functio is an LUA binding. It permits to register a task |
| 5597 | * executed in parallel of the main HAroxy activity. The task is |
| 5598 | * created and it is set in the HAProxy scheduler. It can be called |
| 5599 | * from the "init" section, "post init" or during the runtime. |
| 5600 | * |
| 5601 | * Lua prototype: |
| 5602 | * |
| 5603 | * <none> core.register_task(<function>) |
| 5604 | */ |
| 5605 | static int hlua_register_task(lua_State *L) |
| 5606 | { |
| 5607 | struct hlua *hlua; |
| 5608 | struct task *task; |
| 5609 | int ref; |
| 5610 | |
| 5611 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5612 | |
| 5613 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5614 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5615 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5616 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5617 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5618 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 5619 | task = task_new(MAX_THREADS_MASK); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5620 | task->context = hlua; |
| 5621 | task->process = hlua_process_task; |
| 5622 | |
| 5623 | if (!hlua_ctx_init(hlua, task)) |
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 | |
| 5626 | /* Restore the function in the stack. */ |
| 5627 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5628 | hlua->nargs = 0; |
| 5629 | |
| 5630 | /* Schedule task. */ |
| 5631 | task_schedule(task, now_ms); |
| 5632 | |
| 5633 | return 0; |
| 5634 | } |
| 5635 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5636 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5637 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5638 | * resume converters. |
| 5639 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5640 | 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] | 5641 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5642 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5643 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5644 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5645 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5646 | if (!stream) |
| 5647 | return 0; |
| 5648 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5649 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5650 | * Lua context can be not initialized. This behavior |
| 5651 | * permits to save performances because a systematic |
| 5652 | * Lua initialization cause 5% performances loss. |
| 5653 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5654 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5655 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5656 | if (!stream->hlua) { |
| 5657 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5658 | return 0; |
| 5659 | } |
| 5660 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5661 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5662 | return 0; |
| 5663 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5664 | } |
| 5665 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5666 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5667 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5668 | |
| 5669 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5670 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5671 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5672 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5673 | else |
| 5674 | error = "critical error"; |
| 5675 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5676 | return 0; |
| 5677 | } |
| 5678 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5679 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5680 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5681 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5682 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5683 | return 0; |
| 5684 | } |
| 5685 | |
| 5686 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5687 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5688 | |
| 5689 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5690 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5691 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5692 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5693 | return 0; |
| 5694 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5695 | hlua_smp2lua(stream->hlua->T, smp); |
| 5696 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5697 | |
| 5698 | /* push keywords in the stack. */ |
| 5699 | if (arg_p) { |
| 5700 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5701 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5702 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5703 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5704 | return 0; |
| 5705 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5706 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5707 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5708 | } |
| 5709 | } |
| 5710 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5711 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5712 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5713 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5714 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5715 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5716 | } |
| 5717 | |
| 5718 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5719 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5720 | /* finished. */ |
| 5721 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5722 | /* If the stack is empty, the function fails. */ |
| 5723 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5724 | return 0; |
| 5725 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5726 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5727 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5728 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5729 | return 1; |
| 5730 | |
| 5731 | /* yield. */ |
| 5732 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5733 | 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] | 5734 | return 0; |
| 5735 | |
| 5736 | /* finished with error. */ |
| 5737 | case HLUA_E_ERRMSG: |
| 5738 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5739 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5740 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5741 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5742 | return 0; |
| 5743 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5744 | case HLUA_E_ETMOUT: |
| 5745 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 5746 | return 0; |
| 5747 | |
| 5748 | case HLUA_E_NOMEM: |
| 5749 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 5750 | return 0; |
| 5751 | |
| 5752 | case HLUA_E_YIELD: |
| 5753 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 5754 | return 0; |
| 5755 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5756 | case HLUA_E_ERR: |
| 5757 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5758 | 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] | 5759 | |
| 5760 | default: |
| 5761 | return 0; |
| 5762 | } |
| 5763 | } |
| 5764 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5765 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5766 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5767 | * resume sample-fetches. This function will be called by the sample |
| 5768 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5769 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5770 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5771 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5772 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5773 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5774 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5775 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5776 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5777 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5778 | if (!stream) |
| 5779 | return 0; |
| 5780 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5781 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5782 | * Lua context can be not initialized. This behavior |
| 5783 | * permits to save performances because a systematic |
| 5784 | * Lua initialization cause 5% performances loss. |
| 5785 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5786 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5787 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5788 | if (!stream->hlua) { |
| 5789 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5790 | return 0; |
| 5791 | } |
| 5792 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5793 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5794 | return 0; |
| 5795 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5796 | } |
| 5797 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5798 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5799 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5800 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5801 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5802 | |
| 5803 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5804 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5805 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5806 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5807 | else |
| 5808 | error = "critical error"; |
| 5809 | 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] | 5810 | return 0; |
| 5811 | } |
| 5812 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5813 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5814 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5815 | 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] | 5816 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5817 | return 0; |
| 5818 | } |
| 5819 | |
| 5820 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5821 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5822 | |
| 5823 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5824 | 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] | 5825 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5826 | 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] | 5827 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5828 | return 0; |
| 5829 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5830 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5831 | |
| 5832 | /* push keywords in the stack. */ |
| 5833 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5834 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5835 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5836 | 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] | 5837 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5838 | return 0; |
| 5839 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5840 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5841 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5842 | } |
| 5843 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5844 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5845 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5846 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5847 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5848 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5849 | } |
| 5850 | |
| 5851 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5852 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5853 | /* finished. */ |
| 5854 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5855 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5856 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5857 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5858 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5859 | /* If the stack is empty, the function fails. */ |
| 5860 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5861 | return 0; |
| 5862 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5863 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5864 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5865 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5866 | |
| 5867 | /* Set the end of execution flag. */ |
| 5868 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5869 | return 1; |
| 5870 | |
| 5871 | /* yield. */ |
| 5872 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5873 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5874 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5875 | 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] | 5876 | return 0; |
| 5877 | |
| 5878 | /* finished with error. */ |
| 5879 | case HLUA_E_ERRMSG: |
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 | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5882 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5883 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5884 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5885 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5886 | return 0; |
| 5887 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5888 | case HLUA_E_ETMOUT: |
| 5889 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 5890 | stream_int_retnclose(&stream->si[0], &msg); |
| 5891 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 5892 | return 0; |
| 5893 | |
| 5894 | case HLUA_E_NOMEM: |
| 5895 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 5896 | stream_int_retnclose(&stream->si[0], &msg); |
| 5897 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 5898 | return 0; |
| 5899 | |
| 5900 | case HLUA_E_YIELD: |
| 5901 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 5902 | stream_int_retnclose(&stream->si[0], &msg); |
| 5903 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 5904 | return 0; |
| 5905 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5906 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5907 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5908 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5909 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5910 | 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] | 5911 | |
| 5912 | default: |
| 5913 | return 0; |
| 5914 | } |
| 5915 | } |
| 5916 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5917 | /* This function is an LUA binding used for registering |
| 5918 | * "sample-conv" functions. It expects a converter name used |
| 5919 | * in the haproxy configuration file, and an LUA function. |
| 5920 | */ |
| 5921 | __LJMP static int hlua_register_converters(lua_State *L) |
| 5922 | { |
| 5923 | struct sample_conv_kw_list *sck; |
| 5924 | const char *name; |
| 5925 | int ref; |
| 5926 | int len; |
| 5927 | struct hlua_function *fcn; |
| 5928 | |
| 5929 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 5930 | |
| 5931 | /* First argument : converter name. */ |
| 5932 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5933 | |
| 5934 | /* Second argument : lua function. */ |
| 5935 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5936 | |
| 5937 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5938 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5939 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5940 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5941 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5942 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5943 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5944 | |
| 5945 | /* Fill fcn. */ |
| 5946 | fcn->name = strdup(name); |
| 5947 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5948 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5949 | fcn->function_ref = ref; |
| 5950 | |
| 5951 | /* List head */ |
| 5952 | sck->list.n = sck->list.p = NULL; |
| 5953 | |
| 5954 | /* converter keyword. */ |
| 5955 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5956 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5957 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5958 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5959 | |
| 5960 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 5961 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5962 | 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] | 5963 | sck->kw[0].val_args = NULL; |
| 5964 | sck->kw[0].in_type = SMP_T_STR; |
| 5965 | sck->kw[0].out_type = SMP_T_STR; |
| 5966 | sck->kw[0].private = fcn; |
| 5967 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5968 | /* Register this new converter */ |
| 5969 | sample_register_convs(sck); |
| 5970 | |
| 5971 | return 0; |
| 5972 | } |
| 5973 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5974 | /* This fucntion is an LUA binding used for registering |
| 5975 | * "sample-fetch" functions. It expects a converter name used |
| 5976 | * in the haproxy configuration file, and an LUA function. |
| 5977 | */ |
| 5978 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 5979 | { |
| 5980 | const char *name; |
| 5981 | int ref; |
| 5982 | int len; |
| 5983 | struct sample_fetch_kw_list *sfk; |
| 5984 | struct hlua_function *fcn; |
| 5985 | |
| 5986 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 5987 | |
| 5988 | /* First argument : sample-fetch name. */ |
| 5989 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5990 | |
| 5991 | /* Second argument : lua function. */ |
| 5992 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5993 | |
| 5994 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5995 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5996 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5997 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5998 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5999 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6000 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6001 | |
| 6002 | /* Fill fcn. */ |
| 6003 | fcn->name = strdup(name); |
| 6004 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6005 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6006 | fcn->function_ref = ref; |
| 6007 | |
| 6008 | /* List head */ |
| 6009 | sfk->list.n = sfk->list.p = NULL; |
| 6010 | |
| 6011 | /* sample-fetch keyword. */ |
| 6012 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6013 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6014 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6015 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6016 | |
| 6017 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6018 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6019 | 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] | 6020 | sfk->kw[0].val_args = NULL; |
| 6021 | sfk->kw[0].out_type = SMP_T_STR; |
| 6022 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6023 | sfk->kw[0].val = 0; |
| 6024 | sfk->kw[0].private = fcn; |
| 6025 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6026 | /* Register this new fetch. */ |
| 6027 | sample_register_fetches(sfk); |
| 6028 | |
| 6029 | return 0; |
| 6030 | } |
| 6031 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6032 | /* This function is a wrapper to execute each LUA function declared |
| 6033 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6034 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6035 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6036 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6037 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6038 | 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] | 6039 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6040 | { |
| 6041 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6042 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6043 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6044 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6045 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6046 | |
| 6047 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 6048 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 6049 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 6050 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 6051 | 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] | 6052 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6053 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6054 | return ACT_RET_CONT; |
| 6055 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6056 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6057 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6058 | * Lua context can be not initialized. This behavior |
| 6059 | * permits to save performances because a systematic |
| 6060 | * Lua initialization cause 5% performances loss. |
| 6061 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6062 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6063 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6064 | if (!s->hlua) { |
| 6065 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6066 | rule->arg.hlua_rule->fcn.name); |
| 6067 | return ACT_RET_CONT; |
| 6068 | } |
| 6069 | if (!hlua_ctx_init(s->hlua, s->task)) { |
| 6070 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6071 | rule->arg.hlua_rule->fcn.name); |
| 6072 | return ACT_RET_CONT; |
| 6073 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6074 | } |
| 6075 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6076 | consistency_set(s, dir, &s->hlua->cons); |
| 6077 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6078 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6079 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6080 | |
| 6081 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6082 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6083 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6084 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6085 | else |
| 6086 | error = "critical error"; |
| 6087 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6088 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6089 | return ACT_RET_CONT; |
| 6090 | } |
| 6091 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6092 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6093 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6094 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6095 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6096 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6097 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6098 | } |
| 6099 | |
| 6100 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6101 | 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] | 6102 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6103 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6104 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6105 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6106 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6107 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6108 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6109 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6110 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6111 | |
| 6112 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6113 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6114 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6115 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6116 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6117 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6118 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6119 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6120 | lua_pushstring(s->hlua->T, *arg); |
| 6121 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6122 | } |
| 6123 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6124 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6125 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6126 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6127 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6128 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6129 | } |
| 6130 | |
| 6131 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6132 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6133 | /* finished. */ |
| 6134 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6135 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6136 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6137 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6138 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6139 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6140 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6141 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6142 | |
| 6143 | /* yield. */ |
| 6144 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6145 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6146 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6147 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6148 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6149 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6150 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6151 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6152 | /* Some actions can be wake up when a "write" event |
| 6153 | * is detected on a response channel. This is useful |
| 6154 | * only for actions targetted on the requests. |
| 6155 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6156 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6157 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6158 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6159 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6160 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6161 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6162 | s->req.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6163 | /* We can quit the fcuntion without consistency check |
| 6164 | * because HAProxy is not able to manipulate data, it |
| 6165 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6166 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6167 | |
| 6168 | /* finished with error. */ |
| 6169 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6170 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6171 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6172 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6173 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6174 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6175 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6176 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6177 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6178 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6179 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6180 | case HLUA_E_ETMOUT: |
| 6181 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6182 | stream_int_retnclose(&s->si[0], &msg); |
| 6183 | return ACT_RET_ERR; |
| 6184 | } |
| 6185 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6186 | return 0; |
| 6187 | |
| 6188 | case HLUA_E_NOMEM: |
| 6189 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6190 | stream_int_retnclose(&s->si[0], &msg); |
| 6191 | return ACT_RET_ERR; |
| 6192 | } |
| 6193 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6194 | return 0; |
| 6195 | |
| 6196 | case HLUA_E_YIELD: |
| 6197 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6198 | stream_int_retnclose(&s->si[0], &msg); |
| 6199 | return ACT_RET_ERR; |
| 6200 | } |
| 6201 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6202 | rule->arg.hlua_rule->fcn.name); |
| 6203 | return 0; |
| 6204 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6205 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6206 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6207 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6208 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6209 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6210 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6211 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6212 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6213 | |
| 6214 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6215 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6216 | } |
| 6217 | } |
| 6218 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6219 | struct task *hlua_applet_wakeup(struct task *t) |
| 6220 | { |
| 6221 | struct appctx *ctx = t->context; |
| 6222 | struct stream_interface *si = ctx->owner; |
| 6223 | |
| 6224 | /* If the applet is wake up without any expected work, the sheduler |
| 6225 | * remove it from the run queue. This flag indicate that the applet |
| 6226 | * is waiting for write. If the buffer is full, the main processing |
| 6227 | * will send some data and after call the applet, otherwise it call |
| 6228 | * the applet ASAP. |
| 6229 | */ |
| 6230 | si_applet_cant_put(si); |
| 6231 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6232 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6233 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6234 | } |
| 6235 | |
| 6236 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6237 | { |
| 6238 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6239 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6240 | struct task *task; |
| 6241 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6242 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6243 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6244 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6245 | if (!hlua) { |
| 6246 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6247 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6248 | return 0; |
| 6249 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6250 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6251 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6252 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6253 | |
| 6254 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6255 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6256 | if (!task) { |
| 6257 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6258 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6259 | return 0; |
| 6260 | } |
| 6261 | task->nice = 0; |
| 6262 | task->context = ctx; |
| 6263 | task->process = hlua_applet_wakeup; |
| 6264 | ctx->ctx.hlua_apptcp.task = task; |
| 6265 | |
| 6266 | /* In the execution wrappers linked with a stream, the |
| 6267 | * Lua context can be not initialized. This behavior |
| 6268 | * permits to save performances because a systematic |
| 6269 | * Lua initialization cause 5% performances loss. |
| 6270 | */ |
| 6271 | if (!hlua_ctx_init(hlua, task)) { |
| 6272 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6273 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6274 | return 0; |
| 6275 | } |
| 6276 | |
| 6277 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6278 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6279 | |
| 6280 | /* The following Lua calls can fail. */ |
| 6281 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6282 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6283 | error = lua_tostring(hlua->T, -1); |
| 6284 | else |
| 6285 | error = "critical error"; |
| 6286 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6287 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6288 | RESET_SAFE_LJMP(hlua->T); |
| 6289 | return 0; |
| 6290 | } |
| 6291 | |
| 6292 | /* Check stack available size. */ |
| 6293 | if (!lua_checkstack(hlua->T, 1)) { |
| 6294 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6295 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6296 | RESET_SAFE_LJMP(hlua->T); |
| 6297 | return 0; |
| 6298 | } |
| 6299 | |
| 6300 | /* Restore the function in the stack. */ |
| 6301 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6302 | |
| 6303 | /* Create and and push object stream in the stack. */ |
| 6304 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6305 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6306 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6307 | RESET_SAFE_LJMP(hlua->T); |
| 6308 | return 0; |
| 6309 | } |
| 6310 | hlua->nargs = 1; |
| 6311 | |
| 6312 | /* push keywords in the stack. */ |
| 6313 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6314 | if (!lua_checkstack(hlua->T, 1)) { |
| 6315 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6316 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6317 | RESET_SAFE_LJMP(hlua->T); |
| 6318 | return 0; |
| 6319 | } |
| 6320 | lua_pushstring(hlua->T, *arg); |
| 6321 | hlua->nargs++; |
| 6322 | } |
| 6323 | |
| 6324 | RESET_SAFE_LJMP(hlua->T); |
| 6325 | |
| 6326 | /* Wakeup the applet ASAP. */ |
| 6327 | si_applet_cant_get(si); |
| 6328 | si_applet_cant_put(si); |
| 6329 | |
| 6330 | return 1; |
| 6331 | } |
| 6332 | |
| 6333 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6334 | { |
| 6335 | struct stream_interface *si = ctx->owner; |
| 6336 | struct stream *strm = si_strm(si); |
| 6337 | struct channel *res = si_ic(si); |
| 6338 | struct act_rule *rule = ctx->rule; |
| 6339 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6340 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6341 | |
| 6342 | /* The applet execution is already done. */ |
| 6343 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) |
| 6344 | return; |
| 6345 | |
| 6346 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6347 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6348 | return; |
| 6349 | |
| 6350 | /* Execute the function. */ |
| 6351 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6352 | /* finished. */ |
| 6353 | case HLUA_E_OK: |
| 6354 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6355 | |
| 6356 | /* log time */ |
| 6357 | strm->logs.tv_request = now; |
| 6358 | |
| 6359 | /* eat the whole request */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6360 | co_skip(si_oc(si), si_ob(si)->o); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6361 | res->flags |= CF_READ_NULL; |
| 6362 | si_shutr(si); |
| 6363 | return; |
| 6364 | |
| 6365 | /* yield. */ |
| 6366 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6367 | if (hlua->wake_time != TICK_ETERNITY) |
| 6368 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6369 | return; |
| 6370 | |
| 6371 | /* finished with error. */ |
| 6372 | case HLUA_E_ERRMSG: |
| 6373 | /* Display log. */ |
| 6374 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6375 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6376 | lua_pop(hlua->T, 1); |
| 6377 | goto error; |
| 6378 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6379 | case HLUA_E_ETMOUT: |
| 6380 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6381 | rule->arg.hlua_rule->fcn.name); |
| 6382 | goto error; |
| 6383 | |
| 6384 | case HLUA_E_NOMEM: |
| 6385 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6386 | rule->arg.hlua_rule->fcn.name); |
| 6387 | goto error; |
| 6388 | |
| 6389 | case HLUA_E_YIELD: /* unexpected */ |
| 6390 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6391 | rule->arg.hlua_rule->fcn.name); |
| 6392 | goto error; |
| 6393 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6394 | case HLUA_E_ERR: |
| 6395 | /* Display log. */ |
| 6396 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6397 | rule->arg.hlua_rule->fcn.name); |
| 6398 | goto error; |
| 6399 | |
| 6400 | default: |
| 6401 | goto error; |
| 6402 | } |
| 6403 | |
| 6404 | error: |
| 6405 | |
| 6406 | /* For all other cases, just close the stream. */ |
| 6407 | si_shutw(si); |
| 6408 | si_shutr(si); |
| 6409 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6410 | } |
| 6411 | |
| 6412 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6413 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6414 | task_delete(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6415 | task_free(ctx->ctx.hlua_apptcp.task); |
| 6416 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6417 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6418 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6419 | } |
| 6420 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6421 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6422 | * an errors occurs and -1 if more data are required for initializing |
| 6423 | * the applet. |
| 6424 | */ |
| 6425 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6426 | { |
| 6427 | struct stream_interface *si = ctx->owner; |
| 6428 | struct channel *req = si_oc(si); |
| 6429 | struct http_msg *msg; |
| 6430 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6431 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6432 | char **arg; |
| 6433 | struct hdr_ctx hdr; |
| 6434 | struct task *task; |
| 6435 | 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] | 6436 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6437 | |
| 6438 | /* Wait for a full HTTP request. */ |
| 6439 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 6440 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 6441 | return -1; |
| 6442 | return 0; |
| 6443 | } |
| 6444 | txn = strm->txn; |
| 6445 | msg = &txn->req; |
| 6446 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6447 | /* We want two things in HTTP mode : |
| 6448 | * - enforce server-close mode if we were in keep-alive, so that the |
| 6449 | * applet is released after each response ; |
| 6450 | * - enable request body transfer to the applet in order to resync |
| 6451 | * with the response body. |
| 6452 | */ |
| 6453 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 6454 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6455 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6456 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6457 | if (!hlua) { |
| 6458 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6459 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6460 | return 0; |
| 6461 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6462 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6463 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6464 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6465 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6466 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6467 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6468 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6469 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6470 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6471 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6472 | if (!task) { |
| 6473 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6474 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6475 | return 0; |
| 6476 | } |
| 6477 | task->nice = 0; |
| 6478 | task->context = ctx; |
| 6479 | task->process = hlua_applet_wakeup; |
| 6480 | ctx->ctx.hlua_apphttp.task = task; |
| 6481 | |
| 6482 | /* In the execution wrappers linked with a stream, the |
| 6483 | * Lua context can be not initialized. This behavior |
| 6484 | * permits to save performances because a systematic |
| 6485 | * Lua initialization cause 5% performances loss. |
| 6486 | */ |
| 6487 | if (!hlua_ctx_init(hlua, task)) { |
| 6488 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6489 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6490 | return 0; |
| 6491 | } |
| 6492 | |
| 6493 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6494 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6495 | |
| 6496 | /* The following Lua calls can fail. */ |
| 6497 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6498 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6499 | error = lua_tostring(hlua->T, -1); |
| 6500 | else |
| 6501 | error = "critical error"; |
| 6502 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6503 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6504 | return 0; |
| 6505 | } |
| 6506 | |
| 6507 | /* Check stack available size. */ |
| 6508 | if (!lua_checkstack(hlua->T, 1)) { |
| 6509 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6510 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6511 | RESET_SAFE_LJMP(hlua->T); |
| 6512 | return 0; |
| 6513 | } |
| 6514 | |
| 6515 | /* Restore the function in the stack. */ |
| 6516 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6517 | |
| 6518 | /* Create and and push object stream in the stack. */ |
| 6519 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6520 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6521 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6522 | RESET_SAFE_LJMP(hlua->T); |
| 6523 | return 0; |
| 6524 | } |
| 6525 | hlua->nargs = 1; |
| 6526 | |
| 6527 | /* Look for a 100-continue expected. */ |
| 6528 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 6529 | hdr.idx = 0; |
| 6530 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) && |
| 6531 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 6532 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 6533 | } |
| 6534 | |
| 6535 | /* push keywords in the stack. */ |
| 6536 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6537 | if (!lua_checkstack(hlua->T, 1)) { |
| 6538 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6539 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6540 | RESET_SAFE_LJMP(hlua->T); |
| 6541 | return 0; |
| 6542 | } |
| 6543 | lua_pushstring(hlua->T, *arg); |
| 6544 | hlua->nargs++; |
| 6545 | } |
| 6546 | |
| 6547 | RESET_SAFE_LJMP(hlua->T); |
| 6548 | |
| 6549 | /* Wakeup the applet when data is ready for read. */ |
| 6550 | si_applet_cant_get(si); |
| 6551 | |
| 6552 | return 1; |
| 6553 | } |
| 6554 | |
| 6555 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 6556 | { |
| 6557 | struct stream_interface *si = ctx->owner; |
| 6558 | struct stream *strm = si_strm(si); |
| 6559 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6560 | struct act_rule *rule = ctx->rule; |
| 6561 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6562 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6563 | char *blk1; |
| 6564 | int len1; |
| 6565 | char *blk2; |
| 6566 | int len2; |
| 6567 | int ret; |
| 6568 | |
| 6569 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6570 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6571 | return; |
| 6572 | |
| 6573 | /* Set the currently running flag. */ |
| 6574 | if (!HLUA_IS_RUNNING(hlua) && |
| 6575 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6576 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6577 | /* Wait for full HTTP analysys. */ |
| 6578 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 6579 | si_applet_cant_get(si); |
| 6580 | return; |
| 6581 | } |
| 6582 | |
| 6583 | /* Store the max amount of bytes that we can read. */ |
| 6584 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 6585 | |
| 6586 | /* We need to flush the request header. This left the body |
| 6587 | * for the Lua. |
| 6588 | */ |
| 6589 | |
| 6590 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6591 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6592 | if (ret == -1) |
| 6593 | return; |
| 6594 | |
| 6595 | /* No data available, ask for more data. */ |
| 6596 | if (ret == 1) |
| 6597 | len2 = 0; |
| 6598 | if (ret == 0) |
| 6599 | len1 = 0; |
| 6600 | if (len1 + len2 < strm->txn->req.eoh + 2) { |
| 6601 | si_applet_cant_get(si); |
| 6602 | return; |
| 6603 | } |
| 6604 | |
| 6605 | /* skip the requests bytes. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6606 | co_skip(si_oc(si), strm->txn->req.eoh + 2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6607 | } |
| 6608 | |
| 6609 | /* Executes The applet if it is not done. */ |
| 6610 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6611 | |
| 6612 | /* Execute the function. */ |
| 6613 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6614 | /* finished. */ |
| 6615 | case HLUA_E_OK: |
| 6616 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6617 | break; |
| 6618 | |
| 6619 | /* yield. */ |
| 6620 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6621 | if (hlua->wake_time != TICK_ETERNITY) |
| 6622 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6623 | return; |
| 6624 | |
| 6625 | /* finished with error. */ |
| 6626 | case HLUA_E_ERRMSG: |
| 6627 | /* Display log. */ |
| 6628 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6629 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6630 | lua_pop(hlua->T, 1); |
| 6631 | goto error; |
| 6632 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6633 | case HLUA_E_ETMOUT: |
| 6634 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 6635 | rule->arg.hlua_rule->fcn.name); |
| 6636 | goto error; |
| 6637 | |
| 6638 | case HLUA_E_NOMEM: |
| 6639 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 6640 | rule->arg.hlua_rule->fcn.name); |
| 6641 | goto error; |
| 6642 | |
| 6643 | case HLUA_E_YIELD: /* unexpected */ |
| 6644 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 6645 | rule->arg.hlua_rule->fcn.name); |
| 6646 | goto error; |
| 6647 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6648 | case HLUA_E_ERR: |
| 6649 | /* Display log. */ |
| 6650 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6651 | rule->arg.hlua_rule->fcn.name); |
| 6652 | goto error; |
| 6653 | |
| 6654 | default: |
| 6655 | goto error; |
| 6656 | } |
| 6657 | } |
| 6658 | |
| 6659 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 6660 | |
| 6661 | /* We must send the final chunk. */ |
| 6662 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 6663 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 6664 | |
| 6665 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6666 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6667 | |
| 6668 | /* critical error. */ |
| 6669 | if (ret == -2 || ret == -3) { |
| 6670 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 6671 | rule->arg.hlua_rule->fcn.name); |
| 6672 | goto error; |
| 6673 | } |
| 6674 | |
| 6675 | /* no enough space error. */ |
| 6676 | if (ret == -1) { |
| 6677 | si_applet_cant_put(si); |
| 6678 | return; |
| 6679 | } |
| 6680 | |
| 6681 | /* set the last chunk sent. */ |
| 6682 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6683 | } |
| 6684 | |
| 6685 | /* close the connection. */ |
| 6686 | |
| 6687 | /* status / log */ |
| 6688 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6689 | strm->logs.tv_request = now; |
| 6690 | |
| 6691 | /* eat the whole request */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6692 | co_skip(si_oc(si), si_ob(si)->o); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6693 | res->flags |= CF_READ_NULL; |
| 6694 | si_shutr(si); |
| 6695 | |
| 6696 | return; |
| 6697 | } |
| 6698 | |
| 6699 | error: |
| 6700 | |
| 6701 | /* If we are in HTTP mode, and we are not send any |
| 6702 | * data, return a 500 server error in best effort: |
| 6703 | * if there are no room avalaible in the buffer, |
| 6704 | * just close the connection. |
| 6705 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6706 | ci_putblk(res, error_500, strlen(error_500)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6707 | if (!(strm->flags & SF_ERR_MASK)) |
| 6708 | strm->flags |= SF_ERR_RESOURCE; |
| 6709 | si_shutw(si); |
| 6710 | si_shutr(si); |
| 6711 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6712 | } |
| 6713 | |
| 6714 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6715 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6716 | task_delete(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6717 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6718 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6719 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6720 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6721 | } |
| 6722 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6723 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6724 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6725 | * |
| 6726 | * This function can fail with an abort() due to an Lua critical error. |
| 6727 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6728 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6729 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6730 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6731 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6732 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6733 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6734 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6735 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6736 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6737 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6738 | if (!rule->arg.hlua_rule) { |
| 6739 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6740 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6741 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6742 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6743 | /* Memory for arguments. */ |
| 6744 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6745 | if (!rule->arg.hlua_rule->args) { |
| 6746 | memprintf(err, "out of memory error"); |
| 6747 | return ACT_RET_PRS_ERR; |
| 6748 | } |
| 6749 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6750 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6751 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6752 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6753 | /* Expect some arguments */ |
| 6754 | for (i = 0; i < fcn->nargs; i++) { |
| 6755 | if (*args[i+1] == '\0') { |
| 6756 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6757 | return ACT_RET_PRS_ERR; |
| 6758 | } |
| 6759 | rule->arg.hlua_rule->args[i] = strdup(args[i + 1]); |
| 6760 | if (!rule->arg.hlua_rule->args[i]) { |
| 6761 | memprintf(err, "out of memory error"); |
| 6762 | return ACT_RET_PRS_ERR; |
| 6763 | } |
| 6764 | (*cur_arg)++; |
| 6765 | } |
| 6766 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6767 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6768 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6769 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6770 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6771 | } |
| 6772 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6773 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6774 | struct act_rule *rule, char **err) |
| 6775 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6776 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6777 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6778 | /* HTTP applets are forbidden in tcp-request rules. |
| 6779 | * HTTP applet request requires everything initilized by |
| 6780 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6781 | * The applet will be immediately initilized, but its before |
| 6782 | * the call of this analyzer. |
| 6783 | */ |
| 6784 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6785 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6786 | return ACT_RET_PRS_ERR; |
| 6787 | } |
| 6788 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6789 | /* Memory for the rule. */ |
| 6790 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6791 | if (!rule->arg.hlua_rule) { |
| 6792 | memprintf(err, "out of memory error"); |
| 6793 | return ACT_RET_PRS_ERR; |
| 6794 | } |
| 6795 | |
| 6796 | /* Reference the Lua function and store the reference. */ |
| 6797 | rule->arg.hlua_rule->fcn = *fcn; |
| 6798 | |
| 6799 | /* TODO: later accept arguments. */ |
| 6800 | rule->arg.hlua_rule->args = NULL; |
| 6801 | |
| 6802 | /* Add applet pointer in the rule. */ |
| 6803 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6804 | rule->applet.name = fcn->name; |
| 6805 | rule->applet.init = hlua_applet_http_init; |
| 6806 | rule->applet.fct = hlua_applet_http_fct; |
| 6807 | rule->applet.release = hlua_applet_http_release; |
| 6808 | rule->applet.timeout = hlua_timeout_applet; |
| 6809 | |
| 6810 | return ACT_RET_PRS_OK; |
| 6811 | } |
| 6812 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6813 | /* This function is an LUA binding used for registering |
| 6814 | * "sample-conv" functions. It expects a converter name used |
| 6815 | * in the haproxy configuration file, and an LUA function. |
| 6816 | */ |
| 6817 | __LJMP static int hlua_register_action(lua_State *L) |
| 6818 | { |
| 6819 | struct action_kw_list *akl; |
| 6820 | const char *name; |
| 6821 | int ref; |
| 6822 | int len; |
| 6823 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6824 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6825 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6826 | /* Initialise the number of expected arguments at 0. */ |
| 6827 | nargs = 0; |
| 6828 | |
| 6829 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6830 | 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] | 6831 | |
| 6832 | /* First argument : converter name. */ |
| 6833 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6834 | |
| 6835 | /* Second argument : environment. */ |
| 6836 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6837 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6838 | |
| 6839 | /* Third argument : lua function. */ |
| 6840 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6841 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6842 | /* Fouth argument : number of mandatories arguments expected on the configuration line. */ |
| 6843 | if (lua_gettop(L) >= 4) |
| 6844 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6845 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6846 | /* browse the second argulent as an array. */ |
| 6847 | lua_pushnil(L); |
| 6848 | while (lua_next(L, 2) != 0) { |
| 6849 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6850 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6851 | |
| 6852 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6853 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6854 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6855 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6856 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6857 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6858 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6859 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6860 | |
| 6861 | /* Fill fcn. */ |
| 6862 | fcn->name = strdup(name); |
| 6863 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6864 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6865 | fcn->function_ref = ref; |
| 6866 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6867 | /* Set the expected number od arguments. */ |
| 6868 | fcn->nargs = nargs; |
| 6869 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6870 | /* List head */ |
| 6871 | akl->list.n = akl->list.p = NULL; |
| 6872 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6873 | /* action keyword. */ |
| 6874 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6875 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6876 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6877 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6878 | |
| 6879 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6880 | |
| 6881 | akl->kw[0].match_pfx = 0; |
| 6882 | akl->kw[0].private = fcn; |
| 6883 | akl->kw[0].parse = action_register_lua; |
| 6884 | |
| 6885 | /* select the action registering point. */ |
| 6886 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6887 | tcp_req_cont_keywords_register(akl); |
| 6888 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6889 | tcp_res_cont_keywords_register(akl); |
| 6890 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6891 | http_req_keywords_register(akl); |
| 6892 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6893 | http_res_keywords_register(akl); |
| 6894 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6895 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6896 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6897 | "are expected.", lua_tostring(L, -1))); |
| 6898 | |
| 6899 | /* pop the environment string. */ |
| 6900 | lua_pop(L, 1); |
| 6901 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6902 | return ACT_RET_PRS_OK; |
| 6903 | } |
| 6904 | |
| 6905 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6906 | struct act_rule *rule, char **err) |
| 6907 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6908 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6909 | |
| 6910 | /* Memory for the rule. */ |
| 6911 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6912 | if (!rule->arg.hlua_rule) { |
| 6913 | memprintf(err, "out of memory error"); |
| 6914 | return ACT_RET_PRS_ERR; |
| 6915 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6916 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6917 | /* Reference the Lua function and store the reference. */ |
| 6918 | rule->arg.hlua_rule->fcn = *fcn; |
| 6919 | |
| 6920 | /* TODO: later accept arguments. */ |
| 6921 | rule->arg.hlua_rule->args = NULL; |
| 6922 | |
| 6923 | /* Add applet pointer in the rule. */ |
| 6924 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6925 | rule->applet.name = fcn->name; |
| 6926 | rule->applet.init = hlua_applet_tcp_init; |
| 6927 | rule->applet.fct = hlua_applet_tcp_fct; |
| 6928 | rule->applet.release = hlua_applet_tcp_release; |
| 6929 | rule->applet.timeout = hlua_timeout_applet; |
| 6930 | |
| 6931 | return 0; |
| 6932 | } |
| 6933 | |
| 6934 | /* This function is an LUA binding used for registering |
| 6935 | * "sample-conv" functions. It expects a converter name used |
| 6936 | * in the haproxy configuration file, and an LUA function. |
| 6937 | */ |
| 6938 | __LJMP static int hlua_register_service(lua_State *L) |
| 6939 | { |
| 6940 | struct action_kw_list *akl; |
| 6941 | const char *name; |
| 6942 | const char *env; |
| 6943 | int ref; |
| 6944 | int len; |
| 6945 | struct hlua_function *fcn; |
| 6946 | |
| 6947 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 6948 | |
| 6949 | /* First argument : converter name. */ |
| 6950 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6951 | |
| 6952 | /* Second argument : environment. */ |
| 6953 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6954 | |
| 6955 | /* Third argument : lua function. */ |
| 6956 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6957 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6958 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6959 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 6960 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6961 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6962 | fcn = calloc(1, sizeof(*fcn)); |
| 6963 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6964 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6965 | |
| 6966 | /* Fill fcn. */ |
| 6967 | len = strlen("<lua.>") + strlen(name) + 1; |
| 6968 | fcn->name = calloc(1, len); |
| 6969 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6970 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6971 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 6972 | fcn->function_ref = ref; |
| 6973 | |
| 6974 | /* List head */ |
| 6975 | akl->list.n = akl->list.p = NULL; |
| 6976 | |
| 6977 | /* converter keyword. */ |
| 6978 | len = strlen("lua.") + strlen(name) + 1; |
| 6979 | akl->kw[0].kw = calloc(1, len); |
| 6980 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6981 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6982 | |
| 6983 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6984 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 6985 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6986 | if (strcmp(env, "tcp") == 0) |
| 6987 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6988 | else if (strcmp(env, "http") == 0) |
| 6989 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6990 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6991 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 6992 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6993 | |
| 6994 | akl->kw[0].match_pfx = 0; |
| 6995 | akl->kw[0].private = fcn; |
| 6996 | |
| 6997 | /* End of array. */ |
| 6998 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 6999 | |
| 7000 | /* Register this new converter */ |
| 7001 | service_keywords_register(akl); |
| 7002 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7003 | return 0; |
| 7004 | } |
| 7005 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7006 | /* This function initialises Lua cli handler. It copies the |
| 7007 | * arguments in the Lua stack and create channel IO objects. |
| 7008 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7009 | 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] | 7010 | { |
| 7011 | struct hlua *hlua; |
| 7012 | struct hlua_function *fcn; |
| 7013 | int i; |
| 7014 | const char *error; |
| 7015 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7016 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7017 | appctx->ctx.hlua_cli.fcn = private; |
| 7018 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7019 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7020 | if (!hlua) { |
| 7021 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7022 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7023 | } |
| 7024 | HLUA_INIT(hlua); |
| 7025 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7026 | |
| 7027 | /* Create task used by signal to wakeup applets. |
| 7028 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7029 | * applet_http. It is absolutely compatible. |
| 7030 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7031 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7032 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7033 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7034 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7035 | } |
| 7036 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7037 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7038 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7039 | |
| 7040 | /* Initialises the Lua context */ |
| 7041 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 7042 | 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] | 7043 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7044 | } |
| 7045 | |
| 7046 | /* The following Lua calls can fail. */ |
| 7047 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7048 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7049 | error = lua_tostring(hlua->T, -1); |
| 7050 | else |
| 7051 | error = "critical error"; |
| 7052 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7053 | goto error; |
| 7054 | } |
| 7055 | |
| 7056 | /* Check stack available size. */ |
| 7057 | if (!lua_checkstack(hlua->T, 2)) { |
| 7058 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7059 | goto error; |
| 7060 | } |
| 7061 | |
| 7062 | /* Restore the function in the stack. */ |
| 7063 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7064 | |
| 7065 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7066 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7067 | */ |
| 7068 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7069 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7070 | goto error; |
| 7071 | } |
| 7072 | hlua->nargs = 1; |
| 7073 | |
| 7074 | /* push keywords in the stack. */ |
| 7075 | for (i = 0; *args[i]; i++) { |
| 7076 | /* Check stack available size. */ |
| 7077 | if (!lua_checkstack(hlua->T, 1)) { |
| 7078 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7079 | goto error; |
| 7080 | } |
| 7081 | lua_pushstring(hlua->T, args[i]); |
| 7082 | hlua->nargs++; |
| 7083 | } |
| 7084 | |
| 7085 | /* We must initialize the execution timeouts. */ |
| 7086 | hlua->max_time = hlua_timeout_session; |
| 7087 | |
| 7088 | /* At this point the execution is safe. */ |
| 7089 | RESET_SAFE_LJMP(hlua->T); |
| 7090 | |
| 7091 | /* It's ok */ |
| 7092 | return 0; |
| 7093 | |
| 7094 | /* It's not ok. */ |
| 7095 | error: |
| 7096 | RESET_SAFE_LJMP(hlua->T); |
| 7097 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7098 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7099 | return 1; |
| 7100 | } |
| 7101 | |
| 7102 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7103 | { |
| 7104 | struct hlua *hlua; |
| 7105 | struct stream_interface *si; |
| 7106 | struct hlua_function *fcn; |
| 7107 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7108 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7109 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7110 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7111 | |
| 7112 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7113 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7114 | return 1; |
| 7115 | |
| 7116 | /* Execute the function. */ |
| 7117 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7118 | |
| 7119 | /* finished. */ |
| 7120 | case HLUA_E_OK: |
| 7121 | return 1; |
| 7122 | |
| 7123 | /* yield. */ |
| 7124 | case HLUA_E_AGAIN: |
| 7125 | /* We want write. */ |
| 7126 | if (HLUA_IS_WAKERESWR(hlua)) |
| 7127 | si_applet_cant_put(si); |
| 7128 | /* Set the timeout. */ |
| 7129 | if (hlua->wake_time != TICK_ETERNITY) |
| 7130 | task_schedule(hlua->task, hlua->wake_time); |
| 7131 | return 0; |
| 7132 | |
| 7133 | /* finished with error. */ |
| 7134 | case HLUA_E_ERRMSG: |
| 7135 | /* Display log. */ |
| 7136 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7137 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7138 | lua_pop(hlua->T, 1); |
| 7139 | return 1; |
| 7140 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7141 | case HLUA_E_ETMOUT: |
| 7142 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7143 | fcn->name); |
| 7144 | return 1; |
| 7145 | |
| 7146 | case HLUA_E_NOMEM: |
| 7147 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7148 | fcn->name); |
| 7149 | return 1; |
| 7150 | |
| 7151 | case HLUA_E_YIELD: /* unexpected */ |
| 7152 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7153 | fcn->name); |
| 7154 | return 1; |
| 7155 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7156 | case HLUA_E_ERR: |
| 7157 | /* Display log. */ |
| 7158 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7159 | fcn->name); |
| 7160 | return 1; |
| 7161 | |
| 7162 | default: |
| 7163 | return 1; |
| 7164 | } |
| 7165 | |
| 7166 | return 1; |
| 7167 | } |
| 7168 | |
| 7169 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7170 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7171 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7172 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7173 | } |
| 7174 | |
| 7175 | /* This function is an LUA binding used for registering |
| 7176 | * new keywords in the cli. It expects a list of keywords |
| 7177 | * which are the "path". It is limited to 5 keywords. A |
| 7178 | * description of the command, a function to be executed |
| 7179 | * for the parsing and a function for io handlers. |
| 7180 | */ |
| 7181 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7182 | { |
| 7183 | struct cli_kw_list *cli_kws; |
| 7184 | const char *message; |
| 7185 | int ref_io; |
| 7186 | int len; |
| 7187 | struct hlua_function *fcn; |
| 7188 | int index; |
| 7189 | int i; |
| 7190 | |
| 7191 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7192 | |
| 7193 | /* First argument : an array of maximum 5 keywords. */ |
| 7194 | if (!lua_istable(L, 1)) |
| 7195 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7196 | |
| 7197 | /* Second argument : string with contextual message. */ |
| 7198 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7199 | |
| 7200 | /* Third and fourth argument : lua function. */ |
| 7201 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7202 | |
| 7203 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7204 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7205 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7206 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7207 | fcn = calloc(1, sizeof(*fcn)); |
| 7208 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7209 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7210 | |
| 7211 | /* Fill path. */ |
| 7212 | index = 0; |
| 7213 | lua_pushnil(L); |
| 7214 | while(lua_next(L, 1) != 0) { |
| 7215 | if (index >= 5) |
| 7216 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7217 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7218 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7219 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7220 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7221 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7222 | index++; |
| 7223 | lua_pop(L, 1); |
| 7224 | } |
| 7225 | |
| 7226 | /* Copy help message. */ |
| 7227 | cli_kws->kw[0].usage = strdup(message); |
| 7228 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7229 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7230 | |
| 7231 | /* Fill fcn io handler. */ |
| 7232 | len = strlen("<lua.cli>") + 1; |
| 7233 | for (i = 0; i < index; i++) |
| 7234 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7235 | fcn->name = calloc(1, len); |
| 7236 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7237 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7238 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7239 | for (i = 0; i < index; i++) { |
| 7240 | strncat((char *)fcn->name, ".", len); |
| 7241 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7242 | } |
| 7243 | strncat((char *)fcn->name, ">", len); |
| 7244 | fcn->function_ref = ref_io; |
| 7245 | |
| 7246 | /* Fill last entries. */ |
| 7247 | cli_kws->kw[0].private = fcn; |
| 7248 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7249 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7250 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7251 | |
| 7252 | /* Register this new converter */ |
| 7253 | cli_register_kw(cli_kws); |
| 7254 | |
| 7255 | return 0; |
| 7256 | } |
| 7257 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7258 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7259 | struct proxy *defpx, const char *file, int line, |
| 7260 | char **err, unsigned int *timeout) |
| 7261 | { |
| 7262 | const char *error; |
| 7263 | |
| 7264 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 7265 | if (error && *error != '\0') { |
| 7266 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7267 | return -1; |
| 7268 | } |
| 7269 | return 0; |
| 7270 | } |
| 7271 | |
| 7272 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7273 | struct proxy *defpx, const char *file, int line, |
| 7274 | char **err) |
| 7275 | { |
| 7276 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7277 | file, line, err, &hlua_timeout_session); |
| 7278 | } |
| 7279 | |
| 7280 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7281 | struct proxy *defpx, const char *file, int line, |
| 7282 | char **err) |
| 7283 | { |
| 7284 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7285 | file, line, err, &hlua_timeout_task); |
| 7286 | } |
| 7287 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7288 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7289 | struct proxy *defpx, const char *file, int line, |
| 7290 | char **err) |
| 7291 | { |
| 7292 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7293 | file, line, err, &hlua_timeout_applet); |
| 7294 | } |
| 7295 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7296 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7297 | struct proxy *defpx, const char *file, int line, |
| 7298 | char **err) |
| 7299 | { |
| 7300 | char *error; |
| 7301 | |
| 7302 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7303 | if (*error != '\0') { |
| 7304 | memprintf(err, "%s: invalid number", args[0]); |
| 7305 | return -1; |
| 7306 | } |
| 7307 | return 0; |
| 7308 | } |
| 7309 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7310 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7311 | struct proxy *defpx, const char *file, int line, |
| 7312 | char **err) |
| 7313 | { |
| 7314 | char *error; |
| 7315 | |
| 7316 | if (*(args[1]) == 0) { |
| 7317 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7318 | return -1; |
| 7319 | } |
| 7320 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7321 | if (*error != '\0') { |
| 7322 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7323 | return -1; |
| 7324 | } |
| 7325 | return 0; |
| 7326 | } |
| 7327 | |
| 7328 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7329 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7330 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7331 | * the main lua entry point. |
| 7332 | * |
| 7333 | * This funtion runs with the HAProxy keywords API. It returns -1 if an error is |
| 7334 | * occured, otherwise it returns 0. |
| 7335 | * |
| 7336 | * In some error case, LUA set an error message in top of the stack. This function |
| 7337 | * 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] | 7338 | * |
| 7339 | * This function can fail with an abort() due to an Lua critical error. |
| 7340 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7341 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7342 | */ |
| 7343 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7344 | struct proxy *defpx, const char *file, int line, |
| 7345 | char **err) |
| 7346 | { |
| 7347 | int error; |
| 7348 | |
| 7349 | /* Just load and compile the file. */ |
| 7350 | error = luaL_loadfile(gL.T, args[1]); |
| 7351 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7352 | 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] | 7353 | lua_pop(gL.T, 1); |
| 7354 | return -1; |
| 7355 | } |
| 7356 | |
| 7357 | /* If no syntax error where detected, execute the code. */ |
| 7358 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7359 | switch (error) { |
| 7360 | case LUA_OK: |
| 7361 | break; |
| 7362 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7363 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7364 | lua_pop(gL.T, 1); |
| 7365 | return -1; |
| 7366 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7367 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7368 | return -1; |
| 7369 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7370 | 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] | 7371 | lua_pop(gL.T, 1); |
| 7372 | return -1; |
| 7373 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7374 | 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] | 7375 | lua_pop(gL.T, 1); |
| 7376 | return -1; |
| 7377 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7378 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7379 | lua_pop(gL.T, 1); |
| 7380 | return -1; |
| 7381 | } |
| 7382 | |
| 7383 | return 0; |
| 7384 | } |
| 7385 | |
| 7386 | /* configuration keywords declaration */ |
| 7387 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7388 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7389 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7390 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7391 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7392 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7393 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7394 | { 0, NULL, NULL }, |
| 7395 | }}; |
| 7396 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7397 | /* This function can fail with an abort() due to an Lua critical error. |
| 7398 | * We are in the initialisation process of HAProxy, this abort() is |
| 7399 | * tolerated. |
| 7400 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7401 | int hlua_post_init() |
| 7402 | { |
| 7403 | struct hlua_init_function *init; |
| 7404 | const char *msg; |
| 7405 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7406 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7407 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7408 | /* Call post initialisation function in safe environement. */ |
| 7409 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7410 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7411 | error = lua_tostring(gL.T, -1); |
| 7412 | else |
| 7413 | error = "critical error"; |
| 7414 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7415 | exit(1); |
| 7416 | } |
| 7417 | hlua_fcn_post_init(gL.T); |
| 7418 | RESET_SAFE_LJMP(gL.T); |
| 7419 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7420 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7421 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7422 | ret = hlua_ctx_resume(&gL, 0); |
| 7423 | switch (ret) { |
| 7424 | case HLUA_E_OK: |
| 7425 | lua_pop(gL.T, -1); |
| 7426 | return 1; |
| 7427 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7428 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7429 | return 0; |
| 7430 | case HLUA_E_ERRMSG: |
| 7431 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7432 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7433 | return 0; |
| 7434 | case HLUA_E_ERR: |
| 7435 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7436 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7437 | return 0; |
| 7438 | } |
| 7439 | } |
| 7440 | return 1; |
| 7441 | } |
| 7442 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7443 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7444 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7445 | * is the previously allocated size or the kind of object in case of a new |
| 7446 | * allocation. <nsize> is the requested new size. |
| 7447 | */ |
| 7448 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7449 | { |
| 7450 | struct hlua_mem_allocator *zone = ud; |
| 7451 | |
| 7452 | if (nsize == 0) { |
| 7453 | /* it's a free */ |
| 7454 | if (ptr) |
| 7455 | zone->allocated -= osize; |
| 7456 | free(ptr); |
| 7457 | return NULL; |
| 7458 | } |
| 7459 | |
| 7460 | if (!ptr) { |
| 7461 | /* it's a new allocation */ |
| 7462 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7463 | return NULL; |
| 7464 | |
| 7465 | ptr = malloc(nsize); |
| 7466 | if (ptr) |
| 7467 | zone->allocated += nsize; |
| 7468 | return ptr; |
| 7469 | } |
| 7470 | |
| 7471 | /* it's a realloc */ |
| 7472 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7473 | return NULL; |
| 7474 | |
| 7475 | ptr = realloc(ptr, nsize); |
| 7476 | if (ptr) |
| 7477 | zone->allocated += nsize - osize; |
| 7478 | return ptr; |
| 7479 | } |
| 7480 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7481 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7482 | * We are in the initialisation process of HAProxy, this abort() is |
| 7483 | * tolerated. |
| 7484 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7485 | void hlua_init(void) |
| 7486 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7487 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7488 | int idx; |
| 7489 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7490 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7491 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7492 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7493 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7494 | struct srv_kw *kw; |
| 7495 | int tmp_error; |
| 7496 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7497 | char *args[] = { /* SSL client configuration. */ |
| 7498 | "ssl", |
| 7499 | "verify", |
| 7500 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7501 | NULL |
| 7502 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7503 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7504 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7505 | HA_SPIN_INIT(&hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 7506 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7507 | /* Initialise struct hlua and com signals pool */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7508 | pool_head_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7509 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7510 | /* Register configuration keywords. */ |
| 7511 | cfg_register_keywords(&cfg_kws); |
| 7512 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7513 | /* Init main lua stack. */ |
| 7514 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7515 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7516 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7517 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7518 | hlua_sethlua(&gL); |
| 7519 | gL.Tref = LUA_REFNIL; |
| 7520 | gL.task = NULL; |
| 7521 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7522 | /* From this point, until the end of the initialisation fucntion, |
| 7523 | * the Lua function can fail with an abort. We are in the initialisation |
| 7524 | * process of HAProxy, this abort() is tolerated. |
| 7525 | */ |
| 7526 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7527 | /* Initialise lua. */ |
| 7528 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7529 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7530 | /* Set safe environment for the initialisation. */ |
| 7531 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7532 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7533 | error_msg = lua_tostring(gL.T, -1); |
| 7534 | else |
| 7535 | error_msg = "critical error"; |
| 7536 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7537 | exit(1); |
| 7538 | } |
| 7539 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7540 | /* |
| 7541 | * |
| 7542 | * Create "core" object. |
| 7543 | * |
| 7544 | */ |
| 7545 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7546 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7547 | lua_newtable(gL.T); |
| 7548 | |
| 7549 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7550 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7551 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7552 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7553 | /* Register special functions. */ |
| 7554 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7555 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7556 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7557 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7558 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7559 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7560 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7561 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7562 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7563 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7564 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7565 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7566 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7567 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7568 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7569 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7570 | hlua_class_function(gL.T, "log", hlua_log); |
| 7571 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7572 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7573 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7574 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7575 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7576 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7577 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7578 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7579 | |
| 7580 | /* |
| 7581 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7582 | * Register class Map |
| 7583 | * |
| 7584 | */ |
| 7585 | |
| 7586 | /* This table entry is the object "Map" base. */ |
| 7587 | lua_newtable(gL.T); |
| 7588 | |
| 7589 | /* register pattern types. */ |
| 7590 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7591 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7592 | for (i=0; i<PAT_MATCH_NUM; i++) { |
| 7593 | snprintf(trash.str, trash.size, "_%s", pat_match_names[i]); |
| 7594 | hlua_class_const_int(gL.T, trash.str, i); |
| 7595 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7596 | |
| 7597 | /* register constructor. */ |
| 7598 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7599 | |
| 7600 | /* Create and fill the metatable. */ |
| 7601 | lua_newtable(gL.T); |
| 7602 | |
| 7603 | /* Create and fille the __index entry. */ |
| 7604 | lua_pushstring(gL.T, "__index"); |
| 7605 | lua_newtable(gL.T); |
| 7606 | |
| 7607 | /* Register . */ |
| 7608 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7609 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7610 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7611 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7612 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7613 | /* Register previous table in the registry with reference and named entry. |
| 7614 | * The function hlua_register_metatable() pops the stack, so we |
| 7615 | * previously create a copy of the table. |
| 7616 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7617 | 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] | 7618 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7619 | |
| 7620 | /* Assign the metatable to the mai Map object. */ |
| 7621 | lua_setmetatable(gL.T, -2); |
| 7622 | |
| 7623 | /* Set a name to the table. */ |
| 7624 | lua_setglobal(gL.T, "Map"); |
| 7625 | |
| 7626 | /* |
| 7627 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7628 | * Register class Channel |
| 7629 | * |
| 7630 | */ |
| 7631 | |
| 7632 | /* Create and fill the metatable. */ |
| 7633 | lua_newtable(gL.T); |
| 7634 | |
| 7635 | /* Create and fille the __index entry. */ |
| 7636 | lua_pushstring(gL.T, "__index"); |
| 7637 | lua_newtable(gL.T); |
| 7638 | |
| 7639 | /* Register . */ |
| 7640 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7641 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7642 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7643 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7644 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7645 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7646 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7647 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7648 | 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] | 7649 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7650 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7651 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7652 | |
| 7653 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7654 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7655 | |
| 7656 | /* |
| 7657 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7658 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7659 | * |
| 7660 | */ |
| 7661 | |
| 7662 | /* Create and fill the metatable. */ |
| 7663 | lua_newtable(gL.T); |
| 7664 | |
| 7665 | /* Create and fille the __index entry. */ |
| 7666 | lua_pushstring(gL.T, "__index"); |
| 7667 | lua_newtable(gL.T); |
| 7668 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7669 | /* Browse existing fetches and create the associated |
| 7670 | * object method. |
| 7671 | */ |
| 7672 | sf = NULL; |
| 7673 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7674 | |
| 7675 | /* Dont register the keywork if the arguments check function are |
| 7676 | * not safe during the runtime. |
| 7677 | */ |
| 7678 | if ((sf->val_args != NULL) && |
| 7679 | (sf->val_args != val_payload_lv) && |
| 7680 | (sf->val_args != val_hdr)) |
| 7681 | continue; |
| 7682 | |
| 7683 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7684 | * by an underscore. |
| 7685 | */ |
| 7686 | strncpy(trash.str, sf->kw, trash.size); |
| 7687 | trash.str[trash.size - 1] = '\0'; |
| 7688 | for (p = trash.str; *p; p++) |
| 7689 | if (*p == '.' || *p == '-' || *p == '+') |
| 7690 | *p = '_'; |
| 7691 | |
| 7692 | /* Register the function. */ |
| 7693 | lua_pushstring(gL.T, trash.str); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7694 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7695 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7696 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7697 | } |
| 7698 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7699 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7700 | |
| 7701 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7702 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7703 | |
| 7704 | /* |
| 7705 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7706 | * Register class Converters |
| 7707 | * |
| 7708 | */ |
| 7709 | |
| 7710 | /* Create and fill the metatable. */ |
| 7711 | lua_newtable(gL.T); |
| 7712 | |
| 7713 | /* Create and fill the __index entry. */ |
| 7714 | lua_pushstring(gL.T, "__index"); |
| 7715 | lua_newtable(gL.T); |
| 7716 | |
| 7717 | /* Browse existing converters and create the associated |
| 7718 | * object method. |
| 7719 | */ |
| 7720 | sc = NULL; |
| 7721 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7722 | /* Dont register the keywork if the arguments check function are |
| 7723 | * not safe during the runtime. |
| 7724 | */ |
| 7725 | if (sc->val_args != NULL) |
| 7726 | continue; |
| 7727 | |
| 7728 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7729 | * by an underscore. |
| 7730 | */ |
| 7731 | strncpy(trash.str, sc->kw, trash.size); |
| 7732 | trash.str[trash.size - 1] = '\0'; |
| 7733 | for (p = trash.str; *p; p++) |
| 7734 | if (*p == '.' || *p == '-' || *p == '+') |
| 7735 | *p = '_'; |
| 7736 | |
| 7737 | /* Register the function. */ |
| 7738 | lua_pushstring(gL.T, trash.str); |
| 7739 | lua_pushlightuserdata(gL.T, sc); |
| 7740 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7741 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7742 | } |
| 7743 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7744 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7745 | |
| 7746 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7747 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7748 | |
| 7749 | /* |
| 7750 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7751 | * Register class HTTP |
| 7752 | * |
| 7753 | */ |
| 7754 | |
| 7755 | /* Create and fill the metatable. */ |
| 7756 | lua_newtable(gL.T); |
| 7757 | |
| 7758 | /* Create and fille the __index entry. */ |
| 7759 | lua_pushstring(gL.T, "__index"); |
| 7760 | lua_newtable(gL.T); |
| 7761 | |
| 7762 | /* Register Lua functions. */ |
| 7763 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7764 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7765 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7766 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7767 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7768 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7769 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7770 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7771 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7772 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7773 | |
| 7774 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7775 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7776 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7777 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7778 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7779 | 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] | 7780 | 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] | 7781 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7782 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7783 | |
| 7784 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7785 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7786 | |
| 7787 | /* |
| 7788 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7789 | * Register class AppletTCP |
| 7790 | * |
| 7791 | */ |
| 7792 | |
| 7793 | /* Create and fill the metatable. */ |
| 7794 | lua_newtable(gL.T); |
| 7795 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7796 | /* Create and fille the __index entry. */ |
| 7797 | lua_pushstring(gL.T, "__index"); |
| 7798 | lua_newtable(gL.T); |
| 7799 | |
| 7800 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7801 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7802 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7803 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7804 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7805 | 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] | 7806 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7807 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7808 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7809 | |
| 7810 | lua_settable(gL.T, -3); |
| 7811 | |
| 7812 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7813 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7814 | |
| 7815 | /* |
| 7816 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7817 | * Register class AppletHTTP |
| 7818 | * |
| 7819 | */ |
| 7820 | |
| 7821 | /* Create and fill the metatable. */ |
| 7822 | lua_newtable(gL.T); |
| 7823 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7824 | /* Create and fille the __index entry. */ |
| 7825 | lua_pushstring(gL.T, "__index"); |
| 7826 | lua_newtable(gL.T); |
| 7827 | |
| 7828 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7829 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7830 | 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] | 7831 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7832 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7833 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7834 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7835 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7836 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7837 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7838 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7839 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 7840 | |
| 7841 | lua_settable(gL.T, -3); |
| 7842 | |
| 7843 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7844 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7845 | |
| 7846 | /* |
| 7847 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7848 | * Register class TXN |
| 7849 | * |
| 7850 | */ |
| 7851 | |
| 7852 | /* Create and fill the metatable. */ |
| 7853 | lua_newtable(gL.T); |
| 7854 | |
| 7855 | /* Create and fille the __index entry. */ |
| 7856 | lua_pushstring(gL.T, "__index"); |
| 7857 | lua_newtable(gL.T); |
| 7858 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7859 | /* Register Lua functions. */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7860 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 7861 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7862 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7863 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7864 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7865 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7866 | hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel); |
| 7867 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 7868 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7869 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 7870 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 7871 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 7872 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 7873 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 7874 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7875 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7876 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7877 | |
| 7878 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7879 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7880 | |
| 7881 | /* |
| 7882 | * |
| 7883 | * Register class Socket |
| 7884 | * |
| 7885 | */ |
| 7886 | |
| 7887 | /* Create and fill the metatable. */ |
| 7888 | lua_newtable(gL.T); |
| 7889 | |
| 7890 | /* Create and fille the __index entry. */ |
| 7891 | lua_pushstring(gL.T, "__index"); |
| 7892 | lua_newtable(gL.T); |
| 7893 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7894 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7895 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7896 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7897 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 7898 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 7899 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 7900 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 7901 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 7902 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 7903 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 7904 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 7905 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7906 | 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] | 7907 | |
| 7908 | /* Register the garbage collector entry. */ |
| 7909 | lua_pushstring(gL.T, "__gc"); |
| 7910 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7911 | 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] | 7912 | |
| 7913 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7914 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7915 | |
| 7916 | /* Proxy and server configuration initialisation. */ |
| 7917 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 7918 | init_new_proxy(&socket_proxy); |
| 7919 | socket_proxy.parent = NULL; |
| 7920 | socket_proxy.last_change = now.tv_sec; |
| 7921 | socket_proxy.id = "LUA-SOCKET"; |
| 7922 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 7923 | socket_proxy.maxconn = 0; |
| 7924 | socket_proxy.accept = NULL; |
| 7925 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 7926 | socket_proxy.srv = NULL; |
| 7927 | socket_proxy.conn_retries = 0; |
| 7928 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 7929 | |
| 7930 | /* Init TCP server: unchanged parameters */ |
| 7931 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 7932 | socket_tcp.next = NULL; |
| 7933 | socket_tcp.proxy = &socket_proxy; |
| 7934 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 7935 | LIST_INIT(&socket_tcp.actconns); |
| 7936 | LIST_INIT(&socket_tcp.pendconns); |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 7937 | socket_tcp.priv_conns = NULL; |
| 7938 | socket_tcp.idle_conns = NULL; |
| 7939 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7940 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7941 | socket_tcp.last_change = 0; |
| 7942 | socket_tcp.id = "LUA-TCP-CONN"; |
| 7943 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7944 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7945 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 7946 | |
| 7947 | /* XXX: Copy default parameter from default server, |
| 7948 | * but the default server is not initialized. |
| 7949 | */ |
| 7950 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7951 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 7952 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 7953 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 7954 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 7955 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7956 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7957 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7958 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 7959 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 7960 | |
| 7961 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 7962 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 7963 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 7964 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 7965 | socket_tcp.check.server = &socket_tcp; |
| 7966 | |
| 7967 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 7968 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7969 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7970 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 7971 | socket_tcp.agent.server = &socket_tcp; |
| 7972 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 7973 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7974 | |
| 7975 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7976 | /* Init TCP server: unchanged parameters */ |
| 7977 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 7978 | socket_ssl.next = NULL; |
| 7979 | socket_ssl.proxy = &socket_proxy; |
| 7980 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 7981 | LIST_INIT(&socket_ssl.actconns); |
| 7982 | LIST_INIT(&socket_ssl.pendconns); |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 7983 | socket_tcp.priv_conns = NULL; |
| 7984 | socket_tcp.idle_conns = NULL; |
| 7985 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7986 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7987 | socket_ssl.last_change = 0; |
| 7988 | socket_ssl.id = "LUA-SSL-CONN"; |
| 7989 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7990 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7991 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 7992 | |
| 7993 | /* XXX: Copy default parameter from default server, |
| 7994 | * but the default server is not initialized. |
| 7995 | */ |
| 7996 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7997 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 7998 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 7999 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8000 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8001 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8002 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8003 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8004 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8005 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8006 | |
| 8007 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8008 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8009 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8010 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8011 | socket_ssl.check.server = &socket_ssl; |
| 8012 | |
| 8013 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8014 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8015 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8016 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8017 | socket_ssl.agent.server = &socket_ssl; |
| 8018 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8019 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8020 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8021 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8022 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8023 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8024 | /* |
| 8025 | * |
| 8026 | * If the keyword is not known, we can search in the registered |
| 8027 | * server keywords. This is usefull to configure special SSL |
| 8028 | * features like client certificates and ssl_verify. |
| 8029 | * |
| 8030 | */ |
| 8031 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8032 | if (tmp_error != 0) { |
| 8033 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8034 | abort(); /* This must be never arrives because the command line |
| 8035 | not editable by the user. */ |
| 8036 | } |
| 8037 | idx += kw->skip; |
| 8038 | } |
| 8039 | } |
| 8040 | |
| 8041 | /* Initialize SSL server. */ |
Willy Tarreau | cbe6da5 | 2018-05-18 17:08:28 +0200 | [diff] [blame] | 8042 | if (socket_ssl.xprt->prepare_srv) { |
| 8043 | int saved_used_backed = global.ssl_used_backend; |
| 8044 | // don't affect maxconn automatic computation |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 8045 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
Willy Tarreau | cbe6da5 | 2018-05-18 17:08:28 +0200 | [diff] [blame] | 8046 | global.ssl_used_backend = saved_used_backed; |
| 8047 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8048 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8049 | |
| 8050 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8051 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8052 | |
| 8053 | __attribute__((constructor)) |
| 8054 | static void __hlua_init(void) |
| 8055 | { |
| 8056 | char *ptr = NULL; |
| 8057 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8058 | hap_register_build_opts(ptr, 1); |
| 8059 | } |