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 | */ |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 118 | __decl_hathreads(HA_SPINLOCK_T hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 119 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 120 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 121 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 122 | |
| 123 | #define SET_SAFE_LJMP(__L) \ |
| 124 | ({ \ |
| 125 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 126 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 127 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 128 | lua_atpanic(__L, hlua_panic_safe); \ |
| 129 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 130 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 131 | } else { \ |
| 132 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 133 | ret = 1; \ |
| 134 | } \ |
| 135 | ret; \ |
| 136 | }) |
| 137 | |
| 138 | /* If we are the last function catching Lua errors, we |
| 139 | * must reset the panic function. |
| 140 | */ |
| 141 | #define RESET_SAFE_LJMP(__L) \ |
| 142 | do { \ |
| 143 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 144 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 145 | } while(0) |
| 146 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 147 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 148 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
| 149 | #define APPLET_100C 0x02 /* 100 continue expected. */ |
| 150 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 151 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 152 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 153 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 154 | |
| 155 | #define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n" |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 156 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 157 | /* The main Lua execution context. */ |
| 158 | struct hlua gL; |
| 159 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 160 | /* This is the memory pool containing struct lua for applets |
| 161 | * (including cli). |
| 162 | */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 163 | struct pool_head *pool_head_hlua; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 164 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 165 | /* Used for Socket connection. */ |
| 166 | static struct proxy socket_proxy; |
| 167 | static struct server socket_tcp; |
| 168 | #ifdef USE_OPENSSL |
| 169 | static struct server socket_ssl; |
| 170 | #endif |
| 171 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 172 | /* List head of the function called at the initialisation time. */ |
| 173 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 174 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 175 | /* The following variables contains the reference of the different |
| 176 | * Lua classes. These references are useful for identify metadata |
| 177 | * associated with an object. |
| 178 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 179 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 180 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 181 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 182 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 183 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 184 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 185 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 186 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 187 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 188 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 189 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 190 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 191 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 192 | * because a task may remain alive during all the haproxy execution. |
| 193 | */ |
| 194 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 195 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 196 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 197 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 198 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 199 | * it is used for preventing infinite loops. |
| 200 | * |
| 201 | * I test the scheer with an infinite loop containing one incrementation |
| 202 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 203 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 204 | * to one interrupt each 10 000 instructions. |
| 205 | * |
| 206 | * configured | Number of |
| 207 | * instructions | loops executed |
| 208 | * between two | in milions |
| 209 | * forced yields | |
| 210 | * ---------------+--------------- |
| 211 | * 10 | 160 |
| 212 | * 500 | 670 |
| 213 | * 1000 | 680 |
| 214 | * 5000 | 700 |
| 215 | * 7000 | 700 |
| 216 | * 8000 | 700 |
| 217 | * 9000 | 710 <- ceil |
| 218 | * 10000 | 710 |
| 219 | * 100000 | 710 |
| 220 | * 1000000 | 710 |
| 221 | * |
| 222 | */ |
| 223 | static unsigned int hlua_nb_instruction = 10000; |
| 224 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 225 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 226 | * be enforced on any memory allocation. |
| 227 | */ |
| 228 | struct hlua_mem_allocator { |
| 229 | size_t allocated; |
| 230 | size_t limit; |
| 231 | }; |
| 232 | |
| 233 | static struct hlua_mem_allocator hlua_global_allocator; |
| 234 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 235 | static const char error_500[] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 236 | "HTTP/1.0 500 Internal Server Error\r\n" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 237 | "Cache-Control: no-cache\r\n" |
| 238 | "Connection: close\r\n" |
| 239 | "Content-Type: text/html\r\n" |
| 240 | "\r\n" |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 241 | "<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] | 242 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 243 | /* These functions converts types between HAProxy internal args or |
| 244 | * sample and LUA types. Another function permits to check if the |
| 245 | * LUA stack contains arguments according with an required ARG_T |
| 246 | * format. |
| 247 | */ |
| 248 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 249 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 250 | __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] | 251 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 252 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 253 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 254 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 255 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 256 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 257 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 258 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 259 | do { \ |
| 260 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 261 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 262 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 263 | } while (0) |
| 264 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 265 | /* Used to check an Lua function type in the stack. It creates and |
| 266 | * returns a reference of the function. This function throws an |
| 267 | * error if the rgument is not a "function". |
| 268 | */ |
| 269 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 270 | { |
| 271 | if (!lua_isfunction(L, argno)) { |
| 272 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1)); |
| 273 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 274 | } |
| 275 | lua_pushvalue(L, argno); |
| 276 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 277 | } |
| 278 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 279 | /* Return the string that is of the top of the stack. */ |
| 280 | const char *hlua_get_top_error_string(lua_State *L) |
| 281 | { |
| 282 | if (lua_gettop(L) < 1) |
| 283 | return "unknown error"; |
| 284 | if (lua_type(L, -1) != LUA_TSTRING) |
| 285 | return "unknown error"; |
| 286 | return lua_tostring(L, -1); |
| 287 | } |
| 288 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 289 | /* This function check the number of arguments available in the |
| 290 | * stack. If the number of arguments available is not the same |
| 291 | * then <nb> an error is throwed. |
| 292 | */ |
| 293 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 294 | { |
| 295 | if (lua_gettop(L) == nb) |
| 296 | return; |
| 297 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 298 | } |
| 299 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 300 | /* This fucntion push an error string prefixed by the file name |
| 301 | * and the line number where the error is encountered. |
| 302 | */ |
| 303 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 304 | { |
| 305 | va_list argp; |
| 306 | va_start(argp, fmt); |
| 307 | luaL_where(L, 1); |
| 308 | lua_pushvfstring(L, fmt, argp); |
| 309 | va_end(argp); |
| 310 | lua_concat(L, 2); |
| 311 | return 1; |
| 312 | } |
| 313 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 314 | /* This functions is used with sample fetch and converters. It |
| 315 | * converts the HAProxy configuration argument in a lua stack |
| 316 | * values. |
| 317 | * |
| 318 | * It takes an array of "arg", and each entry of the array is |
| 319 | * converted and pushed in the LUA stack. |
| 320 | */ |
| 321 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 322 | { |
| 323 | switch (arg->type) { |
| 324 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 325 | case ARGT_TIME: |
| 326 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 327 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 328 | break; |
| 329 | |
| 330 | case ARGT_STR: |
| 331 | lua_pushlstring(L, arg->data.str.str, arg->data.str.len); |
| 332 | break; |
| 333 | |
| 334 | case ARGT_IPV4: |
| 335 | case ARGT_IPV6: |
| 336 | case ARGT_MSK4: |
| 337 | case ARGT_MSK6: |
| 338 | case ARGT_FE: |
| 339 | case ARGT_BE: |
| 340 | case ARGT_TAB: |
| 341 | case ARGT_SRV: |
| 342 | case ARGT_USR: |
| 343 | case ARGT_MAP: |
| 344 | default: |
| 345 | lua_pushnil(L); |
| 346 | break; |
| 347 | } |
| 348 | return 1; |
| 349 | } |
| 350 | |
| 351 | /* This function take one entrie in an LUA stack at the index "ud", |
| 352 | * and try to convert it in an HAProxy argument entry. This is useful |
| 353 | * with sample fetch wrappers. The input arguments are gived to the |
| 354 | * lua wrapper and converted as arg list by thi function. |
| 355 | */ |
| 356 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 357 | { |
| 358 | switch (lua_type(L, ud)) { |
| 359 | |
| 360 | case LUA_TNUMBER: |
| 361 | case LUA_TBOOLEAN: |
| 362 | arg->type = ARGT_SINT; |
| 363 | arg->data.sint = lua_tointeger(L, ud); |
| 364 | break; |
| 365 | |
| 366 | case LUA_TSTRING: |
| 367 | arg->type = ARGT_STR; |
| 368 | arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len); |
| 369 | break; |
| 370 | |
| 371 | case LUA_TUSERDATA: |
| 372 | case LUA_TNIL: |
| 373 | case LUA_TTABLE: |
| 374 | case LUA_TFUNCTION: |
| 375 | case LUA_TTHREAD: |
| 376 | case LUA_TLIGHTUSERDATA: |
| 377 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 378 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | return 1; |
| 382 | } |
| 383 | |
| 384 | /* the following functions are used to convert a struct sample |
| 385 | * in Lua type. This useful to convert the return of the |
| 386 | * fetchs or converters. |
| 387 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 388 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 389 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 390 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 391 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 392 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 393 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 394 | break; |
| 395 | |
| 396 | case SMP_T_BIN: |
| 397 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 398 | 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] | 399 | break; |
| 400 | |
| 401 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 402 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 403 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 404 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 405 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 406 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 407 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 408 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 409 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 410 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 411 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 412 | 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] | 413 | break; |
| 414 | default: |
| 415 | lua_pushnil(L); |
| 416 | break; |
| 417 | } |
| 418 | break; |
| 419 | |
| 420 | case SMP_T_IPV4: |
| 421 | case SMP_T_IPV6: |
| 422 | 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] | 423 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 424 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 425 | 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] | 426 | else |
| 427 | lua_pushnil(L); |
| 428 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 429 | default: |
| 430 | lua_pushnil(L); |
| 431 | break; |
| 432 | } |
| 433 | return 1; |
| 434 | } |
| 435 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 436 | /* the following functions are used to convert a struct sample |
| 437 | * in Lua strings. This is useful to convert the return of the |
| 438 | * fetchs or converters. |
| 439 | */ |
| 440 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 441 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 442 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 443 | |
| 444 | case SMP_T_BIN: |
| 445 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 446 | 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] | 447 | break; |
| 448 | |
| 449 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 450 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 451 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 452 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 453 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 454 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 455 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 456 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 457 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 458 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 459 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 460 | 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] | 461 | break; |
| 462 | default: |
| 463 | lua_pushstring(L, ""); |
| 464 | break; |
| 465 | } |
| 466 | break; |
| 467 | |
| 468 | case SMP_T_SINT: |
| 469 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 470 | case SMP_T_IPV4: |
| 471 | case SMP_T_IPV6: |
| 472 | 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] | 473 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 474 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 475 | 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] | 476 | else |
| 477 | lua_pushstring(L, ""); |
| 478 | break; |
| 479 | default: |
| 480 | lua_pushstring(L, ""); |
| 481 | break; |
| 482 | } |
| 483 | return 1; |
| 484 | } |
| 485 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 486 | /* the following functions are used to convert an Lua type in a |
| 487 | * struct sample. This is useful to provide data from a converter |
| 488 | * to the LUA code. |
| 489 | */ |
| 490 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 491 | { |
| 492 | switch (lua_type(L, ud)) { |
| 493 | |
| 494 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 495 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 496 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 497 | break; |
| 498 | |
| 499 | |
| 500 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 501 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 502 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 503 | break; |
| 504 | |
| 505 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 506 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 507 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 508 | 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] | 509 | break; |
| 510 | |
| 511 | case LUA_TUSERDATA: |
| 512 | case LUA_TNIL: |
| 513 | case LUA_TTABLE: |
| 514 | case LUA_TFUNCTION: |
| 515 | case LUA_TTHREAD: |
| 516 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 517 | case LUA_TNONE: |
| 518 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 519 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 520 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 521 | break; |
| 522 | } |
| 523 | return 1; |
| 524 | } |
| 525 | |
| 526 | /* This function check the "argp" builded by another conversion function |
| 527 | * is in accord with the expected argp defined by the "mask". The fucntion |
| 528 | * 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] | 529 | * |
| 530 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 531 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 532 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 533 | __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] | 534 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 535 | { |
| 536 | int min_arg; |
| 537 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 538 | struct proxy *px; |
| 539 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 540 | |
| 541 | idx = 0; |
| 542 | min_arg = ARGM(mask); |
| 543 | mask >>= ARGM_BITS; |
| 544 | |
| 545 | while (1) { |
| 546 | |
| 547 | /* Check oversize. */ |
| 548 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 549 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /* Check for mandatory arguments. */ |
| 553 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 554 | if (idx < min_arg) { |
| 555 | |
| 556 | /* If miss other argument than the first one, we return an error. */ |
| 557 | if (idx > 0) |
| 558 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 559 | |
| 560 | /* If first argument have a certain type, some default values |
| 561 | * may be used. See the function smp_resolve_args(). |
| 562 | */ |
| 563 | switch (mask & ARGT_MASK) { |
| 564 | |
| 565 | case ARGT_FE: |
| 566 | if (!(p->cap & PR_CAP_FE)) |
| 567 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 568 | argp[idx].data.prx = p; |
| 569 | argp[idx].type = ARGT_FE; |
| 570 | argp[idx+1].type = ARGT_STOP; |
| 571 | break; |
| 572 | |
| 573 | case ARGT_BE: |
| 574 | if (!(p->cap & PR_CAP_BE)) |
| 575 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 576 | argp[idx].data.prx = p; |
| 577 | argp[idx].type = ARGT_BE; |
| 578 | argp[idx+1].type = ARGT_STOP; |
| 579 | break; |
| 580 | |
| 581 | case ARGT_TAB: |
| 582 | argp[idx].data.prx = p; |
| 583 | argp[idx].type = ARGT_TAB; |
| 584 | argp[idx+1].type = ARGT_STOP; |
| 585 | break; |
| 586 | |
| 587 | default: |
| 588 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 589 | break; |
| 590 | } |
| 591 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | /* Check for exceed the number of requiered argument. */ |
| 596 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 597 | argp[idx].type != ARGT_STOP) { |
| 598 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 599 | } |
| 600 | |
| 601 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 602 | argp[idx].type == ARGT_STOP) { |
| 603 | return 0; |
| 604 | } |
| 605 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 606 | /* Convert some argument types. */ |
| 607 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 608 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 609 | if (argp[idx].type != ARGT_SINT) |
| 610 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 611 | argp[idx].type = ARGT_SINT; |
| 612 | break; |
| 613 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 614 | case ARGT_TIME: |
| 615 | if (argp[idx].type != ARGT_SINT) |
| 616 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 617 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 618 | break; |
| 619 | |
| 620 | case ARGT_SIZE: |
| 621 | if (argp[idx].type != ARGT_SINT) |
| 622 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 623 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 624 | break; |
| 625 | |
| 626 | case ARGT_FE: |
| 627 | if (argp[idx].type != ARGT_STR) |
| 628 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 629 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 630 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 631 | argp[idx].data.prx = proxy_fe_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 632 | if (!argp[idx].data.prx) |
| 633 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 634 | argp[idx].type = ARGT_FE; |
| 635 | break; |
| 636 | |
| 637 | case ARGT_BE: |
| 638 | if (argp[idx].type != ARGT_STR) |
| 639 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 640 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 641 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 642 | argp[idx].data.prx = proxy_be_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 643 | if (!argp[idx].data.prx) |
| 644 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 645 | argp[idx].type = ARGT_BE; |
| 646 | break; |
| 647 | |
| 648 | case ARGT_TAB: |
| 649 | if (argp[idx].type != ARGT_STR) |
| 650 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 651 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 652 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | e2dc1fa | 2015-05-26 12:08:07 +0200 | [diff] [blame] | 653 | argp[idx].data.prx = proxy_tbl_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 654 | if (!argp[idx].data.prx) |
| 655 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 656 | argp[idx].type = ARGT_TAB; |
| 657 | break; |
| 658 | |
| 659 | case ARGT_SRV: |
| 660 | if (argp[idx].type != ARGT_STR) |
| 661 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 662 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 663 | trash.str[argp[idx].data.str.len] = 0; |
| 664 | sname = strrchr(trash.str, '/'); |
| 665 | if (sname) { |
| 666 | *sname++ = '\0'; |
| 667 | pname = trash.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 668 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 669 | if (!px) |
| 670 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 671 | } |
| 672 | else { |
| 673 | sname = trash.str; |
| 674 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 675 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 676 | argp[idx].data.srv = findserver(px, sname); |
| 677 | if (!argp[idx].data.srv) |
| 678 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 679 | argp[idx].type = ARGT_SRV; |
| 680 | break; |
| 681 | |
| 682 | case ARGT_IPV4: |
| 683 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 684 | trash.str[argp[idx].data.str.len] = 0; |
| 685 | if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4)) |
| 686 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 687 | argp[idx].type = ARGT_IPV4; |
| 688 | break; |
| 689 | |
| 690 | case ARGT_MSK4: |
| 691 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 692 | trash.str[argp[idx].data.str.len] = 0; |
| 693 | if (!str2mask(trash.str, &argp[idx].data.ipv4)) |
| 694 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 695 | argp[idx].type = ARGT_MSK4; |
| 696 | break; |
| 697 | |
| 698 | case ARGT_IPV6: |
| 699 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 700 | trash.str[argp[idx].data.str.len] = 0; |
| 701 | if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6)) |
| 702 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 703 | argp[idx].type = ARGT_IPV6; |
| 704 | break; |
| 705 | |
| 706 | case ARGT_MSK6: |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 707 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 708 | trash.str[argp[idx].data.str.len] = 0; |
| 709 | if (!str2mask6(trash.str, &argp[idx].data.ipv6)) |
| 710 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 711 | argp[idx].type = ARGT_MSK6; |
| 712 | break; |
| 713 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 714 | case ARGT_MAP: |
| 715 | case ARGT_REG: |
| 716 | case ARGT_USR: |
| 717 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 718 | break; |
| 719 | } |
| 720 | |
| 721 | /* Check for type of argument. */ |
| 722 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 723 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 724 | arg_type_names[(mask & ARGT_MASK)], |
| 725 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 726 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 727 | } |
| 728 | |
| 729 | /* Next argument. */ |
| 730 | mask >>= ARGT_BITS; |
| 731 | idx++; |
| 732 | } |
| 733 | } |
| 734 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 735 | /* |
| 736 | * The following functions are used to make correspondance between the the |
| 737 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 738 | * |
| 739 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 740 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 741 | */ |
| 742 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 743 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 744 | struct hlua **hlua = lua_getextraspace(L); |
| 745 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 746 | } |
| 747 | static inline void hlua_sethlua(struct hlua *hlua) |
| 748 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 749 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 750 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 751 | } |
| 752 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 753 | /* This function is used to send logs. It try to send on screen (stderr) |
| 754 | * and on the default syslog server. |
| 755 | */ |
| 756 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 757 | { |
| 758 | struct tm tm; |
| 759 | char *p; |
| 760 | |
| 761 | /* Cleanup the log message. */ |
| 762 | p = trash.str; |
| 763 | for (; *msg != '\0'; msg++, p++) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 764 | if (p >= trash.str + trash.size - 1) { |
| 765 | /* Break the message if exceed the buffer size. */ |
| 766 | *(p-4) = ' '; |
| 767 | *(p-3) = '.'; |
| 768 | *(p-2) = '.'; |
| 769 | *(p-1) = '.'; |
| 770 | break; |
| 771 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 772 | if (isprint(*msg)) |
| 773 | *p = *msg; |
| 774 | else |
| 775 | *p = '.'; |
| 776 | } |
| 777 | *p = '\0'; |
| 778 | |
Thierry FOURNIER | 5554e29 | 2015-09-09 11:21:37 +0200 | [diff] [blame] | 779 | send_log(px, level, "%s\n", trash.str); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 780 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 781 | get_localtime(date.tv_sec, &tm); |
| 782 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 783 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 784 | (int)getpid(), trash.str); |
| 785 | fflush(stderr); |
| 786 | } |
| 787 | } |
| 788 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 789 | /* This function just ensure that the yield will be always |
| 790 | * returned with a timeout and permit to set some flags |
| 791 | */ |
| 792 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 793 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 794 | { |
| 795 | struct hlua *hlua = hlua_gethlua(L); |
| 796 | |
| 797 | /* Set the wake timeout. If timeout is required, we set |
| 798 | * the expiration time. |
| 799 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 800 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 801 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 802 | hlua->flags |= flags; |
| 803 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 804 | /* Process the yield. */ |
| 805 | WILL_LJMP(lua_yieldk(L, nresults, ctx, k)); |
| 806 | } |
| 807 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 808 | /* This function initialises the Lua environment stored in the stream. |
| 809 | * 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] | 810 | * 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] | 811 | * |
| 812 | * This function is particular. it initialises a new Lua thread. If the |
| 813 | * initialisation fails (example: out of memory error), the lua function |
| 814 | * throws an error (longjmp). |
| 815 | * |
| 816 | * This function manipulates two Lua stack: the main and the thread. Only |
| 817 | * the main stack can fail. The thread is not manipulated. This function |
| 818 | * MUST NOT manipulate the created thread stack state, because is not |
| 819 | * proctected agains error throwed by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 820 | */ |
| 821 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 822 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 823 | if (!SET_SAFE_LJMP(gL.T)) { |
| 824 | lua->Tref = LUA_REFNIL; |
| 825 | return 0; |
| 826 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 827 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 828 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 829 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 830 | lua->T = lua_newthread(gL.T); |
| 831 | if (!lua->T) { |
| 832 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | 0a97620 | 2017-07-12 11:18:00 +0200 | [diff] [blame] | 833 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 834 | return 0; |
| 835 | } |
| 836 | hlua_sethlua(lua); |
| 837 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 838 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 839 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 840 | return 1; |
| 841 | } |
| 842 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 843 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 844 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 845 | * is not freed. |
| 846 | */ |
| 847 | void hlua_ctx_destroy(struct hlua *lua) |
| 848 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 849 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 850 | return; |
| 851 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 852 | if (!lua->T) |
| 853 | goto end; |
| 854 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 855 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 856 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 857 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 858 | if (!SET_SAFE_LJMP(lua->T)) |
| 859 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 860 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 861 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 862 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 863 | if (!SET_SAFE_LJMP(gL.T)) |
| 864 | return; |
| 865 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 866 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 867 | /* Forces a garbage collecting process. If the Lua program is finished |
| 868 | * without error, we run the GC on the thread pointer. Its freed all |
| 869 | * the unused memory. |
| 870 | * If the thread is finnish with an error or is currently yielded, |
| 871 | * it seems that the GC applied on the thread doesn't clean anything, |
| 872 | * so e run the GC on the main thread. |
| 873 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 874 | * the garbage collection. |
| 875 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 876 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 877 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 878 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 879 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 880 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 881 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 882 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 883 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 884 | |
| 885 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 886 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | /* This function is used to restore the Lua context when a coroutine |
| 890 | * fails. This function copy the common memory between old coroutine |
| 891 | * and the new coroutine. The old coroutine is destroyed, and its |
| 892 | * replaced by the new coroutine. |
| 893 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 894 | * as string error message and it is copied in the new stack. |
| 895 | */ |
| 896 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 897 | { |
| 898 | lua_State *T; |
| 899 | int new_ref; |
| 900 | |
| 901 | /* Renew the main LUA stack doesn't have sense. */ |
| 902 | if (lua == &gL) |
| 903 | return 0; |
| 904 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 905 | /* New Lua coroutine. */ |
| 906 | T = lua_newthread(gL.T); |
| 907 | if (!T) |
| 908 | return 0; |
| 909 | |
| 910 | /* Copy last error message. */ |
| 911 | if (keep_msg) |
| 912 | lua_xmove(lua->T, T, 1); |
| 913 | |
| 914 | /* Copy data between the coroutines. */ |
| 915 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 916 | lua_xmove(lua->T, T, 1); |
| 917 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 918 | |
| 919 | /* Destroy old data. */ |
| 920 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 921 | |
| 922 | /* The thread is garbage collected by Lua. */ |
| 923 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 924 | |
| 925 | /* Fill the struct with the new coroutine values. */ |
| 926 | lua->Mref = new_ref; |
| 927 | lua->T = T; |
| 928 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 929 | |
| 930 | /* Set context. */ |
| 931 | hlua_sethlua(lua); |
| 932 | |
| 933 | return 1; |
| 934 | } |
| 935 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 936 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 937 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 938 | struct hlua *hlua = hlua_gethlua(L); |
| 939 | |
| 940 | /* Lua cannot yield when its returning from a function, |
| 941 | * so, we can fix the interrupt hook to 1 instruction, |
| 942 | * expecting that the function is finnished. |
| 943 | */ |
| 944 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 945 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 946 | return; |
| 947 | } |
| 948 | |
| 949 | /* restore the interrupt condition. */ |
| 950 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 951 | |
| 952 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 953 | * If the state is not yieldable, trying yield causes an error. |
| 954 | */ |
| 955 | if (lua_isyieldable(L)) |
| 956 | WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 957 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 958 | /* If we cannot yield, update the clock and check the timeout. */ |
| 959 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 960 | hlua->run_time += now_ms - hlua->start_time; |
| 961 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 962 | lua_pushfstring(L, "execution timeout"); |
| 963 | WILL_LJMP(lua_error(L)); |
| 964 | } |
| 965 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 966 | /* Update the start time. */ |
| 967 | hlua->start_time = now_ms; |
| 968 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 969 | /* Try to interrupt the process at the end of the current |
| 970 | * unyieldable function. |
| 971 | */ |
| 972 | 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] | 973 | } |
| 974 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 975 | /* This function start or resumes the Lua stack execution. If the flag |
| 976 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 977 | * The function return an error. |
| 978 | * |
| 979 | * The function can returns 4 values: |
| 980 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 981 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 982 | * task wakeup. |
| 983 | * - HLUA_E_ERRMSG : An error has occured, an error message is set in |
| 984 | * the top of the stack. |
| 985 | * - HLUA_E_ERR : An error has occured without error message. |
| 986 | * |
| 987 | * If an error occured, the stack is renewed and it is ready to run new |
| 988 | * LUA code. |
| 989 | */ |
| 990 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 991 | { |
| 992 | int ret; |
| 993 | const char *msg; |
| 994 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 995 | /* Initialise run time counter. */ |
| 996 | if (!HLUA_IS_RUNNING(lua)) |
| 997 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 998 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 999 | /* Lock the whole Lua execution. This lock must be before the |
| 1000 | * label "resume_execution". |
| 1001 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1002 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1003 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1004 | resume_execution: |
| 1005 | |
| 1006 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1007 | * instructions. it is used for preventing infinite loops. |
| 1008 | */ |
| 1009 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1010 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1011 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1012 | HLUA_SET_RUN(lua); |
| 1013 | HLUA_CLR_CTRLYIELD(lua); |
| 1014 | HLUA_CLR_WAKERESWR(lua); |
| 1015 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1016 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1017 | /* Update the start time. */ |
| 1018 | lua->start_time = now_ms; |
| 1019 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1020 | /* Call the function. */ |
| 1021 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1022 | switch (ret) { |
| 1023 | |
| 1024 | case LUA_OK: |
| 1025 | ret = HLUA_E_OK; |
| 1026 | break; |
| 1027 | |
| 1028 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1029 | /* Check if the execution timeout is expired. It it is the case, we |
| 1030 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1031 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1032 | tv_update_date(0, 1); |
| 1033 | lua->run_time += now_ms - lua->start_time; |
| 1034 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1035 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1036 | if (!lua_checkstack(lua->T, 1)) { |
| 1037 | ret = HLUA_E_ERR; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1038 | break; |
| 1039 | } |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1040 | lua_pushfstring(lua->T, "execution timeout"); |
| 1041 | ret = HLUA_E_ERRMSG; |
| 1042 | break; |
| 1043 | } |
| 1044 | /* Process the forced yield. if the general yield is not allowed or |
| 1045 | * if no task were associated this the current Lua execution |
| 1046 | * coroutine, we resume the execution. Else we want to return in the |
| 1047 | * scheduler and we want to be waked up again, to continue the |
| 1048 | * current Lua execution. So we schedule our own task. |
| 1049 | */ |
| 1050 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1051 | if (!yield_allowed || !lua->task) |
| 1052 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1053 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1054 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1055 | if (!yield_allowed) { |
| 1056 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1057 | if (!lua_checkstack(lua->T, 1)) { |
| 1058 | ret = HLUA_E_ERR; |
| 1059 | break; |
| 1060 | } |
| 1061 | lua_pushfstring(lua->T, "yield not allowed"); |
| 1062 | ret = HLUA_E_ERRMSG; |
| 1063 | break; |
| 1064 | } |
| 1065 | ret = HLUA_E_AGAIN; |
| 1066 | break; |
| 1067 | |
| 1068 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1069 | |
| 1070 | /* Special exit case. The traditionnal exit is returned as an error |
| 1071 | * because the errors ares the only one mean to return immediately |
| 1072 | * from and lua execution. |
| 1073 | */ |
| 1074 | if (lua->flags & HLUA_EXIT) { |
| 1075 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1076 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1077 | break; |
| 1078 | } |
| 1079 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1080 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1081 | if (!lua_checkstack(lua->T, 1)) { |
| 1082 | ret = HLUA_E_ERR; |
| 1083 | break; |
| 1084 | } |
| 1085 | msg = lua_tostring(lua->T, -1); |
| 1086 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1087 | lua_pop(lua->T, 1); |
| 1088 | if (msg) |
| 1089 | lua_pushfstring(lua->T, "runtime error: %s", msg); |
| 1090 | else |
| 1091 | lua_pushfstring(lua->T, "unknown runtime error"); |
| 1092 | ret = HLUA_E_ERRMSG; |
| 1093 | break; |
| 1094 | |
| 1095 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1096 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1097 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1098 | if (!lua_checkstack(lua->T, 1)) { |
| 1099 | ret = HLUA_E_ERR; |
| 1100 | break; |
| 1101 | } |
| 1102 | lua_pushfstring(lua->T, "out of memory error"); |
| 1103 | ret = HLUA_E_ERRMSG; |
| 1104 | break; |
| 1105 | |
| 1106 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1107 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1108 | if (!lua_checkstack(lua->T, 1)) { |
| 1109 | ret = HLUA_E_ERR; |
| 1110 | break; |
| 1111 | } |
| 1112 | msg = lua_tostring(lua->T, -1); |
| 1113 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1114 | lua_pop(lua->T, 1); |
| 1115 | if (msg) |
| 1116 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1117 | else |
| 1118 | lua_pushfstring(lua->T, "message handler error"); |
| 1119 | ret = HLUA_E_ERRMSG; |
| 1120 | break; |
| 1121 | |
| 1122 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1123 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1124 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1125 | if (!lua_checkstack(lua->T, 1)) { |
| 1126 | ret = HLUA_E_ERR; |
| 1127 | break; |
| 1128 | } |
| 1129 | lua_pushfstring(lua->T, "unknonwn error"); |
| 1130 | ret = HLUA_E_ERRMSG; |
| 1131 | break; |
| 1132 | } |
| 1133 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1134 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1135 | if (lua->flags & HLUA_MUST_GC && |
| 1136 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1137 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1138 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1139 | switch (ret) { |
| 1140 | case HLUA_E_AGAIN: |
| 1141 | break; |
| 1142 | |
| 1143 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1144 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1145 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1146 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1147 | break; |
| 1148 | |
| 1149 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1150 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1151 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1152 | hlua_ctx_renew(lua, 0); |
| 1153 | break; |
| 1154 | |
| 1155 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1156 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1157 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1158 | break; |
| 1159 | } |
| 1160 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1161 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1162 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1163 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1164 | return ret; |
| 1165 | } |
| 1166 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1167 | /* This function exit the current code. */ |
| 1168 | __LJMP static int hlua_done(lua_State *L) |
| 1169 | { |
| 1170 | struct hlua *hlua = hlua_gethlua(L); |
| 1171 | |
| 1172 | hlua->flags |= HLUA_EXIT; |
| 1173 | WILL_LJMP(lua_error(L)); |
| 1174 | |
| 1175 | return 0; |
| 1176 | } |
| 1177 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1178 | /* This function is an LUA binding. It provides a function |
| 1179 | * for deleting ACL from a referenced ACL file. |
| 1180 | */ |
| 1181 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1182 | { |
| 1183 | const char *name; |
| 1184 | const char *key; |
| 1185 | struct pat_ref *ref; |
| 1186 | |
| 1187 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1188 | |
| 1189 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1190 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1191 | |
| 1192 | ref = pat_ref_lookup(name); |
| 1193 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1194 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1195 | |
| 1196 | pat_ref_delete(ref, key); |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
| 1200 | /* This function is an LUA binding. It provides a function |
| 1201 | * for deleting map entry from a referenced map file. |
| 1202 | */ |
| 1203 | static int hlua_del_map(lua_State *L) |
| 1204 | { |
| 1205 | const char *name; |
| 1206 | const char *key; |
| 1207 | struct pat_ref *ref; |
| 1208 | |
| 1209 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1210 | |
| 1211 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1212 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1213 | |
| 1214 | ref = pat_ref_lookup(name); |
| 1215 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1216 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1217 | |
| 1218 | pat_ref_delete(ref, key); |
| 1219 | return 0; |
| 1220 | } |
| 1221 | |
| 1222 | /* This function is an LUA binding. It provides a function |
| 1223 | * for adding ACL pattern from a referenced ACL file. |
| 1224 | */ |
| 1225 | static int hlua_add_acl(lua_State *L) |
| 1226 | { |
| 1227 | const char *name; |
| 1228 | const char *key; |
| 1229 | struct pat_ref *ref; |
| 1230 | |
| 1231 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1232 | |
| 1233 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1234 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1235 | |
| 1236 | ref = pat_ref_lookup(name); |
| 1237 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1238 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1239 | |
| 1240 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1241 | pat_ref_add(ref, key, NULL, NULL); |
| 1242 | return 0; |
| 1243 | } |
| 1244 | |
| 1245 | /* This function is an LUA binding. It provides a function |
| 1246 | * for setting map pattern and sample from a referenced map |
| 1247 | * file. |
| 1248 | */ |
| 1249 | static int hlua_set_map(lua_State *L) |
| 1250 | { |
| 1251 | const char *name; |
| 1252 | const char *key; |
| 1253 | const char *value; |
| 1254 | struct pat_ref *ref; |
| 1255 | |
| 1256 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1257 | |
| 1258 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1259 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1260 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1261 | |
| 1262 | ref = pat_ref_lookup(name); |
| 1263 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1264 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1265 | |
| 1266 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1267 | pat_ref_set(ref, key, value, NULL); |
| 1268 | else |
| 1269 | pat_ref_add(ref, key, value, NULL); |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1273 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1274 | * an integer or user data. This data is associated with a metatable. This |
| 1275 | * metatable have an original version registred in the global context with |
| 1276 | * the name of the object (_G[<name>] = <metable> ). |
| 1277 | * |
| 1278 | * A metable is a table that modify the standard behavior of a standard |
| 1279 | * access to the associated data. The entries of this new metatable are |
| 1280 | * defined as is: |
| 1281 | * |
| 1282 | * http://lua-users.org/wiki/MetatableEvents |
| 1283 | * |
| 1284 | * __index |
| 1285 | * |
| 1286 | * we access an absent field in a table, the result is nil. This is |
| 1287 | * true, but it is not the whole truth. Actually, such access triggers |
| 1288 | * the interpreter to look for an __index metamethod: If there is no |
| 1289 | * such method, as usually happens, then the access results in nil; |
| 1290 | * otherwise, the metamethod will provide the result. |
| 1291 | * |
| 1292 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1293 | * the key does not appear in the table, but the metatable has an __index |
| 1294 | * property: |
| 1295 | * |
| 1296 | * - if the value is a function, the function is called, passing in the |
| 1297 | * table and the key; the return value of that function is returned as |
| 1298 | * the result. |
| 1299 | * |
| 1300 | * - if the value is another table, the value of the key in that table is |
| 1301 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1302 | * table's metatable has an __index property, then it continues on up) |
| 1303 | * |
| 1304 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1305 | * |
| 1306 | * http://www.lua.org/pil/13.4.1.html |
| 1307 | * |
| 1308 | * __newindex |
| 1309 | * |
| 1310 | * Like __index, but control property assignment. |
| 1311 | * |
| 1312 | * __mode - Control weak references. A string value with one or both |
| 1313 | * of the characters 'k' and 'v' which specifies that the the |
| 1314 | * keys and/or values in the table are weak references. |
| 1315 | * |
| 1316 | * __call - Treat a table like a function. When a table is followed by |
| 1317 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1318 | * a __call key pointing to a function, that function is invoked |
| 1319 | * (passing any specified arguments) and the return value is |
| 1320 | * returned. |
| 1321 | * |
| 1322 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1323 | * called, if the metatable for myTable has a __metatable |
| 1324 | * key, the value of that key is returned instead of the |
| 1325 | * actual metatable. |
| 1326 | * |
| 1327 | * __tostring - Control string representation. When the builtin |
| 1328 | * "tostring( myTable )" function is called, if the metatable |
| 1329 | * for myTable has a __tostring property set to a function, |
| 1330 | * that function is invoked (passing myTable to it) and the |
| 1331 | * return value is used as the string representation. |
| 1332 | * |
| 1333 | * __len - Control table length. When the table length is requested using |
| 1334 | * the length operator ( '#' ), if the metatable for myTable has |
| 1335 | * a __len key pointing to a function, that function is invoked |
| 1336 | * (passing myTable to it) and the return value used as the value |
| 1337 | * of "#myTable". |
| 1338 | * |
| 1339 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1340 | * collected, if the metatable has a __gc field pointing to a |
| 1341 | * function, that function is first invoked, passing the userdata |
| 1342 | * to it. The __gc metamethod is not called for tables. |
| 1343 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1344 | * |
| 1345 | * Special metamethods for redefining standard operators: |
| 1346 | * http://www.lua.org/pil/13.1.html |
| 1347 | * |
| 1348 | * __add "+" |
| 1349 | * __sub "-" |
| 1350 | * __mul "*" |
| 1351 | * __div "/" |
| 1352 | * __unm "!" |
| 1353 | * __pow "^" |
| 1354 | * __concat ".." |
| 1355 | * |
| 1356 | * Special methods for redfining standar relations |
| 1357 | * http://www.lua.org/pil/13.2.html |
| 1358 | * |
| 1359 | * __eq "==" |
| 1360 | * __lt "<" |
| 1361 | * __le "<=" |
| 1362 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1363 | |
| 1364 | /* |
| 1365 | * |
| 1366 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1367 | * Class Map |
| 1368 | * |
| 1369 | * |
| 1370 | */ |
| 1371 | |
| 1372 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1373 | * a class session, otherwise it throws an error. |
| 1374 | */ |
| 1375 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1376 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1377 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | /* This function is the map constructor. It don't need |
| 1381 | * the class Map object. It creates and return a new Map |
| 1382 | * object. It must be called only during "body" or "init" |
| 1383 | * context because it process some filesystem accesses. |
| 1384 | */ |
| 1385 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1386 | { |
| 1387 | const char *fn; |
| 1388 | int match = PAT_MATCH_STR; |
| 1389 | struct sample_conv conv; |
| 1390 | const char *file = ""; |
| 1391 | int line = 0; |
| 1392 | lua_Debug ar; |
| 1393 | char *err = NULL; |
| 1394 | struct arg args[2]; |
| 1395 | |
| 1396 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1397 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1398 | |
| 1399 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1400 | |
| 1401 | if (lua_gettop(L) >= 2) { |
| 1402 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1403 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1404 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1405 | } |
| 1406 | |
| 1407 | /* Get Lua filename and line number. */ |
| 1408 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1409 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1410 | if (ar.currentline > 0) { /* is there info? */ |
| 1411 | file = ar.short_src; |
| 1412 | line = ar.currentline; |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | /* fill fake sample_conv struct. */ |
| 1417 | conv.kw = ""; /* unused. */ |
| 1418 | conv.process = NULL; /* unused. */ |
| 1419 | conv.arg_mask = 0; /* unused. */ |
| 1420 | conv.val_args = NULL; /* unused. */ |
| 1421 | conv.out_type = SMP_T_STR; |
| 1422 | conv.private = (void *)(long)match; |
| 1423 | switch (match) { |
| 1424 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1425 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1426 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1427 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1428 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1429 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1430 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1431 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1432 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1433 | default: |
| 1434 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1435 | } |
| 1436 | |
| 1437 | /* fill fake args. */ |
| 1438 | args[0].type = ARGT_STR; |
| 1439 | args[0].data.str.str = (char *)fn; |
| 1440 | args[1].type = ARGT_STOP; |
| 1441 | |
| 1442 | /* load the map. */ |
| 1443 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1444 | /* error case: we cant use luaL_error because we must |
| 1445 | * free the err variable. |
| 1446 | */ |
| 1447 | luaL_where(L, 1); |
| 1448 | lua_pushfstring(L, "'new': %s.", err); |
| 1449 | lua_concat(L, 2); |
| 1450 | free(err); |
| 1451 | WILL_LJMP(lua_error(L)); |
| 1452 | } |
| 1453 | |
| 1454 | /* create the lua object. */ |
| 1455 | lua_newtable(L); |
| 1456 | lua_pushlightuserdata(L, args[0].data.map); |
| 1457 | lua_rawseti(L, -2, 0); |
| 1458 | |
| 1459 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1460 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1461 | lua_setmetatable(L, -2); |
| 1462 | |
| 1463 | |
| 1464 | return 1; |
| 1465 | } |
| 1466 | |
| 1467 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1468 | { |
| 1469 | struct map_descriptor *desc; |
| 1470 | struct pattern *pat; |
| 1471 | struct sample smp; |
| 1472 | |
| 1473 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1474 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1475 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1476 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1477 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1478 | } |
| 1479 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1480 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1481 | smp.flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1482 | 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] | 1483 | } |
| 1484 | |
| 1485 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1486 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1487 | if (str) |
| 1488 | lua_pushstring(L, ""); |
| 1489 | else |
| 1490 | lua_pushnil(L); |
| 1491 | return 1; |
| 1492 | } |
| 1493 | |
| 1494 | /* 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] | 1495 | 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] | 1496 | return 1; |
| 1497 | } |
| 1498 | |
| 1499 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1500 | { |
| 1501 | return _hlua_map_lookup(L, 0); |
| 1502 | } |
| 1503 | |
| 1504 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1505 | { |
| 1506 | return _hlua_map_lookup(L, 1); |
| 1507 | } |
| 1508 | |
| 1509 | /* |
| 1510 | * |
| 1511 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1512 | * Class Socket |
| 1513 | * |
| 1514 | * |
| 1515 | */ |
| 1516 | |
| 1517 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1518 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1519 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | /* This function is the handler called for each I/O on the established |
| 1523 | * connection. It is used for notify space avalaible to send or data |
| 1524 | * received. |
| 1525 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1526 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1527 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1528 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1529 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1530 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1531 | if (appctx->ctx.hlua_cosocket.die) { |
| 1532 | si_shutw(si); |
| 1533 | si_shutr(si); |
| 1534 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1535 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1536 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1537 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1538 | } |
| 1539 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1540 | /* If the connection object is not avalaible, close all the |
| 1541 | * streams and wakeup everithing waiting for. |
| 1542 | */ |
| 1543 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1544 | si_shutw(si); |
| 1545 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1546 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1547 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1548 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1549 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1550 | } |
| 1551 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1552 | /* If we cant write, wakeup the pending write signals. */ |
| 1553 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1554 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1555 | |
| 1556 | /* If we cant read, wakeup the pending read signals. */ |
| 1557 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1558 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1559 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1560 | /* if the connection is not estabkished, inform the stream that we want |
| 1561 | * to be notified whenever the connection completes. |
| 1562 | */ |
| 1563 | if (!(c->flags & CO_FL_CONNECTED)) { |
| 1564 | si_applet_cant_get(si); |
| 1565 | si_applet_cant_put(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1566 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1567 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1568 | |
| 1569 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1570 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1571 | |
| 1572 | /* 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] | 1573 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1574 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1575 | |
| 1576 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1577 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1578 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1579 | } |
| 1580 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1581 | /* This function is called when the "struct stream" is destroyed. |
| 1582 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1583 | * Wake all the pending signals. |
| 1584 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1585 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1586 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1587 | struct xref *peer; |
| 1588 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1589 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1590 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1591 | if (peer) |
| 1592 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1593 | |
| 1594 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1595 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1596 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1597 | } |
| 1598 | |
| 1599 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1600 | * uses this object. If the stream does not exists, just quit. |
| 1601 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1602 | * pending signal can rest in the read and write lists. destroy |
| 1603 | * it. |
| 1604 | */ |
| 1605 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1606 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1607 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1608 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1609 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1610 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1611 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1612 | |
| 1613 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1614 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1615 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1616 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1617 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1618 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1619 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1620 | appctx->ctx.hlua_cosocket.die = 1; |
| 1621 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1622 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1623 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1624 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1625 | return 0; |
| 1626 | } |
| 1627 | |
| 1628 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1629 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1630 | */ |
| 1631 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1632 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1633 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1634 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1635 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1636 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1637 | MAY_LJMP(check_args(L, 1, "close")); |
| 1638 | |
| 1639 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1640 | |
| 1641 | /* Check if we run on the same thread than the xreator thread. |
| 1642 | * We cannot access to the socket if the thread is different. |
| 1643 | */ |
| 1644 | if (socket->tid != tid) |
| 1645 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1646 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1647 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1648 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1649 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1650 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1651 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1652 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1653 | appctx->ctx.hlua_cosocket.die = 1; |
| 1654 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1655 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1656 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1657 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1658 | return 0; |
| 1659 | } |
| 1660 | |
| 1661 | /* This Lua function assumes that the stack contain three parameters. |
| 1662 | * 1 - USERDATA containing a struct socket |
| 1663 | * 2 - INTEGER with values of the macro defined below |
| 1664 | * If the integer is -1, we must read at most one line. |
| 1665 | * If the integer is -2, we ust read all the data until the |
| 1666 | * end of the stream. |
| 1667 | * If the integer is positive value, we must read a number of |
| 1668 | * bytes corresponding to this value. |
| 1669 | */ |
| 1670 | #define HLSR_READ_LINE (-1) |
| 1671 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1672 | __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] | 1673 | { |
| 1674 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1675 | int wanted = lua_tointeger(L, 2); |
| 1676 | struct hlua *hlua = hlua_gethlua(L); |
| 1677 | struct appctx *appctx; |
| 1678 | int len; |
| 1679 | int nblk; |
| 1680 | char *blk1; |
| 1681 | int len1; |
| 1682 | char *blk2; |
| 1683 | int len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1684 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1685 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1686 | struct stream_interface *si; |
| 1687 | struct stream *s; |
| 1688 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1689 | |
| 1690 | /* Check if this lua stack is schedulable. */ |
| 1691 | if (!hlua || !hlua->task) |
| 1692 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1693 | "'frontend', 'backend' or 'task'")); |
| 1694 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1695 | /* Check if we run on the same thread than the xreator thread. |
| 1696 | * We cannot access to the socket if the thread is different. |
| 1697 | */ |
| 1698 | if (socket->tid != tid) |
| 1699 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1700 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1701 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1702 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1703 | if (!peer) |
| 1704 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1705 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1706 | si = appctx->owner; |
| 1707 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1708 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1709 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1710 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1711 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1712 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1713 | if (nblk < 0) /* Connection close. */ |
| 1714 | goto connection_closed; |
| 1715 | if (nblk == 0) /* No data avalaible. */ |
| 1716 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1717 | |
| 1718 | /* remove final \r\n. */ |
| 1719 | if (nblk == 1) { |
| 1720 | if (blk1[len1-1] == '\n') { |
| 1721 | len1--; |
| 1722 | skip_at_end++; |
| 1723 | if (blk1[len1-1] == '\r') { |
| 1724 | len1--; |
| 1725 | skip_at_end++; |
| 1726 | } |
| 1727 | } |
| 1728 | } |
| 1729 | else { |
| 1730 | if (blk2[len2-1] == '\n') { |
| 1731 | len2--; |
| 1732 | skip_at_end++; |
| 1733 | if (blk2[len2-1] == '\r') { |
| 1734 | len2--; |
| 1735 | skip_at_end++; |
| 1736 | } |
| 1737 | } |
| 1738 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1742 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1743 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1744 | if (nblk < 0) /* Connection close. */ |
| 1745 | goto connection_closed; |
| 1746 | if (nblk == 0) /* No data avalaible. */ |
| 1747 | goto connection_empty; |
| 1748 | } |
| 1749 | |
| 1750 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1751 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1752 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1753 | if (nblk < 0) /* Connection close. */ |
| 1754 | goto connection_closed; |
| 1755 | if (nblk == 0) /* No data avalaible. */ |
| 1756 | goto connection_empty; |
| 1757 | |
| 1758 | if (len1 > wanted) { |
| 1759 | nblk = 1; |
| 1760 | len1 = wanted; |
| 1761 | } if (nblk == 2 && len1 + len2 > wanted) |
| 1762 | len2 = wanted - len1; |
| 1763 | } |
| 1764 | |
| 1765 | len = len1; |
| 1766 | |
| 1767 | luaL_addlstring(&socket->b, blk1, len1); |
| 1768 | if (nblk == 2) { |
| 1769 | len += len2; |
| 1770 | luaL_addlstring(&socket->b, blk2, len2); |
| 1771 | } |
| 1772 | |
| 1773 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1774 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1775 | |
| 1776 | /* Don't wait anything. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1777 | stream_int_notify(&s->si[0]); |
| 1778 | stream_int_update_applet(&s->si[0]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1779 | |
| 1780 | /* If the pattern reclaim to read all the data |
| 1781 | * in the connection, got out. |
| 1782 | */ |
| 1783 | if (wanted == HLSR_READ_ALL) |
| 1784 | goto connection_empty; |
| 1785 | else if (wanted >= 0 && len < wanted) |
| 1786 | goto connection_empty; |
| 1787 | |
| 1788 | /* Return result. */ |
| 1789 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1790 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1791 | return 1; |
| 1792 | |
| 1793 | connection_closed: |
| 1794 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1795 | xref_unlock(&socket->xref, peer); |
| 1796 | |
| 1797 | no_peer: |
| 1798 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1799 | /* If the buffer containds data. */ |
| 1800 | if (socket->b.n > 0) { |
| 1801 | luaL_pushresult(&socket->b); |
| 1802 | return 1; |
| 1803 | } |
| 1804 | lua_pushnil(L); |
| 1805 | lua_pushstring(L, "connection closed."); |
| 1806 | return 2; |
| 1807 | |
| 1808 | connection_empty: |
| 1809 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1810 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1811 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1812 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1813 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1814 | } |
| 1815 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1816 | 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] | 1817 | return 0; |
| 1818 | } |
| 1819 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1820 | /* This Lua function gets two parameters. The first one can be string |
| 1821 | * or a number. If the string is "*l", the user requires one line. If |
| 1822 | * the string is "*a", the user requires all the contents of the stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1823 | * If the value is a number, the user require a number of bytes equal |
| 1824 | * to the value. The default value is "*l" (a line). |
| 1825 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1826 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1827 | * integer takes this values: |
| 1828 | * -1 : read a line |
| 1829 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1830 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1831 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1832 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1833 | * concatenated with the read data. |
| 1834 | */ |
| 1835 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1836 | { |
| 1837 | int wanted = HLSR_READ_LINE; |
| 1838 | const char *pattern; |
| 1839 | int type; |
| 1840 | char *error; |
| 1841 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1842 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1843 | |
| 1844 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1845 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1846 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1847 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1848 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1849 | /* Check if we run on the same thread than the xreator thread. |
| 1850 | * We cannot access to the socket if the thread is different. |
| 1851 | */ |
| 1852 | if (socket->tid != tid) |
| 1853 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1854 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1855 | /* check for pattern. */ |
| 1856 | if (lua_gettop(L) >= 2) { |
| 1857 | type = lua_type(L, 2); |
| 1858 | if (type == LUA_TSTRING) { |
| 1859 | pattern = lua_tostring(L, 2); |
| 1860 | if (strcmp(pattern, "*a") == 0) |
| 1861 | wanted = HLSR_READ_ALL; |
| 1862 | else if (strcmp(pattern, "*l") == 0) |
| 1863 | wanted = HLSR_READ_LINE; |
| 1864 | else { |
| 1865 | wanted = strtoll(pattern, &error, 10); |
| 1866 | if (*error != '\0') |
| 1867 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1868 | } |
| 1869 | } |
| 1870 | else if (type == LUA_TNUMBER) { |
| 1871 | wanted = lua_tointeger(L, 2); |
| 1872 | if (wanted < 0) |
| 1873 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | /* Set pattern. */ |
| 1878 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1879 | |
| 1880 | /* Check if we would replace the top by itself. */ |
| 1881 | if (lua_gettop(L) != 2) |
| 1882 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1883 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1884 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1885 | luaL_buffinit(L, &socket->b); |
| 1886 | |
| 1887 | /* Check prefix. */ |
| 1888 | if (lua_gettop(L) >= 3) { |
| 1889 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1890 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1891 | pattern = lua_tolstring(L, 3, &len); |
| 1892 | luaL_addlstring(&socket->b, pattern, len); |
| 1893 | } |
| 1894 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1895 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | /* Write the Lua input string in the output buffer. |
| 1899 | * This fucntion returns a yield if no space are available. |
| 1900 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1901 | 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] | 1902 | { |
| 1903 | struct hlua_socket *socket; |
| 1904 | struct hlua *hlua = hlua_gethlua(L); |
| 1905 | struct appctx *appctx; |
| 1906 | size_t buf_len; |
| 1907 | const char *buf; |
| 1908 | int len; |
| 1909 | int send_len; |
| 1910 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1911 | struct xref *peer; |
| 1912 | struct stream_interface *si; |
| 1913 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1914 | |
| 1915 | /* Check if this lua stack is schedulable. */ |
| 1916 | if (!hlua || !hlua->task) |
| 1917 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1918 | "'frontend', 'backend' or 'task'")); |
| 1919 | |
| 1920 | /* Get object */ |
| 1921 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1922 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1923 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1924 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1925 | /* Check if we run on the same thread than the xreator thread. |
| 1926 | * We cannot access to the socket if the thread is different. |
| 1927 | */ |
| 1928 | if (socket->tid != tid) |
| 1929 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1930 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1931 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1932 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1933 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1934 | lua_pushinteger(L, -1); |
| 1935 | return 1; |
| 1936 | } |
| 1937 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1938 | si = appctx->owner; |
| 1939 | s = si_strm(si); |
| 1940 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1941 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1942 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1943 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1944 | lua_pushinteger(L, -1); |
| 1945 | return 1; |
| 1946 | } |
| 1947 | |
| 1948 | /* Update the input buffer data. */ |
| 1949 | buf += sent; |
| 1950 | send_len = buf_len - sent; |
| 1951 | |
| 1952 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1953 | if (sent >= buf_len) { |
| 1954 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1955 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1956 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1957 | |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1958 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 1959 | * the request buffer if its not required. |
| 1960 | */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1961 | if (s->req.buf->size == 0) { |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1962 | appctx = hlua->task->context; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1963 | if (!channel_alloc_buffer(&s->req, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1964 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1965 | } |
| 1966 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1967 | /* Check for avalaible space. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1968 | len = buffer_total_space(s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1969 | if (len <= 0) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1970 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1971 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 1972 | xref_unlock(&socket->xref, peer); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1973 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1974 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1975 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1976 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1977 | |
| 1978 | /* send data */ |
| 1979 | if (len < send_len) |
| 1980 | send_len = len; |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1981 | len = ci_putblk(&s->req, buf+sent, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1982 | |
| 1983 | /* "Not enough space" (-1), "Buffer too little to contain |
| 1984 | * the data" (-2) are not expected because the available length |
| 1985 | * is tested. |
| 1986 | * Other unknown error are also not expected. |
| 1987 | */ |
| 1988 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1989 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1990 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1991 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1992 | MAY_LJMP(hlua_socket_close(L)); |
| 1993 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1994 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1995 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1996 | return 1; |
| 1997 | } |
| 1998 | |
| 1999 | /* update buffers. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2000 | stream_int_notify(&s->si[0]); |
| 2001 | stream_int_update_applet(&s->si[0]); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2002 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2003 | s->req.rex = TICK_ETERNITY; |
| 2004 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2005 | |
| 2006 | /* Update length sent. */ |
| 2007 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2008 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2009 | |
| 2010 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2011 | if (sent + len >= buf_len) { |
| 2012 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2013 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2014 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | |
| 2016 | hlua_socket_write_yield_return: |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2017 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2018 | 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] | 2019 | return 0; |
| 2020 | } |
| 2021 | |
| 2022 | /* This function initiate the send of data. It just check the input |
| 2023 | * parameters and push an integer in the Lua stack that contain the |
| 2024 | * amount of data writed in the buffer. This is used by the function |
| 2025 | * "hlua_socket_write_yield" that can yield. |
| 2026 | * |
| 2027 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2028 | * the associated object. The second is a string buffer. The third is |
| 2029 | * a facultative integer that represents where is the buffer position |
| 2030 | * of the start of the data that can send. The first byte is the |
| 2031 | * position "1". The default value is "1". The fourth argument is a |
| 2032 | * facultative integer that represents where is the buffer position |
| 2033 | * of the end of the data that can send. The default is the last byte. |
| 2034 | */ |
| 2035 | static int hlua_socket_send(struct lua_State *L) |
| 2036 | { |
| 2037 | int i; |
| 2038 | int j; |
| 2039 | const char *buf; |
| 2040 | size_t buf_len; |
| 2041 | |
| 2042 | /* Check number of arguments. */ |
| 2043 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2044 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2045 | |
| 2046 | /* Get the string. */ |
| 2047 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2048 | |
| 2049 | /* Get and check j. */ |
| 2050 | if (lua_gettop(L) == 4) { |
| 2051 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2052 | if (j < 0) |
| 2053 | j = buf_len + j + 1; |
| 2054 | if (j > buf_len) |
| 2055 | j = buf_len + 1; |
| 2056 | lua_pop(L, 1); |
| 2057 | } |
| 2058 | else |
| 2059 | j = buf_len; |
| 2060 | |
| 2061 | /* Get and check i. */ |
| 2062 | if (lua_gettop(L) == 3) { |
| 2063 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2064 | if (i < 0) |
| 2065 | i = buf_len + i + 1; |
| 2066 | if (i > buf_len) |
| 2067 | i = buf_len + 1; |
| 2068 | lua_pop(L, 1); |
| 2069 | } else |
| 2070 | i = 1; |
| 2071 | |
| 2072 | /* Check bth i and j. */ |
| 2073 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2074 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2075 | return 1; |
| 2076 | } |
| 2077 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2078 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2079 | return 1; |
| 2080 | } |
| 2081 | if (i == 0) |
| 2082 | i = 1; |
| 2083 | if (j == 0) |
| 2084 | j = 1; |
| 2085 | |
| 2086 | /* Pop the string. */ |
| 2087 | lua_pop(L, 1); |
| 2088 | |
| 2089 | /* Update the buffer length. */ |
| 2090 | buf += i - 1; |
| 2091 | buf_len = j - i + 1; |
| 2092 | lua_pushlstring(L, buf, buf_len); |
| 2093 | |
| 2094 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2095 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2096 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2097 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2098 | } |
| 2099 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2100 | #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] | 2101 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2102 | { |
| 2103 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2104 | int ret; |
| 2105 | int len; |
| 2106 | char *p; |
| 2107 | |
| 2108 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2109 | if (ret <= 0) { |
| 2110 | lua_pushnil(L); |
| 2111 | return 1; |
| 2112 | } |
| 2113 | |
| 2114 | if (ret == AF_UNIX) { |
| 2115 | lua_pushstring(L, buffer+1); |
| 2116 | return 1; |
| 2117 | } |
| 2118 | else if (ret == AF_INET6) { |
| 2119 | buffer[0] = '['; |
| 2120 | len = strlen(buffer); |
| 2121 | buffer[len] = ']'; |
| 2122 | len++; |
| 2123 | buffer[len] = ':'; |
| 2124 | len++; |
| 2125 | p = buffer; |
| 2126 | } |
| 2127 | else if (ret == AF_INET) { |
| 2128 | p = buffer + 1; |
| 2129 | len = strlen(p); |
| 2130 | p[len] = ':'; |
| 2131 | len++; |
| 2132 | } |
| 2133 | else { |
| 2134 | lua_pushnil(L); |
| 2135 | return 1; |
| 2136 | } |
| 2137 | |
| 2138 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2139 | lua_pushnil(L); |
| 2140 | return 1; |
| 2141 | } |
| 2142 | |
| 2143 | lua_pushstring(L, p); |
| 2144 | return 1; |
| 2145 | } |
| 2146 | |
| 2147 | /* Returns information about the peer of the connection. */ |
| 2148 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2149 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2150 | struct hlua_socket *socket; |
| 2151 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2152 | struct xref *peer; |
| 2153 | struct appctx *appctx; |
| 2154 | struct stream_interface *si; |
| 2155 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2156 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2157 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2158 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2159 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2160 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2161 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2162 | /* Check if we run on the same thread than the xreator thread. |
| 2163 | * We cannot access to the socket if the thread is different. |
| 2164 | */ |
| 2165 | if (socket->tid != tid) |
| 2166 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2167 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2168 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2169 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2170 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2171 | lua_pushnil(L); |
| 2172 | return 1; |
| 2173 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2174 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2175 | si = appctx->owner; |
| 2176 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2177 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2178 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2179 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2180 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2181 | lua_pushnil(L); |
| 2182 | return 1; |
| 2183 | } |
| 2184 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2185 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2186 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2187 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2188 | lua_pushnil(L); |
| 2189 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2190 | } |
| 2191 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2192 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2193 | xref_unlock(&socket->xref, peer); |
| 2194 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2195 | } |
| 2196 | |
| 2197 | /* Returns information about my connection side. */ |
| 2198 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2199 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2200 | struct hlua_socket *socket; |
| 2201 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2202 | struct appctx *appctx; |
| 2203 | struct xref *peer; |
| 2204 | struct stream_interface *si; |
| 2205 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2206 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2207 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2208 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2209 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2210 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2211 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2212 | /* Check if we run on the same thread than the xreator thread. |
| 2213 | * We cannot access to the socket if the thread is different. |
| 2214 | */ |
| 2215 | if (socket->tid != tid) |
| 2216 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2217 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2218 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2219 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2220 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2221 | lua_pushnil(L); |
| 2222 | return 1; |
| 2223 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2224 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2225 | si = appctx->owner; |
| 2226 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2227 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2228 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2229 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2230 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2231 | lua_pushnil(L); |
| 2232 | return 1; |
| 2233 | } |
| 2234 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2235 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2236 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2237 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2238 | lua_pushnil(L); |
| 2239 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2240 | } |
| 2241 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2242 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2243 | xref_unlock(&socket->xref, peer); |
| 2244 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2248 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2249 | .obj_type = OBJ_TYPE_APPLET, |
| 2250 | .name = "<LUA_TCP>", |
| 2251 | .fct = hlua_socket_handler, |
| 2252 | .release = hlua_socket_release, |
| 2253 | }; |
| 2254 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2255 | __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] | 2256 | { |
| 2257 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2258 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2259 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2260 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2261 | struct stream_interface *si; |
| 2262 | struct stream *s; |
| 2263 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2264 | /* Check if we run on the same thread than the xreator thread. |
| 2265 | * We cannot access to the socket if the thread is different. |
| 2266 | */ |
| 2267 | if (socket->tid != tid) |
| 2268 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2269 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2270 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2271 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2272 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2273 | lua_pushnil(L); |
| 2274 | lua_pushstring(L, "Can't connect"); |
| 2275 | return 2; |
| 2276 | } |
| 2277 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2278 | si = appctx->owner; |
| 2279 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2280 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2281 | /* Check if we run on the same thread than the xreator thread. |
| 2282 | * We cannot access to the socket if the thread is different. |
| 2283 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2284 | if (socket->tid != tid) { |
| 2285 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2286 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2287 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2288 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2289 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2290 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2291 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2292 | lua_pushnil(L); |
| 2293 | lua_pushstring(L, "Can't connect"); |
| 2294 | return 2; |
| 2295 | } |
| 2296 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2297 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2298 | |
| 2299 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2300 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2301 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2302 | lua_pushinteger(L, 1); |
| 2303 | return 1; |
| 2304 | } |
| 2305 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2306 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2307 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2308 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2309 | } |
| 2310 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2311 | 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] | 2312 | return 0; |
| 2313 | } |
| 2314 | |
| 2315 | /* This function fail or initite the connection. */ |
| 2316 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2317 | { |
| 2318 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2319 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2320 | const char *ip; |
| 2321 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2322 | struct hlua *hlua; |
| 2323 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2324 | int low, high; |
| 2325 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2326 | struct xref *peer; |
| 2327 | struct stream_interface *si; |
| 2328 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2329 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2330 | if (lua_gettop(L) < 2) |
| 2331 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2332 | |
| 2333 | /* Get args. */ |
| 2334 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2335 | |
| 2336 | /* Check if we run on the same thread than the xreator thread. |
| 2337 | * We cannot access to the socket if the thread is different. |
| 2338 | */ |
| 2339 | if (socket->tid != tid) |
| 2340 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2341 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2342 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2343 | if (lua_gettop(L) >= 3) { |
| 2344 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2345 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2346 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2347 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2348 | * that are not enclosed within square brackets. |
| 2349 | */ |
| 2350 | if (port > 0) { |
| 2351 | luaL_buffinit(L, &b); |
| 2352 | luaL_addstring(&b, ip); |
| 2353 | luaL_addchar(&b, ':'); |
| 2354 | luaL_pushresult(&b); |
| 2355 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2356 | } |
| 2357 | } |
| 2358 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2359 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2360 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2361 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2362 | lua_pushnil(L); |
| 2363 | return 1; |
| 2364 | } |
| 2365 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2366 | si = appctx->owner; |
| 2367 | s = si_strm(si); |
| 2368 | |
| 2369 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2370 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2371 | if (!conn) { |
| 2372 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2373 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2374 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2375 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2376 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2377 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2378 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2379 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2380 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2381 | if (!addr) { |
| 2382 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2383 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2384 | } |
| 2385 | if (low != high) { |
| 2386 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2387 | 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] | 2388 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2389 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2390 | |
| 2391 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2392 | if (low == 0) { |
| 2393 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2394 | if (port == -1) { |
| 2395 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2396 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2397 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2398 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2399 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2400 | if (port == -1) { |
| 2401 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2402 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2403 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2404 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2405 | } |
| 2406 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2407 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2408 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2409 | appctx = objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2410 | |
| 2411 | /* inform the stream that we want to be notified whenever the |
| 2412 | * connection completes. |
| 2413 | */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2414 | si_applet_cant_get(&s->si[0]); |
| 2415 | si_applet_cant_put(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2416 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2417 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2418 | hlua->flags |= HLUA_MUST_GC; |
| 2419 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2420 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2421 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2422 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2423 | } |
| 2424 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2425 | 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] | 2426 | |
| 2427 | return 0; |
| 2428 | } |
| 2429 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2430 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2431 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2432 | { |
| 2433 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2434 | struct xref *peer; |
| 2435 | struct appctx *appctx; |
| 2436 | struct stream_interface *si; |
| 2437 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2438 | |
| 2439 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2440 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2441 | |
| 2442 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2443 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2444 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2445 | lua_pushnil(L); |
| 2446 | return 1; |
| 2447 | } |
| 2448 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2449 | si = appctx->owner; |
| 2450 | s = si_strm(si); |
| 2451 | |
| 2452 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2453 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2454 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2455 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2456 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2457 | |
| 2458 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2459 | { |
| 2460 | return 0; |
| 2461 | } |
| 2462 | |
| 2463 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2464 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2465 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2466 | int tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2467 | struct xref *peer; |
| 2468 | struct appctx *appctx; |
| 2469 | struct stream_interface *si; |
| 2470 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2471 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2472 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2473 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2474 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2475 | tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2476 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2477 | /* Check if we run on the same thread than the xreator thread. |
| 2478 | * We cannot access to the socket if the thread is different. |
| 2479 | */ |
| 2480 | if (socket->tid != tid) |
| 2481 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2482 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2483 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2484 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2485 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2486 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2487 | WILL_LJMP(lua_error(L)); |
| 2488 | return 0; |
| 2489 | } |
| 2490 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2491 | si = appctx->owner; |
| 2492 | s = si_strm(si); |
| 2493 | |
| 2494 | s->req.rto = tmout; |
| 2495 | s->req.wto = tmout; |
| 2496 | s->res.rto = tmout; |
| 2497 | s->res.wto = tmout; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2498 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2499 | |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2500 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2504 | { |
| 2505 | struct hlua_socket *socket; |
| 2506 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2507 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2508 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2509 | |
| 2510 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2511 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2512 | hlua_pusherror(L, "socket: full stack"); |
| 2513 | goto out_fail_conf; |
| 2514 | } |
| 2515 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2516 | /* Create the object: obj[0] = userdata. */ |
| 2517 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2518 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2519 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2520 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2521 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2522 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2523 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2524 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2525 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2526 | goto out_fail_conf; |
| 2527 | } |
| 2528 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2529 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2530 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2531 | lua_setmetatable(L, -2); |
| 2532 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2533 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2534 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2535 | if (!appctx) { |
| 2536 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2537 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2538 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2539 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2540 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2541 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2542 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2543 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2544 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2545 | /* Now create a session, task and stream for this applet */ |
| 2546 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2547 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2548 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2549 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2550 | } |
| 2551 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2552 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2553 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2554 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2555 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2556 | } |
| 2557 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2558 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2559 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2560 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2561 | /* Configure "right" stream interface. this "si" is used to connect |
| 2562 | * and retrieve data from the server. The connection is initialized |
| 2563 | * with the "struct server". |
| 2564 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2565 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2566 | |
| 2567 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2568 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2569 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2570 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2571 | task_wakeup(strm->task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2572 | /* Return yield waiting for connection. */ |
| 2573 | return 1; |
| 2574 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2575 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2576 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2577 | out_fail_sess: |
| 2578 | appctx_free(appctx); |
| 2579 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2580 | WILL_LJMP(lua_error(L)); |
| 2581 | return 0; |
| 2582 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2583 | |
| 2584 | /* |
| 2585 | * |
| 2586 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2587 | * Class Channel |
| 2588 | * |
| 2589 | * |
| 2590 | */ |
| 2591 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2592 | /* The state between the channel data and the HTTP parser state can be |
| 2593 | * unconsistent, so reset the parser and call it again. Warning, this |
| 2594 | * action not revalidate the request and not send a 400 if the modified |
| 2595 | * resuest is not valid. |
| 2596 | * |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2597 | * This function never fails. The direction is set using dir, which equals |
| 2598 | * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2599 | */ |
| 2600 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2601 | { |
| 2602 | /* Protocol HTTP. */ |
| 2603 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2604 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2605 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2606 | http_txn_reset_req(stream->txn); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2607 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2608 | http_txn_reset_res(stream->txn); |
| 2609 | |
| 2610 | if (stream->txn->hdr_idx.v) |
| 2611 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2612 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2613 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2614 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2615 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2616 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2617 | } |
| 2618 | } |
| 2619 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2620 | /* This function is called before the Lua execution. It stores |
| 2621 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2622 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2623 | 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] | 2624 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2625 | c->mode = stream->be->mode; |
| 2626 | switch (c->mode) { |
| 2627 | case PR_MODE_HTTP: |
| 2628 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2629 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2630 | c->data.http.state = stream->txn->req.msg_state; |
| 2631 | else |
| 2632 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2633 | break; |
| 2634 | default: |
| 2635 | break; |
| 2636 | } |
| 2637 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2638 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2639 | /* This function is called after the Lua execution. it |
| 2640 | * returns true if the parser state is consistent, otherwise, |
| 2641 | * it return false. |
| 2642 | * |
| 2643 | * In HTTP mode, the parser state must be in the same state |
| 2644 | * or greater when we exit the function. Even if we do a |
| 2645 | * control yield. This prevent to break the HTTP message |
| 2646 | * from the Lua code. |
| 2647 | */ |
| 2648 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2649 | { |
| 2650 | if (c->mode != stream->be->mode) |
| 2651 | return 0; |
| 2652 | |
| 2653 | switch (c->mode) { |
| 2654 | case PR_MODE_HTTP: |
| 2655 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2656 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2657 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2658 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2659 | else |
| 2660 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2661 | default: |
| 2662 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2663 | } |
| 2664 | return 1; |
| 2665 | } |
| 2666 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2667 | /* Returns the struct hlua_channel join to the class channel in the |
| 2668 | * stack entry "ud" or throws an argument error. |
| 2669 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2670 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2671 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2672 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2673 | } |
| 2674 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2675 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2676 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2677 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2678 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2679 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2680 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2681 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2682 | return 0; |
| 2683 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2684 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2685 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2686 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2687 | |
| 2688 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2689 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2690 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2691 | return 1; |
| 2692 | } |
| 2693 | |
| 2694 | /* Duplicate all the data present in the input channel and put it |
| 2695 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2696 | * the stack if the channel is closed and all the data are consumed, |
| 2697 | * returns 0 if no data are available, otherwise it returns the length |
| 2698 | * of the builded string. |
| 2699 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2700 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2701 | { |
| 2702 | char *blk1; |
| 2703 | char *blk2; |
| 2704 | int len1; |
| 2705 | int len2; |
| 2706 | int ret; |
| 2707 | luaL_Buffer b; |
| 2708 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2709 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2710 | if (unlikely(ret == 0)) |
| 2711 | return 0; |
| 2712 | |
| 2713 | if (unlikely(ret < 0)) { |
| 2714 | lua_pushnil(L); |
| 2715 | return -1; |
| 2716 | } |
| 2717 | |
| 2718 | luaL_buffinit(L, &b); |
| 2719 | luaL_addlstring(&b, blk1, len1); |
| 2720 | if (unlikely(ret == 2)) |
| 2721 | luaL_addlstring(&b, blk2, len2); |
| 2722 | luaL_pushresult(&b); |
| 2723 | |
| 2724 | if (unlikely(ret == 2)) |
| 2725 | return len1 + len2; |
| 2726 | return len1; |
| 2727 | } |
| 2728 | |
| 2729 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2730 | * a yield. This function keep the data in the buffer. |
| 2731 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2732 | __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] | 2733 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2734 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2735 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2736 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2737 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2738 | if (_hlua_channel_dup(chn, L) == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2739 | 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] | 2740 | return 1; |
| 2741 | } |
| 2742 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2743 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2744 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2745 | { |
| 2746 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2747 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2748 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2749 | } |
| 2750 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2751 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2752 | * a yield. This function consumes the data in the buffer. It returns |
| 2753 | * a string containing the data or a nil pointer if no data are available |
| 2754 | * and the channel is closed. |
| 2755 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2756 | __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] | 2757 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2758 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2759 | int ret; |
| 2760 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2761 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2762 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2763 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2764 | if (unlikely(ret == 0)) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2765 | 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] | 2766 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2767 | if (unlikely(ret == -1)) |
| 2768 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2769 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2770 | chn->buf->i -= ret; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2771 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2772 | return 1; |
| 2773 | } |
| 2774 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2775 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 2776 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2777 | { |
| 2778 | MAY_LJMP(check_args(L, 1, "get")); |
| 2779 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2780 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2781 | } |
| 2782 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2783 | /* This functions consumes and returns one line. If the channel is closed, |
| 2784 | * and the last data does not contains a final '\n', the data are returned |
| 2785 | * without the final '\n'. When no more data are avalaible, it returns nil |
| 2786 | * value. |
| 2787 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2788 | __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] | 2789 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2790 | char *blk1; |
| 2791 | char *blk2; |
| 2792 | int len1; |
| 2793 | int len2; |
| 2794 | int len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2795 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2796 | int ret; |
| 2797 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2798 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2799 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2800 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2801 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2802 | if (ret == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2803 | 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] | 2804 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2805 | if (ret == -1) { |
| 2806 | lua_pushnil(L); |
| 2807 | return 1; |
| 2808 | } |
| 2809 | |
| 2810 | luaL_buffinit(L, &b); |
| 2811 | luaL_addlstring(&b, blk1, len1); |
| 2812 | len = len1; |
| 2813 | if (unlikely(ret == 2)) { |
| 2814 | luaL_addlstring(&b, blk2, len2); |
| 2815 | len += len2; |
| 2816 | } |
| 2817 | luaL_pushresult(&b); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2818 | 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] | 2819 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2820 | return 1; |
| 2821 | } |
| 2822 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2823 | /* Check arguments for the fucntion "hlua_channel_getline_yield". */ |
| 2824 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2825 | { |
| 2826 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2827 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2828 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2829 | } |
| 2830 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2831 | /* This function takes a string as input, and append it at the |
| 2832 | * input side of channel. If the data is too big, but a space |
| 2833 | * is probably available after sending some data, the function |
| 2834 | * yield. If the data is bigger than the buffer, or if the |
| 2835 | * channel is closed, it returns -1. otherwise, it returns the |
| 2836 | * amount of data writed. |
| 2837 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2838 | __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] | 2839 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2840 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2841 | size_t len; |
| 2842 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2843 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2844 | int ret; |
| 2845 | int max; |
| 2846 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2847 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2848 | * the request buffer if its not required. |
| 2849 | */ |
| 2850 | if (chn->buf->size == 0) { |
| 2851 | si_applet_cant_put(chn_prod(chn)); |
| 2852 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
| 2853 | } |
| 2854 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2855 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2856 | if (max > len - l) |
| 2857 | max = len - l; |
| 2858 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2859 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2860 | if (ret == -2 || ret == -3) { |
| 2861 | lua_pushinteger(L, -1); |
| 2862 | return 1; |
| 2863 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2864 | if (ret == -1) { |
| 2865 | chn->flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2866 | 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] | 2867 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2868 | l += ret; |
| 2869 | lua_pop(L, 1); |
| 2870 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2871 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2872 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2873 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
| 2874 | if (max == 0 && chn->buf->o == 0) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2875 | /* There are no space avalaible, and the output buffer is empty. |
| 2876 | * in this case, we cannot add more data, so we cannot yield, |
| 2877 | * we return the amount of copyied data. |
| 2878 | */ |
| 2879 | return 1; |
| 2880 | } |
| 2881 | if (l < len) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2882 | 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] | 2883 | return 1; |
| 2884 | } |
| 2885 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2886 | /* just a wrapper of "hlua_channel_append_yield". It returns the length |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2887 | * of the writed string, or -1 if the channel is closed or if the |
| 2888 | * buffer size is too little for the data. |
| 2889 | */ |
| 2890 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2891 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2892 | size_t len; |
| 2893 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2894 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2895 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2896 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2897 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2898 | lua_pushinteger(L, 0); |
| 2899 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2900 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2901 | } |
| 2902 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2903 | /* just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2904 | * his process by cleaning the buffer. The result is a replacement |
| 2905 | * of the current data. It returns the length of the writed string, |
| 2906 | * or -1 if the channel is closed or if the buffer size is too |
| 2907 | * little for the data. |
| 2908 | */ |
| 2909 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2910 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2911 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2912 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2913 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2914 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2915 | lua_pushinteger(L, 0); |
| 2916 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2917 | chn->buf->i = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2918 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2919 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | /* Append data in the output side of the buffer. This data is immediatly |
| 2923 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 2924 | * cannot contains the data, the function yield. The function returns -1 |
| 2925 | * if the channel is closed. |
| 2926 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2927 | __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] | 2928 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2929 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2930 | size_t len; |
| 2931 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2932 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2933 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2934 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2935 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2936 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2937 | lua_pushinteger(L, -1); |
| 2938 | return 1; |
| 2939 | } |
| 2940 | |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2941 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2942 | * the request buffer if its not required. |
| 2943 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2944 | if (chn->buf->size == 0) { |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2945 | si_applet_cant_put(chn_prod(chn)); |
| 2946 | 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] | 2947 | } |
| 2948 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2949 | /* the writed data will be immediatly sent, so we can check |
| 2950 | * the avalaible space without taking in account the reserve. |
| 2951 | * The reserve is guaranted for the processing of incoming |
| 2952 | * data, because the buffer will be flushed. |
| 2953 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2954 | max = chn->buf->size - buffer_len(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2955 | |
| 2956 | /* If there are no space avalaible, and the output buffer is empty. |
| 2957 | * in this case, we cannot add more data, so we cannot yield, |
| 2958 | * we return the amount of copyied data. |
| 2959 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2960 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2961 | return 1; |
| 2962 | |
| 2963 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2964 | if (max > len - l) |
| 2965 | max = len - l; |
| 2966 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2967 | /* The buffer avalaible size may be not contiguous. This test |
| 2968 | * detects a non contiguous buffer and realign it. |
| 2969 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2970 | if (bi_space_for_replace(chn->buf) < max) |
| 2971 | buffer_slow_realign(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2972 | |
| 2973 | /* Copy input data in the buffer. */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2974 | 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] | 2975 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2976 | /* buffer replace considers that the input part is filled. |
| 2977 | * so, I must forward these new data in the output part. |
| 2978 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2979 | b_adv(chn->buf, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2980 | |
| 2981 | l += max; |
| 2982 | lua_pop(L, 1); |
| 2983 | lua_pushinteger(L, l); |
| 2984 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2985 | /* If there are no space avalaible, and the output buffer is empty. |
| 2986 | * in this case, we cannot add more data, so we cannot yield, |
| 2987 | * we return the amount of copyied data. |
| 2988 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2989 | max = chn->buf->size - buffer_len(chn->buf); |
| 2990 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2991 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2992 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2993 | if (l < len) { |
| 2994 | /* If we are waiting for space in the response buffer, we |
| 2995 | * must set the flag WAKERESWR. This flag required the task |
| 2996 | * wake up if any activity is detected on the response buffer. |
| 2997 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2998 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2999 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3000 | else |
| 3001 | HLUA_SET_WAKEREQWR(hlua); |
| 3002 | 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] | 3003 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3004 | |
| 3005 | return 1; |
| 3006 | } |
| 3007 | |
| 3008 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3009 | * yield the LUA process, and resume it without checking the |
| 3010 | * input arguments. |
| 3011 | */ |
| 3012 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3013 | { |
| 3014 | MAY_LJMP(check_args(L, 2, "send")); |
| 3015 | lua_pushinteger(L, 0); |
| 3016 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3017 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | /* This function forward and amount of butes. The data pass from |
| 3021 | * the input side of the buffer to the output side, and can be |
| 3022 | * forwarded. This function never fails. |
| 3023 | * |
| 3024 | * The Lua function takes an amount of bytes to be forwarded in |
| 3025 | * imput. It returns the number of bytes forwarded. |
| 3026 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3027 | __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] | 3028 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3029 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3030 | int len; |
| 3031 | int l; |
| 3032 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3033 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3034 | |
| 3035 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3036 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3037 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3038 | |
| 3039 | max = len - l; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3040 | if (max > chn->buf->i) |
| 3041 | max = chn->buf->i; |
| 3042 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3043 | l += max; |
| 3044 | |
| 3045 | lua_pop(L, 1); |
| 3046 | lua_pushinteger(L, l); |
| 3047 | |
| 3048 | /* Check if it miss bytes to forward. */ |
| 3049 | if (l < len) { |
| 3050 | /* The the input channel or the output channel are closed, we |
| 3051 | * must return the amount of data forwarded. |
| 3052 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3053 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3054 | return 1; |
| 3055 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3056 | /* If we are waiting for space data in the response buffer, we |
| 3057 | * must set the flag WAKERESWR. This flag required the task |
| 3058 | * wake up if any activity is detected on the response buffer. |
| 3059 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3060 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3061 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3062 | else |
| 3063 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3064 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3065 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 3066 | 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] | 3067 | } |
| 3068 | |
| 3069 | return 1; |
| 3070 | } |
| 3071 | |
| 3072 | /* Just check the input and prepare the stack for the previous |
| 3073 | * function "hlua_channel_forward_yield" |
| 3074 | */ |
| 3075 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3076 | { |
| 3077 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3078 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3079 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3080 | |
| 3081 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3082 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3083 | } |
| 3084 | |
| 3085 | /* Just returns the number of bytes available in the input |
| 3086 | * side of the buffer. This function never fails. |
| 3087 | */ |
| 3088 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3089 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3090 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3091 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3092 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3093 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3094 | lua_pushinteger(L, chn->buf->i); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3095 | return 1; |
| 3096 | } |
| 3097 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3098 | /* Returns true if the channel is full. */ |
| 3099 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3100 | { |
| 3101 | struct channel *chn; |
| 3102 | int rem; |
| 3103 | |
| 3104 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3105 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3106 | |
| 3107 | rem = chn->buf->size; |
| 3108 | rem -= chn->buf->o; /* Output size */ |
| 3109 | rem -= chn->buf->i; /* Input size */ |
| 3110 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3111 | |
| 3112 | lua_pushboolean(L, rem <= 0); |
| 3113 | return 1; |
| 3114 | } |
| 3115 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3116 | /* Just returns the number of bytes available in the output |
| 3117 | * side of the buffer. This function never fails. |
| 3118 | */ |
| 3119 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3120 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3121 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3122 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3123 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3124 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3125 | lua_pushinteger(L, chn->buf->o); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3126 | return 1; |
| 3127 | } |
| 3128 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3129 | /* |
| 3130 | * |
| 3131 | * |
| 3132 | * Class Fetches |
| 3133 | * |
| 3134 | * |
| 3135 | */ |
| 3136 | |
| 3137 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3138 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3139 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3140 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3141 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3142 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3143 | } |
| 3144 | |
| 3145 | /* This function creates and push in the stack a fetch object according |
| 3146 | * with a current TXN. |
| 3147 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3148 | 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] | 3149 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3150 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3151 | |
| 3152 | /* Check stack size. */ |
| 3153 | if (!lua_checkstack(L, 3)) |
| 3154 | return 0; |
| 3155 | |
| 3156 | /* Create the object: obj[0] = userdata. |
| 3157 | * Note that the base of the Fetches object is the |
| 3158 | * transaction object. |
| 3159 | */ |
| 3160 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3161 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3162 | lua_rawseti(L, -2, 0); |
| 3163 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3164 | hsmp->s = txn->s; |
| 3165 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3166 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3167 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3168 | |
| 3169 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3170 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3171 | lua_setmetatable(L, -2); |
| 3172 | |
| 3173 | return 1; |
| 3174 | } |
| 3175 | |
| 3176 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3177 | * It uses closure argument to store the associated sample-fetch. It |
| 3178 | * returns only one argument or throws an error. An error is thrown |
| 3179 | * only if an error is encountered during the argument parsing. If |
| 3180 | * the "sample-fetch" function fails, nil is returned. |
| 3181 | */ |
| 3182 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3183 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3184 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3185 | struct sample_fetch *f; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3186 | struct arg args[ARGM_NBARGS + 1]; |
| 3187 | int i; |
| 3188 | struct sample smp; |
| 3189 | |
| 3190 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3191 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3192 | |
| 3193 | /* Get traditionnal arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3194 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3195 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3196 | /* Check execution authorization. */ |
| 3197 | if (f->use & SMP_USE_HTTP_ANY && |
| 3198 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3199 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3200 | "is not available in Lua services", f->kw); |
| 3201 | WILL_LJMP(lua_error(L)); |
| 3202 | } |
| 3203 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3204 | /* Get extra arguments. */ |
| 3205 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3206 | if (i >= ARGM_NBARGS) |
| 3207 | break; |
| 3208 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3209 | } |
| 3210 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 3211 | args[i].data.str.str = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3212 | |
| 3213 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3214 | 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] | 3215 | |
| 3216 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3217 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3218 | lua_pushfstring(L, "error in arguments"); |
| 3219 | WILL_LJMP(lua_error(L)); |
| 3220 | } |
| 3221 | |
| 3222 | /* Initialise the sample. */ |
| 3223 | memset(&smp, 0, sizeof(smp)); |
| 3224 | |
| 3225 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3226 | 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] | 3227 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3228 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3229 | lua_pushstring(L, ""); |
| 3230 | else |
| 3231 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3232 | return 1; |
| 3233 | } |
| 3234 | |
| 3235 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3236 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3237 | hlua_smp2lua_str(L, &smp); |
| 3238 | else |
| 3239 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3240 | return 1; |
| 3241 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3242 | |
| 3243 | /* |
| 3244 | * |
| 3245 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3246 | * Class Converters |
| 3247 | * |
| 3248 | * |
| 3249 | */ |
| 3250 | |
| 3251 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3252 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3253 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3254 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3255 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3256 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3257 | } |
| 3258 | |
| 3259 | /* This function creates and push in the stack a Converters object |
| 3260 | * according with a current TXN. |
| 3261 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3262 | 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] | 3263 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3264 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3265 | |
| 3266 | /* Check stack size. */ |
| 3267 | if (!lua_checkstack(L, 3)) |
| 3268 | return 0; |
| 3269 | |
| 3270 | /* Create the object: obj[0] = userdata. |
| 3271 | * Note that the base of the Converters object is the |
| 3272 | * same than the TXN object. |
| 3273 | */ |
| 3274 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3275 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3276 | lua_rawseti(L, -2, 0); |
| 3277 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3278 | hsmp->s = txn->s; |
| 3279 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3280 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3281 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3282 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3283 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3284 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3285 | lua_setmetatable(L, -2); |
| 3286 | |
| 3287 | return 1; |
| 3288 | } |
| 3289 | |
| 3290 | /* This function is an LUA binding. It is called with each converter. |
| 3291 | * It uses closure argument to store the associated converter. It |
| 3292 | * returns only one argument or throws an error. An error is thrown |
| 3293 | * only if an error is encountered during the argument parsing. If |
| 3294 | * the converter function function fails, nil is returned. |
| 3295 | */ |
| 3296 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3297 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3298 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3299 | struct sample_conv *conv; |
| 3300 | struct arg args[ARGM_NBARGS + 1]; |
| 3301 | int i; |
| 3302 | struct sample smp; |
| 3303 | |
| 3304 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3305 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3306 | |
| 3307 | /* Get traditionnal arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3308 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3309 | |
| 3310 | /* Get extra arguments. */ |
| 3311 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3312 | if (i >= ARGM_NBARGS) |
| 3313 | break; |
| 3314 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3315 | } |
| 3316 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 3317 | args[i].data.str.str = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3318 | |
| 3319 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3320 | 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] | 3321 | |
| 3322 | /* Run the special args checker. */ |
| 3323 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3324 | hlua_pusherror(L, "error in arguments"); |
| 3325 | WILL_LJMP(lua_error(L)); |
| 3326 | } |
| 3327 | |
| 3328 | /* Initialise the sample. */ |
| 3329 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3330 | hlua_pusherror(L, "error in the input argument"); |
| 3331 | WILL_LJMP(lua_error(L)); |
| 3332 | } |
| 3333 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3334 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3335 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3336 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3337 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3338 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3339 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3340 | WILL_LJMP(lua_error(L)); |
| 3341 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3342 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3343 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3344 | hlua_pusherror(L, "error during the input argument casting"); |
| 3345 | WILL_LJMP(lua_error(L)); |
| 3346 | } |
| 3347 | |
| 3348 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3349 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3350 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3351 | lua_pushstring(L, ""); |
| 3352 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3353 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3354 | return 1; |
| 3355 | } |
| 3356 | |
| 3357 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3358 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3359 | hlua_smp2lua_str(L, &smp); |
| 3360 | else |
| 3361 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3362 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3363 | } |
| 3364 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3365 | /* |
| 3366 | * |
| 3367 | * |
| 3368 | * Class AppletTCP |
| 3369 | * |
| 3370 | * |
| 3371 | */ |
| 3372 | |
| 3373 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3374 | * a class stream, otherwise it throws an error. |
| 3375 | */ |
| 3376 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3377 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3378 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | /* This function creates and push in the stack an Applet object |
| 3382 | * according with a current TXN. |
| 3383 | */ |
| 3384 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3385 | { |
| 3386 | struct hlua_appctx *appctx; |
| 3387 | struct stream_interface *si = ctx->owner; |
| 3388 | struct stream *s = si_strm(si); |
| 3389 | struct proxy *p = s->be; |
| 3390 | |
| 3391 | /* Check stack size. */ |
| 3392 | if (!lua_checkstack(L, 3)) |
| 3393 | return 0; |
| 3394 | |
| 3395 | /* Create the object: obj[0] = userdata. |
| 3396 | * Note that the base of the Converters object is the |
| 3397 | * same than the TXN object. |
| 3398 | */ |
| 3399 | lua_newtable(L); |
| 3400 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3401 | lua_rawseti(L, -2, 0); |
| 3402 | appctx->appctx = ctx; |
| 3403 | appctx->htxn.s = s; |
| 3404 | appctx->htxn.p = p; |
| 3405 | |
| 3406 | /* Create the "f" field that contains a list of fetches. */ |
| 3407 | lua_pushstring(L, "f"); |
| 3408 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3409 | return 0; |
| 3410 | lua_settable(L, -3); |
| 3411 | |
| 3412 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3413 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3414 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3415 | return 0; |
| 3416 | lua_settable(L, -3); |
| 3417 | |
| 3418 | /* Create the "c" field that contains a list of converters. */ |
| 3419 | lua_pushstring(L, "c"); |
| 3420 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3421 | return 0; |
| 3422 | lua_settable(L, -3); |
| 3423 | |
| 3424 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3425 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3426 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3427 | return 0; |
| 3428 | lua_settable(L, -3); |
| 3429 | |
| 3430 | /* Pop a class stream metatable and affect it to the table. */ |
| 3431 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3432 | lua_setmetatable(L, -2); |
| 3433 | |
| 3434 | return 1; |
| 3435 | } |
| 3436 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3437 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3438 | { |
| 3439 | struct hlua_appctx *appctx; |
| 3440 | struct stream *s; |
| 3441 | const char *name; |
| 3442 | size_t len; |
| 3443 | struct sample smp; |
| 3444 | |
| 3445 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3446 | |
| 3447 | /* It is useles to retrieve the stream, but this function |
| 3448 | * runs only in a stream context. |
| 3449 | */ |
| 3450 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3451 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3452 | s = appctx->htxn.s; |
| 3453 | |
| 3454 | /* Converts the third argument in a sample. */ |
| 3455 | hlua_lua2smp(L, 3, &smp); |
| 3456 | |
| 3457 | /* Store the sample in a variable. */ |
| 3458 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3459 | vars_set_by_name(name, len, &smp); |
| 3460 | return 0; |
| 3461 | } |
| 3462 | |
| 3463 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3464 | { |
| 3465 | struct hlua_appctx *appctx; |
| 3466 | struct stream *s; |
| 3467 | const char *name; |
| 3468 | size_t len; |
| 3469 | struct sample smp; |
| 3470 | |
| 3471 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3472 | |
| 3473 | /* It is useles to retrieve the stream, but this function |
| 3474 | * runs only in a stream context. |
| 3475 | */ |
| 3476 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3477 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3478 | s = appctx->htxn.s; |
| 3479 | |
| 3480 | /* Unset the variable. */ |
| 3481 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3482 | vars_unset_by_name(name, len, &smp); |
| 3483 | return 0; |
| 3484 | } |
| 3485 | |
| 3486 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3487 | { |
| 3488 | struct hlua_appctx *appctx; |
| 3489 | struct stream *s; |
| 3490 | const char *name; |
| 3491 | size_t len; |
| 3492 | struct sample smp; |
| 3493 | |
| 3494 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3495 | |
| 3496 | /* It is useles to retrieve the stream, but this function |
| 3497 | * runs only in a stream context. |
| 3498 | */ |
| 3499 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3500 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3501 | s = appctx->htxn.s; |
| 3502 | |
| 3503 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3504 | if (!vars_get_by_name(name, len, &smp)) { |
| 3505 | lua_pushnil(L); |
| 3506 | return 1; |
| 3507 | } |
| 3508 | |
| 3509 | return hlua_smp2lua(L, &smp); |
| 3510 | } |
| 3511 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3512 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3513 | { |
| 3514 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3515 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3516 | struct hlua *hlua; |
| 3517 | |
| 3518 | /* 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] | 3519 | if (!s->hlua) |
| 3520 | return 0; |
| 3521 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3522 | |
| 3523 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3524 | |
| 3525 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3526 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3527 | |
| 3528 | /* Get and store new value. */ |
| 3529 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3530 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3531 | |
| 3532 | return 0; |
| 3533 | } |
| 3534 | |
| 3535 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3536 | { |
| 3537 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3538 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3539 | struct hlua *hlua; |
| 3540 | |
| 3541 | /* 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] | 3542 | if (!s->hlua) { |
| 3543 | lua_pushnil(L); |
| 3544 | return 1; |
| 3545 | } |
| 3546 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3547 | |
| 3548 | /* Push configuration index in the stack. */ |
| 3549 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3550 | |
| 3551 | return 1; |
| 3552 | } |
| 3553 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3554 | /* If expected data not yet available, it returns a yield. This function |
| 3555 | * consumes the data in the buffer. It returns a string containing the |
| 3556 | * data. This string can be empty. |
| 3557 | */ |
| 3558 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3559 | { |
| 3560 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3561 | struct stream_interface *si = appctx->appctx->owner; |
| 3562 | int ret; |
| 3563 | char *blk1; |
| 3564 | int len1; |
| 3565 | char *blk2; |
| 3566 | int len2; |
| 3567 | |
| 3568 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3569 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3570 | |
| 3571 | /* Data not yet avalaible. return yield. */ |
| 3572 | if (ret == 0) { |
| 3573 | si_applet_cant_get(si); |
| 3574 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
| 3575 | } |
| 3576 | |
| 3577 | /* End of data: commit the total strings and return. */ |
| 3578 | if (ret < 0) { |
| 3579 | luaL_pushresult(&appctx->b); |
| 3580 | return 1; |
| 3581 | } |
| 3582 | |
| 3583 | /* Ensure that the block 2 length is usable. */ |
| 3584 | if (ret == 1) |
| 3585 | len2 = 0; |
| 3586 | |
| 3587 | /* dont check the max length read and dont check. */ |
| 3588 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3589 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3590 | |
| 3591 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3592 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3593 | luaL_pushresult(&appctx->b); |
| 3594 | return 1; |
| 3595 | } |
| 3596 | |
| 3597 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3598 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3599 | { |
| 3600 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3601 | |
| 3602 | /* Initialise the string catenation. */ |
| 3603 | luaL_buffinit(L, &appctx->b); |
| 3604 | |
| 3605 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3606 | } |
| 3607 | |
| 3608 | /* If expected data not yet available, it returns a yield. This function |
| 3609 | * consumes the data in the buffer. It returns a string containing the |
| 3610 | * data. This string can be empty. |
| 3611 | */ |
| 3612 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3613 | { |
| 3614 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3615 | struct stream_interface *si = appctx->appctx->owner; |
| 3616 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3617 | int ret; |
| 3618 | char *blk1; |
| 3619 | int len1; |
| 3620 | char *blk2; |
| 3621 | int len2; |
| 3622 | |
| 3623 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3624 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3625 | |
| 3626 | /* Data not yet avalaible. return yield. */ |
| 3627 | if (ret == 0) { |
| 3628 | si_applet_cant_get(si); |
| 3629 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3630 | } |
| 3631 | |
| 3632 | /* End of data: commit the total strings and return. */ |
| 3633 | if (ret < 0) { |
| 3634 | luaL_pushresult(&appctx->b); |
| 3635 | return 1; |
| 3636 | } |
| 3637 | |
| 3638 | /* Ensure that the block 2 length is usable. */ |
| 3639 | if (ret == 1) |
| 3640 | len2 = 0; |
| 3641 | |
| 3642 | if (len == -1) { |
| 3643 | |
| 3644 | /* If len == -1, catenate all the data avalaile and |
| 3645 | * yield because we want to get all the data until |
| 3646 | * the end of data stream. |
| 3647 | */ |
| 3648 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3649 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3650 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3651 | si_applet_cant_get(si); |
| 3652 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3653 | |
| 3654 | } else { |
| 3655 | |
| 3656 | /* Copy the fisrt block caping to the length required. */ |
| 3657 | if (len1 > len) |
| 3658 | len1 = len; |
| 3659 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3660 | len -= len1; |
| 3661 | |
| 3662 | /* Copy the second block. */ |
| 3663 | if (len2 > len) |
| 3664 | len2 = len; |
| 3665 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3666 | len -= len2; |
| 3667 | |
| 3668 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3669 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3670 | |
| 3671 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3672 | if (len > 0) { |
| 3673 | lua_pushinteger(L, len); |
| 3674 | lua_replace(L, 2); |
| 3675 | si_applet_cant_get(si); |
| 3676 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3677 | } |
| 3678 | |
| 3679 | /* return the result. */ |
| 3680 | luaL_pushresult(&appctx->b); |
| 3681 | return 1; |
| 3682 | } |
| 3683 | |
| 3684 | /* we never executes this */ |
| 3685 | hlua_pusherror(L, "Lua: internal error"); |
| 3686 | WILL_LJMP(lua_error(L)); |
| 3687 | return 0; |
| 3688 | } |
| 3689 | |
| 3690 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3691 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3692 | { |
| 3693 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3694 | int len = -1; |
| 3695 | |
| 3696 | if (lua_gettop(L) > 2) |
| 3697 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3698 | if (lua_gettop(L) >= 2) { |
| 3699 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3700 | lua_pop(L, 1); |
| 3701 | } |
| 3702 | |
| 3703 | /* Confirm or set the required length */ |
| 3704 | lua_pushinteger(L, len); |
| 3705 | |
| 3706 | /* Initialise the string catenation. */ |
| 3707 | luaL_buffinit(L, &appctx->b); |
| 3708 | |
| 3709 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3710 | } |
| 3711 | |
| 3712 | /* Append data in the output side of the buffer. This data is immediatly |
| 3713 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3714 | * cannot contains the data, the function yield. The function returns -1 |
| 3715 | * if the channel is closed. |
| 3716 | */ |
| 3717 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3718 | { |
| 3719 | size_t len; |
| 3720 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3721 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3722 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3723 | struct stream_interface *si = appctx->appctx->owner; |
| 3724 | struct channel *chn = si_ic(si); |
| 3725 | int max; |
| 3726 | |
| 3727 | /* Get the max amount of data which can write as input in the channel. */ |
| 3728 | max = channel_recv_max(chn); |
| 3729 | if (max > (len - l)) |
| 3730 | max = len - l; |
| 3731 | |
| 3732 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3733 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3734 | |
| 3735 | /* update counters. */ |
| 3736 | l += max; |
| 3737 | lua_pop(L, 1); |
| 3738 | lua_pushinteger(L, l); |
| 3739 | |
| 3740 | /* If some data is not send, declares the situation to the |
| 3741 | * applet, and returns a yield. |
| 3742 | */ |
| 3743 | if (l < len) { |
| 3744 | si_applet_cant_put(si); |
| 3745 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
| 3746 | } |
| 3747 | |
| 3748 | return 1; |
| 3749 | } |
| 3750 | |
| 3751 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3752 | * yield the LUA process, and resume it without checking the |
| 3753 | * input arguments. |
| 3754 | */ |
| 3755 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3756 | { |
| 3757 | MAY_LJMP(check_args(L, 2, "send")); |
| 3758 | lua_pushinteger(L, 0); |
| 3759 | |
| 3760 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3761 | } |
| 3762 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3763 | /* |
| 3764 | * |
| 3765 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3766 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3767 | * |
| 3768 | * |
| 3769 | */ |
| 3770 | |
| 3771 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3772 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3773 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3774 | __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] | 3775 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3776 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3777 | } |
| 3778 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3779 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3780 | * according with a current TXN. |
| 3781 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3782 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3783 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3784 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3785 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3786 | struct stream_interface *si = ctx->owner; |
| 3787 | struct stream *s = si_strm(si); |
| 3788 | struct proxy *px = s->be; |
| 3789 | struct http_txn *txn = s->txn; |
| 3790 | const char *path; |
| 3791 | const char *end; |
| 3792 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3793 | |
| 3794 | /* Check stack size. */ |
| 3795 | if (!lua_checkstack(L, 3)) |
| 3796 | return 0; |
| 3797 | |
| 3798 | /* Create the object: obj[0] = userdata. |
| 3799 | * Note that the base of the Converters object is the |
| 3800 | * same than the TXN object. |
| 3801 | */ |
| 3802 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3803 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3804 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3805 | appctx->appctx = ctx; |
| 3806 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3807 | 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] | 3808 | appctx->htxn.s = s; |
| 3809 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3810 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3811 | /* Create the "f" field that contains a list of fetches. */ |
| 3812 | lua_pushstring(L, "f"); |
| 3813 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3814 | return 0; |
| 3815 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3816 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3817 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3818 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3819 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3820 | return 0; |
| 3821 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3822 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3823 | /* Create the "c" field that contains a list of converters. */ |
| 3824 | lua_pushstring(L, "c"); |
| 3825 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3826 | return 0; |
| 3827 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3828 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3829 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3830 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3831 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3832 | return 0; |
| 3833 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3834 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3835 | /* Stores the request method. */ |
| 3836 | lua_pushstring(L, "method"); |
| 3837 | lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l); |
| 3838 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3839 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3840 | /* Stores the http version. */ |
| 3841 | lua_pushstring(L, "version"); |
| 3842 | lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
| 3843 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3844 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3845 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3846 | * the array on the top of the stack. |
| 3847 | */ |
| 3848 | lua_pushstring(L, "headers"); |
| 3849 | htxn.s = s; |
| 3850 | htxn.p = px; |
| 3851 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3852 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3853 | return 0; |
| 3854 | lua_settable(L, -3); |
| 3855 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3856 | /* Get path and qs */ |
| 3857 | path = http_get_path(txn); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3858 | if (path) { |
| 3859 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 3860 | p = path; |
| 3861 | while (p < end && *p != '?') |
| 3862 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3863 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3864 | /* Stores the request path. */ |
| 3865 | lua_pushstring(L, "path"); |
| 3866 | lua_pushlstring(L, path, p - path); |
| 3867 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3868 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3869 | /* Stores the query string. */ |
| 3870 | lua_pushstring(L, "qs"); |
| 3871 | if (*p == '?') |
| 3872 | p++; |
| 3873 | lua_pushlstring(L, p, end - p); |
| 3874 | lua_settable(L, -3); |
| 3875 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3876 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3877 | /* Stores the request path. */ |
| 3878 | lua_pushstring(L, "length"); |
| 3879 | lua_pushinteger(L, txn->req.body_len); |
| 3880 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3881 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3882 | /* Create an array of HTTP request headers. */ |
| 3883 | lua_pushstring(L, "headers"); |
| 3884 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3885 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3886 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3887 | /* Create an empty array of HTTP request headers. */ |
| 3888 | lua_pushstring(L, "response"); |
| 3889 | lua_newtable(L); |
| 3890 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3891 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3892 | /* Pop a class stream metatable and affect it to the table. */ |
| 3893 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3894 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3895 | |
| 3896 | return 1; |
| 3897 | } |
| 3898 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3899 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3900 | { |
| 3901 | struct hlua_appctx *appctx; |
| 3902 | struct stream *s; |
| 3903 | const char *name; |
| 3904 | size_t len; |
| 3905 | struct sample smp; |
| 3906 | |
| 3907 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3908 | |
| 3909 | /* It is useles to retrieve the stream, but this function |
| 3910 | * runs only in a stream context. |
| 3911 | */ |
| 3912 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3913 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3914 | s = appctx->htxn.s; |
| 3915 | |
| 3916 | /* Converts the third argument in a sample. */ |
| 3917 | hlua_lua2smp(L, 3, &smp); |
| 3918 | |
| 3919 | /* Store the sample in a variable. */ |
| 3920 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3921 | vars_set_by_name(name, len, &smp); |
| 3922 | return 0; |
| 3923 | } |
| 3924 | |
| 3925 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 3926 | { |
| 3927 | struct hlua_appctx *appctx; |
| 3928 | struct stream *s; |
| 3929 | const char *name; |
| 3930 | size_t len; |
| 3931 | struct sample smp; |
| 3932 | |
| 3933 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3934 | |
| 3935 | /* It is useles to retrieve the stream, but this function |
| 3936 | * runs only in a stream context. |
| 3937 | */ |
| 3938 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3939 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3940 | s = appctx->htxn.s; |
| 3941 | |
| 3942 | /* Unset the variable. */ |
| 3943 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3944 | vars_unset_by_name(name, len, &smp); |
| 3945 | return 0; |
| 3946 | } |
| 3947 | |
| 3948 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 3949 | { |
| 3950 | struct hlua_appctx *appctx; |
| 3951 | struct stream *s; |
| 3952 | const char *name; |
| 3953 | size_t len; |
| 3954 | struct sample smp; |
| 3955 | |
| 3956 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3957 | |
| 3958 | /* It is useles to retrieve the stream, but this function |
| 3959 | * runs only in a stream context. |
| 3960 | */ |
| 3961 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3962 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3963 | s = appctx->htxn.s; |
| 3964 | |
| 3965 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3966 | if (!vars_get_by_name(name, len, &smp)) { |
| 3967 | lua_pushnil(L); |
| 3968 | return 1; |
| 3969 | } |
| 3970 | |
| 3971 | return hlua_smp2lua(L, &smp); |
| 3972 | } |
| 3973 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3974 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 3975 | { |
| 3976 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3977 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3978 | struct hlua *hlua; |
| 3979 | |
| 3980 | /* 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] | 3981 | if (!s->hlua) |
| 3982 | return 0; |
| 3983 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3984 | |
| 3985 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3986 | |
| 3987 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3988 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3989 | |
| 3990 | /* Get and store new value. */ |
| 3991 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3992 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3993 | |
| 3994 | return 0; |
| 3995 | } |
| 3996 | |
| 3997 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 3998 | { |
| 3999 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4000 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4001 | struct hlua *hlua; |
| 4002 | |
| 4003 | /* 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] | 4004 | if (!s->hlua) { |
| 4005 | lua_pushnil(L); |
| 4006 | return 1; |
| 4007 | } |
| 4008 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4009 | |
| 4010 | /* Push configuration index in the stack. */ |
| 4011 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4012 | |
| 4013 | return 1; |
| 4014 | } |
| 4015 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4016 | /* If expected data not yet available, it returns a yield. This function |
| 4017 | * consumes the data in the buffer. It returns a string containing the |
| 4018 | * data. This string can be empty. |
| 4019 | */ |
| 4020 | __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] | 4021 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4022 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4023 | struct stream_interface *si = appctx->appctx->owner; |
| 4024 | struct channel *chn = si_ic(si); |
| 4025 | int ret; |
| 4026 | char *blk1; |
| 4027 | int len1; |
| 4028 | char *blk2; |
| 4029 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4030 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4031 | /* Maybe we cant send a 100-continue ? */ |
| 4032 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4033 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4034 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4035 | * big for the buffers. We cant send anything. So, we ignoring |
| 4036 | * the error, considers that the 100-continue is sent, and try |
| 4037 | * to receive. |
| 4038 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4039 | */ |
| 4040 | if (ret == -1) { |
| 4041 | si_applet_cant_put(si); |
| 4042 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 4043 | } |
| 4044 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 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 | /* Check for the end of the data. */ |
| 4048 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 4049 | luaL_pushresult(&appctx->b); |
| 4050 | return 1; |
| 4051 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4052 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4053 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4054 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4055 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4056 | /* Data not yet avalaible. return yield. */ |
| 4057 | if (ret == 0) { |
| 4058 | si_applet_cant_get(si); |
| 4059 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 4060 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4061 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4062 | /* End of data: commit the total strings and return. */ |
| 4063 | if (ret < 0) { |
| 4064 | luaL_pushresult(&appctx->b); |
| 4065 | return 1; |
| 4066 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4067 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4068 | /* Ensure that the block 2 length is usable. */ |
| 4069 | if (ret == 1) |
| 4070 | len2 = 0; |
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 | /* Copy the fisrt block caping to the length required. */ |
| 4073 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4074 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4075 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4076 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4077 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4078 | /* Copy the second block. */ |
| 4079 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4080 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4081 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4082 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4083 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4084 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4085 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4086 | luaL_pushresult(&appctx->b); |
| 4087 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4088 | } |
| 4089 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4090 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 4091 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4092 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4093 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4094 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4095 | /* Initialise the string catenation. */ |
| 4096 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4097 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4098 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4099 | } |
| 4100 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4101 | /* If expected data not yet available, it returns a yield. This function |
| 4102 | * consumes the data in the buffer. It returns a string containing the |
| 4103 | * data. This string can be empty. |
| 4104 | */ |
| 4105 | __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] | 4106 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4107 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4108 | struct stream_interface *si = appctx->appctx->owner; |
| 4109 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4110 | struct channel *chn = si_ic(si); |
| 4111 | int ret; |
| 4112 | char *blk1; |
| 4113 | int len1; |
| 4114 | char *blk2; |
| 4115 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4116 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4117 | /* Maybe we cant send a 100-continue ? */ |
| 4118 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4119 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4120 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4121 | * big for the buffers. We cant send anything. So, we ignoring |
| 4122 | * the error, considers that the 100-continue is sent, and try |
| 4123 | * to receive. |
| 4124 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 4125 | */ |
| 4126 | if (ret == -1) { |
| 4127 | si_applet_cant_put(si); |
| 4128 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4129 | } |
| 4130 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 4131 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4132 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4133 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4134 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4135 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4136 | /* Data not yet avalaible. return yield. */ |
| 4137 | if (ret == 0) { |
| 4138 | si_applet_cant_get(si); |
| 4139 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4140 | } |
| 4141 | |
| 4142 | /* End of data: commit the total strings and return. */ |
| 4143 | if (ret < 0) { |
| 4144 | luaL_pushresult(&appctx->b); |
| 4145 | return 1; |
| 4146 | } |
| 4147 | |
| 4148 | /* Ensure that the block 2 length is usable. */ |
| 4149 | if (ret == 1) |
| 4150 | len2 = 0; |
| 4151 | |
| 4152 | /* Copy the fisrt block caping to the length required. */ |
| 4153 | if (len1 > len) |
| 4154 | len1 = len; |
| 4155 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4156 | len -= len1; |
| 4157 | |
| 4158 | /* Copy the second block. */ |
| 4159 | if (len2 > len) |
| 4160 | len2 = len; |
| 4161 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4162 | len -= len2; |
| 4163 | |
| 4164 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4165 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4166 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4167 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4168 | |
| 4169 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 4170 | if (len > 0) { |
| 4171 | lua_pushinteger(L, len); |
| 4172 | lua_replace(L, 2); |
| 4173 | si_applet_cant_get(si); |
| 4174 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4175 | } |
| 4176 | |
| 4177 | /* return the result. */ |
| 4178 | luaL_pushresult(&appctx->b); |
| 4179 | return 1; |
| 4180 | } |
| 4181 | |
| 4182 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 4183 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4184 | { |
| 4185 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4186 | int len = -1; |
| 4187 | |
| 4188 | /* Check arguments. */ |
| 4189 | if (lua_gettop(L) > 2) |
| 4190 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4191 | if (lua_gettop(L) >= 2) { |
| 4192 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4193 | lua_pop(L, 1); |
| 4194 | } |
| 4195 | |
| 4196 | /* Check the required length */ |
| 4197 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4198 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4199 | lua_pushinteger(L, len); |
| 4200 | |
| 4201 | /* Initialise the string catenation. */ |
| 4202 | luaL_buffinit(L, &appctx->b); |
| 4203 | |
| 4204 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4205 | } |
| 4206 | |
| 4207 | /* Append data in the output side of the buffer. This data is immediatly |
| 4208 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 4209 | * cannot contains the data, the function yield. The function returns -1 |
| 4210 | * if the channel is closed. |
| 4211 | */ |
| 4212 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4213 | { |
| 4214 | size_t len; |
| 4215 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4216 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4217 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4218 | struct stream_interface *si = appctx->appctx->owner; |
| 4219 | struct channel *chn = si_ic(si); |
| 4220 | int max; |
| 4221 | |
| 4222 | /* Get the max amount of data which can write as input in the channel. */ |
| 4223 | max = channel_recv_max(chn); |
| 4224 | if (max > (len - l)) |
| 4225 | max = len - l; |
| 4226 | |
| 4227 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4228 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4229 | |
| 4230 | /* update counters. */ |
| 4231 | l += max; |
| 4232 | lua_pop(L, 1); |
| 4233 | lua_pushinteger(L, l); |
| 4234 | |
| 4235 | /* If some data is not send, declares the situation to the |
| 4236 | * applet, and returns a yield. |
| 4237 | */ |
| 4238 | if (l < len) { |
| 4239 | si_applet_cant_put(si); |
| 4240 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
| 4241 | } |
| 4242 | |
| 4243 | return 1; |
| 4244 | } |
| 4245 | |
| 4246 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4247 | * yield the LUA process, and resume it without checking the |
| 4248 | * input arguments. |
| 4249 | */ |
| 4250 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4251 | { |
| 4252 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4253 | size_t len; |
| 4254 | char hex[10]; |
| 4255 | |
| 4256 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4257 | |
| 4258 | /* If transfer encoding chunked is selected, we surround the data |
| 4259 | * by chunk data. |
| 4260 | */ |
| 4261 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4262 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4263 | lua_pushfstring(L, "%s\r\n", hex); |
| 4264 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4265 | lua_pushstring(L, "\r\n"); |
| 4266 | lua_concat(L, 3); |
| 4267 | } |
| 4268 | |
| 4269 | /* This interger is used for followinf the amount of data sent. */ |
| 4270 | lua_pushinteger(L, 0); |
| 4271 | |
| 4272 | /* We want to send some data. Headers must be sent. */ |
| 4273 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4274 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4275 | WILL_LJMP(lua_error(L)); |
| 4276 | } |
| 4277 | |
| 4278 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4279 | } |
| 4280 | |
| 4281 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4282 | { |
| 4283 | const char *name; |
| 4284 | int ret; |
| 4285 | |
| 4286 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4287 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4288 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4289 | |
| 4290 | /* Push in the stack the "response" entry. */ |
| 4291 | ret = lua_getfield(L, 1, "response"); |
| 4292 | if (ret != LUA_TTABLE) { |
| 4293 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4294 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4295 | WILL_LJMP(lua_error(L)); |
| 4296 | } |
| 4297 | |
| 4298 | /* check if the header is already registered if it is not |
| 4299 | * the case, register it. |
| 4300 | */ |
| 4301 | ret = lua_getfield(L, -1, name); |
| 4302 | if (ret == LUA_TNIL) { |
| 4303 | |
| 4304 | /* Entry not found. */ |
| 4305 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4306 | |
| 4307 | /* Insert the new header name in the array in the top of the stack. |
| 4308 | * It left the new array in the top of the stack. |
| 4309 | */ |
| 4310 | lua_newtable(L); |
| 4311 | lua_pushvalue(L, 2); |
| 4312 | lua_pushvalue(L, -2); |
| 4313 | lua_settable(L, -4); |
| 4314 | |
| 4315 | } else if (ret != LUA_TTABLE) { |
| 4316 | |
| 4317 | /* corruption error. */ |
| 4318 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4319 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4320 | WILL_LJMP(lua_error(L)); |
| 4321 | } |
| 4322 | |
| 4323 | /* Now the top od thestack is an array of values. We push |
| 4324 | * the header value as new entry. |
| 4325 | */ |
| 4326 | lua_pushvalue(L, 3); |
| 4327 | ret = lua_rawlen(L, -2); |
| 4328 | lua_rawseti(L, -2, ret + 1); |
| 4329 | lua_pushboolean(L, 1); |
| 4330 | return 1; |
| 4331 | } |
| 4332 | |
| 4333 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4334 | { |
| 4335 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4336 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4337 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4338 | |
| 4339 | if (status < 100 || status > 599) { |
| 4340 | lua_pushboolean(L, 0); |
| 4341 | return 1; |
| 4342 | } |
| 4343 | |
| 4344 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4345 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4346 | lua_pushboolean(L, 1); |
| 4347 | return 1; |
| 4348 | } |
| 4349 | |
| 4350 | /* We will build the status line and the headers of the HTTP response. |
| 4351 | * We will try send at once if its not possible, we give back the hand |
| 4352 | * waiting for more room. |
| 4353 | */ |
| 4354 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4355 | { |
| 4356 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4357 | struct stream_interface *si = appctx->appctx->owner; |
| 4358 | struct channel *chn = si_ic(si); |
| 4359 | int ret; |
| 4360 | size_t len; |
| 4361 | const char *msg; |
| 4362 | |
| 4363 | /* Get the message as the first argument on the stack. */ |
| 4364 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4365 | |
| 4366 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4367 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4368 | |
| 4369 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4370 | * big for the buffers. |
| 4371 | */ |
| 4372 | if (ret == -2 || ret == -3) { |
| 4373 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4374 | WILL_LJMP(lua_error(L)); |
| 4375 | } |
| 4376 | |
| 4377 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4378 | if (ret == -1) { |
| 4379 | si_applet_cant_put(si); |
| 4380 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
| 4381 | } |
| 4382 | |
| 4383 | /* Headers sent, set the flag. */ |
| 4384 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4385 | return 0; |
| 4386 | } |
| 4387 | |
| 4388 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4389 | { |
| 4390 | struct chunk *tmp = get_trash_chunk(); |
| 4391 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4392 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4393 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4394 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4395 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4396 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4397 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4398 | int hdr_chunked = 0; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4399 | const char *reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4400 | |
| 4401 | if (reason == NULL) |
| 4402 | reason = get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4403 | |
| 4404 | /* Use the same http version than the request. */ |
| 4405 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4406 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4407 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4408 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4409 | |
| 4410 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4411 | lua_pushvalue(L, 0); |
| 4412 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4413 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4414 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4415 | WILL_LJMP(lua_error(L)); |
| 4416 | } |
| 4417 | |
| 4418 | /* Browse the list of headers. */ |
| 4419 | lua_pushnil(L); |
| 4420 | while(lua_next(L, -2) != 0) { |
| 4421 | |
| 4422 | /* We expect a string as -2. */ |
| 4423 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4424 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4425 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4426 | lua_typename(L, lua_type(L, -2))); |
| 4427 | WILL_LJMP(lua_error(L)); |
| 4428 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4429 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4430 | |
| 4431 | /* We expect an array as -1. */ |
| 4432 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4433 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4434 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4435 | name, |
| 4436 | lua_typename(L, lua_type(L, -1))); |
| 4437 | WILL_LJMP(lua_error(L)); |
| 4438 | } |
| 4439 | |
| 4440 | /* Browse the table who is on the top of the stack. */ |
| 4441 | lua_pushnil(L); |
| 4442 | while(lua_next(L, -2) != 0) { |
| 4443 | |
| 4444 | /* We expect a number as -2. */ |
| 4445 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4446 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4447 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4448 | name, |
| 4449 | lua_typename(L, lua_type(L, -2))); |
| 4450 | WILL_LJMP(lua_error(L)); |
| 4451 | } |
| 4452 | id = lua_tointeger(L, -2); |
| 4453 | |
| 4454 | /* We expect a string as -2. */ |
| 4455 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4456 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4457 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4458 | name, id, |
| 4459 | lua_typename(L, lua_type(L, -1))); |
| 4460 | WILL_LJMP(lua_error(L)); |
| 4461 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4462 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4463 | |
| 4464 | /* Catenate a new header. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4465 | if (tmp->len + name_len + 2 + value_len + 2 < tmp->size) { |
| 4466 | memcpy(tmp->str + tmp->len, name, name_len); |
| 4467 | tmp->len += name_len; |
| 4468 | tmp->str[tmp->len++] = ':'; |
| 4469 | tmp->str[tmp->len++] = ' '; |
| 4470 | |
| 4471 | memcpy(tmp->str + tmp->len, value, value_len); |
| 4472 | tmp->len += value_len; |
| 4473 | tmp->str[tmp->len++] = '\r'; |
| 4474 | tmp->str[tmp->len++] = '\n'; |
| 4475 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4476 | |
| 4477 | /* Protocol checks. */ |
| 4478 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4479 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4480 | * is done without control. If it contains a bad value, |
| 4481 | * the content-length remains negative so that we can |
| 4482 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4483 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4484 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4485 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4486 | |
| 4487 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4488 | if (name_len == 17 && value_len == 7 && |
| 4489 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4490 | strcasecmp("chunked", value) == 0) |
| 4491 | hdr_chunked = 1; |
| 4492 | |
| 4493 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4494 | lua_pop(L, 1); |
| 4495 | } |
| 4496 | |
| 4497 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4498 | lua_pop(L, 1); |
| 4499 | } |
| 4500 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4501 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4502 | * and the status code implies the presence of a message body, we must |
| 4503 | * announce a transfer encoding chunked. This is required by haproxy |
| 4504 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4505 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4506 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4507 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4508 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 4509 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4510 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4511 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4512 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4513 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4514 | } |
| 4515 | |
| 4516 | /* Finalize headers. */ |
| 4517 | chunk_appendf(tmp, "\r\n"); |
| 4518 | |
| 4519 | /* Remove the last entry and the array of headers */ |
| 4520 | lua_pop(L, 2); |
| 4521 | |
| 4522 | /* Push the headers block. */ |
| 4523 | lua_pushlstring(L, tmp->str, tmp->len); |
| 4524 | |
| 4525 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4526 | } |
| 4527 | |
| 4528 | /* |
| 4529 | * |
| 4530 | * |
| 4531 | * Class HTTP |
| 4532 | * |
| 4533 | * |
| 4534 | */ |
| 4535 | |
| 4536 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4537 | * a class stream, otherwise it throws an error. |
| 4538 | */ |
| 4539 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4540 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4541 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4542 | } |
| 4543 | |
| 4544 | /* This function creates and push in the stack a HTTP object |
| 4545 | * according with a current TXN. |
| 4546 | */ |
| 4547 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4548 | { |
| 4549 | struct hlua_txn *htxn; |
| 4550 | |
| 4551 | /* Check stack size. */ |
| 4552 | if (!lua_checkstack(L, 3)) |
| 4553 | return 0; |
| 4554 | |
| 4555 | /* Create the object: obj[0] = userdata. |
| 4556 | * Note that the base of the Converters object is the |
| 4557 | * same than the TXN object. |
| 4558 | */ |
| 4559 | lua_newtable(L); |
| 4560 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4561 | lua_rawseti(L, -2, 0); |
| 4562 | |
| 4563 | htxn->s = txn->s; |
| 4564 | htxn->p = txn->p; |
| 4565 | |
| 4566 | /* Pop a class stream metatable and affect it to the table. */ |
| 4567 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4568 | lua_setmetatable(L, -2); |
| 4569 | |
| 4570 | return 1; |
| 4571 | } |
| 4572 | |
| 4573 | /* This function creates ans returns an array of HTTP headers. |
| 4574 | * This function does not fails. It is used as wrapper with the |
| 4575 | * 2 following functions. |
| 4576 | */ |
| 4577 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4578 | { |
| 4579 | const char *cur_ptr, *cur_next, *p; |
| 4580 | int old_idx, cur_idx; |
| 4581 | struct hdr_idx_elem *cur_hdr; |
| 4582 | const char *hn, *hv; |
| 4583 | int hnl, hvl; |
| 4584 | int type; |
| 4585 | const char *in; |
| 4586 | char *out; |
| 4587 | int len; |
| 4588 | |
| 4589 | /* Create the table. */ |
| 4590 | lua_newtable(L); |
| 4591 | |
| 4592 | if (!htxn->s->txn) |
| 4593 | return 1; |
| 4594 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4595 | /* Check if a valid response is parsed */ |
| 4596 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4597 | return 1; |
| 4598 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4599 | /* Build array of headers. */ |
| 4600 | old_idx = 0; |
| 4601 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 4602 | |
| 4603 | while (1) { |
| 4604 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4605 | if (!cur_idx) |
| 4606 | break; |
| 4607 | old_idx = cur_idx; |
| 4608 | |
| 4609 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4610 | cur_ptr = cur_next; |
| 4611 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4612 | |
| 4613 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4614 | * and the next header starts at cur_next. We'll check |
| 4615 | * this header in the list as well as against the default |
| 4616 | * rule. |
| 4617 | */ |
| 4618 | |
| 4619 | /* look for ': *'. */ |
| 4620 | hn = cur_ptr; |
| 4621 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4622 | if (p >= cur_ptr+cur_hdr->len) |
| 4623 | continue; |
| 4624 | hnl = p - hn; |
| 4625 | p++; |
| 4626 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4627 | p++; |
| 4628 | if (p >= cur_ptr+cur_hdr->len) |
| 4629 | continue; |
| 4630 | hv = p; |
| 4631 | hvl = cur_ptr+cur_hdr->len-p; |
| 4632 | |
| 4633 | /* Lowercase the key. Don't check the size of trash, it have |
| 4634 | * the size of one buffer and the input data contains in one |
| 4635 | * buffer. |
| 4636 | */ |
| 4637 | out = trash.str; |
| 4638 | for (in=hn; in<hn+hnl; in++, out++) |
| 4639 | *out = tolower(*in); |
| 4640 | *out = '\0'; |
| 4641 | |
| 4642 | /* Check for existing entry: |
| 4643 | * assume that the table is on the top of the stack, and |
| 4644 | * push the key in the stack, the function lua_gettable() |
| 4645 | * perform the lookup. |
| 4646 | */ |
| 4647 | lua_pushlstring(L, trash.str, hnl); |
| 4648 | lua_gettable(L, -2); |
| 4649 | type = lua_type(L, -1); |
| 4650 | |
| 4651 | switch (type) { |
| 4652 | case LUA_TNIL: |
| 4653 | /* Table not found, create it. */ |
| 4654 | lua_pop(L, 1); /* remove the nil value. */ |
| 4655 | lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */ |
| 4656 | lua_newtable(L); /* create and push empty table. */ |
| 4657 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4658 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4659 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4660 | break; |
| 4661 | |
| 4662 | case LUA_TTABLE: |
| 4663 | /* Entry found: push the value in the table. */ |
| 4664 | len = lua_rawlen(L, -1); |
| 4665 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4666 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4667 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4668 | break; |
| 4669 | |
| 4670 | default: |
| 4671 | /* Other cases are errors. */ |
| 4672 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4673 | WILL_LJMP(lua_error(L)); |
| 4674 | } |
| 4675 | } |
| 4676 | |
| 4677 | return 1; |
| 4678 | } |
| 4679 | |
| 4680 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4681 | { |
| 4682 | struct hlua_txn *htxn; |
| 4683 | |
| 4684 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4685 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4686 | |
| 4687 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4688 | } |
| 4689 | |
| 4690 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4691 | { |
| 4692 | struct hlua_txn *htxn; |
| 4693 | |
| 4694 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4695 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4696 | |
| 4697 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4698 | } |
| 4699 | |
| 4700 | /* This function replace full header, or just a value in |
| 4701 | * the request or in the response. It is a wrapper fir the |
| 4702 | * 4 following functions. |
| 4703 | */ |
| 4704 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4705 | struct http_msg *msg, int action) |
| 4706 | { |
| 4707 | size_t name_len; |
| 4708 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4709 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4710 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4711 | struct my_regex re; |
| 4712 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4713 | /* Check if a valid response is parsed */ |
| 4714 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4715 | return 0; |
| 4716 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4717 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4718 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4719 | |
| 4720 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4721 | regex_free(&re); |
| 4722 | return 0; |
| 4723 | } |
| 4724 | |
| 4725 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4726 | { |
| 4727 | struct hlua_txn *htxn; |
| 4728 | |
| 4729 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4730 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4731 | |
| 4732 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4733 | } |
| 4734 | |
| 4735 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4736 | { |
| 4737 | struct hlua_txn *htxn; |
| 4738 | |
| 4739 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4740 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4741 | |
| 4742 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4743 | } |
| 4744 | |
| 4745 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4746 | { |
| 4747 | struct hlua_txn *htxn; |
| 4748 | |
| 4749 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4750 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4751 | |
| 4752 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4753 | } |
| 4754 | |
| 4755 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4756 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4757 | struct hlua_txn *htxn; |
| 4758 | |
| 4759 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4760 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4761 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4762 | 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] | 4763 | } |
| 4764 | |
| 4765 | /* This function deletes all the occurences of an header. |
| 4766 | * It is a wrapper for the 2 following functions. |
| 4767 | */ |
| 4768 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4769 | { |
| 4770 | size_t len; |
| 4771 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4772 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4773 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4774 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4775 | /* Check if a valid response is parsed */ |
| 4776 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4777 | return 0; |
| 4778 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4779 | ctx.idx = 0; |
| 4780 | while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) |
| 4781 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4782 | return 0; |
| 4783 | } |
| 4784 | |
| 4785 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4786 | { |
| 4787 | struct hlua_txn *htxn; |
| 4788 | |
| 4789 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4790 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4791 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4792 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4793 | } |
| 4794 | |
| 4795 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4796 | { |
| 4797 | struct hlua_txn *htxn; |
| 4798 | |
| 4799 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4800 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4801 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4802 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | /* This function adds an header. It is a wrapper used by |
| 4806 | * the 2 following functions. |
| 4807 | */ |
| 4808 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4809 | { |
| 4810 | size_t name_len; |
| 4811 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4812 | size_t value_len; |
| 4813 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4814 | char *p; |
| 4815 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4816 | /* Check if a valid message is parsed */ |
| 4817 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4818 | return 0; |
| 4819 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4820 | /* Check length. */ |
| 4821 | trash.len = value_len + name_len + 2; |
| 4822 | if (trash.len > trash.size) |
| 4823 | return 0; |
| 4824 | |
| 4825 | /* Creates the header string. */ |
| 4826 | p = trash.str; |
| 4827 | memcpy(p, name, name_len); |
| 4828 | p += name_len; |
| 4829 | *p = ':'; |
| 4830 | p++; |
| 4831 | *p = ' '; |
| 4832 | p++; |
| 4833 | memcpy(p, value, value_len); |
| 4834 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4835 | 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] | 4836 | trash.str, trash.len) != 0); |
| 4837 | |
| 4838 | return 0; |
| 4839 | } |
| 4840 | |
| 4841 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4842 | { |
| 4843 | struct hlua_txn *htxn; |
| 4844 | |
| 4845 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4846 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4847 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 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 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4852 | { |
| 4853 | struct hlua_txn *htxn; |
| 4854 | |
| 4855 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4856 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4857 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4858 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4859 | } |
| 4860 | |
| 4861 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4862 | { |
| 4863 | struct hlua_txn *htxn; |
| 4864 | |
| 4865 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4866 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4867 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4868 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4869 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4870 | } |
| 4871 | |
| 4872 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4873 | { |
| 4874 | struct hlua_txn *htxn; |
| 4875 | |
| 4876 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4877 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4878 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4879 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4880 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4881 | } |
| 4882 | |
| 4883 | /* This function set the method. */ |
| 4884 | static int hlua_http_req_set_meth(lua_State *L) |
| 4885 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4886 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4887 | size_t name_len; |
| 4888 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4889 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4890 | /* Check if a valid request is parsed */ |
| 4891 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4892 | lua_pushboolean(L, 0); |
| 4893 | return 1; |
| 4894 | } |
| 4895 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4896 | 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] | 4897 | return 1; |
| 4898 | } |
| 4899 | |
| 4900 | /* This function set the method. */ |
| 4901 | static int hlua_http_req_set_path(lua_State *L) |
| 4902 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4903 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4904 | size_t name_len; |
| 4905 | 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] | 4906 | |
| 4907 | /* Check if a valid request is parsed */ |
| 4908 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4909 | lua_pushboolean(L, 0); |
| 4910 | return 1; |
| 4911 | } |
| 4912 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4913 | 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] | 4914 | return 1; |
| 4915 | } |
| 4916 | |
| 4917 | /* This function set the query-string. */ |
| 4918 | static int hlua_http_req_set_query(lua_State *L) |
| 4919 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4920 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4921 | size_t name_len; |
| 4922 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4923 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4924 | /* Check if a valid request is parsed */ |
| 4925 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4926 | lua_pushboolean(L, 0); |
| 4927 | return 1; |
| 4928 | } |
| 4929 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4930 | /* Check length. */ |
| 4931 | if (name_len > trash.size - 1) { |
| 4932 | lua_pushboolean(L, 0); |
| 4933 | return 1; |
| 4934 | } |
| 4935 | |
| 4936 | /* Add the mark question as prefix. */ |
| 4937 | chunk_reset(&trash); |
| 4938 | trash.str[trash.len++] = '?'; |
| 4939 | memcpy(trash.str + trash.len, name, name_len); |
| 4940 | trash.len += name_len; |
| 4941 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4942 | 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] | 4943 | return 1; |
| 4944 | } |
| 4945 | |
| 4946 | /* This function set the uri. */ |
| 4947 | static int hlua_http_req_set_uri(lua_State *L) |
| 4948 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4949 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4950 | size_t name_len; |
| 4951 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4952 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4953 | /* Check if a valid request is parsed */ |
| 4954 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4955 | lua_pushboolean(L, 0); |
| 4956 | return 1; |
| 4957 | } |
| 4958 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4959 | 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] | 4960 | return 1; |
| 4961 | } |
| 4962 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4963 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4964 | static int hlua_http_res_set_status(lua_State *L) |
| 4965 | { |
| 4966 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4967 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4968 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4969 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4970 | /* Check if a valid response is parsed */ |
| 4971 | if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4972 | return 0; |
| 4973 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4974 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4975 | return 0; |
| 4976 | } |
| 4977 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4978 | /* |
| 4979 | * |
| 4980 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4981 | * Class TXN |
| 4982 | * |
| 4983 | * |
| 4984 | */ |
| 4985 | |
| 4986 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4987 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4988 | */ |
| 4989 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 4990 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4991 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4992 | } |
| 4993 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4994 | __LJMP static int hlua_set_var(lua_State *L) |
| 4995 | { |
| 4996 | struct hlua_txn *htxn; |
| 4997 | const char *name; |
| 4998 | size_t len; |
| 4999 | struct sample smp; |
| 5000 | |
| 5001 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5002 | |
| 5003 | /* It is useles to retrieve the stream, but this function |
| 5004 | * runs only in a stream context. |
| 5005 | */ |
| 5006 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5007 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5008 | |
| 5009 | /* Converts the third argument in a sample. */ |
| 5010 | hlua_lua2smp(L, 3, &smp); |
| 5011 | |
| 5012 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5013 | 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] | 5014 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5015 | return 0; |
| 5016 | } |
| 5017 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5018 | __LJMP static int hlua_unset_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, "unset_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 | |
| 5033 | /* Unset the variable. */ |
| 5034 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5035 | vars_unset_by_name(name, len, &smp); |
| 5036 | return 0; |
| 5037 | } |
| 5038 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5039 | __LJMP static int hlua_get_var(lua_State *L) |
| 5040 | { |
| 5041 | struct hlua_txn *htxn; |
| 5042 | const char *name; |
| 5043 | size_t len; |
| 5044 | struct sample smp; |
| 5045 | |
| 5046 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5047 | |
| 5048 | /* It is useles to retrieve the stream, but this function |
| 5049 | * runs only in a stream context. |
| 5050 | */ |
| 5051 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5052 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5053 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5054 | 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] | 5055 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5056 | lua_pushnil(L); |
| 5057 | return 1; |
| 5058 | } |
| 5059 | |
| 5060 | return hlua_smp2lua(L, &smp); |
| 5061 | } |
| 5062 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5063 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5064 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5065 | struct hlua *hlua; |
| 5066 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5067 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5068 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5069 | /* It is useles to retrieve the stream, but this function |
| 5070 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5071 | */ |
| 5072 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5073 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5074 | |
| 5075 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5076 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5077 | |
| 5078 | /* Get and store new value. */ |
| 5079 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5080 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5081 | |
| 5082 | return 0; |
| 5083 | } |
| 5084 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5085 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5086 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5087 | struct hlua *hlua; |
| 5088 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5089 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5090 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5091 | /* It is useles to retrieve the stream, but this function |
| 5092 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5093 | */ |
| 5094 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5095 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5096 | |
| 5097 | /* Push configuration index in the stack. */ |
| 5098 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5099 | |
| 5100 | return 1; |
| 5101 | } |
| 5102 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5103 | /* Create stack entry containing a class TXN. This function |
| 5104 | * return 0 if the stack does not contains free slots, |
| 5105 | * otherwise it returns 1. |
| 5106 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5107 | 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] | 5108 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5109 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5110 | |
| 5111 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5112 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5113 | return 0; |
| 5114 | |
| 5115 | /* NOTE: The allocation never fails. The failure |
| 5116 | * throw an error, and the function never returns. |
| 5117 | * if the throw is not avalaible, the process is aborted. |
| 5118 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5119 | /* Create the object: obj[0] = userdata. */ |
| 5120 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5121 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5122 | lua_rawseti(L, -2, 0); |
| 5123 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5124 | htxn->s = s; |
| 5125 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5126 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5127 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5128 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5129 | /* Create the "f" field that contains a list of fetches. */ |
| 5130 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5131 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5132 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5133 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5134 | |
| 5135 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5136 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5137 | 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] | 5138 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5139 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5140 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5141 | /* Create the "c" field that contains a list of converters. */ |
| 5142 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5143 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5144 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5145 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5146 | |
| 5147 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5148 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5149 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5150 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5151 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5152 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5153 | /* Create the "req" field that contains the request channel object. */ |
| 5154 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5155 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5156 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5157 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5158 | |
| 5159 | /* Create the "res" field that contains the response channel object. */ |
| 5160 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5161 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5162 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5163 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5164 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5165 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5166 | lua_pushstring(L, "http"); |
| 5167 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5168 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5169 | return 0; |
| 5170 | } |
| 5171 | else |
| 5172 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5173 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5174 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5175 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5176 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5177 | lua_setmetatable(L, -2); |
| 5178 | |
| 5179 | return 1; |
| 5180 | } |
| 5181 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5182 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5183 | { |
| 5184 | const char *msg; |
| 5185 | struct hlua_txn *htxn; |
| 5186 | |
| 5187 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5188 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5189 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5190 | |
| 5191 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5192 | return 0; |
| 5193 | } |
| 5194 | |
| 5195 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5196 | { |
| 5197 | int level; |
| 5198 | const char *msg; |
| 5199 | struct hlua_txn *htxn; |
| 5200 | |
| 5201 | MAY_LJMP(check_args(L, 3, "log")); |
| 5202 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5203 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5204 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5205 | |
| 5206 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5207 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5208 | |
| 5209 | hlua_sendlog(htxn->s->be, level, msg); |
| 5210 | return 0; |
| 5211 | } |
| 5212 | |
| 5213 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5214 | { |
| 5215 | const char *msg; |
| 5216 | struct hlua_txn *htxn; |
| 5217 | |
| 5218 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5219 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5220 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5221 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5222 | return 0; |
| 5223 | } |
| 5224 | |
| 5225 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5226 | { |
| 5227 | const char *msg; |
| 5228 | struct hlua_txn *htxn; |
| 5229 | |
| 5230 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5231 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5232 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5233 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5234 | return 0; |
| 5235 | } |
| 5236 | |
| 5237 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5238 | { |
| 5239 | const char *msg; |
| 5240 | struct hlua_txn *htxn; |
| 5241 | |
| 5242 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5243 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5244 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5245 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5246 | return 0; |
| 5247 | } |
| 5248 | |
| 5249 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5250 | { |
| 5251 | const char *msg; |
| 5252 | struct hlua_txn *htxn; |
| 5253 | |
| 5254 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5255 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5256 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5257 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5258 | return 0; |
| 5259 | } |
| 5260 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5261 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5262 | { |
| 5263 | struct hlua_txn *htxn; |
| 5264 | int ll; |
| 5265 | |
| 5266 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5267 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5268 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5269 | |
| 5270 | if (ll < 0 || ll > 7) |
| 5271 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5272 | |
| 5273 | htxn->s->logs.level = ll; |
| 5274 | return 0; |
| 5275 | } |
| 5276 | |
| 5277 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5278 | { |
| 5279 | struct hlua_txn *htxn; |
| 5280 | struct connection *cli_conn; |
| 5281 | int tos; |
| 5282 | |
| 5283 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5284 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5285 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5286 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5287 | 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] | 5288 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5289 | |
| 5290 | return 0; |
| 5291 | } |
| 5292 | |
| 5293 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5294 | { |
| 5295 | #ifdef SO_MARK |
| 5296 | struct hlua_txn *htxn; |
| 5297 | struct connection *cli_conn; |
| 5298 | int mark; |
| 5299 | |
| 5300 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5301 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5302 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5303 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5304 | 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] | 5305 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5306 | #endif |
| 5307 | return 0; |
| 5308 | } |
| 5309 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5310 | /* This function is an Lua binding that send pending data |
| 5311 | * to the client, and close the stream interface. |
| 5312 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5313 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5314 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5315 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5316 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5317 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5318 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5319 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5320 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5321 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5322 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5323 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5324 | * session, so we just end the execution of the current |
| 5325 | * lua code. |
| 5326 | */ |
| 5327 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5328 | WILL_LJMP(hlua_done(L)); |
| 5329 | return 0; |
| 5330 | } |
| 5331 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5332 | ic = &htxn->s->req; |
| 5333 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5334 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5335 | if (htxn->s->txn) { |
| 5336 | /* HTTP mode, let's stay in sync with the stream */ |
| 5337 | bi_fast_delete(ic->buf, htxn->s->txn->req.sov); |
| 5338 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 5339 | htxn->s->txn->req.sov = 0; |
| 5340 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 5341 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 5342 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5343 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 5344 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5345 | /* Note that if we want to support keep-alive, we need |
| 5346 | * to bypass the close/shutr_now calls below, but that |
| 5347 | * may only be done if the HTTP request was already |
| 5348 | * processed and the connection header is known (ie |
| 5349 | * not during TCP rules). |
| 5350 | */ |
| 5351 | } |
| 5352 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5353 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5354 | channel_abort(ic); |
| 5355 | channel_auto_close(ic); |
| 5356 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5357 | |
| 5358 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5359 | channel_auto_read(oc); |
| 5360 | channel_auto_close(oc); |
| 5361 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5362 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 5363 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5364 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5365 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5366 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5367 | return 0; |
| 5368 | } |
| 5369 | |
| 5370 | __LJMP static int hlua_log(lua_State *L) |
| 5371 | { |
| 5372 | int level; |
| 5373 | const char *msg; |
| 5374 | |
| 5375 | MAY_LJMP(check_args(L, 2, "log")); |
| 5376 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5377 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5378 | |
| 5379 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5380 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5381 | |
| 5382 | hlua_sendlog(NULL, level, msg); |
| 5383 | return 0; |
| 5384 | } |
| 5385 | |
| 5386 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5387 | { |
| 5388 | const char *msg; |
| 5389 | |
| 5390 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5391 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5392 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5393 | return 0; |
| 5394 | } |
| 5395 | |
| 5396 | __LJMP static int hlua_log_info(lua_State *L) |
| 5397 | { |
| 5398 | const char *msg; |
| 5399 | |
| 5400 | MAY_LJMP(check_args(L, 1, "info")); |
| 5401 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5402 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5403 | return 0; |
| 5404 | } |
| 5405 | |
| 5406 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5407 | { |
| 5408 | const char *msg; |
| 5409 | |
| 5410 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5411 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5412 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5413 | return 0; |
| 5414 | } |
| 5415 | |
| 5416 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5417 | { |
| 5418 | const char *msg; |
| 5419 | |
| 5420 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5421 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5422 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5423 | return 0; |
| 5424 | } |
| 5425 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5426 | __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] | 5427 | { |
| 5428 | int wakeup_ms = lua_tointeger(L, -1); |
| 5429 | if (now_ms < wakeup_ms) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5430 | 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] | 5431 | return 0; |
| 5432 | } |
| 5433 | |
| 5434 | __LJMP static int hlua_sleep(lua_State *L) |
| 5435 | { |
| 5436 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5437 | unsigned int 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 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5440 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5441 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5442 | wakeup_ms = tick_add(now_ms, delay); |
| 5443 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5444 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5445 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5446 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5447 | } |
| 5448 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5449 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5450 | { |
| 5451 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5452 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5453 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5454 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5455 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5456 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5457 | wakeup_ms = tick_add(now_ms, delay); |
| 5458 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5459 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5460 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5461 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5462 | } |
| 5463 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5464 | /* This functionis an LUA binding. it permits to give back |
| 5465 | * the hand at the HAProxy scheduler. It is used when the |
| 5466 | * LUA processing consumes a lot of time. |
| 5467 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5468 | __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] | 5469 | { |
| 5470 | return 0; |
| 5471 | } |
| 5472 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5473 | __LJMP static int hlua_yield(lua_State *L) |
| 5474 | { |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5475 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 5476 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5477 | } |
| 5478 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5479 | /* This function change the nice of the currently executed |
| 5480 | * task. It is used set low or high priority at the current |
| 5481 | * task. |
| 5482 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5483 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5484 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5485 | struct hlua *hlua; |
| 5486 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5487 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5488 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5489 | hlua = hlua_gethlua(L); |
| 5490 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5491 | |
| 5492 | /* If he task is not set, I'm in a start mode. */ |
| 5493 | if (!hlua || !hlua->task) |
| 5494 | return 0; |
| 5495 | |
| 5496 | if (nice < -1024) |
| 5497 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5498 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5499 | nice = 1024; |
| 5500 | |
| 5501 | hlua->task->nice = nice; |
| 5502 | return 0; |
| 5503 | } |
| 5504 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5505 | /* This function is used as a calback of a task. It is called by the |
| 5506 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5507 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5508 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5509 | * |
| 5510 | * Task wrapper are longjmp safe because the only one Lua code |
| 5511 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5512 | */ |
| 5513 | static struct task *hlua_process_task(struct task *task) |
| 5514 | { |
| 5515 | struct hlua *hlua = task->context; |
| 5516 | enum hlua_exec status; |
| 5517 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5518 | /* If it is the first call to the task, we must initialize the |
| 5519 | * execution timeouts. |
| 5520 | */ |
| 5521 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5522 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5523 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5524 | /* Execute the Lua code. */ |
| 5525 | status = hlua_ctx_resume(hlua, 1); |
| 5526 | |
| 5527 | switch (status) { |
| 5528 | /* finished or yield */ |
| 5529 | case HLUA_E_OK: |
| 5530 | hlua_ctx_destroy(hlua); |
| 5531 | task_delete(task); |
| 5532 | task_free(task); |
| 5533 | break; |
| 5534 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5535 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 5536 | notification_gc(&hlua->com); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5537 | if (hlua->wake_time != TICK_ETERNITY) |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5538 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5539 | break; |
| 5540 | |
| 5541 | /* finished with error. */ |
| 5542 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5543 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5544 | hlua_ctx_destroy(hlua); |
| 5545 | task_delete(task); |
| 5546 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5547 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5548 | break; |
| 5549 | |
| 5550 | case HLUA_E_ERR: |
| 5551 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5552 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5553 | hlua_ctx_destroy(hlua); |
| 5554 | task_delete(task); |
| 5555 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5556 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5557 | break; |
| 5558 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5559 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5560 | } |
| 5561 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5562 | /* This function is an LUA binding that register LUA function to be |
| 5563 | * executed after the HAProxy configuration parsing and before the |
| 5564 | * HAProxy scheduler starts. This function expect only one LUA |
| 5565 | * argument that is a function. This function returns nothing, but |
| 5566 | * throws if an error is encountered. |
| 5567 | */ |
| 5568 | __LJMP static int hlua_register_init(lua_State *L) |
| 5569 | { |
| 5570 | struct hlua_init_function *init; |
| 5571 | int ref; |
| 5572 | |
| 5573 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5574 | |
| 5575 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5576 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5577 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5578 | if (!init) |
| 5579 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5580 | |
| 5581 | init->function_ref = ref; |
| 5582 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5583 | return 0; |
| 5584 | } |
| 5585 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5586 | /* This functio is an LUA binding. It permits to register a task |
| 5587 | * executed in parallel of the main HAroxy activity. The task is |
| 5588 | * created and it is set in the HAProxy scheduler. It can be called |
| 5589 | * from the "init" section, "post init" or during the runtime. |
| 5590 | * |
| 5591 | * Lua prototype: |
| 5592 | * |
| 5593 | * <none> core.register_task(<function>) |
| 5594 | */ |
| 5595 | static int hlua_register_task(lua_State *L) |
| 5596 | { |
| 5597 | struct hlua *hlua; |
| 5598 | struct task *task; |
| 5599 | int ref; |
| 5600 | |
| 5601 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5602 | |
| 5603 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5604 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5605 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5606 | if (!hlua) |
| 5607 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5608 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 5609 | task = task_new(MAX_THREADS_MASK); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5610 | task->context = hlua; |
| 5611 | task->process = hlua_process_task; |
| 5612 | |
| 5613 | if (!hlua_ctx_init(hlua, task)) |
| 5614 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5615 | |
| 5616 | /* Restore the function in the stack. */ |
| 5617 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5618 | hlua->nargs = 0; |
| 5619 | |
| 5620 | /* Schedule task. */ |
| 5621 | task_schedule(task, now_ms); |
| 5622 | |
| 5623 | return 0; |
| 5624 | } |
| 5625 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5626 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5627 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5628 | * resume converters. |
| 5629 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5630 | 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] | 5631 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5632 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5633 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5634 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5635 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5636 | if (!stream) |
| 5637 | return 0; |
| 5638 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5639 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5640 | * Lua context can be not initialized. This behavior |
| 5641 | * permits to save performances because a systematic |
| 5642 | * Lua initialization cause 5% performances loss. |
| 5643 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5644 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5645 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5646 | if (!stream->hlua) { |
| 5647 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5648 | return 0; |
| 5649 | } |
| 5650 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5651 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5652 | return 0; |
| 5653 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5654 | } |
| 5655 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5656 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5657 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5658 | |
| 5659 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5660 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5661 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5662 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5663 | else |
| 5664 | error = "critical error"; |
| 5665 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5666 | return 0; |
| 5667 | } |
| 5668 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5669 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5670 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5671 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5672 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5673 | return 0; |
| 5674 | } |
| 5675 | |
| 5676 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5677 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5678 | |
| 5679 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5680 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5681 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5682 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5683 | return 0; |
| 5684 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5685 | hlua_smp2lua(stream->hlua->T, smp); |
| 5686 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5687 | |
| 5688 | /* push keywords in the stack. */ |
| 5689 | if (arg_p) { |
| 5690 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5691 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5692 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5693 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5694 | return 0; |
| 5695 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5696 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5697 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5698 | } |
| 5699 | } |
| 5700 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5701 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5702 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5703 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5704 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5705 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5706 | } |
| 5707 | |
| 5708 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5709 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5710 | /* finished. */ |
| 5711 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5712 | /* If the stack is empty, the function fails. */ |
| 5713 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5714 | return 0; |
| 5715 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5716 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5717 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5718 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5719 | return 1; |
| 5720 | |
| 5721 | /* yield. */ |
| 5722 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5723 | 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] | 5724 | return 0; |
| 5725 | |
| 5726 | /* finished with error. */ |
| 5727 | case HLUA_E_ERRMSG: |
| 5728 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5729 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5730 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5731 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5732 | return 0; |
| 5733 | |
| 5734 | case HLUA_E_ERR: |
| 5735 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5736 | 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] | 5737 | |
| 5738 | default: |
| 5739 | return 0; |
| 5740 | } |
| 5741 | } |
| 5742 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5743 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5744 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5745 | * resume sample-fetches. This function will be called by the sample |
| 5746 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5747 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5748 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5749 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5750 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5751 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5752 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5753 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5754 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5755 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5756 | if (!stream) |
| 5757 | return 0; |
| 5758 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5759 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5760 | * Lua context can be not initialized. This behavior |
| 5761 | * permits to save performances because a systematic |
| 5762 | * Lua initialization cause 5% performances loss. |
| 5763 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5764 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5765 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5766 | if (!stream->hlua) { |
| 5767 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5768 | return 0; |
| 5769 | } |
| 5770 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5771 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5772 | return 0; |
| 5773 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5774 | } |
| 5775 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5776 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5777 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5778 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5779 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5780 | |
| 5781 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5782 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5783 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5784 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5785 | else |
| 5786 | error = "critical error"; |
| 5787 | 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] | 5788 | return 0; |
| 5789 | } |
| 5790 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5791 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5792 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5793 | 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] | 5794 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5795 | return 0; |
| 5796 | } |
| 5797 | |
| 5798 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5799 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5800 | |
| 5801 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5802 | 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] | 5803 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5804 | 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] | 5805 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5806 | return 0; |
| 5807 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5808 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5809 | |
| 5810 | /* push keywords in the stack. */ |
| 5811 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5812 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5813 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5814 | 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] | 5815 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5816 | return 0; |
| 5817 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5818 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5819 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5820 | } |
| 5821 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5822 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5823 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5824 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5825 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5826 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5827 | } |
| 5828 | |
| 5829 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5830 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5831 | /* finished. */ |
| 5832 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5833 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5834 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5835 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5836 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5837 | /* If the stack is empty, the function fails. */ |
| 5838 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5839 | return 0; |
| 5840 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5841 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5842 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5843 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5844 | |
| 5845 | /* Set the end of execution flag. */ |
| 5846 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5847 | return 1; |
| 5848 | |
| 5849 | /* yield. */ |
| 5850 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5851 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5852 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5853 | 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] | 5854 | return 0; |
| 5855 | |
| 5856 | /* finished with error. */ |
| 5857 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5858 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5859 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5860 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5861 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5862 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5863 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5864 | return 0; |
| 5865 | |
| 5866 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5867 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5868 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5869 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5870 | 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] | 5871 | |
| 5872 | default: |
| 5873 | return 0; |
| 5874 | } |
| 5875 | } |
| 5876 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5877 | /* This function is an LUA binding used for registering |
| 5878 | * "sample-conv" functions. It expects a converter name used |
| 5879 | * in the haproxy configuration file, and an LUA function. |
| 5880 | */ |
| 5881 | __LJMP static int hlua_register_converters(lua_State *L) |
| 5882 | { |
| 5883 | struct sample_conv_kw_list *sck; |
| 5884 | const char *name; |
| 5885 | int ref; |
| 5886 | int len; |
| 5887 | struct hlua_function *fcn; |
| 5888 | |
| 5889 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 5890 | |
| 5891 | /* First argument : converter name. */ |
| 5892 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5893 | |
| 5894 | /* Second argument : lua function. */ |
| 5895 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5896 | |
| 5897 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5898 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5899 | if (!sck) |
| 5900 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5901 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5902 | if (!fcn) |
| 5903 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5904 | |
| 5905 | /* Fill fcn. */ |
| 5906 | fcn->name = strdup(name); |
| 5907 | if (!fcn->name) |
| 5908 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5909 | fcn->function_ref = ref; |
| 5910 | |
| 5911 | /* List head */ |
| 5912 | sck->list.n = sck->list.p = NULL; |
| 5913 | |
| 5914 | /* converter keyword. */ |
| 5915 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5916 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5917 | if (!sck->kw[0].kw) |
| 5918 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5919 | |
| 5920 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 5921 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5922 | 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] | 5923 | sck->kw[0].val_args = NULL; |
| 5924 | sck->kw[0].in_type = SMP_T_STR; |
| 5925 | sck->kw[0].out_type = SMP_T_STR; |
| 5926 | sck->kw[0].private = fcn; |
| 5927 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5928 | /* Register this new converter */ |
| 5929 | sample_register_convs(sck); |
| 5930 | |
| 5931 | return 0; |
| 5932 | } |
| 5933 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5934 | /* This fucntion is an LUA binding used for registering |
| 5935 | * "sample-fetch" functions. It expects a converter name used |
| 5936 | * in the haproxy configuration file, and an LUA function. |
| 5937 | */ |
| 5938 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 5939 | { |
| 5940 | const char *name; |
| 5941 | int ref; |
| 5942 | int len; |
| 5943 | struct sample_fetch_kw_list *sfk; |
| 5944 | struct hlua_function *fcn; |
| 5945 | |
| 5946 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 5947 | |
| 5948 | /* First argument : sample-fetch name. */ |
| 5949 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5950 | |
| 5951 | /* Second argument : lua function. */ |
| 5952 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5953 | |
| 5954 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5955 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5956 | if (!sfk) |
| 5957 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5958 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5959 | if (!fcn) |
| 5960 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5961 | |
| 5962 | /* Fill fcn. */ |
| 5963 | fcn->name = strdup(name); |
| 5964 | if (!fcn->name) |
| 5965 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5966 | fcn->function_ref = ref; |
| 5967 | |
| 5968 | /* List head */ |
| 5969 | sfk->list.n = sfk->list.p = NULL; |
| 5970 | |
| 5971 | /* sample-fetch keyword. */ |
| 5972 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5973 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5974 | if (!sfk->kw[0].kw) |
| 5975 | return luaL_error(L, "lua out of memory error."); |
| 5976 | |
| 5977 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 5978 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5979 | 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] | 5980 | sfk->kw[0].val_args = NULL; |
| 5981 | sfk->kw[0].out_type = SMP_T_STR; |
| 5982 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 5983 | sfk->kw[0].val = 0; |
| 5984 | sfk->kw[0].private = fcn; |
| 5985 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5986 | /* Register this new fetch. */ |
| 5987 | sample_register_fetches(sfk); |
| 5988 | |
| 5989 | return 0; |
| 5990 | } |
| 5991 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5992 | /* This function is a wrapper to execute each LUA function declared |
| 5993 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5994 | * return ACT_RET_CONT if the processing is finished (with or without |
| 5995 | * error) and return ACT_RET_YIELD if the function must be called again |
| 5996 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5997 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5998 | 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] | 5999 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6000 | { |
| 6001 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6002 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6003 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6004 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6005 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6006 | |
| 6007 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 6008 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 6009 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 6010 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 6011 | 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] | 6012 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6013 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6014 | return ACT_RET_CONT; |
| 6015 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6016 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6017 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6018 | * Lua context can be not initialized. This behavior |
| 6019 | * permits to save performances because a systematic |
| 6020 | * Lua initialization cause 5% performances loss. |
| 6021 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6022 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6023 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6024 | if (!s->hlua) { |
| 6025 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6026 | rule->arg.hlua_rule->fcn.name); |
| 6027 | return ACT_RET_CONT; |
| 6028 | } |
| 6029 | if (!hlua_ctx_init(s->hlua, s->task)) { |
| 6030 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6031 | rule->arg.hlua_rule->fcn.name); |
| 6032 | return ACT_RET_CONT; |
| 6033 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6034 | } |
| 6035 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6036 | consistency_set(s, dir, &s->hlua->cons); |
| 6037 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6038 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6039 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6040 | |
| 6041 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6042 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6043 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6044 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6045 | else |
| 6046 | error = "critical error"; |
| 6047 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6048 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6049 | return ACT_RET_CONT; |
| 6050 | } |
| 6051 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6052 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6053 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6054 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6055 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6056 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6057 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6058 | } |
| 6059 | |
| 6060 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6061 | 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] | 6062 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6063 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6064 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6065 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6066 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6067 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6068 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6069 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6070 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6071 | |
| 6072 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6073 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6074 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6075 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6076 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6077 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6078 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6079 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6080 | lua_pushstring(s->hlua->T, *arg); |
| 6081 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6082 | } |
| 6083 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6084 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6085 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6086 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6087 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6088 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6089 | } |
| 6090 | |
| 6091 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6092 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6093 | /* finished. */ |
| 6094 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6095 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6096 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6097 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6098 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6099 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 6100 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6101 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6102 | |
| 6103 | /* yield. */ |
| 6104 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6105 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6106 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6107 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6108 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6109 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6110 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6111 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6112 | /* Some actions can be wake up when a "write" event |
| 6113 | * is detected on a response channel. This is useful |
| 6114 | * only for actions targetted on the requests. |
| 6115 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6116 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6117 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6118 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6119 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6120 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6121 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6122 | s->req.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6123 | /* We can quit the fcuntion without consistency check |
| 6124 | * because HAProxy is not able to manipulate data, it |
| 6125 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6126 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6127 | |
| 6128 | /* finished with error. */ |
| 6129 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6130 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6131 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6132 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6133 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6134 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6135 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6136 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6137 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6138 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6139 | |
| 6140 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6141 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6142 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6143 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6144 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6145 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6146 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6147 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6148 | |
| 6149 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6150 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6151 | } |
| 6152 | } |
| 6153 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6154 | struct task *hlua_applet_wakeup(struct task *t) |
| 6155 | { |
| 6156 | struct appctx *ctx = t->context; |
| 6157 | struct stream_interface *si = ctx->owner; |
| 6158 | |
| 6159 | /* If the applet is wake up without any expected work, the sheduler |
| 6160 | * remove it from the run queue. This flag indicate that the applet |
| 6161 | * is waiting for write. If the buffer is full, the main processing |
| 6162 | * will send some data and after call the applet, otherwise it call |
| 6163 | * the applet ASAP. |
| 6164 | */ |
| 6165 | si_applet_cant_put(si); |
| 6166 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6167 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6168 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6169 | } |
| 6170 | |
| 6171 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6172 | { |
| 6173 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6174 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6175 | struct task *task; |
| 6176 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6177 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6178 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6179 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6180 | if (!hlua) { |
| 6181 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6182 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6183 | return 0; |
| 6184 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6185 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6186 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6187 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6188 | |
| 6189 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6190 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6191 | if (!task) { |
| 6192 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6193 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6194 | return 0; |
| 6195 | } |
| 6196 | task->nice = 0; |
| 6197 | task->context = ctx; |
| 6198 | task->process = hlua_applet_wakeup; |
| 6199 | ctx->ctx.hlua_apptcp.task = task; |
| 6200 | |
| 6201 | /* In the execution wrappers linked with a stream, the |
| 6202 | * Lua context can be not initialized. This behavior |
| 6203 | * permits to save performances because a systematic |
| 6204 | * Lua initialization cause 5% performances loss. |
| 6205 | */ |
| 6206 | if (!hlua_ctx_init(hlua, task)) { |
| 6207 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6208 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6209 | return 0; |
| 6210 | } |
| 6211 | |
| 6212 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6213 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6214 | |
| 6215 | /* The following Lua calls can fail. */ |
| 6216 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6217 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6218 | error = lua_tostring(hlua->T, -1); |
| 6219 | else |
| 6220 | error = "critical error"; |
| 6221 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6222 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6223 | RESET_SAFE_LJMP(hlua->T); |
| 6224 | return 0; |
| 6225 | } |
| 6226 | |
| 6227 | /* Check stack available size. */ |
| 6228 | if (!lua_checkstack(hlua->T, 1)) { |
| 6229 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6230 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6231 | RESET_SAFE_LJMP(hlua->T); |
| 6232 | return 0; |
| 6233 | } |
| 6234 | |
| 6235 | /* Restore the function in the stack. */ |
| 6236 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6237 | |
| 6238 | /* Create and and push object stream in the stack. */ |
| 6239 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6240 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6241 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6242 | RESET_SAFE_LJMP(hlua->T); |
| 6243 | return 0; |
| 6244 | } |
| 6245 | hlua->nargs = 1; |
| 6246 | |
| 6247 | /* push keywords in the stack. */ |
| 6248 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6249 | if (!lua_checkstack(hlua->T, 1)) { |
| 6250 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6251 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6252 | RESET_SAFE_LJMP(hlua->T); |
| 6253 | return 0; |
| 6254 | } |
| 6255 | lua_pushstring(hlua->T, *arg); |
| 6256 | hlua->nargs++; |
| 6257 | } |
| 6258 | |
| 6259 | RESET_SAFE_LJMP(hlua->T); |
| 6260 | |
| 6261 | /* Wakeup the applet ASAP. */ |
| 6262 | si_applet_cant_get(si); |
| 6263 | si_applet_cant_put(si); |
| 6264 | |
| 6265 | return 1; |
| 6266 | } |
| 6267 | |
| 6268 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6269 | { |
| 6270 | struct stream_interface *si = ctx->owner; |
| 6271 | struct stream *strm = si_strm(si); |
| 6272 | struct channel *res = si_ic(si); |
| 6273 | struct act_rule *rule = ctx->rule; |
| 6274 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6275 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6276 | |
| 6277 | /* The applet execution is already done. */ |
| 6278 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) |
| 6279 | return; |
| 6280 | |
| 6281 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6282 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6283 | return; |
| 6284 | |
| 6285 | /* Execute the function. */ |
| 6286 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6287 | /* finished. */ |
| 6288 | case HLUA_E_OK: |
| 6289 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6290 | |
| 6291 | /* log time */ |
| 6292 | strm->logs.tv_request = now; |
| 6293 | |
| 6294 | /* eat the whole request */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6295 | co_skip(si_oc(si), si_ob(si)->o); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6296 | res->flags |= CF_READ_NULL; |
| 6297 | si_shutr(si); |
| 6298 | return; |
| 6299 | |
| 6300 | /* yield. */ |
| 6301 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6302 | if (hlua->wake_time != TICK_ETERNITY) |
| 6303 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6304 | return; |
| 6305 | |
| 6306 | /* finished with error. */ |
| 6307 | case HLUA_E_ERRMSG: |
| 6308 | /* Display log. */ |
| 6309 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6310 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6311 | lua_pop(hlua->T, 1); |
| 6312 | goto error; |
| 6313 | |
| 6314 | case HLUA_E_ERR: |
| 6315 | /* Display log. */ |
| 6316 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6317 | rule->arg.hlua_rule->fcn.name); |
| 6318 | goto error; |
| 6319 | |
| 6320 | default: |
| 6321 | goto error; |
| 6322 | } |
| 6323 | |
| 6324 | error: |
| 6325 | |
| 6326 | /* For all other cases, just close the stream. */ |
| 6327 | si_shutw(si); |
| 6328 | si_shutr(si); |
| 6329 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6330 | } |
| 6331 | |
| 6332 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6333 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6334 | task_delete(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6335 | task_free(ctx->ctx.hlua_apptcp.task); |
| 6336 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6337 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6338 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6339 | } |
| 6340 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6341 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6342 | * an errors occurs and -1 if more data are required for initializing |
| 6343 | * the applet. |
| 6344 | */ |
| 6345 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6346 | { |
| 6347 | struct stream_interface *si = ctx->owner; |
| 6348 | struct channel *req = si_oc(si); |
| 6349 | struct http_msg *msg; |
| 6350 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6351 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6352 | char **arg; |
| 6353 | struct hdr_ctx hdr; |
| 6354 | struct task *task; |
| 6355 | 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] | 6356 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6357 | |
| 6358 | /* Wait for a full HTTP request. */ |
| 6359 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 6360 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 6361 | return -1; |
| 6362 | return 0; |
| 6363 | } |
| 6364 | txn = strm->txn; |
| 6365 | msg = &txn->req; |
| 6366 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6367 | /* We want two things in HTTP mode : |
| 6368 | * - enforce server-close mode if we were in keep-alive, so that the |
| 6369 | * applet is released after each response ; |
| 6370 | * - enable request body transfer to the applet in order to resync |
| 6371 | * with the response body. |
| 6372 | */ |
| 6373 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 6374 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6375 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6376 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6377 | if (!hlua) { |
| 6378 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6379 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6380 | return 0; |
| 6381 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6382 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6383 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6384 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6385 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6386 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6387 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6388 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6389 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6390 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6391 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6392 | if (!task) { |
| 6393 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6394 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6395 | return 0; |
| 6396 | } |
| 6397 | task->nice = 0; |
| 6398 | task->context = ctx; |
| 6399 | task->process = hlua_applet_wakeup; |
| 6400 | ctx->ctx.hlua_apphttp.task = task; |
| 6401 | |
| 6402 | /* In the execution wrappers linked with a stream, the |
| 6403 | * Lua context can be not initialized. This behavior |
| 6404 | * permits to save performances because a systematic |
| 6405 | * Lua initialization cause 5% performances loss. |
| 6406 | */ |
| 6407 | if (!hlua_ctx_init(hlua, task)) { |
| 6408 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6409 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6410 | return 0; |
| 6411 | } |
| 6412 | |
| 6413 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6414 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6415 | |
| 6416 | /* The following Lua calls can fail. */ |
| 6417 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6418 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6419 | error = lua_tostring(hlua->T, -1); |
| 6420 | else |
| 6421 | error = "critical error"; |
| 6422 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6423 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6424 | return 0; |
| 6425 | } |
| 6426 | |
| 6427 | /* Check stack available size. */ |
| 6428 | if (!lua_checkstack(hlua->T, 1)) { |
| 6429 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6430 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6431 | RESET_SAFE_LJMP(hlua->T); |
| 6432 | return 0; |
| 6433 | } |
| 6434 | |
| 6435 | /* Restore the function in the stack. */ |
| 6436 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6437 | |
| 6438 | /* Create and and push object stream in the stack. */ |
| 6439 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6440 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6441 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6442 | RESET_SAFE_LJMP(hlua->T); |
| 6443 | return 0; |
| 6444 | } |
| 6445 | hlua->nargs = 1; |
| 6446 | |
| 6447 | /* Look for a 100-continue expected. */ |
| 6448 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 6449 | hdr.idx = 0; |
| 6450 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) && |
| 6451 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 6452 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 6453 | } |
| 6454 | |
| 6455 | /* push keywords in the stack. */ |
| 6456 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6457 | if (!lua_checkstack(hlua->T, 1)) { |
| 6458 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6459 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6460 | RESET_SAFE_LJMP(hlua->T); |
| 6461 | return 0; |
| 6462 | } |
| 6463 | lua_pushstring(hlua->T, *arg); |
| 6464 | hlua->nargs++; |
| 6465 | } |
| 6466 | |
| 6467 | RESET_SAFE_LJMP(hlua->T); |
| 6468 | |
| 6469 | /* Wakeup the applet when data is ready for read. */ |
| 6470 | si_applet_cant_get(si); |
| 6471 | |
| 6472 | return 1; |
| 6473 | } |
| 6474 | |
| 6475 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 6476 | { |
| 6477 | struct stream_interface *si = ctx->owner; |
| 6478 | struct stream *strm = si_strm(si); |
| 6479 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6480 | struct act_rule *rule = ctx->rule; |
| 6481 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6482 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6483 | char *blk1; |
| 6484 | int len1; |
| 6485 | char *blk2; |
| 6486 | int len2; |
| 6487 | int ret; |
| 6488 | |
| 6489 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6490 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6491 | return; |
| 6492 | |
| 6493 | /* Set the currently running flag. */ |
| 6494 | if (!HLUA_IS_RUNNING(hlua) && |
| 6495 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6496 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6497 | /* Wait for full HTTP analysys. */ |
| 6498 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 6499 | si_applet_cant_get(si); |
| 6500 | return; |
| 6501 | } |
| 6502 | |
| 6503 | /* Store the max amount of bytes that we can read. */ |
| 6504 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 6505 | |
| 6506 | /* We need to flush the request header. This left the body |
| 6507 | * for the Lua. |
| 6508 | */ |
| 6509 | |
| 6510 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6511 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6512 | if (ret == -1) |
| 6513 | return; |
| 6514 | |
| 6515 | /* No data available, ask for more data. */ |
| 6516 | if (ret == 1) |
| 6517 | len2 = 0; |
| 6518 | if (ret == 0) |
| 6519 | len1 = 0; |
| 6520 | if (len1 + len2 < strm->txn->req.eoh + 2) { |
| 6521 | si_applet_cant_get(si); |
| 6522 | return; |
| 6523 | } |
| 6524 | |
| 6525 | /* skip the requests bytes. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6526 | co_skip(si_oc(si), strm->txn->req.eoh + 2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6527 | } |
| 6528 | |
| 6529 | /* Executes The applet if it is not done. */ |
| 6530 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6531 | |
| 6532 | /* Execute the function. */ |
| 6533 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6534 | /* finished. */ |
| 6535 | case HLUA_E_OK: |
| 6536 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6537 | break; |
| 6538 | |
| 6539 | /* yield. */ |
| 6540 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6541 | if (hlua->wake_time != TICK_ETERNITY) |
| 6542 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6543 | return; |
| 6544 | |
| 6545 | /* finished with error. */ |
| 6546 | case HLUA_E_ERRMSG: |
| 6547 | /* Display log. */ |
| 6548 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6549 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6550 | lua_pop(hlua->T, 1); |
| 6551 | goto error; |
| 6552 | |
| 6553 | case HLUA_E_ERR: |
| 6554 | /* Display log. */ |
| 6555 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6556 | rule->arg.hlua_rule->fcn.name); |
| 6557 | goto error; |
| 6558 | |
| 6559 | default: |
| 6560 | goto error; |
| 6561 | } |
| 6562 | } |
| 6563 | |
| 6564 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 6565 | |
| 6566 | /* We must send the final chunk. */ |
| 6567 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 6568 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 6569 | |
| 6570 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6571 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6572 | |
| 6573 | /* critical error. */ |
| 6574 | if (ret == -2 || ret == -3) { |
| 6575 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 6576 | rule->arg.hlua_rule->fcn.name); |
| 6577 | goto error; |
| 6578 | } |
| 6579 | |
| 6580 | /* no enough space error. */ |
| 6581 | if (ret == -1) { |
| 6582 | si_applet_cant_put(si); |
| 6583 | return; |
| 6584 | } |
| 6585 | |
| 6586 | /* set the last chunk sent. */ |
| 6587 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6588 | } |
| 6589 | |
| 6590 | /* close the connection. */ |
| 6591 | |
| 6592 | /* status / log */ |
| 6593 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6594 | strm->logs.tv_request = now; |
| 6595 | |
| 6596 | /* eat the whole request */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6597 | co_skip(si_oc(si), si_ob(si)->o); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6598 | res->flags |= CF_READ_NULL; |
| 6599 | si_shutr(si); |
| 6600 | |
| 6601 | return; |
| 6602 | } |
| 6603 | |
| 6604 | error: |
| 6605 | |
| 6606 | /* If we are in HTTP mode, and we are not send any |
| 6607 | * data, return a 500 server error in best effort: |
| 6608 | * if there are no room avalaible in the buffer, |
| 6609 | * just close the connection. |
| 6610 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6611 | ci_putblk(res, error_500, strlen(error_500)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6612 | if (!(strm->flags & SF_ERR_MASK)) |
| 6613 | strm->flags |= SF_ERR_RESOURCE; |
| 6614 | si_shutw(si); |
| 6615 | si_shutr(si); |
| 6616 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6617 | } |
| 6618 | |
| 6619 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6620 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6621 | task_delete(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6622 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6623 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6624 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6625 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6626 | } |
| 6627 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6628 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6629 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6630 | * |
| 6631 | * This function can fail with an abort() due to an Lua critical error. |
| 6632 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6633 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6634 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6635 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6636 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6637 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6638 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6639 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6640 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6641 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6642 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6643 | if (!rule->arg.hlua_rule) { |
| 6644 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6645 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6646 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6647 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6648 | /* Memory for arguments. */ |
| 6649 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6650 | if (!rule->arg.hlua_rule->args) { |
| 6651 | memprintf(err, "out of memory error"); |
| 6652 | return ACT_RET_PRS_ERR; |
| 6653 | } |
| 6654 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6655 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6656 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6657 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6658 | /* Expect some arguments */ |
| 6659 | for (i = 0; i < fcn->nargs; i++) { |
| 6660 | if (*args[i+1] == '\0') { |
| 6661 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6662 | return ACT_RET_PRS_ERR; |
| 6663 | } |
| 6664 | rule->arg.hlua_rule->args[i] = strdup(args[i + 1]); |
| 6665 | if (!rule->arg.hlua_rule->args[i]) { |
| 6666 | memprintf(err, "out of memory error"); |
| 6667 | return ACT_RET_PRS_ERR; |
| 6668 | } |
| 6669 | (*cur_arg)++; |
| 6670 | } |
| 6671 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6672 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6673 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6674 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6675 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6676 | } |
| 6677 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6678 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6679 | struct act_rule *rule, char **err) |
| 6680 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6681 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6682 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6683 | /* HTTP applets are forbidden in tcp-request rules. |
| 6684 | * HTTP applet request requires everything initilized by |
| 6685 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6686 | * The applet will be immediately initilized, but its before |
| 6687 | * the call of this analyzer. |
| 6688 | */ |
| 6689 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6690 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6691 | return ACT_RET_PRS_ERR; |
| 6692 | } |
| 6693 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6694 | /* Memory for the rule. */ |
| 6695 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6696 | if (!rule->arg.hlua_rule) { |
| 6697 | memprintf(err, "out of memory error"); |
| 6698 | return ACT_RET_PRS_ERR; |
| 6699 | } |
| 6700 | |
| 6701 | /* Reference the Lua function and store the reference. */ |
| 6702 | rule->arg.hlua_rule->fcn = *fcn; |
| 6703 | |
| 6704 | /* TODO: later accept arguments. */ |
| 6705 | rule->arg.hlua_rule->args = NULL; |
| 6706 | |
| 6707 | /* Add applet pointer in the rule. */ |
| 6708 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6709 | rule->applet.name = fcn->name; |
| 6710 | rule->applet.init = hlua_applet_http_init; |
| 6711 | rule->applet.fct = hlua_applet_http_fct; |
| 6712 | rule->applet.release = hlua_applet_http_release; |
| 6713 | rule->applet.timeout = hlua_timeout_applet; |
| 6714 | |
| 6715 | return ACT_RET_PRS_OK; |
| 6716 | } |
| 6717 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6718 | /* This function is an LUA binding used for registering |
| 6719 | * "sample-conv" functions. It expects a converter name used |
| 6720 | * in the haproxy configuration file, and an LUA function. |
| 6721 | */ |
| 6722 | __LJMP static int hlua_register_action(lua_State *L) |
| 6723 | { |
| 6724 | struct action_kw_list *akl; |
| 6725 | const char *name; |
| 6726 | int ref; |
| 6727 | int len; |
| 6728 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6729 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6730 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6731 | /* Initialise the number of expected arguments at 0. */ |
| 6732 | nargs = 0; |
| 6733 | |
| 6734 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6735 | 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] | 6736 | |
| 6737 | /* First argument : converter name. */ |
| 6738 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6739 | |
| 6740 | /* Second argument : environment. */ |
| 6741 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6742 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6743 | |
| 6744 | /* Third argument : lua function. */ |
| 6745 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6746 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6747 | /* Fouth argument : number of mandatories arguments expected on the configuration line. */ |
| 6748 | if (lua_gettop(L) >= 4) |
| 6749 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6750 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6751 | /* browse the second argulent as an array. */ |
| 6752 | lua_pushnil(L); |
| 6753 | while (lua_next(L, 2) != 0) { |
| 6754 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6755 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6756 | |
| 6757 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6758 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6759 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6760 | if (!akl) |
| 6761 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6762 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6763 | if (!fcn) |
| 6764 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6765 | |
| 6766 | /* Fill fcn. */ |
| 6767 | fcn->name = strdup(name); |
| 6768 | if (!fcn->name) |
| 6769 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6770 | fcn->function_ref = ref; |
| 6771 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6772 | /* Set the expected number od arguments. */ |
| 6773 | fcn->nargs = nargs; |
| 6774 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6775 | /* List head */ |
| 6776 | akl->list.n = akl->list.p = NULL; |
| 6777 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6778 | /* action keyword. */ |
| 6779 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6780 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6781 | if (!akl->kw[0].kw) |
| 6782 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6783 | |
| 6784 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6785 | |
| 6786 | akl->kw[0].match_pfx = 0; |
| 6787 | akl->kw[0].private = fcn; |
| 6788 | akl->kw[0].parse = action_register_lua; |
| 6789 | |
| 6790 | /* select the action registering point. */ |
| 6791 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6792 | tcp_req_cont_keywords_register(akl); |
| 6793 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6794 | tcp_res_cont_keywords_register(akl); |
| 6795 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6796 | http_req_keywords_register(akl); |
| 6797 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6798 | http_res_keywords_register(akl); |
| 6799 | else |
| 6800 | WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. " |
| 6801 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6802 | "are expected.", lua_tostring(L, -1))); |
| 6803 | |
| 6804 | /* pop the environment string. */ |
| 6805 | lua_pop(L, 1); |
| 6806 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6807 | return ACT_RET_PRS_OK; |
| 6808 | } |
| 6809 | |
| 6810 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6811 | struct act_rule *rule, char **err) |
| 6812 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6813 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6814 | |
| 6815 | /* Memory for the rule. */ |
| 6816 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6817 | if (!rule->arg.hlua_rule) { |
| 6818 | memprintf(err, "out of memory error"); |
| 6819 | return ACT_RET_PRS_ERR; |
| 6820 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6821 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6822 | /* Reference the Lua function and store the reference. */ |
| 6823 | rule->arg.hlua_rule->fcn = *fcn; |
| 6824 | |
| 6825 | /* TODO: later accept arguments. */ |
| 6826 | rule->arg.hlua_rule->args = NULL; |
| 6827 | |
| 6828 | /* Add applet pointer in the rule. */ |
| 6829 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6830 | rule->applet.name = fcn->name; |
| 6831 | rule->applet.init = hlua_applet_tcp_init; |
| 6832 | rule->applet.fct = hlua_applet_tcp_fct; |
| 6833 | rule->applet.release = hlua_applet_tcp_release; |
| 6834 | rule->applet.timeout = hlua_timeout_applet; |
| 6835 | |
| 6836 | return 0; |
| 6837 | } |
| 6838 | |
| 6839 | /* This function is an LUA binding used for registering |
| 6840 | * "sample-conv" functions. It expects a converter name used |
| 6841 | * in the haproxy configuration file, and an LUA function. |
| 6842 | */ |
| 6843 | __LJMP static int hlua_register_service(lua_State *L) |
| 6844 | { |
| 6845 | struct action_kw_list *akl; |
| 6846 | const char *name; |
| 6847 | const char *env; |
| 6848 | int ref; |
| 6849 | int len; |
| 6850 | struct hlua_function *fcn; |
| 6851 | |
| 6852 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 6853 | |
| 6854 | /* First argument : converter name. */ |
| 6855 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6856 | |
| 6857 | /* Second argument : environment. */ |
| 6858 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6859 | |
| 6860 | /* Third argument : lua function. */ |
| 6861 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6862 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6863 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6864 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 6865 | if (!akl) |
| 6866 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6867 | fcn = calloc(1, sizeof(*fcn)); |
| 6868 | if (!fcn) |
| 6869 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6870 | |
| 6871 | /* Fill fcn. */ |
| 6872 | len = strlen("<lua.>") + strlen(name) + 1; |
| 6873 | fcn->name = calloc(1, len); |
| 6874 | if (!fcn->name) |
| 6875 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6876 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 6877 | fcn->function_ref = ref; |
| 6878 | |
| 6879 | /* List head */ |
| 6880 | akl->list.n = akl->list.p = NULL; |
| 6881 | |
| 6882 | /* converter keyword. */ |
| 6883 | len = strlen("lua.") + strlen(name) + 1; |
| 6884 | akl->kw[0].kw = calloc(1, len); |
| 6885 | if (!akl->kw[0].kw) |
| 6886 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6887 | |
| 6888 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6889 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 6890 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6891 | if (strcmp(env, "tcp") == 0) |
| 6892 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6893 | else if (strcmp(env, "http") == 0) |
| 6894 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6895 | else |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6896 | WILL_LJMP(luaL_error(L, "lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 6897 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6898 | |
| 6899 | akl->kw[0].match_pfx = 0; |
| 6900 | akl->kw[0].private = fcn; |
| 6901 | |
| 6902 | /* End of array. */ |
| 6903 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 6904 | |
| 6905 | /* Register this new converter */ |
| 6906 | service_keywords_register(akl); |
| 6907 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6908 | return 0; |
| 6909 | } |
| 6910 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6911 | /* This function initialises Lua cli handler. It copies the |
| 6912 | * arguments in the Lua stack and create channel IO objects. |
| 6913 | */ |
| 6914 | static int hlua_cli_parse_fct(char **args, struct appctx *appctx, void *private) |
| 6915 | { |
| 6916 | struct hlua *hlua; |
| 6917 | struct hlua_function *fcn; |
| 6918 | int i; |
| 6919 | const char *error; |
| 6920 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6921 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6922 | appctx->ctx.hlua_cli.fcn = private; |
| 6923 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6924 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6925 | if (!hlua) { |
| 6926 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6927 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6928 | } |
| 6929 | HLUA_INIT(hlua); |
| 6930 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6931 | |
| 6932 | /* Create task used by signal to wakeup applets. |
| 6933 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 6934 | * applet_http. It is absolutely compatible. |
| 6935 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6936 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6937 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 6938 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6939 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6940 | } |
| 6941 | appctx->ctx.hlua_cli.task->nice = 0; |
| 6942 | appctx->ctx.hlua_cli.task->context = appctx; |
| 6943 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 6944 | |
| 6945 | /* Initialises the Lua context */ |
| 6946 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 6947 | 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] | 6948 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6949 | } |
| 6950 | |
| 6951 | /* The following Lua calls can fail. */ |
| 6952 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 6953 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6954 | error = lua_tostring(hlua->T, -1); |
| 6955 | else |
| 6956 | error = "critical error"; |
| 6957 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 6958 | goto error; |
| 6959 | } |
| 6960 | |
| 6961 | /* Check stack available size. */ |
| 6962 | if (!lua_checkstack(hlua->T, 2)) { |
| 6963 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6964 | goto error; |
| 6965 | } |
| 6966 | |
| 6967 | /* Restore the function in the stack. */ |
| 6968 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 6969 | |
| 6970 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 6971 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6972 | */ |
| 6973 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 6974 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6975 | goto error; |
| 6976 | } |
| 6977 | hlua->nargs = 1; |
| 6978 | |
| 6979 | /* push keywords in the stack. */ |
| 6980 | for (i = 0; *args[i]; i++) { |
| 6981 | /* Check stack available size. */ |
| 6982 | if (!lua_checkstack(hlua->T, 1)) { |
| 6983 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6984 | goto error; |
| 6985 | } |
| 6986 | lua_pushstring(hlua->T, args[i]); |
| 6987 | hlua->nargs++; |
| 6988 | } |
| 6989 | |
| 6990 | /* We must initialize the execution timeouts. */ |
| 6991 | hlua->max_time = hlua_timeout_session; |
| 6992 | |
| 6993 | /* At this point the execution is safe. */ |
| 6994 | RESET_SAFE_LJMP(hlua->T); |
| 6995 | |
| 6996 | /* It's ok */ |
| 6997 | return 0; |
| 6998 | |
| 6999 | /* It's not ok. */ |
| 7000 | error: |
| 7001 | RESET_SAFE_LJMP(hlua->T); |
| 7002 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7003 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7004 | return 1; |
| 7005 | } |
| 7006 | |
| 7007 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7008 | { |
| 7009 | struct hlua *hlua; |
| 7010 | struct stream_interface *si; |
| 7011 | struct hlua_function *fcn; |
| 7012 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7013 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7014 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7015 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7016 | |
| 7017 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7018 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7019 | return 1; |
| 7020 | |
| 7021 | /* Execute the function. */ |
| 7022 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7023 | |
| 7024 | /* finished. */ |
| 7025 | case HLUA_E_OK: |
| 7026 | return 1; |
| 7027 | |
| 7028 | /* yield. */ |
| 7029 | case HLUA_E_AGAIN: |
| 7030 | /* We want write. */ |
| 7031 | if (HLUA_IS_WAKERESWR(hlua)) |
| 7032 | si_applet_cant_put(si); |
| 7033 | /* Set the timeout. */ |
| 7034 | if (hlua->wake_time != TICK_ETERNITY) |
| 7035 | task_schedule(hlua->task, hlua->wake_time); |
| 7036 | return 0; |
| 7037 | |
| 7038 | /* finished with error. */ |
| 7039 | case HLUA_E_ERRMSG: |
| 7040 | /* Display log. */ |
| 7041 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7042 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7043 | lua_pop(hlua->T, 1); |
| 7044 | return 1; |
| 7045 | |
| 7046 | case HLUA_E_ERR: |
| 7047 | /* Display log. */ |
| 7048 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7049 | fcn->name); |
| 7050 | return 1; |
| 7051 | |
| 7052 | default: |
| 7053 | return 1; |
| 7054 | } |
| 7055 | |
| 7056 | return 1; |
| 7057 | } |
| 7058 | |
| 7059 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7060 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7061 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7062 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7063 | } |
| 7064 | |
| 7065 | /* This function is an LUA binding used for registering |
| 7066 | * new keywords in the cli. It expects a list of keywords |
| 7067 | * which are the "path". It is limited to 5 keywords. A |
| 7068 | * description of the command, a function to be executed |
| 7069 | * for the parsing and a function for io handlers. |
| 7070 | */ |
| 7071 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7072 | { |
| 7073 | struct cli_kw_list *cli_kws; |
| 7074 | const char *message; |
| 7075 | int ref_io; |
| 7076 | int len; |
| 7077 | struct hlua_function *fcn; |
| 7078 | int index; |
| 7079 | int i; |
| 7080 | |
| 7081 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7082 | |
| 7083 | /* First argument : an array of maximum 5 keywords. */ |
| 7084 | if (!lua_istable(L, 1)) |
| 7085 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7086 | |
| 7087 | /* Second argument : string with contextual message. */ |
| 7088 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7089 | |
| 7090 | /* Third and fourth argument : lua function. */ |
| 7091 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7092 | |
| 7093 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7094 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7095 | if (!cli_kws) |
| 7096 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 7097 | fcn = calloc(1, sizeof(*fcn)); |
| 7098 | if (!fcn) |
| 7099 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 7100 | |
| 7101 | /* Fill path. */ |
| 7102 | index = 0; |
| 7103 | lua_pushnil(L); |
| 7104 | while(lua_next(L, 1) != 0) { |
| 7105 | if (index >= 5) |
| 7106 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7107 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7108 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7109 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7110 | if (!cli_kws->kw[0].str_kw[index]) |
| 7111 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 7112 | index++; |
| 7113 | lua_pop(L, 1); |
| 7114 | } |
| 7115 | |
| 7116 | /* Copy help message. */ |
| 7117 | cli_kws->kw[0].usage = strdup(message); |
| 7118 | if (!cli_kws->kw[0].usage) |
| 7119 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 7120 | |
| 7121 | /* Fill fcn io handler. */ |
| 7122 | len = strlen("<lua.cli>") + 1; |
| 7123 | for (i = 0; i < index; i++) |
| 7124 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7125 | fcn->name = calloc(1, len); |
| 7126 | if (!fcn->name) |
| 7127 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 7128 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7129 | for (i = 0; i < index; i++) { |
| 7130 | strncat((char *)fcn->name, ".", len); |
| 7131 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7132 | } |
| 7133 | strncat((char *)fcn->name, ">", len); |
| 7134 | fcn->function_ref = ref_io; |
| 7135 | |
| 7136 | /* Fill last entries. */ |
| 7137 | cli_kws->kw[0].private = fcn; |
| 7138 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7139 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7140 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7141 | |
| 7142 | /* Register this new converter */ |
| 7143 | cli_register_kw(cli_kws); |
| 7144 | |
| 7145 | return 0; |
| 7146 | } |
| 7147 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7148 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7149 | struct proxy *defpx, const char *file, int line, |
| 7150 | char **err, unsigned int *timeout) |
| 7151 | { |
| 7152 | const char *error; |
| 7153 | |
| 7154 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 7155 | if (error && *error != '\0') { |
| 7156 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7157 | return -1; |
| 7158 | } |
| 7159 | return 0; |
| 7160 | } |
| 7161 | |
| 7162 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7163 | struct proxy *defpx, const char *file, int line, |
| 7164 | char **err) |
| 7165 | { |
| 7166 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7167 | file, line, err, &hlua_timeout_session); |
| 7168 | } |
| 7169 | |
| 7170 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7171 | struct proxy *defpx, const char *file, int line, |
| 7172 | char **err) |
| 7173 | { |
| 7174 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7175 | file, line, err, &hlua_timeout_task); |
| 7176 | } |
| 7177 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7178 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7179 | struct proxy *defpx, const char *file, int line, |
| 7180 | char **err) |
| 7181 | { |
| 7182 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7183 | file, line, err, &hlua_timeout_applet); |
| 7184 | } |
| 7185 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7186 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7187 | struct proxy *defpx, const char *file, int line, |
| 7188 | char **err) |
| 7189 | { |
| 7190 | char *error; |
| 7191 | |
| 7192 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7193 | if (*error != '\0') { |
| 7194 | memprintf(err, "%s: invalid number", args[0]); |
| 7195 | return -1; |
| 7196 | } |
| 7197 | return 0; |
| 7198 | } |
| 7199 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7200 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7201 | struct proxy *defpx, const char *file, int line, |
| 7202 | char **err) |
| 7203 | { |
| 7204 | char *error; |
| 7205 | |
| 7206 | if (*(args[1]) == 0) { |
| 7207 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7208 | return -1; |
| 7209 | } |
| 7210 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7211 | if (*error != '\0') { |
| 7212 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7213 | return -1; |
| 7214 | } |
| 7215 | return 0; |
| 7216 | } |
| 7217 | |
| 7218 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7219 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7220 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7221 | * the main lua entry point. |
| 7222 | * |
| 7223 | * This funtion runs with the HAProxy keywords API. It returns -1 if an error is |
| 7224 | * occured, otherwise it returns 0. |
| 7225 | * |
| 7226 | * In some error case, LUA set an error message in top of the stack. This function |
| 7227 | * 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] | 7228 | * |
| 7229 | * This function can fail with an abort() due to an Lua critical error. |
| 7230 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7231 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7232 | */ |
| 7233 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7234 | struct proxy *defpx, const char *file, int line, |
| 7235 | char **err) |
| 7236 | { |
| 7237 | int error; |
| 7238 | |
| 7239 | /* Just load and compile the file. */ |
| 7240 | error = luaL_loadfile(gL.T, args[1]); |
| 7241 | if (error) { |
| 7242 | memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1)); |
| 7243 | lua_pop(gL.T, 1); |
| 7244 | return -1; |
| 7245 | } |
| 7246 | |
| 7247 | /* If no syntax error where detected, execute the code. */ |
| 7248 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7249 | switch (error) { |
| 7250 | case LUA_OK: |
| 7251 | break; |
| 7252 | case LUA_ERRRUN: |
| 7253 | memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
| 7254 | lua_pop(gL.T, 1); |
| 7255 | return -1; |
| 7256 | case LUA_ERRMEM: |
| 7257 | memprintf(err, "lua out of memory error\n"); |
| 7258 | return -1; |
| 7259 | case LUA_ERRERR: |
| 7260 | memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1)); |
| 7261 | lua_pop(gL.T, 1); |
| 7262 | return -1; |
| 7263 | case LUA_ERRGCMM: |
| 7264 | memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1)); |
| 7265 | lua_pop(gL.T, 1); |
| 7266 | return -1; |
| 7267 | default: |
| 7268 | memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
| 7269 | lua_pop(gL.T, 1); |
| 7270 | return -1; |
| 7271 | } |
| 7272 | |
| 7273 | return 0; |
| 7274 | } |
| 7275 | |
| 7276 | /* configuration keywords declaration */ |
| 7277 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7278 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7279 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7280 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7281 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7282 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7283 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7284 | { 0, NULL, NULL }, |
| 7285 | }}; |
| 7286 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7287 | /* This function can fail with an abort() due to an Lua critical error. |
| 7288 | * We are in the initialisation process of HAProxy, this abort() is |
| 7289 | * tolerated. |
| 7290 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7291 | int hlua_post_init() |
| 7292 | { |
| 7293 | struct hlua_init_function *init; |
| 7294 | const char *msg; |
| 7295 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7296 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7297 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7298 | /* Call post initialisation function in safe environement. */ |
| 7299 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7300 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7301 | error = lua_tostring(gL.T, -1); |
| 7302 | else |
| 7303 | error = "critical error"; |
| 7304 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7305 | exit(1); |
| 7306 | } |
| 7307 | hlua_fcn_post_init(gL.T); |
| 7308 | RESET_SAFE_LJMP(gL.T); |
| 7309 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7310 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7311 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7312 | ret = hlua_ctx_resume(&gL, 0); |
| 7313 | switch (ret) { |
| 7314 | case HLUA_E_OK: |
| 7315 | lua_pop(gL.T, -1); |
| 7316 | return 1; |
| 7317 | case HLUA_E_AGAIN: |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7318 | ha_alert("lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7319 | return 0; |
| 7320 | case HLUA_E_ERRMSG: |
| 7321 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7322 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7323 | return 0; |
| 7324 | case HLUA_E_ERR: |
| 7325 | default: |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7326 | ha_alert("lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7327 | return 0; |
| 7328 | } |
| 7329 | } |
| 7330 | return 1; |
| 7331 | } |
| 7332 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7333 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7334 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7335 | * is the previously allocated size or the kind of object in case of a new |
| 7336 | * allocation. <nsize> is the requested new size. |
| 7337 | */ |
| 7338 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7339 | { |
| 7340 | struct hlua_mem_allocator *zone = ud; |
| 7341 | |
| 7342 | if (nsize == 0) { |
| 7343 | /* it's a free */ |
| 7344 | if (ptr) |
| 7345 | zone->allocated -= osize; |
| 7346 | free(ptr); |
| 7347 | return NULL; |
| 7348 | } |
| 7349 | |
| 7350 | if (!ptr) { |
| 7351 | /* it's a new allocation */ |
| 7352 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7353 | return NULL; |
| 7354 | |
| 7355 | ptr = malloc(nsize); |
| 7356 | if (ptr) |
| 7357 | zone->allocated += nsize; |
| 7358 | return ptr; |
| 7359 | } |
| 7360 | |
| 7361 | /* it's a realloc */ |
| 7362 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7363 | return NULL; |
| 7364 | |
| 7365 | ptr = realloc(ptr, nsize); |
| 7366 | if (ptr) |
| 7367 | zone->allocated += nsize - osize; |
| 7368 | return ptr; |
| 7369 | } |
| 7370 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7371 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7372 | * We are in the initialisation process of HAProxy, this abort() is |
| 7373 | * tolerated. |
| 7374 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7375 | void hlua_init(void) |
| 7376 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7377 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7378 | int idx; |
| 7379 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7380 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7381 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7382 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7383 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7384 | struct srv_kw *kw; |
| 7385 | int tmp_error; |
| 7386 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7387 | char *args[] = { /* SSL client configuration. */ |
| 7388 | "ssl", |
| 7389 | "verify", |
| 7390 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7391 | NULL |
| 7392 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7393 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7394 | |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7395 | HA_SPIN_INIT(&hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 7396 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7397 | /* Initialise struct hlua and com signals pool */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7398 | pool_head_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7399 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7400 | /* Register configuration keywords. */ |
| 7401 | cfg_register_keywords(&cfg_kws); |
| 7402 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7403 | /* Init main lua stack. */ |
| 7404 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7405 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7406 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7407 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7408 | hlua_sethlua(&gL); |
| 7409 | gL.Tref = LUA_REFNIL; |
| 7410 | gL.task = NULL; |
| 7411 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7412 | /* From this point, until the end of the initialisation fucntion, |
| 7413 | * the Lua function can fail with an abort. We are in the initialisation |
| 7414 | * process of HAProxy, this abort() is tolerated. |
| 7415 | */ |
| 7416 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7417 | /* Initialise lua. */ |
| 7418 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7419 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7420 | /* Set safe environment for the initialisation. */ |
| 7421 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7422 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7423 | error_msg = lua_tostring(gL.T, -1); |
| 7424 | else |
| 7425 | error_msg = "critical error"; |
| 7426 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7427 | exit(1); |
| 7428 | } |
| 7429 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7430 | /* |
| 7431 | * |
| 7432 | * Create "core" object. |
| 7433 | * |
| 7434 | */ |
| 7435 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7436 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7437 | lua_newtable(gL.T); |
| 7438 | |
| 7439 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7440 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7441 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7442 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7443 | /* Register special functions. */ |
| 7444 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7445 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7446 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7447 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7448 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7449 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7450 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7451 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7452 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7453 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7454 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7455 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7456 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7457 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7458 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7459 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7460 | hlua_class_function(gL.T, "log", hlua_log); |
| 7461 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7462 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7463 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7464 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7465 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7466 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7467 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7468 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7469 | |
| 7470 | /* |
| 7471 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7472 | * Register class Map |
| 7473 | * |
| 7474 | */ |
| 7475 | |
| 7476 | /* This table entry is the object "Map" base. */ |
| 7477 | lua_newtable(gL.T); |
| 7478 | |
| 7479 | /* register pattern types. */ |
| 7480 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7481 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7482 | for (i=0; i<PAT_MATCH_NUM; i++) { |
| 7483 | snprintf(trash.str, trash.size, "_%s", pat_match_names[i]); |
| 7484 | hlua_class_const_int(gL.T, trash.str, i); |
| 7485 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7486 | |
| 7487 | /* register constructor. */ |
| 7488 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7489 | |
| 7490 | /* Create and fill the metatable. */ |
| 7491 | lua_newtable(gL.T); |
| 7492 | |
| 7493 | /* Create and fille the __index entry. */ |
| 7494 | lua_pushstring(gL.T, "__index"); |
| 7495 | lua_newtable(gL.T); |
| 7496 | |
| 7497 | /* Register . */ |
| 7498 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7499 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7500 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7501 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7502 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7503 | /* Register previous table in the registry with reference and named entry. |
| 7504 | * The function hlua_register_metatable() pops the stack, so we |
| 7505 | * previously create a copy of the table. |
| 7506 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7507 | 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] | 7508 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7509 | |
| 7510 | /* Assign the metatable to the mai Map object. */ |
| 7511 | lua_setmetatable(gL.T, -2); |
| 7512 | |
| 7513 | /* Set a name to the table. */ |
| 7514 | lua_setglobal(gL.T, "Map"); |
| 7515 | |
| 7516 | /* |
| 7517 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7518 | * Register class Channel |
| 7519 | * |
| 7520 | */ |
| 7521 | |
| 7522 | /* Create and fill the metatable. */ |
| 7523 | lua_newtable(gL.T); |
| 7524 | |
| 7525 | /* Create and fille the __index entry. */ |
| 7526 | lua_pushstring(gL.T, "__index"); |
| 7527 | lua_newtable(gL.T); |
| 7528 | |
| 7529 | /* Register . */ |
| 7530 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7531 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7532 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7533 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7534 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7535 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7536 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7537 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7538 | 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] | 7539 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7540 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7541 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7542 | |
| 7543 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7544 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7545 | |
| 7546 | /* |
| 7547 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7548 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7549 | * |
| 7550 | */ |
| 7551 | |
| 7552 | /* Create and fill the metatable. */ |
| 7553 | lua_newtable(gL.T); |
| 7554 | |
| 7555 | /* Create and fille the __index entry. */ |
| 7556 | lua_pushstring(gL.T, "__index"); |
| 7557 | lua_newtable(gL.T); |
| 7558 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7559 | /* Browse existing fetches and create the associated |
| 7560 | * object method. |
| 7561 | */ |
| 7562 | sf = NULL; |
| 7563 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7564 | |
| 7565 | /* Dont register the keywork if the arguments check function are |
| 7566 | * not safe during the runtime. |
| 7567 | */ |
| 7568 | if ((sf->val_args != NULL) && |
| 7569 | (sf->val_args != val_payload_lv) && |
| 7570 | (sf->val_args != val_hdr)) |
| 7571 | continue; |
| 7572 | |
| 7573 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7574 | * by an underscore. |
| 7575 | */ |
| 7576 | strncpy(trash.str, sf->kw, trash.size); |
| 7577 | trash.str[trash.size - 1] = '\0'; |
| 7578 | for (p = trash.str; *p; p++) |
| 7579 | if (*p == '.' || *p == '-' || *p == '+') |
| 7580 | *p = '_'; |
| 7581 | |
| 7582 | /* Register the function. */ |
| 7583 | lua_pushstring(gL.T, trash.str); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7584 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7585 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7586 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7587 | } |
| 7588 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7589 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7590 | |
| 7591 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7592 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7593 | |
| 7594 | /* |
| 7595 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7596 | * Register class Converters |
| 7597 | * |
| 7598 | */ |
| 7599 | |
| 7600 | /* Create and fill the metatable. */ |
| 7601 | lua_newtable(gL.T); |
| 7602 | |
| 7603 | /* Create and fill the __index entry. */ |
| 7604 | lua_pushstring(gL.T, "__index"); |
| 7605 | lua_newtable(gL.T); |
| 7606 | |
| 7607 | /* Browse existing converters and create the associated |
| 7608 | * object method. |
| 7609 | */ |
| 7610 | sc = NULL; |
| 7611 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7612 | /* Dont register the keywork if the arguments check function are |
| 7613 | * not safe during the runtime. |
| 7614 | */ |
| 7615 | if (sc->val_args != NULL) |
| 7616 | continue; |
| 7617 | |
| 7618 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7619 | * by an underscore. |
| 7620 | */ |
| 7621 | strncpy(trash.str, sc->kw, trash.size); |
| 7622 | trash.str[trash.size - 1] = '\0'; |
| 7623 | for (p = trash.str; *p; p++) |
| 7624 | if (*p == '.' || *p == '-' || *p == '+') |
| 7625 | *p = '_'; |
| 7626 | |
| 7627 | /* Register the function. */ |
| 7628 | lua_pushstring(gL.T, trash.str); |
| 7629 | lua_pushlightuserdata(gL.T, sc); |
| 7630 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7631 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7632 | } |
| 7633 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7634 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7635 | |
| 7636 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7637 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7638 | |
| 7639 | /* |
| 7640 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7641 | * Register class HTTP |
| 7642 | * |
| 7643 | */ |
| 7644 | |
| 7645 | /* Create and fill the metatable. */ |
| 7646 | lua_newtable(gL.T); |
| 7647 | |
| 7648 | /* Create and fille the __index entry. */ |
| 7649 | lua_pushstring(gL.T, "__index"); |
| 7650 | lua_newtable(gL.T); |
| 7651 | |
| 7652 | /* Register Lua functions. */ |
| 7653 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7654 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7655 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7656 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7657 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7658 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7659 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7660 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7661 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7662 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7663 | |
| 7664 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7665 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7666 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7667 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7668 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7669 | 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] | 7670 | 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] | 7671 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7672 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7673 | |
| 7674 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7675 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7676 | |
| 7677 | /* |
| 7678 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7679 | * Register class AppletTCP |
| 7680 | * |
| 7681 | */ |
| 7682 | |
| 7683 | /* Create and fill the metatable. */ |
| 7684 | lua_newtable(gL.T); |
| 7685 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7686 | /* Create and fille the __index entry. */ |
| 7687 | lua_pushstring(gL.T, "__index"); |
| 7688 | lua_newtable(gL.T); |
| 7689 | |
| 7690 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7691 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7692 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7693 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7694 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7695 | 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] | 7696 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7697 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7698 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7699 | |
| 7700 | lua_settable(gL.T, -3); |
| 7701 | |
| 7702 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7703 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7704 | |
| 7705 | /* |
| 7706 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7707 | * Register class AppletHTTP |
| 7708 | * |
| 7709 | */ |
| 7710 | |
| 7711 | /* Create and fill the metatable. */ |
| 7712 | lua_newtable(gL.T); |
| 7713 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7714 | /* Create and fille the __index entry. */ |
| 7715 | lua_pushstring(gL.T, "__index"); |
| 7716 | lua_newtable(gL.T); |
| 7717 | |
| 7718 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7719 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7720 | 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] | 7721 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7722 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7723 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7724 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7725 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7726 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7727 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7728 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7729 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 7730 | |
| 7731 | lua_settable(gL.T, -3); |
| 7732 | |
| 7733 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7734 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7735 | |
| 7736 | /* |
| 7737 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7738 | * Register class TXN |
| 7739 | * |
| 7740 | */ |
| 7741 | |
| 7742 | /* Create and fill the metatable. */ |
| 7743 | lua_newtable(gL.T); |
| 7744 | |
| 7745 | /* Create and fille the __index entry. */ |
| 7746 | lua_pushstring(gL.T, "__index"); |
| 7747 | lua_newtable(gL.T); |
| 7748 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7749 | /* Register Lua functions. */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7750 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 7751 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7752 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7753 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7754 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7755 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7756 | hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel); |
| 7757 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 7758 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7759 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 7760 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 7761 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 7762 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 7763 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 7764 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7765 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7766 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7767 | |
| 7768 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7769 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7770 | |
| 7771 | /* |
| 7772 | * |
| 7773 | * Register class Socket |
| 7774 | * |
| 7775 | */ |
| 7776 | |
| 7777 | /* Create and fill the metatable. */ |
| 7778 | lua_newtable(gL.T); |
| 7779 | |
| 7780 | /* Create and fille the __index entry. */ |
| 7781 | lua_pushstring(gL.T, "__index"); |
| 7782 | lua_newtable(gL.T); |
| 7783 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7784 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7785 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7786 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7787 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 7788 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 7789 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 7790 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 7791 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 7792 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 7793 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 7794 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 7795 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7796 | 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] | 7797 | |
| 7798 | /* Register the garbage collector entry. */ |
| 7799 | lua_pushstring(gL.T, "__gc"); |
| 7800 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7801 | 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] | 7802 | |
| 7803 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7804 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7805 | |
| 7806 | /* Proxy and server configuration initialisation. */ |
| 7807 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 7808 | init_new_proxy(&socket_proxy); |
| 7809 | socket_proxy.parent = NULL; |
| 7810 | socket_proxy.last_change = now.tv_sec; |
| 7811 | socket_proxy.id = "LUA-SOCKET"; |
| 7812 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 7813 | socket_proxy.maxconn = 0; |
| 7814 | socket_proxy.accept = NULL; |
| 7815 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 7816 | socket_proxy.srv = NULL; |
| 7817 | socket_proxy.conn_retries = 0; |
| 7818 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 7819 | |
| 7820 | /* Init TCP server: unchanged parameters */ |
| 7821 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 7822 | socket_tcp.next = NULL; |
| 7823 | socket_tcp.proxy = &socket_proxy; |
| 7824 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 7825 | LIST_INIT(&socket_tcp.actconns); |
| 7826 | LIST_INIT(&socket_tcp.pendconns); |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 7827 | socket_tcp.priv_conns = NULL; |
| 7828 | socket_tcp.idle_conns = NULL; |
| 7829 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7830 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7831 | socket_tcp.last_change = 0; |
| 7832 | socket_tcp.id = "LUA-TCP-CONN"; |
| 7833 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7834 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7835 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 7836 | |
| 7837 | /* XXX: Copy default parameter from default server, |
| 7838 | * but the default server is not initialized. |
| 7839 | */ |
| 7840 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7841 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 7842 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 7843 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 7844 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 7845 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7846 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7847 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7848 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 7849 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 7850 | |
| 7851 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 7852 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 7853 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 7854 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 7855 | socket_tcp.check.server = &socket_tcp; |
| 7856 | |
| 7857 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 7858 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7859 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7860 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 7861 | socket_tcp.agent.server = &socket_tcp; |
| 7862 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 7863 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7864 | |
| 7865 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7866 | /* Init TCP server: unchanged parameters */ |
| 7867 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 7868 | socket_ssl.next = NULL; |
| 7869 | socket_ssl.proxy = &socket_proxy; |
| 7870 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 7871 | LIST_INIT(&socket_ssl.actconns); |
| 7872 | LIST_INIT(&socket_ssl.pendconns); |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 7873 | socket_tcp.priv_conns = NULL; |
| 7874 | socket_tcp.idle_conns = NULL; |
| 7875 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7876 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7877 | socket_ssl.last_change = 0; |
| 7878 | socket_ssl.id = "LUA-SSL-CONN"; |
| 7879 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7880 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7881 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 7882 | |
| 7883 | /* XXX: Copy default parameter from default server, |
| 7884 | * but the default server is not initialized. |
| 7885 | */ |
| 7886 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7887 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 7888 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 7889 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 7890 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 7891 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7892 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7893 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7894 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 7895 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 7896 | |
| 7897 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 7898 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 7899 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 7900 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 7901 | socket_ssl.check.server = &socket_ssl; |
| 7902 | |
| 7903 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 7904 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7905 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7906 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 7907 | socket_ssl.agent.server = &socket_ssl; |
| 7908 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7909 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 7910 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7911 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7912 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7913 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 7914 | /* |
| 7915 | * |
| 7916 | * If the keyword is not known, we can search in the registered |
| 7917 | * server keywords. This is usefull to configure special SSL |
| 7918 | * features like client certificates and ssl_verify. |
| 7919 | * |
| 7920 | */ |
| 7921 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 7922 | if (tmp_error != 0) { |
| 7923 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 7924 | abort(); /* This must be never arrives because the command line |
| 7925 | not editable by the user. */ |
| 7926 | } |
| 7927 | idx += kw->skip; |
| 7928 | } |
| 7929 | } |
| 7930 | |
| 7931 | /* Initialize SSL server. */ |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 7932 | if (socket_ssl.xprt->prepare_srv) |
| 7933 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7934 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7935 | |
| 7936 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7937 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 7938 | |
| 7939 | __attribute__((constructor)) |
| 7940 | static void __hlua_init(void) |
| 7941 | { |
| 7942 | char *ptr = NULL; |
| 7943 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 7944 | hap_register_build_opts(ptr, 1); |
| 7945 | } |