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> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 14 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 16 | #include <lauxlib.h> |
| 17 | #include <lua.h> |
| 18 | #include <lualib.h> |
| 19 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 20 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 21 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 22 | #endif |
| 23 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 24 | #include <ebpttree.h> |
| 25 | |
| 26 | #include <common/cfgparse.h> |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 27 | #include <common/xref.h> |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 28 | #include <common/hathreads.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 29 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 30 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 31 | #include <types/hlua.h> |
| 32 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 33 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 34 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 35 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 36 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 37 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 38 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 39 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 40 | #include <proto/stats.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 41 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 42 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 43 | #include <proto/hlua_fcn.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 44 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 45 | #include <proto/obj_type.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 46 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 47 | #include <proto/payload.h> |
| 48 | #include <proto/proto_http.h> |
| 49 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 50 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 51 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 52 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 53 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 54 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 55 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 56 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 57 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 58 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 59 | * macro is used only for identifying the function that can |
| 60 | * not return because a longjmp is executed. |
| 61 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 62 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 63 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 64 | */ |
| 65 | #define __LJMP |
| 66 | #define WILL_LJMP(func) func |
| 67 | #define MAY_LJMP(func) func |
| 68 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 69 | /* This couple of function executes securely some Lua calls outside of |
| 70 | * the lua runtime environment. Each Lua call can return a longjmp |
| 71 | * if it encounter a memory error. |
| 72 | * |
| 73 | * Lua documentation extract: |
| 74 | * |
| 75 | * If an error happens outside any protected environment, Lua calls |
| 76 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 77 | * exiting the host application. Your panic function can avoid this |
| 78 | * exit by never returning (e.g., doing a long jump to your own |
| 79 | * recovery point outside Lua). |
| 80 | * |
| 81 | * The panic function runs as if it were a message handler (see |
| 82 | * §2.3); in particular, the error message is at the top of the |
| 83 | * stack. However, there is no guarantee about stack space. To push |
| 84 | * anything on the stack, the panic function must first check the |
| 85 | * available space (see §4.2). |
| 86 | * |
| 87 | * We must check all the Lua entry point. This includes: |
| 88 | * - The include/proto/hlua.h exported functions |
| 89 | * - the task wrapper function |
| 90 | * - The action wrapper function |
| 91 | * - The converters wrapper function |
| 92 | * - The sample-fetch wrapper functions |
| 93 | * |
| 94 | * It is tolerated that the initilisation function returns an abort. |
| 95 | * Before each Lua abort, an error message is writed on stderr. |
| 96 | * |
| 97 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 98 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 99 | * because they must be exists in the program stack when the longjmp |
| 100 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 101 | * |
| 102 | * Note that the Lua processing is not really thread safe. It provides |
| 103 | * heavy system which consists to add our own lock function in the Lua |
| 104 | * code and recompile the library. This system will probably not accepted |
| 105 | * by maintainers of various distribs. |
| 106 | * |
| 107 | * Our main excution point of the Lua is the function lua_resume(). A |
| 108 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 109 | * of function and a lua_unlock() at the end of the function. So I |
| 110 | * conclude that the Lua thread safe mode just perform a mutex around |
| 111 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 112 | * easier for distro maintainers. |
| 113 | * |
| 114 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 115 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 116 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 117 | */ |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 118 | #ifdef USE_THREAD |
| 119 | HA_SPINLOCK_T hlua_global_lock; |
| 120 | #endif |
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; \ |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 128 | 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; \ |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 132 | 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); \ |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 146 | 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 | */ |
| 165 | struct pool_head *pool2_hlua; |
| 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)) \ |
| 264 | Alert(__fmt, ## __args); \ |
| 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)) { |
| 274 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1)); |
| 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 | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 291 | /* This function check the number of arguments available in the |
| 292 | * stack. If the number of arguments available is not the same |
| 293 | * then <nb> an error is throwed. |
| 294 | */ |
| 295 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 296 | { |
| 297 | if (lua_gettop(L) == nb) |
| 298 | return; |
| 299 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 300 | } |
| 301 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 302 | /* This fucntion push an error string prefixed by the file name |
| 303 | * and the line number where the error is encountered. |
| 304 | */ |
| 305 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 306 | { |
| 307 | va_list argp; |
| 308 | va_start(argp, fmt); |
| 309 | luaL_where(L, 1); |
| 310 | lua_pushvfstring(L, fmt, argp); |
| 311 | va_end(argp); |
| 312 | lua_concat(L, 2); |
| 313 | return 1; |
| 314 | } |
| 315 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 316 | /* This functions is used with sample fetch and converters. It |
| 317 | * converts the HAProxy configuration argument in a lua stack |
| 318 | * values. |
| 319 | * |
| 320 | * It takes an array of "arg", and each entry of the array is |
| 321 | * converted and pushed in the LUA stack. |
| 322 | */ |
| 323 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 324 | { |
| 325 | switch (arg->type) { |
| 326 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 327 | case ARGT_TIME: |
| 328 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 329 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 330 | break; |
| 331 | |
| 332 | case ARGT_STR: |
| 333 | lua_pushlstring(L, arg->data.str.str, arg->data.str.len); |
| 334 | break; |
| 335 | |
| 336 | case ARGT_IPV4: |
| 337 | case ARGT_IPV6: |
| 338 | case ARGT_MSK4: |
| 339 | case ARGT_MSK6: |
| 340 | case ARGT_FE: |
| 341 | case ARGT_BE: |
| 342 | case ARGT_TAB: |
| 343 | case ARGT_SRV: |
| 344 | case ARGT_USR: |
| 345 | case ARGT_MAP: |
| 346 | default: |
| 347 | lua_pushnil(L); |
| 348 | break; |
| 349 | } |
| 350 | return 1; |
| 351 | } |
| 352 | |
| 353 | /* This function take one entrie in an LUA stack at the index "ud", |
| 354 | * and try to convert it in an HAProxy argument entry. This is useful |
| 355 | * with sample fetch wrappers. The input arguments are gived to the |
| 356 | * lua wrapper and converted as arg list by thi function. |
| 357 | */ |
| 358 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 359 | { |
| 360 | switch (lua_type(L, ud)) { |
| 361 | |
| 362 | case LUA_TNUMBER: |
| 363 | case LUA_TBOOLEAN: |
| 364 | arg->type = ARGT_SINT; |
| 365 | arg->data.sint = lua_tointeger(L, ud); |
| 366 | break; |
| 367 | |
| 368 | case LUA_TSTRING: |
| 369 | arg->type = ARGT_STR; |
| 370 | arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len); |
| 371 | break; |
| 372 | |
| 373 | case LUA_TUSERDATA: |
| 374 | case LUA_TNIL: |
| 375 | case LUA_TTABLE: |
| 376 | case LUA_TFUNCTION: |
| 377 | case LUA_TTHREAD: |
| 378 | case LUA_TLIGHTUSERDATA: |
| 379 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 380 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 381 | break; |
| 382 | } |
| 383 | return 1; |
| 384 | } |
| 385 | |
| 386 | /* the following functions are used to convert a struct sample |
| 387 | * in Lua type. This useful to convert the return of the |
| 388 | * fetchs or converters. |
| 389 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 390 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 391 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 392 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 393 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 394 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 395 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 396 | break; |
| 397 | |
| 398 | case SMP_T_BIN: |
| 399 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 400 | lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 401 | break; |
| 402 | |
| 403 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 404 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 405 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 406 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 407 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 408 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 409 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 410 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 411 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 412 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 413 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 414 | lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 415 | break; |
| 416 | default: |
| 417 | lua_pushnil(L); |
| 418 | break; |
| 419 | } |
| 420 | break; |
| 421 | |
| 422 | case SMP_T_IPV4: |
| 423 | case SMP_T_IPV6: |
| 424 | 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] | 425 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 426 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 427 | lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 428 | else |
| 429 | lua_pushnil(L); |
| 430 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 431 | default: |
| 432 | lua_pushnil(L); |
| 433 | break; |
| 434 | } |
| 435 | return 1; |
| 436 | } |
| 437 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 438 | /* the following functions are used to convert a struct sample |
| 439 | * in Lua strings. This is useful to convert the return of the |
| 440 | * fetchs or converters. |
| 441 | */ |
| 442 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 443 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 444 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 445 | |
| 446 | case SMP_T_BIN: |
| 447 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 448 | lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 449 | break; |
| 450 | |
| 451 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 452 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 453 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 454 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 455 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 456 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 457 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 458 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 459 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 460 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 461 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 462 | lua_pushlstring(L, smp->data.u.meth.str.str, smp->data.u.meth.str.len); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 463 | break; |
| 464 | default: |
| 465 | lua_pushstring(L, ""); |
| 466 | break; |
| 467 | } |
| 468 | break; |
| 469 | |
| 470 | case SMP_T_SINT: |
| 471 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 472 | case SMP_T_IPV4: |
| 473 | case SMP_T_IPV6: |
| 474 | 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] | 475 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 476 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 477 | lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 478 | else |
| 479 | lua_pushstring(L, ""); |
| 480 | break; |
| 481 | default: |
| 482 | lua_pushstring(L, ""); |
| 483 | break; |
| 484 | } |
| 485 | return 1; |
| 486 | } |
| 487 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 488 | /* the following functions are used to convert an Lua type in a |
| 489 | * struct sample. This is useful to provide data from a converter |
| 490 | * to the LUA code. |
| 491 | */ |
| 492 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 493 | { |
| 494 | switch (lua_type(L, ud)) { |
| 495 | |
| 496 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 497 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 498 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 499 | break; |
| 500 | |
| 501 | |
| 502 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 503 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 504 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 505 | break; |
| 506 | |
| 507 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 508 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 509 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 510 | smp->data.u.str.str = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.len); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 511 | break; |
| 512 | |
| 513 | case LUA_TUSERDATA: |
| 514 | case LUA_TNIL: |
| 515 | case LUA_TTABLE: |
| 516 | case LUA_TFUNCTION: |
| 517 | case LUA_TTHREAD: |
| 518 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 519 | case LUA_TNONE: |
| 520 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 521 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 522 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 523 | break; |
| 524 | } |
| 525 | return 1; |
| 526 | } |
| 527 | |
| 528 | /* This function check the "argp" builded by another conversion function |
| 529 | * is in accord with the expected argp defined by the "mask". The fucntion |
| 530 | * 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] | 531 | * |
| 532 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 533 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 534 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 535 | __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] | 536 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 537 | { |
| 538 | int min_arg; |
| 539 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 540 | struct proxy *px; |
| 541 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 542 | |
| 543 | idx = 0; |
| 544 | min_arg = ARGM(mask); |
| 545 | mask >>= ARGM_BITS; |
| 546 | |
| 547 | while (1) { |
| 548 | |
| 549 | /* Check oversize. */ |
| 550 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 551 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /* Check for mandatory arguments. */ |
| 555 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 556 | if (idx < min_arg) { |
| 557 | |
| 558 | /* If miss other argument than the first one, we return an error. */ |
| 559 | if (idx > 0) |
| 560 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 561 | |
| 562 | /* If first argument have a certain type, some default values |
| 563 | * may be used. See the function smp_resolve_args(). |
| 564 | */ |
| 565 | switch (mask & ARGT_MASK) { |
| 566 | |
| 567 | case ARGT_FE: |
| 568 | if (!(p->cap & PR_CAP_FE)) |
| 569 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 570 | argp[idx].data.prx = p; |
| 571 | argp[idx].type = ARGT_FE; |
| 572 | argp[idx+1].type = ARGT_STOP; |
| 573 | break; |
| 574 | |
| 575 | case ARGT_BE: |
| 576 | if (!(p->cap & PR_CAP_BE)) |
| 577 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 578 | argp[idx].data.prx = p; |
| 579 | argp[idx].type = ARGT_BE; |
| 580 | argp[idx+1].type = ARGT_STOP; |
| 581 | break; |
| 582 | |
| 583 | case ARGT_TAB: |
| 584 | argp[idx].data.prx = p; |
| 585 | argp[idx].type = ARGT_TAB; |
| 586 | argp[idx+1].type = ARGT_STOP; |
| 587 | break; |
| 588 | |
| 589 | default: |
| 590 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 591 | break; |
| 592 | } |
| 593 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 594 | return 0; |
| 595 | } |
| 596 | |
| 597 | /* Check for exceed the number of requiered argument. */ |
| 598 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 599 | argp[idx].type != ARGT_STOP) { |
| 600 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 601 | } |
| 602 | |
| 603 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 604 | argp[idx].type == ARGT_STOP) { |
| 605 | return 0; |
| 606 | } |
| 607 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 608 | /* Convert some argument types. */ |
| 609 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 610 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 611 | if (argp[idx].type != ARGT_SINT) |
| 612 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 613 | argp[idx].type = ARGT_SINT; |
| 614 | break; |
| 615 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 616 | case ARGT_TIME: |
| 617 | if (argp[idx].type != ARGT_SINT) |
| 618 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 619 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 620 | break; |
| 621 | |
| 622 | case ARGT_SIZE: |
| 623 | if (argp[idx].type != ARGT_SINT) |
| 624 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 625 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 626 | break; |
| 627 | |
| 628 | case ARGT_FE: |
| 629 | if (argp[idx].type != ARGT_STR) |
| 630 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 631 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 632 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 633 | argp[idx].data.prx = proxy_fe_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 634 | if (!argp[idx].data.prx) |
| 635 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 636 | argp[idx].type = ARGT_FE; |
| 637 | break; |
| 638 | |
| 639 | case ARGT_BE: |
| 640 | if (argp[idx].type != ARGT_STR) |
| 641 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 642 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 643 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 644 | argp[idx].data.prx = proxy_be_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 645 | if (!argp[idx].data.prx) |
| 646 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 647 | argp[idx].type = ARGT_BE; |
| 648 | break; |
| 649 | |
| 650 | case ARGT_TAB: |
| 651 | if (argp[idx].type != ARGT_STR) |
| 652 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 653 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 654 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | e2dc1fa | 2015-05-26 12:08:07 +0200 | [diff] [blame] | 655 | argp[idx].data.prx = proxy_tbl_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 656 | if (!argp[idx].data.prx) |
| 657 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 658 | argp[idx].type = ARGT_TAB; |
| 659 | break; |
| 660 | |
| 661 | case ARGT_SRV: |
| 662 | if (argp[idx].type != ARGT_STR) |
| 663 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 664 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 665 | trash.str[argp[idx].data.str.len] = 0; |
| 666 | sname = strrchr(trash.str, '/'); |
| 667 | if (sname) { |
| 668 | *sname++ = '\0'; |
| 669 | pname = trash.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 670 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 671 | if (!px) |
| 672 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 673 | } |
| 674 | else { |
| 675 | sname = trash.str; |
| 676 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 677 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 678 | argp[idx].data.srv = findserver(px, sname); |
| 679 | if (!argp[idx].data.srv) |
| 680 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 681 | argp[idx].type = ARGT_SRV; |
| 682 | break; |
| 683 | |
| 684 | case ARGT_IPV4: |
| 685 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 686 | trash.str[argp[idx].data.str.len] = 0; |
| 687 | if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4)) |
| 688 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 689 | argp[idx].type = ARGT_IPV4; |
| 690 | break; |
| 691 | |
| 692 | case ARGT_MSK4: |
| 693 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 694 | trash.str[argp[idx].data.str.len] = 0; |
| 695 | if (!str2mask(trash.str, &argp[idx].data.ipv4)) |
| 696 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 697 | argp[idx].type = ARGT_MSK4; |
| 698 | break; |
| 699 | |
| 700 | case ARGT_IPV6: |
| 701 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 702 | trash.str[argp[idx].data.str.len] = 0; |
| 703 | if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6)) |
| 704 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 705 | argp[idx].type = ARGT_IPV6; |
| 706 | break; |
| 707 | |
| 708 | case ARGT_MSK6: |
| 709 | case ARGT_MAP: |
| 710 | case ARGT_REG: |
| 711 | case ARGT_USR: |
| 712 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 713 | break; |
| 714 | } |
| 715 | |
| 716 | /* Check for type of argument. */ |
| 717 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 718 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 719 | arg_type_names[(mask & ARGT_MASK)], |
| 720 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 721 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 722 | } |
| 723 | |
| 724 | /* Next argument. */ |
| 725 | mask >>= ARGT_BITS; |
| 726 | idx++; |
| 727 | } |
| 728 | } |
| 729 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 730 | /* |
| 731 | * The following functions are used to make correspondance between the the |
| 732 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 733 | * |
| 734 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 735 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 736 | */ |
| 737 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 738 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 739 | struct hlua **hlua = lua_getextraspace(L); |
| 740 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 741 | } |
| 742 | static inline void hlua_sethlua(struct hlua *hlua) |
| 743 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 744 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 745 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 746 | } |
| 747 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 748 | /* This function is used to send logs. It try to send on screen (stderr) |
| 749 | * and on the default syslog server. |
| 750 | */ |
| 751 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 752 | { |
| 753 | struct tm tm; |
| 754 | char *p; |
| 755 | |
| 756 | /* Cleanup the log message. */ |
| 757 | p = trash.str; |
| 758 | for (; *msg != '\0'; msg++, p++) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 759 | if (p >= trash.str + trash.size - 1) { |
| 760 | /* Break the message if exceed the buffer size. */ |
| 761 | *(p-4) = ' '; |
| 762 | *(p-3) = '.'; |
| 763 | *(p-2) = '.'; |
| 764 | *(p-1) = '.'; |
| 765 | break; |
| 766 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 767 | if (isprint(*msg)) |
| 768 | *p = *msg; |
| 769 | else |
| 770 | *p = '.'; |
| 771 | } |
| 772 | *p = '\0'; |
| 773 | |
Thierry FOURNIER | 5554e29 | 2015-09-09 11:21:37 +0200 | [diff] [blame] | 774 | send_log(px, level, "%s\n", trash.str); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 775 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 776 | get_localtime(date.tv_sec, &tm); |
| 777 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 778 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 779 | (int)getpid(), trash.str); |
| 780 | fflush(stderr); |
| 781 | } |
| 782 | } |
| 783 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 784 | /* This function just ensure that the yield will be always |
| 785 | * returned with a timeout and permit to set some flags |
| 786 | */ |
| 787 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 788 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 789 | { |
| 790 | struct hlua *hlua = hlua_gethlua(L); |
| 791 | |
| 792 | /* Set the wake timeout. If timeout is required, we set |
| 793 | * the expiration time. |
| 794 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 795 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 796 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 797 | hlua->flags |= flags; |
| 798 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 799 | /* Process the yield. */ |
| 800 | WILL_LJMP(lua_yieldk(L, nresults, ctx, k)); |
| 801 | } |
| 802 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 803 | /* This function initialises the Lua environment stored in the stream. |
| 804 | * 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] | 805 | * 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] | 806 | * |
| 807 | * This function is particular. it initialises a new Lua thread. If the |
| 808 | * initialisation fails (example: out of memory error), the lua function |
| 809 | * throws an error (longjmp). |
| 810 | * |
| 811 | * This function manipulates two Lua stack: the main and the thread. Only |
| 812 | * the main stack can fail. The thread is not manipulated. This function |
| 813 | * MUST NOT manipulate the created thread stack state, because is not |
| 814 | * proctected agains error throwed by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 815 | */ |
| 816 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 817 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 818 | if (!SET_SAFE_LJMP(gL.T)) { |
| 819 | lua->Tref = LUA_REFNIL; |
| 820 | return 0; |
| 821 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 822 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 823 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 824 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 825 | lua->T = lua_newthread(gL.T); |
| 826 | if (!lua->T) { |
| 827 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | 0a97620 | 2017-07-12 11:18:00 +0200 | [diff] [blame] | 828 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 829 | return 0; |
| 830 | } |
| 831 | hlua_sethlua(lua); |
| 832 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 833 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 834 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 835 | return 1; |
| 836 | } |
| 837 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 838 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 839 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 840 | * is not freed. |
| 841 | */ |
| 842 | void hlua_ctx_destroy(struct hlua *lua) |
| 843 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 844 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 845 | return; |
| 846 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 847 | if (!lua->T) |
| 848 | goto end; |
| 849 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 850 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 851 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 852 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 853 | if (!SET_SAFE_LJMP(lua->T)) |
| 854 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 855 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 856 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 857 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 858 | if (!SET_SAFE_LJMP(gL.T)) |
| 859 | return; |
| 860 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 861 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 862 | /* Forces a garbage collecting process. If the Lua program is finished |
| 863 | * without error, we run the GC on the thread pointer. Its freed all |
| 864 | * the unused memory. |
| 865 | * If the thread is finnish with an error or is currently yielded, |
| 866 | * it seems that the GC applied on the thread doesn't clean anything, |
| 867 | * so e run the GC on the main thread. |
| 868 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 869 | * the garbage collection. |
| 870 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 871 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 872 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 873 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 874 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 875 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 876 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 877 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 878 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 879 | |
| 880 | end: |
| 881 | pool_free2(pool2_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | /* This function is used to restore the Lua context when a coroutine |
| 885 | * fails. This function copy the common memory between old coroutine |
| 886 | * and the new coroutine. The old coroutine is destroyed, and its |
| 887 | * replaced by the new coroutine. |
| 888 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 889 | * as string error message and it is copied in the new stack. |
| 890 | */ |
| 891 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 892 | { |
| 893 | lua_State *T; |
| 894 | int new_ref; |
| 895 | |
| 896 | /* Renew the main LUA stack doesn't have sense. */ |
| 897 | if (lua == &gL) |
| 898 | return 0; |
| 899 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 900 | /* New Lua coroutine. */ |
| 901 | T = lua_newthread(gL.T); |
| 902 | if (!T) |
| 903 | return 0; |
| 904 | |
| 905 | /* Copy last error message. */ |
| 906 | if (keep_msg) |
| 907 | lua_xmove(lua->T, T, 1); |
| 908 | |
| 909 | /* Copy data between the coroutines. */ |
| 910 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 911 | lua_xmove(lua->T, T, 1); |
| 912 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 913 | |
| 914 | /* Destroy old data. */ |
| 915 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 916 | |
| 917 | /* The thread is garbage collected by Lua. */ |
| 918 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 919 | |
| 920 | /* Fill the struct with the new coroutine values. */ |
| 921 | lua->Mref = new_ref; |
| 922 | lua->T = T; |
| 923 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 924 | |
| 925 | /* Set context. */ |
| 926 | hlua_sethlua(lua); |
| 927 | |
| 928 | return 1; |
| 929 | } |
| 930 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 931 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 932 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 933 | struct hlua *hlua = hlua_gethlua(L); |
| 934 | |
| 935 | /* Lua cannot yield when its returning from a function, |
| 936 | * so, we can fix the interrupt hook to 1 instruction, |
| 937 | * expecting that the function is finnished. |
| 938 | */ |
| 939 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 940 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 941 | return; |
| 942 | } |
| 943 | |
| 944 | /* restore the interrupt condition. */ |
| 945 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 946 | |
| 947 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 948 | * If the state is not yieldable, trying yield causes an error. |
| 949 | */ |
| 950 | if (lua_isyieldable(L)) |
| 951 | WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 952 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 953 | /* If we cannot yield, update the clock and check the timeout. */ |
| 954 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 955 | hlua->run_time += now_ms - hlua->start_time; |
| 956 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 957 | lua_pushfstring(L, "execution timeout"); |
| 958 | WILL_LJMP(lua_error(L)); |
| 959 | } |
| 960 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 961 | /* Update the start time. */ |
| 962 | hlua->start_time = now_ms; |
| 963 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 964 | /* Try to interrupt the process at the end of the current |
| 965 | * unyieldable function. |
| 966 | */ |
| 967 | 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] | 968 | } |
| 969 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 970 | /* This function start or resumes the Lua stack execution. If the flag |
| 971 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 972 | * The function return an error. |
| 973 | * |
| 974 | * The function can returns 4 values: |
| 975 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 976 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 977 | * task wakeup. |
| 978 | * - HLUA_E_ERRMSG : An error has occured, an error message is set in |
| 979 | * the top of the stack. |
| 980 | * - HLUA_E_ERR : An error has occured without error message. |
| 981 | * |
| 982 | * If an error occured, the stack is renewed and it is ready to run new |
| 983 | * LUA code. |
| 984 | */ |
| 985 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 986 | { |
| 987 | int ret; |
| 988 | const char *msg; |
| 989 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 990 | /* Initialise run time counter. */ |
| 991 | if (!HLUA_IS_RUNNING(lua)) |
| 992 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 993 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 994 | /* Lock the whole Lua execution. This lock must be before the |
| 995 | * label "resume_execution". |
| 996 | */ |
| 997 | SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
| 998 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 999 | resume_execution: |
| 1000 | |
| 1001 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1002 | * instructions. it is used for preventing infinite loops. |
| 1003 | */ |
| 1004 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1005 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1006 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1007 | HLUA_SET_RUN(lua); |
| 1008 | HLUA_CLR_CTRLYIELD(lua); |
| 1009 | HLUA_CLR_WAKERESWR(lua); |
| 1010 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1011 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1012 | /* Update the start time. */ |
| 1013 | lua->start_time = now_ms; |
| 1014 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1015 | /* Call the function. */ |
| 1016 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1017 | switch (ret) { |
| 1018 | |
| 1019 | case LUA_OK: |
| 1020 | ret = HLUA_E_OK; |
| 1021 | break; |
| 1022 | |
| 1023 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1024 | /* Check if the execution timeout is expired. It it is the case, we |
| 1025 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1026 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1027 | tv_update_date(0, 1); |
| 1028 | lua->run_time += now_ms - lua->start_time; |
| 1029 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1030 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1031 | if (!lua_checkstack(lua->T, 1)) { |
| 1032 | ret = HLUA_E_ERR; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1033 | break; |
| 1034 | } |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1035 | lua_pushfstring(lua->T, "execution timeout"); |
| 1036 | ret = HLUA_E_ERRMSG; |
| 1037 | break; |
| 1038 | } |
| 1039 | /* Process the forced yield. if the general yield is not allowed or |
| 1040 | * if no task were associated this the current Lua execution |
| 1041 | * coroutine, we resume the execution. Else we want to return in the |
| 1042 | * scheduler and we want to be waked up again, to continue the |
| 1043 | * current Lua execution. So we schedule our own task. |
| 1044 | */ |
| 1045 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1046 | if (!yield_allowed || !lua->task) |
| 1047 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1048 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1049 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1050 | if (!yield_allowed) { |
| 1051 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1052 | if (!lua_checkstack(lua->T, 1)) { |
| 1053 | ret = HLUA_E_ERR; |
| 1054 | break; |
| 1055 | } |
| 1056 | lua_pushfstring(lua->T, "yield not allowed"); |
| 1057 | ret = HLUA_E_ERRMSG; |
| 1058 | break; |
| 1059 | } |
| 1060 | ret = HLUA_E_AGAIN; |
| 1061 | break; |
| 1062 | |
| 1063 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1064 | |
| 1065 | /* Special exit case. The traditionnal exit is returned as an error |
| 1066 | * because the errors ares the only one mean to return immediately |
| 1067 | * from and lua execution. |
| 1068 | */ |
| 1069 | if (lua->flags & HLUA_EXIT) { |
| 1070 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1071 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1072 | break; |
| 1073 | } |
| 1074 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1075 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1076 | if (!lua_checkstack(lua->T, 1)) { |
| 1077 | ret = HLUA_E_ERR; |
| 1078 | break; |
| 1079 | } |
| 1080 | msg = lua_tostring(lua->T, -1); |
| 1081 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1082 | lua_pop(lua->T, 1); |
| 1083 | if (msg) |
| 1084 | lua_pushfstring(lua->T, "runtime error: %s", msg); |
| 1085 | else |
| 1086 | lua_pushfstring(lua->T, "unknown runtime error"); |
| 1087 | ret = HLUA_E_ERRMSG; |
| 1088 | break; |
| 1089 | |
| 1090 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1091 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1092 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1093 | if (!lua_checkstack(lua->T, 1)) { |
| 1094 | ret = HLUA_E_ERR; |
| 1095 | break; |
| 1096 | } |
| 1097 | lua_pushfstring(lua->T, "out of memory error"); |
| 1098 | ret = HLUA_E_ERRMSG; |
| 1099 | break; |
| 1100 | |
| 1101 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1102 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1103 | if (!lua_checkstack(lua->T, 1)) { |
| 1104 | ret = HLUA_E_ERR; |
| 1105 | break; |
| 1106 | } |
| 1107 | msg = lua_tostring(lua->T, -1); |
| 1108 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1109 | lua_pop(lua->T, 1); |
| 1110 | if (msg) |
| 1111 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1112 | else |
| 1113 | lua_pushfstring(lua->T, "message handler error"); |
| 1114 | ret = HLUA_E_ERRMSG; |
| 1115 | break; |
| 1116 | |
| 1117 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1118 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1119 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1120 | if (!lua_checkstack(lua->T, 1)) { |
| 1121 | ret = HLUA_E_ERR; |
| 1122 | break; |
| 1123 | } |
| 1124 | lua_pushfstring(lua->T, "unknonwn error"); |
| 1125 | ret = HLUA_E_ERRMSG; |
| 1126 | break; |
| 1127 | } |
| 1128 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1129 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1130 | if (lua->flags & HLUA_MUST_GC && |
| 1131 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1132 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1133 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1134 | switch (ret) { |
| 1135 | case HLUA_E_AGAIN: |
| 1136 | break; |
| 1137 | |
| 1138 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1139 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1140 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1141 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1142 | break; |
| 1143 | |
| 1144 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1145 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1146 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1147 | hlua_ctx_renew(lua, 0); |
| 1148 | break; |
| 1149 | |
| 1150 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1151 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1152 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1153 | break; |
| 1154 | } |
| 1155 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1156 | /* This is the main exit point, remove the Lua lock. */ |
| 1157 | SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
| 1158 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1159 | return ret; |
| 1160 | } |
| 1161 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1162 | /* This function exit the current code. */ |
| 1163 | __LJMP static int hlua_done(lua_State *L) |
| 1164 | { |
| 1165 | struct hlua *hlua = hlua_gethlua(L); |
| 1166 | |
| 1167 | hlua->flags |= HLUA_EXIT; |
| 1168 | WILL_LJMP(lua_error(L)); |
| 1169 | |
| 1170 | return 0; |
| 1171 | } |
| 1172 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1173 | /* This function is an LUA binding. It provides a function |
| 1174 | * for deleting ACL from a referenced ACL file. |
| 1175 | */ |
| 1176 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1177 | { |
| 1178 | const char *name; |
| 1179 | const char *key; |
| 1180 | struct pat_ref *ref; |
| 1181 | |
| 1182 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1183 | |
| 1184 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1185 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1186 | |
| 1187 | ref = pat_ref_lookup(name); |
| 1188 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1189 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1190 | |
| 1191 | pat_ref_delete(ref, key); |
| 1192 | return 0; |
| 1193 | } |
| 1194 | |
| 1195 | /* This function is an LUA binding. It provides a function |
| 1196 | * for deleting map entry from a referenced map file. |
| 1197 | */ |
| 1198 | static int hlua_del_map(lua_State *L) |
| 1199 | { |
| 1200 | const char *name; |
| 1201 | const char *key; |
| 1202 | struct pat_ref *ref; |
| 1203 | |
| 1204 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1205 | |
| 1206 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1207 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1208 | |
| 1209 | ref = pat_ref_lookup(name); |
| 1210 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1211 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1212 | |
| 1213 | pat_ref_delete(ref, key); |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
| 1217 | /* This function is an LUA binding. It provides a function |
| 1218 | * for adding ACL pattern from a referenced ACL file. |
| 1219 | */ |
| 1220 | static int hlua_add_acl(lua_State *L) |
| 1221 | { |
| 1222 | const char *name; |
| 1223 | const char *key; |
| 1224 | struct pat_ref *ref; |
| 1225 | |
| 1226 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1227 | |
| 1228 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1229 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1230 | |
| 1231 | ref = pat_ref_lookup(name); |
| 1232 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1233 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1234 | |
| 1235 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1236 | pat_ref_add(ref, key, NULL, NULL); |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
| 1240 | /* This function is an LUA binding. It provides a function |
| 1241 | * for setting map pattern and sample from a referenced map |
| 1242 | * file. |
| 1243 | */ |
| 1244 | static int hlua_set_map(lua_State *L) |
| 1245 | { |
| 1246 | const char *name; |
| 1247 | const char *key; |
| 1248 | const char *value; |
| 1249 | struct pat_ref *ref; |
| 1250 | |
| 1251 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1252 | |
| 1253 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1254 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1255 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1256 | |
| 1257 | ref = pat_ref_lookup(name); |
| 1258 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1259 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1260 | |
| 1261 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1262 | pat_ref_set(ref, key, value, NULL); |
| 1263 | else |
| 1264 | pat_ref_add(ref, key, value, NULL); |
| 1265 | return 0; |
| 1266 | } |
| 1267 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1268 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1269 | * an integer or user data. This data is associated with a metatable. This |
| 1270 | * metatable have an original version registred in the global context with |
| 1271 | * the name of the object (_G[<name>] = <metable> ). |
| 1272 | * |
| 1273 | * A metable is a table that modify the standard behavior of a standard |
| 1274 | * access to the associated data. The entries of this new metatable are |
| 1275 | * defined as is: |
| 1276 | * |
| 1277 | * http://lua-users.org/wiki/MetatableEvents |
| 1278 | * |
| 1279 | * __index |
| 1280 | * |
| 1281 | * we access an absent field in a table, the result is nil. This is |
| 1282 | * true, but it is not the whole truth. Actually, such access triggers |
| 1283 | * the interpreter to look for an __index metamethod: If there is no |
| 1284 | * such method, as usually happens, then the access results in nil; |
| 1285 | * otherwise, the metamethod will provide the result. |
| 1286 | * |
| 1287 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1288 | * the key does not appear in the table, but the metatable has an __index |
| 1289 | * property: |
| 1290 | * |
| 1291 | * - if the value is a function, the function is called, passing in the |
| 1292 | * table and the key; the return value of that function is returned as |
| 1293 | * the result. |
| 1294 | * |
| 1295 | * - if the value is another table, the value of the key in that table is |
| 1296 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1297 | * table's metatable has an __index property, then it continues on up) |
| 1298 | * |
| 1299 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1300 | * |
| 1301 | * http://www.lua.org/pil/13.4.1.html |
| 1302 | * |
| 1303 | * __newindex |
| 1304 | * |
| 1305 | * Like __index, but control property assignment. |
| 1306 | * |
| 1307 | * __mode - Control weak references. A string value with one or both |
| 1308 | * of the characters 'k' and 'v' which specifies that the the |
| 1309 | * keys and/or values in the table are weak references. |
| 1310 | * |
| 1311 | * __call - Treat a table like a function. When a table is followed by |
| 1312 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1313 | * a __call key pointing to a function, that function is invoked |
| 1314 | * (passing any specified arguments) and the return value is |
| 1315 | * returned. |
| 1316 | * |
| 1317 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1318 | * called, if the metatable for myTable has a __metatable |
| 1319 | * key, the value of that key is returned instead of the |
| 1320 | * actual metatable. |
| 1321 | * |
| 1322 | * __tostring - Control string representation. When the builtin |
| 1323 | * "tostring( myTable )" function is called, if the metatable |
| 1324 | * for myTable has a __tostring property set to a function, |
| 1325 | * that function is invoked (passing myTable to it) and the |
| 1326 | * return value is used as the string representation. |
| 1327 | * |
| 1328 | * __len - Control table length. When the table length is requested using |
| 1329 | * the length operator ( '#' ), if the metatable for myTable has |
| 1330 | * a __len key pointing to a function, that function is invoked |
| 1331 | * (passing myTable to it) and the return value used as the value |
| 1332 | * of "#myTable". |
| 1333 | * |
| 1334 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1335 | * collected, if the metatable has a __gc field pointing to a |
| 1336 | * function, that function is first invoked, passing the userdata |
| 1337 | * to it. The __gc metamethod is not called for tables. |
| 1338 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1339 | * |
| 1340 | * Special metamethods for redefining standard operators: |
| 1341 | * http://www.lua.org/pil/13.1.html |
| 1342 | * |
| 1343 | * __add "+" |
| 1344 | * __sub "-" |
| 1345 | * __mul "*" |
| 1346 | * __div "/" |
| 1347 | * __unm "!" |
| 1348 | * __pow "^" |
| 1349 | * __concat ".." |
| 1350 | * |
| 1351 | * Special methods for redfining standar relations |
| 1352 | * http://www.lua.org/pil/13.2.html |
| 1353 | * |
| 1354 | * __eq "==" |
| 1355 | * __lt "<" |
| 1356 | * __le "<=" |
| 1357 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1358 | |
| 1359 | /* |
| 1360 | * |
| 1361 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1362 | * Class Map |
| 1363 | * |
| 1364 | * |
| 1365 | */ |
| 1366 | |
| 1367 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1368 | * a class session, otherwise it throws an error. |
| 1369 | */ |
| 1370 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1371 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1372 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | /* This function is the map constructor. It don't need |
| 1376 | * the class Map object. It creates and return a new Map |
| 1377 | * object. It must be called only during "body" or "init" |
| 1378 | * context because it process some filesystem accesses. |
| 1379 | */ |
| 1380 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1381 | { |
| 1382 | const char *fn; |
| 1383 | int match = PAT_MATCH_STR; |
| 1384 | struct sample_conv conv; |
| 1385 | const char *file = ""; |
| 1386 | int line = 0; |
| 1387 | lua_Debug ar; |
| 1388 | char *err = NULL; |
| 1389 | struct arg args[2]; |
| 1390 | |
| 1391 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1392 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1393 | |
| 1394 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1395 | |
| 1396 | if (lua_gettop(L) >= 2) { |
| 1397 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1398 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1399 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1400 | } |
| 1401 | |
| 1402 | /* Get Lua filename and line number. */ |
| 1403 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1404 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1405 | if (ar.currentline > 0) { /* is there info? */ |
| 1406 | file = ar.short_src; |
| 1407 | line = ar.currentline; |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | /* fill fake sample_conv struct. */ |
| 1412 | conv.kw = ""; /* unused. */ |
| 1413 | conv.process = NULL; /* unused. */ |
| 1414 | conv.arg_mask = 0; /* unused. */ |
| 1415 | conv.val_args = NULL; /* unused. */ |
| 1416 | conv.out_type = SMP_T_STR; |
| 1417 | conv.private = (void *)(long)match; |
| 1418 | switch (match) { |
| 1419 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1420 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1421 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1422 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1423 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1424 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1425 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1426 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1427 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1428 | default: |
| 1429 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1430 | } |
| 1431 | |
| 1432 | /* fill fake args. */ |
| 1433 | args[0].type = ARGT_STR; |
| 1434 | args[0].data.str.str = (char *)fn; |
| 1435 | args[1].type = ARGT_STOP; |
| 1436 | |
| 1437 | /* load the map. */ |
| 1438 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1439 | /* error case: we cant use luaL_error because we must |
| 1440 | * free the err variable. |
| 1441 | */ |
| 1442 | luaL_where(L, 1); |
| 1443 | lua_pushfstring(L, "'new': %s.", err); |
| 1444 | lua_concat(L, 2); |
| 1445 | free(err); |
| 1446 | WILL_LJMP(lua_error(L)); |
| 1447 | } |
| 1448 | |
| 1449 | /* create the lua object. */ |
| 1450 | lua_newtable(L); |
| 1451 | lua_pushlightuserdata(L, args[0].data.map); |
| 1452 | lua_rawseti(L, -2, 0); |
| 1453 | |
| 1454 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1455 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1456 | lua_setmetatable(L, -2); |
| 1457 | |
| 1458 | |
| 1459 | return 1; |
| 1460 | } |
| 1461 | |
| 1462 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1463 | { |
| 1464 | struct map_descriptor *desc; |
| 1465 | struct pattern *pat; |
| 1466 | struct sample smp; |
| 1467 | |
| 1468 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1469 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1470 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1471 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1472 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1473 | } |
| 1474 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1475 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1476 | smp.flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1477 | smp.data.u.str.str = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.len)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1481 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1482 | if (str) |
| 1483 | lua_pushstring(L, ""); |
| 1484 | else |
| 1485 | lua_pushnil(L); |
| 1486 | return 1; |
| 1487 | } |
| 1488 | |
| 1489 | /* The Lua pattern must return a string, so we can't check the returned type */ |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1490 | lua_pushlstring(L, pat->data->u.str.str, pat->data->u.str.len); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1491 | return 1; |
| 1492 | } |
| 1493 | |
| 1494 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1495 | { |
| 1496 | return _hlua_map_lookup(L, 0); |
| 1497 | } |
| 1498 | |
| 1499 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1500 | { |
| 1501 | return _hlua_map_lookup(L, 1); |
| 1502 | } |
| 1503 | |
| 1504 | /* |
| 1505 | * |
| 1506 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1507 | * Class Socket |
| 1508 | * |
| 1509 | * |
| 1510 | */ |
| 1511 | |
| 1512 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1513 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1514 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1515 | } |
| 1516 | |
| 1517 | /* This function is the handler called for each I/O on the established |
| 1518 | * connection. It is used for notify space avalaible to send or data |
| 1519 | * received. |
| 1520 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1521 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1522 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1523 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | 50fe03b | 2014-11-28 13:59:31 +0100 | [diff] [blame] | 1524 | struct connection *c = objt_conn(si_opposite(si)->end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1525 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1526 | if (appctx->ctx.hlua_cosocket.die) { |
| 1527 | si_shutw(si); |
| 1528 | si_shutr(si); |
| 1529 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1530 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1531 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1532 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1533 | } |
| 1534 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1535 | /* If the connection object is not avalaible, close all the |
| 1536 | * streams and wakeup everithing waiting for. |
| 1537 | */ |
| 1538 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1539 | si_shutw(si); |
| 1540 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1541 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1542 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1543 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1544 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1545 | } |
| 1546 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1547 | /* If we cant write, wakeup the pending write signals. */ |
| 1548 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1549 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1550 | |
| 1551 | /* If we cant read, wakeup the pending read signals. */ |
| 1552 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1553 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1554 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1555 | /* if the connection is not estabkished, inform the stream that we want |
| 1556 | * to be notified whenever the connection completes. |
| 1557 | */ |
| 1558 | if (!(c->flags & CO_FL_CONNECTED)) { |
| 1559 | si_applet_cant_get(si); |
| 1560 | si_applet_cant_put(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1561 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1562 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1563 | |
| 1564 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1565 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1566 | |
| 1567 | /* 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] | 1568 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1569 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1570 | |
| 1571 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1572 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1573 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1574 | } |
| 1575 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1576 | /* This function is called when the "struct stream" is destroyed. |
| 1577 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1578 | * Wake all the pending signals. |
| 1579 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1580 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1581 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1582 | struct xref *peer; |
| 1583 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1584 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1585 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1586 | if (peer) |
| 1587 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1588 | |
| 1589 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1590 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1591 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1595 | * uses this object. If the stream does not exists, just quit. |
| 1596 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1597 | * pending signal can rest in the read and write lists. destroy |
| 1598 | * it. |
| 1599 | */ |
| 1600 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1601 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1602 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1603 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1604 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1605 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1606 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1607 | |
| 1608 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1609 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1610 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1611 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1612 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1613 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1614 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1615 | appctx->ctx.hlua_cosocket.die = 1; |
| 1616 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1617 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1618 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1619 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1620 | return 0; |
| 1621 | } |
| 1622 | |
| 1623 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1624 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1625 | */ |
| 1626 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1627 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1628 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1629 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1630 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1631 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1632 | MAY_LJMP(check_args(L, 1, "close")); |
| 1633 | |
| 1634 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1635 | |
| 1636 | /* Check if we run on the same thread than the xreator thread. |
| 1637 | * We cannot access to the socket if the thread is different. |
| 1638 | */ |
| 1639 | if (socket->tid != tid) |
| 1640 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1641 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1642 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1643 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1644 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1645 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1646 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1647 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1648 | appctx->ctx.hlua_cosocket.die = 1; |
| 1649 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1650 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1651 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1652 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1653 | return 0; |
| 1654 | } |
| 1655 | |
| 1656 | /* This Lua function assumes that the stack contain three parameters. |
| 1657 | * 1 - USERDATA containing a struct socket |
| 1658 | * 2 - INTEGER with values of the macro defined below |
| 1659 | * If the integer is -1, we must read at most one line. |
| 1660 | * If the integer is -2, we ust read all the data until the |
| 1661 | * end of the stream. |
| 1662 | * If the integer is positive value, we must read a number of |
| 1663 | * bytes corresponding to this value. |
| 1664 | */ |
| 1665 | #define HLSR_READ_LINE (-1) |
| 1666 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1667 | __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] | 1668 | { |
| 1669 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1670 | int wanted = lua_tointeger(L, 2); |
| 1671 | struct hlua *hlua = hlua_gethlua(L); |
| 1672 | struct appctx *appctx; |
| 1673 | int len; |
| 1674 | int nblk; |
| 1675 | char *blk1; |
| 1676 | int len1; |
| 1677 | char *blk2; |
| 1678 | int len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1679 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1680 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1681 | struct stream_interface *si; |
| 1682 | struct stream *s; |
| 1683 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1684 | |
| 1685 | /* Check if this lua stack is schedulable. */ |
| 1686 | if (!hlua || !hlua->task) |
| 1687 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1688 | "'frontend', 'backend' or 'task'")); |
| 1689 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1690 | /* Check if we run on the same thread than the xreator thread. |
| 1691 | * We cannot access to the socket if the thread is different. |
| 1692 | */ |
| 1693 | if (socket->tid != tid) |
| 1694 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1695 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1696 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1697 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1698 | if (!peer) |
| 1699 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1700 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1701 | si = appctx->owner; |
| 1702 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1703 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1704 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1705 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1706 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1707 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1708 | if (nblk < 0) /* Connection close. */ |
| 1709 | goto connection_closed; |
| 1710 | if (nblk == 0) /* No data avalaible. */ |
| 1711 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1712 | |
| 1713 | /* remove final \r\n. */ |
| 1714 | if (nblk == 1) { |
| 1715 | if (blk1[len1-1] == '\n') { |
| 1716 | len1--; |
| 1717 | skip_at_end++; |
| 1718 | if (blk1[len1-1] == '\r') { |
| 1719 | len1--; |
| 1720 | skip_at_end++; |
| 1721 | } |
| 1722 | } |
| 1723 | } |
| 1724 | else { |
| 1725 | if (blk2[len2-1] == '\n') { |
| 1726 | len2--; |
| 1727 | skip_at_end++; |
| 1728 | if (blk2[len2-1] == '\r') { |
| 1729 | len2--; |
| 1730 | skip_at_end++; |
| 1731 | } |
| 1732 | } |
| 1733 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1734 | } |
| 1735 | |
| 1736 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1737 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1738 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1739 | if (nblk < 0) /* Connection close. */ |
| 1740 | goto connection_closed; |
| 1741 | if (nblk == 0) /* No data avalaible. */ |
| 1742 | goto connection_empty; |
| 1743 | } |
| 1744 | |
| 1745 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1746 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1747 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1748 | if (nblk < 0) /* Connection close. */ |
| 1749 | goto connection_closed; |
| 1750 | if (nblk == 0) /* No data avalaible. */ |
| 1751 | goto connection_empty; |
| 1752 | |
| 1753 | if (len1 > wanted) { |
| 1754 | nblk = 1; |
| 1755 | len1 = wanted; |
| 1756 | } if (nblk == 2 && len1 + len2 > wanted) |
| 1757 | len2 = wanted - len1; |
| 1758 | } |
| 1759 | |
| 1760 | len = len1; |
| 1761 | |
| 1762 | luaL_addlstring(&socket->b, blk1, len1); |
| 1763 | if (nblk == 2) { |
| 1764 | len += len2; |
| 1765 | luaL_addlstring(&socket->b, blk2, len2); |
| 1766 | } |
| 1767 | |
| 1768 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1769 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1770 | |
| 1771 | /* Don't wait anything. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1772 | stream_int_notify(&s->si[0]); |
| 1773 | stream_int_update_applet(&s->si[0]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1774 | |
| 1775 | /* If the pattern reclaim to read all the data |
| 1776 | * in the connection, got out. |
| 1777 | */ |
| 1778 | if (wanted == HLSR_READ_ALL) |
| 1779 | goto connection_empty; |
| 1780 | else if (wanted >= 0 && len < wanted) |
| 1781 | goto connection_empty; |
| 1782 | |
| 1783 | /* Return result. */ |
| 1784 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1785 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1786 | return 1; |
| 1787 | |
| 1788 | connection_closed: |
| 1789 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1790 | xref_unlock(&socket->xref, peer); |
| 1791 | |
| 1792 | no_peer: |
| 1793 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1794 | /* If the buffer containds data. */ |
| 1795 | if (socket->b.n > 0) { |
| 1796 | luaL_pushresult(&socket->b); |
| 1797 | return 1; |
| 1798 | } |
| 1799 | lua_pushnil(L); |
| 1800 | lua_pushstring(L, "connection closed."); |
| 1801 | return 2; |
| 1802 | |
| 1803 | connection_empty: |
| 1804 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1805 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1806 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1807 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1808 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1809 | } |
| 1810 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1811 | 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] | 1812 | return 0; |
| 1813 | } |
| 1814 | |
| 1815 | /* This Lus function gets two parameters. The first one can be string |
| 1816 | * or a number. If the string is "*l", the user require one line. If |
| 1817 | * the string is "*a", the user require all the content of the stream. |
| 1818 | * If the value is a number, the user require a number of bytes equal |
| 1819 | * to the value. The default value is "*l" (a line). |
| 1820 | * |
| 1821 | * This paraeter with a variable type is converted in integer. This |
| 1822 | * integer takes this values: |
| 1823 | * -1 : read a line |
| 1824 | * -2 : read all the stream |
| 1825 | * >0 : amount if bytes. |
| 1826 | * |
| 1827 | * The second parameter is optinal. It contains a string that must be |
| 1828 | * concatenated with the read data. |
| 1829 | */ |
| 1830 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1831 | { |
| 1832 | int wanted = HLSR_READ_LINE; |
| 1833 | const char *pattern; |
| 1834 | int type; |
| 1835 | char *error; |
| 1836 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1837 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1838 | |
| 1839 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1840 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1841 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1842 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1843 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1844 | /* Check if we run on the same thread than the xreator thread. |
| 1845 | * We cannot access to the socket if the thread is different. |
| 1846 | */ |
| 1847 | if (socket->tid != tid) |
| 1848 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1849 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1850 | /* check for pattern. */ |
| 1851 | if (lua_gettop(L) >= 2) { |
| 1852 | type = lua_type(L, 2); |
| 1853 | if (type == LUA_TSTRING) { |
| 1854 | pattern = lua_tostring(L, 2); |
| 1855 | if (strcmp(pattern, "*a") == 0) |
| 1856 | wanted = HLSR_READ_ALL; |
| 1857 | else if (strcmp(pattern, "*l") == 0) |
| 1858 | wanted = HLSR_READ_LINE; |
| 1859 | else { |
| 1860 | wanted = strtoll(pattern, &error, 10); |
| 1861 | if (*error != '\0') |
| 1862 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1863 | } |
| 1864 | } |
| 1865 | else if (type == LUA_TNUMBER) { |
| 1866 | wanted = lua_tointeger(L, 2); |
| 1867 | if (wanted < 0) |
| 1868 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1869 | } |
| 1870 | } |
| 1871 | |
| 1872 | /* Set pattern. */ |
| 1873 | lua_pushinteger(L, wanted); |
| 1874 | lua_replace(L, 2); |
| 1875 | |
| 1876 | /* init bufffer, and fiil it wih prefix. */ |
| 1877 | luaL_buffinit(L, &socket->b); |
| 1878 | |
| 1879 | /* Check prefix. */ |
| 1880 | if (lua_gettop(L) >= 3) { |
| 1881 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1882 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1883 | pattern = lua_tolstring(L, 3, &len); |
| 1884 | luaL_addlstring(&socket->b, pattern, len); |
| 1885 | } |
| 1886 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1887 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1888 | } |
| 1889 | |
| 1890 | /* Write the Lua input string in the output buffer. |
| 1891 | * This fucntion returns a yield if no space are available. |
| 1892 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1893 | static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1894 | { |
| 1895 | struct hlua_socket *socket; |
| 1896 | struct hlua *hlua = hlua_gethlua(L); |
| 1897 | struct appctx *appctx; |
| 1898 | size_t buf_len; |
| 1899 | const char *buf; |
| 1900 | int len; |
| 1901 | int send_len; |
| 1902 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1903 | struct xref *peer; |
| 1904 | struct stream_interface *si; |
| 1905 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1906 | |
| 1907 | /* Check if this lua stack is schedulable. */ |
| 1908 | if (!hlua || !hlua->task) |
| 1909 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1910 | "'frontend', 'backend' or 'task'")); |
| 1911 | |
| 1912 | /* Get object */ |
| 1913 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1914 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1915 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1916 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1917 | /* Check if we run on the same thread than the xreator thread. |
| 1918 | * We cannot access to the socket if the thread is different. |
| 1919 | */ |
| 1920 | if (socket->tid != tid) |
| 1921 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1922 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1923 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1924 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1925 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1926 | lua_pushinteger(L, -1); |
| 1927 | return 1; |
| 1928 | } |
| 1929 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1930 | si = appctx->owner; |
| 1931 | s = si_strm(si); |
| 1932 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1933 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1934 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1935 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1936 | lua_pushinteger(L, -1); |
| 1937 | return 1; |
| 1938 | } |
| 1939 | |
| 1940 | /* Update the input buffer data. */ |
| 1941 | buf += sent; |
| 1942 | send_len = buf_len - sent; |
| 1943 | |
| 1944 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1945 | if (sent >= buf_len) { |
| 1946 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1947 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1948 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1949 | |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1950 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 1951 | * the request buffer if its not required. |
| 1952 | */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1953 | if (s->req.buf->size == 0) { |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1954 | appctx = hlua->task->context; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1955 | if (!channel_alloc_buffer(&s->req, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1956 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1957 | } |
| 1958 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1959 | /* Check for avalaible space. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1960 | len = buffer_total_space(s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1961 | if (len <= 0) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1962 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1963 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 1964 | xref_unlock(&socket->xref, peer); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1965 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1966 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1967 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1968 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1969 | |
| 1970 | /* send data */ |
| 1971 | if (len < send_len) |
| 1972 | send_len = len; |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1973 | len = ci_putblk(&s->req, buf+sent, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | |
| 1975 | /* "Not enough space" (-1), "Buffer too little to contain |
| 1976 | * the data" (-2) are not expected because the available length |
| 1977 | * is tested. |
| 1978 | * Other unknown error are also not expected. |
| 1979 | */ |
| 1980 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1981 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1982 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1983 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1984 | MAY_LJMP(hlua_socket_close(L)); |
| 1985 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1986 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1987 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1988 | return 1; |
| 1989 | } |
| 1990 | |
| 1991 | /* update buffers. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1992 | stream_int_notify(&s->si[0]); |
| 1993 | stream_int_update_applet(&s->si[0]); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 1994 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1995 | s->req.rex = TICK_ETERNITY; |
| 1996 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1997 | |
| 1998 | /* Update length sent. */ |
| 1999 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2000 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2001 | |
| 2002 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2003 | if (sent + len >= buf_len) { |
| 2004 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2005 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2006 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2007 | |
| 2008 | hlua_socket_write_yield_return: |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2009 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2010 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2011 | return 0; |
| 2012 | } |
| 2013 | |
| 2014 | /* This function initiate the send of data. It just check the input |
| 2015 | * parameters and push an integer in the Lua stack that contain the |
| 2016 | * amount of data writed in the buffer. This is used by the function |
| 2017 | * "hlua_socket_write_yield" that can yield. |
| 2018 | * |
| 2019 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2020 | * the associated object. The second is a string buffer. The third is |
| 2021 | * a facultative integer that represents where is the buffer position |
| 2022 | * of the start of the data that can send. The first byte is the |
| 2023 | * position "1". The default value is "1". The fourth argument is a |
| 2024 | * facultative integer that represents where is the buffer position |
| 2025 | * of the end of the data that can send. The default is the last byte. |
| 2026 | */ |
| 2027 | static int hlua_socket_send(struct lua_State *L) |
| 2028 | { |
| 2029 | int i; |
| 2030 | int j; |
| 2031 | const char *buf; |
| 2032 | size_t buf_len; |
| 2033 | |
| 2034 | /* Check number of arguments. */ |
| 2035 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2036 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2037 | |
| 2038 | /* Get the string. */ |
| 2039 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2040 | |
| 2041 | /* Get and check j. */ |
| 2042 | if (lua_gettop(L) == 4) { |
| 2043 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2044 | if (j < 0) |
| 2045 | j = buf_len + j + 1; |
| 2046 | if (j > buf_len) |
| 2047 | j = buf_len + 1; |
| 2048 | lua_pop(L, 1); |
| 2049 | } |
| 2050 | else |
| 2051 | j = buf_len; |
| 2052 | |
| 2053 | /* Get and check i. */ |
| 2054 | if (lua_gettop(L) == 3) { |
| 2055 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2056 | if (i < 0) |
| 2057 | i = buf_len + i + 1; |
| 2058 | if (i > buf_len) |
| 2059 | i = buf_len + 1; |
| 2060 | lua_pop(L, 1); |
| 2061 | } else |
| 2062 | i = 1; |
| 2063 | |
| 2064 | /* Check bth i and j. */ |
| 2065 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2066 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2067 | return 1; |
| 2068 | } |
| 2069 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2070 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2071 | return 1; |
| 2072 | } |
| 2073 | if (i == 0) |
| 2074 | i = 1; |
| 2075 | if (j == 0) |
| 2076 | j = 1; |
| 2077 | |
| 2078 | /* Pop the string. */ |
| 2079 | lua_pop(L, 1); |
| 2080 | |
| 2081 | /* Update the buffer length. */ |
| 2082 | buf += i - 1; |
| 2083 | buf_len = j - i + 1; |
| 2084 | lua_pushlstring(L, buf, buf_len); |
| 2085 | |
| 2086 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2087 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2088 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2089 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2090 | } |
| 2091 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2092 | #define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345") |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2093 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2094 | { |
| 2095 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2096 | int ret; |
| 2097 | int len; |
| 2098 | char *p; |
| 2099 | |
| 2100 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2101 | if (ret <= 0) { |
| 2102 | lua_pushnil(L); |
| 2103 | return 1; |
| 2104 | } |
| 2105 | |
| 2106 | if (ret == AF_UNIX) { |
| 2107 | lua_pushstring(L, buffer+1); |
| 2108 | return 1; |
| 2109 | } |
| 2110 | else if (ret == AF_INET6) { |
| 2111 | buffer[0] = '['; |
| 2112 | len = strlen(buffer); |
| 2113 | buffer[len] = ']'; |
| 2114 | len++; |
| 2115 | buffer[len] = ':'; |
| 2116 | len++; |
| 2117 | p = buffer; |
| 2118 | } |
| 2119 | else if (ret == AF_INET) { |
| 2120 | p = buffer + 1; |
| 2121 | len = strlen(p); |
| 2122 | p[len] = ':'; |
| 2123 | len++; |
| 2124 | } |
| 2125 | else { |
| 2126 | lua_pushnil(L); |
| 2127 | return 1; |
| 2128 | } |
| 2129 | |
| 2130 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2131 | lua_pushnil(L); |
| 2132 | return 1; |
| 2133 | } |
| 2134 | |
| 2135 | lua_pushstring(L, p); |
| 2136 | return 1; |
| 2137 | } |
| 2138 | |
| 2139 | /* Returns information about the peer of the connection. */ |
| 2140 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2141 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2142 | struct hlua_socket *socket; |
| 2143 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2144 | struct xref *peer; |
| 2145 | struct appctx *appctx; |
| 2146 | struct stream_interface *si; |
| 2147 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2148 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2149 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2150 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2151 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2152 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2153 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2154 | /* Check if we run on the same thread than the xreator thread. |
| 2155 | * We cannot access to the socket if the thread is different. |
| 2156 | */ |
| 2157 | if (socket->tid != tid) |
| 2158 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2159 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2160 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2161 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2162 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2163 | lua_pushnil(L); |
| 2164 | return 1; |
| 2165 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2166 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2167 | si = appctx->owner; |
| 2168 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2169 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2170 | conn = objt_conn(s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2171 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2172 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2173 | lua_pushnil(L); |
| 2174 | return 1; |
| 2175 | } |
| 2176 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2177 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2178 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2179 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2180 | lua_pushnil(L); |
| 2181 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2182 | } |
| 2183 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2184 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2185 | xref_unlock(&socket->xref, peer); |
| 2186 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2187 | } |
| 2188 | |
| 2189 | /* Returns information about my connection side. */ |
| 2190 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2191 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2192 | struct hlua_socket *socket; |
| 2193 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2194 | struct appctx *appctx; |
| 2195 | struct xref *peer; |
| 2196 | struct stream_interface *si; |
| 2197 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2198 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2199 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2200 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2201 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2202 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2203 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2204 | /* Check if we run on the same thread than the xreator thread. |
| 2205 | * We cannot access to the socket if the thread is different. |
| 2206 | */ |
| 2207 | if (socket->tid != tid) |
| 2208 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2209 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2210 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2211 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2212 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2213 | lua_pushnil(L); |
| 2214 | return 1; |
| 2215 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2216 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2217 | si = appctx->owner; |
| 2218 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2219 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2220 | conn = objt_conn(s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2221 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2222 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2223 | lua_pushnil(L); |
| 2224 | return 1; |
| 2225 | } |
| 2226 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2227 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2228 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2229 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2230 | lua_pushnil(L); |
| 2231 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2232 | } |
| 2233 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2234 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2235 | xref_unlock(&socket->xref, peer); |
| 2236 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2237 | } |
| 2238 | |
| 2239 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2240 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2241 | .obj_type = OBJ_TYPE_APPLET, |
| 2242 | .name = "<LUA_TCP>", |
| 2243 | .fct = hlua_socket_handler, |
| 2244 | .release = hlua_socket_release, |
| 2245 | }; |
| 2246 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2247 | __LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2248 | { |
| 2249 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2250 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2251 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2252 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2253 | struct stream_interface *si; |
| 2254 | struct stream *s; |
| 2255 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2256 | /* Check if we run on the same thread than the xreator thread. |
| 2257 | * We cannot access to the socket if the thread is different. |
| 2258 | */ |
| 2259 | if (socket->tid != tid) |
| 2260 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2261 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2262 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2263 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2264 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2265 | lua_pushnil(L); |
| 2266 | lua_pushstring(L, "Can't connect"); |
| 2267 | return 2; |
| 2268 | } |
| 2269 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2270 | si = appctx->owner; |
| 2271 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2272 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2273 | /* Check if we run on the same thread than the xreator thread. |
| 2274 | * We cannot access to the socket if the thread is different. |
| 2275 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2276 | if (socket->tid != tid) { |
| 2277 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2278 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2279 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2280 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2281 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2282 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2283 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2284 | lua_pushnil(L); |
| 2285 | lua_pushstring(L, "Can't connect"); |
| 2286 | return 2; |
| 2287 | } |
| 2288 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2289 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2290 | |
| 2291 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2292 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2293 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2294 | lua_pushinteger(L, 1); |
| 2295 | return 1; |
| 2296 | } |
| 2297 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2298 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2299 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2301 | } |
| 2302 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2303 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2304 | return 0; |
| 2305 | } |
| 2306 | |
| 2307 | /* This function fail or initite the connection. */ |
| 2308 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2309 | { |
| 2310 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2311 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2312 | const char *ip; |
| 2313 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2314 | struct hlua *hlua; |
| 2315 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2316 | int low, high; |
| 2317 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2318 | struct xref *peer; |
| 2319 | struct stream_interface *si; |
| 2320 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2322 | if (lua_gettop(L) < 2) |
| 2323 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2324 | |
| 2325 | /* Get args. */ |
| 2326 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2327 | |
| 2328 | /* Check if we run on the same thread than the xreator thread. |
| 2329 | * We cannot access to the socket if the thread is different. |
| 2330 | */ |
| 2331 | if (socket->tid != tid) |
| 2332 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2333 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2334 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2335 | if (lua_gettop(L) >= 3) |
| 2336 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2337 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2338 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2339 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2340 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2341 | lua_pushnil(L); |
| 2342 | return 1; |
| 2343 | } |
| 2344 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2345 | si = appctx->owner; |
| 2346 | s = si_strm(si); |
| 2347 | |
| 2348 | /* Initialise connection. */ |
| 2349 | conn = si_alloc_conn(&s->si[1]); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2350 | if (!conn) { |
| 2351 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2352 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2353 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2354 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2355 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2356 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2357 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2358 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2359 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2360 | if (!addr) { |
| 2361 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2362 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2363 | } |
| 2364 | if (low != high) { |
| 2365 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2366 | 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] | 2367 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2368 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2369 | |
| 2370 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2371 | if (low == 0) { |
| 2372 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2373 | if (port == -1) { |
| 2374 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2375 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2376 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2377 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2378 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2379 | if (port == -1) { |
| 2380 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2381 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2382 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2383 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2384 | } |
| 2385 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2386 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2387 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2388 | appctx = objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2389 | |
| 2390 | /* inform the stream that we want to be notified whenever the |
| 2391 | * connection completes. |
| 2392 | */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2393 | si_applet_cant_get(&s->si[0]); |
| 2394 | si_applet_cant_put(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2395 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2396 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2397 | hlua->flags |= HLUA_MUST_GC; |
| 2398 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2399 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2400 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2401 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2402 | } |
| 2403 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2404 | 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] | 2405 | |
| 2406 | return 0; |
| 2407 | } |
| 2408 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2409 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2410 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2411 | { |
| 2412 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2413 | struct xref *peer; |
| 2414 | struct appctx *appctx; |
| 2415 | struct stream_interface *si; |
| 2416 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2417 | |
| 2418 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2419 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2420 | |
| 2421 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2422 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2423 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2424 | lua_pushnil(L); |
| 2425 | return 1; |
| 2426 | } |
| 2427 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2428 | si = appctx->owner; |
| 2429 | s = si_strm(si); |
| 2430 | |
| 2431 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2432 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2433 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2434 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2435 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2436 | |
| 2437 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2438 | { |
| 2439 | return 0; |
| 2440 | } |
| 2441 | |
| 2442 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2443 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2444 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2445 | int tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2446 | struct xref *peer; |
| 2447 | struct appctx *appctx; |
| 2448 | struct stream_interface *si; |
| 2449 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2450 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2451 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2452 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2453 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2454 | tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2455 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2456 | /* Check if we run on the same thread than the xreator thread. |
| 2457 | * We cannot access to the socket if the thread is different. |
| 2458 | */ |
| 2459 | if (socket->tid != tid) |
| 2460 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2461 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2462 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2463 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2464 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2465 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2466 | WILL_LJMP(lua_error(L)); |
| 2467 | return 0; |
| 2468 | } |
| 2469 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2470 | si = appctx->owner; |
| 2471 | s = si_strm(si); |
| 2472 | |
| 2473 | s->req.rto = tmout; |
| 2474 | s->req.wto = tmout; |
| 2475 | s->res.rto = tmout; |
| 2476 | s->res.wto = tmout; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2477 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2478 | |
| 2479 | return 0; |
| 2480 | } |
| 2481 | |
| 2482 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2483 | { |
| 2484 | struct hlua_socket *socket; |
| 2485 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2486 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2487 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2488 | |
| 2489 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2490 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2491 | hlua_pusherror(L, "socket: full stack"); |
| 2492 | goto out_fail_conf; |
| 2493 | } |
| 2494 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2495 | /* Create the object: obj[0] = userdata. */ |
| 2496 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2497 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2498 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2499 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2500 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2501 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2502 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2503 | if (!pool2_stream || !pool2_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2504 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2505 | goto out_fail_conf; |
| 2506 | } |
| 2507 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2508 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2509 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2510 | lua_setmetatable(L, -2); |
| 2511 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2512 | /* Create the applet context */ |
Thierry FOURNIER | 4325ab7 | 2017-07-12 11:53:38 +0200 | [diff] [blame] | 2513 | appctx = appctx_new(&update_applet, 1UL << tid); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2514 | if (!appctx) { |
| 2515 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2516 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2517 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2518 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2519 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2520 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2521 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2522 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2523 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2524 | /* Now create a session, task and stream for this applet */ |
| 2525 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2526 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2527 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2528 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2529 | } |
| 2530 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2531 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2532 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2533 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2534 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2535 | } |
| 2536 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2537 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2538 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2539 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2540 | /* Configure "right" stream interface. this "si" is used to connect |
| 2541 | * and retrieve data from the server. The connection is initialized |
| 2542 | * with the "struct server". |
| 2543 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2544 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2545 | |
| 2546 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2547 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2548 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2549 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2550 | task_wakeup(strm->task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2551 | /* Return yield waiting for connection. */ |
| 2552 | return 1; |
| 2553 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2554 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2555 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2556 | out_fail_sess: |
| 2557 | appctx_free(appctx); |
| 2558 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2559 | WILL_LJMP(lua_error(L)); |
| 2560 | return 0; |
| 2561 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2562 | |
| 2563 | /* |
| 2564 | * |
| 2565 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2566 | * Class Channel |
| 2567 | * |
| 2568 | * |
| 2569 | */ |
| 2570 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2571 | /* The state between the channel data and the HTTP parser state can be |
| 2572 | * unconsistent, so reset the parser and call it again. Warning, this |
| 2573 | * action not revalidate the request and not send a 400 if the modified |
| 2574 | * resuest is not valid. |
| 2575 | * |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2576 | * This function never fails. The direction is set using dir, which equals |
| 2577 | * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2578 | */ |
| 2579 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2580 | { |
| 2581 | /* Protocol HTTP. */ |
| 2582 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2583 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2584 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2585 | http_txn_reset_req(stream->txn); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2586 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2587 | http_txn_reset_res(stream->txn); |
| 2588 | |
| 2589 | if (stream->txn->hdr_idx.v) |
| 2590 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2591 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2592 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2593 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2594 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2595 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2596 | } |
| 2597 | } |
| 2598 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2599 | /* This function is called before the Lua execution. It stores |
| 2600 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2601 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2602 | 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] | 2603 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2604 | c->mode = stream->be->mode; |
| 2605 | switch (c->mode) { |
| 2606 | case PR_MODE_HTTP: |
| 2607 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2608 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2609 | c->data.http.state = stream->txn->req.msg_state; |
| 2610 | else |
| 2611 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2612 | break; |
| 2613 | default: |
| 2614 | break; |
| 2615 | } |
| 2616 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2617 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2618 | /* This function is called after the Lua execution. it |
| 2619 | * returns true if the parser state is consistent, otherwise, |
| 2620 | * it return false. |
| 2621 | * |
| 2622 | * In HTTP mode, the parser state must be in the same state |
| 2623 | * or greater when we exit the function. Even if we do a |
| 2624 | * control yield. This prevent to break the HTTP message |
| 2625 | * from the Lua code. |
| 2626 | */ |
| 2627 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2628 | { |
| 2629 | if (c->mode != stream->be->mode) |
| 2630 | return 0; |
| 2631 | |
| 2632 | switch (c->mode) { |
| 2633 | case PR_MODE_HTTP: |
| 2634 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2635 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2636 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2637 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2638 | else |
| 2639 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2640 | default: |
| 2641 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2642 | } |
| 2643 | return 1; |
| 2644 | } |
| 2645 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2646 | /* Returns the struct hlua_channel join to the class channel in the |
| 2647 | * stack entry "ud" or throws an argument error. |
| 2648 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2649 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2650 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2651 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2652 | } |
| 2653 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2654 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2655 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2656 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2657 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2658 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2659 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2660 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2661 | return 0; |
| 2662 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2663 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2664 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2665 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2666 | |
| 2667 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2668 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2669 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2670 | return 1; |
| 2671 | } |
| 2672 | |
| 2673 | /* Duplicate all the data present in the input channel and put it |
| 2674 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2675 | * the stack if the channel is closed and all the data are consumed, |
| 2676 | * returns 0 if no data are available, otherwise it returns the length |
| 2677 | * of the builded string. |
| 2678 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2679 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2680 | { |
| 2681 | char *blk1; |
| 2682 | char *blk2; |
| 2683 | int len1; |
| 2684 | int len2; |
| 2685 | int ret; |
| 2686 | luaL_Buffer b; |
| 2687 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2688 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2689 | if (unlikely(ret == 0)) |
| 2690 | return 0; |
| 2691 | |
| 2692 | if (unlikely(ret < 0)) { |
| 2693 | lua_pushnil(L); |
| 2694 | return -1; |
| 2695 | } |
| 2696 | |
| 2697 | luaL_buffinit(L, &b); |
| 2698 | luaL_addlstring(&b, blk1, len1); |
| 2699 | if (unlikely(ret == 2)) |
| 2700 | luaL_addlstring(&b, blk2, len2); |
| 2701 | luaL_pushresult(&b); |
| 2702 | |
| 2703 | if (unlikely(ret == 2)) |
| 2704 | return len1 + len2; |
| 2705 | return len1; |
| 2706 | } |
| 2707 | |
| 2708 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2709 | * a yield. This function keep the data in the buffer. |
| 2710 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2711 | __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] | 2712 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2713 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2714 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2715 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2716 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2717 | if (_hlua_channel_dup(chn, L) == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2718 | 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] | 2719 | return 1; |
| 2720 | } |
| 2721 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2722 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2723 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2724 | { |
| 2725 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2726 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2727 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2728 | } |
| 2729 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2730 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2731 | * a yield. This function consumes the data in the buffer. It returns |
| 2732 | * a string containing the data or a nil pointer if no data are available |
| 2733 | * and the channel is closed. |
| 2734 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2735 | __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] | 2736 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2737 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2738 | int ret; |
| 2739 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2740 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2741 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2742 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2743 | if (unlikely(ret == 0)) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2744 | 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] | 2745 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2746 | if (unlikely(ret == -1)) |
| 2747 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2748 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2749 | chn->buf->i -= ret; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2750 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2751 | return 1; |
| 2752 | } |
| 2753 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2754 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 2755 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2756 | { |
| 2757 | MAY_LJMP(check_args(L, 1, "get")); |
| 2758 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2759 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2760 | } |
| 2761 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2762 | /* This functions consumes and returns one line. If the channel is closed, |
| 2763 | * and the last data does not contains a final '\n', the data are returned |
| 2764 | * without the final '\n'. When no more data are avalaible, it returns nil |
| 2765 | * value. |
| 2766 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2767 | __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] | 2768 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2769 | char *blk1; |
| 2770 | char *blk2; |
| 2771 | int len1; |
| 2772 | int len2; |
| 2773 | int len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2774 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2775 | int ret; |
| 2776 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2777 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2778 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2779 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2780 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2781 | if (ret == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2782 | 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] | 2783 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2784 | if (ret == -1) { |
| 2785 | lua_pushnil(L); |
| 2786 | return 1; |
| 2787 | } |
| 2788 | |
| 2789 | luaL_buffinit(L, &b); |
| 2790 | luaL_addlstring(&b, blk1, len1); |
| 2791 | len = len1; |
| 2792 | if (unlikely(ret == 2)) { |
| 2793 | luaL_addlstring(&b, blk2, len2); |
| 2794 | len += len2; |
| 2795 | } |
| 2796 | luaL_pushresult(&b); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2797 | buffer_replace2(chn->buf, chn->buf->p, chn->buf->p + len, NULL, 0); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2798 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2799 | return 1; |
| 2800 | } |
| 2801 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2802 | /* Check arguments for the fucntion "hlua_channel_getline_yield". */ |
| 2803 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2804 | { |
| 2805 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2806 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2807 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2808 | } |
| 2809 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2810 | /* This function takes a string as input, and append it at the |
| 2811 | * input side of channel. If the data is too big, but a space |
| 2812 | * is probably available after sending some data, the function |
| 2813 | * yield. If the data is bigger than the buffer, or if the |
| 2814 | * channel is closed, it returns -1. otherwise, it returns the |
| 2815 | * amount of data writed. |
| 2816 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2817 | __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] | 2818 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2819 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2820 | size_t len; |
| 2821 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2822 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2823 | int ret; |
| 2824 | int max; |
| 2825 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2826 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2827 | * the request buffer if its not required. |
| 2828 | */ |
| 2829 | if (chn->buf->size == 0) { |
| 2830 | si_applet_cant_put(chn_prod(chn)); |
| 2831 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
| 2832 | } |
| 2833 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2834 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2835 | if (max > len - l) |
| 2836 | max = len - l; |
| 2837 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2838 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2839 | if (ret == -2 || ret == -3) { |
| 2840 | lua_pushinteger(L, -1); |
| 2841 | return 1; |
| 2842 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2843 | if (ret == -1) { |
| 2844 | chn->flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2845 | 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] | 2846 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2847 | l += ret; |
| 2848 | lua_pop(L, 1); |
| 2849 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2850 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2851 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2852 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
| 2853 | if (max == 0 && chn->buf->o == 0) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2854 | /* There are no space avalaible, and the output buffer is empty. |
| 2855 | * in this case, we cannot add more data, so we cannot yield, |
| 2856 | * we return the amount of copyied data. |
| 2857 | */ |
| 2858 | return 1; |
| 2859 | } |
| 2860 | if (l < len) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2861 | 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] | 2862 | return 1; |
| 2863 | } |
| 2864 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2865 | /* just a wrapper of "hlua_channel_append_yield". It returns the length |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2866 | * of the writed string, or -1 if the channel is closed or if the |
| 2867 | * buffer size is too little for the data. |
| 2868 | */ |
| 2869 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2870 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2871 | size_t len; |
| 2872 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2873 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2874 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2875 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2876 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2877 | lua_pushinteger(L, 0); |
| 2878 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2879 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2880 | } |
| 2881 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2882 | /* just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2883 | * his process by cleaning the buffer. The result is a replacement |
| 2884 | * of the current data. It returns the length of the writed string, |
| 2885 | * or -1 if the channel is closed or if the buffer size is too |
| 2886 | * little for the data. |
| 2887 | */ |
| 2888 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2889 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2890 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2891 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2892 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2893 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2894 | lua_pushinteger(L, 0); |
| 2895 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2896 | chn->buf->i = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2897 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2898 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2899 | } |
| 2900 | |
| 2901 | /* Append data in the output side of the buffer. This data is immediatly |
| 2902 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 2903 | * cannot contains the data, the function yield. The function returns -1 |
| 2904 | * if the channel is closed. |
| 2905 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2906 | __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] | 2907 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2908 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2909 | size_t len; |
| 2910 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2911 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2912 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2913 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2914 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2915 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2916 | lua_pushinteger(L, -1); |
| 2917 | return 1; |
| 2918 | } |
| 2919 | |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2920 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2921 | * the request buffer if its not required. |
| 2922 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2923 | if (chn->buf->size == 0) { |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2924 | si_applet_cant_put(chn_prod(chn)); |
| 2925 | 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] | 2926 | } |
| 2927 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2928 | /* the writed data will be immediatly sent, so we can check |
| 2929 | * the avalaible space without taking in account the reserve. |
| 2930 | * The reserve is guaranted for the processing of incoming |
| 2931 | * data, because the buffer will be flushed. |
| 2932 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2933 | max = chn->buf->size - buffer_len(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2934 | |
| 2935 | /* If there are no space avalaible, and the output buffer is empty. |
| 2936 | * in this case, we cannot add more data, so we cannot yield, |
| 2937 | * we return the amount of copyied data. |
| 2938 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2939 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2940 | return 1; |
| 2941 | |
| 2942 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2943 | if (max > len - l) |
| 2944 | max = len - l; |
| 2945 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2946 | /* The buffer avalaible size may be not contiguous. This test |
| 2947 | * detects a non contiguous buffer and realign it. |
| 2948 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2949 | if (bi_space_for_replace(chn->buf) < max) |
| 2950 | buffer_slow_realign(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2951 | |
| 2952 | /* Copy input data in the buffer. */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2953 | max = buffer_replace2(chn->buf, chn->buf->p, chn->buf->p, str + l, max); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2954 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2955 | /* buffer replace considers that the input part is filled. |
| 2956 | * so, I must forward these new data in the output part. |
| 2957 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2958 | b_adv(chn->buf, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2959 | |
| 2960 | l += max; |
| 2961 | lua_pop(L, 1); |
| 2962 | lua_pushinteger(L, l); |
| 2963 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2964 | /* If there are no space avalaible, and the output buffer is empty. |
| 2965 | * in this case, we cannot add more data, so we cannot yield, |
| 2966 | * we return the amount of copyied data. |
| 2967 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2968 | max = chn->buf->size - buffer_len(chn->buf); |
| 2969 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2970 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2971 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2972 | if (l < len) { |
| 2973 | /* If we are waiting for space in the response buffer, we |
| 2974 | * must set the flag WAKERESWR. This flag required the task |
| 2975 | * wake up if any activity is detected on the response buffer. |
| 2976 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2977 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2978 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2979 | else |
| 2980 | HLUA_SET_WAKEREQWR(hlua); |
| 2981 | 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] | 2982 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2983 | |
| 2984 | return 1; |
| 2985 | } |
| 2986 | |
| 2987 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 2988 | * yield the LUA process, and resume it without checking the |
| 2989 | * input arguments. |
| 2990 | */ |
| 2991 | __LJMP static int hlua_channel_send(lua_State *L) |
| 2992 | { |
| 2993 | MAY_LJMP(check_args(L, 2, "send")); |
| 2994 | lua_pushinteger(L, 0); |
| 2995 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2996 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | /* This function forward and amount of butes. The data pass from |
| 3000 | * the input side of the buffer to the output side, and can be |
| 3001 | * forwarded. This function never fails. |
| 3002 | * |
| 3003 | * The Lua function takes an amount of bytes to be forwarded in |
| 3004 | * imput. It returns the number of bytes forwarded. |
| 3005 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3006 | __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] | 3007 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3008 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3009 | int len; |
| 3010 | int l; |
| 3011 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3012 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3013 | |
| 3014 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3015 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3016 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3017 | |
| 3018 | max = len - l; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3019 | if (max > chn->buf->i) |
| 3020 | max = chn->buf->i; |
| 3021 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3022 | l += max; |
| 3023 | |
| 3024 | lua_pop(L, 1); |
| 3025 | lua_pushinteger(L, l); |
| 3026 | |
| 3027 | /* Check if it miss bytes to forward. */ |
| 3028 | if (l < len) { |
| 3029 | /* The the input channel or the output channel are closed, we |
| 3030 | * must return the amount of data forwarded. |
| 3031 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3032 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3033 | return 1; |
| 3034 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3035 | /* If we are waiting for space data in the response buffer, we |
| 3036 | * must set the flag WAKERESWR. This flag required the task |
| 3037 | * wake up if any activity is detected on the response buffer. |
| 3038 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3039 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3040 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3041 | else |
| 3042 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3043 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3044 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 3045 | 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] | 3046 | } |
| 3047 | |
| 3048 | return 1; |
| 3049 | } |
| 3050 | |
| 3051 | /* Just check the input and prepare the stack for the previous |
| 3052 | * function "hlua_channel_forward_yield" |
| 3053 | */ |
| 3054 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3055 | { |
| 3056 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3057 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3058 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3059 | |
| 3060 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3061 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | /* Just returns the number of bytes available in the input |
| 3065 | * side of the buffer. This function never fails. |
| 3066 | */ |
| 3067 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3068 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3069 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3070 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3071 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3072 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3073 | lua_pushinteger(L, chn->buf->i); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3074 | return 1; |
| 3075 | } |
| 3076 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3077 | /* Returns true if the channel is full. */ |
| 3078 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3079 | { |
| 3080 | struct channel *chn; |
| 3081 | int rem; |
| 3082 | |
| 3083 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3084 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3085 | |
| 3086 | rem = chn->buf->size; |
| 3087 | rem -= chn->buf->o; /* Output size */ |
| 3088 | rem -= chn->buf->i; /* Input size */ |
| 3089 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3090 | |
| 3091 | lua_pushboolean(L, rem <= 0); |
| 3092 | return 1; |
| 3093 | } |
| 3094 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3095 | /* Just returns the number of bytes available in the output |
| 3096 | * side of the buffer. This function never fails. |
| 3097 | */ |
| 3098 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3099 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3100 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3101 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3102 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3103 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3104 | lua_pushinteger(L, chn->buf->o); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3105 | return 1; |
| 3106 | } |
| 3107 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3108 | /* |
| 3109 | * |
| 3110 | * |
| 3111 | * Class Fetches |
| 3112 | * |
| 3113 | * |
| 3114 | */ |
| 3115 | |
| 3116 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3117 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3118 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3119 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3120 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3121 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3122 | } |
| 3123 | |
| 3124 | /* This function creates and push in the stack a fetch object according |
| 3125 | * with a current TXN. |
| 3126 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3127 | 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] | 3128 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3129 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3130 | |
| 3131 | /* Check stack size. */ |
| 3132 | if (!lua_checkstack(L, 3)) |
| 3133 | return 0; |
| 3134 | |
| 3135 | /* Create the object: obj[0] = userdata. |
| 3136 | * Note that the base of the Fetches object is the |
| 3137 | * transaction object. |
| 3138 | */ |
| 3139 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3140 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3141 | lua_rawseti(L, -2, 0); |
| 3142 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3143 | hsmp->s = txn->s; |
| 3144 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3145 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3146 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3147 | |
| 3148 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3149 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3150 | lua_setmetatable(L, -2); |
| 3151 | |
| 3152 | return 1; |
| 3153 | } |
| 3154 | |
| 3155 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3156 | * It uses closure argument to store the associated sample-fetch. It |
| 3157 | * returns only one argument or throws an error. An error is thrown |
| 3158 | * only if an error is encountered during the argument parsing. If |
| 3159 | * the "sample-fetch" function fails, nil is returned. |
| 3160 | */ |
| 3161 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3162 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3163 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3164 | struct sample_fetch *f; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3165 | struct arg args[ARGM_NBARGS + 1]; |
| 3166 | int i; |
| 3167 | struct sample smp; |
| 3168 | |
| 3169 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3170 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3171 | |
| 3172 | /* Get traditionnal arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3173 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3174 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3175 | /* Check execution authorization. */ |
| 3176 | if (f->use & SMP_USE_HTTP_ANY && |
| 3177 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3178 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3179 | "is not available in Lua services", f->kw); |
| 3180 | WILL_LJMP(lua_error(L)); |
| 3181 | } |
| 3182 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3183 | /* Get extra arguments. */ |
| 3184 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3185 | if (i >= ARGM_NBARGS) |
| 3186 | break; |
| 3187 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3188 | } |
| 3189 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 3190 | args[i].data.str.str = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3191 | |
| 3192 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3193 | 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] | 3194 | |
| 3195 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3196 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3197 | lua_pushfstring(L, "error in arguments"); |
| 3198 | WILL_LJMP(lua_error(L)); |
| 3199 | } |
| 3200 | |
| 3201 | /* Initialise the sample. */ |
| 3202 | memset(&smp, 0, sizeof(smp)); |
| 3203 | |
| 3204 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3205 | 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] | 3206 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3207 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3208 | lua_pushstring(L, ""); |
| 3209 | else |
| 3210 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3211 | return 1; |
| 3212 | } |
| 3213 | |
| 3214 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3215 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3216 | hlua_smp2lua_str(L, &smp); |
| 3217 | else |
| 3218 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3219 | return 1; |
| 3220 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3221 | |
| 3222 | /* |
| 3223 | * |
| 3224 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3225 | * Class Converters |
| 3226 | * |
| 3227 | * |
| 3228 | */ |
| 3229 | |
| 3230 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3231 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3232 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3233 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3234 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3235 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | /* This function creates and push in the stack a Converters object |
| 3239 | * according with a current TXN. |
| 3240 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3241 | 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] | 3242 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3243 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3244 | |
| 3245 | /* Check stack size. */ |
| 3246 | if (!lua_checkstack(L, 3)) |
| 3247 | return 0; |
| 3248 | |
| 3249 | /* Create the object: obj[0] = userdata. |
| 3250 | * Note that the base of the Converters object is the |
| 3251 | * same than the TXN object. |
| 3252 | */ |
| 3253 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3254 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3255 | lua_rawseti(L, -2, 0); |
| 3256 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3257 | hsmp->s = txn->s; |
| 3258 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3259 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3260 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3261 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3262 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3263 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3264 | lua_setmetatable(L, -2); |
| 3265 | |
| 3266 | return 1; |
| 3267 | } |
| 3268 | |
| 3269 | /* This function is an LUA binding. It is called with each converter. |
| 3270 | * It uses closure argument to store the associated converter. It |
| 3271 | * returns only one argument or throws an error. An error is thrown |
| 3272 | * only if an error is encountered during the argument parsing. If |
| 3273 | * the converter function function fails, nil is returned. |
| 3274 | */ |
| 3275 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3276 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3277 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3278 | struct sample_conv *conv; |
| 3279 | struct arg args[ARGM_NBARGS + 1]; |
| 3280 | int i; |
| 3281 | struct sample smp; |
| 3282 | |
| 3283 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3284 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3285 | |
| 3286 | /* Get traditionnal arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3287 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3288 | |
| 3289 | /* Get extra arguments. */ |
| 3290 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3291 | if (i >= ARGM_NBARGS) |
| 3292 | break; |
| 3293 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3294 | } |
| 3295 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 3296 | args[i].data.str.str = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3297 | |
| 3298 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3299 | 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] | 3300 | |
| 3301 | /* Run the special args checker. */ |
| 3302 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3303 | hlua_pusherror(L, "error in arguments"); |
| 3304 | WILL_LJMP(lua_error(L)); |
| 3305 | } |
| 3306 | |
| 3307 | /* Initialise the sample. */ |
| 3308 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3309 | hlua_pusherror(L, "error in the input argument"); |
| 3310 | WILL_LJMP(lua_error(L)); |
| 3311 | } |
| 3312 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3313 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3314 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3315 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3316 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3317 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3318 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3319 | WILL_LJMP(lua_error(L)); |
| 3320 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3321 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3322 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3323 | hlua_pusherror(L, "error during the input argument casting"); |
| 3324 | WILL_LJMP(lua_error(L)); |
| 3325 | } |
| 3326 | |
| 3327 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3328 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3329 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3330 | lua_pushstring(L, ""); |
| 3331 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3332 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3333 | return 1; |
| 3334 | } |
| 3335 | |
| 3336 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3337 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3338 | hlua_smp2lua_str(L, &smp); |
| 3339 | else |
| 3340 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3341 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3342 | } |
| 3343 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3344 | /* |
| 3345 | * |
| 3346 | * |
| 3347 | * Class AppletTCP |
| 3348 | * |
| 3349 | * |
| 3350 | */ |
| 3351 | |
| 3352 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3353 | * a class stream, otherwise it throws an error. |
| 3354 | */ |
| 3355 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3356 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3357 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | /* This function creates and push in the stack an Applet object |
| 3361 | * according with a current TXN. |
| 3362 | */ |
| 3363 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3364 | { |
| 3365 | struct hlua_appctx *appctx; |
| 3366 | struct stream_interface *si = ctx->owner; |
| 3367 | struct stream *s = si_strm(si); |
| 3368 | struct proxy *p = s->be; |
| 3369 | |
| 3370 | /* Check stack size. */ |
| 3371 | if (!lua_checkstack(L, 3)) |
| 3372 | return 0; |
| 3373 | |
| 3374 | /* Create the object: obj[0] = userdata. |
| 3375 | * Note that the base of the Converters object is the |
| 3376 | * same than the TXN object. |
| 3377 | */ |
| 3378 | lua_newtable(L); |
| 3379 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3380 | lua_rawseti(L, -2, 0); |
| 3381 | appctx->appctx = ctx; |
| 3382 | appctx->htxn.s = s; |
| 3383 | appctx->htxn.p = p; |
| 3384 | |
| 3385 | /* Create the "f" field that contains a list of fetches. */ |
| 3386 | lua_pushstring(L, "f"); |
| 3387 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3388 | return 0; |
| 3389 | lua_settable(L, -3); |
| 3390 | |
| 3391 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3392 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3393 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3394 | return 0; |
| 3395 | lua_settable(L, -3); |
| 3396 | |
| 3397 | /* Create the "c" field that contains a list of converters. */ |
| 3398 | lua_pushstring(L, "c"); |
| 3399 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3400 | return 0; |
| 3401 | lua_settable(L, -3); |
| 3402 | |
| 3403 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3404 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3405 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3406 | return 0; |
| 3407 | lua_settable(L, -3); |
| 3408 | |
| 3409 | /* Pop a class stream metatable and affect it to the table. */ |
| 3410 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3411 | lua_setmetatable(L, -2); |
| 3412 | |
| 3413 | return 1; |
| 3414 | } |
| 3415 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3416 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3417 | { |
| 3418 | struct hlua_appctx *appctx; |
| 3419 | struct stream *s; |
| 3420 | const char *name; |
| 3421 | size_t len; |
| 3422 | struct sample smp; |
| 3423 | |
| 3424 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3425 | |
| 3426 | /* It is useles to retrieve the stream, but this function |
| 3427 | * runs only in a stream context. |
| 3428 | */ |
| 3429 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3430 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3431 | s = appctx->htxn.s; |
| 3432 | |
| 3433 | /* Converts the third argument in a sample. */ |
| 3434 | hlua_lua2smp(L, 3, &smp); |
| 3435 | |
| 3436 | /* Store the sample in a variable. */ |
| 3437 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3438 | vars_set_by_name(name, len, &smp); |
| 3439 | return 0; |
| 3440 | } |
| 3441 | |
| 3442 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3443 | { |
| 3444 | struct hlua_appctx *appctx; |
| 3445 | struct stream *s; |
| 3446 | const char *name; |
| 3447 | size_t len; |
| 3448 | struct sample smp; |
| 3449 | |
| 3450 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3451 | |
| 3452 | /* It is useles to retrieve the stream, but this function |
| 3453 | * runs only in a stream context. |
| 3454 | */ |
| 3455 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3456 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3457 | s = appctx->htxn.s; |
| 3458 | |
| 3459 | /* Unset the variable. */ |
| 3460 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3461 | vars_unset_by_name(name, len, &smp); |
| 3462 | return 0; |
| 3463 | } |
| 3464 | |
| 3465 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3466 | { |
| 3467 | struct hlua_appctx *appctx; |
| 3468 | struct stream *s; |
| 3469 | const char *name; |
| 3470 | size_t len; |
| 3471 | struct sample smp; |
| 3472 | |
| 3473 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3474 | |
| 3475 | /* It is useles to retrieve the stream, but this function |
| 3476 | * runs only in a stream context. |
| 3477 | */ |
| 3478 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3479 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3480 | s = appctx->htxn.s; |
| 3481 | |
| 3482 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3483 | if (!vars_get_by_name(name, len, &smp)) { |
| 3484 | lua_pushnil(L); |
| 3485 | return 1; |
| 3486 | } |
| 3487 | |
| 3488 | return hlua_smp2lua(L, &smp); |
| 3489 | } |
| 3490 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3491 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3492 | { |
| 3493 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3494 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3495 | struct hlua *hlua; |
| 3496 | |
| 3497 | /* 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] | 3498 | if (!s->hlua) |
| 3499 | return 0; |
| 3500 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3501 | |
| 3502 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3503 | |
| 3504 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3505 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3506 | |
| 3507 | /* Get and store new value. */ |
| 3508 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3509 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3510 | |
| 3511 | return 0; |
| 3512 | } |
| 3513 | |
| 3514 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3515 | { |
| 3516 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3517 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3518 | struct hlua *hlua; |
| 3519 | |
| 3520 | /* 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] | 3521 | if (!s->hlua) { |
| 3522 | lua_pushnil(L); |
| 3523 | return 1; |
| 3524 | } |
| 3525 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3526 | |
| 3527 | /* Push configuration index in the stack. */ |
| 3528 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3529 | |
| 3530 | return 1; |
| 3531 | } |
| 3532 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3533 | /* If expected data not yet available, it returns a yield. This function |
| 3534 | * consumes the data in the buffer. It returns a string containing the |
| 3535 | * data. This string can be empty. |
| 3536 | */ |
| 3537 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3538 | { |
| 3539 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3540 | struct stream_interface *si = appctx->appctx->owner; |
| 3541 | int ret; |
| 3542 | char *blk1; |
| 3543 | int len1; |
| 3544 | char *blk2; |
| 3545 | int len2; |
| 3546 | |
| 3547 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3548 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3549 | |
| 3550 | /* Data not yet avalaible. return yield. */ |
| 3551 | if (ret == 0) { |
| 3552 | si_applet_cant_get(si); |
| 3553 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
| 3554 | } |
| 3555 | |
| 3556 | /* End of data: commit the total strings and return. */ |
| 3557 | if (ret < 0) { |
| 3558 | luaL_pushresult(&appctx->b); |
| 3559 | return 1; |
| 3560 | } |
| 3561 | |
| 3562 | /* Ensure that the block 2 length is usable. */ |
| 3563 | if (ret == 1) |
| 3564 | len2 = 0; |
| 3565 | |
| 3566 | /* dont check the max length read and dont check. */ |
| 3567 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3568 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3569 | |
| 3570 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3571 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3572 | luaL_pushresult(&appctx->b); |
| 3573 | return 1; |
| 3574 | } |
| 3575 | |
| 3576 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3577 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3578 | { |
| 3579 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3580 | |
| 3581 | /* Initialise the string catenation. */ |
| 3582 | luaL_buffinit(L, &appctx->b); |
| 3583 | |
| 3584 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3585 | } |
| 3586 | |
| 3587 | /* If expected data not yet available, it returns a yield. This function |
| 3588 | * consumes the data in the buffer. It returns a string containing the |
| 3589 | * data. This string can be empty. |
| 3590 | */ |
| 3591 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3592 | { |
| 3593 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3594 | struct stream_interface *si = appctx->appctx->owner; |
| 3595 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3596 | int ret; |
| 3597 | char *blk1; |
| 3598 | int len1; |
| 3599 | char *blk2; |
| 3600 | int len2; |
| 3601 | |
| 3602 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3603 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3604 | |
| 3605 | /* Data not yet avalaible. return yield. */ |
| 3606 | if (ret == 0) { |
| 3607 | si_applet_cant_get(si); |
| 3608 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3609 | } |
| 3610 | |
| 3611 | /* End of data: commit the total strings and return. */ |
| 3612 | if (ret < 0) { |
| 3613 | luaL_pushresult(&appctx->b); |
| 3614 | return 1; |
| 3615 | } |
| 3616 | |
| 3617 | /* Ensure that the block 2 length is usable. */ |
| 3618 | if (ret == 1) |
| 3619 | len2 = 0; |
| 3620 | |
| 3621 | if (len == -1) { |
| 3622 | |
| 3623 | /* If len == -1, catenate all the data avalaile and |
| 3624 | * yield because we want to get all the data until |
| 3625 | * the end of data stream. |
| 3626 | */ |
| 3627 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3628 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3629 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3630 | si_applet_cant_get(si); |
| 3631 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3632 | |
| 3633 | } else { |
| 3634 | |
| 3635 | /* Copy the fisrt block caping to the length required. */ |
| 3636 | if (len1 > len) |
| 3637 | len1 = len; |
| 3638 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3639 | len -= len1; |
| 3640 | |
| 3641 | /* Copy the second block. */ |
| 3642 | if (len2 > len) |
| 3643 | len2 = len; |
| 3644 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3645 | len -= len2; |
| 3646 | |
| 3647 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3648 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3649 | |
| 3650 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3651 | if (len > 0) { |
| 3652 | lua_pushinteger(L, len); |
| 3653 | lua_replace(L, 2); |
| 3654 | si_applet_cant_get(si); |
| 3655 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3656 | } |
| 3657 | |
| 3658 | /* return the result. */ |
| 3659 | luaL_pushresult(&appctx->b); |
| 3660 | return 1; |
| 3661 | } |
| 3662 | |
| 3663 | /* we never executes this */ |
| 3664 | hlua_pusherror(L, "Lua: internal error"); |
| 3665 | WILL_LJMP(lua_error(L)); |
| 3666 | return 0; |
| 3667 | } |
| 3668 | |
| 3669 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3670 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3671 | { |
| 3672 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3673 | int len = -1; |
| 3674 | |
| 3675 | if (lua_gettop(L) > 2) |
| 3676 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3677 | if (lua_gettop(L) >= 2) { |
| 3678 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3679 | lua_pop(L, 1); |
| 3680 | } |
| 3681 | |
| 3682 | /* Confirm or set the required length */ |
| 3683 | lua_pushinteger(L, len); |
| 3684 | |
| 3685 | /* Initialise the string catenation. */ |
| 3686 | luaL_buffinit(L, &appctx->b); |
| 3687 | |
| 3688 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3689 | } |
| 3690 | |
| 3691 | /* Append data in the output side of the buffer. This data is immediatly |
| 3692 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3693 | * cannot contains the data, the function yield. The function returns -1 |
| 3694 | * if the channel is closed. |
| 3695 | */ |
| 3696 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3697 | { |
| 3698 | size_t len; |
| 3699 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3700 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3701 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3702 | struct stream_interface *si = appctx->appctx->owner; |
| 3703 | struct channel *chn = si_ic(si); |
| 3704 | int max; |
| 3705 | |
| 3706 | /* Get the max amount of data which can write as input in the channel. */ |
| 3707 | max = channel_recv_max(chn); |
| 3708 | if (max > (len - l)) |
| 3709 | max = len - l; |
| 3710 | |
| 3711 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3712 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3713 | |
| 3714 | /* update counters. */ |
| 3715 | l += max; |
| 3716 | lua_pop(L, 1); |
| 3717 | lua_pushinteger(L, l); |
| 3718 | |
| 3719 | /* If some data is not send, declares the situation to the |
| 3720 | * applet, and returns a yield. |
| 3721 | */ |
| 3722 | if (l < len) { |
| 3723 | si_applet_cant_put(si); |
| 3724 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
| 3725 | } |
| 3726 | |
| 3727 | return 1; |
| 3728 | } |
| 3729 | |
| 3730 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3731 | * yield the LUA process, and resume it without checking the |
| 3732 | * input arguments. |
| 3733 | */ |
| 3734 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3735 | { |
| 3736 | MAY_LJMP(check_args(L, 2, "send")); |
| 3737 | lua_pushinteger(L, 0); |
| 3738 | |
| 3739 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3740 | } |
| 3741 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3742 | /* |
| 3743 | * |
| 3744 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3745 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3746 | * |
| 3747 | * |
| 3748 | */ |
| 3749 | |
| 3750 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3751 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3752 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3753 | __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] | 3754 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3755 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3756 | } |
| 3757 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3758 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3759 | * according with a current TXN. |
| 3760 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3761 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3762 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3763 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3764 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3765 | struct stream_interface *si = ctx->owner; |
| 3766 | struct stream *s = si_strm(si); |
| 3767 | struct proxy *px = s->be; |
| 3768 | struct http_txn *txn = s->txn; |
| 3769 | const char *path; |
| 3770 | const char *end; |
| 3771 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3772 | |
| 3773 | /* Check stack size. */ |
| 3774 | if (!lua_checkstack(L, 3)) |
| 3775 | return 0; |
| 3776 | |
| 3777 | /* Create the object: obj[0] = userdata. |
| 3778 | * Note that the base of the Converters object is the |
| 3779 | * same than the TXN object. |
| 3780 | */ |
| 3781 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3782 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3783 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3784 | appctx->appctx = ctx; |
| 3785 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3786 | 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] | 3787 | appctx->htxn.s = s; |
| 3788 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3789 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3790 | /* Create the "f" field that contains a list of fetches. */ |
| 3791 | lua_pushstring(L, "f"); |
| 3792 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3793 | return 0; |
| 3794 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3795 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3796 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3797 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3798 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3799 | return 0; |
| 3800 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3801 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3802 | /* Create the "c" field that contains a list of converters. */ |
| 3803 | lua_pushstring(L, "c"); |
| 3804 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3805 | return 0; |
| 3806 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3807 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3808 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3809 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3810 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3811 | return 0; |
| 3812 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3813 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3814 | /* Stores the request method. */ |
| 3815 | lua_pushstring(L, "method"); |
| 3816 | lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l); |
| 3817 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3818 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3819 | /* Stores the http version. */ |
| 3820 | lua_pushstring(L, "version"); |
| 3821 | lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
| 3822 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3823 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3824 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3825 | * the array on the top of the stack. |
| 3826 | */ |
| 3827 | lua_pushstring(L, "headers"); |
| 3828 | htxn.s = s; |
| 3829 | htxn.p = px; |
| 3830 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3831 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3832 | return 0; |
| 3833 | lua_settable(L, -3); |
| 3834 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3835 | /* Get path and qs */ |
| 3836 | path = http_get_path(txn); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3837 | if (path) { |
| 3838 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 3839 | p = path; |
| 3840 | while (p < end && *p != '?') |
| 3841 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3842 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3843 | /* Stores the request path. */ |
| 3844 | lua_pushstring(L, "path"); |
| 3845 | lua_pushlstring(L, path, p - path); |
| 3846 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3847 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3848 | /* Stores the query string. */ |
| 3849 | lua_pushstring(L, "qs"); |
| 3850 | if (*p == '?') |
| 3851 | p++; |
| 3852 | lua_pushlstring(L, p, end - p); |
| 3853 | lua_settable(L, -3); |
| 3854 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3855 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3856 | /* Stores the request path. */ |
| 3857 | lua_pushstring(L, "length"); |
| 3858 | lua_pushinteger(L, txn->req.body_len); |
| 3859 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3860 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3861 | /* Create an array of HTTP request headers. */ |
| 3862 | lua_pushstring(L, "headers"); |
| 3863 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3864 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3865 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3866 | /* Create an empty array of HTTP request headers. */ |
| 3867 | lua_pushstring(L, "response"); |
| 3868 | lua_newtable(L); |
| 3869 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3870 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3871 | /* Pop a class stream metatable and affect it to the table. */ |
| 3872 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3873 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3874 | |
| 3875 | return 1; |
| 3876 | } |
| 3877 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3878 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3879 | { |
| 3880 | struct hlua_appctx *appctx; |
| 3881 | struct stream *s; |
| 3882 | const char *name; |
| 3883 | size_t len; |
| 3884 | struct sample smp; |
| 3885 | |
| 3886 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3887 | |
| 3888 | /* It is useles to retrieve the stream, but this function |
| 3889 | * runs only in a stream context. |
| 3890 | */ |
| 3891 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3892 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3893 | s = appctx->htxn.s; |
| 3894 | |
| 3895 | /* Converts the third argument in a sample. */ |
| 3896 | hlua_lua2smp(L, 3, &smp); |
| 3897 | |
| 3898 | /* Store the sample in a variable. */ |
| 3899 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3900 | vars_set_by_name(name, len, &smp); |
| 3901 | return 0; |
| 3902 | } |
| 3903 | |
| 3904 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 3905 | { |
| 3906 | struct hlua_appctx *appctx; |
| 3907 | struct stream *s; |
| 3908 | const char *name; |
| 3909 | size_t len; |
| 3910 | struct sample smp; |
| 3911 | |
| 3912 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3913 | |
| 3914 | /* It is useles to retrieve the stream, but this function |
| 3915 | * runs only in a stream context. |
| 3916 | */ |
| 3917 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3918 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3919 | s = appctx->htxn.s; |
| 3920 | |
| 3921 | /* Unset the variable. */ |
| 3922 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3923 | vars_unset_by_name(name, len, &smp); |
| 3924 | return 0; |
| 3925 | } |
| 3926 | |
| 3927 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 3928 | { |
| 3929 | struct hlua_appctx *appctx; |
| 3930 | struct stream *s; |
| 3931 | const char *name; |
| 3932 | size_t len; |
| 3933 | struct sample smp; |
| 3934 | |
| 3935 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3936 | |
| 3937 | /* It is useles to retrieve the stream, but this function |
| 3938 | * runs only in a stream context. |
| 3939 | */ |
| 3940 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3941 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3942 | s = appctx->htxn.s; |
| 3943 | |
| 3944 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3945 | if (!vars_get_by_name(name, len, &smp)) { |
| 3946 | lua_pushnil(L); |
| 3947 | return 1; |
| 3948 | } |
| 3949 | |
| 3950 | return hlua_smp2lua(L, &smp); |
| 3951 | } |
| 3952 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3953 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 3954 | { |
| 3955 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3956 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3957 | struct hlua *hlua; |
| 3958 | |
| 3959 | /* 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] | 3960 | if (!s->hlua) |
| 3961 | return 0; |
| 3962 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3963 | |
| 3964 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3965 | |
| 3966 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3967 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3968 | |
| 3969 | /* Get and store new value. */ |
| 3970 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3971 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3972 | |
| 3973 | return 0; |
| 3974 | } |
| 3975 | |
| 3976 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 3977 | { |
| 3978 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3979 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3980 | struct hlua *hlua; |
| 3981 | |
| 3982 | /* 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] | 3983 | if (!s->hlua) { |
| 3984 | lua_pushnil(L); |
| 3985 | return 1; |
| 3986 | } |
| 3987 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3988 | |
| 3989 | /* Push configuration index in the stack. */ |
| 3990 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3991 | |
| 3992 | return 1; |
| 3993 | } |
| 3994 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3995 | /* If expected data not yet available, it returns a yield. This function |
| 3996 | * consumes the data in the buffer. It returns a string containing the |
| 3997 | * data. This string can be empty. |
| 3998 | */ |
| 3999 | __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] | 4000 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4001 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4002 | struct stream_interface *si = appctx->appctx->owner; |
| 4003 | struct channel *chn = si_ic(si); |
| 4004 | int ret; |
| 4005 | char *blk1; |
| 4006 | int len1; |
| 4007 | char *blk2; |
| 4008 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4009 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4010 | /* Maybe we cant send a 100-continue ? */ |
| 4011 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4012 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4013 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4014 | * big for the buffers. We cant send anything. So, we ignoring |
| 4015 | * the error, considers that the 100-continue is sent, and try |
| 4016 | * to receive. |
| 4017 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4018 | */ |
| 4019 | if (ret == -1) { |
| 4020 | si_applet_cant_put(si); |
| 4021 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 4022 | } |
| 4023 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4024 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4025 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4026 | /* Check for the end of the data. */ |
| 4027 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4028 | luaL_pushresult(&appctx->b); |
| 4029 | return 1; |
| 4030 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4031 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4032 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4033 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4034 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4035 | /* Data not yet avalaible. return yield. */ |
| 4036 | if (ret == 0) { |
| 4037 | si_applet_cant_get(si); |
| 4038 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 4039 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4040 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4041 | /* End of data: commit the total strings and return. */ |
| 4042 | if (ret < 0) { |
| 4043 | luaL_pushresult(&appctx->b); |
| 4044 | return 1; |
| 4045 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4046 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4047 | /* Ensure that the block 2 length is usable. */ |
| 4048 | if (ret == 1) |
| 4049 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4050 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4051 | /* Copy the fisrt block caping to the length required. */ |
| 4052 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4053 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4054 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4055 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4056 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4057 | /* Copy the second block. */ |
| 4058 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4059 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4060 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4061 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4062 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4063 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4064 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4065 | luaL_pushresult(&appctx->b); |
| 4066 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4067 | } |
| 4068 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4069 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 4070 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4071 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4072 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4073 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4074 | /* Initialise the string catenation. */ |
| 4075 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4076 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4077 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4078 | } |
| 4079 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4080 | /* If expected data not yet available, it returns a yield. This function |
| 4081 | * consumes the data in the buffer. It returns a string containing the |
| 4082 | * data. This string can be empty. |
| 4083 | */ |
| 4084 | __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] | 4085 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4086 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4087 | struct stream_interface *si = appctx->appctx->owner; |
| 4088 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4089 | struct channel *chn = si_ic(si); |
| 4090 | int ret; |
| 4091 | char *blk1; |
| 4092 | int len1; |
| 4093 | char *blk2; |
| 4094 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4095 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4096 | /* Maybe we cant send a 100-continue ? */ |
| 4097 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4098 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4099 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4100 | * big for the buffers. We cant send anything. So, we ignoring |
| 4101 | * the error, considers that the 100-continue is sent, and try |
| 4102 | * to receive. |
| 4103 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4104 | */ |
| 4105 | if (ret == -1) { |
| 4106 | si_applet_cant_put(si); |
| 4107 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4108 | } |
| 4109 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4110 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4111 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4112 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4113 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4114 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4115 | /* Data not yet avalaible. return yield. */ |
| 4116 | if (ret == 0) { |
| 4117 | si_applet_cant_get(si); |
| 4118 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4119 | } |
| 4120 | |
| 4121 | /* End of data: commit the total strings and return. */ |
| 4122 | if (ret < 0) { |
| 4123 | luaL_pushresult(&appctx->b); |
| 4124 | return 1; |
| 4125 | } |
| 4126 | |
| 4127 | /* Ensure that the block 2 length is usable. */ |
| 4128 | if (ret == 1) |
| 4129 | len2 = 0; |
| 4130 | |
| 4131 | /* Copy the fisrt block caping to the length required. */ |
| 4132 | if (len1 > len) |
| 4133 | len1 = len; |
| 4134 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4135 | len -= len1; |
| 4136 | |
| 4137 | /* Copy the second block. */ |
| 4138 | if (len2 > len) |
| 4139 | len2 = len; |
| 4140 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4141 | len -= len2; |
| 4142 | |
| 4143 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4144 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4145 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4146 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4147 | |
| 4148 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 4149 | if (len > 0) { |
| 4150 | lua_pushinteger(L, len); |
| 4151 | lua_replace(L, 2); |
| 4152 | si_applet_cant_get(si); |
| 4153 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4154 | } |
| 4155 | |
| 4156 | /* return the result. */ |
| 4157 | luaL_pushresult(&appctx->b); |
| 4158 | return 1; |
| 4159 | } |
| 4160 | |
| 4161 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 4162 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4163 | { |
| 4164 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4165 | int len = -1; |
| 4166 | |
| 4167 | /* Check arguments. */ |
| 4168 | if (lua_gettop(L) > 2) |
| 4169 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4170 | if (lua_gettop(L) >= 2) { |
| 4171 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4172 | lua_pop(L, 1); |
| 4173 | } |
| 4174 | |
| 4175 | /* Check the required length */ |
| 4176 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4177 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4178 | lua_pushinteger(L, len); |
| 4179 | |
| 4180 | /* Initialise the string catenation. */ |
| 4181 | luaL_buffinit(L, &appctx->b); |
| 4182 | |
| 4183 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4184 | } |
| 4185 | |
| 4186 | /* Append data in the output side of the buffer. This data is immediatly |
| 4187 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 4188 | * cannot contains the data, the function yield. The function returns -1 |
| 4189 | * if the channel is closed. |
| 4190 | */ |
| 4191 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4192 | { |
| 4193 | size_t len; |
| 4194 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4195 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4196 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4197 | struct stream_interface *si = appctx->appctx->owner; |
| 4198 | struct channel *chn = si_ic(si); |
| 4199 | int max; |
| 4200 | |
| 4201 | /* Get the max amount of data which can write as input in the channel. */ |
| 4202 | max = channel_recv_max(chn); |
| 4203 | if (max > (len - l)) |
| 4204 | max = len - l; |
| 4205 | |
| 4206 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4207 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4208 | |
| 4209 | /* update counters. */ |
| 4210 | l += max; |
| 4211 | lua_pop(L, 1); |
| 4212 | lua_pushinteger(L, l); |
| 4213 | |
| 4214 | /* If some data is not send, declares the situation to the |
| 4215 | * applet, and returns a yield. |
| 4216 | */ |
| 4217 | if (l < len) { |
| 4218 | si_applet_cant_put(si); |
| 4219 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
| 4220 | } |
| 4221 | |
| 4222 | return 1; |
| 4223 | } |
| 4224 | |
| 4225 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4226 | * yield the LUA process, and resume it without checking the |
| 4227 | * input arguments. |
| 4228 | */ |
| 4229 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4230 | { |
| 4231 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4232 | size_t len; |
| 4233 | char hex[10]; |
| 4234 | |
| 4235 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4236 | |
| 4237 | /* If transfer encoding chunked is selected, we surround the data |
| 4238 | * by chunk data. |
| 4239 | */ |
| 4240 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4241 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4242 | lua_pushfstring(L, "%s\r\n", hex); |
| 4243 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4244 | lua_pushstring(L, "\r\n"); |
| 4245 | lua_concat(L, 3); |
| 4246 | } |
| 4247 | |
| 4248 | /* This interger is used for followinf the amount of data sent. */ |
| 4249 | lua_pushinteger(L, 0); |
| 4250 | |
| 4251 | /* We want to send some data. Headers must be sent. */ |
| 4252 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4253 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4254 | WILL_LJMP(lua_error(L)); |
| 4255 | } |
| 4256 | |
| 4257 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4258 | } |
| 4259 | |
| 4260 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4261 | { |
| 4262 | const char *name; |
| 4263 | int ret; |
| 4264 | |
| 4265 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4266 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4267 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4268 | |
| 4269 | /* Push in the stack the "response" entry. */ |
| 4270 | ret = lua_getfield(L, 1, "response"); |
| 4271 | if (ret != LUA_TTABLE) { |
| 4272 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4273 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4274 | WILL_LJMP(lua_error(L)); |
| 4275 | } |
| 4276 | |
| 4277 | /* check if the header is already registered if it is not |
| 4278 | * the case, register it. |
| 4279 | */ |
| 4280 | ret = lua_getfield(L, -1, name); |
| 4281 | if (ret == LUA_TNIL) { |
| 4282 | |
| 4283 | /* Entry not found. */ |
| 4284 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4285 | |
| 4286 | /* Insert the new header name in the array in the top of the stack. |
| 4287 | * It left the new array in the top of the stack. |
| 4288 | */ |
| 4289 | lua_newtable(L); |
| 4290 | lua_pushvalue(L, 2); |
| 4291 | lua_pushvalue(L, -2); |
| 4292 | lua_settable(L, -4); |
| 4293 | |
| 4294 | } else if (ret != LUA_TTABLE) { |
| 4295 | |
| 4296 | /* corruption error. */ |
| 4297 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4298 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4299 | WILL_LJMP(lua_error(L)); |
| 4300 | } |
| 4301 | |
| 4302 | /* Now the top od thestack is an array of values. We push |
| 4303 | * the header value as new entry. |
| 4304 | */ |
| 4305 | lua_pushvalue(L, 3); |
| 4306 | ret = lua_rawlen(L, -2); |
| 4307 | lua_rawseti(L, -2, ret + 1); |
| 4308 | lua_pushboolean(L, 1); |
| 4309 | return 1; |
| 4310 | } |
| 4311 | |
| 4312 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4313 | { |
| 4314 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4315 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4316 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4317 | |
| 4318 | if (status < 100 || status > 599) { |
| 4319 | lua_pushboolean(L, 0); |
| 4320 | return 1; |
| 4321 | } |
| 4322 | |
| 4323 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4324 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4325 | lua_pushboolean(L, 1); |
| 4326 | return 1; |
| 4327 | } |
| 4328 | |
| 4329 | /* We will build the status line and the headers of the HTTP response. |
| 4330 | * We will try send at once if its not possible, we give back the hand |
| 4331 | * waiting for more room. |
| 4332 | */ |
| 4333 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4334 | { |
| 4335 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4336 | struct stream_interface *si = appctx->appctx->owner; |
| 4337 | struct channel *chn = si_ic(si); |
| 4338 | int ret; |
| 4339 | size_t len; |
| 4340 | const char *msg; |
| 4341 | |
| 4342 | /* Get the message as the first argument on the stack. */ |
| 4343 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4344 | |
| 4345 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4346 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4347 | |
| 4348 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4349 | * big for the buffers. |
| 4350 | */ |
| 4351 | if (ret == -2 || ret == -3) { |
| 4352 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4353 | WILL_LJMP(lua_error(L)); |
| 4354 | } |
| 4355 | |
| 4356 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4357 | if (ret == -1) { |
| 4358 | si_applet_cant_put(si); |
| 4359 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
| 4360 | } |
| 4361 | |
| 4362 | /* Headers sent, set the flag. */ |
| 4363 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4364 | return 0; |
| 4365 | } |
| 4366 | |
| 4367 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4368 | { |
| 4369 | struct chunk *tmp = get_trash_chunk(); |
| 4370 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4371 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4372 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4373 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4374 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4375 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4376 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4377 | int hdr_chunked = 0; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4378 | const char *reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4379 | |
| 4380 | if (reason == NULL) |
| 4381 | reason = get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4382 | |
| 4383 | /* Use the same http version than the request. */ |
| 4384 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4385 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4386 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4387 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4388 | |
| 4389 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4390 | lua_pushvalue(L, 0); |
| 4391 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4392 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4393 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4394 | WILL_LJMP(lua_error(L)); |
| 4395 | } |
| 4396 | |
| 4397 | /* Browse the list of headers. */ |
| 4398 | lua_pushnil(L); |
| 4399 | while(lua_next(L, -2) != 0) { |
| 4400 | |
| 4401 | /* We expect a string as -2. */ |
| 4402 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4403 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4404 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4405 | lua_typename(L, lua_type(L, -2))); |
| 4406 | WILL_LJMP(lua_error(L)); |
| 4407 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4408 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4409 | |
| 4410 | /* We expect an array as -1. */ |
| 4411 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4412 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4413 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4414 | name, |
| 4415 | lua_typename(L, lua_type(L, -1))); |
| 4416 | WILL_LJMP(lua_error(L)); |
| 4417 | } |
| 4418 | |
| 4419 | /* Browse the table who is on the top of the stack. */ |
| 4420 | lua_pushnil(L); |
| 4421 | while(lua_next(L, -2) != 0) { |
| 4422 | |
| 4423 | /* We expect a number as -2. */ |
| 4424 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4425 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4426 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4427 | name, |
| 4428 | lua_typename(L, lua_type(L, -2))); |
| 4429 | WILL_LJMP(lua_error(L)); |
| 4430 | } |
| 4431 | id = lua_tointeger(L, -2); |
| 4432 | |
| 4433 | /* We expect a string as -2. */ |
| 4434 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4435 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4436 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4437 | name, id, |
| 4438 | lua_typename(L, lua_type(L, -1))); |
| 4439 | WILL_LJMP(lua_error(L)); |
| 4440 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4441 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4442 | |
| 4443 | /* Catenate a new header. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4444 | if (tmp->len + name_len + 2 + value_len + 2 < tmp->size) { |
| 4445 | memcpy(tmp->str + tmp->len, name, name_len); |
| 4446 | tmp->len += name_len; |
| 4447 | tmp->str[tmp->len++] = ':'; |
| 4448 | tmp->str[tmp->len++] = ' '; |
| 4449 | |
| 4450 | memcpy(tmp->str + tmp->len, value, value_len); |
| 4451 | tmp->len += value_len; |
| 4452 | tmp->str[tmp->len++] = '\r'; |
| 4453 | tmp->str[tmp->len++] = '\n'; |
| 4454 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4455 | |
| 4456 | /* Protocol checks. */ |
| 4457 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4458 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4459 | * is done without control. If it contains a bad value, |
| 4460 | * the content-length remains negative so that we can |
| 4461 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4462 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4463 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4464 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4465 | |
| 4466 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4467 | if (name_len == 17 && value_len == 7 && |
| 4468 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4469 | strcasecmp("chunked", value) == 0) |
| 4470 | hdr_chunked = 1; |
| 4471 | |
| 4472 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4473 | lua_pop(L, 1); |
| 4474 | } |
| 4475 | |
| 4476 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4477 | lua_pop(L, 1); |
| 4478 | } |
| 4479 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4480 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4481 | * and the status code implies the presence of a message body, we must |
| 4482 | * announce a transfer encoding chunked. This is required by haproxy |
| 4483 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4484 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4485 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4486 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4487 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 4488 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4489 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4490 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4491 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4492 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4493 | } |
| 4494 | |
| 4495 | /* Finalize headers. */ |
| 4496 | chunk_appendf(tmp, "\r\n"); |
| 4497 | |
| 4498 | /* Remove the last entry and the array of headers */ |
| 4499 | lua_pop(L, 2); |
| 4500 | |
| 4501 | /* Push the headers block. */ |
| 4502 | lua_pushlstring(L, tmp->str, tmp->len); |
| 4503 | |
| 4504 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4505 | } |
| 4506 | |
| 4507 | /* |
| 4508 | * |
| 4509 | * |
| 4510 | * Class HTTP |
| 4511 | * |
| 4512 | * |
| 4513 | */ |
| 4514 | |
| 4515 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4516 | * a class stream, otherwise it throws an error. |
| 4517 | */ |
| 4518 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4519 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4520 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4521 | } |
| 4522 | |
| 4523 | /* This function creates and push in the stack a HTTP object |
| 4524 | * according with a current TXN. |
| 4525 | */ |
| 4526 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4527 | { |
| 4528 | struct hlua_txn *htxn; |
| 4529 | |
| 4530 | /* Check stack size. */ |
| 4531 | if (!lua_checkstack(L, 3)) |
| 4532 | return 0; |
| 4533 | |
| 4534 | /* Create the object: obj[0] = userdata. |
| 4535 | * Note that the base of the Converters object is the |
| 4536 | * same than the TXN object. |
| 4537 | */ |
| 4538 | lua_newtable(L); |
| 4539 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4540 | lua_rawseti(L, -2, 0); |
| 4541 | |
| 4542 | htxn->s = txn->s; |
| 4543 | htxn->p = txn->p; |
| 4544 | |
| 4545 | /* Pop a class stream metatable and affect it to the table. */ |
| 4546 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4547 | lua_setmetatable(L, -2); |
| 4548 | |
| 4549 | return 1; |
| 4550 | } |
| 4551 | |
| 4552 | /* This function creates ans returns an array of HTTP headers. |
| 4553 | * This function does not fails. It is used as wrapper with the |
| 4554 | * 2 following functions. |
| 4555 | */ |
| 4556 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4557 | { |
| 4558 | const char *cur_ptr, *cur_next, *p; |
| 4559 | int old_idx, cur_idx; |
| 4560 | struct hdr_idx_elem *cur_hdr; |
| 4561 | const char *hn, *hv; |
| 4562 | int hnl, hvl; |
| 4563 | int type; |
| 4564 | const char *in; |
| 4565 | char *out; |
| 4566 | int len; |
| 4567 | |
| 4568 | /* Create the table. */ |
| 4569 | lua_newtable(L); |
| 4570 | |
| 4571 | if (!htxn->s->txn) |
| 4572 | return 1; |
| 4573 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4574 | /* Check if a valid response is parsed */ |
| 4575 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4576 | return 1; |
| 4577 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4578 | /* Build array of headers. */ |
| 4579 | old_idx = 0; |
| 4580 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 4581 | |
| 4582 | while (1) { |
| 4583 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4584 | if (!cur_idx) |
| 4585 | break; |
| 4586 | old_idx = cur_idx; |
| 4587 | |
| 4588 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4589 | cur_ptr = cur_next; |
| 4590 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4591 | |
| 4592 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4593 | * and the next header starts at cur_next. We'll check |
| 4594 | * this header in the list as well as against the default |
| 4595 | * rule. |
| 4596 | */ |
| 4597 | |
| 4598 | /* look for ': *'. */ |
| 4599 | hn = cur_ptr; |
| 4600 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4601 | if (p >= cur_ptr+cur_hdr->len) |
| 4602 | continue; |
| 4603 | hnl = p - hn; |
| 4604 | p++; |
| 4605 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4606 | p++; |
| 4607 | if (p >= cur_ptr+cur_hdr->len) |
| 4608 | continue; |
| 4609 | hv = p; |
| 4610 | hvl = cur_ptr+cur_hdr->len-p; |
| 4611 | |
| 4612 | /* Lowercase the key. Don't check the size of trash, it have |
| 4613 | * the size of one buffer and the input data contains in one |
| 4614 | * buffer. |
| 4615 | */ |
| 4616 | out = trash.str; |
| 4617 | for (in=hn; in<hn+hnl; in++, out++) |
| 4618 | *out = tolower(*in); |
| 4619 | *out = '\0'; |
| 4620 | |
| 4621 | /* Check for existing entry: |
| 4622 | * assume that the table is on the top of the stack, and |
| 4623 | * push the key in the stack, the function lua_gettable() |
| 4624 | * perform the lookup. |
| 4625 | */ |
| 4626 | lua_pushlstring(L, trash.str, hnl); |
| 4627 | lua_gettable(L, -2); |
| 4628 | type = lua_type(L, -1); |
| 4629 | |
| 4630 | switch (type) { |
| 4631 | case LUA_TNIL: |
| 4632 | /* Table not found, create it. */ |
| 4633 | lua_pop(L, 1); /* remove the nil value. */ |
| 4634 | lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */ |
| 4635 | lua_newtable(L); /* create and push empty table. */ |
| 4636 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4637 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4638 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4639 | break; |
| 4640 | |
| 4641 | case LUA_TTABLE: |
| 4642 | /* Entry found: push the value in the table. */ |
| 4643 | len = lua_rawlen(L, -1); |
| 4644 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4645 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4646 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4647 | break; |
| 4648 | |
| 4649 | default: |
| 4650 | /* Other cases are errors. */ |
| 4651 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4652 | WILL_LJMP(lua_error(L)); |
| 4653 | } |
| 4654 | } |
| 4655 | |
| 4656 | return 1; |
| 4657 | } |
| 4658 | |
| 4659 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4660 | { |
| 4661 | struct hlua_txn *htxn; |
| 4662 | |
| 4663 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4664 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4665 | |
| 4666 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4667 | } |
| 4668 | |
| 4669 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4670 | { |
| 4671 | struct hlua_txn *htxn; |
| 4672 | |
| 4673 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4674 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4675 | |
| 4676 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4677 | } |
| 4678 | |
| 4679 | /* This function replace full header, or just a value in |
| 4680 | * the request or in the response. It is a wrapper fir the |
| 4681 | * 4 following functions. |
| 4682 | */ |
| 4683 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4684 | struct http_msg *msg, int action) |
| 4685 | { |
| 4686 | size_t name_len; |
| 4687 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4688 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4689 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4690 | struct my_regex re; |
| 4691 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4692 | /* Check if a valid response is parsed */ |
| 4693 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4694 | return 0; |
| 4695 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4696 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4697 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4698 | |
| 4699 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4700 | regex_free(&re); |
| 4701 | return 0; |
| 4702 | } |
| 4703 | |
| 4704 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4705 | { |
| 4706 | struct hlua_txn *htxn; |
| 4707 | |
| 4708 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4709 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4710 | |
| 4711 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4712 | } |
| 4713 | |
| 4714 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4715 | { |
| 4716 | struct hlua_txn *htxn; |
| 4717 | |
| 4718 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4719 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4720 | |
| 4721 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4722 | } |
| 4723 | |
| 4724 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4725 | { |
| 4726 | struct hlua_txn *htxn; |
| 4727 | |
| 4728 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4729 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4730 | |
| 4731 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4732 | } |
| 4733 | |
| 4734 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4735 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4736 | struct hlua_txn *htxn; |
| 4737 | |
| 4738 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4739 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4740 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4741 | 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] | 4742 | } |
| 4743 | |
| 4744 | /* This function deletes all the occurences of an header. |
| 4745 | * It is a wrapper for the 2 following functions. |
| 4746 | */ |
| 4747 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4748 | { |
| 4749 | size_t len; |
| 4750 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4751 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4752 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4753 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4754 | /* Check if a valid response is parsed */ |
| 4755 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4756 | return 0; |
| 4757 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4758 | ctx.idx = 0; |
| 4759 | while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) |
| 4760 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4761 | return 0; |
| 4762 | } |
| 4763 | |
| 4764 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4765 | { |
| 4766 | struct hlua_txn *htxn; |
| 4767 | |
| 4768 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4769 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4770 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4771 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4772 | } |
| 4773 | |
| 4774 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4775 | { |
| 4776 | struct hlua_txn *htxn; |
| 4777 | |
| 4778 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4779 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4780 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4781 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4782 | } |
| 4783 | |
| 4784 | /* This function adds an header. It is a wrapper used by |
| 4785 | * the 2 following functions. |
| 4786 | */ |
| 4787 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4788 | { |
| 4789 | size_t name_len; |
| 4790 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4791 | size_t value_len; |
| 4792 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4793 | char *p; |
| 4794 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4795 | /* Check if a valid message is parsed */ |
| 4796 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4797 | return 0; |
| 4798 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4799 | /* Check length. */ |
| 4800 | trash.len = value_len + name_len + 2; |
| 4801 | if (trash.len > trash.size) |
| 4802 | return 0; |
| 4803 | |
| 4804 | /* Creates the header string. */ |
| 4805 | p = trash.str; |
| 4806 | memcpy(p, name, name_len); |
| 4807 | p += name_len; |
| 4808 | *p = ':'; |
| 4809 | p++; |
| 4810 | *p = ' '; |
| 4811 | p++; |
| 4812 | memcpy(p, value, value_len); |
| 4813 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4814 | lua_pushboolean(L, http_header_add_tail2(msg, &htxn->s->txn->hdr_idx, |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4815 | trash.str, trash.len) != 0); |
| 4816 | |
| 4817 | return 0; |
| 4818 | } |
| 4819 | |
| 4820 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4821 | { |
| 4822 | struct hlua_txn *htxn; |
| 4823 | |
| 4824 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4825 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4826 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4827 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4828 | } |
| 4829 | |
| 4830 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4831 | { |
| 4832 | struct hlua_txn *htxn; |
| 4833 | |
| 4834 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4835 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4836 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4837 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4838 | } |
| 4839 | |
| 4840 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4841 | { |
| 4842 | struct hlua_txn *htxn; |
| 4843 | |
| 4844 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4845 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4846 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4847 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4848 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4849 | } |
| 4850 | |
| 4851 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4852 | { |
| 4853 | struct hlua_txn *htxn; |
| 4854 | |
| 4855 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4856 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4857 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4858 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4859 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4860 | } |
| 4861 | |
| 4862 | /* This function set the method. */ |
| 4863 | static int hlua_http_req_set_meth(lua_State *L) |
| 4864 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4865 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4866 | size_t name_len; |
| 4867 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4868 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4869 | /* Check if a valid request is parsed */ |
| 4870 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4871 | lua_pushboolean(L, 0); |
| 4872 | return 1; |
| 4873 | } |
| 4874 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4875 | 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] | 4876 | return 1; |
| 4877 | } |
| 4878 | |
| 4879 | /* This function set the method. */ |
| 4880 | static int hlua_http_req_set_path(lua_State *L) |
| 4881 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4882 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4883 | size_t name_len; |
| 4884 | 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] | 4885 | |
| 4886 | /* Check if a valid request is parsed */ |
| 4887 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4888 | lua_pushboolean(L, 0); |
| 4889 | return 1; |
| 4890 | } |
| 4891 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4892 | 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] | 4893 | return 1; |
| 4894 | } |
| 4895 | |
| 4896 | /* This function set the query-string. */ |
| 4897 | static int hlua_http_req_set_query(lua_State *L) |
| 4898 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4899 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4900 | size_t name_len; |
| 4901 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4902 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4903 | /* Check if a valid request is parsed */ |
| 4904 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4905 | lua_pushboolean(L, 0); |
| 4906 | return 1; |
| 4907 | } |
| 4908 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4909 | /* Check length. */ |
| 4910 | if (name_len > trash.size - 1) { |
| 4911 | lua_pushboolean(L, 0); |
| 4912 | return 1; |
| 4913 | } |
| 4914 | |
| 4915 | /* Add the mark question as prefix. */ |
| 4916 | chunk_reset(&trash); |
| 4917 | trash.str[trash.len++] = '?'; |
| 4918 | memcpy(trash.str + trash.len, name, name_len); |
| 4919 | trash.len += name_len; |
| 4920 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4921 | lua_pushboolean(L, http_replace_req_line(2, trash.str, trash.len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4922 | return 1; |
| 4923 | } |
| 4924 | |
| 4925 | /* This function set the uri. */ |
| 4926 | static int hlua_http_req_set_uri(lua_State *L) |
| 4927 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4928 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4929 | size_t name_len; |
| 4930 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4931 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4932 | /* Check if a valid request is parsed */ |
| 4933 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4934 | lua_pushboolean(L, 0); |
| 4935 | return 1; |
| 4936 | } |
| 4937 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4938 | 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] | 4939 | return 1; |
| 4940 | } |
| 4941 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4942 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4943 | static int hlua_http_res_set_status(lua_State *L) |
| 4944 | { |
| 4945 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4946 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4947 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4948 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4949 | /* Check if a valid response is parsed */ |
| 4950 | if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4951 | return 0; |
| 4952 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4953 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4954 | return 0; |
| 4955 | } |
| 4956 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4957 | /* |
| 4958 | * |
| 4959 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4960 | * Class TXN |
| 4961 | * |
| 4962 | * |
| 4963 | */ |
| 4964 | |
| 4965 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4966 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4967 | */ |
| 4968 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 4969 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4970 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4971 | } |
| 4972 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4973 | __LJMP static int hlua_set_var(lua_State *L) |
| 4974 | { |
| 4975 | struct hlua_txn *htxn; |
| 4976 | const char *name; |
| 4977 | size_t len; |
| 4978 | struct sample smp; |
| 4979 | |
| 4980 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4981 | |
| 4982 | /* It is useles to retrieve the stream, but this function |
| 4983 | * runs only in a stream context. |
| 4984 | */ |
| 4985 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4986 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4987 | |
| 4988 | /* Converts the third argument in a sample. */ |
| 4989 | hlua_lua2smp(L, 3, &smp); |
| 4990 | |
| 4991 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 4992 | 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] | 4993 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4994 | return 0; |
| 4995 | } |
| 4996 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 4997 | __LJMP static int hlua_unset_var(lua_State *L) |
| 4998 | { |
| 4999 | struct hlua_txn *htxn; |
| 5000 | const char *name; |
| 5001 | size_t len; |
| 5002 | struct sample smp; |
| 5003 | |
| 5004 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5005 | |
| 5006 | /* It is useles to retrieve the stream, but this function |
| 5007 | * runs only in a stream context. |
| 5008 | */ |
| 5009 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5010 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5011 | |
| 5012 | /* Unset the variable. */ |
| 5013 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5014 | vars_unset_by_name(name, len, &smp); |
| 5015 | return 0; |
| 5016 | } |
| 5017 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5018 | __LJMP static int hlua_get_var(lua_State *L) |
| 5019 | { |
| 5020 | struct hlua_txn *htxn; |
| 5021 | const char *name; |
| 5022 | size_t len; |
| 5023 | struct sample smp; |
| 5024 | |
| 5025 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5026 | |
| 5027 | /* It is useles to retrieve the stream, but this function |
| 5028 | * runs only in a stream context. |
| 5029 | */ |
| 5030 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5031 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5032 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5033 | 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] | 5034 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5035 | lua_pushnil(L); |
| 5036 | return 1; |
| 5037 | } |
| 5038 | |
| 5039 | return hlua_smp2lua(L, &smp); |
| 5040 | } |
| 5041 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5042 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5043 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5044 | struct hlua *hlua; |
| 5045 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5046 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5047 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5048 | /* It is useles to retrieve the stream, but this function |
| 5049 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5050 | */ |
| 5051 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5052 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5053 | |
| 5054 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5055 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5056 | |
| 5057 | /* Get and store new value. */ |
| 5058 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5059 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5060 | |
| 5061 | return 0; |
| 5062 | } |
| 5063 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5064 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5065 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5066 | struct hlua *hlua; |
| 5067 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5068 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5069 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5070 | /* It is useles to retrieve the stream, but this function |
| 5071 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5072 | */ |
| 5073 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5074 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5075 | |
| 5076 | /* Push configuration index in the stack. */ |
| 5077 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5078 | |
| 5079 | return 1; |
| 5080 | } |
| 5081 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5082 | /* Create stack entry containing a class TXN. This function |
| 5083 | * return 0 if the stack does not contains free slots, |
| 5084 | * otherwise it returns 1. |
| 5085 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5086 | 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] | 5087 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5088 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5089 | |
| 5090 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5091 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5092 | return 0; |
| 5093 | |
| 5094 | /* NOTE: The allocation never fails. The failure |
| 5095 | * throw an error, and the function never returns. |
| 5096 | * if the throw is not avalaible, the process is aborted. |
| 5097 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5098 | /* Create the object: obj[0] = userdata. */ |
| 5099 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5100 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5101 | lua_rawseti(L, -2, 0); |
| 5102 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5103 | htxn->s = s; |
| 5104 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5105 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5106 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5107 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5108 | /* Create the "f" field that contains a list of fetches. */ |
| 5109 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5110 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5111 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5112 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5113 | |
| 5114 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5115 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5116 | 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] | 5117 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5118 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5119 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5120 | /* Create the "c" field that contains a list of converters. */ |
| 5121 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5122 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5123 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5124 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5125 | |
| 5126 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5127 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5128 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5129 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5130 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5131 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5132 | /* Create the "req" field that contains the request channel object. */ |
| 5133 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5134 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5135 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5136 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5137 | |
| 5138 | /* Create the "res" field that contains the response channel object. */ |
| 5139 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5140 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5141 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5142 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5143 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5144 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5145 | lua_pushstring(L, "http"); |
| 5146 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5147 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5148 | return 0; |
| 5149 | } |
| 5150 | else |
| 5151 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5152 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5153 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5154 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5155 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5156 | lua_setmetatable(L, -2); |
| 5157 | |
| 5158 | return 1; |
| 5159 | } |
| 5160 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5161 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5162 | { |
| 5163 | const char *msg; |
| 5164 | struct hlua_txn *htxn; |
| 5165 | |
| 5166 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5167 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5168 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5169 | |
| 5170 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5171 | return 0; |
| 5172 | } |
| 5173 | |
| 5174 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5175 | { |
| 5176 | int level; |
| 5177 | const char *msg; |
| 5178 | struct hlua_txn *htxn; |
| 5179 | |
| 5180 | MAY_LJMP(check_args(L, 3, "log")); |
| 5181 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5182 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5183 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5184 | |
| 5185 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5186 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5187 | |
| 5188 | hlua_sendlog(htxn->s->be, level, msg); |
| 5189 | return 0; |
| 5190 | } |
| 5191 | |
| 5192 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5193 | { |
| 5194 | const char *msg; |
| 5195 | struct hlua_txn *htxn; |
| 5196 | |
| 5197 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5198 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5199 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5200 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5201 | return 0; |
| 5202 | } |
| 5203 | |
| 5204 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5205 | { |
| 5206 | const char *msg; |
| 5207 | struct hlua_txn *htxn; |
| 5208 | |
| 5209 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5210 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5211 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5212 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5213 | return 0; |
| 5214 | } |
| 5215 | |
| 5216 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5217 | { |
| 5218 | const char *msg; |
| 5219 | struct hlua_txn *htxn; |
| 5220 | |
| 5221 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5222 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5223 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5224 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5225 | return 0; |
| 5226 | } |
| 5227 | |
| 5228 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5229 | { |
| 5230 | const char *msg; |
| 5231 | struct hlua_txn *htxn; |
| 5232 | |
| 5233 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5234 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5235 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5236 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5237 | return 0; |
| 5238 | } |
| 5239 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5240 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5241 | { |
| 5242 | struct hlua_txn *htxn; |
| 5243 | int ll; |
| 5244 | |
| 5245 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5246 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5247 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5248 | |
| 5249 | if (ll < 0 || ll > 7) |
| 5250 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5251 | |
| 5252 | htxn->s->logs.level = ll; |
| 5253 | return 0; |
| 5254 | } |
| 5255 | |
| 5256 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5257 | { |
| 5258 | struct hlua_txn *htxn; |
| 5259 | struct connection *cli_conn; |
| 5260 | int tos; |
| 5261 | |
| 5262 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5263 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5264 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5265 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5266 | 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] | 5267 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5268 | |
| 5269 | return 0; |
| 5270 | } |
| 5271 | |
| 5272 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5273 | { |
| 5274 | #ifdef SO_MARK |
| 5275 | struct hlua_txn *htxn; |
| 5276 | struct connection *cli_conn; |
| 5277 | int mark; |
| 5278 | |
| 5279 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5280 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5281 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5282 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5283 | 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] | 5284 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5285 | #endif |
| 5286 | return 0; |
| 5287 | } |
| 5288 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5289 | /* This function is an Lua binding that send pending data |
| 5290 | * to the client, and close the stream interface. |
| 5291 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5292 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5293 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5294 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5295 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5296 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5297 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5298 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5299 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5300 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5301 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5302 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5303 | * session, so we just end the execution of the current |
| 5304 | * lua code. |
| 5305 | */ |
| 5306 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5307 | WILL_LJMP(hlua_done(L)); |
| 5308 | return 0; |
| 5309 | } |
| 5310 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5311 | ic = &htxn->s->req; |
| 5312 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5313 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5314 | if (htxn->s->txn) { |
| 5315 | /* HTTP mode, let's stay in sync with the stream */ |
| 5316 | bi_fast_delete(ic->buf, htxn->s->txn->req.sov); |
| 5317 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 5318 | htxn->s->txn->req.sov = 0; |
| 5319 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 5320 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 5321 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5322 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 5323 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5324 | /* Note that if we want to support keep-alive, we need |
| 5325 | * to bypass the close/shutr_now calls below, but that |
| 5326 | * may only be done if the HTTP request was already |
| 5327 | * processed and the connection header is known (ie |
| 5328 | * not during TCP rules). |
| 5329 | */ |
| 5330 | } |
| 5331 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5332 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5333 | channel_abort(ic); |
| 5334 | channel_auto_close(ic); |
| 5335 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5336 | |
| 5337 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5338 | channel_auto_read(oc); |
| 5339 | channel_auto_close(oc); |
| 5340 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5341 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 5342 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5343 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5344 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5345 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5346 | return 0; |
| 5347 | } |
| 5348 | |
| 5349 | __LJMP static int hlua_log(lua_State *L) |
| 5350 | { |
| 5351 | int level; |
| 5352 | const char *msg; |
| 5353 | |
| 5354 | MAY_LJMP(check_args(L, 2, "log")); |
| 5355 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5356 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5357 | |
| 5358 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5359 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5360 | |
| 5361 | hlua_sendlog(NULL, level, msg); |
| 5362 | return 0; |
| 5363 | } |
| 5364 | |
| 5365 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5366 | { |
| 5367 | const char *msg; |
| 5368 | |
| 5369 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5370 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5371 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5372 | return 0; |
| 5373 | } |
| 5374 | |
| 5375 | __LJMP static int hlua_log_info(lua_State *L) |
| 5376 | { |
| 5377 | const char *msg; |
| 5378 | |
| 5379 | MAY_LJMP(check_args(L, 1, "info")); |
| 5380 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5381 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5382 | return 0; |
| 5383 | } |
| 5384 | |
| 5385 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5386 | { |
| 5387 | const char *msg; |
| 5388 | |
| 5389 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5390 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5391 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5392 | return 0; |
| 5393 | } |
| 5394 | |
| 5395 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5396 | { |
| 5397 | const char *msg; |
| 5398 | |
| 5399 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5400 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5401 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5402 | return 0; |
| 5403 | } |
| 5404 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5405 | __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] | 5406 | { |
| 5407 | int wakeup_ms = lua_tointeger(L, -1); |
| 5408 | if (now_ms < wakeup_ms) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5409 | 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] | 5410 | return 0; |
| 5411 | } |
| 5412 | |
| 5413 | __LJMP static int hlua_sleep(lua_State *L) |
| 5414 | { |
| 5415 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5416 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5417 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5418 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5419 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5420 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5421 | wakeup_ms = tick_add(now_ms, delay); |
| 5422 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5423 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5424 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5425 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5426 | } |
| 5427 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5428 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5429 | { |
| 5430 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5431 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5432 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5433 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5434 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5435 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5436 | wakeup_ms = tick_add(now_ms, delay); |
| 5437 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5438 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5439 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5440 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5441 | } |
| 5442 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5443 | /* This functionis an LUA binding. it permits to give back |
| 5444 | * the hand at the HAProxy scheduler. It is used when the |
| 5445 | * LUA processing consumes a lot of time. |
| 5446 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5447 | __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] | 5448 | { |
| 5449 | return 0; |
| 5450 | } |
| 5451 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5452 | __LJMP static int hlua_yield(lua_State *L) |
| 5453 | { |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5454 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 5455 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5456 | } |
| 5457 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5458 | /* This function change the nice of the currently executed |
| 5459 | * task. It is used set low or high priority at the current |
| 5460 | * task. |
| 5461 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5462 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5463 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5464 | struct hlua *hlua; |
| 5465 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5466 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5467 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5468 | hlua = hlua_gethlua(L); |
| 5469 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5470 | |
| 5471 | /* If he task is not set, I'm in a start mode. */ |
| 5472 | if (!hlua || !hlua->task) |
| 5473 | return 0; |
| 5474 | |
| 5475 | if (nice < -1024) |
| 5476 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5477 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5478 | nice = 1024; |
| 5479 | |
| 5480 | hlua->task->nice = nice; |
| 5481 | return 0; |
| 5482 | } |
| 5483 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5484 | /* This function is used as a calback of a task. It is called by the |
| 5485 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5486 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5487 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5488 | * |
| 5489 | * Task wrapper are longjmp safe because the only one Lua code |
| 5490 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5491 | */ |
| 5492 | static struct task *hlua_process_task(struct task *task) |
| 5493 | { |
| 5494 | struct hlua *hlua = task->context; |
| 5495 | enum hlua_exec status; |
| 5496 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5497 | /* If it is the first call to the task, we must initialize the |
| 5498 | * execution timeouts. |
| 5499 | */ |
| 5500 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5501 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5502 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5503 | /* Execute the Lua code. */ |
| 5504 | status = hlua_ctx_resume(hlua, 1); |
| 5505 | |
| 5506 | switch (status) { |
| 5507 | /* finished or yield */ |
| 5508 | case HLUA_E_OK: |
| 5509 | hlua_ctx_destroy(hlua); |
| 5510 | task_delete(task); |
| 5511 | task_free(task); |
| 5512 | break; |
| 5513 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5514 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
| 5515 | if (hlua->wake_time != TICK_ETERNITY) |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5516 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5517 | break; |
| 5518 | |
| 5519 | /* finished with error. */ |
| 5520 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5521 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5522 | hlua_ctx_destroy(hlua); |
| 5523 | task_delete(task); |
| 5524 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5525 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5526 | break; |
| 5527 | |
| 5528 | case HLUA_E_ERR: |
| 5529 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5530 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5531 | hlua_ctx_destroy(hlua); |
| 5532 | task_delete(task); |
| 5533 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5534 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5535 | break; |
| 5536 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5537 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5538 | } |
| 5539 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5540 | /* This function is an LUA binding that register LUA function to be |
| 5541 | * executed after the HAProxy configuration parsing and before the |
| 5542 | * HAProxy scheduler starts. This function expect only one LUA |
| 5543 | * argument that is a function. This function returns nothing, but |
| 5544 | * throws if an error is encountered. |
| 5545 | */ |
| 5546 | __LJMP static int hlua_register_init(lua_State *L) |
| 5547 | { |
| 5548 | struct hlua_init_function *init; |
| 5549 | int ref; |
| 5550 | |
| 5551 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5552 | |
| 5553 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5554 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5555 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5556 | if (!init) |
| 5557 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5558 | |
| 5559 | init->function_ref = ref; |
| 5560 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5561 | return 0; |
| 5562 | } |
| 5563 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5564 | /* This functio is an LUA binding. It permits to register a task |
| 5565 | * executed in parallel of the main HAroxy activity. The task is |
| 5566 | * created and it is set in the HAProxy scheduler. It can be called |
| 5567 | * from the "init" section, "post init" or during the runtime. |
| 5568 | * |
| 5569 | * Lua prototype: |
| 5570 | * |
| 5571 | * <none> core.register_task(<function>) |
| 5572 | */ |
| 5573 | static int hlua_register_task(lua_State *L) |
| 5574 | { |
| 5575 | struct hlua *hlua; |
| 5576 | struct task *task; |
| 5577 | int ref; |
| 5578 | |
| 5579 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5580 | |
| 5581 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5582 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5583 | hlua = pool_alloc2(pool2_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5584 | if (!hlua) |
| 5585 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5586 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 5587 | task = task_new(MAX_THREADS_MASK); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5588 | task->context = hlua; |
| 5589 | task->process = hlua_process_task; |
| 5590 | |
| 5591 | if (!hlua_ctx_init(hlua, task)) |
| 5592 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5593 | |
| 5594 | /* Restore the function in the stack. */ |
| 5595 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5596 | hlua->nargs = 0; |
| 5597 | |
| 5598 | /* Schedule task. */ |
| 5599 | task_schedule(task, now_ms); |
| 5600 | |
| 5601 | return 0; |
| 5602 | } |
| 5603 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5604 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5605 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5606 | * resume converters. |
| 5607 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5608 | 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] | 5609 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5610 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5611 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5612 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5613 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5614 | if (!stream) |
| 5615 | return 0; |
| 5616 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5617 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5618 | * Lua context can be not initialized. This behavior |
| 5619 | * permits to save performances because a systematic |
| 5620 | * Lua initialization cause 5% performances loss. |
| 5621 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5622 | if (!stream->hlua) { |
| 5623 | stream->hlua = pool_alloc2(pool2_hlua); |
| 5624 | if (!stream->hlua) { |
| 5625 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5626 | return 0; |
| 5627 | } |
| 5628 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5629 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5630 | return 0; |
| 5631 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5632 | } |
| 5633 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5634 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5635 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5636 | |
| 5637 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5638 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5639 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5640 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5641 | else |
| 5642 | error = "critical error"; |
| 5643 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5644 | return 0; |
| 5645 | } |
| 5646 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5647 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5648 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5649 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5650 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5651 | return 0; |
| 5652 | } |
| 5653 | |
| 5654 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5655 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5656 | |
| 5657 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5658 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5659 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5660 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5661 | return 0; |
| 5662 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5663 | hlua_smp2lua(stream->hlua->T, smp); |
| 5664 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5665 | |
| 5666 | /* push keywords in the stack. */ |
| 5667 | if (arg_p) { |
| 5668 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5669 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5670 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5671 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5672 | return 0; |
| 5673 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5674 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5675 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5676 | } |
| 5677 | } |
| 5678 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5679 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5680 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5681 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5682 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5683 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5684 | } |
| 5685 | |
| 5686 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5687 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5688 | /* finished. */ |
| 5689 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5690 | /* If the stack is empty, the function fails. */ |
| 5691 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5692 | return 0; |
| 5693 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5694 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5695 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5696 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5697 | return 1; |
| 5698 | |
| 5699 | /* yield. */ |
| 5700 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5701 | 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] | 5702 | return 0; |
| 5703 | |
| 5704 | /* finished with error. */ |
| 5705 | case HLUA_E_ERRMSG: |
| 5706 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5707 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5708 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5709 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5710 | return 0; |
| 5711 | |
| 5712 | case HLUA_E_ERR: |
| 5713 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5714 | 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] | 5715 | |
| 5716 | default: |
| 5717 | return 0; |
| 5718 | } |
| 5719 | } |
| 5720 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5721 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5722 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5723 | * resume sample-fetches. This function will be called by the sample |
| 5724 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5725 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5726 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5727 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5728 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5729 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5730 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5731 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5732 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5733 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5734 | if (!stream) |
| 5735 | return 0; |
| 5736 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5737 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5738 | * Lua context can be not initialized. This behavior |
| 5739 | * permits to save performances because a systematic |
| 5740 | * Lua initialization cause 5% performances loss. |
| 5741 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5742 | if (!stream->hlua) { |
| 5743 | stream->hlua = pool_alloc2(pool2_hlua); |
| 5744 | if (!stream->hlua) { |
| 5745 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5746 | return 0; |
| 5747 | } |
| 5748 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5749 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5750 | return 0; |
| 5751 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5752 | } |
| 5753 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5754 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5755 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5756 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5757 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5758 | |
| 5759 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5760 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5761 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5762 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5763 | else |
| 5764 | error = "critical error"; |
| 5765 | 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] | 5766 | return 0; |
| 5767 | } |
| 5768 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5769 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5770 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5771 | 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] | 5772 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5773 | return 0; |
| 5774 | } |
| 5775 | |
| 5776 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5777 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5778 | |
| 5779 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5780 | 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] | 5781 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5782 | 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] | 5783 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5784 | return 0; |
| 5785 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5786 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5787 | |
| 5788 | /* push keywords in the stack. */ |
| 5789 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5790 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5791 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5792 | 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] | 5793 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5794 | return 0; |
| 5795 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5796 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5797 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5798 | } |
| 5799 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5800 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5801 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5802 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5803 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5804 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5805 | } |
| 5806 | |
| 5807 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5808 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5809 | /* finished. */ |
| 5810 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5811 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5812 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5813 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5814 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5815 | /* If the stack is empty, the function fails. */ |
| 5816 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5817 | return 0; |
| 5818 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5819 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5820 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5821 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5822 | |
| 5823 | /* Set the end of execution flag. */ |
| 5824 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5825 | return 1; |
| 5826 | |
| 5827 | /* yield. */ |
| 5828 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5829 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5830 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5831 | 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] | 5832 | return 0; |
| 5833 | |
| 5834 | /* finished with error. */ |
| 5835 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5836 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5837 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5838 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5839 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5840 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5841 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5842 | return 0; |
| 5843 | |
| 5844 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5845 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5846 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5847 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5848 | 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] | 5849 | |
| 5850 | default: |
| 5851 | return 0; |
| 5852 | } |
| 5853 | } |
| 5854 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5855 | /* This function is an LUA binding used for registering |
| 5856 | * "sample-conv" functions. It expects a converter name used |
| 5857 | * in the haproxy configuration file, and an LUA function. |
| 5858 | */ |
| 5859 | __LJMP static int hlua_register_converters(lua_State *L) |
| 5860 | { |
| 5861 | struct sample_conv_kw_list *sck; |
| 5862 | const char *name; |
| 5863 | int ref; |
| 5864 | int len; |
| 5865 | struct hlua_function *fcn; |
| 5866 | |
| 5867 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 5868 | |
| 5869 | /* First argument : converter name. */ |
| 5870 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5871 | |
| 5872 | /* Second argument : lua function. */ |
| 5873 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5874 | |
| 5875 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5876 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5877 | if (!sck) |
| 5878 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5879 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5880 | if (!fcn) |
| 5881 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5882 | |
| 5883 | /* Fill fcn. */ |
| 5884 | fcn->name = strdup(name); |
| 5885 | if (!fcn->name) |
| 5886 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5887 | fcn->function_ref = ref; |
| 5888 | |
| 5889 | /* List head */ |
| 5890 | sck->list.n = sck->list.p = NULL; |
| 5891 | |
| 5892 | /* converter keyword. */ |
| 5893 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5894 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5895 | if (!sck->kw[0].kw) |
| 5896 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5897 | |
| 5898 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 5899 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5900 | 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] | 5901 | sck->kw[0].val_args = NULL; |
| 5902 | sck->kw[0].in_type = SMP_T_STR; |
| 5903 | sck->kw[0].out_type = SMP_T_STR; |
| 5904 | sck->kw[0].private = fcn; |
| 5905 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5906 | /* Register this new converter */ |
| 5907 | sample_register_convs(sck); |
| 5908 | |
| 5909 | return 0; |
| 5910 | } |
| 5911 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5912 | /* This fucntion is an LUA binding used for registering |
| 5913 | * "sample-fetch" functions. It expects a converter name used |
| 5914 | * in the haproxy configuration file, and an LUA function. |
| 5915 | */ |
| 5916 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 5917 | { |
| 5918 | const char *name; |
| 5919 | int ref; |
| 5920 | int len; |
| 5921 | struct sample_fetch_kw_list *sfk; |
| 5922 | struct hlua_function *fcn; |
| 5923 | |
| 5924 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 5925 | |
| 5926 | /* First argument : sample-fetch name. */ |
| 5927 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5928 | |
| 5929 | /* Second argument : lua function. */ |
| 5930 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5931 | |
| 5932 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5933 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5934 | if (!sfk) |
| 5935 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5936 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5937 | if (!fcn) |
| 5938 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5939 | |
| 5940 | /* Fill fcn. */ |
| 5941 | fcn->name = strdup(name); |
| 5942 | if (!fcn->name) |
| 5943 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5944 | fcn->function_ref = ref; |
| 5945 | |
| 5946 | /* List head */ |
| 5947 | sfk->list.n = sfk->list.p = NULL; |
| 5948 | |
| 5949 | /* sample-fetch keyword. */ |
| 5950 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5951 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5952 | if (!sfk->kw[0].kw) |
| 5953 | return luaL_error(L, "lua out of memory error."); |
| 5954 | |
| 5955 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 5956 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5957 | 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] | 5958 | sfk->kw[0].val_args = NULL; |
| 5959 | sfk->kw[0].out_type = SMP_T_STR; |
| 5960 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 5961 | sfk->kw[0].val = 0; |
| 5962 | sfk->kw[0].private = fcn; |
| 5963 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5964 | /* Register this new fetch. */ |
| 5965 | sample_register_fetches(sfk); |
| 5966 | |
| 5967 | return 0; |
| 5968 | } |
| 5969 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5970 | /* This function is a wrapper to execute each LUA function declared |
| 5971 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5972 | * return ACT_RET_CONT if the processing is finished (with or without |
| 5973 | * error) and return ACT_RET_YIELD if the function must be called again |
| 5974 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5975 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5976 | 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] | 5977 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5978 | { |
| 5979 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5980 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5981 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5982 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5983 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5984 | |
| 5985 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 5986 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 5987 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 5988 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 5989 | 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] | 5990 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5991 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5992 | return ACT_RET_CONT; |
| 5993 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5994 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5995 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5996 | * Lua context can be not initialized. This behavior |
| 5997 | * permits to save performances because a systematic |
| 5998 | * Lua initialization cause 5% performances loss. |
| 5999 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6000 | if (!s->hlua) { |
| 6001 | s->hlua = pool_alloc2(pool2_hlua); |
| 6002 | if (!s->hlua) { |
| 6003 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6004 | rule->arg.hlua_rule->fcn.name); |
| 6005 | return ACT_RET_CONT; |
| 6006 | } |
| 6007 | if (!hlua_ctx_init(s->hlua, s->task)) { |
| 6008 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6009 | rule->arg.hlua_rule->fcn.name); |
| 6010 | return ACT_RET_CONT; |
| 6011 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6012 | } |
| 6013 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6014 | consistency_set(s, dir, &s->hlua->cons); |
| 6015 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6016 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6017 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6018 | |
| 6019 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6020 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6021 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6022 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6023 | else |
| 6024 | error = "critical error"; |
| 6025 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6026 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6027 | return ACT_RET_CONT; |
| 6028 | } |
| 6029 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6030 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6031 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6032 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6033 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6034 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6035 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6036 | } |
| 6037 | |
| 6038 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6039 | 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] | 6040 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6041 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6042 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6043 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6044 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6045 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6046 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6047 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6048 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6049 | |
| 6050 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6051 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6052 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6053 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6054 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6055 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6056 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6057 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6058 | lua_pushstring(s->hlua->T, *arg); |
| 6059 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6060 | } |
| 6061 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6062 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6063 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6064 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6065 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6066 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6067 | } |
| 6068 | |
| 6069 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6070 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6071 | /* finished. */ |
| 6072 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6073 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6074 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6075 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6076 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6077 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6078 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6079 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6080 | |
| 6081 | /* yield. */ |
| 6082 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6083 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6084 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6085 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6086 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6087 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6088 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6089 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6090 | /* Some actions can be wake up when a "write" event |
| 6091 | * is detected on a response channel. This is useful |
| 6092 | * only for actions targetted on the requests. |
| 6093 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6094 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6095 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6096 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6097 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6098 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6099 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6100 | s->req.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6101 | /* We can quit the fcuntion without consistency check |
| 6102 | * because HAProxy is not able to manipulate data, it |
| 6103 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6104 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6105 | |
| 6106 | /* finished with error. */ |
| 6107 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6108 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6109 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6110 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6111 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6112 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6113 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6114 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6115 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6116 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6117 | |
| 6118 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6119 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6120 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6121 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6122 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6123 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6124 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6125 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6126 | |
| 6127 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6128 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6129 | } |
| 6130 | } |
| 6131 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6132 | struct task *hlua_applet_wakeup(struct task *t) |
| 6133 | { |
| 6134 | struct appctx *ctx = t->context; |
| 6135 | struct stream_interface *si = ctx->owner; |
| 6136 | |
| 6137 | /* If the applet is wake up without any expected work, the sheduler |
| 6138 | * remove it from the run queue. This flag indicate that the applet |
| 6139 | * is waiting for write. If the buffer is full, the main processing |
| 6140 | * will send some data and after call the applet, otherwise it call |
| 6141 | * the applet ASAP. |
| 6142 | */ |
| 6143 | si_applet_cant_put(si); |
| 6144 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6145 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6146 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6147 | } |
| 6148 | |
| 6149 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6150 | { |
| 6151 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6152 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6153 | struct task *task; |
| 6154 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6155 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6156 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6157 | hlua = pool_alloc2(pool2_hlua); |
| 6158 | if (!hlua) { |
| 6159 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6160 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6161 | return 0; |
| 6162 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6163 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6164 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6165 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6166 | |
| 6167 | /* Create task used by signal to wakeup applets. */ |
Thierry FOURNIER | 4325ab7 | 2017-07-12 11:53:38 +0200 | [diff] [blame] | 6168 | task = task_new(1UL << tid); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6169 | if (!task) { |
| 6170 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6171 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6172 | return 0; |
| 6173 | } |
| 6174 | task->nice = 0; |
| 6175 | task->context = ctx; |
| 6176 | task->process = hlua_applet_wakeup; |
| 6177 | ctx->ctx.hlua_apptcp.task = task; |
| 6178 | |
| 6179 | /* In the execution wrappers linked with a stream, the |
| 6180 | * Lua context can be not initialized. This behavior |
| 6181 | * permits to save performances because a systematic |
| 6182 | * Lua initialization cause 5% performances loss. |
| 6183 | */ |
| 6184 | if (!hlua_ctx_init(hlua, task)) { |
| 6185 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6186 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6187 | return 0; |
| 6188 | } |
| 6189 | |
| 6190 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6191 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6192 | |
| 6193 | /* The following Lua calls can fail. */ |
| 6194 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6195 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6196 | error = lua_tostring(hlua->T, -1); |
| 6197 | else |
| 6198 | error = "critical error"; |
| 6199 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6200 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6201 | RESET_SAFE_LJMP(hlua->T); |
| 6202 | return 0; |
| 6203 | } |
| 6204 | |
| 6205 | /* Check stack available size. */ |
| 6206 | if (!lua_checkstack(hlua->T, 1)) { |
| 6207 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6208 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6209 | RESET_SAFE_LJMP(hlua->T); |
| 6210 | return 0; |
| 6211 | } |
| 6212 | |
| 6213 | /* Restore the function in the stack. */ |
| 6214 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6215 | |
| 6216 | /* Create and and push object stream in the stack. */ |
| 6217 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6218 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6219 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6220 | RESET_SAFE_LJMP(hlua->T); |
| 6221 | return 0; |
| 6222 | } |
| 6223 | hlua->nargs = 1; |
| 6224 | |
| 6225 | /* push keywords in the stack. */ |
| 6226 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6227 | if (!lua_checkstack(hlua->T, 1)) { |
| 6228 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6229 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6230 | RESET_SAFE_LJMP(hlua->T); |
| 6231 | return 0; |
| 6232 | } |
| 6233 | lua_pushstring(hlua->T, *arg); |
| 6234 | hlua->nargs++; |
| 6235 | } |
| 6236 | |
| 6237 | RESET_SAFE_LJMP(hlua->T); |
| 6238 | |
| 6239 | /* Wakeup the applet ASAP. */ |
| 6240 | si_applet_cant_get(si); |
| 6241 | si_applet_cant_put(si); |
| 6242 | |
| 6243 | return 1; |
| 6244 | } |
| 6245 | |
| 6246 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6247 | { |
| 6248 | struct stream_interface *si = ctx->owner; |
| 6249 | struct stream *strm = si_strm(si); |
| 6250 | struct channel *res = si_ic(si); |
| 6251 | struct act_rule *rule = ctx->rule; |
| 6252 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6253 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6254 | |
| 6255 | /* The applet execution is already done. */ |
| 6256 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) |
| 6257 | return; |
| 6258 | |
| 6259 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6260 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6261 | return; |
| 6262 | |
| 6263 | /* Execute the function. */ |
| 6264 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6265 | /* finished. */ |
| 6266 | case HLUA_E_OK: |
| 6267 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6268 | |
| 6269 | /* log time */ |
| 6270 | strm->logs.tv_request = now; |
| 6271 | |
| 6272 | /* eat the whole request */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6273 | co_skip(si_oc(si), si_ob(si)->o); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6274 | res->flags |= CF_READ_NULL; |
| 6275 | si_shutr(si); |
| 6276 | return; |
| 6277 | |
| 6278 | /* yield. */ |
| 6279 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6280 | if (hlua->wake_time != TICK_ETERNITY) |
| 6281 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6282 | return; |
| 6283 | |
| 6284 | /* finished with error. */ |
| 6285 | case HLUA_E_ERRMSG: |
| 6286 | /* Display log. */ |
| 6287 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6288 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6289 | lua_pop(hlua->T, 1); |
| 6290 | goto error; |
| 6291 | |
| 6292 | case HLUA_E_ERR: |
| 6293 | /* Display log. */ |
| 6294 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6295 | rule->arg.hlua_rule->fcn.name); |
| 6296 | goto error; |
| 6297 | |
| 6298 | default: |
| 6299 | goto error; |
| 6300 | } |
| 6301 | |
| 6302 | error: |
| 6303 | |
| 6304 | /* For all other cases, just close the stream. */ |
| 6305 | si_shutw(si); |
| 6306 | si_shutr(si); |
| 6307 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6308 | } |
| 6309 | |
| 6310 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6311 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6312 | task_delete(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6313 | task_free(ctx->ctx.hlua_apptcp.task); |
| 6314 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6315 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6316 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6317 | } |
| 6318 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6319 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6320 | * an errors occurs and -1 if more data are required for initializing |
| 6321 | * the applet. |
| 6322 | */ |
| 6323 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6324 | { |
| 6325 | struct stream_interface *si = ctx->owner; |
| 6326 | struct channel *req = si_oc(si); |
| 6327 | struct http_msg *msg; |
| 6328 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6329 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6330 | char **arg; |
| 6331 | struct hdr_ctx hdr; |
| 6332 | struct task *task; |
| 6333 | 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] | 6334 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6335 | |
| 6336 | /* Wait for a full HTTP request. */ |
| 6337 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 6338 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 6339 | return -1; |
| 6340 | return 0; |
| 6341 | } |
| 6342 | txn = strm->txn; |
| 6343 | msg = &txn->req; |
| 6344 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6345 | /* We want two things in HTTP mode : |
| 6346 | * - enforce server-close mode if we were in keep-alive, so that the |
| 6347 | * applet is released after each response ; |
| 6348 | * - enable request body transfer to the applet in order to resync |
| 6349 | * with the response body. |
| 6350 | */ |
| 6351 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 6352 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6353 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6354 | hlua = pool_alloc2(pool2_hlua); |
| 6355 | if (!hlua) { |
| 6356 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6357 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6358 | return 0; |
| 6359 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6360 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6361 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6362 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6363 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6364 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6365 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6366 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6367 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6368 | /* Create task used by signal to wakeup applets. */ |
Thierry FOURNIER | 4325ab7 | 2017-07-12 11:53:38 +0200 | [diff] [blame] | 6369 | task = task_new(1UL << tid); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6370 | if (!task) { |
| 6371 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6372 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6373 | return 0; |
| 6374 | } |
| 6375 | task->nice = 0; |
| 6376 | task->context = ctx; |
| 6377 | task->process = hlua_applet_wakeup; |
| 6378 | ctx->ctx.hlua_apphttp.task = task; |
| 6379 | |
| 6380 | /* In the execution wrappers linked with a stream, the |
| 6381 | * Lua context can be not initialized. This behavior |
| 6382 | * permits to save performances because a systematic |
| 6383 | * Lua initialization cause 5% performances loss. |
| 6384 | */ |
| 6385 | if (!hlua_ctx_init(hlua, task)) { |
| 6386 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6387 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6388 | return 0; |
| 6389 | } |
| 6390 | |
| 6391 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6392 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6393 | |
| 6394 | /* The following Lua calls can fail. */ |
| 6395 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6396 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6397 | error = lua_tostring(hlua->T, -1); |
| 6398 | else |
| 6399 | error = "critical error"; |
| 6400 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6401 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6402 | return 0; |
| 6403 | } |
| 6404 | |
| 6405 | /* Check stack available size. */ |
| 6406 | if (!lua_checkstack(hlua->T, 1)) { |
| 6407 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6408 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6409 | RESET_SAFE_LJMP(hlua->T); |
| 6410 | return 0; |
| 6411 | } |
| 6412 | |
| 6413 | /* Restore the function in the stack. */ |
| 6414 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6415 | |
| 6416 | /* Create and and push object stream in the stack. */ |
| 6417 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6418 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6419 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6420 | RESET_SAFE_LJMP(hlua->T); |
| 6421 | return 0; |
| 6422 | } |
| 6423 | hlua->nargs = 1; |
| 6424 | |
| 6425 | /* Look for a 100-continue expected. */ |
| 6426 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 6427 | hdr.idx = 0; |
| 6428 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) && |
| 6429 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 6430 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 6431 | } |
| 6432 | |
| 6433 | /* push keywords in the stack. */ |
| 6434 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6435 | if (!lua_checkstack(hlua->T, 1)) { |
| 6436 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6437 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6438 | RESET_SAFE_LJMP(hlua->T); |
| 6439 | return 0; |
| 6440 | } |
| 6441 | lua_pushstring(hlua->T, *arg); |
| 6442 | hlua->nargs++; |
| 6443 | } |
| 6444 | |
| 6445 | RESET_SAFE_LJMP(hlua->T); |
| 6446 | |
| 6447 | /* Wakeup the applet when data is ready for read. */ |
| 6448 | si_applet_cant_get(si); |
| 6449 | |
| 6450 | return 1; |
| 6451 | } |
| 6452 | |
| 6453 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 6454 | { |
| 6455 | struct stream_interface *si = ctx->owner; |
| 6456 | struct stream *strm = si_strm(si); |
| 6457 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6458 | struct act_rule *rule = ctx->rule; |
| 6459 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6460 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6461 | char *blk1; |
| 6462 | int len1; |
| 6463 | char *blk2; |
| 6464 | int len2; |
| 6465 | int ret; |
| 6466 | |
| 6467 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6468 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6469 | return; |
| 6470 | |
| 6471 | /* Set the currently running flag. */ |
| 6472 | if (!HLUA_IS_RUNNING(hlua) && |
| 6473 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6474 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6475 | /* Wait for full HTTP analysys. */ |
| 6476 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 6477 | si_applet_cant_get(si); |
| 6478 | return; |
| 6479 | } |
| 6480 | |
| 6481 | /* Store the max amount of bytes that we can read. */ |
| 6482 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 6483 | |
| 6484 | /* We need to flush the request header. This left the body |
| 6485 | * for the Lua. |
| 6486 | */ |
| 6487 | |
| 6488 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6489 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6490 | if (ret == -1) |
| 6491 | return; |
| 6492 | |
| 6493 | /* No data available, ask for more data. */ |
| 6494 | if (ret == 1) |
| 6495 | len2 = 0; |
| 6496 | if (ret == 0) |
| 6497 | len1 = 0; |
| 6498 | if (len1 + len2 < strm->txn->req.eoh + 2) { |
| 6499 | si_applet_cant_get(si); |
| 6500 | return; |
| 6501 | } |
| 6502 | |
| 6503 | /* skip the requests bytes. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6504 | co_skip(si_oc(si), strm->txn->req.eoh + 2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6505 | } |
| 6506 | |
| 6507 | /* Executes The applet if it is not done. */ |
| 6508 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6509 | |
| 6510 | /* Execute the function. */ |
| 6511 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6512 | /* finished. */ |
| 6513 | case HLUA_E_OK: |
| 6514 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6515 | break; |
| 6516 | |
| 6517 | /* yield. */ |
| 6518 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6519 | if (hlua->wake_time != TICK_ETERNITY) |
| 6520 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6521 | return; |
| 6522 | |
| 6523 | /* finished with error. */ |
| 6524 | case HLUA_E_ERRMSG: |
| 6525 | /* Display log. */ |
| 6526 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6527 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6528 | lua_pop(hlua->T, 1); |
| 6529 | goto error; |
| 6530 | |
| 6531 | case HLUA_E_ERR: |
| 6532 | /* Display log. */ |
| 6533 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6534 | rule->arg.hlua_rule->fcn.name); |
| 6535 | goto error; |
| 6536 | |
| 6537 | default: |
| 6538 | goto error; |
| 6539 | } |
| 6540 | } |
| 6541 | |
| 6542 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 6543 | |
| 6544 | /* We must send the final chunk. */ |
| 6545 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 6546 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 6547 | |
| 6548 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6549 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6550 | |
| 6551 | /* critical error. */ |
| 6552 | if (ret == -2 || ret == -3) { |
| 6553 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 6554 | rule->arg.hlua_rule->fcn.name); |
| 6555 | goto error; |
| 6556 | } |
| 6557 | |
| 6558 | /* no enough space error. */ |
| 6559 | if (ret == -1) { |
| 6560 | si_applet_cant_put(si); |
| 6561 | return; |
| 6562 | } |
| 6563 | |
| 6564 | /* set the last chunk sent. */ |
| 6565 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6566 | } |
| 6567 | |
| 6568 | /* close the connection. */ |
| 6569 | |
| 6570 | /* status / log */ |
| 6571 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6572 | strm->logs.tv_request = now; |
| 6573 | |
| 6574 | /* eat the whole request */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6575 | co_skip(si_oc(si), si_ob(si)->o); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6576 | res->flags |= CF_READ_NULL; |
| 6577 | si_shutr(si); |
| 6578 | |
| 6579 | return; |
| 6580 | } |
| 6581 | |
| 6582 | error: |
| 6583 | |
| 6584 | /* If we are in HTTP mode, and we are not send any |
| 6585 | * data, return a 500 server error in best effort: |
| 6586 | * if there are no room avalaible in the buffer, |
| 6587 | * just close the connection. |
| 6588 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6589 | ci_putblk(res, error_500, strlen(error_500)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6590 | if (!(strm->flags & SF_ERR_MASK)) |
| 6591 | strm->flags |= SF_ERR_RESOURCE; |
| 6592 | si_shutw(si); |
| 6593 | si_shutr(si); |
| 6594 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6595 | } |
| 6596 | |
| 6597 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6598 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6599 | task_delete(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6600 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6601 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6602 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6603 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6604 | } |
| 6605 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6606 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6607 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6608 | * |
| 6609 | * This function can fail with an abort() due to an Lua critical error. |
| 6610 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6611 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6612 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6613 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6614 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6615 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6616 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6617 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6618 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6619 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6620 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6621 | if (!rule->arg.hlua_rule) { |
| 6622 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6623 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6624 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6625 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6626 | /* Memory for arguments. */ |
| 6627 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6628 | if (!rule->arg.hlua_rule->args) { |
| 6629 | memprintf(err, "out of memory error"); |
| 6630 | return ACT_RET_PRS_ERR; |
| 6631 | } |
| 6632 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6633 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6634 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6635 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6636 | /* Expect some arguments */ |
| 6637 | for (i = 0; i < fcn->nargs; i++) { |
| 6638 | if (*args[i+1] == '\0') { |
| 6639 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6640 | return ACT_RET_PRS_ERR; |
| 6641 | } |
| 6642 | rule->arg.hlua_rule->args[i] = strdup(args[i + 1]); |
| 6643 | if (!rule->arg.hlua_rule->args[i]) { |
| 6644 | memprintf(err, "out of memory error"); |
| 6645 | return ACT_RET_PRS_ERR; |
| 6646 | } |
| 6647 | (*cur_arg)++; |
| 6648 | } |
| 6649 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6650 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6651 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6652 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6653 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6654 | } |
| 6655 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6656 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6657 | struct act_rule *rule, char **err) |
| 6658 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6659 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6660 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6661 | /* HTTP applets are forbidden in tcp-request rules. |
| 6662 | * HTTP applet request requires everything initilized by |
| 6663 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6664 | * The applet will be immediately initilized, but its before |
| 6665 | * the call of this analyzer. |
| 6666 | */ |
| 6667 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6668 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6669 | return ACT_RET_PRS_ERR; |
| 6670 | } |
| 6671 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6672 | /* Memory for the rule. */ |
| 6673 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6674 | if (!rule->arg.hlua_rule) { |
| 6675 | memprintf(err, "out of memory error"); |
| 6676 | return ACT_RET_PRS_ERR; |
| 6677 | } |
| 6678 | |
| 6679 | /* Reference the Lua function and store the reference. */ |
| 6680 | rule->arg.hlua_rule->fcn = *fcn; |
| 6681 | |
| 6682 | /* TODO: later accept arguments. */ |
| 6683 | rule->arg.hlua_rule->args = NULL; |
| 6684 | |
| 6685 | /* Add applet pointer in the rule. */ |
| 6686 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6687 | rule->applet.name = fcn->name; |
| 6688 | rule->applet.init = hlua_applet_http_init; |
| 6689 | rule->applet.fct = hlua_applet_http_fct; |
| 6690 | rule->applet.release = hlua_applet_http_release; |
| 6691 | rule->applet.timeout = hlua_timeout_applet; |
| 6692 | |
| 6693 | return ACT_RET_PRS_OK; |
| 6694 | } |
| 6695 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6696 | /* This function is an LUA binding used for registering |
| 6697 | * "sample-conv" functions. It expects a converter name used |
| 6698 | * in the haproxy configuration file, and an LUA function. |
| 6699 | */ |
| 6700 | __LJMP static int hlua_register_action(lua_State *L) |
| 6701 | { |
| 6702 | struct action_kw_list *akl; |
| 6703 | const char *name; |
| 6704 | int ref; |
| 6705 | int len; |
| 6706 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6707 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6708 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6709 | /* Initialise the number of expected arguments at 0. */ |
| 6710 | nargs = 0; |
| 6711 | |
| 6712 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6713 | 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] | 6714 | |
| 6715 | /* First argument : converter name. */ |
| 6716 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6717 | |
| 6718 | /* Second argument : environment. */ |
| 6719 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6720 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6721 | |
| 6722 | /* Third argument : lua function. */ |
| 6723 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6724 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6725 | /* Fouth argument : number of mandatories arguments expected on the configuration line. */ |
| 6726 | if (lua_gettop(L) >= 4) |
| 6727 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6728 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6729 | /* browse the second argulent as an array. */ |
| 6730 | lua_pushnil(L); |
| 6731 | while (lua_next(L, 2) != 0) { |
| 6732 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6733 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6734 | |
| 6735 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6736 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6737 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6738 | if (!akl) |
| 6739 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6740 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6741 | if (!fcn) |
| 6742 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6743 | |
| 6744 | /* Fill fcn. */ |
| 6745 | fcn->name = strdup(name); |
| 6746 | if (!fcn->name) |
| 6747 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6748 | fcn->function_ref = ref; |
| 6749 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6750 | /* Set the expected number od arguments. */ |
| 6751 | fcn->nargs = nargs; |
| 6752 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6753 | /* List head */ |
| 6754 | akl->list.n = akl->list.p = NULL; |
| 6755 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6756 | /* action keyword. */ |
| 6757 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6758 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6759 | if (!akl->kw[0].kw) |
| 6760 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6761 | |
| 6762 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6763 | |
| 6764 | akl->kw[0].match_pfx = 0; |
| 6765 | akl->kw[0].private = fcn; |
| 6766 | akl->kw[0].parse = action_register_lua; |
| 6767 | |
| 6768 | /* select the action registering point. */ |
| 6769 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6770 | tcp_req_cont_keywords_register(akl); |
| 6771 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6772 | tcp_res_cont_keywords_register(akl); |
| 6773 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6774 | http_req_keywords_register(akl); |
| 6775 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6776 | http_res_keywords_register(akl); |
| 6777 | else |
| 6778 | WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. " |
| 6779 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6780 | "are expected.", lua_tostring(L, -1))); |
| 6781 | |
| 6782 | /* pop the environment string. */ |
| 6783 | lua_pop(L, 1); |
| 6784 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6785 | return ACT_RET_PRS_OK; |
| 6786 | } |
| 6787 | |
| 6788 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6789 | struct act_rule *rule, char **err) |
| 6790 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6791 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6792 | |
| 6793 | /* Memory for the rule. */ |
| 6794 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6795 | if (!rule->arg.hlua_rule) { |
| 6796 | memprintf(err, "out of memory error"); |
| 6797 | return ACT_RET_PRS_ERR; |
| 6798 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6799 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6800 | /* Reference the Lua function and store the reference. */ |
| 6801 | rule->arg.hlua_rule->fcn = *fcn; |
| 6802 | |
| 6803 | /* TODO: later accept arguments. */ |
| 6804 | rule->arg.hlua_rule->args = NULL; |
| 6805 | |
| 6806 | /* Add applet pointer in the rule. */ |
| 6807 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6808 | rule->applet.name = fcn->name; |
| 6809 | rule->applet.init = hlua_applet_tcp_init; |
| 6810 | rule->applet.fct = hlua_applet_tcp_fct; |
| 6811 | rule->applet.release = hlua_applet_tcp_release; |
| 6812 | rule->applet.timeout = hlua_timeout_applet; |
| 6813 | |
| 6814 | return 0; |
| 6815 | } |
| 6816 | |
| 6817 | /* This function is an LUA binding used for registering |
| 6818 | * "sample-conv" functions. It expects a converter name used |
| 6819 | * in the haproxy configuration file, and an LUA function. |
| 6820 | */ |
| 6821 | __LJMP static int hlua_register_service(lua_State *L) |
| 6822 | { |
| 6823 | struct action_kw_list *akl; |
| 6824 | const char *name; |
| 6825 | const char *env; |
| 6826 | int ref; |
| 6827 | int len; |
| 6828 | struct hlua_function *fcn; |
| 6829 | |
| 6830 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 6831 | |
| 6832 | /* First argument : converter name. */ |
| 6833 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6834 | |
| 6835 | /* Second argument : environment. */ |
| 6836 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6837 | |
| 6838 | /* Third argument : lua function. */ |
| 6839 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6840 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6841 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6842 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 6843 | if (!akl) |
| 6844 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6845 | fcn = calloc(1, sizeof(*fcn)); |
| 6846 | if (!fcn) |
| 6847 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6848 | |
| 6849 | /* Fill fcn. */ |
| 6850 | len = strlen("<lua.>") + strlen(name) + 1; |
| 6851 | fcn->name = calloc(1, len); |
| 6852 | if (!fcn->name) |
| 6853 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6854 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 6855 | fcn->function_ref = ref; |
| 6856 | |
| 6857 | /* List head */ |
| 6858 | akl->list.n = akl->list.p = NULL; |
| 6859 | |
| 6860 | /* converter keyword. */ |
| 6861 | len = strlen("lua.") + strlen(name) + 1; |
| 6862 | akl->kw[0].kw = calloc(1, len); |
| 6863 | if (!akl->kw[0].kw) |
| 6864 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6865 | |
| 6866 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6867 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 6868 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6869 | if (strcmp(env, "tcp") == 0) |
| 6870 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6871 | else if (strcmp(env, "http") == 0) |
| 6872 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6873 | else |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6874 | WILL_LJMP(luaL_error(L, "lua service environment '%s' is unknown. " |
| 6875 | "'tcp' or 'http' are expected.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6876 | |
| 6877 | akl->kw[0].match_pfx = 0; |
| 6878 | akl->kw[0].private = fcn; |
| 6879 | |
| 6880 | /* End of array. */ |
| 6881 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 6882 | |
| 6883 | /* Register this new converter */ |
| 6884 | service_keywords_register(akl); |
| 6885 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6886 | return 0; |
| 6887 | } |
| 6888 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6889 | /* This function initialises Lua cli handler. It copies the |
| 6890 | * arguments in the Lua stack and create channel IO objects. |
| 6891 | */ |
| 6892 | static int hlua_cli_parse_fct(char **args, struct appctx *appctx, void *private) |
| 6893 | { |
| 6894 | struct hlua *hlua; |
| 6895 | struct hlua_function *fcn; |
| 6896 | int i; |
| 6897 | const char *error; |
| 6898 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6899 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6900 | appctx->ctx.hlua_cli.fcn = private; |
| 6901 | |
| 6902 | hlua = pool_alloc2(pool2_hlua); |
| 6903 | if (!hlua) { |
| 6904 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6905 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6906 | } |
| 6907 | HLUA_INIT(hlua); |
| 6908 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6909 | |
| 6910 | /* Create task used by signal to wakeup applets. |
| 6911 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 6912 | * applet_http. It is absolutely compatible. |
| 6913 | */ |
Thierry FOURNIER | 4325ab7 | 2017-07-12 11:53:38 +0200 | [diff] [blame] | 6914 | appctx->ctx.hlua_cli.task = task_new(1UL << tid); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6915 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 6916 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6917 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6918 | } |
| 6919 | appctx->ctx.hlua_cli.task->nice = 0; |
| 6920 | appctx->ctx.hlua_cli.task->context = appctx; |
| 6921 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 6922 | |
| 6923 | /* Initialises the Lua context */ |
| 6924 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 6925 | 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] | 6926 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6927 | } |
| 6928 | |
| 6929 | /* The following Lua calls can fail. */ |
| 6930 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 6931 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6932 | error = lua_tostring(hlua->T, -1); |
| 6933 | else |
| 6934 | error = "critical error"; |
| 6935 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 6936 | goto error; |
| 6937 | } |
| 6938 | |
| 6939 | /* Check stack available size. */ |
| 6940 | if (!lua_checkstack(hlua->T, 2)) { |
| 6941 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6942 | goto error; |
| 6943 | } |
| 6944 | |
| 6945 | /* Restore the function in the stack. */ |
| 6946 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 6947 | |
| 6948 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 6949 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6950 | */ |
| 6951 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 6952 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6953 | goto error; |
| 6954 | } |
| 6955 | hlua->nargs = 1; |
| 6956 | |
| 6957 | /* push keywords in the stack. */ |
| 6958 | for (i = 0; *args[i]; i++) { |
| 6959 | /* Check stack available size. */ |
| 6960 | if (!lua_checkstack(hlua->T, 1)) { |
| 6961 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6962 | goto error; |
| 6963 | } |
| 6964 | lua_pushstring(hlua->T, args[i]); |
| 6965 | hlua->nargs++; |
| 6966 | } |
| 6967 | |
| 6968 | /* We must initialize the execution timeouts. */ |
| 6969 | hlua->max_time = hlua_timeout_session; |
| 6970 | |
| 6971 | /* At this point the execution is safe. */ |
| 6972 | RESET_SAFE_LJMP(hlua->T); |
| 6973 | |
| 6974 | /* It's ok */ |
| 6975 | return 0; |
| 6976 | |
| 6977 | /* It's not ok. */ |
| 6978 | error: |
| 6979 | RESET_SAFE_LJMP(hlua->T); |
| 6980 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6981 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6982 | return 1; |
| 6983 | } |
| 6984 | |
| 6985 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 6986 | { |
| 6987 | struct hlua *hlua; |
| 6988 | struct stream_interface *si; |
| 6989 | struct hlua_function *fcn; |
| 6990 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6991 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6992 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 6993 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6994 | |
| 6995 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6996 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6997 | return 1; |
| 6998 | |
| 6999 | /* Execute the function. */ |
| 7000 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7001 | |
| 7002 | /* finished. */ |
| 7003 | case HLUA_E_OK: |
| 7004 | return 1; |
| 7005 | |
| 7006 | /* yield. */ |
| 7007 | case HLUA_E_AGAIN: |
| 7008 | /* We want write. */ |
| 7009 | if (HLUA_IS_WAKERESWR(hlua)) |
| 7010 | si_applet_cant_put(si); |
| 7011 | /* Set the timeout. */ |
| 7012 | if (hlua->wake_time != TICK_ETERNITY) |
| 7013 | task_schedule(hlua->task, hlua->wake_time); |
| 7014 | return 0; |
| 7015 | |
| 7016 | /* finished with error. */ |
| 7017 | case HLUA_E_ERRMSG: |
| 7018 | /* Display log. */ |
| 7019 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7020 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7021 | lua_pop(hlua->T, 1); |
| 7022 | return 1; |
| 7023 | |
| 7024 | case HLUA_E_ERR: |
| 7025 | /* Display log. */ |
| 7026 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7027 | fcn->name); |
| 7028 | return 1; |
| 7029 | |
| 7030 | default: |
| 7031 | return 1; |
| 7032 | } |
| 7033 | |
| 7034 | return 1; |
| 7035 | } |
| 7036 | |
| 7037 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7038 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7039 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7040 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7041 | } |
| 7042 | |
| 7043 | /* This function is an LUA binding used for registering |
| 7044 | * new keywords in the cli. It expects a list of keywords |
| 7045 | * which are the "path". It is limited to 5 keywords. A |
| 7046 | * description of the command, a function to be executed |
| 7047 | * for the parsing and a function for io handlers. |
| 7048 | */ |
| 7049 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7050 | { |
| 7051 | struct cli_kw_list *cli_kws; |
| 7052 | const char *message; |
| 7053 | int ref_io; |
| 7054 | int len; |
| 7055 | struct hlua_function *fcn; |
| 7056 | int index; |
| 7057 | int i; |
| 7058 | |
| 7059 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7060 | |
| 7061 | /* First argument : an array of maximum 5 keywords. */ |
| 7062 | if (!lua_istable(L, 1)) |
| 7063 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7064 | |
| 7065 | /* Second argument : string with contextual message. */ |
| 7066 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7067 | |
| 7068 | /* Third and fourth argument : lua function. */ |
| 7069 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7070 | |
| 7071 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7072 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7073 | if (!cli_kws) |
| 7074 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 7075 | fcn = calloc(1, sizeof(*fcn)); |
| 7076 | if (!fcn) |
| 7077 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 7078 | |
| 7079 | /* Fill path. */ |
| 7080 | index = 0; |
| 7081 | lua_pushnil(L); |
| 7082 | while(lua_next(L, 1) != 0) { |
| 7083 | if (index >= 5) |
| 7084 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7085 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7086 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7087 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7088 | if (!cli_kws->kw[0].str_kw[index]) |
| 7089 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 7090 | index++; |
| 7091 | lua_pop(L, 1); |
| 7092 | } |
| 7093 | |
| 7094 | /* Copy help message. */ |
| 7095 | cli_kws->kw[0].usage = strdup(message); |
| 7096 | if (!cli_kws->kw[0].usage) |
| 7097 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 7098 | |
| 7099 | /* Fill fcn io handler. */ |
| 7100 | len = strlen("<lua.cli>") + 1; |
| 7101 | for (i = 0; i < index; i++) |
| 7102 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7103 | fcn->name = calloc(1, len); |
| 7104 | if (!fcn->name) |
| 7105 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 7106 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7107 | for (i = 0; i < index; i++) { |
| 7108 | strncat((char *)fcn->name, ".", len); |
| 7109 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7110 | } |
| 7111 | strncat((char *)fcn->name, ">", len); |
| 7112 | fcn->function_ref = ref_io; |
| 7113 | |
| 7114 | /* Fill last entries. */ |
| 7115 | cli_kws->kw[0].private = fcn; |
| 7116 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7117 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7118 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7119 | |
| 7120 | /* Register this new converter */ |
| 7121 | cli_register_kw(cli_kws); |
| 7122 | |
| 7123 | return 0; |
| 7124 | } |
| 7125 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7126 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7127 | struct proxy *defpx, const char *file, int line, |
| 7128 | char **err, unsigned int *timeout) |
| 7129 | { |
| 7130 | const char *error; |
| 7131 | |
| 7132 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 7133 | if (error && *error != '\0') { |
| 7134 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7135 | return -1; |
| 7136 | } |
| 7137 | return 0; |
| 7138 | } |
| 7139 | |
| 7140 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7141 | struct proxy *defpx, const char *file, int line, |
| 7142 | char **err) |
| 7143 | { |
| 7144 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7145 | file, line, err, &hlua_timeout_session); |
| 7146 | } |
| 7147 | |
| 7148 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7149 | struct proxy *defpx, const char *file, int line, |
| 7150 | char **err) |
| 7151 | { |
| 7152 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7153 | file, line, err, &hlua_timeout_task); |
| 7154 | } |
| 7155 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7156 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7157 | struct proxy *defpx, const char *file, int line, |
| 7158 | char **err) |
| 7159 | { |
| 7160 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7161 | file, line, err, &hlua_timeout_applet); |
| 7162 | } |
| 7163 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7164 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7165 | struct proxy *defpx, const char *file, int line, |
| 7166 | char **err) |
| 7167 | { |
| 7168 | char *error; |
| 7169 | |
| 7170 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7171 | if (*error != '\0') { |
| 7172 | memprintf(err, "%s: invalid number", args[0]); |
| 7173 | return -1; |
| 7174 | } |
| 7175 | return 0; |
| 7176 | } |
| 7177 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7178 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7179 | struct proxy *defpx, const char *file, int line, |
| 7180 | char **err) |
| 7181 | { |
| 7182 | char *error; |
| 7183 | |
| 7184 | if (*(args[1]) == 0) { |
| 7185 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7186 | return -1; |
| 7187 | } |
| 7188 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7189 | if (*error != '\0') { |
| 7190 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7191 | return -1; |
| 7192 | } |
| 7193 | return 0; |
| 7194 | } |
| 7195 | |
| 7196 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7197 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7198 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7199 | * the main lua entry point. |
| 7200 | * |
| 7201 | * This funtion runs with the HAProxy keywords API. It returns -1 if an error is |
| 7202 | * occured, otherwise it returns 0. |
| 7203 | * |
| 7204 | * In some error case, LUA set an error message in top of the stack. This function |
| 7205 | * 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] | 7206 | * |
| 7207 | * This function can fail with an abort() due to an Lua critical error. |
| 7208 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7209 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7210 | */ |
| 7211 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7212 | struct proxy *defpx, const char *file, int line, |
| 7213 | char **err) |
| 7214 | { |
| 7215 | int error; |
| 7216 | |
| 7217 | /* Just load and compile the file. */ |
| 7218 | error = luaL_loadfile(gL.T, args[1]); |
| 7219 | if (error) { |
| 7220 | memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1)); |
| 7221 | lua_pop(gL.T, 1); |
| 7222 | return -1; |
| 7223 | } |
| 7224 | |
| 7225 | /* If no syntax error where detected, execute the code. */ |
| 7226 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7227 | switch (error) { |
| 7228 | case LUA_OK: |
| 7229 | break; |
| 7230 | case LUA_ERRRUN: |
| 7231 | memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
| 7232 | lua_pop(gL.T, 1); |
| 7233 | return -1; |
| 7234 | case LUA_ERRMEM: |
| 7235 | memprintf(err, "lua out of memory error\n"); |
| 7236 | return -1; |
| 7237 | case LUA_ERRERR: |
| 7238 | memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1)); |
| 7239 | lua_pop(gL.T, 1); |
| 7240 | return -1; |
| 7241 | case LUA_ERRGCMM: |
| 7242 | memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1)); |
| 7243 | lua_pop(gL.T, 1); |
| 7244 | return -1; |
| 7245 | default: |
| 7246 | memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
| 7247 | lua_pop(gL.T, 1); |
| 7248 | return -1; |
| 7249 | } |
| 7250 | |
| 7251 | return 0; |
| 7252 | } |
| 7253 | |
| 7254 | /* configuration keywords declaration */ |
| 7255 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7256 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7257 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7258 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7259 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7260 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7261 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7262 | { 0, NULL, NULL }, |
| 7263 | }}; |
| 7264 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7265 | /* This function can fail with an abort() due to an Lua critical error. |
| 7266 | * We are in the initialisation process of HAProxy, this abort() is |
| 7267 | * tolerated. |
| 7268 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7269 | int hlua_post_init() |
| 7270 | { |
| 7271 | struct hlua_init_function *init; |
| 7272 | const char *msg; |
| 7273 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7274 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7275 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7276 | /* Call post initialisation function in safe environement. */ |
| 7277 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7278 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7279 | error = lua_tostring(gL.T, -1); |
| 7280 | else |
| 7281 | error = "critical error"; |
| 7282 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7283 | exit(1); |
| 7284 | } |
| 7285 | hlua_fcn_post_init(gL.T); |
| 7286 | RESET_SAFE_LJMP(gL.T); |
| 7287 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7288 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7289 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7290 | ret = hlua_ctx_resume(&gL, 0); |
| 7291 | switch (ret) { |
| 7292 | case HLUA_E_OK: |
| 7293 | lua_pop(gL.T, -1); |
| 7294 | return 1; |
| 7295 | case HLUA_E_AGAIN: |
| 7296 | Alert("lua init: yield not allowed.\n"); |
| 7297 | return 0; |
| 7298 | case HLUA_E_ERRMSG: |
| 7299 | msg = lua_tostring(gL.T, -1); |
| 7300 | Alert("lua init: %s.\n", msg); |
| 7301 | return 0; |
| 7302 | case HLUA_E_ERR: |
| 7303 | default: |
| 7304 | Alert("lua init: unknown runtime error.\n"); |
| 7305 | return 0; |
| 7306 | } |
| 7307 | } |
| 7308 | return 1; |
| 7309 | } |
| 7310 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7311 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7312 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7313 | * is the previously allocated size or the kind of object in case of a new |
| 7314 | * allocation. <nsize> is the requested new size. |
| 7315 | */ |
| 7316 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7317 | { |
| 7318 | struct hlua_mem_allocator *zone = ud; |
| 7319 | |
| 7320 | if (nsize == 0) { |
| 7321 | /* it's a free */ |
| 7322 | if (ptr) |
| 7323 | zone->allocated -= osize; |
| 7324 | free(ptr); |
| 7325 | return NULL; |
| 7326 | } |
| 7327 | |
| 7328 | if (!ptr) { |
| 7329 | /* it's a new allocation */ |
| 7330 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7331 | return NULL; |
| 7332 | |
| 7333 | ptr = malloc(nsize); |
| 7334 | if (ptr) |
| 7335 | zone->allocated += nsize; |
| 7336 | return ptr; |
| 7337 | } |
| 7338 | |
| 7339 | /* it's a realloc */ |
| 7340 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7341 | return NULL; |
| 7342 | |
| 7343 | ptr = realloc(ptr, nsize); |
| 7344 | if (ptr) |
| 7345 | zone->allocated += nsize - osize; |
| 7346 | return ptr; |
| 7347 | } |
| 7348 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7349 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7350 | * We are in the initialisation process of HAProxy, this abort() is |
| 7351 | * tolerated. |
| 7352 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7353 | void hlua_init(void) |
| 7354 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7355 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7356 | int idx; |
| 7357 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7358 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7359 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7360 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7361 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7362 | struct srv_kw *kw; |
| 7363 | int tmp_error; |
| 7364 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7365 | char *args[] = { /* SSL client configuration. */ |
| 7366 | "ssl", |
| 7367 | "verify", |
| 7368 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7369 | NULL |
| 7370 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7371 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7372 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 7373 | SPIN_INIT(&hlua_global_lock); |
| 7374 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7375 | /* Initialise struct hlua and com signals pool */ |
| 7376 | pool2_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7377 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7378 | /* Register configuration keywords. */ |
| 7379 | cfg_register_keywords(&cfg_kws); |
| 7380 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7381 | /* Init main lua stack. */ |
| 7382 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7383 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7384 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7385 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7386 | hlua_sethlua(&gL); |
| 7387 | gL.Tref = LUA_REFNIL; |
| 7388 | gL.task = NULL; |
| 7389 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7390 | /* From this point, until the end of the initialisation fucntion, |
| 7391 | * the Lua function can fail with an abort. We are in the initialisation |
| 7392 | * process of HAProxy, this abort() is tolerated. |
| 7393 | */ |
| 7394 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7395 | /* Initialise lua. */ |
| 7396 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7397 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7398 | /* Set safe environment for the initialisation. */ |
| 7399 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7400 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7401 | error_msg = lua_tostring(gL.T, -1); |
| 7402 | else |
| 7403 | error_msg = "critical error"; |
| 7404 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7405 | exit(1); |
| 7406 | } |
| 7407 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7408 | /* |
| 7409 | * |
| 7410 | * Create "core" object. |
| 7411 | * |
| 7412 | */ |
| 7413 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7414 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7415 | lua_newtable(gL.T); |
| 7416 | |
| 7417 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7418 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7419 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7420 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7421 | /* Register special functions. */ |
| 7422 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7423 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7424 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7425 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7426 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7427 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7428 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7429 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7430 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7431 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7432 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7433 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7434 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7435 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7436 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7437 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7438 | hlua_class_function(gL.T, "log", hlua_log); |
| 7439 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7440 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7441 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7442 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7443 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7444 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7445 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7446 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7447 | |
| 7448 | /* |
| 7449 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7450 | * Register class Map |
| 7451 | * |
| 7452 | */ |
| 7453 | |
| 7454 | /* This table entry is the object "Map" base. */ |
| 7455 | lua_newtable(gL.T); |
| 7456 | |
| 7457 | /* register pattern types. */ |
| 7458 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7459 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7460 | for (i=0; i<PAT_MATCH_NUM; i++) { |
| 7461 | snprintf(trash.str, trash.size, "_%s", pat_match_names[i]); |
| 7462 | hlua_class_const_int(gL.T, trash.str, i); |
| 7463 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7464 | |
| 7465 | /* register constructor. */ |
| 7466 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7467 | |
| 7468 | /* Create and fill the metatable. */ |
| 7469 | lua_newtable(gL.T); |
| 7470 | |
| 7471 | /* Create and fille the __index entry. */ |
| 7472 | lua_pushstring(gL.T, "__index"); |
| 7473 | lua_newtable(gL.T); |
| 7474 | |
| 7475 | /* Register . */ |
| 7476 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7477 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7478 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7479 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7480 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7481 | /* Register previous table in the registry with reference and named entry. |
| 7482 | * The function hlua_register_metatable() pops the stack, so we |
| 7483 | * previously create a copy of the table. |
| 7484 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7485 | 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] | 7486 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7487 | |
| 7488 | /* Assign the metatable to the mai Map object. */ |
| 7489 | lua_setmetatable(gL.T, -2); |
| 7490 | |
| 7491 | /* Set a name to the table. */ |
| 7492 | lua_setglobal(gL.T, "Map"); |
| 7493 | |
| 7494 | /* |
| 7495 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7496 | * Register class Channel |
| 7497 | * |
| 7498 | */ |
| 7499 | |
| 7500 | /* Create and fill the metatable. */ |
| 7501 | lua_newtable(gL.T); |
| 7502 | |
| 7503 | /* Create and fille the __index entry. */ |
| 7504 | lua_pushstring(gL.T, "__index"); |
| 7505 | lua_newtable(gL.T); |
| 7506 | |
| 7507 | /* Register . */ |
| 7508 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7509 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7510 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7511 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7512 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7513 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7514 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7515 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7516 | 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] | 7517 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7518 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7519 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7520 | |
| 7521 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7522 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7523 | |
| 7524 | /* |
| 7525 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7526 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7527 | * |
| 7528 | */ |
| 7529 | |
| 7530 | /* Create and fill the metatable. */ |
| 7531 | lua_newtable(gL.T); |
| 7532 | |
| 7533 | /* Create and fille the __index entry. */ |
| 7534 | lua_pushstring(gL.T, "__index"); |
| 7535 | lua_newtable(gL.T); |
| 7536 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7537 | /* Browse existing fetches and create the associated |
| 7538 | * object method. |
| 7539 | */ |
| 7540 | sf = NULL; |
| 7541 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7542 | |
| 7543 | /* Dont register the keywork if the arguments check function are |
| 7544 | * not safe during the runtime. |
| 7545 | */ |
| 7546 | if ((sf->val_args != NULL) && |
| 7547 | (sf->val_args != val_payload_lv) && |
| 7548 | (sf->val_args != val_hdr)) |
| 7549 | continue; |
| 7550 | |
| 7551 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7552 | * by an underscore. |
| 7553 | */ |
| 7554 | strncpy(trash.str, sf->kw, trash.size); |
| 7555 | trash.str[trash.size - 1] = '\0'; |
| 7556 | for (p = trash.str; *p; p++) |
| 7557 | if (*p == '.' || *p == '-' || *p == '+') |
| 7558 | *p = '_'; |
| 7559 | |
| 7560 | /* Register the function. */ |
| 7561 | lua_pushstring(gL.T, trash.str); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7562 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7563 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7564 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7565 | } |
| 7566 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7567 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7568 | |
| 7569 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7570 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7571 | |
| 7572 | /* |
| 7573 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7574 | * Register class Converters |
| 7575 | * |
| 7576 | */ |
| 7577 | |
| 7578 | /* Create and fill the metatable. */ |
| 7579 | lua_newtable(gL.T); |
| 7580 | |
| 7581 | /* Create and fill the __index entry. */ |
| 7582 | lua_pushstring(gL.T, "__index"); |
| 7583 | lua_newtable(gL.T); |
| 7584 | |
| 7585 | /* Browse existing converters and create the associated |
| 7586 | * object method. |
| 7587 | */ |
| 7588 | sc = NULL; |
| 7589 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7590 | /* Dont register the keywork if the arguments check function are |
| 7591 | * not safe during the runtime. |
| 7592 | */ |
| 7593 | if (sc->val_args != NULL) |
| 7594 | continue; |
| 7595 | |
| 7596 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7597 | * by an underscore. |
| 7598 | */ |
| 7599 | strncpy(trash.str, sc->kw, trash.size); |
| 7600 | trash.str[trash.size - 1] = '\0'; |
| 7601 | for (p = trash.str; *p; p++) |
| 7602 | if (*p == '.' || *p == '-' || *p == '+') |
| 7603 | *p = '_'; |
| 7604 | |
| 7605 | /* Register the function. */ |
| 7606 | lua_pushstring(gL.T, trash.str); |
| 7607 | lua_pushlightuserdata(gL.T, sc); |
| 7608 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7609 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7610 | } |
| 7611 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7612 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7613 | |
| 7614 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7615 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7616 | |
| 7617 | /* |
| 7618 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7619 | * Register class HTTP |
| 7620 | * |
| 7621 | */ |
| 7622 | |
| 7623 | /* Create and fill the metatable. */ |
| 7624 | lua_newtable(gL.T); |
| 7625 | |
| 7626 | /* Create and fille the __index entry. */ |
| 7627 | lua_pushstring(gL.T, "__index"); |
| 7628 | lua_newtable(gL.T); |
| 7629 | |
| 7630 | /* Register Lua functions. */ |
| 7631 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7632 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7633 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7634 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7635 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7636 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7637 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7638 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7639 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7640 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7641 | |
| 7642 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7643 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7644 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7645 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7646 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7647 | 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] | 7648 | 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] | 7649 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7650 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7651 | |
| 7652 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7653 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7654 | |
| 7655 | /* |
| 7656 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7657 | * Register class AppletTCP |
| 7658 | * |
| 7659 | */ |
| 7660 | |
| 7661 | /* Create and fill the metatable. */ |
| 7662 | lua_newtable(gL.T); |
| 7663 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7664 | /* Create and fille the __index entry. */ |
| 7665 | lua_pushstring(gL.T, "__index"); |
| 7666 | lua_newtable(gL.T); |
| 7667 | |
| 7668 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7669 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7670 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7671 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7672 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7673 | 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] | 7674 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7675 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7676 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7677 | |
| 7678 | lua_settable(gL.T, -3); |
| 7679 | |
| 7680 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7681 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7682 | |
| 7683 | /* |
| 7684 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7685 | * Register class AppletHTTP |
| 7686 | * |
| 7687 | */ |
| 7688 | |
| 7689 | /* Create and fill the metatable. */ |
| 7690 | lua_newtable(gL.T); |
| 7691 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7692 | /* Create and fille the __index entry. */ |
| 7693 | lua_pushstring(gL.T, "__index"); |
| 7694 | lua_newtable(gL.T); |
| 7695 | |
| 7696 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7697 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7698 | 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] | 7699 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7700 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7701 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7702 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7703 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7704 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7705 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7706 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7707 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 7708 | |
| 7709 | lua_settable(gL.T, -3); |
| 7710 | |
| 7711 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7712 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7713 | |
| 7714 | /* |
| 7715 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7716 | * Register class TXN |
| 7717 | * |
| 7718 | */ |
| 7719 | |
| 7720 | /* Create and fill the metatable. */ |
| 7721 | lua_newtable(gL.T); |
| 7722 | |
| 7723 | /* Create and fille the __index entry. */ |
| 7724 | lua_pushstring(gL.T, "__index"); |
| 7725 | lua_newtable(gL.T); |
| 7726 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7727 | /* Register Lua functions. */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7728 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 7729 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7730 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7731 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7732 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7733 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7734 | hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel); |
| 7735 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 7736 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7737 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 7738 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 7739 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 7740 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 7741 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 7742 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7743 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7744 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7745 | |
| 7746 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7747 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7748 | |
| 7749 | /* |
| 7750 | * |
| 7751 | * Register class Socket |
| 7752 | * |
| 7753 | */ |
| 7754 | |
| 7755 | /* Create and fill the metatable. */ |
| 7756 | lua_newtable(gL.T); |
| 7757 | |
| 7758 | /* Create and fille the __index entry. */ |
| 7759 | lua_pushstring(gL.T, "__index"); |
| 7760 | lua_newtable(gL.T); |
| 7761 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7762 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7763 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7764 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7765 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 7766 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 7767 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 7768 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 7769 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 7770 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 7771 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 7772 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 7773 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7774 | 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] | 7775 | |
| 7776 | /* Register the garbage collector entry. */ |
| 7777 | lua_pushstring(gL.T, "__gc"); |
| 7778 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7779 | 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] | 7780 | |
| 7781 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7782 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7783 | |
| 7784 | /* Proxy and server configuration initialisation. */ |
| 7785 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 7786 | init_new_proxy(&socket_proxy); |
| 7787 | socket_proxy.parent = NULL; |
| 7788 | socket_proxy.last_change = now.tv_sec; |
| 7789 | socket_proxy.id = "LUA-SOCKET"; |
| 7790 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 7791 | socket_proxy.maxconn = 0; |
| 7792 | socket_proxy.accept = NULL; |
| 7793 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 7794 | socket_proxy.srv = NULL; |
| 7795 | socket_proxy.conn_retries = 0; |
| 7796 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 7797 | |
| 7798 | /* Init TCP server: unchanged parameters */ |
| 7799 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 7800 | socket_tcp.next = NULL; |
| 7801 | socket_tcp.proxy = &socket_proxy; |
| 7802 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 7803 | LIST_INIT(&socket_tcp.actconns); |
| 7804 | LIST_INIT(&socket_tcp.pendconns); |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 7805 | socket_tcp.priv_conns = NULL; |
| 7806 | socket_tcp.idle_conns = NULL; |
| 7807 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7808 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7809 | socket_tcp.last_change = 0; |
| 7810 | socket_tcp.id = "LUA-TCP-CONN"; |
| 7811 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7812 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7813 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 7814 | |
| 7815 | /* XXX: Copy default parameter from default server, |
| 7816 | * but the default server is not initialized. |
| 7817 | */ |
| 7818 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7819 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 7820 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 7821 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 7822 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 7823 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7824 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7825 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7826 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 7827 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 7828 | |
| 7829 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 7830 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 7831 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 7832 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 7833 | socket_tcp.check.server = &socket_tcp; |
| 7834 | |
| 7835 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 7836 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7837 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7838 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 7839 | socket_tcp.agent.server = &socket_tcp; |
| 7840 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 7841 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7842 | |
| 7843 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7844 | /* Init TCP server: unchanged parameters */ |
| 7845 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 7846 | socket_ssl.next = NULL; |
| 7847 | socket_ssl.proxy = &socket_proxy; |
| 7848 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 7849 | LIST_INIT(&socket_ssl.actconns); |
| 7850 | LIST_INIT(&socket_ssl.pendconns); |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 7851 | socket_tcp.priv_conns = NULL; |
| 7852 | socket_tcp.idle_conns = NULL; |
| 7853 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7854 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7855 | socket_ssl.last_change = 0; |
| 7856 | socket_ssl.id = "LUA-SSL-CONN"; |
| 7857 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7858 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7859 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 7860 | |
| 7861 | /* XXX: Copy default parameter from default server, |
| 7862 | * but the default server is not initialized. |
| 7863 | */ |
| 7864 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7865 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 7866 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 7867 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 7868 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 7869 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7870 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7871 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7872 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 7873 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 7874 | |
| 7875 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 7876 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 7877 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 7878 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 7879 | socket_ssl.check.server = &socket_ssl; |
| 7880 | |
| 7881 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 7882 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7883 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7884 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 7885 | socket_ssl.agent.server = &socket_ssl; |
| 7886 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7887 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 7888 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7889 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7890 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7891 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 7892 | /* |
| 7893 | * |
| 7894 | * If the keyword is not known, we can search in the registered |
| 7895 | * server keywords. This is usefull to configure special SSL |
| 7896 | * features like client certificates and ssl_verify. |
| 7897 | * |
| 7898 | */ |
| 7899 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 7900 | if (tmp_error != 0) { |
| 7901 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 7902 | abort(); /* This must be never arrives because the command line |
| 7903 | not editable by the user. */ |
| 7904 | } |
| 7905 | idx += kw->skip; |
| 7906 | } |
| 7907 | } |
| 7908 | |
| 7909 | /* Initialize SSL server. */ |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 7910 | if (socket_ssl.xprt->prepare_srv) |
| 7911 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7912 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7913 | |
| 7914 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7915 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 7916 | |
| 7917 | __attribute__((constructor)) |
| 7918 | static void __hlua_init(void) |
| 7919 | { |
| 7920 | char *ptr = NULL; |
| 7921 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 7922 | hap_register_build_opts(ptr, 1); |
| 7923 | } |