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> |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 47 | #include <proto/queue.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 48 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 49 | #include <proto/payload.h> |
| 50 | #include <proto/proto_http.h> |
| 51 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 52 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 53 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 54 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 55 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 56 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 57 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 58 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 59 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 60 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 61 | * macro is used only for identifying the function that can |
| 62 | * not return because a longjmp is executed. |
| 63 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 64 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 65 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 66 | */ |
| 67 | #define __LJMP |
| 68 | #define WILL_LJMP(func) func |
| 69 | #define MAY_LJMP(func) func |
| 70 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 71 | /* This couple of function executes securely some Lua calls outside of |
| 72 | * the lua runtime environment. Each Lua call can return a longjmp |
| 73 | * if it encounter a memory error. |
| 74 | * |
| 75 | * Lua documentation extract: |
| 76 | * |
| 77 | * If an error happens outside any protected environment, Lua calls |
| 78 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 79 | * exiting the host application. Your panic function can avoid this |
| 80 | * exit by never returning (e.g., doing a long jump to your own |
| 81 | * recovery point outside Lua). |
| 82 | * |
| 83 | * The panic function runs as if it were a message handler (see |
| 84 | * §2.3); in particular, the error message is at the top of the |
| 85 | * stack. However, there is no guarantee about stack space. To push |
| 86 | * anything on the stack, the panic function must first check the |
| 87 | * available space (see §4.2). |
| 88 | * |
| 89 | * We must check all the Lua entry point. This includes: |
| 90 | * - The include/proto/hlua.h exported functions |
| 91 | * - the task wrapper function |
| 92 | * - The action wrapper function |
| 93 | * - The converters wrapper function |
| 94 | * - The sample-fetch wrapper functions |
| 95 | * |
| 96 | * It is tolerated that the initilisation function returns an abort. |
| 97 | * Before each Lua abort, an error message is writed on stderr. |
| 98 | * |
| 99 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 100 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 101 | * because they must be exists in the program stack when the longjmp |
| 102 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 103 | * |
| 104 | * Note that the Lua processing is not really thread safe. It provides |
| 105 | * heavy system which consists to add our own lock function in the Lua |
| 106 | * code and recompile the library. This system will probably not accepted |
| 107 | * by maintainers of various distribs. |
| 108 | * |
| 109 | * Our main excution point of the Lua is the function lua_resume(). A |
| 110 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 111 | * of function and a lua_unlock() at the end of the function. So I |
| 112 | * conclude that the Lua thread safe mode just perform a mutex around |
| 113 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 114 | * easier for distro maintainers. |
| 115 | * |
| 116 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 117 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 118 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 119 | */ |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 120 | __decl_hathreads(HA_SPINLOCK_T hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 121 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 122 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 123 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 124 | |
| 125 | #define SET_SAFE_LJMP(__L) \ |
| 126 | ({ \ |
| 127 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 128 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 129 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 130 | lua_atpanic(__L, hlua_panic_safe); \ |
| 131 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 132 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 133 | } else { \ |
| 134 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 135 | ret = 1; \ |
| 136 | } \ |
| 137 | ret; \ |
| 138 | }) |
| 139 | |
| 140 | /* If we are the last function catching Lua errors, we |
| 141 | * must reset the panic function. |
| 142 | */ |
| 143 | #define RESET_SAFE_LJMP(__L) \ |
| 144 | do { \ |
| 145 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 146 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 147 | } while(0) |
| 148 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 149 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 150 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
| 151 | #define APPLET_100C 0x02 /* 100 continue expected. */ |
| 152 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 153 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 154 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 155 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 156 | |
| 157 | #define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n" |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 158 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 159 | /* The main Lua execution context. */ |
| 160 | struct hlua gL; |
| 161 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 162 | /* This is the memory pool containing struct lua for applets |
| 163 | * (including cli). |
| 164 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 165 | struct pool_head *pool_head_hlua; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 166 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 167 | /* Used for Socket connection. */ |
| 168 | static struct proxy socket_proxy; |
| 169 | static struct server socket_tcp; |
| 170 | #ifdef USE_OPENSSL |
| 171 | static struct server socket_ssl; |
| 172 | #endif |
| 173 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 174 | /* List head of the function called at the initialisation time. */ |
| 175 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 176 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 177 | /* The following variables contains the reference of the different |
| 178 | * Lua classes. These references are useful for identify metadata |
| 179 | * associated with an object. |
| 180 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 181 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 182 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 183 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 184 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 185 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 186 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 187 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 188 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 189 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 190 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 191 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 192 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 193 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 194 | * because a task may remain alive during all the haproxy execution. |
| 195 | */ |
| 196 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 197 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 198 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 199 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 200 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 201 | * it is used for preventing infinite loops. |
| 202 | * |
| 203 | * I test the scheer with an infinite loop containing one incrementation |
| 204 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 205 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 206 | * to one interrupt each 10 000 instructions. |
| 207 | * |
| 208 | * configured | Number of |
| 209 | * instructions | loops executed |
| 210 | * between two | in milions |
| 211 | * forced yields | |
| 212 | * ---------------+--------------- |
| 213 | * 10 | 160 |
| 214 | * 500 | 670 |
| 215 | * 1000 | 680 |
| 216 | * 5000 | 700 |
| 217 | * 7000 | 700 |
| 218 | * 8000 | 700 |
| 219 | * 9000 | 710 <- ceil |
| 220 | * 10000 | 710 |
| 221 | * 100000 | 710 |
| 222 | * 1000000 | 710 |
| 223 | * |
| 224 | */ |
| 225 | static unsigned int hlua_nb_instruction = 10000; |
| 226 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 227 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 228 | * be enforced on any memory allocation. |
| 229 | */ |
| 230 | struct hlua_mem_allocator { |
| 231 | size_t allocated; |
| 232 | size_t limit; |
| 233 | }; |
| 234 | |
| 235 | static struct hlua_mem_allocator hlua_global_allocator; |
| 236 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 237 | static const char error_500[] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 238 | "HTTP/1.0 500 Internal Server Error\r\n" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 239 | "Cache-Control: no-cache\r\n" |
| 240 | "Connection: close\r\n" |
| 241 | "Content-Type: text/html\r\n" |
| 242 | "\r\n" |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 243 | "<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] | 244 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 245 | /* These functions converts types between HAProxy internal args or |
| 246 | * sample and LUA types. Another function permits to check if the |
| 247 | * LUA stack contains arguments according with an required ARG_T |
| 248 | * format. |
| 249 | */ |
| 250 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 251 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 252 | __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] | 253 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 254 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 255 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 256 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 257 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 258 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 259 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 260 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 261 | do { \ |
| 262 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 263 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 264 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 265 | } while (0) |
| 266 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 267 | /* Used to check an Lua function type in the stack. It creates and |
| 268 | * returns a reference of the function. This function throws an |
| 269 | * error if the rgument is not a "function". |
| 270 | */ |
| 271 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 272 | { |
| 273 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 274 | 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] | 275 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 276 | } |
| 277 | lua_pushvalue(L, argno); |
| 278 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 279 | } |
| 280 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 281 | /* Return the string that is of the top of the stack. */ |
| 282 | const char *hlua_get_top_error_string(lua_State *L) |
| 283 | { |
| 284 | if (lua_gettop(L) < 1) |
| 285 | return "unknown error"; |
| 286 | if (lua_type(L, -1) != LUA_TSTRING) |
| 287 | return "unknown error"; |
| 288 | return lua_tostring(L, -1); |
| 289 | } |
| 290 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 291 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 292 | { |
| 293 | lua_Debug ar; |
| 294 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 295 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 296 | int filled = 0; |
| 297 | |
| 298 | while (lua_getstack(L, level++, &ar)) { |
| 299 | |
| 300 | /* Add separator */ |
| 301 | if (filled) |
| 302 | chunk_appendf(msg, ", "); |
| 303 | filled = 1; |
| 304 | |
| 305 | /* Fill fields: |
| 306 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 307 | * 'l': fills in the field currentline; |
| 308 | * 'n': fills in the field name and namewhat; |
| 309 | * 't': fills in the field istailcall; |
| 310 | */ |
| 311 | lua_getinfo(L, "Slnt", &ar); |
| 312 | |
| 313 | /* Append code localisation */ |
| 314 | if (ar.currentline > 0) |
| 315 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 316 | else |
| 317 | chunk_appendf(msg, "%s ", ar.short_src); |
| 318 | |
| 319 | /* |
| 320 | * Get function name |
| 321 | * |
| 322 | * if namewhat is no empty, name is defined. |
| 323 | * what contains "Lua" for Lua function, "C" for C function, |
| 324 | * or "main" for main code. |
| 325 | */ |
| 326 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 327 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 328 | |
| 329 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 330 | chunk_appendf(msg, "main chunk"); |
| 331 | |
| 332 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 333 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 334 | |
| 335 | else /* nothing left... */ |
| 336 | chunk_appendf(msg, "?"); |
| 337 | |
| 338 | |
| 339 | /* Display tailed call */ |
| 340 | if (ar.istailcall) |
| 341 | chunk_appendf(msg, " ..."); |
| 342 | } |
| 343 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 344 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 348 | /* This function check the number of arguments available in the |
| 349 | * stack. If the number of arguments available is not the same |
| 350 | * then <nb> an error is throwed. |
| 351 | */ |
| 352 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 353 | { |
| 354 | if (lua_gettop(L) == nb) |
| 355 | return; |
| 356 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 357 | } |
| 358 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 359 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 360 | * and the line number where the error is encountered. |
| 361 | */ |
| 362 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 363 | { |
| 364 | va_list argp; |
| 365 | va_start(argp, fmt); |
| 366 | luaL_where(L, 1); |
| 367 | lua_pushvfstring(L, fmt, argp); |
| 368 | va_end(argp); |
| 369 | lua_concat(L, 2); |
| 370 | return 1; |
| 371 | } |
| 372 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 373 | /* This functions is used with sample fetch and converters. It |
| 374 | * converts the HAProxy configuration argument in a lua stack |
| 375 | * values. |
| 376 | * |
| 377 | * It takes an array of "arg", and each entry of the array is |
| 378 | * converted and pushed in the LUA stack. |
| 379 | */ |
| 380 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 381 | { |
| 382 | switch (arg->type) { |
| 383 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 384 | case ARGT_TIME: |
| 385 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 386 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 387 | break; |
| 388 | |
| 389 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 390 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 391 | break; |
| 392 | |
| 393 | case ARGT_IPV4: |
| 394 | case ARGT_IPV6: |
| 395 | case ARGT_MSK4: |
| 396 | case ARGT_MSK6: |
| 397 | case ARGT_FE: |
| 398 | case ARGT_BE: |
| 399 | case ARGT_TAB: |
| 400 | case ARGT_SRV: |
| 401 | case ARGT_USR: |
| 402 | case ARGT_MAP: |
| 403 | default: |
| 404 | lua_pushnil(L); |
| 405 | break; |
| 406 | } |
| 407 | return 1; |
| 408 | } |
| 409 | |
| 410 | /* This function take one entrie in an LUA stack at the index "ud", |
| 411 | * and try to convert it in an HAProxy argument entry. This is useful |
| 412 | * with sample fetch wrappers. The input arguments are gived to the |
| 413 | * lua wrapper and converted as arg list by thi function. |
| 414 | */ |
| 415 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 416 | { |
| 417 | switch (lua_type(L, ud)) { |
| 418 | |
| 419 | case LUA_TNUMBER: |
| 420 | case LUA_TBOOLEAN: |
| 421 | arg->type = ARGT_SINT; |
| 422 | arg->data.sint = lua_tointeger(L, ud); |
| 423 | break; |
| 424 | |
| 425 | case LUA_TSTRING: |
| 426 | arg->type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 427 | arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 428 | break; |
| 429 | |
| 430 | case LUA_TUSERDATA: |
| 431 | case LUA_TNIL: |
| 432 | case LUA_TTABLE: |
| 433 | case LUA_TFUNCTION: |
| 434 | case LUA_TTHREAD: |
| 435 | case LUA_TLIGHTUSERDATA: |
| 436 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 437 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 438 | break; |
| 439 | } |
| 440 | return 1; |
| 441 | } |
| 442 | |
| 443 | /* the following functions are used to convert a struct sample |
| 444 | * in Lua type. This useful to convert the return of the |
| 445 | * fetchs or converters. |
| 446 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 447 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 448 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 449 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 450 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 451 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 452 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 453 | break; |
| 454 | |
| 455 | case SMP_T_BIN: |
| 456 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 457 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 458 | break; |
| 459 | |
| 460 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 461 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 462 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 463 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 464 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 465 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 466 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 467 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 468 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 469 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 470 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 471 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 472 | break; |
| 473 | default: |
| 474 | lua_pushnil(L); |
| 475 | break; |
| 476 | } |
| 477 | break; |
| 478 | |
| 479 | case SMP_T_IPV4: |
| 480 | case SMP_T_IPV6: |
| 481 | 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] | 482 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 483 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 484 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 485 | else |
| 486 | lua_pushnil(L); |
| 487 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 488 | default: |
| 489 | lua_pushnil(L); |
| 490 | break; |
| 491 | } |
| 492 | return 1; |
| 493 | } |
| 494 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 495 | /* the following functions are used to convert a struct sample |
| 496 | * in Lua strings. This is useful to convert the return of the |
| 497 | * fetchs or converters. |
| 498 | */ |
| 499 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 500 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 501 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 502 | |
| 503 | case SMP_T_BIN: |
| 504 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 505 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 506 | break; |
| 507 | |
| 508 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 509 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 510 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 511 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 512 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 513 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 514 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 515 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 516 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 517 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 518 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 519 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 520 | break; |
| 521 | default: |
| 522 | lua_pushstring(L, ""); |
| 523 | break; |
| 524 | } |
| 525 | break; |
| 526 | |
| 527 | case SMP_T_SINT: |
| 528 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 529 | case SMP_T_IPV4: |
| 530 | case SMP_T_IPV6: |
| 531 | 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] | 532 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 533 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 534 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 535 | else |
| 536 | lua_pushstring(L, ""); |
| 537 | break; |
| 538 | default: |
| 539 | lua_pushstring(L, ""); |
| 540 | break; |
| 541 | } |
| 542 | return 1; |
| 543 | } |
| 544 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 545 | /* the following functions are used to convert an Lua type in a |
| 546 | * struct sample. This is useful to provide data from a converter |
| 547 | * to the LUA code. |
| 548 | */ |
| 549 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 550 | { |
| 551 | switch (lua_type(L, ud)) { |
| 552 | |
| 553 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 554 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 555 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 556 | break; |
| 557 | |
| 558 | |
| 559 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 560 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 561 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 562 | break; |
| 563 | |
| 564 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 565 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 566 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 567 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 568 | break; |
| 569 | |
| 570 | case LUA_TUSERDATA: |
| 571 | case LUA_TNIL: |
| 572 | case LUA_TTABLE: |
| 573 | case LUA_TFUNCTION: |
| 574 | case LUA_TTHREAD: |
| 575 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 576 | case LUA_TNONE: |
| 577 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 578 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 579 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 580 | break; |
| 581 | } |
| 582 | return 1; |
| 583 | } |
| 584 | |
| 585 | /* This function check the "argp" builded by another conversion function |
| 586 | * is in accord with the expected argp defined by the "mask". The fucntion |
| 587 | * 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] | 588 | * |
| 589 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 590 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 591 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 592 | __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] | 593 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 594 | { |
| 595 | int min_arg; |
| 596 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 597 | struct proxy *px; |
| 598 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 599 | |
| 600 | idx = 0; |
| 601 | min_arg = ARGM(mask); |
| 602 | mask >>= ARGM_BITS; |
| 603 | |
| 604 | while (1) { |
| 605 | |
| 606 | /* Check oversize. */ |
| 607 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 608 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | /* Check for mandatory arguments. */ |
| 612 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 613 | if (idx < min_arg) { |
| 614 | |
| 615 | /* If miss other argument than the first one, we return an error. */ |
| 616 | if (idx > 0) |
| 617 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 618 | |
| 619 | /* If first argument have a certain type, some default values |
| 620 | * may be used. See the function smp_resolve_args(). |
| 621 | */ |
| 622 | switch (mask & ARGT_MASK) { |
| 623 | |
| 624 | case ARGT_FE: |
| 625 | if (!(p->cap & PR_CAP_FE)) |
| 626 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 627 | argp[idx].data.prx = p; |
| 628 | argp[idx].type = ARGT_FE; |
| 629 | argp[idx+1].type = ARGT_STOP; |
| 630 | break; |
| 631 | |
| 632 | case ARGT_BE: |
| 633 | if (!(p->cap & PR_CAP_BE)) |
| 634 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 635 | argp[idx].data.prx = p; |
| 636 | argp[idx].type = ARGT_BE; |
| 637 | argp[idx+1].type = ARGT_STOP; |
| 638 | break; |
| 639 | |
| 640 | case ARGT_TAB: |
| 641 | argp[idx].data.prx = p; |
| 642 | argp[idx].type = ARGT_TAB; |
| 643 | argp[idx+1].type = ARGT_STOP; |
| 644 | break; |
| 645 | |
| 646 | default: |
| 647 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 648 | break; |
| 649 | } |
| 650 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | /* Check for exceed the number of requiered argument. */ |
| 655 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 656 | argp[idx].type != ARGT_STOP) { |
| 657 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 658 | } |
| 659 | |
| 660 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 661 | argp[idx].type == ARGT_STOP) { |
| 662 | return 0; |
| 663 | } |
| 664 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 665 | /* Convert some argument types. */ |
| 666 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 667 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 668 | if (argp[idx].type != ARGT_SINT) |
| 669 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 670 | argp[idx].type = ARGT_SINT; |
| 671 | break; |
| 672 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 673 | case ARGT_TIME: |
| 674 | if (argp[idx].type != ARGT_SINT) |
| 675 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 676 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 677 | break; |
| 678 | |
| 679 | case ARGT_SIZE: |
| 680 | if (argp[idx].type != ARGT_SINT) |
| 681 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 682 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 683 | break; |
| 684 | |
| 685 | case ARGT_FE: |
| 686 | if (argp[idx].type != ARGT_STR) |
| 687 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 688 | memcpy(trash.area, argp[idx].data.str.area, |
| 689 | argp[idx].data.str.data); |
| 690 | trash.area[argp[idx].data.str.data] = 0; |
| 691 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 692 | if (!argp[idx].data.prx) |
| 693 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 694 | argp[idx].type = ARGT_FE; |
| 695 | break; |
| 696 | |
| 697 | case ARGT_BE: |
| 698 | if (argp[idx].type != ARGT_STR) |
| 699 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 700 | memcpy(trash.area, argp[idx].data.str.area, |
| 701 | argp[idx].data.str.data); |
| 702 | trash.area[argp[idx].data.str.data] = 0; |
| 703 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 704 | if (!argp[idx].data.prx) |
| 705 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 706 | argp[idx].type = ARGT_BE; |
| 707 | break; |
| 708 | |
| 709 | case ARGT_TAB: |
| 710 | if (argp[idx].type != ARGT_STR) |
| 711 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 712 | memcpy(trash.area, argp[idx].data.str.area, |
| 713 | argp[idx].data.str.data); |
| 714 | trash.area[argp[idx].data.str.data] = 0; |
| 715 | argp[idx].data.prx = proxy_tbl_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 716 | if (!argp[idx].data.prx) |
| 717 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 718 | argp[idx].type = ARGT_TAB; |
| 719 | break; |
| 720 | |
| 721 | case ARGT_SRV: |
| 722 | if (argp[idx].type != ARGT_STR) |
| 723 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 724 | memcpy(trash.area, argp[idx].data.str.area, |
| 725 | argp[idx].data.str.data); |
| 726 | trash.area[argp[idx].data.str.data] = 0; |
| 727 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 728 | if (sname) { |
| 729 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 730 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 731 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 732 | if (!px) |
| 733 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 734 | } |
| 735 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 736 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 737 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 738 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 739 | argp[idx].data.srv = findserver(px, sname); |
| 740 | if (!argp[idx].data.srv) |
| 741 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 742 | argp[idx].type = ARGT_SRV; |
| 743 | break; |
| 744 | |
| 745 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 746 | memcpy(trash.area, argp[idx].data.str.area, |
| 747 | argp[idx].data.str.data); |
| 748 | trash.area[argp[idx].data.str.data] = 0; |
| 749 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 750 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 751 | argp[idx].type = ARGT_IPV4; |
| 752 | break; |
| 753 | |
| 754 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 755 | memcpy(trash.area, argp[idx].data.str.area, |
| 756 | argp[idx].data.str.data); |
| 757 | trash.area[argp[idx].data.str.data] = 0; |
| 758 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 759 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 760 | argp[idx].type = ARGT_MSK4; |
| 761 | break; |
| 762 | |
| 763 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 764 | memcpy(trash.area, argp[idx].data.str.area, |
| 765 | argp[idx].data.str.data); |
| 766 | trash.area[argp[idx].data.str.data] = 0; |
| 767 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 768 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 769 | argp[idx].type = ARGT_IPV6; |
| 770 | break; |
| 771 | |
| 772 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 773 | memcpy(trash.area, argp[idx].data.str.area, |
| 774 | argp[idx].data.str.data); |
| 775 | trash.area[argp[idx].data.str.data] = 0; |
| 776 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 777 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 778 | argp[idx].type = ARGT_MSK6; |
| 779 | break; |
| 780 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 781 | case ARGT_MAP: |
| 782 | case ARGT_REG: |
| 783 | case ARGT_USR: |
| 784 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 785 | break; |
| 786 | } |
| 787 | |
| 788 | /* Check for type of argument. */ |
| 789 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 790 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 791 | arg_type_names[(mask & ARGT_MASK)], |
| 792 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 793 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 794 | } |
| 795 | |
| 796 | /* Next argument. */ |
| 797 | mask >>= ARGT_BITS; |
| 798 | idx++; |
| 799 | } |
| 800 | } |
| 801 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 802 | /* |
| 803 | * The following functions are used to make correspondance between the the |
| 804 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 805 | * |
| 806 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 807 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 808 | */ |
| 809 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 810 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 811 | struct hlua **hlua = lua_getextraspace(L); |
| 812 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 813 | } |
| 814 | static inline void hlua_sethlua(struct hlua *hlua) |
| 815 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 816 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 817 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 818 | } |
| 819 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 820 | /* This function is used to send logs. It try to send on screen (stderr) |
| 821 | * and on the default syslog server. |
| 822 | */ |
| 823 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 824 | { |
| 825 | struct tm tm; |
| 826 | char *p; |
| 827 | |
| 828 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 829 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 830 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 831 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 832 | /* Break the message if exceed the buffer size. */ |
| 833 | *(p-4) = ' '; |
| 834 | *(p-3) = '.'; |
| 835 | *(p-2) = '.'; |
| 836 | *(p-1) = '.'; |
| 837 | break; |
| 838 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 839 | if (isprint(*msg)) |
| 840 | *p = *msg; |
| 841 | else |
| 842 | *p = '.'; |
| 843 | } |
| 844 | *p = '\0'; |
| 845 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 846 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 847 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 848 | get_localtime(date.tv_sec, &tm); |
| 849 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 850 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 851 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 852 | fflush(stderr); |
| 853 | } |
| 854 | } |
| 855 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 856 | /* This function just ensure that the yield will be always |
| 857 | * returned with a timeout and permit to set some flags |
| 858 | */ |
| 859 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 860 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 861 | { |
| 862 | struct hlua *hlua = hlua_gethlua(L); |
| 863 | |
| 864 | /* Set the wake timeout. If timeout is required, we set |
| 865 | * the expiration time. |
| 866 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 867 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 868 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 869 | hlua->flags |= flags; |
| 870 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 871 | /* Process the yield. */ |
| 872 | WILL_LJMP(lua_yieldk(L, nresults, ctx, k)); |
| 873 | } |
| 874 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 875 | /* This function initialises the Lua environment stored in the stream. |
| 876 | * 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] | 877 | * 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] | 878 | * |
| 879 | * This function is particular. it initialises a new Lua thread. If the |
| 880 | * initialisation fails (example: out of memory error), the lua function |
| 881 | * throws an error (longjmp). |
| 882 | * |
| 883 | * This function manipulates two Lua stack: the main and the thread. Only |
| 884 | * the main stack can fail. The thread is not manipulated. This function |
| 885 | * MUST NOT manipulate the created thread stack state, because is not |
| 886 | * proctected agains error throwed by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 887 | */ |
| 888 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 889 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 890 | if (!SET_SAFE_LJMP(gL.T)) { |
| 891 | lua->Tref = LUA_REFNIL; |
| 892 | return 0; |
| 893 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 894 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 895 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 896 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 897 | lua->T = lua_newthread(gL.T); |
| 898 | if (!lua->T) { |
| 899 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | 0a97620 | 2017-07-12 11:18:00 +0200 | [diff] [blame] | 900 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 901 | return 0; |
| 902 | } |
| 903 | hlua_sethlua(lua); |
| 904 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 905 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 906 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 907 | return 1; |
| 908 | } |
| 909 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 910 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 911 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 912 | * is not freed. |
| 913 | */ |
| 914 | void hlua_ctx_destroy(struct hlua *lua) |
| 915 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 916 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 917 | return; |
| 918 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 919 | if (!lua->T) |
| 920 | goto end; |
| 921 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 922 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 923 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 924 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 925 | if (!SET_SAFE_LJMP(lua->T)) |
| 926 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 927 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 928 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 929 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 930 | if (!SET_SAFE_LJMP(gL.T)) |
| 931 | return; |
| 932 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 933 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 934 | /* Forces a garbage collecting process. If the Lua program is finished |
| 935 | * without error, we run the GC on the thread pointer. Its freed all |
| 936 | * the unused memory. |
| 937 | * If the thread is finnish with an error or is currently yielded, |
| 938 | * it seems that the GC applied on the thread doesn't clean anything, |
| 939 | * so e run the GC on the main thread. |
| 940 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 941 | * the garbage collection. |
| 942 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 943 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 944 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 945 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 946 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 947 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 948 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 949 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 950 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 951 | |
| 952 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 953 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 954 | } |
| 955 | |
| 956 | /* This function is used to restore the Lua context when a coroutine |
| 957 | * fails. This function copy the common memory between old coroutine |
| 958 | * and the new coroutine. The old coroutine is destroyed, and its |
| 959 | * replaced by the new coroutine. |
| 960 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 961 | * as string error message and it is copied in the new stack. |
| 962 | */ |
| 963 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 964 | { |
| 965 | lua_State *T; |
| 966 | int new_ref; |
| 967 | |
| 968 | /* Renew the main LUA stack doesn't have sense. */ |
| 969 | if (lua == &gL) |
| 970 | return 0; |
| 971 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 972 | /* New Lua coroutine. */ |
| 973 | T = lua_newthread(gL.T); |
| 974 | if (!T) |
| 975 | return 0; |
| 976 | |
| 977 | /* Copy last error message. */ |
| 978 | if (keep_msg) |
| 979 | lua_xmove(lua->T, T, 1); |
| 980 | |
| 981 | /* Copy data between the coroutines. */ |
| 982 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 983 | lua_xmove(lua->T, T, 1); |
| 984 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 985 | |
| 986 | /* Destroy old data. */ |
| 987 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 988 | |
| 989 | /* The thread is garbage collected by Lua. */ |
| 990 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 991 | |
| 992 | /* Fill the struct with the new coroutine values. */ |
| 993 | lua->Mref = new_ref; |
| 994 | lua->T = T; |
| 995 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 996 | |
| 997 | /* Set context. */ |
| 998 | hlua_sethlua(lua); |
| 999 | |
| 1000 | return 1; |
| 1001 | } |
| 1002 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1003 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1004 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1005 | struct hlua *hlua = hlua_gethlua(L); |
| 1006 | |
| 1007 | /* Lua cannot yield when its returning from a function, |
| 1008 | * so, we can fix the interrupt hook to 1 instruction, |
| 1009 | * expecting that the function is finnished. |
| 1010 | */ |
| 1011 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1012 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1013 | return; |
| 1014 | } |
| 1015 | |
| 1016 | /* restore the interrupt condition. */ |
| 1017 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1018 | |
| 1019 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1020 | * If the state is not yieldable, trying yield causes an error. |
| 1021 | */ |
| 1022 | if (lua_isyieldable(L)) |
| 1023 | WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 1024 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1025 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1026 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1027 | hlua->run_time += now_ms - hlua->start_time; |
| 1028 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1029 | lua_pushfstring(L, "execution timeout"); |
| 1030 | WILL_LJMP(lua_error(L)); |
| 1031 | } |
| 1032 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1033 | /* Update the start time. */ |
| 1034 | hlua->start_time = now_ms; |
| 1035 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1036 | /* Try to interrupt the process at the end of the current |
| 1037 | * unyieldable function. |
| 1038 | */ |
| 1039 | 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] | 1040 | } |
| 1041 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1042 | /* This function start or resumes the Lua stack execution. If the flag |
| 1043 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1044 | * The function return an error. |
| 1045 | * |
| 1046 | * The function can returns 4 values: |
| 1047 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1048 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1049 | * task wakeup. |
| 1050 | * - HLUA_E_ERRMSG : An error has occured, an error message is set in |
| 1051 | * the top of the stack. |
| 1052 | * - HLUA_E_ERR : An error has occured without error message. |
| 1053 | * |
| 1054 | * If an error occured, the stack is renewed and it is ready to run new |
| 1055 | * LUA code. |
| 1056 | */ |
| 1057 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1058 | { |
| 1059 | int ret; |
| 1060 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1061 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1062 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1063 | /* Initialise run time counter. */ |
| 1064 | if (!HLUA_IS_RUNNING(lua)) |
| 1065 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1066 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1067 | /* Lock the whole Lua execution. This lock must be before the |
| 1068 | * label "resume_execution". |
| 1069 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1070 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1071 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1072 | resume_execution: |
| 1073 | |
| 1074 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1075 | * instructions. it is used for preventing infinite loops. |
| 1076 | */ |
| 1077 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1078 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1079 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1080 | HLUA_SET_RUN(lua); |
| 1081 | HLUA_CLR_CTRLYIELD(lua); |
| 1082 | HLUA_CLR_WAKERESWR(lua); |
| 1083 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1084 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1085 | /* Update the start time. */ |
| 1086 | lua->start_time = now_ms; |
| 1087 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1088 | /* Call the function. */ |
| 1089 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1090 | switch (ret) { |
| 1091 | |
| 1092 | case LUA_OK: |
| 1093 | ret = HLUA_E_OK; |
| 1094 | break; |
| 1095 | |
| 1096 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1097 | /* Check if the execution timeout is expired. It it is the case, we |
| 1098 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1099 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1100 | tv_update_date(0, 1); |
| 1101 | lua->run_time += now_ms - lua->start_time; |
| 1102 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1103 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1104 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1105 | break; |
| 1106 | } |
| 1107 | /* Process the forced yield. if the general yield is not allowed or |
| 1108 | * if no task were associated this the current Lua execution |
| 1109 | * coroutine, we resume the execution. Else we want to return in the |
| 1110 | * scheduler and we want to be waked up again, to continue the |
| 1111 | * current Lua execution. So we schedule our own task. |
| 1112 | */ |
| 1113 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1114 | if (!yield_allowed || !lua->task) |
| 1115 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1116 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1117 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1118 | if (!yield_allowed) { |
| 1119 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1120 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1121 | break; |
| 1122 | } |
| 1123 | ret = HLUA_E_AGAIN; |
| 1124 | break; |
| 1125 | |
| 1126 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1127 | |
| 1128 | /* Special exit case. The traditionnal exit is returned as an error |
| 1129 | * because the errors ares the only one mean to return immediately |
| 1130 | * from and lua execution. |
| 1131 | */ |
| 1132 | if (lua->flags & HLUA_EXIT) { |
| 1133 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1134 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1135 | break; |
| 1136 | } |
| 1137 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1138 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1139 | if (!lua_checkstack(lua->T, 1)) { |
| 1140 | ret = HLUA_E_ERR; |
| 1141 | break; |
| 1142 | } |
| 1143 | msg = lua_tostring(lua->T, -1); |
| 1144 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1145 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1146 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1147 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1148 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1149 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1150 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1151 | ret = HLUA_E_ERRMSG; |
| 1152 | break; |
| 1153 | |
| 1154 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1155 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1156 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1157 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1158 | break; |
| 1159 | |
| 1160 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1161 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1162 | if (!lua_checkstack(lua->T, 1)) { |
| 1163 | ret = HLUA_E_ERR; |
| 1164 | break; |
| 1165 | } |
| 1166 | msg = lua_tostring(lua->T, -1); |
| 1167 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1168 | lua_pop(lua->T, 1); |
| 1169 | if (msg) |
| 1170 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1171 | else |
| 1172 | lua_pushfstring(lua->T, "message handler error"); |
| 1173 | ret = HLUA_E_ERRMSG; |
| 1174 | break; |
| 1175 | |
| 1176 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1177 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1178 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1179 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1180 | break; |
| 1181 | } |
| 1182 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1183 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1184 | if (lua->flags & HLUA_MUST_GC && |
| 1185 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1186 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1187 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1188 | switch (ret) { |
| 1189 | case HLUA_E_AGAIN: |
| 1190 | break; |
| 1191 | |
| 1192 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1193 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1194 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1195 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1196 | break; |
| 1197 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1198 | case HLUA_E_ETMOUT: |
| 1199 | case HLUA_E_NOMEM: |
| 1200 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1201 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1202 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1203 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1204 | hlua_ctx_renew(lua, 0); |
| 1205 | break; |
| 1206 | |
| 1207 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1208 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1209 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1210 | break; |
| 1211 | } |
| 1212 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1213 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1214 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1215 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1216 | return ret; |
| 1217 | } |
| 1218 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1219 | /* This function exit the current code. */ |
| 1220 | __LJMP static int hlua_done(lua_State *L) |
| 1221 | { |
| 1222 | struct hlua *hlua = hlua_gethlua(L); |
| 1223 | |
| 1224 | hlua->flags |= HLUA_EXIT; |
| 1225 | WILL_LJMP(lua_error(L)); |
| 1226 | |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1230 | /* This function is an LUA binding. It provides a function |
| 1231 | * for deleting ACL from a referenced ACL file. |
| 1232 | */ |
| 1233 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1234 | { |
| 1235 | const char *name; |
| 1236 | const char *key; |
| 1237 | struct pat_ref *ref; |
| 1238 | |
| 1239 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1240 | |
| 1241 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1242 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1243 | |
| 1244 | ref = pat_ref_lookup(name); |
| 1245 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1246 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1247 | |
| 1248 | pat_ref_delete(ref, key); |
| 1249 | return 0; |
| 1250 | } |
| 1251 | |
| 1252 | /* This function is an LUA binding. It provides a function |
| 1253 | * for deleting map entry from a referenced map file. |
| 1254 | */ |
| 1255 | static int hlua_del_map(lua_State *L) |
| 1256 | { |
| 1257 | const char *name; |
| 1258 | const char *key; |
| 1259 | struct pat_ref *ref; |
| 1260 | |
| 1261 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1262 | |
| 1263 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1264 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1265 | |
| 1266 | ref = pat_ref_lookup(name); |
| 1267 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1268 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1269 | |
| 1270 | pat_ref_delete(ref, key); |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
| 1274 | /* This function is an LUA binding. It provides a function |
| 1275 | * for adding ACL pattern from a referenced ACL file. |
| 1276 | */ |
| 1277 | static int hlua_add_acl(lua_State *L) |
| 1278 | { |
| 1279 | const char *name; |
| 1280 | const char *key; |
| 1281 | struct pat_ref *ref; |
| 1282 | |
| 1283 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1284 | |
| 1285 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1286 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1287 | |
| 1288 | ref = pat_ref_lookup(name); |
| 1289 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1290 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1291 | |
| 1292 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1293 | pat_ref_add(ref, key, NULL, NULL); |
| 1294 | return 0; |
| 1295 | } |
| 1296 | |
| 1297 | /* This function is an LUA binding. It provides a function |
| 1298 | * for setting map pattern and sample from a referenced map |
| 1299 | * file. |
| 1300 | */ |
| 1301 | static int hlua_set_map(lua_State *L) |
| 1302 | { |
| 1303 | const char *name; |
| 1304 | const char *key; |
| 1305 | const char *value; |
| 1306 | struct pat_ref *ref; |
| 1307 | |
| 1308 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1309 | |
| 1310 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1311 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1312 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1313 | |
| 1314 | ref = pat_ref_lookup(name); |
| 1315 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1316 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1317 | |
| 1318 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1319 | pat_ref_set(ref, key, value, NULL); |
| 1320 | else |
| 1321 | pat_ref_add(ref, key, value, NULL); |
| 1322 | return 0; |
| 1323 | } |
| 1324 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1325 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1326 | * an integer or user data. This data is associated with a metatable. This |
| 1327 | * metatable have an original version registred in the global context with |
| 1328 | * the name of the object (_G[<name>] = <metable> ). |
| 1329 | * |
| 1330 | * A metable is a table that modify the standard behavior of a standard |
| 1331 | * access to the associated data. The entries of this new metatable are |
| 1332 | * defined as is: |
| 1333 | * |
| 1334 | * http://lua-users.org/wiki/MetatableEvents |
| 1335 | * |
| 1336 | * __index |
| 1337 | * |
| 1338 | * we access an absent field in a table, the result is nil. This is |
| 1339 | * true, but it is not the whole truth. Actually, such access triggers |
| 1340 | * the interpreter to look for an __index metamethod: If there is no |
| 1341 | * such method, as usually happens, then the access results in nil; |
| 1342 | * otherwise, the metamethod will provide the result. |
| 1343 | * |
| 1344 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1345 | * the key does not appear in the table, but the metatable has an __index |
| 1346 | * property: |
| 1347 | * |
| 1348 | * - if the value is a function, the function is called, passing in the |
| 1349 | * table and the key; the return value of that function is returned as |
| 1350 | * the result. |
| 1351 | * |
| 1352 | * - if the value is another table, the value of the key in that table is |
| 1353 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1354 | * table's metatable has an __index property, then it continues on up) |
| 1355 | * |
| 1356 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1357 | * |
| 1358 | * http://www.lua.org/pil/13.4.1.html |
| 1359 | * |
| 1360 | * __newindex |
| 1361 | * |
| 1362 | * Like __index, but control property assignment. |
| 1363 | * |
| 1364 | * __mode - Control weak references. A string value with one or both |
| 1365 | * of the characters 'k' and 'v' which specifies that the the |
| 1366 | * keys and/or values in the table are weak references. |
| 1367 | * |
| 1368 | * __call - Treat a table like a function. When a table is followed by |
| 1369 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1370 | * a __call key pointing to a function, that function is invoked |
| 1371 | * (passing any specified arguments) and the return value is |
| 1372 | * returned. |
| 1373 | * |
| 1374 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1375 | * called, if the metatable for myTable has a __metatable |
| 1376 | * key, the value of that key is returned instead of the |
| 1377 | * actual metatable. |
| 1378 | * |
| 1379 | * __tostring - Control string representation. When the builtin |
| 1380 | * "tostring( myTable )" function is called, if the metatable |
| 1381 | * for myTable has a __tostring property set to a function, |
| 1382 | * that function is invoked (passing myTable to it) and the |
| 1383 | * return value is used as the string representation. |
| 1384 | * |
| 1385 | * __len - Control table length. When the table length is requested using |
| 1386 | * the length operator ( '#' ), if the metatable for myTable has |
| 1387 | * a __len key pointing to a function, that function is invoked |
| 1388 | * (passing myTable to it) and the return value used as the value |
| 1389 | * of "#myTable". |
| 1390 | * |
| 1391 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1392 | * collected, if the metatable has a __gc field pointing to a |
| 1393 | * function, that function is first invoked, passing the userdata |
| 1394 | * to it. The __gc metamethod is not called for tables. |
| 1395 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1396 | * |
| 1397 | * Special metamethods for redefining standard operators: |
| 1398 | * http://www.lua.org/pil/13.1.html |
| 1399 | * |
| 1400 | * __add "+" |
| 1401 | * __sub "-" |
| 1402 | * __mul "*" |
| 1403 | * __div "/" |
| 1404 | * __unm "!" |
| 1405 | * __pow "^" |
| 1406 | * __concat ".." |
| 1407 | * |
| 1408 | * Special methods for redfining standar relations |
| 1409 | * http://www.lua.org/pil/13.2.html |
| 1410 | * |
| 1411 | * __eq "==" |
| 1412 | * __lt "<" |
| 1413 | * __le "<=" |
| 1414 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1415 | |
| 1416 | /* |
| 1417 | * |
| 1418 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1419 | * Class Map |
| 1420 | * |
| 1421 | * |
| 1422 | */ |
| 1423 | |
| 1424 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1425 | * a class session, otherwise it throws an error. |
| 1426 | */ |
| 1427 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1428 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1429 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | /* This function is the map constructor. It don't need |
| 1433 | * the class Map object. It creates and return a new Map |
| 1434 | * object. It must be called only during "body" or "init" |
| 1435 | * context because it process some filesystem accesses. |
| 1436 | */ |
| 1437 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1438 | { |
| 1439 | const char *fn; |
| 1440 | int match = PAT_MATCH_STR; |
| 1441 | struct sample_conv conv; |
| 1442 | const char *file = ""; |
| 1443 | int line = 0; |
| 1444 | lua_Debug ar; |
| 1445 | char *err = NULL; |
| 1446 | struct arg args[2]; |
| 1447 | |
| 1448 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1449 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1450 | |
| 1451 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1452 | |
| 1453 | if (lua_gettop(L) >= 2) { |
| 1454 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1455 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1456 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1457 | } |
| 1458 | |
| 1459 | /* Get Lua filename and line number. */ |
| 1460 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1461 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1462 | if (ar.currentline > 0) { /* is there info? */ |
| 1463 | file = ar.short_src; |
| 1464 | line = ar.currentline; |
| 1465 | } |
| 1466 | } |
| 1467 | |
| 1468 | /* fill fake sample_conv struct. */ |
| 1469 | conv.kw = ""; /* unused. */ |
| 1470 | conv.process = NULL; /* unused. */ |
| 1471 | conv.arg_mask = 0; /* unused. */ |
| 1472 | conv.val_args = NULL; /* unused. */ |
| 1473 | conv.out_type = SMP_T_STR; |
| 1474 | conv.private = (void *)(long)match; |
| 1475 | switch (match) { |
| 1476 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1477 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1478 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1479 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1480 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1481 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1482 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1483 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1484 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1485 | default: |
| 1486 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1487 | } |
| 1488 | |
| 1489 | /* fill fake args. */ |
| 1490 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1491 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1492 | args[1].type = ARGT_STOP; |
| 1493 | |
| 1494 | /* load the map. */ |
| 1495 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1496 | /* error case: we cant use luaL_error because we must |
| 1497 | * free the err variable. |
| 1498 | */ |
| 1499 | luaL_where(L, 1); |
| 1500 | lua_pushfstring(L, "'new': %s.", err); |
| 1501 | lua_concat(L, 2); |
| 1502 | free(err); |
| 1503 | WILL_LJMP(lua_error(L)); |
| 1504 | } |
| 1505 | |
| 1506 | /* create the lua object. */ |
| 1507 | lua_newtable(L); |
| 1508 | lua_pushlightuserdata(L, args[0].data.map); |
| 1509 | lua_rawseti(L, -2, 0); |
| 1510 | |
| 1511 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1512 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1513 | lua_setmetatable(L, -2); |
| 1514 | |
| 1515 | |
| 1516 | return 1; |
| 1517 | } |
| 1518 | |
| 1519 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1520 | { |
| 1521 | struct map_descriptor *desc; |
| 1522 | struct pattern *pat; |
| 1523 | struct sample smp; |
| 1524 | |
| 1525 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1526 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1527 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1528 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1529 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1530 | } |
| 1531 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1532 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1533 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1534 | smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1535 | } |
| 1536 | |
| 1537 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1538 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1539 | if (str) |
| 1540 | lua_pushstring(L, ""); |
| 1541 | else |
| 1542 | lua_pushnil(L); |
| 1543 | return 1; |
| 1544 | } |
| 1545 | |
| 1546 | /* The Lua pattern must return a string, so we can't check the returned type */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1547 | lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1548 | return 1; |
| 1549 | } |
| 1550 | |
| 1551 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1552 | { |
| 1553 | return _hlua_map_lookup(L, 0); |
| 1554 | } |
| 1555 | |
| 1556 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1557 | { |
| 1558 | return _hlua_map_lookup(L, 1); |
| 1559 | } |
| 1560 | |
| 1561 | /* |
| 1562 | * |
| 1563 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1564 | * Class Socket |
| 1565 | * |
| 1566 | * |
| 1567 | */ |
| 1568 | |
| 1569 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1570 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1571 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | /* This function is the handler called for each I/O on the established |
| 1575 | * connection. It is used for notify space avalaible to send or data |
| 1576 | * received. |
| 1577 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1578 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1579 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1580 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1581 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1582 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1583 | if (appctx->ctx.hlua_cosocket.die) { |
| 1584 | si_shutw(si); |
| 1585 | si_shutr(si); |
| 1586 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1587 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1588 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1589 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1590 | } |
| 1591 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1592 | /* If the connection object is not avalaible, close all the |
| 1593 | * streams and wakeup everithing waiting for. |
| 1594 | */ |
| 1595 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1596 | si_shutw(si); |
| 1597 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1598 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1599 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1600 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1601 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1602 | } |
| 1603 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1604 | /* If we cant write, wakeup the pending write signals. */ |
| 1605 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1606 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1607 | |
| 1608 | /* If we cant read, wakeup the pending read signals. */ |
| 1609 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1610 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1611 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1612 | /* if the connection is not estabkished, inform the stream that we want |
| 1613 | * to be notified whenever the connection completes. |
| 1614 | */ |
| 1615 | if (!(c->flags & CO_FL_CONNECTED)) { |
| 1616 | si_applet_cant_get(si); |
| 1617 | si_applet_cant_put(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1618 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1619 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1620 | |
| 1621 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1622 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1623 | |
| 1624 | /* 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] | 1625 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1626 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1627 | |
| 1628 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1629 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1630 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1631 | |
| 1632 | /* Some data were injected in the buffer, notify the stream |
| 1633 | * interface. |
| 1634 | */ |
| 1635 | if (!channel_is_empty(si_ic(si))) |
| 1636 | stream_int_update(si); |
| 1637 | |
| 1638 | /* If write notifications are registered, we considers we want |
| 1639 | * to write, so we set the flag cant put |
| 1640 | */ |
| 1641 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
| 1642 | si_applet_cant_put(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1643 | } |
| 1644 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1645 | /* This function is called when the "struct stream" is destroyed. |
| 1646 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1647 | * Wake all the pending signals. |
| 1648 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1649 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1650 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1651 | struct xref *peer; |
| 1652 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1653 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1654 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1655 | if (peer) |
| 1656 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1657 | |
| 1658 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1659 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1660 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1661 | } |
| 1662 | |
| 1663 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1664 | * uses this object. If the stream does not exists, just quit. |
| 1665 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1666 | * pending signal can rest in the read and write lists. destroy |
| 1667 | * it. |
| 1668 | */ |
| 1669 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1670 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1671 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1672 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1673 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1674 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1675 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1676 | |
| 1677 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1678 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1679 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1680 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1681 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1682 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1683 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1684 | appctx->ctx.hlua_cosocket.die = 1; |
| 1685 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1686 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1687 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1688 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1689 | return 0; |
| 1690 | } |
| 1691 | |
| 1692 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1693 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1694 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1695 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1696 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1697 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1698 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1699 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1700 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1701 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1702 | |
| 1703 | /* Check if we run on the same thread than the xreator thread. |
| 1704 | * We cannot access to the socket if the thread is different. |
| 1705 | */ |
| 1706 | if (socket->tid != tid) |
| 1707 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1708 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1709 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1710 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1711 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1712 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1713 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1714 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1715 | appctx->ctx.hlua_cosocket.die = 1; |
| 1716 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1717 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1718 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1719 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1720 | return 0; |
| 1721 | } |
| 1722 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1723 | /* The close function calls close_helper. |
| 1724 | */ |
| 1725 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1726 | { |
| 1727 | MAY_LJMP(check_args(L, 1, "close")); |
| 1728 | return hlua_socket_close_helper(L); |
| 1729 | } |
| 1730 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1731 | /* This Lua function assumes that the stack contain three parameters. |
| 1732 | * 1 - USERDATA containing a struct socket |
| 1733 | * 2 - INTEGER with values of the macro defined below |
| 1734 | * If the integer is -1, we must read at most one line. |
| 1735 | * If the integer is -2, we ust read all the data until the |
| 1736 | * end of the stream. |
| 1737 | * If the integer is positive value, we must read a number of |
| 1738 | * bytes corresponding to this value. |
| 1739 | */ |
| 1740 | #define HLSR_READ_LINE (-1) |
| 1741 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1742 | __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] | 1743 | { |
| 1744 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1745 | int wanted = lua_tointeger(L, 2); |
| 1746 | struct hlua *hlua = hlua_gethlua(L); |
| 1747 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1748 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1749 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1750 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1751 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1752 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1753 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1754 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1755 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1756 | struct stream_interface *si; |
| 1757 | struct stream *s; |
| 1758 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1759 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1760 | |
| 1761 | /* Check if this lua stack is schedulable. */ |
| 1762 | if (!hlua || !hlua->task) |
| 1763 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1764 | "'frontend', 'backend' or 'task'")); |
| 1765 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1766 | /* Check if we run on the same thread than the xreator thread. |
| 1767 | * We cannot access to the socket if the thread is different. |
| 1768 | */ |
| 1769 | if (socket->tid != tid) |
| 1770 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1771 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1772 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1773 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1774 | if (!peer) |
| 1775 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1776 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1777 | si = appctx->owner; |
| 1778 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1779 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1780 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1781 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1782 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1783 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1784 | if (nblk < 0) /* Connection close. */ |
| 1785 | goto connection_closed; |
| 1786 | if (nblk == 0) /* No data avalaible. */ |
| 1787 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1788 | |
| 1789 | /* remove final \r\n. */ |
| 1790 | if (nblk == 1) { |
| 1791 | if (blk1[len1-1] == '\n') { |
| 1792 | len1--; |
| 1793 | skip_at_end++; |
| 1794 | if (blk1[len1-1] == '\r') { |
| 1795 | len1--; |
| 1796 | skip_at_end++; |
| 1797 | } |
| 1798 | } |
| 1799 | } |
| 1800 | else { |
| 1801 | if (blk2[len2-1] == '\n') { |
| 1802 | len2--; |
| 1803 | skip_at_end++; |
| 1804 | if (blk2[len2-1] == '\r') { |
| 1805 | len2--; |
| 1806 | skip_at_end++; |
| 1807 | } |
| 1808 | } |
| 1809 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1810 | } |
| 1811 | |
| 1812 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1813 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1814 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1815 | if (nblk < 0) /* Connection close. */ |
| 1816 | goto connection_closed; |
| 1817 | if (nblk == 0) /* No data avalaible. */ |
| 1818 | goto connection_empty; |
| 1819 | } |
| 1820 | |
| 1821 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1822 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1823 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1824 | if (nblk < 0) /* Connection close. */ |
| 1825 | goto connection_closed; |
| 1826 | if (nblk == 0) /* No data avalaible. */ |
| 1827 | goto connection_empty; |
| 1828 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1829 | missing_bytes = wanted - socket->b.n; |
| 1830 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1831 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1832 | len1 = missing_bytes; |
| 1833 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1834 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | len = len1; |
| 1838 | |
| 1839 | luaL_addlstring(&socket->b, blk1, len1); |
| 1840 | if (nblk == 2) { |
| 1841 | len += len2; |
| 1842 | luaL_addlstring(&socket->b, blk2, len2); |
| 1843 | } |
| 1844 | |
| 1845 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1846 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1847 | |
| 1848 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1849 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1850 | |
| 1851 | /* If the pattern reclaim to read all the data |
| 1852 | * in the connection, got out. |
| 1853 | */ |
| 1854 | if (wanted == HLSR_READ_ALL) |
| 1855 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1856 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1857 | goto connection_empty; |
| 1858 | |
| 1859 | /* Return result. */ |
| 1860 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1861 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1862 | return 1; |
| 1863 | |
| 1864 | connection_closed: |
| 1865 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1866 | xref_unlock(&socket->xref, peer); |
| 1867 | |
| 1868 | no_peer: |
| 1869 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1870 | /* If the buffer containds data. */ |
| 1871 | if (socket->b.n > 0) { |
| 1872 | luaL_pushresult(&socket->b); |
| 1873 | return 1; |
| 1874 | } |
| 1875 | lua_pushnil(L); |
| 1876 | lua_pushstring(L, "connection closed."); |
| 1877 | return 2; |
| 1878 | |
| 1879 | connection_empty: |
| 1880 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1881 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1882 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1883 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1884 | } |
| 1885 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1886 | 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] | 1887 | return 0; |
| 1888 | } |
| 1889 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1890 | /* This Lua function gets two parameters. The first one can be string |
| 1891 | * or a number. If the string is "*l", the user requires one line. If |
| 1892 | * 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] | 1893 | * If the value is a number, the user require a number of bytes equal |
| 1894 | * to the value. The default value is "*l" (a line). |
| 1895 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1896 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1897 | * integer takes this values: |
| 1898 | * -1 : read a line |
| 1899 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1900 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1901 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1902 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1903 | * concatenated with the read data. |
| 1904 | */ |
| 1905 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1906 | { |
| 1907 | int wanted = HLSR_READ_LINE; |
| 1908 | const char *pattern; |
| 1909 | int type; |
| 1910 | char *error; |
| 1911 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1912 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1913 | |
| 1914 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1915 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1916 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1917 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1918 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1919 | /* Check if we run on the same thread than the xreator thread. |
| 1920 | * We cannot access to the socket if the thread is different. |
| 1921 | */ |
| 1922 | if (socket->tid != tid) |
| 1923 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1924 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1925 | /* check for pattern. */ |
| 1926 | if (lua_gettop(L) >= 2) { |
| 1927 | type = lua_type(L, 2); |
| 1928 | if (type == LUA_TSTRING) { |
| 1929 | pattern = lua_tostring(L, 2); |
| 1930 | if (strcmp(pattern, "*a") == 0) |
| 1931 | wanted = HLSR_READ_ALL; |
| 1932 | else if (strcmp(pattern, "*l") == 0) |
| 1933 | wanted = HLSR_READ_LINE; |
| 1934 | else { |
| 1935 | wanted = strtoll(pattern, &error, 10); |
| 1936 | if (*error != '\0') |
| 1937 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1938 | } |
| 1939 | } |
| 1940 | else if (type == LUA_TNUMBER) { |
| 1941 | wanted = lua_tointeger(L, 2); |
| 1942 | if (wanted < 0) |
| 1943 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1944 | } |
| 1945 | } |
| 1946 | |
| 1947 | /* Set pattern. */ |
| 1948 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1949 | |
| 1950 | /* Check if we would replace the top by itself. */ |
| 1951 | if (lua_gettop(L) != 2) |
| 1952 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1953 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1954 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1955 | luaL_buffinit(L, &socket->b); |
| 1956 | |
| 1957 | /* Check prefix. */ |
| 1958 | if (lua_gettop(L) >= 3) { |
| 1959 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1960 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1961 | pattern = lua_tolstring(L, 3, &len); |
| 1962 | luaL_addlstring(&socket->b, pattern, len); |
| 1963 | } |
| 1964 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1965 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1969 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1970 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1971 | 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] | 1972 | { |
| 1973 | struct hlua_socket *socket; |
| 1974 | struct hlua *hlua = hlua_gethlua(L); |
| 1975 | struct appctx *appctx; |
| 1976 | size_t buf_len; |
| 1977 | const char *buf; |
| 1978 | int len; |
| 1979 | int send_len; |
| 1980 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1981 | struct xref *peer; |
| 1982 | struct stream_interface *si; |
| 1983 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1984 | |
| 1985 | /* Check if this lua stack is schedulable. */ |
| 1986 | if (!hlua || !hlua->task) |
| 1987 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1988 | "'frontend', 'backend' or 'task'")); |
| 1989 | |
| 1990 | /* Get object */ |
| 1991 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1992 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1993 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1994 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1995 | /* Check if we run on the same thread than the xreator thread. |
| 1996 | * We cannot access to the socket if the thread is different. |
| 1997 | */ |
| 1998 | if (socket->tid != tid) |
| 1999 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2000 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2001 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2002 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2003 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2004 | lua_pushinteger(L, -1); |
| 2005 | return 1; |
| 2006 | } |
| 2007 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2008 | si = appctx->owner; |
| 2009 | s = si_strm(si); |
| 2010 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2011 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2012 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2013 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2014 | lua_pushinteger(L, -1); |
| 2015 | return 1; |
| 2016 | } |
| 2017 | |
| 2018 | /* Update the input buffer data. */ |
| 2019 | buf += sent; |
| 2020 | send_len = buf_len - sent; |
| 2021 | |
| 2022 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2023 | if (sent >= buf_len) { |
| 2024 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2025 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2026 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2027 | |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2028 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2029 | * the request buffer if its not required. |
| 2030 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2031 | if (s->req.buf.size == 0) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2032 | if (!channel_alloc_buffer(&s->req, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2033 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2034 | } |
| 2035 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2036 | /* Check for avalaible space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2037 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2038 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2039 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2040 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2041 | |
| 2042 | /* send data */ |
| 2043 | if (len < send_len) |
| 2044 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2045 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2046 | |
| 2047 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2048 | * the data" (-2) are not expected because the available length |
| 2049 | * is tested. |
| 2050 | * Other unknown error are also not expected. |
| 2051 | */ |
| 2052 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2053 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2054 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2055 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2056 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2057 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2058 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2059 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2060 | return 1; |
| 2061 | } |
| 2062 | |
| 2063 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2064 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2065 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2066 | s->req.rex = TICK_ETERNITY; |
| 2067 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2068 | |
| 2069 | /* Update length sent. */ |
| 2070 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2071 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2072 | |
| 2073 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2074 | if (sent + len >= buf_len) { |
| 2075 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2076 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2077 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2078 | |
| 2079 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2080 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2081 | xref_unlock(&socket->xref, peer); |
| 2082 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2083 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2084 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2085 | 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] | 2086 | return 0; |
| 2087 | } |
| 2088 | |
| 2089 | /* This function initiate the send of data. It just check the input |
| 2090 | * parameters and push an integer in the Lua stack that contain the |
| 2091 | * amount of data writed in the buffer. This is used by the function |
| 2092 | * "hlua_socket_write_yield" that can yield. |
| 2093 | * |
| 2094 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2095 | * the associated object. The second is a string buffer. The third is |
| 2096 | * a facultative integer that represents where is the buffer position |
| 2097 | * of the start of the data that can send. The first byte is the |
| 2098 | * position "1". The default value is "1". The fourth argument is a |
| 2099 | * facultative integer that represents where is the buffer position |
| 2100 | * of the end of the data that can send. The default is the last byte. |
| 2101 | */ |
| 2102 | static int hlua_socket_send(struct lua_State *L) |
| 2103 | { |
| 2104 | int i; |
| 2105 | int j; |
| 2106 | const char *buf; |
| 2107 | size_t buf_len; |
| 2108 | |
| 2109 | /* Check number of arguments. */ |
| 2110 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2111 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2112 | |
| 2113 | /* Get the string. */ |
| 2114 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2115 | |
| 2116 | /* Get and check j. */ |
| 2117 | if (lua_gettop(L) == 4) { |
| 2118 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2119 | if (j < 0) |
| 2120 | j = buf_len + j + 1; |
| 2121 | if (j > buf_len) |
| 2122 | j = buf_len + 1; |
| 2123 | lua_pop(L, 1); |
| 2124 | } |
| 2125 | else |
| 2126 | j = buf_len; |
| 2127 | |
| 2128 | /* Get and check i. */ |
| 2129 | if (lua_gettop(L) == 3) { |
| 2130 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2131 | if (i < 0) |
| 2132 | i = buf_len + i + 1; |
| 2133 | if (i > buf_len) |
| 2134 | i = buf_len + 1; |
| 2135 | lua_pop(L, 1); |
| 2136 | } else |
| 2137 | i = 1; |
| 2138 | |
| 2139 | /* Check bth i and j. */ |
| 2140 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2141 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2142 | return 1; |
| 2143 | } |
| 2144 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2145 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2146 | return 1; |
| 2147 | } |
| 2148 | if (i == 0) |
| 2149 | i = 1; |
| 2150 | if (j == 0) |
| 2151 | j = 1; |
| 2152 | |
| 2153 | /* Pop the string. */ |
| 2154 | lua_pop(L, 1); |
| 2155 | |
| 2156 | /* Update the buffer length. */ |
| 2157 | buf += i - 1; |
| 2158 | buf_len = j - i + 1; |
| 2159 | lua_pushlstring(L, buf, buf_len); |
| 2160 | |
| 2161 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2162 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2163 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2164 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2165 | } |
| 2166 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2167 | #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] | 2168 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2169 | { |
| 2170 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2171 | int ret; |
| 2172 | int len; |
| 2173 | char *p; |
| 2174 | |
| 2175 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2176 | if (ret <= 0) { |
| 2177 | lua_pushnil(L); |
| 2178 | return 1; |
| 2179 | } |
| 2180 | |
| 2181 | if (ret == AF_UNIX) { |
| 2182 | lua_pushstring(L, buffer+1); |
| 2183 | return 1; |
| 2184 | } |
| 2185 | else if (ret == AF_INET6) { |
| 2186 | buffer[0] = '['; |
| 2187 | len = strlen(buffer); |
| 2188 | buffer[len] = ']'; |
| 2189 | len++; |
| 2190 | buffer[len] = ':'; |
| 2191 | len++; |
| 2192 | p = buffer; |
| 2193 | } |
| 2194 | else if (ret == AF_INET) { |
| 2195 | p = buffer + 1; |
| 2196 | len = strlen(p); |
| 2197 | p[len] = ':'; |
| 2198 | len++; |
| 2199 | } |
| 2200 | else { |
| 2201 | lua_pushnil(L); |
| 2202 | return 1; |
| 2203 | } |
| 2204 | |
| 2205 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2206 | lua_pushnil(L); |
| 2207 | return 1; |
| 2208 | } |
| 2209 | |
| 2210 | lua_pushstring(L, p); |
| 2211 | return 1; |
| 2212 | } |
| 2213 | |
| 2214 | /* Returns information about the peer of the connection. */ |
| 2215 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2216 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2217 | struct hlua_socket *socket; |
| 2218 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2219 | struct xref *peer; |
| 2220 | struct appctx *appctx; |
| 2221 | struct stream_interface *si; |
| 2222 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2223 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2224 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2225 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2226 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2227 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2228 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2229 | /* Check if we run on the same thread than the xreator thread. |
| 2230 | * We cannot access to the socket if the thread is different. |
| 2231 | */ |
| 2232 | if (socket->tid != tid) |
| 2233 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2234 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2235 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2236 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2237 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2238 | lua_pushnil(L); |
| 2239 | return 1; |
| 2240 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2241 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2242 | si = appctx->owner; |
| 2243 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2244 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2245 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2246 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2247 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2248 | lua_pushnil(L); |
| 2249 | return 1; |
| 2250 | } |
| 2251 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2252 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2253 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2254 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2255 | lua_pushnil(L); |
| 2256 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2257 | } |
| 2258 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2259 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2260 | xref_unlock(&socket->xref, peer); |
| 2261 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2262 | } |
| 2263 | |
| 2264 | /* Returns information about my connection side. */ |
| 2265 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2266 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2267 | struct hlua_socket *socket; |
| 2268 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2269 | struct appctx *appctx; |
| 2270 | struct xref *peer; |
| 2271 | struct stream_interface *si; |
| 2272 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2273 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2274 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2275 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2276 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2277 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2278 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2279 | /* Check if we run on the same thread than the xreator thread. |
| 2280 | * We cannot access to the socket if the thread is different. |
| 2281 | */ |
| 2282 | if (socket->tid != tid) |
| 2283 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2284 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2285 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2286 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2287 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | lua_pushnil(L); |
| 2289 | return 1; |
| 2290 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2291 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2292 | si = appctx->owner; |
| 2293 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2294 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2295 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2296 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2297 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2298 | lua_pushnil(L); |
| 2299 | return 1; |
| 2300 | } |
| 2301 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2302 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2303 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2304 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2305 | lua_pushnil(L); |
| 2306 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2307 | } |
| 2308 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2309 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2310 | xref_unlock(&socket->xref, peer); |
| 2311 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2312 | } |
| 2313 | |
| 2314 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2315 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2316 | .obj_type = OBJ_TYPE_APPLET, |
| 2317 | .name = "<LUA_TCP>", |
| 2318 | .fct = hlua_socket_handler, |
| 2319 | .release = hlua_socket_release, |
| 2320 | }; |
| 2321 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2322 | __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] | 2323 | { |
| 2324 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2325 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2326 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2327 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2328 | struct stream_interface *si; |
| 2329 | struct stream *s; |
| 2330 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2331 | /* Check if we run on the same thread than the xreator thread. |
| 2332 | * We cannot access to the socket if the thread is different. |
| 2333 | */ |
| 2334 | if (socket->tid != tid) |
| 2335 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2336 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2337 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2338 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2339 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2340 | lua_pushnil(L); |
| 2341 | lua_pushstring(L, "Can't connect"); |
| 2342 | return 2; |
| 2343 | } |
| 2344 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2345 | si = appctx->owner; |
| 2346 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2347 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2348 | /* Check if we run on the same thread than the xreator thread. |
| 2349 | * We cannot access to the socket if the thread is different. |
| 2350 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2351 | if (socket->tid != tid) { |
| 2352 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2353 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2354 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2355 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2356 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2357 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2358 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2359 | lua_pushnil(L); |
| 2360 | lua_pushstring(L, "Can't connect"); |
| 2361 | return 2; |
| 2362 | } |
| 2363 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2364 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2365 | |
| 2366 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2367 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2368 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2369 | lua_pushinteger(L, 1); |
| 2370 | return 1; |
| 2371 | } |
| 2372 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2373 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2374 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2375 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2376 | } |
| 2377 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2378 | 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] | 2379 | return 0; |
| 2380 | } |
| 2381 | |
| 2382 | /* This function fail or initite the connection. */ |
| 2383 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2384 | { |
| 2385 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2386 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2387 | const char *ip; |
| 2388 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2389 | struct hlua *hlua; |
| 2390 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2391 | int low, high; |
| 2392 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2393 | struct xref *peer; |
| 2394 | struct stream_interface *si; |
| 2395 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2396 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2397 | if (lua_gettop(L) < 2) |
| 2398 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2399 | |
| 2400 | /* Get args. */ |
| 2401 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2402 | |
| 2403 | /* Check if we run on the same thread than the xreator thread. |
| 2404 | * We cannot access to the socket if the thread is different. |
| 2405 | */ |
| 2406 | if (socket->tid != tid) |
| 2407 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2408 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2409 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2410 | if (lua_gettop(L) >= 3) { |
| 2411 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2412 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2413 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2414 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2415 | * that are not enclosed within square brackets. |
| 2416 | */ |
| 2417 | if (port > 0) { |
| 2418 | luaL_buffinit(L, &b); |
| 2419 | luaL_addstring(&b, ip); |
| 2420 | luaL_addchar(&b, ':'); |
| 2421 | luaL_pushresult(&b); |
| 2422 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2423 | } |
| 2424 | } |
| 2425 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2426 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2427 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2428 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2429 | lua_pushnil(L); |
| 2430 | return 1; |
| 2431 | } |
| 2432 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2433 | si = appctx->owner; |
| 2434 | s = si_strm(si); |
| 2435 | |
| 2436 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2437 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2438 | if (!conn) { |
| 2439 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2440 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2441 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2442 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2443 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2444 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2445 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2446 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2447 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2448 | if (!addr) { |
| 2449 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2450 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2451 | } |
| 2452 | if (low != high) { |
| 2453 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2454 | 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] | 2455 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2456 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2457 | |
| 2458 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2459 | if (low == 0) { |
| 2460 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2461 | if (port == -1) { |
| 2462 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2463 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2464 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2465 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2466 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2467 | if (port == -1) { |
| 2468 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2469 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2470 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2471 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2472 | } |
| 2473 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2474 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2475 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2476 | appctx = objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2477 | |
| 2478 | /* inform the stream that we want to be notified whenever the |
| 2479 | * connection completes. |
| 2480 | */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2481 | si_applet_cant_get(&s->si[0]); |
| 2482 | si_applet_cant_put(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2483 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2484 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2485 | hlua->flags |= HLUA_MUST_GC; |
| 2486 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2487 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2488 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2489 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2490 | } |
| 2491 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2492 | |
| 2493 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2494 | /* Return yield waiting for connection. */ |
| 2495 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2496 | 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] | 2497 | |
| 2498 | return 0; |
| 2499 | } |
| 2500 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2501 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2502 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2503 | { |
| 2504 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2505 | struct xref *peer; |
| 2506 | struct appctx *appctx; |
| 2507 | struct stream_interface *si; |
| 2508 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2509 | |
| 2510 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2511 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2512 | |
| 2513 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2514 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2515 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2516 | lua_pushnil(L); |
| 2517 | return 1; |
| 2518 | } |
| 2519 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2520 | si = appctx->owner; |
| 2521 | s = si_strm(si); |
| 2522 | |
| 2523 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2524 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2525 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2526 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2527 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2528 | |
| 2529 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2530 | { |
| 2531 | return 0; |
| 2532 | } |
| 2533 | |
| 2534 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2535 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2536 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2537 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2538 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2539 | struct xref *peer; |
| 2540 | struct appctx *appctx; |
| 2541 | struct stream_interface *si; |
| 2542 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2543 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2544 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2545 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2546 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2547 | |
| 2548 | /* round up for inputs that are fractions and convert to millis */ |
| 2549 | dtmout = (0.5 + MAY_LJMP(luaL_checknumber(L, 2))) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2550 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2551 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2552 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2553 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2554 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2555 | if (dtmout > INT_MAX) /* overflow check */ |
| 2556 | WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d", INT_MAX)); |
| 2557 | |
| 2558 | tmout = MS_TO_TICKS((int)dtmout); |
| 2559 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2560 | /* Check if we run on the same thread than the xreator thread. |
| 2561 | * We cannot access to the socket if the thread is different. |
| 2562 | */ |
| 2563 | if (socket->tid != tid) |
| 2564 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2565 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2566 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2567 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2568 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2569 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2570 | WILL_LJMP(lua_error(L)); |
| 2571 | return 0; |
| 2572 | } |
| 2573 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2574 | si = appctx->owner; |
| 2575 | s = si_strm(si); |
| 2576 | |
| 2577 | s->req.rto = tmout; |
| 2578 | s->req.wto = tmout; |
| 2579 | s->res.rto = tmout; |
| 2580 | s->res.wto = tmout; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2581 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2582 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2583 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2584 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2588 | { |
| 2589 | struct hlua_socket *socket; |
| 2590 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2591 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2592 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2593 | |
| 2594 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2595 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2596 | hlua_pusherror(L, "socket: full stack"); |
| 2597 | goto out_fail_conf; |
| 2598 | } |
| 2599 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2600 | /* Create the object: obj[0] = userdata. */ |
| 2601 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2602 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2603 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2604 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2605 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2607 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2608 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2609 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2610 | goto out_fail_conf; |
| 2611 | } |
| 2612 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2613 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2614 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2615 | lua_setmetatable(L, -2); |
| 2616 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2617 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2618 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2619 | if (!appctx) { |
| 2620 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2621 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2622 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2623 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2624 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2625 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2626 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2627 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2628 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2629 | /* Now create a session, task and stream for this applet */ |
| 2630 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2631 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2632 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2633 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2634 | } |
| 2635 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2636 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2637 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2638 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2639 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2640 | } |
| 2641 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2642 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2643 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2644 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2645 | /* Configure "right" stream interface. this "si" is used to connect |
| 2646 | * and retrieve data from the server. The connection is initialized |
| 2647 | * with the "struct server". |
| 2648 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2649 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2650 | |
| 2651 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2652 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2653 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2654 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2655 | return 1; |
| 2656 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2657 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2658 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2659 | out_fail_sess: |
| 2660 | appctx_free(appctx); |
| 2661 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2662 | WILL_LJMP(lua_error(L)); |
| 2663 | return 0; |
| 2664 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2665 | |
| 2666 | /* |
| 2667 | * |
| 2668 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2669 | * Class Channel |
| 2670 | * |
| 2671 | * |
| 2672 | */ |
| 2673 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2674 | /* The state between the channel data and the HTTP parser state can be |
| 2675 | * unconsistent, so reset the parser and call it again. Warning, this |
| 2676 | * action not revalidate the request and not send a 400 if the modified |
| 2677 | * resuest is not valid. |
| 2678 | * |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2679 | * This function never fails. The direction is set using dir, which equals |
| 2680 | * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2681 | */ |
| 2682 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2683 | { |
| 2684 | /* Protocol HTTP. */ |
| 2685 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2686 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2687 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2688 | http_txn_reset_req(stream->txn); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2689 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2690 | http_txn_reset_res(stream->txn); |
| 2691 | |
| 2692 | if (stream->txn->hdr_idx.v) |
| 2693 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2694 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2695 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2696 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2697 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2698 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2699 | } |
| 2700 | } |
| 2701 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2702 | /* This function is called before the Lua execution. It stores |
| 2703 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2704 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2705 | 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] | 2706 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2707 | c->mode = stream->be->mode; |
| 2708 | switch (c->mode) { |
| 2709 | case PR_MODE_HTTP: |
| 2710 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2711 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2712 | c->data.http.state = stream->txn->req.msg_state; |
| 2713 | else |
| 2714 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2715 | break; |
| 2716 | default: |
| 2717 | break; |
| 2718 | } |
| 2719 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2720 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2721 | /* This function is called after the Lua execution. it |
| 2722 | * returns true if the parser state is consistent, otherwise, |
| 2723 | * it return false. |
| 2724 | * |
| 2725 | * In HTTP mode, the parser state must be in the same state |
| 2726 | * or greater when we exit the function. Even if we do a |
| 2727 | * control yield. This prevent to break the HTTP message |
| 2728 | * from the Lua code. |
| 2729 | */ |
| 2730 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2731 | { |
| 2732 | if (c->mode != stream->be->mode) |
| 2733 | return 0; |
| 2734 | |
| 2735 | switch (c->mode) { |
| 2736 | case PR_MODE_HTTP: |
| 2737 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2738 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2739 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2740 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2741 | else |
| 2742 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2743 | default: |
| 2744 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2745 | } |
| 2746 | return 1; |
| 2747 | } |
| 2748 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2749 | /* Returns the struct hlua_channel join to the class channel in the |
| 2750 | * stack entry "ud" or throws an argument error. |
| 2751 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2752 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2753 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2754 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2755 | } |
| 2756 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2757 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2758 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2759 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2760 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2761 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2762 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2763 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2764 | return 0; |
| 2765 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2766 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2767 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2768 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2769 | |
| 2770 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2771 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2772 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2773 | return 1; |
| 2774 | } |
| 2775 | |
| 2776 | /* Duplicate all the data present in the input channel and put it |
| 2777 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2778 | * the stack if the channel is closed and all the data are consumed, |
| 2779 | * returns 0 if no data are available, otherwise it returns the length |
| 2780 | * of the builded string. |
| 2781 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2782 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2783 | { |
| 2784 | char *blk1; |
| 2785 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2786 | size_t len1; |
| 2787 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2788 | int ret; |
| 2789 | luaL_Buffer b; |
| 2790 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2791 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2792 | if (unlikely(ret == 0)) |
| 2793 | return 0; |
| 2794 | |
| 2795 | if (unlikely(ret < 0)) { |
| 2796 | lua_pushnil(L); |
| 2797 | return -1; |
| 2798 | } |
| 2799 | |
| 2800 | luaL_buffinit(L, &b); |
| 2801 | luaL_addlstring(&b, blk1, len1); |
| 2802 | if (unlikely(ret == 2)) |
| 2803 | luaL_addlstring(&b, blk2, len2); |
| 2804 | luaL_pushresult(&b); |
| 2805 | |
| 2806 | if (unlikely(ret == 2)) |
| 2807 | return len1 + len2; |
| 2808 | return len1; |
| 2809 | } |
| 2810 | |
| 2811 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2812 | * a yield. This function keep the data in the buffer. |
| 2813 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2814 | __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] | 2815 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2816 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2817 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2818 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2819 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2820 | if (_hlua_channel_dup(chn, L) == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2821 | 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] | 2822 | return 1; |
| 2823 | } |
| 2824 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2825 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2826 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2827 | { |
| 2828 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2829 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2830 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2831 | } |
| 2832 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2833 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2834 | * a yield. This function consumes the data in the buffer. It returns |
| 2835 | * a string containing the data or a nil pointer if no data are available |
| 2836 | * and the channel is closed. |
| 2837 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2838 | __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] | 2839 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2840 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2841 | int ret; |
| 2842 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2843 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2844 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2845 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2846 | if (unlikely(ret == 0)) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2847 | 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] | 2848 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2849 | if (unlikely(ret == -1)) |
| 2850 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2851 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2852 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2853 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2854 | return 1; |
| 2855 | } |
| 2856 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2857 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 2858 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2859 | { |
| 2860 | MAY_LJMP(check_args(L, 1, "get")); |
| 2861 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2862 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2863 | } |
| 2864 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2865 | /* This functions consumes and returns one line. If the channel is closed, |
| 2866 | * and the last data does not contains a final '\n', the data are returned |
| 2867 | * without the final '\n'. When no more data are avalaible, it returns nil |
| 2868 | * value. |
| 2869 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2870 | __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] | 2871 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2872 | char *blk1; |
| 2873 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2874 | size_t len1; |
| 2875 | size_t len2; |
| 2876 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2877 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2878 | int ret; |
| 2879 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2880 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2881 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2882 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2883 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2884 | if (ret == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2885 | 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] | 2886 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2887 | if (ret == -1) { |
| 2888 | lua_pushnil(L); |
| 2889 | return 1; |
| 2890 | } |
| 2891 | |
| 2892 | luaL_buffinit(L, &b); |
| 2893 | luaL_addlstring(&b, blk1, len1); |
| 2894 | len = len1; |
| 2895 | if (unlikely(ret == 2)) { |
| 2896 | luaL_addlstring(&b, blk2, len2); |
| 2897 | len += len2; |
| 2898 | } |
| 2899 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2900 | b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2901 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2902 | return 1; |
| 2903 | } |
| 2904 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2905 | /* Check arguments for the fucntion "hlua_channel_getline_yield". */ |
| 2906 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2907 | { |
| 2908 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2909 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2910 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2911 | } |
| 2912 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2913 | /* This function takes a string as input, and append it at the |
| 2914 | * input side of channel. If the data is too big, but a space |
| 2915 | * is probably available after sending some data, the function |
| 2916 | * yield. If the data is bigger than the buffer, or if the |
| 2917 | * channel is closed, it returns -1. otherwise, it returns the |
| 2918 | * amount of data writed. |
| 2919 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2920 | __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] | 2921 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2922 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2923 | size_t len; |
| 2924 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2925 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2926 | int ret; |
| 2927 | int max; |
| 2928 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2929 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2930 | * the request buffer if its not required. |
| 2931 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2932 | if (chn->buf.size == 0) { |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2933 | si_applet_cant_put(chn_prod(chn)); |
| 2934 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
| 2935 | } |
| 2936 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2937 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2938 | if (max > len - l) |
| 2939 | max = len - l; |
| 2940 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2941 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2942 | if (ret == -2 || ret == -3) { |
| 2943 | lua_pushinteger(L, -1); |
| 2944 | return 1; |
| 2945 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2946 | if (ret == -1) { |
| 2947 | chn->flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2948 | 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] | 2949 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2950 | l += ret; |
| 2951 | lua_pop(L, 1); |
| 2952 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2953 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2954 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2955 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2956 | if (max == 0 && co_data(chn) == 0) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2957 | /* There are no space avalaible, and the output buffer is empty. |
| 2958 | * in this case, we cannot add more data, so we cannot yield, |
| 2959 | * we return the amount of copyied data. |
| 2960 | */ |
| 2961 | return 1; |
| 2962 | } |
| 2963 | if (l < len) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2964 | 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] | 2965 | return 1; |
| 2966 | } |
| 2967 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2968 | /* just a wrapper of "hlua_channel_append_yield". It returns the length |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2969 | * of the writed string, or -1 if the channel is closed or if the |
| 2970 | * buffer size is too little for the data. |
| 2971 | */ |
| 2972 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2973 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2974 | size_t len; |
| 2975 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2976 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2977 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2978 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2979 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2980 | lua_pushinteger(L, 0); |
| 2981 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2982 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2983 | } |
| 2984 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2985 | /* just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2986 | * his process by cleaning the buffer. The result is a replacement |
| 2987 | * of the current data. It returns the length of the writed string, |
| 2988 | * or -1 if the channel is closed or if the buffer size is too |
| 2989 | * little for the data. |
| 2990 | */ |
| 2991 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2992 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2993 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2994 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2995 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2996 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2997 | lua_pushinteger(L, 0); |
| 2998 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2999 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3000 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3001 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3002 | } |
| 3003 | |
| 3004 | /* Append data in the output side of the buffer. This data is immediatly |
| 3005 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3006 | * cannot contains the data, the function yield. The function returns -1 |
| 3007 | * if the channel is closed. |
| 3008 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3009 | __LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3010 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3011 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3012 | size_t len; |
| 3013 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3014 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3015 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3016 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3017 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3018 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3019 | lua_pushinteger(L, -1); |
| 3020 | return 1; |
| 3021 | } |
| 3022 | |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3023 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 3024 | * the request buffer if its not required. |
| 3025 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3026 | if (chn->buf.size == 0) { |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 3027 | si_applet_cant_put(chn_prod(chn)); |
| 3028 | 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] | 3029 | } |
| 3030 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3031 | /* the writed data will be immediatly sent, so we can check |
| 3032 | * the avalaible space without taking in account the reserve. |
| 3033 | * The reserve is guaranted for the processing of incoming |
| 3034 | * data, because the buffer will be flushed. |
| 3035 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3036 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3037 | |
| 3038 | /* If there are no space avalaible, and the output buffer is empty. |
| 3039 | * in this case, we cannot add more data, so we cannot yield, |
| 3040 | * we return the amount of copyied data. |
| 3041 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3042 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3043 | return 1; |
| 3044 | |
| 3045 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3046 | if (max > len - l) |
| 3047 | max = len - l; |
| 3048 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3049 | /* The buffer avalaible size may be not contiguous. This test |
| 3050 | * detects a non contiguous buffer and realign it. |
| 3051 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3052 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3053 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3054 | |
| 3055 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3056 | max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3057 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3058 | /* buffer replace considers that the input part is filled. |
| 3059 | * so, I must forward these new data in the output part. |
| 3060 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3061 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3062 | |
| 3063 | l += max; |
| 3064 | lua_pop(L, 1); |
| 3065 | lua_pushinteger(L, l); |
| 3066 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3067 | /* If there are no space avalaible, and the output buffer is empty. |
| 3068 | * in this case, we cannot add more data, so we cannot yield, |
| 3069 | * we return the amount of copyied data. |
| 3070 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3071 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3072 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3073 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3074 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3075 | if (l < len) { |
| 3076 | /* If we are waiting for space in the response buffer, we |
| 3077 | * must set the flag WAKERESWR. This flag required the task |
| 3078 | * wake up if any activity is detected on the response buffer. |
| 3079 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3080 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3081 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3082 | else |
| 3083 | HLUA_SET_WAKEREQWR(hlua); |
| 3084 | 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] | 3085 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3086 | |
| 3087 | return 1; |
| 3088 | } |
| 3089 | |
| 3090 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3091 | * yield the LUA process, and resume it without checking the |
| 3092 | * input arguments. |
| 3093 | */ |
| 3094 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3095 | { |
| 3096 | MAY_LJMP(check_args(L, 2, "send")); |
| 3097 | lua_pushinteger(L, 0); |
| 3098 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3099 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | /* This function forward and amount of butes. The data pass from |
| 3103 | * the input side of the buffer to the output side, and can be |
| 3104 | * forwarded. This function never fails. |
| 3105 | * |
| 3106 | * The Lua function takes an amount of bytes to be forwarded in |
| 3107 | * imput. It returns the number of bytes forwarded. |
| 3108 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3109 | __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] | 3110 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3111 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3112 | int len; |
| 3113 | int l; |
| 3114 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3115 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3116 | |
| 3117 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3118 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3119 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3120 | |
| 3121 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3122 | if (max > ci_data(chn)) |
| 3123 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3124 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3125 | l += max; |
| 3126 | |
| 3127 | lua_pop(L, 1); |
| 3128 | lua_pushinteger(L, l); |
| 3129 | |
| 3130 | /* Check if it miss bytes to forward. */ |
| 3131 | if (l < len) { |
| 3132 | /* The the input channel or the output channel are closed, we |
| 3133 | * must return the amount of data forwarded. |
| 3134 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3135 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3136 | return 1; |
| 3137 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3138 | /* If we are waiting for space data in the response buffer, we |
| 3139 | * must set the flag WAKERESWR. This flag required the task |
| 3140 | * wake up if any activity is detected on the response buffer. |
| 3141 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3142 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3143 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3144 | else |
| 3145 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3146 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3147 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 3148 | 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] | 3149 | } |
| 3150 | |
| 3151 | return 1; |
| 3152 | } |
| 3153 | |
| 3154 | /* Just check the input and prepare the stack for the previous |
| 3155 | * function "hlua_channel_forward_yield" |
| 3156 | */ |
| 3157 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3158 | { |
| 3159 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3160 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3161 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3162 | |
| 3163 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3164 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3165 | } |
| 3166 | |
| 3167 | /* Just returns the number of bytes available in the input |
| 3168 | * side of the buffer. This function never fails. |
| 3169 | */ |
| 3170 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3171 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3172 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3173 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3174 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3175 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3176 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3177 | return 1; |
| 3178 | } |
| 3179 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3180 | /* Returns true if the channel is full. */ |
| 3181 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3182 | { |
| 3183 | struct channel *chn; |
| 3184 | int rem; |
| 3185 | |
| 3186 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3187 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3188 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3189 | rem = b_room(&chn->buf); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3190 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3191 | |
| 3192 | lua_pushboolean(L, rem <= 0); |
| 3193 | return 1; |
| 3194 | } |
| 3195 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3196 | /* Just returns the number of bytes available in the output |
| 3197 | * side of the buffer. This function never fails. |
| 3198 | */ |
| 3199 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3200 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3201 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3202 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3203 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3204 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3205 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3206 | return 1; |
| 3207 | } |
| 3208 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3209 | /* |
| 3210 | * |
| 3211 | * |
| 3212 | * Class Fetches |
| 3213 | * |
| 3214 | * |
| 3215 | */ |
| 3216 | |
| 3217 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3218 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3219 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3220 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3221 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3222 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3223 | } |
| 3224 | |
| 3225 | /* This function creates and push in the stack a fetch object according |
| 3226 | * with a current TXN. |
| 3227 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3228 | 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] | 3229 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3230 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3231 | |
| 3232 | /* Check stack size. */ |
| 3233 | if (!lua_checkstack(L, 3)) |
| 3234 | return 0; |
| 3235 | |
| 3236 | /* Create the object: obj[0] = userdata. |
| 3237 | * Note that the base of the Fetches object is the |
| 3238 | * transaction object. |
| 3239 | */ |
| 3240 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3241 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3242 | lua_rawseti(L, -2, 0); |
| 3243 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3244 | hsmp->s = txn->s; |
| 3245 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3246 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3247 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3248 | |
| 3249 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3250 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3251 | lua_setmetatable(L, -2); |
| 3252 | |
| 3253 | return 1; |
| 3254 | } |
| 3255 | |
| 3256 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3257 | * It uses closure argument to store the associated sample-fetch. It |
| 3258 | * returns only one argument or throws an error. An error is thrown |
| 3259 | * only if an error is encountered during the argument parsing. If |
| 3260 | * the "sample-fetch" function fails, nil is returned. |
| 3261 | */ |
| 3262 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3263 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3264 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3265 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3266 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3267 | int i; |
| 3268 | struct sample smp; |
| 3269 | |
| 3270 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3271 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3272 | |
| 3273 | /* Get traditionnal arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3274 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3275 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3276 | /* Check execution authorization. */ |
| 3277 | if (f->use & SMP_USE_HTTP_ANY && |
| 3278 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3279 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3280 | "is not available in Lua services", f->kw); |
| 3281 | WILL_LJMP(lua_error(L)); |
| 3282 | } |
| 3283 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3284 | /* Get extra arguments. */ |
| 3285 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3286 | if (i >= ARGM_NBARGS) |
| 3287 | break; |
| 3288 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3289 | } |
| 3290 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3291 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3292 | |
| 3293 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3294 | 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] | 3295 | |
| 3296 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3297 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3298 | lua_pushfstring(L, "error in arguments"); |
| 3299 | WILL_LJMP(lua_error(L)); |
| 3300 | } |
| 3301 | |
| 3302 | /* Initialise the sample. */ |
| 3303 | memset(&smp, 0, sizeof(smp)); |
| 3304 | |
| 3305 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3306 | 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] | 3307 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3308 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3309 | lua_pushstring(L, ""); |
| 3310 | else |
| 3311 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3312 | return 1; |
| 3313 | } |
| 3314 | |
| 3315 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3316 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3317 | hlua_smp2lua_str(L, &smp); |
| 3318 | else |
| 3319 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3320 | return 1; |
| 3321 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3322 | |
| 3323 | /* |
| 3324 | * |
| 3325 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3326 | * Class Converters |
| 3327 | * |
| 3328 | * |
| 3329 | */ |
| 3330 | |
| 3331 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3332 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3333 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3334 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3335 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3336 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | /* This function creates and push in the stack a Converters object |
| 3340 | * according with a current TXN. |
| 3341 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3342 | 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] | 3343 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3344 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3345 | |
| 3346 | /* Check stack size. */ |
| 3347 | if (!lua_checkstack(L, 3)) |
| 3348 | return 0; |
| 3349 | |
| 3350 | /* Create the object: obj[0] = userdata. |
| 3351 | * Note that the base of the Converters object is the |
| 3352 | * same than the TXN object. |
| 3353 | */ |
| 3354 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3355 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3356 | lua_rawseti(L, -2, 0); |
| 3357 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3358 | hsmp->s = txn->s; |
| 3359 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3360 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3361 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3362 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3363 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3364 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3365 | lua_setmetatable(L, -2); |
| 3366 | |
| 3367 | return 1; |
| 3368 | } |
| 3369 | |
| 3370 | /* This function is an LUA binding. It is called with each converter. |
| 3371 | * It uses closure argument to store the associated converter. It |
| 3372 | * returns only one argument or throws an error. An error is thrown |
| 3373 | * only if an error is encountered during the argument parsing. If |
| 3374 | * the converter function function fails, nil is returned. |
| 3375 | */ |
| 3376 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3377 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3378 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3379 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3380 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3381 | int i; |
| 3382 | struct sample smp; |
| 3383 | |
| 3384 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3385 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3386 | |
| 3387 | /* Get traditionnal arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3388 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3389 | |
| 3390 | /* Get extra arguments. */ |
| 3391 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3392 | if (i >= ARGM_NBARGS) |
| 3393 | break; |
| 3394 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3395 | } |
| 3396 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3397 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3398 | |
| 3399 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3400 | 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] | 3401 | |
| 3402 | /* Run the special args checker. */ |
| 3403 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3404 | hlua_pusherror(L, "error in arguments"); |
| 3405 | WILL_LJMP(lua_error(L)); |
| 3406 | } |
| 3407 | |
| 3408 | /* Initialise the sample. */ |
| 3409 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3410 | hlua_pusherror(L, "error in the input argument"); |
| 3411 | WILL_LJMP(lua_error(L)); |
| 3412 | } |
| 3413 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3414 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3415 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3416 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3417 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3418 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3419 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3420 | WILL_LJMP(lua_error(L)); |
| 3421 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3422 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3423 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3424 | hlua_pusherror(L, "error during the input argument casting"); |
| 3425 | WILL_LJMP(lua_error(L)); |
| 3426 | } |
| 3427 | |
| 3428 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3429 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3430 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3431 | lua_pushstring(L, ""); |
| 3432 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3433 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3434 | return 1; |
| 3435 | } |
| 3436 | |
| 3437 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3438 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3439 | hlua_smp2lua_str(L, &smp); |
| 3440 | else |
| 3441 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3442 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3443 | } |
| 3444 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3445 | /* |
| 3446 | * |
| 3447 | * |
| 3448 | * Class AppletTCP |
| 3449 | * |
| 3450 | * |
| 3451 | */ |
| 3452 | |
| 3453 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3454 | * a class stream, otherwise it throws an error. |
| 3455 | */ |
| 3456 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3457 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3458 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3459 | } |
| 3460 | |
| 3461 | /* This function creates and push in the stack an Applet object |
| 3462 | * according with a current TXN. |
| 3463 | */ |
| 3464 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3465 | { |
| 3466 | struct hlua_appctx *appctx; |
| 3467 | struct stream_interface *si = ctx->owner; |
| 3468 | struct stream *s = si_strm(si); |
| 3469 | struct proxy *p = s->be; |
| 3470 | |
| 3471 | /* Check stack size. */ |
| 3472 | if (!lua_checkstack(L, 3)) |
| 3473 | return 0; |
| 3474 | |
| 3475 | /* Create the object: obj[0] = userdata. |
| 3476 | * Note that the base of the Converters object is the |
| 3477 | * same than the TXN object. |
| 3478 | */ |
| 3479 | lua_newtable(L); |
| 3480 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3481 | lua_rawseti(L, -2, 0); |
| 3482 | appctx->appctx = ctx; |
| 3483 | appctx->htxn.s = s; |
| 3484 | appctx->htxn.p = p; |
| 3485 | |
| 3486 | /* Create the "f" field that contains a list of fetches. */ |
| 3487 | lua_pushstring(L, "f"); |
| 3488 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3489 | return 0; |
| 3490 | lua_settable(L, -3); |
| 3491 | |
| 3492 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3493 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3494 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3495 | return 0; |
| 3496 | lua_settable(L, -3); |
| 3497 | |
| 3498 | /* Create the "c" field that contains a list of converters. */ |
| 3499 | lua_pushstring(L, "c"); |
| 3500 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3501 | return 0; |
| 3502 | lua_settable(L, -3); |
| 3503 | |
| 3504 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3505 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3506 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3507 | return 0; |
| 3508 | lua_settable(L, -3); |
| 3509 | |
| 3510 | /* Pop a class stream metatable and affect it to the table. */ |
| 3511 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3512 | lua_setmetatable(L, -2); |
| 3513 | |
| 3514 | return 1; |
| 3515 | } |
| 3516 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3517 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3518 | { |
| 3519 | struct hlua_appctx *appctx; |
| 3520 | struct stream *s; |
| 3521 | const char *name; |
| 3522 | size_t len; |
| 3523 | struct sample smp; |
| 3524 | |
| 3525 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3526 | |
| 3527 | /* It is useles to retrieve the stream, but this function |
| 3528 | * runs only in a stream context. |
| 3529 | */ |
| 3530 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3531 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3532 | s = appctx->htxn.s; |
| 3533 | |
| 3534 | /* Converts the third argument in a sample. */ |
| 3535 | hlua_lua2smp(L, 3, &smp); |
| 3536 | |
| 3537 | /* Store the sample in a variable. */ |
| 3538 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3539 | vars_set_by_name(name, len, &smp); |
| 3540 | return 0; |
| 3541 | } |
| 3542 | |
| 3543 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3544 | { |
| 3545 | struct hlua_appctx *appctx; |
| 3546 | struct stream *s; |
| 3547 | const char *name; |
| 3548 | size_t len; |
| 3549 | struct sample smp; |
| 3550 | |
| 3551 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3552 | |
| 3553 | /* It is useles to retrieve the stream, but this function |
| 3554 | * runs only in a stream context. |
| 3555 | */ |
| 3556 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3557 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3558 | s = appctx->htxn.s; |
| 3559 | |
| 3560 | /* Unset the variable. */ |
| 3561 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3562 | vars_unset_by_name(name, len, &smp); |
| 3563 | return 0; |
| 3564 | } |
| 3565 | |
| 3566 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3567 | { |
| 3568 | struct hlua_appctx *appctx; |
| 3569 | struct stream *s; |
| 3570 | const char *name; |
| 3571 | size_t len; |
| 3572 | struct sample smp; |
| 3573 | |
| 3574 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3575 | |
| 3576 | /* It is useles to retrieve the stream, but this function |
| 3577 | * runs only in a stream context. |
| 3578 | */ |
| 3579 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3580 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3581 | s = appctx->htxn.s; |
| 3582 | |
| 3583 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3584 | if (!vars_get_by_name(name, len, &smp)) { |
| 3585 | lua_pushnil(L); |
| 3586 | return 1; |
| 3587 | } |
| 3588 | |
| 3589 | return hlua_smp2lua(L, &smp); |
| 3590 | } |
| 3591 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3592 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3593 | { |
| 3594 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3595 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3596 | struct hlua *hlua; |
| 3597 | |
| 3598 | /* 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] | 3599 | if (!s->hlua) |
| 3600 | return 0; |
| 3601 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3602 | |
| 3603 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3604 | |
| 3605 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3606 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3607 | |
| 3608 | /* Get and store new value. */ |
| 3609 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3610 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3611 | |
| 3612 | return 0; |
| 3613 | } |
| 3614 | |
| 3615 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3616 | { |
| 3617 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3618 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3619 | struct hlua *hlua; |
| 3620 | |
| 3621 | /* 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] | 3622 | if (!s->hlua) { |
| 3623 | lua_pushnil(L); |
| 3624 | return 1; |
| 3625 | } |
| 3626 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3627 | |
| 3628 | /* Push configuration index in the stack. */ |
| 3629 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3630 | |
| 3631 | return 1; |
| 3632 | } |
| 3633 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3634 | /* If expected data not yet available, it returns a yield. This function |
| 3635 | * consumes the data in the buffer. It returns a string containing the |
| 3636 | * data. This string can be empty. |
| 3637 | */ |
| 3638 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3639 | { |
| 3640 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3641 | struct stream_interface *si = appctx->appctx->owner; |
| 3642 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3643 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3644 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3645 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3646 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3647 | |
| 3648 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3649 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3650 | |
| 3651 | /* Data not yet avalaible. return yield. */ |
| 3652 | if (ret == 0) { |
| 3653 | si_applet_cant_get(si); |
| 3654 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
| 3655 | } |
| 3656 | |
| 3657 | /* End of data: commit the total strings and return. */ |
| 3658 | if (ret < 0) { |
| 3659 | luaL_pushresult(&appctx->b); |
| 3660 | return 1; |
| 3661 | } |
| 3662 | |
| 3663 | /* Ensure that the block 2 length is usable. */ |
| 3664 | if (ret == 1) |
| 3665 | len2 = 0; |
| 3666 | |
| 3667 | /* dont check the max length read and dont check. */ |
| 3668 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3669 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3670 | |
| 3671 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3672 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3673 | luaL_pushresult(&appctx->b); |
| 3674 | return 1; |
| 3675 | } |
| 3676 | |
| 3677 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3678 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3679 | { |
| 3680 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3681 | |
| 3682 | /* Initialise the string catenation. */ |
| 3683 | luaL_buffinit(L, &appctx->b); |
| 3684 | |
| 3685 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3686 | } |
| 3687 | |
| 3688 | /* If expected data not yet available, it returns a yield. This function |
| 3689 | * consumes the data in the buffer. It returns a string containing the |
| 3690 | * data. This string can be empty. |
| 3691 | */ |
| 3692 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3693 | { |
| 3694 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3695 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3696 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3697 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3698 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3699 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3700 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3701 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3702 | |
| 3703 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3704 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3705 | |
| 3706 | /* Data not yet avalaible. return yield. */ |
| 3707 | if (ret == 0) { |
| 3708 | si_applet_cant_get(si); |
| 3709 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3710 | } |
| 3711 | |
| 3712 | /* End of data: commit the total strings and return. */ |
| 3713 | if (ret < 0) { |
| 3714 | luaL_pushresult(&appctx->b); |
| 3715 | return 1; |
| 3716 | } |
| 3717 | |
| 3718 | /* Ensure that the block 2 length is usable. */ |
| 3719 | if (ret == 1) |
| 3720 | len2 = 0; |
| 3721 | |
| 3722 | if (len == -1) { |
| 3723 | |
| 3724 | /* If len == -1, catenate all the data avalaile and |
| 3725 | * yield because we want to get all the data until |
| 3726 | * the end of data stream. |
| 3727 | */ |
| 3728 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3729 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3730 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3731 | si_applet_cant_get(si); |
| 3732 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3733 | |
| 3734 | } else { |
| 3735 | |
| 3736 | /* Copy the fisrt block caping to the length required. */ |
| 3737 | if (len1 > len) |
| 3738 | len1 = len; |
| 3739 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3740 | len -= len1; |
| 3741 | |
| 3742 | /* Copy the second block. */ |
| 3743 | if (len2 > len) |
| 3744 | len2 = len; |
| 3745 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3746 | len -= len2; |
| 3747 | |
| 3748 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3749 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3750 | |
| 3751 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3752 | if (len > 0) { |
| 3753 | lua_pushinteger(L, len); |
| 3754 | lua_replace(L, 2); |
| 3755 | si_applet_cant_get(si); |
| 3756 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3757 | } |
| 3758 | |
| 3759 | /* return the result. */ |
| 3760 | luaL_pushresult(&appctx->b); |
| 3761 | return 1; |
| 3762 | } |
| 3763 | |
| 3764 | /* we never executes this */ |
| 3765 | hlua_pusherror(L, "Lua: internal error"); |
| 3766 | WILL_LJMP(lua_error(L)); |
| 3767 | return 0; |
| 3768 | } |
| 3769 | |
| 3770 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3771 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3772 | { |
| 3773 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3774 | int len = -1; |
| 3775 | |
| 3776 | if (lua_gettop(L) > 2) |
| 3777 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3778 | if (lua_gettop(L) >= 2) { |
| 3779 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3780 | lua_pop(L, 1); |
| 3781 | } |
| 3782 | |
| 3783 | /* Confirm or set the required length */ |
| 3784 | lua_pushinteger(L, len); |
| 3785 | |
| 3786 | /* Initialise the string catenation. */ |
| 3787 | luaL_buffinit(L, &appctx->b); |
| 3788 | |
| 3789 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3790 | } |
| 3791 | |
| 3792 | /* Append data in the output side of the buffer. This data is immediatly |
| 3793 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3794 | * cannot contains the data, the function yield. The function returns -1 |
| 3795 | * if the channel is closed. |
| 3796 | */ |
| 3797 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3798 | { |
| 3799 | size_t len; |
| 3800 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3801 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3802 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3803 | struct stream_interface *si = appctx->appctx->owner; |
| 3804 | struct channel *chn = si_ic(si); |
| 3805 | int max; |
| 3806 | |
| 3807 | /* Get the max amount of data which can write as input in the channel. */ |
| 3808 | max = channel_recv_max(chn); |
| 3809 | if (max > (len - l)) |
| 3810 | max = len - l; |
| 3811 | |
| 3812 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3813 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3814 | |
| 3815 | /* update counters. */ |
| 3816 | l += max; |
| 3817 | lua_pop(L, 1); |
| 3818 | lua_pushinteger(L, l); |
| 3819 | |
| 3820 | /* If some data is not send, declares the situation to the |
| 3821 | * applet, and returns a yield. |
| 3822 | */ |
| 3823 | if (l < len) { |
| 3824 | si_applet_cant_put(si); |
| 3825 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
| 3826 | } |
| 3827 | |
| 3828 | return 1; |
| 3829 | } |
| 3830 | |
| 3831 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3832 | * yield the LUA process, and resume it without checking the |
| 3833 | * input arguments. |
| 3834 | */ |
| 3835 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3836 | { |
| 3837 | MAY_LJMP(check_args(L, 2, "send")); |
| 3838 | lua_pushinteger(L, 0); |
| 3839 | |
| 3840 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3841 | } |
| 3842 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3843 | /* |
| 3844 | * |
| 3845 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3846 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3847 | * |
| 3848 | * |
| 3849 | */ |
| 3850 | |
| 3851 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3852 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3853 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3854 | __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] | 3855 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3856 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3857 | } |
| 3858 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3859 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3860 | * according with a current TXN. |
| 3861 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3862 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3863 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3864 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3865 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3866 | struct stream_interface *si = ctx->owner; |
| 3867 | struct stream *s = si_strm(si); |
| 3868 | struct proxy *px = s->be; |
| 3869 | struct http_txn *txn = s->txn; |
| 3870 | const char *path; |
| 3871 | const char *end; |
| 3872 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3873 | |
| 3874 | /* Check stack size. */ |
| 3875 | if (!lua_checkstack(L, 3)) |
| 3876 | return 0; |
| 3877 | |
| 3878 | /* Create the object: obj[0] = userdata. |
| 3879 | * Note that the base of the Converters object is the |
| 3880 | * same than the TXN object. |
| 3881 | */ |
| 3882 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3883 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3884 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3885 | appctx->appctx = ctx; |
| 3886 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3887 | 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] | 3888 | appctx->htxn.s = s; |
| 3889 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3890 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3891 | /* Create the "f" field that contains a list of fetches. */ |
| 3892 | lua_pushstring(L, "f"); |
| 3893 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3894 | return 0; |
| 3895 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3896 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3897 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3898 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3899 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3900 | return 0; |
| 3901 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3902 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3903 | /* Create the "c" field that contains a list of converters. */ |
| 3904 | lua_pushstring(L, "c"); |
| 3905 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3906 | return 0; |
| 3907 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3908 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3909 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3910 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3911 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3912 | return 0; |
| 3913 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3914 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3915 | /* Stores the request method. */ |
| 3916 | lua_pushstring(L, "method"); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3917 | lua_pushlstring(L, ci_head(txn->req.chn), txn->req.sl.rq.m_l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3918 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3919 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3920 | /* Stores the http version. */ |
| 3921 | lua_pushstring(L, "version"); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3922 | lua_pushlstring(L, ci_head(txn->req.chn) + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3923 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3924 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3925 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3926 | * the array on the top of the stack. |
| 3927 | */ |
| 3928 | lua_pushstring(L, "headers"); |
| 3929 | htxn.s = s; |
| 3930 | htxn.p = px; |
| 3931 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3932 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3933 | return 0; |
| 3934 | lua_settable(L, -3); |
| 3935 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3936 | /* Get path and qs */ |
| 3937 | path = http_get_path(txn); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3938 | if (path) { |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3939 | end = ci_head(txn->req.chn) + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3940 | p = path; |
| 3941 | while (p < end && *p != '?') |
| 3942 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3943 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3944 | /* Stores the request path. */ |
| 3945 | lua_pushstring(L, "path"); |
| 3946 | lua_pushlstring(L, path, p - path); |
| 3947 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3948 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3949 | /* Stores the query string. */ |
| 3950 | lua_pushstring(L, "qs"); |
| 3951 | if (*p == '?') |
| 3952 | p++; |
| 3953 | lua_pushlstring(L, p, end - p); |
| 3954 | lua_settable(L, -3); |
| 3955 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3956 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3957 | /* Stores the request path. */ |
| 3958 | lua_pushstring(L, "length"); |
| 3959 | lua_pushinteger(L, txn->req.body_len); |
| 3960 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3961 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3962 | /* Create an array of HTTP request headers. */ |
| 3963 | lua_pushstring(L, "headers"); |
| 3964 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3965 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3966 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3967 | /* Create an empty array of HTTP request headers. */ |
| 3968 | lua_pushstring(L, "response"); |
| 3969 | lua_newtable(L); |
| 3970 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3971 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3972 | /* Pop a class stream metatable and affect it to the table. */ |
| 3973 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3974 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3975 | |
| 3976 | return 1; |
| 3977 | } |
| 3978 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3979 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3980 | { |
| 3981 | struct hlua_appctx *appctx; |
| 3982 | struct stream *s; |
| 3983 | const char *name; |
| 3984 | size_t len; |
| 3985 | struct sample smp; |
| 3986 | |
| 3987 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3988 | |
| 3989 | /* It is useles to retrieve the stream, but this function |
| 3990 | * runs only in a stream context. |
| 3991 | */ |
| 3992 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3993 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3994 | s = appctx->htxn.s; |
| 3995 | |
| 3996 | /* Converts the third argument in a sample. */ |
| 3997 | hlua_lua2smp(L, 3, &smp); |
| 3998 | |
| 3999 | /* Store the sample in a variable. */ |
| 4000 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4001 | vars_set_by_name(name, len, &smp); |
| 4002 | return 0; |
| 4003 | } |
| 4004 | |
| 4005 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4006 | { |
| 4007 | struct hlua_appctx *appctx; |
| 4008 | struct stream *s; |
| 4009 | const char *name; |
| 4010 | size_t len; |
| 4011 | struct sample smp; |
| 4012 | |
| 4013 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4014 | |
| 4015 | /* It is useles to retrieve the stream, but this function |
| 4016 | * runs only in a stream context. |
| 4017 | */ |
| 4018 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4019 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4020 | s = appctx->htxn.s; |
| 4021 | |
| 4022 | /* Unset the variable. */ |
| 4023 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4024 | vars_unset_by_name(name, len, &smp); |
| 4025 | return 0; |
| 4026 | } |
| 4027 | |
| 4028 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4029 | { |
| 4030 | struct hlua_appctx *appctx; |
| 4031 | struct stream *s; |
| 4032 | const char *name; |
| 4033 | size_t len; |
| 4034 | struct sample smp; |
| 4035 | |
| 4036 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4037 | |
| 4038 | /* It is useles to retrieve the stream, but this function |
| 4039 | * runs only in a stream context. |
| 4040 | */ |
| 4041 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4042 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4043 | s = appctx->htxn.s; |
| 4044 | |
| 4045 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4046 | if (!vars_get_by_name(name, len, &smp)) { |
| 4047 | lua_pushnil(L); |
| 4048 | return 1; |
| 4049 | } |
| 4050 | |
| 4051 | return hlua_smp2lua(L, &smp); |
| 4052 | } |
| 4053 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4054 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4055 | { |
| 4056 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4057 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4058 | struct hlua *hlua; |
| 4059 | |
| 4060 | /* 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] | 4061 | if (!s->hlua) |
| 4062 | return 0; |
| 4063 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4064 | |
| 4065 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4066 | |
| 4067 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4068 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4069 | |
| 4070 | /* Get and store new value. */ |
| 4071 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4072 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4073 | |
| 4074 | return 0; |
| 4075 | } |
| 4076 | |
| 4077 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4078 | { |
| 4079 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4080 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4081 | struct hlua *hlua; |
| 4082 | |
| 4083 | /* 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] | 4084 | if (!s->hlua) { |
| 4085 | lua_pushnil(L); |
| 4086 | return 1; |
| 4087 | } |
| 4088 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4089 | |
| 4090 | /* Push configuration index in the stack. */ |
| 4091 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4092 | |
| 4093 | return 1; |
| 4094 | } |
| 4095 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4096 | /* If expected data not yet available, it returns a yield. This function |
| 4097 | * consumes the data in the buffer. It returns a string containing the |
| 4098 | * data. This string can be empty. |
| 4099 | */ |
| 4100 | __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] | 4101 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4102 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4103 | struct stream_interface *si = appctx->appctx->owner; |
| 4104 | struct channel *chn = si_ic(si); |
| 4105 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4106 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4107 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4108 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4109 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4110 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4111 | /* Maybe we cant send a 100-continue ? */ |
| 4112 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4113 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4114 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4115 | * big for the buffers. We cant send anything. So, we ignoring |
| 4116 | * the error, considers that the 100-continue is sent, and try |
| 4117 | * to receive. |
| 4118 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4119 | */ |
| 4120 | if (ret == -1) { |
| 4121 | si_applet_cant_put(si); |
| 4122 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 4123 | } |
| 4124 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4125 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4126 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4127 | /* Check for the end of the data. */ |
| 4128 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4129 | luaL_pushresult(&appctx->b); |
| 4130 | return 1; |
| 4131 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4132 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4133 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4134 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4135 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4136 | /* Data not yet avalaible. return yield. */ |
| 4137 | if (ret == 0) { |
| 4138 | si_applet_cant_get(si); |
| 4139 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 4140 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4141 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4142 | /* End of data: commit the total strings and return. */ |
| 4143 | if (ret < 0) { |
| 4144 | luaL_pushresult(&appctx->b); |
| 4145 | return 1; |
| 4146 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4147 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4148 | /* Ensure that the block 2 length is usable. */ |
| 4149 | if (ret == 1) |
| 4150 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4151 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4152 | /* Copy the fisrt block caping to the length required. */ |
| 4153 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4154 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4155 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4156 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4157 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4158 | /* Copy the second block. */ |
| 4159 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4160 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4161 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4162 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4163 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4164 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4165 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4166 | luaL_pushresult(&appctx->b); |
| 4167 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4168 | } |
| 4169 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4170 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 4171 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4172 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4173 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4174 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4175 | /* Initialise the string catenation. */ |
| 4176 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4177 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4178 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4179 | } |
| 4180 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4181 | /* If expected data not yet available, it returns a yield. This function |
| 4182 | * consumes the data in the buffer. It returns a string containing the |
| 4183 | * data. This string can be empty. |
| 4184 | */ |
| 4185 | __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] | 4186 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4187 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4188 | struct stream_interface *si = appctx->appctx->owner; |
| 4189 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4190 | struct channel *chn = si_ic(si); |
| 4191 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4192 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4193 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4194 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4195 | size_t len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4196 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4197 | /* Maybe we cant send a 100-continue ? */ |
| 4198 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4199 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4200 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4201 | * big for the buffers. We cant send anything. So, we ignoring |
| 4202 | * the error, considers that the 100-continue is sent, and try |
| 4203 | * to receive. |
| 4204 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4205 | */ |
| 4206 | if (ret == -1) { |
| 4207 | si_applet_cant_put(si); |
| 4208 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4209 | } |
| 4210 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4211 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4212 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4213 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4214 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4215 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4216 | /* Data not yet avalaible. return yield. */ |
| 4217 | if (ret == 0) { |
| 4218 | si_applet_cant_get(si); |
| 4219 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4220 | } |
| 4221 | |
| 4222 | /* End of data: commit the total strings and return. */ |
| 4223 | if (ret < 0) { |
| 4224 | luaL_pushresult(&appctx->b); |
| 4225 | return 1; |
| 4226 | } |
| 4227 | |
| 4228 | /* Ensure that the block 2 length is usable. */ |
| 4229 | if (ret == 1) |
| 4230 | len2 = 0; |
| 4231 | |
| 4232 | /* Copy the fisrt block caping to the length required. */ |
| 4233 | if (len1 > len) |
| 4234 | len1 = len; |
| 4235 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4236 | len -= len1; |
| 4237 | |
| 4238 | /* Copy the second block. */ |
| 4239 | if (len2 > len) |
| 4240 | len2 = len; |
| 4241 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4242 | len -= len2; |
| 4243 | |
| 4244 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4245 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4246 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4247 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4248 | |
| 4249 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 4250 | if (len > 0) { |
| 4251 | lua_pushinteger(L, len); |
| 4252 | lua_replace(L, 2); |
| 4253 | si_applet_cant_get(si); |
| 4254 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4255 | } |
| 4256 | |
| 4257 | /* return the result. */ |
| 4258 | luaL_pushresult(&appctx->b); |
| 4259 | return 1; |
| 4260 | } |
| 4261 | |
| 4262 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 4263 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4264 | { |
| 4265 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4266 | int len = -1; |
| 4267 | |
| 4268 | /* Check arguments. */ |
| 4269 | if (lua_gettop(L) > 2) |
| 4270 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4271 | if (lua_gettop(L) >= 2) { |
| 4272 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4273 | lua_pop(L, 1); |
| 4274 | } |
| 4275 | |
| 4276 | /* Check the required length */ |
| 4277 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4278 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4279 | lua_pushinteger(L, len); |
| 4280 | |
| 4281 | /* Initialise the string catenation. */ |
| 4282 | luaL_buffinit(L, &appctx->b); |
| 4283 | |
| 4284 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4285 | } |
| 4286 | |
| 4287 | /* Append data in the output side of the buffer. This data is immediatly |
| 4288 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 4289 | * cannot contains the data, the function yield. The function returns -1 |
| 4290 | * if the channel is closed. |
| 4291 | */ |
| 4292 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4293 | { |
| 4294 | size_t len; |
| 4295 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4296 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4297 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4298 | struct stream_interface *si = appctx->appctx->owner; |
| 4299 | struct channel *chn = si_ic(si); |
| 4300 | int max; |
| 4301 | |
| 4302 | /* Get the max amount of data which can write as input in the channel. */ |
| 4303 | max = channel_recv_max(chn); |
| 4304 | if (max > (len - l)) |
| 4305 | max = len - l; |
| 4306 | |
| 4307 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4308 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4309 | |
| 4310 | /* update counters. */ |
| 4311 | l += max; |
| 4312 | lua_pop(L, 1); |
| 4313 | lua_pushinteger(L, l); |
| 4314 | |
| 4315 | /* If some data is not send, declares the situation to the |
| 4316 | * applet, and returns a yield. |
| 4317 | */ |
| 4318 | if (l < len) { |
| 4319 | si_applet_cant_put(si); |
| 4320 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
| 4321 | } |
| 4322 | |
| 4323 | return 1; |
| 4324 | } |
| 4325 | |
| 4326 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4327 | * yield the LUA process, and resume it without checking the |
| 4328 | * input arguments. |
| 4329 | */ |
| 4330 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4331 | { |
| 4332 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4333 | size_t len; |
| 4334 | char hex[10]; |
| 4335 | |
| 4336 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4337 | |
| 4338 | /* If transfer encoding chunked is selected, we surround the data |
| 4339 | * by chunk data. |
| 4340 | */ |
| 4341 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4342 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4343 | lua_pushfstring(L, "%s\r\n", hex); |
| 4344 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4345 | lua_pushstring(L, "\r\n"); |
| 4346 | lua_concat(L, 3); |
| 4347 | } |
| 4348 | |
| 4349 | /* This interger is used for followinf the amount of data sent. */ |
| 4350 | lua_pushinteger(L, 0); |
| 4351 | |
| 4352 | /* We want to send some data. Headers must be sent. */ |
| 4353 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4354 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4355 | WILL_LJMP(lua_error(L)); |
| 4356 | } |
| 4357 | |
| 4358 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4359 | } |
| 4360 | |
| 4361 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4362 | { |
| 4363 | const char *name; |
| 4364 | int ret; |
| 4365 | |
| 4366 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4367 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4368 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4369 | |
| 4370 | /* Push in the stack the "response" entry. */ |
| 4371 | ret = lua_getfield(L, 1, "response"); |
| 4372 | if (ret != LUA_TTABLE) { |
| 4373 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4374 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4375 | WILL_LJMP(lua_error(L)); |
| 4376 | } |
| 4377 | |
| 4378 | /* check if the header is already registered if it is not |
| 4379 | * the case, register it. |
| 4380 | */ |
| 4381 | ret = lua_getfield(L, -1, name); |
| 4382 | if (ret == LUA_TNIL) { |
| 4383 | |
| 4384 | /* Entry not found. */ |
| 4385 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4386 | |
| 4387 | /* Insert the new header name in the array in the top of the stack. |
| 4388 | * It left the new array in the top of the stack. |
| 4389 | */ |
| 4390 | lua_newtable(L); |
| 4391 | lua_pushvalue(L, 2); |
| 4392 | lua_pushvalue(L, -2); |
| 4393 | lua_settable(L, -4); |
| 4394 | |
| 4395 | } else if (ret != LUA_TTABLE) { |
| 4396 | |
| 4397 | /* corruption error. */ |
| 4398 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4399 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4400 | WILL_LJMP(lua_error(L)); |
| 4401 | } |
| 4402 | |
| 4403 | /* Now the top od thestack is an array of values. We push |
| 4404 | * the header value as new entry. |
| 4405 | */ |
| 4406 | lua_pushvalue(L, 3); |
| 4407 | ret = lua_rawlen(L, -2); |
| 4408 | lua_rawseti(L, -2, ret + 1); |
| 4409 | lua_pushboolean(L, 1); |
| 4410 | return 1; |
| 4411 | } |
| 4412 | |
| 4413 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4414 | { |
| 4415 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4416 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4417 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4418 | |
| 4419 | if (status < 100 || status > 599) { |
| 4420 | lua_pushboolean(L, 0); |
| 4421 | return 1; |
| 4422 | } |
| 4423 | |
| 4424 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4425 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4426 | lua_pushboolean(L, 1); |
| 4427 | return 1; |
| 4428 | } |
| 4429 | |
| 4430 | /* We will build the status line and the headers of the HTTP response. |
| 4431 | * We will try send at once if its not possible, we give back the hand |
| 4432 | * waiting for more room. |
| 4433 | */ |
| 4434 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4435 | { |
| 4436 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4437 | struct stream_interface *si = appctx->appctx->owner; |
| 4438 | struct channel *chn = si_ic(si); |
| 4439 | int ret; |
| 4440 | size_t len; |
| 4441 | const char *msg; |
| 4442 | |
| 4443 | /* Get the message as the first argument on the stack. */ |
| 4444 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4445 | |
| 4446 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4447 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4448 | |
| 4449 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4450 | * big for the buffers. |
| 4451 | */ |
| 4452 | if (ret == -2 || ret == -3) { |
| 4453 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4454 | WILL_LJMP(lua_error(L)); |
| 4455 | } |
| 4456 | |
| 4457 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4458 | if (ret == -1) { |
| 4459 | si_applet_cant_put(si); |
| 4460 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
| 4461 | } |
| 4462 | |
| 4463 | /* Headers sent, set the flag. */ |
| 4464 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4465 | return 0; |
| 4466 | } |
| 4467 | |
| 4468 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4469 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4470 | struct buffer *tmp = get_trash_chunk(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4471 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4472 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4473 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4474 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4475 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4476 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4477 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4478 | int hdr_chunked = 0; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4479 | const char *reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4480 | |
| 4481 | if (reason == NULL) |
| 4482 | reason = get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4483 | |
| 4484 | /* Use the same http version than the request. */ |
| 4485 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4486 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4487 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4488 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4489 | |
| 4490 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4491 | lua_pushvalue(L, 0); |
| 4492 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4493 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4494 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4495 | WILL_LJMP(lua_error(L)); |
| 4496 | } |
| 4497 | |
| 4498 | /* Browse the list of headers. */ |
| 4499 | lua_pushnil(L); |
| 4500 | while(lua_next(L, -2) != 0) { |
| 4501 | |
| 4502 | /* We expect a string as -2. */ |
| 4503 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4504 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4505 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4506 | lua_typename(L, lua_type(L, -2))); |
| 4507 | WILL_LJMP(lua_error(L)); |
| 4508 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4509 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4510 | |
| 4511 | /* We expect an array as -1. */ |
| 4512 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4513 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4514 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4515 | name, |
| 4516 | lua_typename(L, lua_type(L, -1))); |
| 4517 | WILL_LJMP(lua_error(L)); |
| 4518 | } |
| 4519 | |
| 4520 | /* Browse the table who is on the top of the stack. */ |
| 4521 | lua_pushnil(L); |
| 4522 | while(lua_next(L, -2) != 0) { |
| 4523 | |
| 4524 | /* We expect a number as -2. */ |
| 4525 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4526 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4527 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4528 | name, |
| 4529 | lua_typename(L, lua_type(L, -2))); |
| 4530 | WILL_LJMP(lua_error(L)); |
| 4531 | } |
| 4532 | id = lua_tointeger(L, -2); |
| 4533 | |
| 4534 | /* We expect a string as -2. */ |
| 4535 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4536 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4537 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4538 | name, id, |
| 4539 | lua_typename(L, lua_type(L, -1))); |
| 4540 | WILL_LJMP(lua_error(L)); |
| 4541 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4542 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4543 | |
| 4544 | /* Catenate a new header. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4545 | if (tmp->data + name_len + 2 + value_len + 2 < tmp->size) { |
| 4546 | memcpy(tmp->area + tmp->data, name, name_len); |
| 4547 | tmp->data += name_len; |
| 4548 | tmp->area[tmp->data++] = ':'; |
| 4549 | tmp->area[tmp->data++] = ' '; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4550 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4551 | memcpy(tmp->area + tmp->data, value, |
| 4552 | value_len); |
| 4553 | tmp->data += value_len; |
| 4554 | tmp->area[tmp->data++] = '\r'; |
| 4555 | tmp->area[tmp->data++] = '\n'; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4556 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4557 | |
| 4558 | /* Protocol checks. */ |
| 4559 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4560 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4561 | * is done without control. If it contains a bad value, |
| 4562 | * the content-length remains negative so that we can |
| 4563 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4564 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4565 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4566 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4567 | |
| 4568 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4569 | if (name_len == 17 && value_len == 7 && |
| 4570 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4571 | strcasecmp("chunked", value) == 0) |
| 4572 | hdr_chunked = 1; |
| 4573 | |
| 4574 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4575 | lua_pop(L, 1); |
| 4576 | } |
| 4577 | |
| 4578 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4579 | lua_pop(L, 1); |
| 4580 | } |
| 4581 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4582 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4583 | * and the status code implies the presence of a message body, we must |
| 4584 | * announce a transfer encoding chunked. This is required by haproxy |
| 4585 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4586 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4587 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4588 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4589 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 4590 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4591 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4592 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4593 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4594 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4595 | } |
| 4596 | |
| 4597 | /* Finalize headers. */ |
| 4598 | chunk_appendf(tmp, "\r\n"); |
| 4599 | |
| 4600 | /* Remove the last entry and the array of headers */ |
| 4601 | lua_pop(L, 2); |
| 4602 | |
| 4603 | /* Push the headers block. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4604 | lua_pushlstring(L, tmp->area, tmp->data); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4605 | |
| 4606 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4607 | } |
| 4608 | |
| 4609 | /* |
| 4610 | * |
| 4611 | * |
| 4612 | * Class HTTP |
| 4613 | * |
| 4614 | * |
| 4615 | */ |
| 4616 | |
| 4617 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4618 | * a class stream, otherwise it throws an error. |
| 4619 | */ |
| 4620 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4621 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4622 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4623 | } |
| 4624 | |
| 4625 | /* This function creates and push in the stack a HTTP object |
| 4626 | * according with a current TXN. |
| 4627 | */ |
| 4628 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4629 | { |
| 4630 | struct hlua_txn *htxn; |
| 4631 | |
| 4632 | /* Check stack size. */ |
| 4633 | if (!lua_checkstack(L, 3)) |
| 4634 | return 0; |
| 4635 | |
| 4636 | /* Create the object: obj[0] = userdata. |
| 4637 | * Note that the base of the Converters object is the |
| 4638 | * same than the TXN object. |
| 4639 | */ |
| 4640 | lua_newtable(L); |
| 4641 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4642 | lua_rawseti(L, -2, 0); |
| 4643 | |
| 4644 | htxn->s = txn->s; |
| 4645 | htxn->p = txn->p; |
| 4646 | |
| 4647 | /* Pop a class stream metatable and affect it to the table. */ |
| 4648 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4649 | lua_setmetatable(L, -2); |
| 4650 | |
| 4651 | return 1; |
| 4652 | } |
| 4653 | |
| 4654 | /* This function creates ans returns an array of HTTP headers. |
| 4655 | * This function does not fails. It is used as wrapper with the |
| 4656 | * 2 following functions. |
| 4657 | */ |
| 4658 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4659 | { |
| 4660 | const char *cur_ptr, *cur_next, *p; |
| 4661 | int old_idx, cur_idx; |
| 4662 | struct hdr_idx_elem *cur_hdr; |
| 4663 | const char *hn, *hv; |
| 4664 | int hnl, hvl; |
| 4665 | int type; |
| 4666 | const char *in; |
| 4667 | char *out; |
| 4668 | int len; |
| 4669 | |
| 4670 | /* Create the table. */ |
| 4671 | lua_newtable(L); |
| 4672 | |
| 4673 | if (!htxn->s->txn) |
| 4674 | return 1; |
| 4675 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4676 | /* Check if a valid response is parsed */ |
| 4677 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4678 | return 1; |
| 4679 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4680 | /* Build array of headers. */ |
| 4681 | old_idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 4682 | cur_next = ci_head(msg->chn) + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4683 | |
| 4684 | while (1) { |
| 4685 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4686 | if (!cur_idx) |
| 4687 | break; |
| 4688 | old_idx = cur_idx; |
| 4689 | |
| 4690 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4691 | cur_ptr = cur_next; |
| 4692 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4693 | |
| 4694 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4695 | * and the next header starts at cur_next. We'll check |
| 4696 | * this header in the list as well as against the default |
| 4697 | * rule. |
| 4698 | */ |
| 4699 | |
| 4700 | /* look for ': *'. */ |
| 4701 | hn = cur_ptr; |
| 4702 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4703 | if (p >= cur_ptr+cur_hdr->len) |
| 4704 | continue; |
| 4705 | hnl = p - hn; |
| 4706 | p++; |
| 4707 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4708 | p++; |
| 4709 | if (p >= cur_ptr+cur_hdr->len) |
| 4710 | continue; |
| 4711 | hv = p; |
| 4712 | hvl = cur_ptr+cur_hdr->len-p; |
| 4713 | |
| 4714 | /* Lowercase the key. Don't check the size of trash, it have |
| 4715 | * the size of one buffer and the input data contains in one |
| 4716 | * buffer. |
| 4717 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4718 | out = trash.area; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4719 | for (in=hn; in<hn+hnl; in++, out++) |
| 4720 | *out = tolower(*in); |
| 4721 | *out = '\0'; |
| 4722 | |
| 4723 | /* Check for existing entry: |
| 4724 | * assume that the table is on the top of the stack, and |
| 4725 | * push the key in the stack, the function lua_gettable() |
| 4726 | * perform the lookup. |
| 4727 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4728 | lua_pushlstring(L, trash.area, hnl); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4729 | lua_gettable(L, -2); |
| 4730 | type = lua_type(L, -1); |
| 4731 | |
| 4732 | switch (type) { |
| 4733 | case LUA_TNIL: |
| 4734 | /* Table not found, create it. */ |
| 4735 | lua_pop(L, 1); /* remove the nil value. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4736 | lua_pushlstring(L, trash.area, hnl); /* push the header name as key. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4737 | lua_newtable(L); /* create and push empty table. */ |
| 4738 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4739 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4740 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4741 | break; |
| 4742 | |
| 4743 | case LUA_TTABLE: |
| 4744 | /* Entry found: push the value in the table. */ |
| 4745 | len = lua_rawlen(L, -1); |
| 4746 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4747 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4748 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4749 | break; |
| 4750 | |
| 4751 | default: |
| 4752 | /* Other cases are errors. */ |
| 4753 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4754 | WILL_LJMP(lua_error(L)); |
| 4755 | } |
| 4756 | } |
| 4757 | |
| 4758 | return 1; |
| 4759 | } |
| 4760 | |
| 4761 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4762 | { |
| 4763 | struct hlua_txn *htxn; |
| 4764 | |
| 4765 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4766 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4767 | |
| 4768 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4769 | } |
| 4770 | |
| 4771 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4772 | { |
| 4773 | struct hlua_txn *htxn; |
| 4774 | |
| 4775 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4776 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4777 | |
| 4778 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4779 | } |
| 4780 | |
| 4781 | /* This function replace full header, or just a value in |
| 4782 | * the request or in the response. It is a wrapper fir the |
| 4783 | * 4 following functions. |
| 4784 | */ |
| 4785 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4786 | struct http_msg *msg, int action) |
| 4787 | { |
| 4788 | size_t name_len; |
| 4789 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4790 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4791 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4792 | struct my_regex re; |
| 4793 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4794 | /* Check if a valid response is parsed */ |
| 4795 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4796 | return 0; |
| 4797 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4798 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4799 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4800 | |
| 4801 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4802 | regex_free(&re); |
| 4803 | return 0; |
| 4804 | } |
| 4805 | |
| 4806 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4807 | { |
| 4808 | struct hlua_txn *htxn; |
| 4809 | |
| 4810 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4811 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4812 | |
| 4813 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4814 | } |
| 4815 | |
| 4816 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4817 | { |
| 4818 | struct hlua_txn *htxn; |
| 4819 | |
| 4820 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4821 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4822 | |
| 4823 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4824 | } |
| 4825 | |
| 4826 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4827 | { |
| 4828 | struct hlua_txn *htxn; |
| 4829 | |
| 4830 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4831 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4832 | |
| 4833 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4834 | } |
| 4835 | |
| 4836 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4837 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4838 | struct hlua_txn *htxn; |
| 4839 | |
| 4840 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4841 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4842 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4843 | 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] | 4844 | } |
| 4845 | |
| 4846 | /* This function deletes all the occurences of an header. |
| 4847 | * It is a wrapper for the 2 following functions. |
| 4848 | */ |
| 4849 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4850 | { |
| 4851 | size_t len; |
| 4852 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4853 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4854 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4855 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4856 | /* Check if a valid response is parsed */ |
| 4857 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4858 | return 0; |
| 4859 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4860 | ctx.idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 4861 | while (http_find_header2(name, len, ci_head(msg->chn), &txn->hdr_idx, &ctx)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4862 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4863 | return 0; |
| 4864 | } |
| 4865 | |
| 4866 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4867 | { |
| 4868 | struct hlua_txn *htxn; |
| 4869 | |
| 4870 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4871 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4872 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4873 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4874 | } |
| 4875 | |
| 4876 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4877 | { |
| 4878 | struct hlua_txn *htxn; |
| 4879 | |
| 4880 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4881 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4882 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4883 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4884 | } |
| 4885 | |
| 4886 | /* This function adds an header. It is a wrapper used by |
| 4887 | * the 2 following functions. |
| 4888 | */ |
| 4889 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4890 | { |
| 4891 | size_t name_len; |
| 4892 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4893 | size_t value_len; |
| 4894 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4895 | char *p; |
| 4896 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4897 | /* Check if a valid message is parsed */ |
| 4898 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4899 | return 0; |
| 4900 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4901 | /* Check length. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4902 | trash.data = value_len + name_len + 2; |
| 4903 | if (trash.data > trash.size) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4904 | return 0; |
| 4905 | |
| 4906 | /* Creates the header string. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4907 | p = trash.area; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4908 | memcpy(p, name, name_len); |
| 4909 | p += name_len; |
| 4910 | *p = ':'; |
| 4911 | p++; |
| 4912 | *p = ' '; |
| 4913 | p++; |
| 4914 | memcpy(p, value, value_len); |
| 4915 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4916 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4917 | trash.area, trash.data) != 0); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4918 | |
| 4919 | return 0; |
| 4920 | } |
| 4921 | |
| 4922 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4923 | { |
| 4924 | struct hlua_txn *htxn; |
| 4925 | |
| 4926 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4927 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4928 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4929 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4930 | } |
| 4931 | |
| 4932 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4933 | { |
| 4934 | struct hlua_txn *htxn; |
| 4935 | |
| 4936 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4937 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4938 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4939 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4940 | } |
| 4941 | |
| 4942 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4943 | { |
| 4944 | struct hlua_txn *htxn; |
| 4945 | |
| 4946 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4947 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4948 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4949 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4950 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4951 | } |
| 4952 | |
| 4953 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4954 | { |
| 4955 | struct hlua_txn *htxn; |
| 4956 | |
| 4957 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4958 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4959 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4960 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4961 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4962 | } |
| 4963 | |
| 4964 | /* This function set the method. */ |
| 4965 | static int hlua_http_req_set_meth(lua_State *L) |
| 4966 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4967 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4968 | size_t name_len; |
| 4969 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4970 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4971 | /* Check if a valid request is parsed */ |
| 4972 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4973 | lua_pushboolean(L, 0); |
| 4974 | return 1; |
| 4975 | } |
| 4976 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4977 | 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] | 4978 | return 1; |
| 4979 | } |
| 4980 | |
| 4981 | /* This function set the method. */ |
| 4982 | static int hlua_http_req_set_path(lua_State *L) |
| 4983 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4984 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4985 | size_t name_len; |
| 4986 | 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] | 4987 | |
| 4988 | /* Check if a valid request is parsed */ |
| 4989 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4990 | lua_pushboolean(L, 0); |
| 4991 | return 1; |
| 4992 | } |
| 4993 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4994 | 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] | 4995 | return 1; |
| 4996 | } |
| 4997 | |
| 4998 | /* This function set the query-string. */ |
| 4999 | static int hlua_http_req_set_query(lua_State *L) |
| 5000 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5001 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5002 | size_t name_len; |
| 5003 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5004 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5005 | /* Check if a valid request is parsed */ |
| 5006 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5007 | lua_pushboolean(L, 0); |
| 5008 | return 1; |
| 5009 | } |
| 5010 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5011 | /* Check length. */ |
| 5012 | if (name_len > trash.size - 1) { |
| 5013 | lua_pushboolean(L, 0); |
| 5014 | return 1; |
| 5015 | } |
| 5016 | |
| 5017 | /* Add the mark question as prefix. */ |
| 5018 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5019 | trash.area[trash.data++] = '?'; |
| 5020 | memcpy(trash.area + trash.data, name, name_len); |
| 5021 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5022 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5023 | lua_pushboolean(L, |
| 5024 | http_replace_req_line(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5025 | return 1; |
| 5026 | } |
| 5027 | |
| 5028 | /* This function set the uri. */ |
| 5029 | static int hlua_http_req_set_uri(lua_State *L) |
| 5030 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5031 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5032 | size_t name_len; |
| 5033 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5034 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5035 | /* Check if a valid request is parsed */ |
| 5036 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 5037 | lua_pushboolean(L, 0); |
| 5038 | return 1; |
| 5039 | } |
| 5040 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 5041 | 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] | 5042 | return 1; |
| 5043 | } |
| 5044 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5045 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5046 | static int hlua_http_res_set_status(lua_State *L) |
| 5047 | { |
| 5048 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5049 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5050 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5051 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 5052 | /* Check if a valid response is parsed */ |
| 5053 | if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 5054 | return 0; |
| 5055 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5056 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5057 | return 0; |
| 5058 | } |
| 5059 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5060 | /* |
| 5061 | * |
| 5062 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5063 | * Class TXN |
| 5064 | * |
| 5065 | * |
| 5066 | */ |
| 5067 | |
| 5068 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5069 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5070 | */ |
| 5071 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5072 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5073 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5074 | } |
| 5075 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5076 | __LJMP static int hlua_set_var(lua_State *L) |
| 5077 | { |
| 5078 | struct hlua_txn *htxn; |
| 5079 | const char *name; |
| 5080 | size_t len; |
| 5081 | struct sample smp; |
| 5082 | |
| 5083 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5084 | |
| 5085 | /* It is useles to retrieve the stream, but this function |
| 5086 | * runs only in a stream context. |
| 5087 | */ |
| 5088 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5089 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5090 | |
| 5091 | /* Converts the third argument in a sample. */ |
| 5092 | hlua_lua2smp(L, 3, &smp); |
| 5093 | |
| 5094 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5095 | 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] | 5096 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5097 | return 0; |
| 5098 | } |
| 5099 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5100 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5101 | { |
| 5102 | struct hlua_txn *htxn; |
| 5103 | const char *name; |
| 5104 | size_t len; |
| 5105 | struct sample smp; |
| 5106 | |
| 5107 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5108 | |
| 5109 | /* It is useles to retrieve the stream, but this function |
| 5110 | * runs only in a stream context. |
| 5111 | */ |
| 5112 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5113 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5114 | |
| 5115 | /* Unset the variable. */ |
| 5116 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5117 | vars_unset_by_name(name, len, &smp); |
| 5118 | return 0; |
| 5119 | } |
| 5120 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5121 | __LJMP static int hlua_get_var(lua_State *L) |
| 5122 | { |
| 5123 | struct hlua_txn *htxn; |
| 5124 | const char *name; |
| 5125 | size_t len; |
| 5126 | struct sample smp; |
| 5127 | |
| 5128 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5129 | |
| 5130 | /* It is useles to retrieve the stream, but this function |
| 5131 | * runs only in a stream context. |
| 5132 | */ |
| 5133 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5134 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5135 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5136 | 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] | 5137 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5138 | lua_pushnil(L); |
| 5139 | return 1; |
| 5140 | } |
| 5141 | |
| 5142 | return hlua_smp2lua(L, &smp); |
| 5143 | } |
| 5144 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5145 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5146 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5147 | struct hlua *hlua; |
| 5148 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5149 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5150 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5151 | /* It is useles to retrieve the stream, but this function |
| 5152 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5153 | */ |
| 5154 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5155 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5156 | |
| 5157 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5158 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5159 | |
| 5160 | /* Get and store new value. */ |
| 5161 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5162 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5163 | |
| 5164 | return 0; |
| 5165 | } |
| 5166 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5167 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5168 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5169 | struct hlua *hlua; |
| 5170 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5171 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5172 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5173 | /* It is useles to retrieve the stream, but this function |
| 5174 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5175 | */ |
| 5176 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5177 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5178 | |
| 5179 | /* Push configuration index in the stack. */ |
| 5180 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5181 | |
| 5182 | return 1; |
| 5183 | } |
| 5184 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5185 | /* Create stack entry containing a class TXN. This function |
| 5186 | * return 0 if the stack does not contains free slots, |
| 5187 | * otherwise it returns 1. |
| 5188 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5189 | 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] | 5190 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5191 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5192 | |
| 5193 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5194 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5195 | return 0; |
| 5196 | |
| 5197 | /* NOTE: The allocation never fails. The failure |
| 5198 | * throw an error, and the function never returns. |
| 5199 | * if the throw is not avalaible, the process is aborted. |
| 5200 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5201 | /* Create the object: obj[0] = userdata. */ |
| 5202 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5203 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5204 | lua_rawseti(L, -2, 0); |
| 5205 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5206 | htxn->s = s; |
| 5207 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5208 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5209 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5210 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5211 | /* Create the "f" field that contains a list of fetches. */ |
| 5212 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5213 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5214 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5215 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5216 | |
| 5217 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5218 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5219 | 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] | 5220 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5221 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5222 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5223 | /* Create the "c" field that contains a list of converters. */ |
| 5224 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5225 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5226 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5227 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5228 | |
| 5229 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5230 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5231 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5232 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5233 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5234 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5235 | /* Create the "req" field that contains the request channel object. */ |
| 5236 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5237 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5238 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5239 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5240 | |
| 5241 | /* Create the "res" field that contains the response channel object. */ |
| 5242 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5243 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5244 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5245 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5246 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5247 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5248 | lua_pushstring(L, "http"); |
| 5249 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5250 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5251 | return 0; |
| 5252 | } |
| 5253 | else |
| 5254 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5255 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5256 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5257 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5258 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5259 | lua_setmetatable(L, -2); |
| 5260 | |
| 5261 | return 1; |
| 5262 | } |
| 5263 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5264 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5265 | { |
| 5266 | const char *msg; |
| 5267 | struct hlua_txn *htxn; |
| 5268 | |
| 5269 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5270 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5271 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5272 | |
| 5273 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5274 | return 0; |
| 5275 | } |
| 5276 | |
| 5277 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5278 | { |
| 5279 | int level; |
| 5280 | const char *msg; |
| 5281 | struct hlua_txn *htxn; |
| 5282 | |
| 5283 | MAY_LJMP(check_args(L, 3, "log")); |
| 5284 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5285 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5286 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5287 | |
| 5288 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5289 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5290 | |
| 5291 | hlua_sendlog(htxn->s->be, level, msg); |
| 5292 | return 0; |
| 5293 | } |
| 5294 | |
| 5295 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5296 | { |
| 5297 | const char *msg; |
| 5298 | struct hlua_txn *htxn; |
| 5299 | |
| 5300 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5301 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5302 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5303 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5304 | return 0; |
| 5305 | } |
| 5306 | |
| 5307 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5308 | { |
| 5309 | const char *msg; |
| 5310 | struct hlua_txn *htxn; |
| 5311 | |
| 5312 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5313 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5314 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5315 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5316 | return 0; |
| 5317 | } |
| 5318 | |
| 5319 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5320 | { |
| 5321 | const char *msg; |
| 5322 | struct hlua_txn *htxn; |
| 5323 | |
| 5324 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5325 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5326 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5327 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5328 | return 0; |
| 5329 | } |
| 5330 | |
| 5331 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5332 | { |
| 5333 | const char *msg; |
| 5334 | struct hlua_txn *htxn; |
| 5335 | |
| 5336 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5337 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5338 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5339 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5340 | return 0; |
| 5341 | } |
| 5342 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5343 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5344 | { |
| 5345 | struct hlua_txn *htxn; |
| 5346 | int ll; |
| 5347 | |
| 5348 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5349 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5350 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5351 | |
| 5352 | if (ll < 0 || ll > 7) |
| 5353 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5354 | |
| 5355 | htxn->s->logs.level = ll; |
| 5356 | return 0; |
| 5357 | } |
| 5358 | |
| 5359 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5360 | { |
| 5361 | struct hlua_txn *htxn; |
| 5362 | struct connection *cli_conn; |
| 5363 | int tos; |
| 5364 | |
| 5365 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5366 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5367 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5368 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5369 | 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] | 5370 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5371 | |
| 5372 | return 0; |
| 5373 | } |
| 5374 | |
| 5375 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5376 | { |
| 5377 | #ifdef SO_MARK |
| 5378 | struct hlua_txn *htxn; |
| 5379 | struct connection *cli_conn; |
| 5380 | int mark; |
| 5381 | |
| 5382 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5383 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5384 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5385 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5386 | 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] | 5387 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5388 | #endif |
| 5389 | return 0; |
| 5390 | } |
| 5391 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5392 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5393 | { |
| 5394 | struct hlua_txn *htxn; |
| 5395 | |
| 5396 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5397 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5398 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5399 | return 0; |
| 5400 | } |
| 5401 | |
| 5402 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5403 | { |
| 5404 | struct hlua_txn *htxn; |
| 5405 | |
| 5406 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5407 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5408 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5409 | return 0; |
| 5410 | } |
| 5411 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5412 | /* This function is an Lua binding that send pending data |
| 5413 | * to the client, and close the stream interface. |
| 5414 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5415 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5416 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5417 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5418 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5419 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5420 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5421 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5422 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5423 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5424 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5425 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5426 | * session, so we just end the execution of the current |
| 5427 | * lua code. |
| 5428 | */ |
| 5429 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5430 | WILL_LJMP(hlua_done(L)); |
| 5431 | return 0; |
| 5432 | } |
| 5433 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5434 | ic = &htxn->s->req; |
| 5435 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5436 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5437 | if (htxn->s->txn) { |
| 5438 | /* HTTP mode, let's stay in sync with the stream */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 5439 | b_del(&ic->buf, htxn->s->txn->req.sov); |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5440 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 5441 | htxn->s->txn->req.sov = 0; |
| 5442 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 5443 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 5444 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5445 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 5446 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5447 | /* Note that if we want to support keep-alive, we need |
| 5448 | * to bypass the close/shutr_now calls below, but that |
| 5449 | * may only be done if the HTTP request was already |
| 5450 | * processed and the connection header is known (ie |
| 5451 | * not during TCP rules). |
| 5452 | */ |
| 5453 | } |
| 5454 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5455 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5456 | channel_abort(ic); |
| 5457 | channel_auto_close(ic); |
| 5458 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5459 | |
| 5460 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5461 | channel_auto_read(oc); |
| 5462 | channel_auto_close(oc); |
| 5463 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5464 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 5465 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5466 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5467 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5468 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5469 | return 0; |
| 5470 | } |
| 5471 | |
| 5472 | __LJMP static int hlua_log(lua_State *L) |
| 5473 | { |
| 5474 | int level; |
| 5475 | const char *msg; |
| 5476 | |
| 5477 | MAY_LJMP(check_args(L, 2, "log")); |
| 5478 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5479 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5480 | |
| 5481 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5482 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5483 | |
| 5484 | hlua_sendlog(NULL, level, msg); |
| 5485 | return 0; |
| 5486 | } |
| 5487 | |
| 5488 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5489 | { |
| 5490 | const char *msg; |
| 5491 | |
| 5492 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5493 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5494 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5495 | return 0; |
| 5496 | } |
| 5497 | |
| 5498 | __LJMP static int hlua_log_info(lua_State *L) |
| 5499 | { |
| 5500 | const char *msg; |
| 5501 | |
| 5502 | MAY_LJMP(check_args(L, 1, "info")); |
| 5503 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5504 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5505 | return 0; |
| 5506 | } |
| 5507 | |
| 5508 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5509 | { |
| 5510 | const char *msg; |
| 5511 | |
| 5512 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5513 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5514 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5515 | return 0; |
| 5516 | } |
| 5517 | |
| 5518 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5519 | { |
| 5520 | const char *msg; |
| 5521 | |
| 5522 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5523 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5524 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5525 | return 0; |
| 5526 | } |
| 5527 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5528 | __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] | 5529 | { |
| 5530 | int wakeup_ms = lua_tointeger(L, -1); |
| 5531 | if (now_ms < wakeup_ms) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5532 | 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] | 5533 | return 0; |
| 5534 | } |
| 5535 | |
| 5536 | __LJMP static int hlua_sleep(lua_State *L) |
| 5537 | { |
| 5538 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5539 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5540 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5541 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5542 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5543 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5544 | wakeup_ms = tick_add(now_ms, delay); |
| 5545 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5546 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5547 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5548 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5549 | } |
| 5550 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5551 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5552 | { |
| 5553 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5554 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5555 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5556 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5557 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5558 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5559 | wakeup_ms = tick_add(now_ms, delay); |
| 5560 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5561 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5562 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5563 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5564 | } |
| 5565 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5566 | /* This functionis an LUA binding. it permits to give back |
| 5567 | * the hand at the HAProxy scheduler. It is used when the |
| 5568 | * LUA processing consumes a lot of time. |
| 5569 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5570 | __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] | 5571 | { |
| 5572 | return 0; |
| 5573 | } |
| 5574 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5575 | __LJMP static int hlua_yield(lua_State *L) |
| 5576 | { |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5577 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 5578 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5579 | } |
| 5580 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5581 | /* This function change the nice of the currently executed |
| 5582 | * task. It is used set low or high priority at the current |
| 5583 | * task. |
| 5584 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5585 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5586 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5587 | struct hlua *hlua; |
| 5588 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5589 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5590 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5591 | hlua = hlua_gethlua(L); |
| 5592 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5593 | |
| 5594 | /* If he task is not set, I'm in a start mode. */ |
| 5595 | if (!hlua || !hlua->task) |
| 5596 | return 0; |
| 5597 | |
| 5598 | if (nice < -1024) |
| 5599 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5600 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5601 | nice = 1024; |
| 5602 | |
| 5603 | hlua->task->nice = nice; |
| 5604 | return 0; |
| 5605 | } |
| 5606 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5607 | /* This function is used as a calback of a task. It is called by the |
| 5608 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5609 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5610 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5611 | * |
| 5612 | * Task wrapper are longjmp safe because the only one Lua code |
| 5613 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5614 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5615 | static struct task *hlua_process_task(struct task *task, void *context, unsigned short state) |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5616 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5617 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5618 | enum hlua_exec status; |
| 5619 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 5620 | if (task->thread_mask == MAX_THREADS_MASK) |
| 5621 | task_set_affinity(task, tid_bit); |
| 5622 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5623 | /* If it is the first call to the task, we must initialize the |
| 5624 | * execution timeouts. |
| 5625 | */ |
| 5626 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5627 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5628 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5629 | /* Execute the Lua code. */ |
| 5630 | status = hlua_ctx_resume(hlua, 1); |
| 5631 | |
| 5632 | switch (status) { |
| 5633 | /* finished or yield */ |
| 5634 | case HLUA_E_OK: |
| 5635 | hlua_ctx_destroy(hlua); |
| 5636 | task_delete(task); |
| 5637 | task_free(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 5638 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5639 | break; |
| 5640 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5641 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 5642 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 5643 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5644 | break; |
| 5645 | |
| 5646 | /* finished with error. */ |
| 5647 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5648 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5649 | hlua_ctx_destroy(hlua); |
| 5650 | task_delete(task); |
| 5651 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5652 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5653 | break; |
| 5654 | |
| 5655 | case HLUA_E_ERR: |
| 5656 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5657 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5658 | hlua_ctx_destroy(hlua); |
| 5659 | task_delete(task); |
| 5660 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5661 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5662 | break; |
| 5663 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5664 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5665 | } |
| 5666 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5667 | /* This function is an LUA binding that register LUA function to be |
| 5668 | * executed after the HAProxy configuration parsing and before the |
| 5669 | * HAProxy scheduler starts. This function expect only one LUA |
| 5670 | * argument that is a function. This function returns nothing, but |
| 5671 | * throws if an error is encountered. |
| 5672 | */ |
| 5673 | __LJMP static int hlua_register_init(lua_State *L) |
| 5674 | { |
| 5675 | struct hlua_init_function *init; |
| 5676 | int ref; |
| 5677 | |
| 5678 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5679 | |
| 5680 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5681 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5682 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5683 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5684 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5685 | |
| 5686 | init->function_ref = ref; |
| 5687 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5688 | return 0; |
| 5689 | } |
| 5690 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5691 | /* This functio is an LUA binding. It permits to register a task |
| 5692 | * executed in parallel of the main HAroxy activity. The task is |
| 5693 | * created and it is set in the HAProxy scheduler. It can be called |
| 5694 | * from the "init" section, "post init" or during the runtime. |
| 5695 | * |
| 5696 | * Lua prototype: |
| 5697 | * |
| 5698 | * <none> core.register_task(<function>) |
| 5699 | */ |
| 5700 | static int hlua_register_task(lua_State *L) |
| 5701 | { |
| 5702 | struct hlua *hlua; |
| 5703 | struct task *task; |
| 5704 | int ref; |
| 5705 | |
| 5706 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5707 | |
| 5708 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5709 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5710 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5711 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5712 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5713 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 5714 | task = task_new(MAX_THREADS_MASK); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5715 | task->context = hlua; |
| 5716 | task->process = hlua_process_task; |
| 5717 | |
| 5718 | if (!hlua_ctx_init(hlua, task)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5719 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5720 | |
| 5721 | /* Restore the function in the stack. */ |
| 5722 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5723 | hlua->nargs = 0; |
| 5724 | |
| 5725 | /* Schedule task. */ |
| 5726 | task_schedule(task, now_ms); |
| 5727 | |
| 5728 | return 0; |
| 5729 | } |
| 5730 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5731 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5732 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5733 | * resume converters. |
| 5734 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5735 | 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] | 5736 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5737 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5738 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5739 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5740 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5741 | if (!stream) |
| 5742 | return 0; |
| 5743 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5744 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5745 | * Lua context can be not initialized. This behavior |
| 5746 | * permits to save performances because a systematic |
| 5747 | * Lua initialization cause 5% performances loss. |
| 5748 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5749 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5750 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5751 | if (!stream->hlua) { |
| 5752 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5753 | return 0; |
| 5754 | } |
| 5755 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5756 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5757 | return 0; |
| 5758 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5759 | } |
| 5760 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5761 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5762 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5763 | |
| 5764 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5765 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5766 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5767 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5768 | else |
| 5769 | error = "critical error"; |
| 5770 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5771 | return 0; |
| 5772 | } |
| 5773 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5774 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5775 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5776 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5777 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5778 | return 0; |
| 5779 | } |
| 5780 | |
| 5781 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5782 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5783 | |
| 5784 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5785 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5786 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5787 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5788 | return 0; |
| 5789 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5790 | hlua_smp2lua(stream->hlua->T, smp); |
| 5791 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5792 | |
| 5793 | /* push keywords in the stack. */ |
| 5794 | if (arg_p) { |
| 5795 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5796 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5797 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5798 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5799 | return 0; |
| 5800 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5801 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5802 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5803 | } |
| 5804 | } |
| 5805 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5806 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5807 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5808 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5809 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5810 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5811 | } |
| 5812 | |
| 5813 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5814 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5815 | /* finished. */ |
| 5816 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5817 | /* If the stack is empty, the function fails. */ |
| 5818 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5819 | return 0; |
| 5820 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5821 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5822 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5823 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5824 | return 1; |
| 5825 | |
| 5826 | /* yield. */ |
| 5827 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5828 | 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] | 5829 | return 0; |
| 5830 | |
| 5831 | /* finished with error. */ |
| 5832 | case HLUA_E_ERRMSG: |
| 5833 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5834 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5835 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5836 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5837 | return 0; |
| 5838 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5839 | case HLUA_E_ETMOUT: |
| 5840 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 5841 | return 0; |
| 5842 | |
| 5843 | case HLUA_E_NOMEM: |
| 5844 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 5845 | return 0; |
| 5846 | |
| 5847 | case HLUA_E_YIELD: |
| 5848 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 5849 | return 0; |
| 5850 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5851 | case HLUA_E_ERR: |
| 5852 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5853 | 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] | 5854 | |
| 5855 | default: |
| 5856 | return 0; |
| 5857 | } |
| 5858 | } |
| 5859 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5860 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5861 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5862 | * resume sample-fetches. This function will be called by the sample |
| 5863 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5864 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5865 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5866 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5867 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5868 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5869 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5870 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5871 | const struct buffer msg = { }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5872 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5873 | if (!stream) |
| 5874 | return 0; |
| 5875 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5876 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5877 | * Lua context can be not initialized. This behavior |
| 5878 | * permits to save performances because a systematic |
| 5879 | * Lua initialization cause 5% performances loss. |
| 5880 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5881 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5882 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5883 | if (!stream->hlua) { |
| 5884 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5885 | return 0; |
| 5886 | } |
| 5887 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5888 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5889 | return 0; |
| 5890 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5891 | } |
| 5892 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5893 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5894 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5895 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5896 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5897 | |
| 5898 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5899 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5900 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5901 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5902 | else |
| 5903 | error = "critical error"; |
| 5904 | 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] | 5905 | return 0; |
| 5906 | } |
| 5907 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5908 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5909 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5910 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5911 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5912 | return 0; |
| 5913 | } |
| 5914 | |
| 5915 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5916 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5917 | |
| 5918 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5919 | 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] | 5920 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5921 | 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] | 5922 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5923 | return 0; |
| 5924 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5925 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5926 | |
| 5927 | /* push keywords in the stack. */ |
| 5928 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5929 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5930 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5931 | 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] | 5932 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5933 | return 0; |
| 5934 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5935 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5936 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5937 | } |
| 5938 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5939 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5940 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5941 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5942 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5943 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5944 | } |
| 5945 | |
| 5946 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5947 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5948 | /* finished. */ |
| 5949 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5950 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5951 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5952 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5953 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5954 | /* If the stack is empty, the function fails. */ |
| 5955 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5956 | return 0; |
| 5957 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5958 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5959 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5960 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5961 | |
| 5962 | /* Set the end of execution flag. */ |
| 5963 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5964 | return 1; |
| 5965 | |
| 5966 | /* yield. */ |
| 5967 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5968 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5969 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5970 | 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] | 5971 | return 0; |
| 5972 | |
| 5973 | /* finished with error. */ |
| 5974 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5975 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5976 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5977 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5978 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5979 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5980 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5981 | return 0; |
| 5982 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5983 | case HLUA_E_ETMOUT: |
| 5984 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 5985 | stream_int_retnclose(&stream->si[0], &msg); |
| 5986 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 5987 | return 0; |
| 5988 | |
| 5989 | case HLUA_E_NOMEM: |
| 5990 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 5991 | stream_int_retnclose(&stream->si[0], &msg); |
| 5992 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 5993 | return 0; |
| 5994 | |
| 5995 | case HLUA_E_YIELD: |
| 5996 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
| 5997 | stream_int_retnclose(&stream->si[0], &msg); |
| 5998 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 5999 | return 0; |
| 6000 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6001 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6002 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6003 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6004 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6005 | 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] | 6006 | |
| 6007 | default: |
| 6008 | return 0; |
| 6009 | } |
| 6010 | } |
| 6011 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6012 | /* This function is an LUA binding used for registering |
| 6013 | * "sample-conv" functions. It expects a converter name used |
| 6014 | * in the haproxy configuration file, and an LUA function. |
| 6015 | */ |
| 6016 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6017 | { |
| 6018 | struct sample_conv_kw_list *sck; |
| 6019 | const char *name; |
| 6020 | int ref; |
| 6021 | int len; |
| 6022 | struct hlua_function *fcn; |
| 6023 | |
| 6024 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6025 | |
| 6026 | /* First argument : converter name. */ |
| 6027 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6028 | |
| 6029 | /* Second argument : lua function. */ |
| 6030 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6031 | |
| 6032 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6033 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6034 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6035 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6036 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6037 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6038 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6039 | |
| 6040 | /* Fill fcn. */ |
| 6041 | fcn->name = strdup(name); |
| 6042 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6043 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6044 | fcn->function_ref = ref; |
| 6045 | |
| 6046 | /* List head */ |
| 6047 | sck->list.n = sck->list.p = NULL; |
| 6048 | |
| 6049 | /* converter keyword. */ |
| 6050 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6051 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6052 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6053 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6054 | |
| 6055 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6056 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6057 | 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] | 6058 | sck->kw[0].val_args = NULL; |
| 6059 | sck->kw[0].in_type = SMP_T_STR; |
| 6060 | sck->kw[0].out_type = SMP_T_STR; |
| 6061 | sck->kw[0].private = fcn; |
| 6062 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6063 | /* Register this new converter */ |
| 6064 | sample_register_convs(sck); |
| 6065 | |
| 6066 | return 0; |
| 6067 | } |
| 6068 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6069 | /* This fucntion is an LUA binding used for registering |
| 6070 | * "sample-fetch" functions. It expects a converter name used |
| 6071 | * in the haproxy configuration file, and an LUA function. |
| 6072 | */ |
| 6073 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6074 | { |
| 6075 | const char *name; |
| 6076 | int ref; |
| 6077 | int len; |
| 6078 | struct sample_fetch_kw_list *sfk; |
| 6079 | struct hlua_function *fcn; |
| 6080 | |
| 6081 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6082 | |
| 6083 | /* First argument : sample-fetch name. */ |
| 6084 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6085 | |
| 6086 | /* Second argument : lua function. */ |
| 6087 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6088 | |
| 6089 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6090 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6091 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6092 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6093 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6094 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6095 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6096 | |
| 6097 | /* Fill fcn. */ |
| 6098 | fcn->name = strdup(name); |
| 6099 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6100 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6101 | fcn->function_ref = ref; |
| 6102 | |
| 6103 | /* List head */ |
| 6104 | sfk->list.n = sfk->list.p = NULL; |
| 6105 | |
| 6106 | /* sample-fetch keyword. */ |
| 6107 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6108 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6109 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6110 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6111 | |
| 6112 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6113 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6114 | 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] | 6115 | sfk->kw[0].val_args = NULL; |
| 6116 | sfk->kw[0].out_type = SMP_T_STR; |
| 6117 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6118 | sfk->kw[0].val = 0; |
| 6119 | sfk->kw[0].private = fcn; |
| 6120 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6121 | /* Register this new fetch. */ |
| 6122 | sample_register_fetches(sfk); |
| 6123 | |
| 6124 | return 0; |
| 6125 | } |
| 6126 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6127 | /* This function is a wrapper to execute each LUA function declared |
| 6128 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6129 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6130 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6131 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6132 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6133 | 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] | 6134 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6135 | { |
| 6136 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6137 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6138 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6139 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6140 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6141 | |
| 6142 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 6143 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 6144 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 6145 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 6146 | 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] | 6147 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6148 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6149 | return ACT_RET_CONT; |
| 6150 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6151 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6152 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6153 | * Lua context can be not initialized. This behavior |
| 6154 | * permits to save performances because a systematic |
| 6155 | * Lua initialization cause 5% performances loss. |
| 6156 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6157 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6158 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6159 | if (!s->hlua) { |
| 6160 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6161 | rule->arg.hlua_rule->fcn.name); |
| 6162 | return ACT_RET_CONT; |
| 6163 | } |
| 6164 | if (!hlua_ctx_init(s->hlua, s->task)) { |
| 6165 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6166 | rule->arg.hlua_rule->fcn.name); |
| 6167 | return ACT_RET_CONT; |
| 6168 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6169 | } |
| 6170 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6171 | consistency_set(s, dir, &s->hlua->cons); |
| 6172 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6173 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6174 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6175 | |
| 6176 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6177 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6178 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6179 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6180 | else |
| 6181 | error = "critical error"; |
| 6182 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6183 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6184 | return ACT_RET_CONT; |
| 6185 | } |
| 6186 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6187 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6188 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6189 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6190 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6191 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6192 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6193 | } |
| 6194 | |
| 6195 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6196 | 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] | 6197 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6198 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6199 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6200 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6201 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6202 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6203 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6204 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6205 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6206 | |
| 6207 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6208 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6209 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6210 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6211 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6212 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6213 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6214 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6215 | lua_pushstring(s->hlua->T, *arg); |
| 6216 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6217 | } |
| 6218 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6219 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6220 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6221 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6222 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6223 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6224 | } |
| 6225 | |
| 6226 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6227 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6228 | /* finished. */ |
| 6229 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6230 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6231 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6232 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6233 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6234 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6235 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6236 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6237 | |
| 6238 | /* yield. */ |
| 6239 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6240 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6241 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6242 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6243 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6244 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6245 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6246 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6247 | /* Some actions can be wake up when a "write" event |
| 6248 | * is detected on a response channel. This is useful |
| 6249 | * only for actions targetted on the requests. |
| 6250 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6251 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6252 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6253 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6254 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6255 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6256 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6257 | s->req.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6258 | /* We can quit the fcuntion without consistency check |
| 6259 | * because HAProxy is not able to manipulate data, it |
| 6260 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6261 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6262 | |
| 6263 | /* finished with error. */ |
| 6264 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6265 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6266 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6267 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6268 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6269 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6270 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6271 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6272 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6273 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6274 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6275 | case HLUA_E_ETMOUT: |
| 6276 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6277 | stream_int_retnclose(&s->si[0], &msg); |
| 6278 | return ACT_RET_ERR; |
| 6279 | } |
| 6280 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6281 | return 0; |
| 6282 | |
| 6283 | case HLUA_E_NOMEM: |
| 6284 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6285 | stream_int_retnclose(&s->si[0], &msg); |
| 6286 | return ACT_RET_ERR; |
| 6287 | } |
| 6288 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6289 | return 0; |
| 6290 | |
| 6291 | case HLUA_E_YIELD: |
| 6292 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
| 6293 | stream_int_retnclose(&s->si[0], &msg); |
| 6294 | return ACT_RET_ERR; |
| 6295 | } |
| 6296 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6297 | rule->arg.hlua_rule->fcn.name); |
| 6298 | return 0; |
| 6299 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6300 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6301 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6302 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6303 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6304 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6305 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6306 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6307 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6308 | |
| 6309 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6310 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6311 | } |
| 6312 | } |
| 6313 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6314 | struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6315 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6316 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6317 | struct stream_interface *si = ctx->owner; |
| 6318 | |
| 6319 | /* If the applet is wake up without any expected work, the sheduler |
| 6320 | * remove it from the run queue. This flag indicate that the applet |
| 6321 | * is waiting for write. If the buffer is full, the main processing |
| 6322 | * will send some data and after call the applet, otherwise it call |
| 6323 | * the applet ASAP. |
| 6324 | */ |
| 6325 | si_applet_cant_put(si); |
| 6326 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6327 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6328 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6329 | } |
| 6330 | |
| 6331 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6332 | { |
| 6333 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6334 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6335 | struct task *task; |
| 6336 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6337 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6338 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6339 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6340 | if (!hlua) { |
| 6341 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6342 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6343 | return 0; |
| 6344 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6345 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6346 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6347 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6348 | |
| 6349 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6350 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6351 | if (!task) { |
| 6352 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6353 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6354 | return 0; |
| 6355 | } |
| 6356 | task->nice = 0; |
| 6357 | task->context = ctx; |
| 6358 | task->process = hlua_applet_wakeup; |
| 6359 | ctx->ctx.hlua_apptcp.task = task; |
| 6360 | |
| 6361 | /* In the execution wrappers linked with a stream, the |
| 6362 | * Lua context can be not initialized. This behavior |
| 6363 | * permits to save performances because a systematic |
| 6364 | * Lua initialization cause 5% performances loss. |
| 6365 | */ |
| 6366 | if (!hlua_ctx_init(hlua, task)) { |
| 6367 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6368 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6369 | return 0; |
| 6370 | } |
| 6371 | |
| 6372 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6373 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6374 | |
| 6375 | /* The following Lua calls can fail. */ |
| 6376 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6377 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6378 | error = lua_tostring(hlua->T, -1); |
| 6379 | else |
| 6380 | error = "critical error"; |
| 6381 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6382 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6383 | RESET_SAFE_LJMP(hlua->T); |
| 6384 | return 0; |
| 6385 | } |
| 6386 | |
| 6387 | /* Check stack available size. */ |
| 6388 | if (!lua_checkstack(hlua->T, 1)) { |
| 6389 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6390 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6391 | RESET_SAFE_LJMP(hlua->T); |
| 6392 | return 0; |
| 6393 | } |
| 6394 | |
| 6395 | /* Restore the function in the stack. */ |
| 6396 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6397 | |
| 6398 | /* Create and and push object stream in the stack. */ |
| 6399 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6400 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6401 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6402 | RESET_SAFE_LJMP(hlua->T); |
| 6403 | return 0; |
| 6404 | } |
| 6405 | hlua->nargs = 1; |
| 6406 | |
| 6407 | /* push keywords in the stack. */ |
| 6408 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6409 | if (!lua_checkstack(hlua->T, 1)) { |
| 6410 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6411 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6412 | RESET_SAFE_LJMP(hlua->T); |
| 6413 | return 0; |
| 6414 | } |
| 6415 | lua_pushstring(hlua->T, *arg); |
| 6416 | hlua->nargs++; |
| 6417 | } |
| 6418 | |
| 6419 | RESET_SAFE_LJMP(hlua->T); |
| 6420 | |
| 6421 | /* Wakeup the applet ASAP. */ |
| 6422 | si_applet_cant_get(si); |
| 6423 | si_applet_cant_put(si); |
| 6424 | |
| 6425 | return 1; |
| 6426 | } |
| 6427 | |
| 6428 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6429 | { |
| 6430 | struct stream_interface *si = ctx->owner; |
| 6431 | struct stream *strm = si_strm(si); |
| 6432 | struct channel *res = si_ic(si); |
| 6433 | struct act_rule *rule = ctx->rule; |
| 6434 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6435 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6436 | |
| 6437 | /* The applet execution is already done. */ |
| 6438 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) |
| 6439 | return; |
| 6440 | |
| 6441 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6442 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6443 | return; |
| 6444 | |
| 6445 | /* Execute the function. */ |
| 6446 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6447 | /* finished. */ |
| 6448 | case HLUA_E_OK: |
| 6449 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6450 | |
| 6451 | /* log time */ |
| 6452 | strm->logs.tv_request = now; |
| 6453 | |
| 6454 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6455 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6456 | res->flags |= CF_READ_NULL; |
| 6457 | si_shutr(si); |
| 6458 | return; |
| 6459 | |
| 6460 | /* yield. */ |
| 6461 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6462 | if (hlua->wake_time != TICK_ETERNITY) |
| 6463 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6464 | return; |
| 6465 | |
| 6466 | /* finished with error. */ |
| 6467 | case HLUA_E_ERRMSG: |
| 6468 | /* Display log. */ |
| 6469 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6470 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6471 | lua_pop(hlua->T, 1); |
| 6472 | goto error; |
| 6473 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6474 | case HLUA_E_ETMOUT: |
| 6475 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6476 | rule->arg.hlua_rule->fcn.name); |
| 6477 | goto error; |
| 6478 | |
| 6479 | case HLUA_E_NOMEM: |
| 6480 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6481 | rule->arg.hlua_rule->fcn.name); |
| 6482 | goto error; |
| 6483 | |
| 6484 | case HLUA_E_YIELD: /* unexpected */ |
| 6485 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6486 | rule->arg.hlua_rule->fcn.name); |
| 6487 | goto error; |
| 6488 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6489 | case HLUA_E_ERR: |
| 6490 | /* Display log. */ |
| 6491 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6492 | rule->arg.hlua_rule->fcn.name); |
| 6493 | goto error; |
| 6494 | |
| 6495 | default: |
| 6496 | goto error; |
| 6497 | } |
| 6498 | |
| 6499 | error: |
| 6500 | |
| 6501 | /* For all other cases, just close the stream. */ |
| 6502 | si_shutw(si); |
| 6503 | si_shutr(si); |
| 6504 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6505 | } |
| 6506 | |
| 6507 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6508 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6509 | task_delete(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6510 | task_free(ctx->ctx.hlua_apptcp.task); |
| 6511 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6512 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6513 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6514 | } |
| 6515 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6516 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6517 | * an errors occurs and -1 if more data are required for initializing |
| 6518 | * the applet. |
| 6519 | */ |
| 6520 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6521 | { |
| 6522 | struct stream_interface *si = ctx->owner; |
| 6523 | struct channel *req = si_oc(si); |
| 6524 | struct http_msg *msg; |
| 6525 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6526 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6527 | char **arg; |
| 6528 | struct hdr_ctx hdr; |
| 6529 | struct task *task; |
| 6530 | 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] | 6531 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6532 | |
| 6533 | /* Wait for a full HTTP request. */ |
| 6534 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 6535 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 6536 | return -1; |
| 6537 | return 0; |
| 6538 | } |
| 6539 | txn = strm->txn; |
| 6540 | msg = &txn->req; |
| 6541 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6542 | /* We want two things in HTTP mode : |
| 6543 | * - enforce server-close mode if we were in keep-alive, so that the |
| 6544 | * applet is released after each response ; |
| 6545 | * - enable request body transfer to the applet in order to resync |
| 6546 | * with the response body. |
| 6547 | */ |
| 6548 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 6549 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6550 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6551 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6552 | if (!hlua) { |
| 6553 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6554 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6555 | return 0; |
| 6556 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6557 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6558 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6559 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6560 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6561 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6562 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6563 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6564 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6565 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6566 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6567 | if (!task) { |
| 6568 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6569 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6570 | return 0; |
| 6571 | } |
| 6572 | task->nice = 0; |
| 6573 | task->context = ctx; |
| 6574 | task->process = hlua_applet_wakeup; |
| 6575 | ctx->ctx.hlua_apphttp.task = task; |
| 6576 | |
| 6577 | /* In the execution wrappers linked with a stream, the |
| 6578 | * Lua context can be not initialized. This behavior |
| 6579 | * permits to save performances because a systematic |
| 6580 | * Lua initialization cause 5% performances loss. |
| 6581 | */ |
| 6582 | if (!hlua_ctx_init(hlua, task)) { |
| 6583 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6584 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6585 | return 0; |
| 6586 | } |
| 6587 | |
| 6588 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6589 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6590 | |
| 6591 | /* The following Lua calls can fail. */ |
| 6592 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6593 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6594 | error = lua_tostring(hlua->T, -1); |
| 6595 | else |
| 6596 | error = "critical error"; |
| 6597 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6598 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6599 | return 0; |
| 6600 | } |
| 6601 | |
| 6602 | /* Check stack available size. */ |
| 6603 | if (!lua_checkstack(hlua->T, 1)) { |
| 6604 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6605 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6606 | RESET_SAFE_LJMP(hlua->T); |
| 6607 | return 0; |
| 6608 | } |
| 6609 | |
| 6610 | /* Restore the function in the stack. */ |
| 6611 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6612 | |
| 6613 | /* Create and and push object stream in the stack. */ |
| 6614 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6615 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6616 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6617 | RESET_SAFE_LJMP(hlua->T); |
| 6618 | return 0; |
| 6619 | } |
| 6620 | hlua->nargs = 1; |
| 6621 | |
| 6622 | /* Look for a 100-continue expected. */ |
| 6623 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 6624 | hdr.idx = 0; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6625 | if (http_find_header2("Expect", 6, ci_head(req), &txn->hdr_idx, &hdr) && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6626 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 6627 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 6628 | } |
| 6629 | |
| 6630 | /* push keywords in the stack. */ |
| 6631 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6632 | if (!lua_checkstack(hlua->T, 1)) { |
| 6633 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6634 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6635 | RESET_SAFE_LJMP(hlua->T); |
| 6636 | return 0; |
| 6637 | } |
| 6638 | lua_pushstring(hlua->T, *arg); |
| 6639 | hlua->nargs++; |
| 6640 | } |
| 6641 | |
| 6642 | RESET_SAFE_LJMP(hlua->T); |
| 6643 | |
| 6644 | /* Wakeup the applet when data is ready for read. */ |
| 6645 | si_applet_cant_get(si); |
| 6646 | |
| 6647 | return 1; |
| 6648 | } |
| 6649 | |
| 6650 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 6651 | { |
| 6652 | struct stream_interface *si = ctx->owner; |
| 6653 | struct stream *strm = si_strm(si); |
| 6654 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6655 | struct act_rule *rule = ctx->rule; |
| 6656 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6657 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 6658 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 6659 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 6660 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 6661 | size_t len2; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6662 | int ret; |
| 6663 | |
| 6664 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6665 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6666 | return; |
| 6667 | |
| 6668 | /* Set the currently running flag. */ |
| 6669 | if (!HLUA_IS_RUNNING(hlua) && |
| 6670 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6671 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6672 | /* Wait for full HTTP analysys. */ |
| 6673 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 6674 | si_applet_cant_get(si); |
| 6675 | return; |
| 6676 | } |
| 6677 | |
| 6678 | /* Store the max amount of bytes that we can read. */ |
| 6679 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 6680 | |
| 6681 | /* We need to flush the request header. This left the body |
| 6682 | * for the Lua. |
| 6683 | */ |
| 6684 | |
| 6685 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6686 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6687 | if (ret == -1) |
| 6688 | return; |
| 6689 | |
| 6690 | /* No data available, ask for more data. */ |
| 6691 | if (ret == 1) |
| 6692 | len2 = 0; |
| 6693 | if (ret == 0) |
| 6694 | len1 = 0; |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 6695 | if (len1 + len2 < strm->txn->req.eoh + strm->txn->req.eol) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6696 | si_applet_cant_get(si); |
| 6697 | return; |
| 6698 | } |
| 6699 | |
| 6700 | /* skip the requests bytes. */ |
Thierry FOURNIER | 70d318c | 2018-06-30 10:37:33 +0200 | [diff] [blame] | 6701 | co_skip(si_oc(si), strm->txn->req.eoh + strm->txn->req.eol); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6702 | } |
| 6703 | |
| 6704 | /* Executes The applet if it is not done. */ |
| 6705 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6706 | |
| 6707 | /* Execute the function. */ |
| 6708 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6709 | /* finished. */ |
| 6710 | case HLUA_E_OK: |
| 6711 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6712 | break; |
| 6713 | |
| 6714 | /* yield. */ |
| 6715 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6716 | if (hlua->wake_time != TICK_ETERNITY) |
| 6717 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6718 | return; |
| 6719 | |
| 6720 | /* finished with error. */ |
| 6721 | case HLUA_E_ERRMSG: |
| 6722 | /* Display log. */ |
| 6723 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6724 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6725 | lua_pop(hlua->T, 1); |
| 6726 | goto error; |
| 6727 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6728 | case HLUA_E_ETMOUT: |
| 6729 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 6730 | rule->arg.hlua_rule->fcn.name); |
| 6731 | goto error; |
| 6732 | |
| 6733 | case HLUA_E_NOMEM: |
| 6734 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 6735 | rule->arg.hlua_rule->fcn.name); |
| 6736 | goto error; |
| 6737 | |
| 6738 | case HLUA_E_YIELD: /* unexpected */ |
| 6739 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 6740 | rule->arg.hlua_rule->fcn.name); |
| 6741 | goto error; |
| 6742 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6743 | case HLUA_E_ERR: |
| 6744 | /* Display log. */ |
| 6745 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6746 | rule->arg.hlua_rule->fcn.name); |
| 6747 | goto error; |
| 6748 | |
| 6749 | default: |
| 6750 | goto error; |
| 6751 | } |
| 6752 | } |
| 6753 | |
| 6754 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 6755 | |
| 6756 | /* We must send the final chunk. */ |
| 6757 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 6758 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 6759 | |
| 6760 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6761 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6762 | |
| 6763 | /* critical error. */ |
| 6764 | if (ret == -2 || ret == -3) { |
| 6765 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 6766 | rule->arg.hlua_rule->fcn.name); |
| 6767 | goto error; |
| 6768 | } |
| 6769 | |
| 6770 | /* no enough space error. */ |
| 6771 | if (ret == -1) { |
| 6772 | si_applet_cant_put(si); |
| 6773 | return; |
| 6774 | } |
| 6775 | |
| 6776 | /* set the last chunk sent. */ |
| 6777 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6778 | } |
| 6779 | |
| 6780 | /* close the connection. */ |
| 6781 | |
| 6782 | /* status / log */ |
| 6783 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6784 | strm->logs.tv_request = now; |
| 6785 | |
| 6786 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6787 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6788 | res->flags |= CF_READ_NULL; |
| 6789 | si_shutr(si); |
| 6790 | |
| 6791 | return; |
| 6792 | } |
| 6793 | |
| 6794 | error: |
| 6795 | |
| 6796 | /* If we are in HTTP mode, and we are not send any |
| 6797 | * data, return a 500 server error in best effort: |
| 6798 | * if there are no room avalaible in the buffer, |
| 6799 | * just close the connection. |
| 6800 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6801 | ci_putblk(res, error_500, strlen(error_500)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6802 | if (!(strm->flags & SF_ERR_MASK)) |
| 6803 | strm->flags |= SF_ERR_RESOURCE; |
| 6804 | si_shutw(si); |
| 6805 | si_shutr(si); |
| 6806 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6807 | } |
| 6808 | |
| 6809 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6810 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6811 | task_delete(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6812 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6813 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6814 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6815 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6816 | } |
| 6817 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6818 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6819 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6820 | * |
| 6821 | * This function can fail with an abort() due to an Lua critical error. |
| 6822 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6823 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6824 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6825 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6826 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6827 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6828 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6829 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6830 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6831 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6832 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6833 | if (!rule->arg.hlua_rule) { |
| 6834 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6835 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6836 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6837 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6838 | /* Memory for arguments. */ |
| 6839 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6840 | if (!rule->arg.hlua_rule->args) { |
| 6841 | memprintf(err, "out of memory error"); |
| 6842 | return ACT_RET_PRS_ERR; |
| 6843 | } |
| 6844 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6845 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6846 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6847 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6848 | /* Expect some arguments */ |
| 6849 | for (i = 0; i < fcn->nargs; i++) { |
| 6850 | if (*args[i+1] == '\0') { |
| 6851 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6852 | return ACT_RET_PRS_ERR; |
| 6853 | } |
| 6854 | rule->arg.hlua_rule->args[i] = strdup(args[i + 1]); |
| 6855 | if (!rule->arg.hlua_rule->args[i]) { |
| 6856 | memprintf(err, "out of memory error"); |
| 6857 | return ACT_RET_PRS_ERR; |
| 6858 | } |
| 6859 | (*cur_arg)++; |
| 6860 | } |
| 6861 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6862 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6863 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6864 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6865 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6866 | } |
| 6867 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6868 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6869 | struct act_rule *rule, char **err) |
| 6870 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6871 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6872 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6873 | /* HTTP applets are forbidden in tcp-request rules. |
| 6874 | * HTTP applet request requires everything initilized by |
| 6875 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6876 | * The applet will be immediately initilized, but its before |
| 6877 | * the call of this analyzer. |
| 6878 | */ |
| 6879 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6880 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6881 | return ACT_RET_PRS_ERR; |
| 6882 | } |
| 6883 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6884 | /* Memory for the rule. */ |
| 6885 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6886 | if (!rule->arg.hlua_rule) { |
| 6887 | memprintf(err, "out of memory error"); |
| 6888 | return ACT_RET_PRS_ERR; |
| 6889 | } |
| 6890 | |
| 6891 | /* Reference the Lua function and store the reference. */ |
| 6892 | rule->arg.hlua_rule->fcn = *fcn; |
| 6893 | |
| 6894 | /* TODO: later accept arguments. */ |
| 6895 | rule->arg.hlua_rule->args = NULL; |
| 6896 | |
| 6897 | /* Add applet pointer in the rule. */ |
| 6898 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6899 | rule->applet.name = fcn->name; |
| 6900 | rule->applet.init = hlua_applet_http_init; |
| 6901 | rule->applet.fct = hlua_applet_http_fct; |
| 6902 | rule->applet.release = hlua_applet_http_release; |
| 6903 | rule->applet.timeout = hlua_timeout_applet; |
| 6904 | |
| 6905 | return ACT_RET_PRS_OK; |
| 6906 | } |
| 6907 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6908 | /* This function is an LUA binding used for registering |
| 6909 | * "sample-conv" functions. It expects a converter name used |
| 6910 | * in the haproxy configuration file, and an LUA function. |
| 6911 | */ |
| 6912 | __LJMP static int hlua_register_action(lua_State *L) |
| 6913 | { |
| 6914 | struct action_kw_list *akl; |
| 6915 | const char *name; |
| 6916 | int ref; |
| 6917 | int len; |
| 6918 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6919 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6920 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6921 | /* Initialise the number of expected arguments at 0. */ |
| 6922 | nargs = 0; |
| 6923 | |
| 6924 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6925 | 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] | 6926 | |
| 6927 | /* First argument : converter name. */ |
| 6928 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6929 | |
| 6930 | /* Second argument : environment. */ |
| 6931 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6932 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6933 | |
| 6934 | /* Third argument : lua function. */ |
| 6935 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6936 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6937 | /* Fouth argument : number of mandatories arguments expected on the configuration line. */ |
| 6938 | if (lua_gettop(L) >= 4) |
| 6939 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6940 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6941 | /* browse the second argulent as an array. */ |
| 6942 | lua_pushnil(L); |
| 6943 | while (lua_next(L, 2) != 0) { |
| 6944 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6945 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6946 | |
| 6947 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6948 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6949 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6950 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6951 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6952 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6953 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6954 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6955 | |
| 6956 | /* Fill fcn. */ |
| 6957 | fcn->name = strdup(name); |
| 6958 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6959 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6960 | fcn->function_ref = ref; |
| 6961 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6962 | /* Set the expected number od arguments. */ |
| 6963 | fcn->nargs = nargs; |
| 6964 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6965 | /* List head */ |
| 6966 | akl->list.n = akl->list.p = NULL; |
| 6967 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6968 | /* action keyword. */ |
| 6969 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6970 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6971 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6972 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6973 | |
| 6974 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6975 | |
| 6976 | akl->kw[0].match_pfx = 0; |
| 6977 | akl->kw[0].private = fcn; |
| 6978 | akl->kw[0].parse = action_register_lua; |
| 6979 | |
| 6980 | /* select the action registering point. */ |
| 6981 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6982 | tcp_req_cont_keywords_register(akl); |
| 6983 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6984 | tcp_res_cont_keywords_register(akl); |
| 6985 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6986 | http_req_keywords_register(akl); |
| 6987 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6988 | http_res_keywords_register(akl); |
| 6989 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6990 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6991 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6992 | "are expected.", lua_tostring(L, -1))); |
| 6993 | |
| 6994 | /* pop the environment string. */ |
| 6995 | lua_pop(L, 1); |
| 6996 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6997 | return ACT_RET_PRS_OK; |
| 6998 | } |
| 6999 | |
| 7000 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7001 | struct act_rule *rule, char **err) |
| 7002 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7003 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7004 | |
| 7005 | /* Memory for the rule. */ |
| 7006 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7007 | if (!rule->arg.hlua_rule) { |
| 7008 | memprintf(err, "out of memory error"); |
| 7009 | return ACT_RET_PRS_ERR; |
| 7010 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7011 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7012 | /* Reference the Lua function and store the reference. */ |
| 7013 | rule->arg.hlua_rule->fcn = *fcn; |
| 7014 | |
| 7015 | /* TODO: later accept arguments. */ |
| 7016 | rule->arg.hlua_rule->args = NULL; |
| 7017 | |
| 7018 | /* Add applet pointer in the rule. */ |
| 7019 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7020 | rule->applet.name = fcn->name; |
| 7021 | rule->applet.init = hlua_applet_tcp_init; |
| 7022 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7023 | rule->applet.release = hlua_applet_tcp_release; |
| 7024 | rule->applet.timeout = hlua_timeout_applet; |
| 7025 | |
| 7026 | return 0; |
| 7027 | } |
| 7028 | |
| 7029 | /* This function is an LUA binding used for registering |
| 7030 | * "sample-conv" functions. It expects a converter name used |
| 7031 | * in the haproxy configuration file, and an LUA function. |
| 7032 | */ |
| 7033 | __LJMP static int hlua_register_service(lua_State *L) |
| 7034 | { |
| 7035 | struct action_kw_list *akl; |
| 7036 | const char *name; |
| 7037 | const char *env; |
| 7038 | int ref; |
| 7039 | int len; |
| 7040 | struct hlua_function *fcn; |
| 7041 | |
| 7042 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7043 | |
| 7044 | /* First argument : converter name. */ |
| 7045 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7046 | |
| 7047 | /* Second argument : environment. */ |
| 7048 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7049 | |
| 7050 | /* Third argument : lua function. */ |
| 7051 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7052 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7053 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7054 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7055 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7056 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7057 | fcn = calloc(1, sizeof(*fcn)); |
| 7058 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7059 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7060 | |
| 7061 | /* Fill fcn. */ |
| 7062 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7063 | fcn->name = calloc(1, len); |
| 7064 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7065 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7066 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7067 | fcn->function_ref = ref; |
| 7068 | |
| 7069 | /* List head */ |
| 7070 | akl->list.n = akl->list.p = NULL; |
| 7071 | |
| 7072 | /* converter keyword. */ |
| 7073 | len = strlen("lua.") + strlen(name) + 1; |
| 7074 | akl->kw[0].kw = calloc(1, len); |
| 7075 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7076 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7077 | |
| 7078 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7079 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7080 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7081 | if (strcmp(env, "tcp") == 0) |
| 7082 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7083 | else if (strcmp(env, "http") == 0) |
| 7084 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7085 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7086 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7087 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7088 | |
| 7089 | akl->kw[0].match_pfx = 0; |
| 7090 | akl->kw[0].private = fcn; |
| 7091 | |
| 7092 | /* End of array. */ |
| 7093 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7094 | |
| 7095 | /* Register this new converter */ |
| 7096 | service_keywords_register(akl); |
| 7097 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7098 | return 0; |
| 7099 | } |
| 7100 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7101 | /* This function initialises Lua cli handler. It copies the |
| 7102 | * arguments in the Lua stack and create channel IO objects. |
| 7103 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7104 | 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] | 7105 | { |
| 7106 | struct hlua *hlua; |
| 7107 | struct hlua_function *fcn; |
| 7108 | int i; |
| 7109 | const char *error; |
| 7110 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7111 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7112 | appctx->ctx.hlua_cli.fcn = private; |
| 7113 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7114 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7115 | if (!hlua) { |
| 7116 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7117 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7118 | } |
| 7119 | HLUA_INIT(hlua); |
| 7120 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7121 | |
| 7122 | /* Create task used by signal to wakeup applets. |
| 7123 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7124 | * applet_http. It is absolutely compatible. |
| 7125 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7126 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7127 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7128 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7129 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7130 | } |
| 7131 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7132 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7133 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7134 | |
| 7135 | /* Initialises the Lua context */ |
| 7136 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 7137 | 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] | 7138 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7139 | } |
| 7140 | |
| 7141 | /* The following Lua calls can fail. */ |
| 7142 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7143 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7144 | error = lua_tostring(hlua->T, -1); |
| 7145 | else |
| 7146 | error = "critical error"; |
| 7147 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7148 | goto error; |
| 7149 | } |
| 7150 | |
| 7151 | /* Check stack available size. */ |
| 7152 | if (!lua_checkstack(hlua->T, 2)) { |
| 7153 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7154 | goto error; |
| 7155 | } |
| 7156 | |
| 7157 | /* Restore the function in the stack. */ |
| 7158 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7159 | |
| 7160 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7161 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7162 | */ |
| 7163 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7164 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7165 | goto error; |
| 7166 | } |
| 7167 | hlua->nargs = 1; |
| 7168 | |
| 7169 | /* push keywords in the stack. */ |
| 7170 | for (i = 0; *args[i]; i++) { |
| 7171 | /* Check stack available size. */ |
| 7172 | if (!lua_checkstack(hlua->T, 1)) { |
| 7173 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7174 | goto error; |
| 7175 | } |
| 7176 | lua_pushstring(hlua->T, args[i]); |
| 7177 | hlua->nargs++; |
| 7178 | } |
| 7179 | |
| 7180 | /* We must initialize the execution timeouts. */ |
| 7181 | hlua->max_time = hlua_timeout_session; |
| 7182 | |
| 7183 | /* At this point the execution is safe. */ |
| 7184 | RESET_SAFE_LJMP(hlua->T); |
| 7185 | |
| 7186 | /* It's ok */ |
| 7187 | return 0; |
| 7188 | |
| 7189 | /* It's not ok. */ |
| 7190 | error: |
| 7191 | RESET_SAFE_LJMP(hlua->T); |
| 7192 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7193 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7194 | return 1; |
| 7195 | } |
| 7196 | |
| 7197 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7198 | { |
| 7199 | struct hlua *hlua; |
| 7200 | struct stream_interface *si; |
| 7201 | struct hlua_function *fcn; |
| 7202 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7203 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7204 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7205 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7206 | |
| 7207 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7208 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7209 | return 1; |
| 7210 | |
| 7211 | /* Execute the function. */ |
| 7212 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7213 | |
| 7214 | /* finished. */ |
| 7215 | case HLUA_E_OK: |
| 7216 | return 1; |
| 7217 | |
| 7218 | /* yield. */ |
| 7219 | case HLUA_E_AGAIN: |
| 7220 | /* We want write. */ |
| 7221 | if (HLUA_IS_WAKERESWR(hlua)) |
| 7222 | si_applet_cant_put(si); |
| 7223 | /* Set the timeout. */ |
| 7224 | if (hlua->wake_time != TICK_ETERNITY) |
| 7225 | task_schedule(hlua->task, hlua->wake_time); |
| 7226 | return 0; |
| 7227 | |
| 7228 | /* finished with error. */ |
| 7229 | case HLUA_E_ERRMSG: |
| 7230 | /* Display log. */ |
| 7231 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7232 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7233 | lua_pop(hlua->T, 1); |
| 7234 | return 1; |
| 7235 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7236 | case HLUA_E_ETMOUT: |
| 7237 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7238 | fcn->name); |
| 7239 | return 1; |
| 7240 | |
| 7241 | case HLUA_E_NOMEM: |
| 7242 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7243 | fcn->name); |
| 7244 | return 1; |
| 7245 | |
| 7246 | case HLUA_E_YIELD: /* unexpected */ |
| 7247 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7248 | fcn->name); |
| 7249 | return 1; |
| 7250 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7251 | case HLUA_E_ERR: |
| 7252 | /* Display log. */ |
| 7253 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7254 | fcn->name); |
| 7255 | return 1; |
| 7256 | |
| 7257 | default: |
| 7258 | return 1; |
| 7259 | } |
| 7260 | |
| 7261 | return 1; |
| 7262 | } |
| 7263 | |
| 7264 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7265 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7266 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7267 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7268 | } |
| 7269 | |
| 7270 | /* This function is an LUA binding used for registering |
| 7271 | * new keywords in the cli. It expects a list of keywords |
| 7272 | * which are the "path". It is limited to 5 keywords. A |
| 7273 | * description of the command, a function to be executed |
| 7274 | * for the parsing and a function for io handlers. |
| 7275 | */ |
| 7276 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7277 | { |
| 7278 | struct cli_kw_list *cli_kws; |
| 7279 | const char *message; |
| 7280 | int ref_io; |
| 7281 | int len; |
| 7282 | struct hlua_function *fcn; |
| 7283 | int index; |
| 7284 | int i; |
| 7285 | |
| 7286 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7287 | |
| 7288 | /* First argument : an array of maximum 5 keywords. */ |
| 7289 | if (!lua_istable(L, 1)) |
| 7290 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7291 | |
| 7292 | /* Second argument : string with contextual message. */ |
| 7293 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7294 | |
| 7295 | /* Third and fourth argument : lua function. */ |
| 7296 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7297 | |
| 7298 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7299 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7300 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7301 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7302 | fcn = calloc(1, sizeof(*fcn)); |
| 7303 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7304 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7305 | |
| 7306 | /* Fill path. */ |
| 7307 | index = 0; |
| 7308 | lua_pushnil(L); |
| 7309 | while(lua_next(L, 1) != 0) { |
| 7310 | if (index >= 5) |
| 7311 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7312 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7313 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7314 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7315 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7316 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7317 | index++; |
| 7318 | lua_pop(L, 1); |
| 7319 | } |
| 7320 | |
| 7321 | /* Copy help message. */ |
| 7322 | cli_kws->kw[0].usage = strdup(message); |
| 7323 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7324 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7325 | |
| 7326 | /* Fill fcn io handler. */ |
| 7327 | len = strlen("<lua.cli>") + 1; |
| 7328 | for (i = 0; i < index; i++) |
| 7329 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7330 | fcn->name = calloc(1, len); |
| 7331 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7332 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7333 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7334 | for (i = 0; i < index; i++) { |
| 7335 | strncat((char *)fcn->name, ".", len); |
| 7336 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7337 | } |
| 7338 | strncat((char *)fcn->name, ">", len); |
| 7339 | fcn->function_ref = ref_io; |
| 7340 | |
| 7341 | /* Fill last entries. */ |
| 7342 | cli_kws->kw[0].private = fcn; |
| 7343 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7344 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7345 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7346 | |
| 7347 | /* Register this new converter */ |
| 7348 | cli_register_kw(cli_kws); |
| 7349 | |
| 7350 | return 0; |
| 7351 | } |
| 7352 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7353 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7354 | struct proxy *defpx, const char *file, int line, |
| 7355 | char **err, unsigned int *timeout) |
| 7356 | { |
| 7357 | const char *error; |
| 7358 | |
| 7359 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 7360 | if (error && *error != '\0') { |
| 7361 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7362 | return -1; |
| 7363 | } |
| 7364 | return 0; |
| 7365 | } |
| 7366 | |
| 7367 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7368 | struct proxy *defpx, const char *file, int line, |
| 7369 | char **err) |
| 7370 | { |
| 7371 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7372 | file, line, err, &hlua_timeout_session); |
| 7373 | } |
| 7374 | |
| 7375 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7376 | struct proxy *defpx, const char *file, int line, |
| 7377 | char **err) |
| 7378 | { |
| 7379 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7380 | file, line, err, &hlua_timeout_task); |
| 7381 | } |
| 7382 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7383 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7384 | struct proxy *defpx, const char *file, int line, |
| 7385 | char **err) |
| 7386 | { |
| 7387 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7388 | file, line, err, &hlua_timeout_applet); |
| 7389 | } |
| 7390 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7391 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7392 | struct proxy *defpx, const char *file, int line, |
| 7393 | char **err) |
| 7394 | { |
| 7395 | char *error; |
| 7396 | |
| 7397 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7398 | if (*error != '\0') { |
| 7399 | memprintf(err, "%s: invalid number", args[0]); |
| 7400 | return -1; |
| 7401 | } |
| 7402 | return 0; |
| 7403 | } |
| 7404 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7405 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7406 | struct proxy *defpx, const char *file, int line, |
| 7407 | char **err) |
| 7408 | { |
| 7409 | char *error; |
| 7410 | |
| 7411 | if (*(args[1]) == 0) { |
| 7412 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7413 | return -1; |
| 7414 | } |
| 7415 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7416 | if (*error != '\0') { |
| 7417 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7418 | return -1; |
| 7419 | } |
| 7420 | return 0; |
| 7421 | } |
| 7422 | |
| 7423 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7424 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7425 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7426 | * the main lua entry point. |
| 7427 | * |
| 7428 | * This funtion runs with the HAProxy keywords API. It returns -1 if an error is |
| 7429 | * occured, otherwise it returns 0. |
| 7430 | * |
| 7431 | * In some error case, LUA set an error message in top of the stack. This function |
| 7432 | * 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] | 7433 | * |
| 7434 | * This function can fail with an abort() due to an Lua critical error. |
| 7435 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7436 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7437 | */ |
| 7438 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7439 | struct proxy *defpx, const char *file, int line, |
| 7440 | char **err) |
| 7441 | { |
| 7442 | int error; |
| 7443 | |
| 7444 | /* Just load and compile the file. */ |
| 7445 | error = luaL_loadfile(gL.T, args[1]); |
| 7446 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7447 | 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] | 7448 | lua_pop(gL.T, 1); |
| 7449 | return -1; |
| 7450 | } |
| 7451 | |
| 7452 | /* If no syntax error where detected, execute the code. */ |
| 7453 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7454 | switch (error) { |
| 7455 | case LUA_OK: |
| 7456 | break; |
| 7457 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7458 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7459 | lua_pop(gL.T, 1); |
| 7460 | return -1; |
| 7461 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7462 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7463 | return -1; |
| 7464 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7465 | 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] | 7466 | lua_pop(gL.T, 1); |
| 7467 | return -1; |
| 7468 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7469 | 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] | 7470 | lua_pop(gL.T, 1); |
| 7471 | return -1; |
| 7472 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7473 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7474 | lua_pop(gL.T, 1); |
| 7475 | return -1; |
| 7476 | } |
| 7477 | |
| 7478 | return 0; |
| 7479 | } |
| 7480 | |
| 7481 | /* configuration keywords declaration */ |
| 7482 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7483 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7484 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7485 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7486 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7487 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7488 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7489 | { 0, NULL, NULL }, |
| 7490 | }}; |
| 7491 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7492 | /* This function can fail with an abort() due to an Lua critical error. |
| 7493 | * We are in the initialisation process of HAProxy, this abort() is |
| 7494 | * tolerated. |
| 7495 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7496 | int hlua_post_init() |
| 7497 | { |
| 7498 | struct hlua_init_function *init; |
| 7499 | const char *msg; |
| 7500 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7501 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7502 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7503 | /* Call post initialisation function in safe environement. */ |
| 7504 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7505 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7506 | error = lua_tostring(gL.T, -1); |
| 7507 | else |
| 7508 | error = "critical error"; |
| 7509 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7510 | exit(1); |
| 7511 | } |
| 7512 | hlua_fcn_post_init(gL.T); |
| 7513 | RESET_SAFE_LJMP(gL.T); |
| 7514 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7515 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7516 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7517 | ret = hlua_ctx_resume(&gL, 0); |
| 7518 | switch (ret) { |
| 7519 | case HLUA_E_OK: |
| 7520 | lua_pop(gL.T, -1); |
| 7521 | return 1; |
| 7522 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7523 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7524 | return 0; |
| 7525 | case HLUA_E_ERRMSG: |
| 7526 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7527 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7528 | return 0; |
| 7529 | case HLUA_E_ERR: |
| 7530 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7531 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7532 | return 0; |
| 7533 | } |
| 7534 | } |
| 7535 | return 1; |
| 7536 | } |
| 7537 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7538 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7539 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7540 | * is the previously allocated size or the kind of object in case of a new |
| 7541 | * allocation. <nsize> is the requested new size. |
| 7542 | */ |
| 7543 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7544 | { |
| 7545 | struct hlua_mem_allocator *zone = ud; |
| 7546 | |
| 7547 | if (nsize == 0) { |
| 7548 | /* it's a free */ |
| 7549 | if (ptr) |
| 7550 | zone->allocated -= osize; |
| 7551 | free(ptr); |
| 7552 | return NULL; |
| 7553 | } |
| 7554 | |
| 7555 | if (!ptr) { |
| 7556 | /* it's a new allocation */ |
| 7557 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7558 | return NULL; |
| 7559 | |
| 7560 | ptr = malloc(nsize); |
| 7561 | if (ptr) |
| 7562 | zone->allocated += nsize; |
| 7563 | return ptr; |
| 7564 | } |
| 7565 | |
| 7566 | /* it's a realloc */ |
| 7567 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7568 | return NULL; |
| 7569 | |
| 7570 | ptr = realloc(ptr, nsize); |
| 7571 | if (ptr) |
| 7572 | zone->allocated += nsize - osize; |
| 7573 | return ptr; |
| 7574 | } |
| 7575 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7576 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7577 | * We are in the initialisation process of HAProxy, this abort() is |
| 7578 | * tolerated. |
| 7579 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7580 | void hlua_init(void) |
| 7581 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7582 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7583 | int idx; |
| 7584 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7585 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7586 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7587 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7588 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7589 | struct srv_kw *kw; |
| 7590 | int tmp_error; |
| 7591 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7592 | char *args[] = { /* SSL client configuration. */ |
| 7593 | "ssl", |
| 7594 | "verify", |
| 7595 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7596 | NULL |
| 7597 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7598 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7599 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7600 | HA_SPIN_INIT(&hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 7601 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7602 | /* Initialise struct hlua and com signals pool */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7603 | pool_head_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7604 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7605 | /* Register configuration keywords. */ |
| 7606 | cfg_register_keywords(&cfg_kws); |
| 7607 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7608 | /* Init main lua stack. */ |
| 7609 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7610 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7611 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7612 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7613 | hlua_sethlua(&gL); |
| 7614 | gL.Tref = LUA_REFNIL; |
| 7615 | gL.task = NULL; |
| 7616 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7617 | /* From this point, until the end of the initialisation fucntion, |
| 7618 | * the Lua function can fail with an abort. We are in the initialisation |
| 7619 | * process of HAProxy, this abort() is tolerated. |
| 7620 | */ |
| 7621 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7622 | /* Initialise lua. */ |
| 7623 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7624 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7625 | /* Set safe environment for the initialisation. */ |
| 7626 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7627 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7628 | error_msg = lua_tostring(gL.T, -1); |
| 7629 | else |
| 7630 | error_msg = "critical error"; |
| 7631 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7632 | exit(1); |
| 7633 | } |
| 7634 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7635 | /* |
| 7636 | * |
| 7637 | * Create "core" object. |
| 7638 | * |
| 7639 | */ |
| 7640 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7641 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7642 | lua_newtable(gL.T); |
| 7643 | |
| 7644 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7645 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7646 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7647 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7648 | /* Register special functions. */ |
| 7649 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7650 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7651 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7652 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7653 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7654 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7655 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7656 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7657 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7658 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7659 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7660 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7661 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7662 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7663 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7664 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7665 | hlua_class_function(gL.T, "log", hlua_log); |
| 7666 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7667 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7668 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7669 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7670 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7671 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7672 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7673 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7674 | |
| 7675 | /* |
| 7676 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7677 | * Register class Map |
| 7678 | * |
| 7679 | */ |
| 7680 | |
| 7681 | /* This table entry is the object "Map" base. */ |
| 7682 | lua_newtable(gL.T); |
| 7683 | |
| 7684 | /* register pattern types. */ |
| 7685 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7686 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7687 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7688 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 7689 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7690 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7691 | |
| 7692 | /* register constructor. */ |
| 7693 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7694 | |
| 7695 | /* Create and fill the metatable. */ |
| 7696 | lua_newtable(gL.T); |
| 7697 | |
| 7698 | /* Create and fille the __index entry. */ |
| 7699 | lua_pushstring(gL.T, "__index"); |
| 7700 | lua_newtable(gL.T); |
| 7701 | |
| 7702 | /* Register . */ |
| 7703 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7704 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7705 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7706 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7707 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7708 | /* Register previous table in the registry with reference and named entry. |
| 7709 | * The function hlua_register_metatable() pops the stack, so we |
| 7710 | * previously create a copy of the table. |
| 7711 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7712 | 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] | 7713 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7714 | |
| 7715 | /* Assign the metatable to the mai Map object. */ |
| 7716 | lua_setmetatable(gL.T, -2); |
| 7717 | |
| 7718 | /* Set a name to the table. */ |
| 7719 | lua_setglobal(gL.T, "Map"); |
| 7720 | |
| 7721 | /* |
| 7722 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7723 | * Register class Channel |
| 7724 | * |
| 7725 | */ |
| 7726 | |
| 7727 | /* Create and fill the metatable. */ |
| 7728 | lua_newtable(gL.T); |
| 7729 | |
| 7730 | /* Create and fille the __index entry. */ |
| 7731 | lua_pushstring(gL.T, "__index"); |
| 7732 | lua_newtable(gL.T); |
| 7733 | |
| 7734 | /* Register . */ |
| 7735 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7736 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7737 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7738 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7739 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7740 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7741 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7742 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7743 | 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] | 7744 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7745 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7746 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7747 | |
| 7748 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7749 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7750 | |
| 7751 | /* |
| 7752 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7753 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7754 | * |
| 7755 | */ |
| 7756 | |
| 7757 | /* Create and fill the metatable. */ |
| 7758 | lua_newtable(gL.T); |
| 7759 | |
| 7760 | /* Create and fille the __index entry. */ |
| 7761 | lua_pushstring(gL.T, "__index"); |
| 7762 | lua_newtable(gL.T); |
| 7763 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7764 | /* Browse existing fetches and create the associated |
| 7765 | * object method. |
| 7766 | */ |
| 7767 | sf = NULL; |
| 7768 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7769 | |
| 7770 | /* Dont register the keywork if the arguments check function are |
| 7771 | * not safe during the runtime. |
| 7772 | */ |
| 7773 | if ((sf->val_args != NULL) && |
| 7774 | (sf->val_args != val_payload_lv) && |
| 7775 | (sf->val_args != val_hdr)) |
| 7776 | continue; |
| 7777 | |
| 7778 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7779 | * by an underscore. |
| 7780 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7781 | strncpy(trash.area, sf->kw, trash.size); |
| 7782 | trash.area[trash.size - 1] = '\0'; |
| 7783 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7784 | if (*p == '.' || *p == '-' || *p == '+') |
| 7785 | *p = '_'; |
| 7786 | |
| 7787 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7788 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7789 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7790 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7791 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7792 | } |
| 7793 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7794 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7795 | |
| 7796 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7797 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7798 | |
| 7799 | /* |
| 7800 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7801 | * Register class Converters |
| 7802 | * |
| 7803 | */ |
| 7804 | |
| 7805 | /* Create and fill the metatable. */ |
| 7806 | lua_newtable(gL.T); |
| 7807 | |
| 7808 | /* Create and fill the __index entry. */ |
| 7809 | lua_pushstring(gL.T, "__index"); |
| 7810 | lua_newtable(gL.T); |
| 7811 | |
| 7812 | /* Browse existing converters and create the associated |
| 7813 | * object method. |
| 7814 | */ |
| 7815 | sc = NULL; |
| 7816 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7817 | /* Dont register the keywork if the arguments check function are |
| 7818 | * not safe during the runtime. |
| 7819 | */ |
| 7820 | if (sc->val_args != NULL) |
| 7821 | continue; |
| 7822 | |
| 7823 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7824 | * by an underscore. |
| 7825 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7826 | strncpy(trash.area, sc->kw, trash.size); |
| 7827 | trash.area[trash.size - 1] = '\0'; |
| 7828 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7829 | if (*p == '.' || *p == '-' || *p == '+') |
| 7830 | *p = '_'; |
| 7831 | |
| 7832 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7833 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7834 | lua_pushlightuserdata(gL.T, sc); |
| 7835 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7836 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7837 | } |
| 7838 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7839 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7840 | |
| 7841 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7842 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7843 | |
| 7844 | /* |
| 7845 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7846 | * Register class HTTP |
| 7847 | * |
| 7848 | */ |
| 7849 | |
| 7850 | /* Create and fill the metatable. */ |
| 7851 | lua_newtable(gL.T); |
| 7852 | |
| 7853 | /* Create and fille the __index entry. */ |
| 7854 | lua_pushstring(gL.T, "__index"); |
| 7855 | lua_newtable(gL.T); |
| 7856 | |
| 7857 | /* Register Lua functions. */ |
| 7858 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7859 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7860 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7861 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7862 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7863 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7864 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7865 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7866 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7867 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7868 | |
| 7869 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7870 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7871 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7872 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7873 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7874 | 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] | 7875 | 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] | 7876 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7877 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7878 | |
| 7879 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7880 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7881 | |
| 7882 | /* |
| 7883 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7884 | * Register class AppletTCP |
| 7885 | * |
| 7886 | */ |
| 7887 | |
| 7888 | /* Create and fill the metatable. */ |
| 7889 | lua_newtable(gL.T); |
| 7890 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7891 | /* Create and fille the __index entry. */ |
| 7892 | lua_pushstring(gL.T, "__index"); |
| 7893 | lua_newtable(gL.T); |
| 7894 | |
| 7895 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7896 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7897 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7898 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7899 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7900 | 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] | 7901 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7902 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7903 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7904 | |
| 7905 | lua_settable(gL.T, -3); |
| 7906 | |
| 7907 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7908 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7909 | |
| 7910 | /* |
| 7911 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7912 | * Register class AppletHTTP |
| 7913 | * |
| 7914 | */ |
| 7915 | |
| 7916 | /* Create and fill the metatable. */ |
| 7917 | lua_newtable(gL.T); |
| 7918 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7919 | /* Create and fille the __index entry. */ |
| 7920 | lua_pushstring(gL.T, "__index"); |
| 7921 | lua_newtable(gL.T); |
| 7922 | |
| 7923 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7924 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7925 | 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] | 7926 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7927 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7928 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7929 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7930 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7931 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7932 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7933 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7934 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 7935 | |
| 7936 | lua_settable(gL.T, -3); |
| 7937 | |
| 7938 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7939 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7940 | |
| 7941 | /* |
| 7942 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7943 | * Register class TXN |
| 7944 | * |
| 7945 | */ |
| 7946 | |
| 7947 | /* Create and fill the metatable. */ |
| 7948 | lua_newtable(gL.T); |
| 7949 | |
| 7950 | /* Create and fille the __index entry. */ |
| 7951 | lua_pushstring(gL.T, "__index"); |
| 7952 | lua_newtable(gL.T); |
| 7953 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7954 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7955 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 7956 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 7957 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 7958 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 7959 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 7960 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 7961 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 7962 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 7963 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 7964 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 7965 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 7966 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 7967 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 7968 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 7969 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 7970 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 7971 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7972 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7973 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7974 | |
| 7975 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7976 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7977 | |
| 7978 | /* |
| 7979 | * |
| 7980 | * Register class Socket |
| 7981 | * |
| 7982 | */ |
| 7983 | |
| 7984 | /* Create and fill the metatable. */ |
| 7985 | lua_newtable(gL.T); |
| 7986 | |
| 7987 | /* Create and fille the __index entry. */ |
| 7988 | lua_pushstring(gL.T, "__index"); |
| 7989 | lua_newtable(gL.T); |
| 7990 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7991 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7992 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7993 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7994 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 7995 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 7996 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 7997 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 7998 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 7999 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8000 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8001 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8002 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8003 | 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] | 8004 | |
| 8005 | /* Register the garbage collector entry. */ |
| 8006 | lua_pushstring(gL.T, "__gc"); |
| 8007 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8008 | 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] | 8009 | |
| 8010 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8011 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8012 | |
| 8013 | /* Proxy and server configuration initialisation. */ |
| 8014 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8015 | init_new_proxy(&socket_proxy); |
| 8016 | socket_proxy.parent = NULL; |
| 8017 | socket_proxy.last_change = now.tv_sec; |
| 8018 | socket_proxy.id = "LUA-SOCKET"; |
| 8019 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8020 | socket_proxy.maxconn = 0; |
| 8021 | socket_proxy.accept = NULL; |
| 8022 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8023 | socket_proxy.srv = NULL; |
| 8024 | socket_proxy.conn_retries = 0; |
| 8025 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8026 | |
| 8027 | /* Init TCP server: unchanged parameters */ |
| 8028 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8029 | socket_tcp.next = NULL; |
| 8030 | socket_tcp.proxy = &socket_proxy; |
| 8031 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8032 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8033 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8034 | socket_tcp.priv_conns = NULL; |
| 8035 | socket_tcp.idle_conns = NULL; |
| 8036 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8037 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8038 | socket_tcp.last_change = 0; |
| 8039 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8040 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8041 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8042 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8043 | |
| 8044 | /* XXX: Copy default parameter from default server, |
| 8045 | * but the default server is not initialized. |
| 8046 | */ |
| 8047 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8048 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8049 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8050 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8051 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8052 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8053 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8054 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8055 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8056 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8057 | |
| 8058 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8059 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8060 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8061 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8062 | socket_tcp.check.server = &socket_tcp; |
| 8063 | |
| 8064 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8065 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8066 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8067 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8068 | socket_tcp.agent.server = &socket_tcp; |
| 8069 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8070 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8071 | |
| 8072 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8073 | /* Init TCP server: unchanged parameters */ |
| 8074 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8075 | socket_ssl.next = NULL; |
| 8076 | socket_ssl.proxy = &socket_proxy; |
| 8077 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8078 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8079 | socket_ssl.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8080 | socket_tcp.priv_conns = NULL; |
| 8081 | socket_tcp.idle_conns = NULL; |
| 8082 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8083 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8084 | socket_ssl.last_change = 0; |
| 8085 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8086 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8087 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8088 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8089 | |
| 8090 | /* XXX: Copy default parameter from default server, |
| 8091 | * but the default server is not initialized. |
| 8092 | */ |
| 8093 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8094 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8095 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8096 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8097 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8098 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8099 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8100 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8101 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8102 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8103 | |
| 8104 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8105 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8106 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8107 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8108 | socket_ssl.check.server = &socket_ssl; |
| 8109 | |
| 8110 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8111 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8112 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8113 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8114 | socket_ssl.agent.server = &socket_ssl; |
| 8115 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8116 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8117 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8118 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8119 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8120 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8121 | /* |
| 8122 | * |
| 8123 | * If the keyword is not known, we can search in the registered |
| 8124 | * server keywords. This is usefull to configure special SSL |
| 8125 | * features like client certificates and ssl_verify. |
| 8126 | * |
| 8127 | */ |
| 8128 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8129 | if (tmp_error != 0) { |
| 8130 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8131 | abort(); /* This must be never arrives because the command line |
| 8132 | not editable by the user. */ |
| 8133 | } |
| 8134 | idx += kw->skip; |
| 8135 | } |
| 8136 | } |
| 8137 | |
| 8138 | /* Initialize SSL server. */ |
Willy Tarreau | cbe6da5 | 2018-05-18 17:08:28 +0200 | [diff] [blame] | 8139 | if (socket_ssl.xprt->prepare_srv) { |
| 8140 | int saved_used_backed = global.ssl_used_backend; |
| 8141 | // don't affect maxconn automatic computation |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 8142 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
Willy Tarreau | cbe6da5 | 2018-05-18 17:08:28 +0200 | [diff] [blame] | 8143 | global.ssl_used_backend = saved_used_backed; |
| 8144 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8145 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8146 | |
| 8147 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8148 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8149 | |
| 8150 | __attribute__((constructor)) |
| 8151 | static void __hlua_init(void) |
| 8152 | { |
| 8153 | char *ptr = NULL; |
| 8154 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8155 | hap_register_build_opts(ptr, 1); |
| 8156 | } |