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 | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 28 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 29 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 30 | #include <types/hlua.h> |
| 31 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 32 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 33 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 34 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 35 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 36 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 37 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 38 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 39 | #include <proto/stats.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 40 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 41 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 42 | #include <proto/hlua_fcn.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 43 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 44 | #include <proto/obj_type.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 45 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 46 | #include <proto/payload.h> |
| 47 | #include <proto/proto_http.h> |
| 48 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 49 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 50 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 51 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 52 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 53 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 54 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 55 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 56 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 57 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 58 | * macro is used only for identifying the function that can |
| 59 | * not return because a longjmp is executed. |
| 60 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 61 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 62 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 63 | */ |
| 64 | #define __LJMP |
| 65 | #define WILL_LJMP(func) func |
| 66 | #define MAY_LJMP(func) func |
| 67 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 68 | /* This couple of function executes securely some Lua calls outside of |
| 69 | * the lua runtime environment. Each Lua call can return a longjmp |
| 70 | * if it encounter a memory error. |
| 71 | * |
| 72 | * Lua documentation extract: |
| 73 | * |
| 74 | * If an error happens outside any protected environment, Lua calls |
| 75 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 76 | * exiting the host application. Your panic function can avoid this |
| 77 | * exit by never returning (e.g., doing a long jump to your own |
| 78 | * recovery point outside Lua). |
| 79 | * |
| 80 | * The panic function runs as if it were a message handler (see |
| 81 | * §2.3); in particular, the error message is at the top of the |
| 82 | * stack. However, there is no guarantee about stack space. To push |
| 83 | * anything on the stack, the panic function must first check the |
| 84 | * available space (see §4.2). |
| 85 | * |
| 86 | * We must check all the Lua entry point. This includes: |
| 87 | * - The include/proto/hlua.h exported functions |
| 88 | * - the task wrapper function |
| 89 | * - The action wrapper function |
| 90 | * - The converters wrapper function |
| 91 | * - The sample-fetch wrapper functions |
| 92 | * |
| 93 | * It is tolerated that the initilisation function returns an abort. |
| 94 | * Before each Lua abort, an error message is writed on stderr. |
| 95 | * |
| 96 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 97 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 98 | * because they must be exists in the program stack when the longjmp |
| 99 | * is called. |
| 100 | */ |
| 101 | jmp_buf safe_ljmp_env; |
| 102 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 103 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 104 | |
| 105 | #define SET_SAFE_LJMP(__L) \ |
| 106 | ({ \ |
| 107 | int ret; \ |
| 108 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 109 | lua_atpanic(__L, hlua_panic_safe); \ |
| 110 | ret = 0; \ |
| 111 | } else { \ |
| 112 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 113 | ret = 1; \ |
| 114 | } \ |
| 115 | ret; \ |
| 116 | }) |
| 117 | |
| 118 | /* If we are the last function catching Lua errors, we |
| 119 | * must reset the panic function. |
| 120 | */ |
| 121 | #define RESET_SAFE_LJMP(__L) \ |
| 122 | do { \ |
| 123 | lua_atpanic(__L, hlua_panic_safe); \ |
| 124 | } while(0) |
| 125 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 126 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 127 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
| 128 | #define APPLET_100C 0x02 /* 100 continue expected. */ |
| 129 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 130 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 131 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 132 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 133 | |
| 134 | #define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n" |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 135 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 136 | /* The main Lua execution context. */ |
| 137 | struct hlua gL; |
| 138 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 139 | /* This is the memory pool containing struct lua for applets |
| 140 | * (including cli). |
| 141 | */ |
| 142 | struct pool_head *pool2_hlua; |
| 143 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 144 | /* Used for Socket connection. */ |
| 145 | static struct proxy socket_proxy; |
| 146 | static struct server socket_tcp; |
| 147 | #ifdef USE_OPENSSL |
| 148 | static struct server socket_ssl; |
| 149 | #endif |
| 150 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 151 | /* List head of the function called at the initialisation time. */ |
| 152 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 153 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 154 | /* The following variables contains the reference of the different |
| 155 | * Lua classes. These references are useful for identify metadata |
| 156 | * associated with an object. |
| 157 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 158 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 159 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 160 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 161 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 162 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 163 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 164 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 165 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 166 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 167 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 168 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 169 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 170 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 171 | * because a task may remain alive during all the haproxy execution. |
| 172 | */ |
| 173 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 174 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 175 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 176 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 177 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 178 | * it is used for preventing infinite loops. |
| 179 | * |
| 180 | * I test the scheer with an infinite loop containing one incrementation |
| 181 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 182 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 183 | * to one interrupt each 10 000 instructions. |
| 184 | * |
| 185 | * configured | Number of |
| 186 | * instructions | loops executed |
| 187 | * between two | in milions |
| 188 | * forced yields | |
| 189 | * ---------------+--------------- |
| 190 | * 10 | 160 |
| 191 | * 500 | 670 |
| 192 | * 1000 | 680 |
| 193 | * 5000 | 700 |
| 194 | * 7000 | 700 |
| 195 | * 8000 | 700 |
| 196 | * 9000 | 710 <- ceil |
| 197 | * 10000 | 710 |
| 198 | * 100000 | 710 |
| 199 | * 1000000 | 710 |
| 200 | * |
| 201 | */ |
| 202 | static unsigned int hlua_nb_instruction = 10000; |
| 203 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 204 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 205 | * be enforced on any memory allocation. |
| 206 | */ |
| 207 | struct hlua_mem_allocator { |
| 208 | size_t allocated; |
| 209 | size_t limit; |
| 210 | }; |
| 211 | |
| 212 | static struct hlua_mem_allocator hlua_global_allocator; |
| 213 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 214 | static const char error_500[] = |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 215 | "HTTP/1.0 500 Internal Server Error\r\n" |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 216 | "Cache-Control: no-cache\r\n" |
| 217 | "Connection: close\r\n" |
| 218 | "Content-Type: text/html\r\n" |
| 219 | "\r\n" |
Jarno Huuskonen | 16ad94a | 2017-01-09 14:17:10 +0200 | [diff] [blame] | 220 | "<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] | 221 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 222 | /* These functions converts types between HAProxy internal args or |
| 223 | * sample and LUA types. Another function permits to check if the |
| 224 | * LUA stack contains arguments according with an required ARG_T |
| 225 | * format. |
| 226 | */ |
| 227 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 228 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 229 | __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] | 230 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 231 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 232 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 233 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 234 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 235 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 236 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 237 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 238 | do { \ |
| 239 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 240 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
| 241 | Alert(__fmt, ## __args); \ |
| 242 | } while (0) |
| 243 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 244 | /* Used to check an Lua function type in the stack. It creates and |
| 245 | * returns a reference of the function. This function throws an |
| 246 | * error if the rgument is not a "function". |
| 247 | */ |
| 248 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 249 | { |
| 250 | if (!lua_isfunction(L, argno)) { |
| 251 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1)); |
| 252 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 253 | } |
| 254 | lua_pushvalue(L, argno); |
| 255 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 256 | } |
| 257 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 258 | /* Return the string that is of the top of the stack. */ |
| 259 | const char *hlua_get_top_error_string(lua_State *L) |
| 260 | { |
| 261 | if (lua_gettop(L) < 1) |
| 262 | return "unknown error"; |
| 263 | if (lua_type(L, -1) != LUA_TSTRING) |
| 264 | return "unknown error"; |
| 265 | return lua_tostring(L, -1); |
| 266 | } |
| 267 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 268 | /* This function check the number of arguments available in the |
| 269 | * stack. If the number of arguments available is not the same |
| 270 | * then <nb> an error is throwed. |
| 271 | */ |
| 272 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 273 | { |
| 274 | if (lua_gettop(L) == nb) |
| 275 | return; |
| 276 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 277 | } |
| 278 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 279 | /* This fucntion push an error string prefixed by the file name |
| 280 | * and the line number where the error is encountered. |
| 281 | */ |
| 282 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 283 | { |
| 284 | va_list argp; |
| 285 | va_start(argp, fmt); |
| 286 | luaL_where(L, 1); |
| 287 | lua_pushvfstring(L, fmt, argp); |
| 288 | va_end(argp); |
| 289 | lua_concat(L, 2); |
| 290 | return 1; |
| 291 | } |
| 292 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 293 | /* This functions is used with sample fetch and converters. It |
| 294 | * converts the HAProxy configuration argument in a lua stack |
| 295 | * values. |
| 296 | * |
| 297 | * It takes an array of "arg", and each entry of the array is |
| 298 | * converted and pushed in the LUA stack. |
| 299 | */ |
| 300 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 301 | { |
| 302 | switch (arg->type) { |
| 303 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 304 | case ARGT_TIME: |
| 305 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 306 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 307 | break; |
| 308 | |
| 309 | case ARGT_STR: |
| 310 | lua_pushlstring(L, arg->data.str.str, arg->data.str.len); |
| 311 | break; |
| 312 | |
| 313 | case ARGT_IPV4: |
| 314 | case ARGT_IPV6: |
| 315 | case ARGT_MSK4: |
| 316 | case ARGT_MSK6: |
| 317 | case ARGT_FE: |
| 318 | case ARGT_BE: |
| 319 | case ARGT_TAB: |
| 320 | case ARGT_SRV: |
| 321 | case ARGT_USR: |
| 322 | case ARGT_MAP: |
| 323 | default: |
| 324 | lua_pushnil(L); |
| 325 | break; |
| 326 | } |
| 327 | return 1; |
| 328 | } |
| 329 | |
| 330 | /* This function take one entrie in an LUA stack at the index "ud", |
| 331 | * and try to convert it in an HAProxy argument entry. This is useful |
| 332 | * with sample fetch wrappers. The input arguments are gived to the |
| 333 | * lua wrapper and converted as arg list by thi function. |
| 334 | */ |
| 335 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 336 | { |
| 337 | switch (lua_type(L, ud)) { |
| 338 | |
| 339 | case LUA_TNUMBER: |
| 340 | case LUA_TBOOLEAN: |
| 341 | arg->type = ARGT_SINT; |
| 342 | arg->data.sint = lua_tointeger(L, ud); |
| 343 | break; |
| 344 | |
| 345 | case LUA_TSTRING: |
| 346 | arg->type = ARGT_STR; |
| 347 | arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len); |
| 348 | break; |
| 349 | |
| 350 | case LUA_TUSERDATA: |
| 351 | case LUA_TNIL: |
| 352 | case LUA_TTABLE: |
| 353 | case LUA_TFUNCTION: |
| 354 | case LUA_TTHREAD: |
| 355 | case LUA_TLIGHTUSERDATA: |
| 356 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 357 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 358 | break; |
| 359 | } |
| 360 | return 1; |
| 361 | } |
| 362 | |
| 363 | /* the following functions are used to convert a struct sample |
| 364 | * in Lua type. This useful to convert the return of the |
| 365 | * fetchs or converters. |
| 366 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 367 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 368 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 369 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 370 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 371 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 372 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 373 | break; |
| 374 | |
| 375 | case SMP_T_BIN: |
| 376 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 377 | 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] | 378 | break; |
| 379 | |
| 380 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 381 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 382 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 383 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 384 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 385 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 386 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 387 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 388 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 389 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 390 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 391 | 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] | 392 | break; |
| 393 | default: |
| 394 | lua_pushnil(L); |
| 395 | break; |
| 396 | } |
| 397 | break; |
| 398 | |
| 399 | case SMP_T_IPV4: |
| 400 | case SMP_T_IPV6: |
| 401 | 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] | 402 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 403 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 404 | 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] | 405 | else |
| 406 | lua_pushnil(L); |
| 407 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 408 | default: |
| 409 | lua_pushnil(L); |
| 410 | break; |
| 411 | } |
| 412 | return 1; |
| 413 | } |
| 414 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 415 | /* the following functions are used to convert a struct sample |
| 416 | * in Lua strings. This is useful to convert the return of the |
| 417 | * fetchs or converters. |
| 418 | */ |
| 419 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 420 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 421 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 422 | |
| 423 | case SMP_T_BIN: |
| 424 | case SMP_T_STR: |
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); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 426 | break; |
| 427 | |
| 428 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 429 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 430 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 431 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 432 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 433 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 434 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 435 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 436 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 437 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 438 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 439 | 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] | 440 | break; |
| 441 | default: |
| 442 | lua_pushstring(L, ""); |
| 443 | break; |
| 444 | } |
| 445 | break; |
| 446 | |
| 447 | case SMP_T_SINT: |
| 448 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 449 | case SMP_T_IPV4: |
| 450 | case SMP_T_IPV6: |
| 451 | 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] | 452 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 453 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 454 | 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] | 455 | else |
| 456 | lua_pushstring(L, ""); |
| 457 | break; |
| 458 | default: |
| 459 | lua_pushstring(L, ""); |
| 460 | break; |
| 461 | } |
| 462 | return 1; |
| 463 | } |
| 464 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 465 | /* the following functions are used to convert an Lua type in a |
| 466 | * struct sample. This is useful to provide data from a converter |
| 467 | * to the LUA code. |
| 468 | */ |
| 469 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 470 | { |
| 471 | switch (lua_type(L, ud)) { |
| 472 | |
| 473 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 474 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 475 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 476 | break; |
| 477 | |
| 478 | |
| 479 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 480 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 481 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 482 | break; |
| 483 | |
| 484 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 485 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 486 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 487 | 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] | 488 | break; |
| 489 | |
| 490 | case LUA_TUSERDATA: |
| 491 | case LUA_TNIL: |
| 492 | case LUA_TTABLE: |
| 493 | case LUA_TFUNCTION: |
| 494 | case LUA_TTHREAD: |
| 495 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 496 | case LUA_TNONE: |
| 497 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 498 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 499 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 500 | break; |
| 501 | } |
| 502 | return 1; |
| 503 | } |
| 504 | |
| 505 | /* This function check the "argp" builded by another conversion function |
| 506 | * is in accord with the expected argp defined by the "mask". The fucntion |
| 507 | * 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] | 508 | * |
| 509 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 510 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 511 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 512 | __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] | 513 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 514 | { |
| 515 | int min_arg; |
| 516 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 517 | struct proxy *px; |
| 518 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 519 | |
| 520 | idx = 0; |
| 521 | min_arg = ARGM(mask); |
| 522 | mask >>= ARGM_BITS; |
| 523 | |
| 524 | while (1) { |
| 525 | |
| 526 | /* Check oversize. */ |
| 527 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 528 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | /* Check for mandatory arguments. */ |
| 532 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 533 | if (idx < min_arg) { |
| 534 | |
| 535 | /* If miss other argument than the first one, we return an error. */ |
| 536 | if (idx > 0) |
| 537 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 538 | |
| 539 | /* If first argument have a certain type, some default values |
| 540 | * may be used. See the function smp_resolve_args(). |
| 541 | */ |
| 542 | switch (mask & ARGT_MASK) { |
| 543 | |
| 544 | case ARGT_FE: |
| 545 | if (!(p->cap & PR_CAP_FE)) |
| 546 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 547 | argp[idx].data.prx = p; |
| 548 | argp[idx].type = ARGT_FE; |
| 549 | argp[idx+1].type = ARGT_STOP; |
| 550 | break; |
| 551 | |
| 552 | case ARGT_BE: |
| 553 | if (!(p->cap & PR_CAP_BE)) |
| 554 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 555 | argp[idx].data.prx = p; |
| 556 | argp[idx].type = ARGT_BE; |
| 557 | argp[idx+1].type = ARGT_STOP; |
| 558 | break; |
| 559 | |
| 560 | case ARGT_TAB: |
| 561 | argp[idx].data.prx = p; |
| 562 | argp[idx].type = ARGT_TAB; |
| 563 | argp[idx+1].type = ARGT_STOP; |
| 564 | break; |
| 565 | |
| 566 | default: |
| 567 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 568 | break; |
| 569 | } |
| 570 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | /* Check for exceed the number of requiered argument. */ |
| 575 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 576 | argp[idx].type != ARGT_STOP) { |
| 577 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 578 | } |
| 579 | |
| 580 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 581 | argp[idx].type == ARGT_STOP) { |
| 582 | return 0; |
| 583 | } |
| 584 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 585 | /* Convert some argument types. */ |
| 586 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 587 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 588 | if (argp[idx].type != ARGT_SINT) |
| 589 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 590 | argp[idx].type = ARGT_SINT; |
| 591 | break; |
| 592 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 593 | case ARGT_TIME: |
| 594 | if (argp[idx].type != ARGT_SINT) |
| 595 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 596 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 597 | break; |
| 598 | |
| 599 | case ARGT_SIZE: |
| 600 | if (argp[idx].type != ARGT_SINT) |
| 601 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 602 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 603 | break; |
| 604 | |
| 605 | case ARGT_FE: |
| 606 | if (argp[idx].type != ARGT_STR) |
| 607 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 608 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 609 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 610 | argp[idx].data.prx = proxy_fe_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 611 | if (!argp[idx].data.prx) |
| 612 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 613 | argp[idx].type = ARGT_FE; |
| 614 | break; |
| 615 | |
| 616 | case ARGT_BE: |
| 617 | if (argp[idx].type != ARGT_STR) |
| 618 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 619 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 620 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 621 | argp[idx].data.prx = proxy_be_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 622 | if (!argp[idx].data.prx) |
| 623 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 624 | argp[idx].type = ARGT_BE; |
| 625 | break; |
| 626 | |
| 627 | case ARGT_TAB: |
| 628 | if (argp[idx].type != ARGT_STR) |
| 629 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 630 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 631 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | e2dc1fa | 2015-05-26 12:08:07 +0200 | [diff] [blame] | 632 | argp[idx].data.prx = proxy_tbl_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 633 | if (!argp[idx].data.prx) |
| 634 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 635 | argp[idx].type = ARGT_TAB; |
| 636 | break; |
| 637 | |
| 638 | case ARGT_SRV: |
| 639 | if (argp[idx].type != ARGT_STR) |
| 640 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 641 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 642 | trash.str[argp[idx].data.str.len] = 0; |
| 643 | sname = strrchr(trash.str, '/'); |
| 644 | if (sname) { |
| 645 | *sname++ = '\0'; |
| 646 | pname = trash.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 647 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 648 | if (!px) |
| 649 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 650 | } |
| 651 | else { |
| 652 | sname = trash.str; |
| 653 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 654 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 655 | argp[idx].data.srv = findserver(px, sname); |
| 656 | if (!argp[idx].data.srv) |
| 657 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 658 | argp[idx].type = ARGT_SRV; |
| 659 | break; |
| 660 | |
| 661 | case ARGT_IPV4: |
| 662 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 663 | trash.str[argp[idx].data.str.len] = 0; |
| 664 | if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4)) |
| 665 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 666 | argp[idx].type = ARGT_IPV4; |
| 667 | break; |
| 668 | |
| 669 | case ARGT_MSK4: |
| 670 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 671 | trash.str[argp[idx].data.str.len] = 0; |
| 672 | if (!str2mask(trash.str, &argp[idx].data.ipv4)) |
| 673 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 674 | argp[idx].type = ARGT_MSK4; |
| 675 | break; |
| 676 | |
| 677 | case ARGT_IPV6: |
| 678 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 679 | trash.str[argp[idx].data.str.len] = 0; |
| 680 | if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6)) |
| 681 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 682 | argp[idx].type = ARGT_IPV6; |
| 683 | break; |
| 684 | |
| 685 | case ARGT_MSK6: |
| 686 | case ARGT_MAP: |
| 687 | case ARGT_REG: |
| 688 | case ARGT_USR: |
| 689 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 690 | break; |
| 691 | } |
| 692 | |
| 693 | /* Check for type of argument. */ |
| 694 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 695 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 696 | arg_type_names[(mask & ARGT_MASK)], |
| 697 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 698 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 699 | } |
| 700 | |
| 701 | /* Next argument. */ |
| 702 | mask >>= ARGT_BITS; |
| 703 | idx++; |
| 704 | } |
| 705 | } |
| 706 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 707 | /* |
| 708 | * The following functions are used to make correspondance between the the |
| 709 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 710 | * |
| 711 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 712 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 713 | */ |
| 714 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 715 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 716 | struct hlua **hlua = lua_getextraspace(L); |
| 717 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 718 | } |
| 719 | static inline void hlua_sethlua(struct hlua *hlua) |
| 720 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 721 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 722 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 723 | } |
| 724 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 725 | /* This function is used to send logs. It try to send on screen (stderr) |
| 726 | * and on the default syslog server. |
| 727 | */ |
| 728 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 729 | { |
| 730 | struct tm tm; |
| 731 | char *p; |
| 732 | |
| 733 | /* Cleanup the log message. */ |
| 734 | p = trash.str; |
| 735 | for (; *msg != '\0'; msg++, p++) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 736 | if (p >= trash.str + trash.size - 1) { |
| 737 | /* Break the message if exceed the buffer size. */ |
| 738 | *(p-4) = ' '; |
| 739 | *(p-3) = '.'; |
| 740 | *(p-2) = '.'; |
| 741 | *(p-1) = '.'; |
| 742 | break; |
| 743 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 744 | if (isprint(*msg)) |
| 745 | *p = *msg; |
| 746 | else |
| 747 | *p = '.'; |
| 748 | } |
| 749 | *p = '\0'; |
| 750 | |
Thierry FOURNIER | 5554e29 | 2015-09-09 11:21:37 +0200 | [diff] [blame] | 751 | send_log(px, level, "%s\n", trash.str); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 752 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 753 | get_localtime(date.tv_sec, &tm); |
| 754 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 755 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 756 | (int)getpid(), trash.str); |
| 757 | fflush(stderr); |
| 758 | } |
| 759 | } |
| 760 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 761 | /* This function just ensure that the yield will be always |
| 762 | * returned with a timeout and permit to set some flags |
| 763 | */ |
| 764 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 765 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 766 | { |
| 767 | struct hlua *hlua = hlua_gethlua(L); |
| 768 | |
| 769 | /* Set the wake timeout. If timeout is required, we set |
| 770 | * the expiration time. |
| 771 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 772 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 773 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 774 | hlua->flags |= flags; |
| 775 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 776 | /* Process the yield. */ |
| 777 | WILL_LJMP(lua_yieldk(L, nresults, ctx, k)); |
| 778 | } |
| 779 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 780 | /* This function initialises the Lua environment stored in the stream. |
| 781 | * 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] | 782 | * 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] | 783 | * |
| 784 | * This function is particular. it initialises a new Lua thread. If the |
| 785 | * initialisation fails (example: out of memory error), the lua function |
| 786 | * throws an error (longjmp). |
| 787 | * |
| 788 | * This function manipulates two Lua stack: the main and the thread. Only |
| 789 | * the main stack can fail. The thread is not manipulated. This function |
| 790 | * MUST NOT manipulate the created thread stack state, because is not |
| 791 | * proctected agains error throwed by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 792 | */ |
| 793 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 794 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 795 | if (!SET_SAFE_LJMP(gL.T)) { |
| 796 | lua->Tref = LUA_REFNIL; |
| 797 | return 0; |
| 798 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 799 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 800 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 801 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 802 | lua->T = lua_newthread(gL.T); |
| 803 | if (!lua->T) { |
| 804 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | 0a97620 | 2017-07-12 11:18:00 +0200 | [diff] [blame] | 805 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 806 | return 0; |
| 807 | } |
| 808 | hlua_sethlua(lua); |
| 809 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 810 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 811 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 812 | return 1; |
| 813 | } |
| 814 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 815 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 816 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 817 | * is not freed. |
| 818 | */ |
| 819 | void hlua_ctx_destroy(struct hlua *lua) |
| 820 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 821 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 822 | return; |
| 823 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 824 | if (!lua->T) |
| 825 | goto end; |
| 826 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 827 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 828 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 829 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 830 | if (!SET_SAFE_LJMP(lua->T)) |
| 831 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 832 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 833 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 834 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 835 | if (!SET_SAFE_LJMP(gL.T)) |
| 836 | return; |
| 837 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 838 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 839 | /* Forces a garbage collecting process. If the Lua program is finished |
| 840 | * without error, we run the GC on the thread pointer. Its freed all |
| 841 | * the unused memory. |
| 842 | * If the thread is finnish with an error or is currently yielded, |
| 843 | * it seems that the GC applied on the thread doesn't clean anything, |
| 844 | * so e run the GC on the main thread. |
| 845 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 846 | * the garbage collection. |
| 847 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 848 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 849 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 850 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 851 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 852 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 853 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 854 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 855 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 856 | |
| 857 | end: |
| 858 | pool_free2(pool2_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | /* This function is used to restore the Lua context when a coroutine |
| 862 | * fails. This function copy the common memory between old coroutine |
| 863 | * and the new coroutine. The old coroutine is destroyed, and its |
| 864 | * replaced by the new coroutine. |
| 865 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 866 | * as string error message and it is copied in the new stack. |
| 867 | */ |
| 868 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 869 | { |
| 870 | lua_State *T; |
| 871 | int new_ref; |
| 872 | |
| 873 | /* Renew the main LUA stack doesn't have sense. */ |
| 874 | if (lua == &gL) |
| 875 | return 0; |
| 876 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 877 | /* New Lua coroutine. */ |
| 878 | T = lua_newthread(gL.T); |
| 879 | if (!T) |
| 880 | return 0; |
| 881 | |
| 882 | /* Copy last error message. */ |
| 883 | if (keep_msg) |
| 884 | lua_xmove(lua->T, T, 1); |
| 885 | |
| 886 | /* Copy data between the coroutines. */ |
| 887 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 888 | lua_xmove(lua->T, T, 1); |
| 889 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 890 | |
| 891 | /* Destroy old data. */ |
| 892 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 893 | |
| 894 | /* The thread is garbage collected by Lua. */ |
| 895 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 896 | |
| 897 | /* Fill the struct with the new coroutine values. */ |
| 898 | lua->Mref = new_ref; |
| 899 | lua->T = T; |
| 900 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 901 | |
| 902 | /* Set context. */ |
| 903 | hlua_sethlua(lua); |
| 904 | |
| 905 | return 1; |
| 906 | } |
| 907 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 908 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 909 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 910 | struct hlua *hlua = hlua_gethlua(L); |
| 911 | |
| 912 | /* Lua cannot yield when its returning from a function, |
| 913 | * so, we can fix the interrupt hook to 1 instruction, |
| 914 | * expecting that the function is finnished. |
| 915 | */ |
| 916 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 917 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 918 | return; |
| 919 | } |
| 920 | |
| 921 | /* restore the interrupt condition. */ |
| 922 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 923 | |
| 924 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 925 | * If the state is not yieldable, trying yield causes an error. |
| 926 | */ |
| 927 | if (lua_isyieldable(L)) |
| 928 | WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 929 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 930 | /* If we cannot yield, update the clock and check the timeout. */ |
| 931 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 932 | hlua->run_time += now_ms - hlua->start_time; |
| 933 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 934 | lua_pushfstring(L, "execution timeout"); |
| 935 | WILL_LJMP(lua_error(L)); |
| 936 | } |
| 937 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 938 | /* Update the start time. */ |
| 939 | hlua->start_time = now_ms; |
| 940 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 941 | /* Try to interrupt the process at the end of the current |
| 942 | * unyieldable function. |
| 943 | */ |
| 944 | 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] | 945 | } |
| 946 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 947 | /* This function start or resumes the Lua stack execution. If the flag |
| 948 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 949 | * The function return an error. |
| 950 | * |
| 951 | * The function can returns 4 values: |
| 952 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 953 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 954 | * task wakeup. |
| 955 | * - HLUA_E_ERRMSG : An error has occured, an error message is set in |
| 956 | * the top of the stack. |
| 957 | * - HLUA_E_ERR : An error has occured without error message. |
| 958 | * |
| 959 | * If an error occured, the stack is renewed and it is ready to run new |
| 960 | * LUA code. |
| 961 | */ |
| 962 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 963 | { |
| 964 | int ret; |
| 965 | const char *msg; |
| 966 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 967 | /* Initialise run time counter. */ |
| 968 | if (!HLUA_IS_RUNNING(lua)) |
| 969 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 970 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 971 | resume_execution: |
| 972 | |
| 973 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 974 | * instructions. it is used for preventing infinite loops. |
| 975 | */ |
| 976 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 977 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 978 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 979 | HLUA_SET_RUN(lua); |
| 980 | HLUA_CLR_CTRLYIELD(lua); |
| 981 | HLUA_CLR_WAKERESWR(lua); |
| 982 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 983 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 984 | /* Update the start time. */ |
| 985 | lua->start_time = now_ms; |
| 986 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 987 | /* Call the function. */ |
| 988 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 989 | switch (ret) { |
| 990 | |
| 991 | case LUA_OK: |
| 992 | ret = HLUA_E_OK; |
| 993 | break; |
| 994 | |
| 995 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 996 | /* Check if the execution timeout is expired. It it is the case, we |
| 997 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 998 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 999 | tv_update_date(0, 1); |
| 1000 | lua->run_time += now_ms - lua->start_time; |
| 1001 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1002 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1003 | if (!lua_checkstack(lua->T, 1)) { |
| 1004 | ret = HLUA_E_ERR; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1005 | break; |
| 1006 | } |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1007 | lua_pushfstring(lua->T, "execution timeout"); |
| 1008 | ret = HLUA_E_ERRMSG; |
| 1009 | break; |
| 1010 | } |
| 1011 | /* Process the forced yield. if the general yield is not allowed or |
| 1012 | * if no task were associated this the current Lua execution |
| 1013 | * coroutine, we resume the execution. Else we want to return in the |
| 1014 | * scheduler and we want to be waked up again, to continue the |
| 1015 | * current Lua execution. So we schedule our own task. |
| 1016 | */ |
| 1017 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1018 | if (!yield_allowed || !lua->task) |
| 1019 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1020 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1021 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1022 | if (!yield_allowed) { |
| 1023 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1024 | if (!lua_checkstack(lua->T, 1)) { |
| 1025 | ret = HLUA_E_ERR; |
| 1026 | break; |
| 1027 | } |
| 1028 | lua_pushfstring(lua->T, "yield not allowed"); |
| 1029 | ret = HLUA_E_ERRMSG; |
| 1030 | break; |
| 1031 | } |
| 1032 | ret = HLUA_E_AGAIN; |
| 1033 | break; |
| 1034 | |
| 1035 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1036 | |
| 1037 | /* Special exit case. The traditionnal exit is returned as an error |
| 1038 | * because the errors ares the only one mean to return immediately |
| 1039 | * from and lua execution. |
| 1040 | */ |
| 1041 | if (lua->flags & HLUA_EXIT) { |
| 1042 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1043 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1044 | break; |
| 1045 | } |
| 1046 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1047 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1048 | if (!lua_checkstack(lua->T, 1)) { |
| 1049 | ret = HLUA_E_ERR; |
| 1050 | break; |
| 1051 | } |
| 1052 | msg = lua_tostring(lua->T, -1); |
| 1053 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1054 | lua_pop(lua->T, 1); |
| 1055 | if (msg) |
| 1056 | lua_pushfstring(lua->T, "runtime error: %s", msg); |
| 1057 | else |
| 1058 | lua_pushfstring(lua->T, "unknown runtime error"); |
| 1059 | ret = HLUA_E_ERRMSG; |
| 1060 | break; |
| 1061 | |
| 1062 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1063 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1064 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1065 | if (!lua_checkstack(lua->T, 1)) { |
| 1066 | ret = HLUA_E_ERR; |
| 1067 | break; |
| 1068 | } |
| 1069 | lua_pushfstring(lua->T, "out of memory error"); |
| 1070 | ret = HLUA_E_ERRMSG; |
| 1071 | break; |
| 1072 | |
| 1073 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1074 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1075 | if (!lua_checkstack(lua->T, 1)) { |
| 1076 | ret = HLUA_E_ERR; |
| 1077 | break; |
| 1078 | } |
| 1079 | msg = lua_tostring(lua->T, -1); |
| 1080 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1081 | lua_pop(lua->T, 1); |
| 1082 | if (msg) |
| 1083 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1084 | else |
| 1085 | lua_pushfstring(lua->T, "message handler error"); |
| 1086 | ret = HLUA_E_ERRMSG; |
| 1087 | break; |
| 1088 | |
| 1089 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1090 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1091 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1092 | if (!lua_checkstack(lua->T, 1)) { |
| 1093 | ret = HLUA_E_ERR; |
| 1094 | break; |
| 1095 | } |
| 1096 | lua_pushfstring(lua->T, "unknonwn error"); |
| 1097 | ret = HLUA_E_ERRMSG; |
| 1098 | break; |
| 1099 | } |
| 1100 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1101 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1102 | if (lua->flags & HLUA_MUST_GC && |
| 1103 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1104 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1105 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1106 | switch (ret) { |
| 1107 | case HLUA_E_AGAIN: |
| 1108 | break; |
| 1109 | |
| 1110 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1111 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1112 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1113 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1114 | break; |
| 1115 | |
| 1116 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1117 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1118 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1119 | hlua_ctx_renew(lua, 0); |
| 1120 | break; |
| 1121 | |
| 1122 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1123 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1124 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1125 | break; |
| 1126 | } |
| 1127 | |
| 1128 | return ret; |
| 1129 | } |
| 1130 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1131 | /* This function exit the current code. */ |
| 1132 | __LJMP static int hlua_done(lua_State *L) |
| 1133 | { |
| 1134 | struct hlua *hlua = hlua_gethlua(L); |
| 1135 | |
| 1136 | hlua->flags |= HLUA_EXIT; |
| 1137 | WILL_LJMP(lua_error(L)); |
| 1138 | |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1142 | /* This function is an LUA binding. It provides a function |
| 1143 | * for deleting ACL from a referenced ACL file. |
| 1144 | */ |
| 1145 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1146 | { |
| 1147 | const char *name; |
| 1148 | const char *key; |
| 1149 | struct pat_ref *ref; |
| 1150 | |
| 1151 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1152 | |
| 1153 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1154 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1155 | |
| 1156 | ref = pat_ref_lookup(name); |
| 1157 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1158 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1159 | |
| 1160 | pat_ref_delete(ref, key); |
| 1161 | return 0; |
| 1162 | } |
| 1163 | |
| 1164 | /* This function is an LUA binding. It provides a function |
| 1165 | * for deleting map entry from a referenced map file. |
| 1166 | */ |
| 1167 | static int hlua_del_map(lua_State *L) |
| 1168 | { |
| 1169 | const char *name; |
| 1170 | const char *key; |
| 1171 | struct pat_ref *ref; |
| 1172 | |
| 1173 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1174 | |
| 1175 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1176 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1177 | |
| 1178 | ref = pat_ref_lookup(name); |
| 1179 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1180 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1181 | |
| 1182 | pat_ref_delete(ref, key); |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | /* This function is an LUA binding. It provides a function |
| 1187 | * for adding ACL pattern from a referenced ACL file. |
| 1188 | */ |
| 1189 | static int hlua_add_acl(lua_State *L) |
| 1190 | { |
| 1191 | const char *name; |
| 1192 | const char *key; |
| 1193 | struct pat_ref *ref; |
| 1194 | |
| 1195 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1196 | |
| 1197 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1198 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1199 | |
| 1200 | ref = pat_ref_lookup(name); |
| 1201 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1202 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1203 | |
| 1204 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1205 | pat_ref_add(ref, key, NULL, NULL); |
| 1206 | return 0; |
| 1207 | } |
| 1208 | |
| 1209 | /* This function is an LUA binding. It provides a function |
| 1210 | * for setting map pattern and sample from a referenced map |
| 1211 | * file. |
| 1212 | */ |
| 1213 | static int hlua_set_map(lua_State *L) |
| 1214 | { |
| 1215 | const char *name; |
| 1216 | const char *key; |
| 1217 | const char *value; |
| 1218 | struct pat_ref *ref; |
| 1219 | |
| 1220 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1221 | |
| 1222 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1223 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1224 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1225 | |
| 1226 | ref = pat_ref_lookup(name); |
| 1227 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1228 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1229 | |
| 1230 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1231 | pat_ref_set(ref, key, value, NULL); |
| 1232 | else |
| 1233 | pat_ref_add(ref, key, value, NULL); |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1237 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1238 | * an integer or user data. This data is associated with a metatable. This |
| 1239 | * metatable have an original version registred in the global context with |
| 1240 | * the name of the object (_G[<name>] = <metable> ). |
| 1241 | * |
| 1242 | * A metable is a table that modify the standard behavior of a standard |
| 1243 | * access to the associated data. The entries of this new metatable are |
| 1244 | * defined as is: |
| 1245 | * |
| 1246 | * http://lua-users.org/wiki/MetatableEvents |
| 1247 | * |
| 1248 | * __index |
| 1249 | * |
| 1250 | * we access an absent field in a table, the result is nil. This is |
| 1251 | * true, but it is not the whole truth. Actually, such access triggers |
| 1252 | * the interpreter to look for an __index metamethod: If there is no |
| 1253 | * such method, as usually happens, then the access results in nil; |
| 1254 | * otherwise, the metamethod will provide the result. |
| 1255 | * |
| 1256 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1257 | * the key does not appear in the table, but the metatable has an __index |
| 1258 | * property: |
| 1259 | * |
| 1260 | * - if the value is a function, the function is called, passing in the |
| 1261 | * table and the key; the return value of that function is returned as |
| 1262 | * the result. |
| 1263 | * |
| 1264 | * - if the value is another table, the value of the key in that table is |
| 1265 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1266 | * table's metatable has an __index property, then it continues on up) |
| 1267 | * |
| 1268 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1269 | * |
| 1270 | * http://www.lua.org/pil/13.4.1.html |
| 1271 | * |
| 1272 | * __newindex |
| 1273 | * |
| 1274 | * Like __index, but control property assignment. |
| 1275 | * |
| 1276 | * __mode - Control weak references. A string value with one or both |
| 1277 | * of the characters 'k' and 'v' which specifies that the the |
| 1278 | * keys and/or values in the table are weak references. |
| 1279 | * |
| 1280 | * __call - Treat a table like a function. When a table is followed by |
| 1281 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1282 | * a __call key pointing to a function, that function is invoked |
| 1283 | * (passing any specified arguments) and the return value is |
| 1284 | * returned. |
| 1285 | * |
| 1286 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1287 | * called, if the metatable for myTable has a __metatable |
| 1288 | * key, the value of that key is returned instead of the |
| 1289 | * actual metatable. |
| 1290 | * |
| 1291 | * __tostring - Control string representation. When the builtin |
| 1292 | * "tostring( myTable )" function is called, if the metatable |
| 1293 | * for myTable has a __tostring property set to a function, |
| 1294 | * that function is invoked (passing myTable to it) and the |
| 1295 | * return value is used as the string representation. |
| 1296 | * |
| 1297 | * __len - Control table length. When the table length is requested using |
| 1298 | * the length operator ( '#' ), if the metatable for myTable has |
| 1299 | * a __len key pointing to a function, that function is invoked |
| 1300 | * (passing myTable to it) and the return value used as the value |
| 1301 | * of "#myTable". |
| 1302 | * |
| 1303 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1304 | * collected, if the metatable has a __gc field pointing to a |
| 1305 | * function, that function is first invoked, passing the userdata |
| 1306 | * to it. The __gc metamethod is not called for tables. |
| 1307 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1308 | * |
| 1309 | * Special metamethods for redefining standard operators: |
| 1310 | * http://www.lua.org/pil/13.1.html |
| 1311 | * |
| 1312 | * __add "+" |
| 1313 | * __sub "-" |
| 1314 | * __mul "*" |
| 1315 | * __div "/" |
| 1316 | * __unm "!" |
| 1317 | * __pow "^" |
| 1318 | * __concat ".." |
| 1319 | * |
| 1320 | * Special methods for redfining standar relations |
| 1321 | * http://www.lua.org/pil/13.2.html |
| 1322 | * |
| 1323 | * __eq "==" |
| 1324 | * __lt "<" |
| 1325 | * __le "<=" |
| 1326 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1327 | |
| 1328 | /* |
| 1329 | * |
| 1330 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1331 | * Class Map |
| 1332 | * |
| 1333 | * |
| 1334 | */ |
| 1335 | |
| 1336 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1337 | * a class session, otherwise it throws an error. |
| 1338 | */ |
| 1339 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1340 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1341 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | /* This function is the map constructor. It don't need |
| 1345 | * the class Map object. It creates and return a new Map |
| 1346 | * object. It must be called only during "body" or "init" |
| 1347 | * context because it process some filesystem accesses. |
| 1348 | */ |
| 1349 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1350 | { |
| 1351 | const char *fn; |
| 1352 | int match = PAT_MATCH_STR; |
| 1353 | struct sample_conv conv; |
| 1354 | const char *file = ""; |
| 1355 | int line = 0; |
| 1356 | lua_Debug ar; |
| 1357 | char *err = NULL; |
| 1358 | struct arg args[2]; |
| 1359 | |
| 1360 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1361 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1362 | |
| 1363 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1364 | |
| 1365 | if (lua_gettop(L) >= 2) { |
| 1366 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1367 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1368 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1369 | } |
| 1370 | |
| 1371 | /* Get Lua filename and line number. */ |
| 1372 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1373 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1374 | if (ar.currentline > 0) { /* is there info? */ |
| 1375 | file = ar.short_src; |
| 1376 | line = ar.currentline; |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | /* fill fake sample_conv struct. */ |
| 1381 | conv.kw = ""; /* unused. */ |
| 1382 | conv.process = NULL; /* unused. */ |
| 1383 | conv.arg_mask = 0; /* unused. */ |
| 1384 | conv.val_args = NULL; /* unused. */ |
| 1385 | conv.out_type = SMP_T_STR; |
| 1386 | conv.private = (void *)(long)match; |
| 1387 | switch (match) { |
| 1388 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1389 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1390 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1391 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1392 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1393 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1394 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1395 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1396 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1397 | default: |
| 1398 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1399 | } |
| 1400 | |
| 1401 | /* fill fake args. */ |
| 1402 | args[0].type = ARGT_STR; |
| 1403 | args[0].data.str.str = (char *)fn; |
| 1404 | args[1].type = ARGT_STOP; |
| 1405 | |
| 1406 | /* load the map. */ |
| 1407 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1408 | /* error case: we cant use luaL_error because we must |
| 1409 | * free the err variable. |
| 1410 | */ |
| 1411 | luaL_where(L, 1); |
| 1412 | lua_pushfstring(L, "'new': %s.", err); |
| 1413 | lua_concat(L, 2); |
| 1414 | free(err); |
| 1415 | WILL_LJMP(lua_error(L)); |
| 1416 | } |
| 1417 | |
| 1418 | /* create the lua object. */ |
| 1419 | lua_newtable(L); |
| 1420 | lua_pushlightuserdata(L, args[0].data.map); |
| 1421 | lua_rawseti(L, -2, 0); |
| 1422 | |
| 1423 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1424 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1425 | lua_setmetatable(L, -2); |
| 1426 | |
| 1427 | |
| 1428 | return 1; |
| 1429 | } |
| 1430 | |
| 1431 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1432 | { |
| 1433 | struct map_descriptor *desc; |
| 1434 | struct pattern *pat; |
| 1435 | struct sample smp; |
| 1436 | |
| 1437 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1438 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1439 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1440 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1441 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1442 | } |
| 1443 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1444 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1445 | smp.flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1446 | 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] | 1447 | } |
| 1448 | |
| 1449 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1450 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1451 | if (str) |
| 1452 | lua_pushstring(L, ""); |
| 1453 | else |
| 1454 | lua_pushnil(L); |
| 1455 | return 1; |
| 1456 | } |
| 1457 | |
| 1458 | /* 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] | 1459 | 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] | 1460 | return 1; |
| 1461 | } |
| 1462 | |
| 1463 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1464 | { |
| 1465 | return _hlua_map_lookup(L, 0); |
| 1466 | } |
| 1467 | |
| 1468 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1469 | { |
| 1470 | return _hlua_map_lookup(L, 1); |
| 1471 | } |
| 1472 | |
| 1473 | /* |
| 1474 | * |
| 1475 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1476 | * Class Socket |
| 1477 | * |
| 1478 | * |
| 1479 | */ |
| 1480 | |
| 1481 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1482 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1483 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | /* This function is the handler called for each I/O on the established |
| 1487 | * connection. It is used for notify space avalaible to send or data |
| 1488 | * received. |
| 1489 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1490 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1491 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1492 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | 50fe03b | 2014-11-28 13:59:31 +0100 | [diff] [blame] | 1493 | struct connection *c = objt_conn(si_opposite(si)->end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1494 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1495 | if (appctx->ctx.hlua_cosocket.die) { |
| 1496 | si_shutw(si); |
| 1497 | si_shutr(si); |
| 1498 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1499 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1500 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1501 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1502 | } |
| 1503 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1504 | /* If the connection object is not avalaible, close all the |
| 1505 | * streams and wakeup everithing waiting for. |
| 1506 | */ |
| 1507 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1508 | si_shutw(si); |
| 1509 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1510 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1511 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1512 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1513 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1514 | } |
| 1515 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1516 | /* If we cant write, wakeup the pending write signals. */ |
| 1517 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1518 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1519 | |
| 1520 | /* If we cant read, wakeup the pending read signals. */ |
| 1521 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1522 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1523 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1524 | /* if the connection is not estabkished, inform the stream that we want |
| 1525 | * to be notified whenever the connection completes. |
| 1526 | */ |
| 1527 | if (!(c->flags & CO_FL_CONNECTED)) { |
| 1528 | si_applet_cant_get(si); |
| 1529 | si_applet_cant_put(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1530 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1531 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1532 | |
| 1533 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1534 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1535 | |
| 1536 | /* 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] | 1537 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1538 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1539 | |
| 1540 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1541 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1542 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1543 | } |
| 1544 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1545 | /* This function is called when the "struct stream" is destroyed. |
| 1546 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1547 | * Wake all the pending signals. |
| 1548 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1549 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1550 | { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1551 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1552 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1553 | |
| 1554 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1555 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1556 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1560 | * uses this object. If the stream does not exists, just quit. |
| 1561 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1562 | * pending signal can rest in the read and write lists. destroy |
| 1563 | * it. |
| 1564 | */ |
| 1565 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1566 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1567 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1568 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1569 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1570 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1571 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1572 | |
| 1573 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1574 | peer = xref_get_peer(&socket->xref); |
| 1575 | if (!peer) { |
| 1576 | xref_disconnect(&socket->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1577 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1578 | } |
| 1579 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1580 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1581 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1582 | appctx->ctx.hlua_cosocket.die = 1; |
| 1583 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1584 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1585 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
| 1586 | xref_disconnect(&socket->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1587 | return 0; |
| 1588 | } |
| 1589 | |
| 1590 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1591 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1592 | */ |
| 1593 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1594 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1595 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1596 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1597 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1598 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1599 | MAY_LJMP(check_args(L, 1, "close")); |
| 1600 | |
| 1601 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1602 | peer = xref_get_peer(&socket->xref); |
| 1603 | if (!peer) { |
| 1604 | xref_disconnect(&socket->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1605 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1606 | } |
| 1607 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1608 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1609 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1610 | appctx->ctx.hlua_cosocket.die = 1; |
| 1611 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1612 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1613 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
| 1614 | xref_disconnect(&socket->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1615 | return 0; |
| 1616 | } |
| 1617 | |
| 1618 | /* This Lua function assumes that the stack contain three parameters. |
| 1619 | * 1 - USERDATA containing a struct socket |
| 1620 | * 2 - INTEGER with values of the macro defined below |
| 1621 | * If the integer is -1, we must read at most one line. |
| 1622 | * If the integer is -2, we ust read all the data until the |
| 1623 | * end of the stream. |
| 1624 | * If the integer is positive value, we must read a number of |
| 1625 | * bytes corresponding to this value. |
| 1626 | */ |
| 1627 | #define HLSR_READ_LINE (-1) |
| 1628 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1629 | __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] | 1630 | { |
| 1631 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1632 | int wanted = lua_tointeger(L, 2); |
| 1633 | struct hlua *hlua = hlua_gethlua(L); |
| 1634 | struct appctx *appctx; |
| 1635 | int len; |
| 1636 | int nblk; |
| 1637 | char *blk1; |
| 1638 | int len1; |
| 1639 | char *blk2; |
| 1640 | int len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1641 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1642 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1643 | struct stream_interface *si; |
| 1644 | struct stream *s; |
| 1645 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1646 | |
| 1647 | /* Check if this lua stack is schedulable. */ |
| 1648 | if (!hlua || !hlua->task) |
| 1649 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1650 | "'frontend', 'backend' or 'task'")); |
| 1651 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1652 | /* check for connection break. If some data where read, return it. */ |
| 1653 | peer = xref_get_peer(&socket->xref); |
| 1654 | if (!peer) { |
| 1655 | xref_disconnect(&socket->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1656 | goto connection_closed; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1657 | } |
| 1658 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1659 | si = appctx->owner; |
| 1660 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1661 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1662 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1663 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1664 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1665 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1666 | if (nblk < 0) /* Connection close. */ |
| 1667 | goto connection_closed; |
| 1668 | if (nblk == 0) /* No data avalaible. */ |
| 1669 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1670 | |
| 1671 | /* remove final \r\n. */ |
| 1672 | if (nblk == 1) { |
| 1673 | if (blk1[len1-1] == '\n') { |
| 1674 | len1--; |
| 1675 | skip_at_end++; |
| 1676 | if (blk1[len1-1] == '\r') { |
| 1677 | len1--; |
| 1678 | skip_at_end++; |
| 1679 | } |
| 1680 | } |
| 1681 | } |
| 1682 | else { |
| 1683 | if (blk2[len2-1] == '\n') { |
| 1684 | len2--; |
| 1685 | skip_at_end++; |
| 1686 | if (blk2[len2-1] == '\r') { |
| 1687 | len2--; |
| 1688 | skip_at_end++; |
| 1689 | } |
| 1690 | } |
| 1691 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1692 | } |
| 1693 | |
| 1694 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1695 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1696 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1697 | if (nblk < 0) /* Connection close. */ |
| 1698 | goto connection_closed; |
| 1699 | if (nblk == 0) /* No data avalaible. */ |
| 1700 | goto connection_empty; |
| 1701 | } |
| 1702 | |
| 1703 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1704 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1705 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1706 | if (nblk < 0) /* Connection close. */ |
| 1707 | goto connection_closed; |
| 1708 | if (nblk == 0) /* No data avalaible. */ |
| 1709 | goto connection_empty; |
| 1710 | |
| 1711 | if (len1 > wanted) { |
| 1712 | nblk = 1; |
| 1713 | len1 = wanted; |
| 1714 | } if (nblk == 2 && len1 + len2 > wanted) |
| 1715 | len2 = wanted - len1; |
| 1716 | } |
| 1717 | |
| 1718 | len = len1; |
| 1719 | |
| 1720 | luaL_addlstring(&socket->b, blk1, len1); |
| 1721 | if (nblk == 2) { |
| 1722 | len += len2; |
| 1723 | luaL_addlstring(&socket->b, blk2, len2); |
| 1724 | } |
| 1725 | |
| 1726 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1727 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1728 | |
| 1729 | /* Don't wait anything. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1730 | stream_int_notify(&s->si[0]); |
| 1731 | stream_int_update_applet(&s->si[0]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1732 | |
| 1733 | /* If the pattern reclaim to read all the data |
| 1734 | * in the connection, got out. |
| 1735 | */ |
| 1736 | if (wanted == HLSR_READ_ALL) |
| 1737 | goto connection_empty; |
| 1738 | else if (wanted >= 0 && len < wanted) |
| 1739 | goto connection_empty; |
| 1740 | |
| 1741 | /* Return result. */ |
| 1742 | luaL_pushresult(&socket->b); |
| 1743 | return 1; |
| 1744 | |
| 1745 | connection_closed: |
| 1746 | |
| 1747 | /* If the buffer containds data. */ |
| 1748 | if (socket->b.n > 0) { |
| 1749 | luaL_pushresult(&socket->b); |
| 1750 | return 1; |
| 1751 | } |
| 1752 | lua_pushnil(L); |
| 1753 | lua_pushstring(L, "connection closed."); |
| 1754 | return 2; |
| 1755 | |
| 1756 | connection_empty: |
| 1757 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1758 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1759 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1760 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1761 | 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] | 1762 | return 0; |
| 1763 | } |
| 1764 | |
| 1765 | /* This Lus function gets two parameters. The first one can be string |
| 1766 | * or a number. If the string is "*l", the user require one line. If |
| 1767 | * the string is "*a", the user require all the content of the stream. |
| 1768 | * If the value is a number, the user require a number of bytes equal |
| 1769 | * to the value. The default value is "*l" (a line). |
| 1770 | * |
| 1771 | * This paraeter with a variable type is converted in integer. This |
| 1772 | * integer takes this values: |
| 1773 | * -1 : read a line |
| 1774 | * -2 : read all the stream |
| 1775 | * >0 : amount if bytes. |
| 1776 | * |
| 1777 | * The second parameter is optinal. It contains a string that must be |
| 1778 | * concatenated with the read data. |
| 1779 | */ |
| 1780 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1781 | { |
| 1782 | int wanted = HLSR_READ_LINE; |
| 1783 | const char *pattern; |
| 1784 | int type; |
| 1785 | char *error; |
| 1786 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1787 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1788 | |
| 1789 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1790 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1791 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1792 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1793 | |
| 1794 | /* check for pattern. */ |
| 1795 | if (lua_gettop(L) >= 2) { |
| 1796 | type = lua_type(L, 2); |
| 1797 | if (type == LUA_TSTRING) { |
| 1798 | pattern = lua_tostring(L, 2); |
| 1799 | if (strcmp(pattern, "*a") == 0) |
| 1800 | wanted = HLSR_READ_ALL; |
| 1801 | else if (strcmp(pattern, "*l") == 0) |
| 1802 | wanted = HLSR_READ_LINE; |
| 1803 | else { |
| 1804 | wanted = strtoll(pattern, &error, 10); |
| 1805 | if (*error != '\0') |
| 1806 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1807 | } |
| 1808 | } |
| 1809 | else if (type == LUA_TNUMBER) { |
| 1810 | wanted = lua_tointeger(L, 2); |
| 1811 | if (wanted < 0) |
| 1812 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | /* Set pattern. */ |
| 1817 | lua_pushinteger(L, wanted); |
| 1818 | lua_replace(L, 2); |
| 1819 | |
| 1820 | /* init bufffer, and fiil it wih prefix. */ |
| 1821 | luaL_buffinit(L, &socket->b); |
| 1822 | |
| 1823 | /* Check prefix. */ |
| 1824 | if (lua_gettop(L) >= 3) { |
| 1825 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1826 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1827 | pattern = lua_tolstring(L, 3, &len); |
| 1828 | luaL_addlstring(&socket->b, pattern, len); |
| 1829 | } |
| 1830 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1831 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | /* Write the Lua input string in the output buffer. |
| 1835 | * This fucntion returns a yield if no space are available. |
| 1836 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1837 | 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] | 1838 | { |
| 1839 | struct hlua_socket *socket; |
| 1840 | struct hlua *hlua = hlua_gethlua(L); |
| 1841 | struct appctx *appctx; |
| 1842 | size_t buf_len; |
| 1843 | const char *buf; |
| 1844 | int len; |
| 1845 | int send_len; |
| 1846 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1847 | struct xref *peer; |
| 1848 | struct stream_interface *si; |
| 1849 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1850 | |
| 1851 | /* Check if this lua stack is schedulable. */ |
| 1852 | if (!hlua || !hlua->task) |
| 1853 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1854 | "'frontend', 'backend' or 'task'")); |
| 1855 | |
| 1856 | /* Get object */ |
| 1857 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1858 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1859 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1860 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1861 | /* check for connection break. If some data where read, return it. */ |
| 1862 | peer = xref_get_peer(&socket->xref); |
| 1863 | if (!peer) { |
| 1864 | xref_disconnect(&socket->xref); |
| 1865 | lua_pushinteger(L, -1); |
| 1866 | return 1; |
| 1867 | } |
| 1868 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1869 | si = appctx->owner; |
| 1870 | s = si_strm(si); |
| 1871 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1872 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1873 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1874 | lua_pushinteger(L, -1); |
| 1875 | return 1; |
| 1876 | } |
| 1877 | |
| 1878 | /* Update the input buffer data. */ |
| 1879 | buf += sent; |
| 1880 | send_len = buf_len - sent; |
| 1881 | |
| 1882 | /* All the data are sent. */ |
| 1883 | if (sent >= buf_len) |
| 1884 | return 1; /* Implicitly return the length sent. */ |
| 1885 | |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1886 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 1887 | * the request buffer if its not required. |
| 1888 | */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1889 | if (s->req.buf->size == 0) { |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1890 | appctx = hlua->task->context; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1891 | if (!channel_alloc_buffer(&s->req, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1892 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1893 | } |
| 1894 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1895 | /* Check for avalaible space. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1896 | len = buffer_total_space(s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1897 | if (len <= 0) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1898 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1899 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1900 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1901 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 1902 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1903 | |
| 1904 | /* send data */ |
| 1905 | if (len < send_len) |
| 1906 | send_len = len; |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1907 | len = ci_putblk(&s->req, buf+sent, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1908 | |
| 1909 | /* "Not enough space" (-1), "Buffer too little to contain |
| 1910 | * the data" (-2) are not expected because the available length |
| 1911 | * is tested. |
| 1912 | * Other unknown error are also not expected. |
| 1913 | */ |
| 1914 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1915 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1916 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1917 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1918 | MAY_LJMP(hlua_socket_close(L)); |
| 1919 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1920 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1921 | return 1; |
| 1922 | } |
| 1923 | |
| 1924 | /* update buffers. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1925 | stream_int_notify(&s->si[0]); |
| 1926 | stream_int_update_applet(&s->si[0]); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 1927 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1928 | s->req.rex = TICK_ETERNITY; |
| 1929 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1930 | |
| 1931 | /* Update length sent. */ |
| 1932 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1933 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1934 | |
| 1935 | /* All the data buffer is sent ? */ |
| 1936 | if (sent + len >= buf_len) |
| 1937 | return 1; |
| 1938 | |
| 1939 | hlua_socket_write_yield_return: |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1940 | 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] | 1941 | return 0; |
| 1942 | } |
| 1943 | |
| 1944 | /* This function initiate the send of data. It just check the input |
| 1945 | * parameters and push an integer in the Lua stack that contain the |
| 1946 | * amount of data writed in the buffer. This is used by the function |
| 1947 | * "hlua_socket_write_yield" that can yield. |
| 1948 | * |
| 1949 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 1950 | * the associated object. The second is a string buffer. The third is |
| 1951 | * a facultative integer that represents where is the buffer position |
| 1952 | * of the start of the data that can send. The first byte is the |
| 1953 | * position "1". The default value is "1". The fourth argument is a |
| 1954 | * facultative integer that represents where is the buffer position |
| 1955 | * of the end of the data that can send. The default is the last byte. |
| 1956 | */ |
| 1957 | static int hlua_socket_send(struct lua_State *L) |
| 1958 | { |
| 1959 | int i; |
| 1960 | int j; |
| 1961 | const char *buf; |
| 1962 | size_t buf_len; |
| 1963 | |
| 1964 | /* Check number of arguments. */ |
| 1965 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 1966 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 1967 | |
| 1968 | /* Get the string. */ |
| 1969 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 1970 | |
| 1971 | /* Get and check j. */ |
| 1972 | if (lua_gettop(L) == 4) { |
| 1973 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 1974 | if (j < 0) |
| 1975 | j = buf_len + j + 1; |
| 1976 | if (j > buf_len) |
| 1977 | j = buf_len + 1; |
| 1978 | lua_pop(L, 1); |
| 1979 | } |
| 1980 | else |
| 1981 | j = buf_len; |
| 1982 | |
| 1983 | /* Get and check i. */ |
| 1984 | if (lua_gettop(L) == 3) { |
| 1985 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 1986 | if (i < 0) |
| 1987 | i = buf_len + i + 1; |
| 1988 | if (i > buf_len) |
| 1989 | i = buf_len + 1; |
| 1990 | lua_pop(L, 1); |
| 1991 | } else |
| 1992 | i = 1; |
| 1993 | |
| 1994 | /* Check bth i and j. */ |
| 1995 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1996 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1997 | return 1; |
| 1998 | } |
| 1999 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2000 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2001 | return 1; |
| 2002 | } |
| 2003 | if (i == 0) |
| 2004 | i = 1; |
| 2005 | if (j == 0) |
| 2006 | j = 1; |
| 2007 | |
| 2008 | /* Pop the string. */ |
| 2009 | lua_pop(L, 1); |
| 2010 | |
| 2011 | /* Update the buffer length. */ |
| 2012 | buf += i - 1; |
| 2013 | buf_len = j - i + 1; |
| 2014 | lua_pushlstring(L, buf, buf_len); |
| 2015 | |
| 2016 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2017 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2018 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2019 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2020 | } |
| 2021 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2022 | #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] | 2023 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2024 | { |
| 2025 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2026 | int ret; |
| 2027 | int len; |
| 2028 | char *p; |
| 2029 | |
| 2030 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2031 | if (ret <= 0) { |
| 2032 | lua_pushnil(L); |
| 2033 | return 1; |
| 2034 | } |
| 2035 | |
| 2036 | if (ret == AF_UNIX) { |
| 2037 | lua_pushstring(L, buffer+1); |
| 2038 | return 1; |
| 2039 | } |
| 2040 | else if (ret == AF_INET6) { |
| 2041 | buffer[0] = '['; |
| 2042 | len = strlen(buffer); |
| 2043 | buffer[len] = ']'; |
| 2044 | len++; |
| 2045 | buffer[len] = ':'; |
| 2046 | len++; |
| 2047 | p = buffer; |
| 2048 | } |
| 2049 | else if (ret == AF_INET) { |
| 2050 | p = buffer + 1; |
| 2051 | len = strlen(p); |
| 2052 | p[len] = ':'; |
| 2053 | len++; |
| 2054 | } |
| 2055 | else { |
| 2056 | lua_pushnil(L); |
| 2057 | return 1; |
| 2058 | } |
| 2059 | |
| 2060 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2061 | lua_pushnil(L); |
| 2062 | return 1; |
| 2063 | } |
| 2064 | |
| 2065 | lua_pushstring(L, p); |
| 2066 | return 1; |
| 2067 | } |
| 2068 | |
| 2069 | /* Returns information about the peer of the connection. */ |
| 2070 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2071 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2072 | struct hlua_socket *socket; |
| 2073 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2074 | struct xref *peer; |
| 2075 | struct appctx *appctx; |
| 2076 | struct stream_interface *si; |
| 2077 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2078 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2079 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2080 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2081 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2082 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2083 | /* check for connection break. If some data where read, return it. */ |
| 2084 | peer = xref_get_peer(&socket->xref); |
| 2085 | if (!peer) { |
| 2086 | xref_disconnect(&socket->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2087 | lua_pushnil(L); |
| 2088 | return 1; |
| 2089 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2090 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2091 | si = appctx->owner; |
| 2092 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2093 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2094 | conn = objt_conn(s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2095 | if (!conn) { |
| 2096 | lua_pushnil(L); |
| 2097 | return 1; |
| 2098 | } |
| 2099 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2100 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2101 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2102 | lua_pushnil(L); |
| 2103 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | return MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2107 | } |
| 2108 | |
| 2109 | /* Returns information about my connection side. */ |
| 2110 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2111 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2112 | struct hlua_socket *socket; |
| 2113 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2114 | struct appctx *appctx; |
| 2115 | struct xref *peer; |
| 2116 | struct stream_interface *si; |
| 2117 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2118 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2119 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2120 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2121 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2122 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2123 | /* check for connection break. If some data where read, return it. */ |
| 2124 | peer = xref_get_peer(&socket->xref); |
| 2125 | if (!peer) { |
| 2126 | xref_disconnect(&socket->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2127 | lua_pushnil(L); |
| 2128 | return 1; |
| 2129 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2130 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2131 | si = appctx->owner; |
| 2132 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2133 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2134 | conn = objt_conn(s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2135 | if (!conn) { |
| 2136 | lua_pushnil(L); |
| 2137 | return 1; |
| 2138 | } |
| 2139 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2140 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2141 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2142 | lua_pushnil(L); |
| 2143 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2144 | } |
| 2145 | |
| 2146 | return hlua_socket_info(L, &conn->addr.from); |
| 2147 | } |
| 2148 | |
| 2149 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2150 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2151 | .obj_type = OBJ_TYPE_APPLET, |
| 2152 | .name = "<LUA_TCP>", |
| 2153 | .fct = hlua_socket_handler, |
| 2154 | .release = hlua_socket_release, |
| 2155 | }; |
| 2156 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2157 | __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] | 2158 | { |
| 2159 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2160 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2161 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2162 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2163 | struct stream_interface *si; |
| 2164 | struct stream *s; |
| 2165 | |
| 2166 | /* check for connection break. If some data where read, return it. */ |
| 2167 | peer = xref_get_peer(&socket->xref); |
| 2168 | if (!peer) { |
| 2169 | xref_disconnect(&socket->xref); |
| 2170 | lua_pushnil(L); |
| 2171 | lua_pushstring(L, "Can't connect"); |
| 2172 | return 2; |
| 2173 | } |
| 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 | |
| 2178 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2179 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2180 | lua_pushnil(L); |
| 2181 | lua_pushstring(L, "Can't connect"); |
| 2182 | return 2; |
| 2183 | } |
| 2184 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2185 | appctx = objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2186 | |
| 2187 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2188 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2189 | lua_pushinteger(L, 1); |
| 2190 | return 1; |
| 2191 | } |
| 2192 | |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2193 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2194 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2195 | 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] | 2196 | return 0; |
| 2197 | } |
| 2198 | |
| 2199 | /* This function fail or initite the connection. */ |
| 2200 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2201 | { |
| 2202 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2203 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2204 | const char *ip; |
| 2205 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2206 | struct hlua *hlua; |
| 2207 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2208 | int low, high; |
| 2209 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2210 | struct xref *peer; |
| 2211 | struct stream_interface *si; |
| 2212 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2213 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2214 | if (lua_gettop(L) < 2) |
| 2215 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2216 | |
| 2217 | /* Get args. */ |
| 2218 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2219 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2220 | if (lua_gettop(L) >= 3) |
| 2221 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2222 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2223 | /* check for connection break. If some data where read, return it. */ |
| 2224 | peer = xref_get_peer(&socket->xref); |
| 2225 | if (!peer) { |
| 2226 | xref_disconnect(&socket->xref); |
| 2227 | lua_pushnil(L); |
| 2228 | return 1; |
| 2229 | } |
| 2230 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2231 | si = appctx->owner; |
| 2232 | s = si_strm(si); |
| 2233 | |
| 2234 | /* Initialise connection. */ |
| 2235 | conn = si_alloc_conn(&s->si[1]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2236 | if (!conn) |
| 2237 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2238 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2239 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2240 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2241 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2242 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2243 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2244 | if (!addr) |
| 2245 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
| 2246 | if (low != high) |
| 2247 | WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip)); |
| 2248 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2249 | |
| 2250 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2251 | if (low == 0) { |
| 2252 | if (conn->addr.to.ss_family == AF_INET) { |
| 2253 | if (port == -1) |
| 2254 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
| 2255 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2256 | } else if (conn->addr.to.ss_family == AF_INET6) { |
| 2257 | if (port == -1) |
| 2258 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
| 2259 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2260 | } |
| 2261 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2262 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2263 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2264 | appctx = objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2265 | |
| 2266 | /* inform the stream that we want to be notified whenever the |
| 2267 | * connection completes. |
| 2268 | */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2269 | si_applet_cant_get(&s->si[0]); |
| 2270 | si_applet_cant_put(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2271 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2272 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2273 | hlua->flags |= HLUA_MUST_GC; |
| 2274 | |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 2275 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2276 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2277 | 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] | 2278 | |
| 2279 | return 0; |
| 2280 | } |
| 2281 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2282 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2283 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2284 | { |
| 2285 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2286 | struct xref *peer; |
| 2287 | struct appctx *appctx; |
| 2288 | struct stream_interface *si; |
| 2289 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2290 | |
| 2291 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2292 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2293 | |
| 2294 | /* check for connection break. If some data where read, return it. */ |
| 2295 | peer = xref_get_peer(&socket->xref); |
| 2296 | if (!peer) { |
| 2297 | xref_disconnect(&socket->xref); |
| 2298 | lua_pushnil(L); |
| 2299 | return 1; |
| 2300 | } |
| 2301 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2302 | si = appctx->owner; |
| 2303 | s = si_strm(si); |
| 2304 | |
| 2305 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2307 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2308 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2309 | |
| 2310 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2311 | { |
| 2312 | return 0; |
| 2313 | } |
| 2314 | |
| 2315 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2316 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2317 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2318 | int tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2319 | struct xref *peer; |
| 2320 | struct appctx *appctx; |
| 2321 | struct stream_interface *si; |
| 2322 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2323 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2324 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2325 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2326 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2327 | tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2328 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2329 | /* check for connection break. If some data where read, return it. */ |
| 2330 | peer = xref_get_peer(&socket->xref); |
| 2331 | if (!peer) { |
| 2332 | xref_disconnect(&socket->xref); |
| 2333 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2334 | WILL_LJMP(lua_error(L)); |
| 2335 | return 0; |
| 2336 | } |
| 2337 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2338 | si = appctx->owner; |
| 2339 | s = si_strm(si); |
| 2340 | |
| 2341 | s->req.rto = tmout; |
| 2342 | s->req.wto = tmout; |
| 2343 | s->res.rto = tmout; |
| 2344 | s->res.wto = tmout; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2345 | |
| 2346 | return 0; |
| 2347 | } |
| 2348 | |
| 2349 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2350 | { |
| 2351 | struct hlua_socket *socket; |
| 2352 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2353 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2354 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2355 | |
| 2356 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2357 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2358 | hlua_pusherror(L, "socket: full stack"); |
| 2359 | goto out_fail_conf; |
| 2360 | } |
| 2361 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2362 | /* Create the object: obj[0] = userdata. */ |
| 2363 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2364 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2365 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2366 | memset(socket, 0, sizeof(*socket)); |
| 2367 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2368 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2369 | if (!pool2_stream || !pool2_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2370 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2371 | goto out_fail_conf; |
| 2372 | } |
| 2373 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2374 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2375 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2376 | lua_setmetatable(L, -2); |
| 2377 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2378 | /* Create the applet context */ |
| 2379 | appctx = appctx_new(&update_applet); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2380 | if (!appctx) { |
| 2381 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2382 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2383 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2384 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2385 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2386 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2387 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2388 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2389 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2390 | /* Now create a session, task and stream for this applet */ |
| 2391 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2392 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2393 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2394 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2395 | } |
| 2396 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2397 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2398 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2399 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2400 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2401 | } |
| 2402 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2403 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2404 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2405 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2406 | /* Configure "right" stream interface. this "si" is used to connect |
| 2407 | * and retrieve data from the server. The connection is initialized |
| 2408 | * with the "struct server". |
| 2409 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2410 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2411 | |
| 2412 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2413 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2414 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2415 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2416 | task_wakeup(strm->task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2417 | /* Return yield waiting for connection. */ |
| 2418 | return 1; |
| 2419 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2420 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2421 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2422 | out_fail_sess: |
| 2423 | appctx_free(appctx); |
| 2424 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2425 | WILL_LJMP(lua_error(L)); |
| 2426 | return 0; |
| 2427 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2428 | |
| 2429 | /* |
| 2430 | * |
| 2431 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2432 | * Class Channel |
| 2433 | * |
| 2434 | * |
| 2435 | */ |
| 2436 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2437 | /* The state between the channel data and the HTTP parser state can be |
| 2438 | * unconsistent, so reset the parser and call it again. Warning, this |
| 2439 | * action not revalidate the request and not send a 400 if the modified |
| 2440 | * resuest is not valid. |
| 2441 | * |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2442 | * This function never fails. The direction is set using dir, which equals |
| 2443 | * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2444 | */ |
| 2445 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2446 | { |
| 2447 | /* Protocol HTTP. */ |
| 2448 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2449 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2450 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2451 | http_txn_reset_req(stream->txn); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2452 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2453 | http_txn_reset_res(stream->txn); |
| 2454 | |
| 2455 | if (stream->txn->hdr_idx.v) |
| 2456 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2457 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2458 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2459 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2460 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2461 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2462 | } |
| 2463 | } |
| 2464 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2465 | /* This function is called before the Lua execution. It stores |
| 2466 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2467 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2468 | 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] | 2469 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2470 | c->mode = stream->be->mode; |
| 2471 | switch (c->mode) { |
| 2472 | case PR_MODE_HTTP: |
| 2473 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2474 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2475 | c->data.http.state = stream->txn->req.msg_state; |
| 2476 | else |
| 2477 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2478 | break; |
| 2479 | default: |
| 2480 | break; |
| 2481 | } |
| 2482 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2483 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2484 | /* This function is called after the Lua execution. it |
| 2485 | * returns true if the parser state is consistent, otherwise, |
| 2486 | * it return false. |
| 2487 | * |
| 2488 | * In HTTP mode, the parser state must be in the same state |
| 2489 | * or greater when we exit the function. Even if we do a |
| 2490 | * control yield. This prevent to break the HTTP message |
| 2491 | * from the Lua code. |
| 2492 | */ |
| 2493 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2494 | { |
| 2495 | if (c->mode != stream->be->mode) |
| 2496 | return 0; |
| 2497 | |
| 2498 | switch (c->mode) { |
| 2499 | case PR_MODE_HTTP: |
| 2500 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2501 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2502 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2503 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2504 | else |
| 2505 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2506 | default: |
| 2507 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2508 | } |
| 2509 | return 1; |
| 2510 | } |
| 2511 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2512 | /* Returns the struct hlua_channel join to the class channel in the |
| 2513 | * stack entry "ud" or throws an argument error. |
| 2514 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2515 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2516 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2517 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2518 | } |
| 2519 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2520 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2521 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2522 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2523 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2524 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2525 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2526 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2527 | return 0; |
| 2528 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2529 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2530 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2531 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2532 | |
| 2533 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2534 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2535 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2536 | return 1; |
| 2537 | } |
| 2538 | |
| 2539 | /* Duplicate all the data present in the input channel and put it |
| 2540 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2541 | * the stack if the channel is closed and all the data are consumed, |
| 2542 | * returns 0 if no data are available, otherwise it returns the length |
| 2543 | * of the builded string. |
| 2544 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2545 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2546 | { |
| 2547 | char *blk1; |
| 2548 | char *blk2; |
| 2549 | int len1; |
| 2550 | int len2; |
| 2551 | int ret; |
| 2552 | luaL_Buffer b; |
| 2553 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2554 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2555 | if (unlikely(ret == 0)) |
| 2556 | return 0; |
| 2557 | |
| 2558 | if (unlikely(ret < 0)) { |
| 2559 | lua_pushnil(L); |
| 2560 | return -1; |
| 2561 | } |
| 2562 | |
| 2563 | luaL_buffinit(L, &b); |
| 2564 | luaL_addlstring(&b, blk1, len1); |
| 2565 | if (unlikely(ret == 2)) |
| 2566 | luaL_addlstring(&b, blk2, len2); |
| 2567 | luaL_pushresult(&b); |
| 2568 | |
| 2569 | if (unlikely(ret == 2)) |
| 2570 | return len1 + len2; |
| 2571 | return len1; |
| 2572 | } |
| 2573 | |
| 2574 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2575 | * a yield. This function keep the data in the buffer. |
| 2576 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2577 | __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] | 2578 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2579 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2580 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2581 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2582 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2583 | if (_hlua_channel_dup(chn, L) == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2584 | 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] | 2585 | return 1; |
| 2586 | } |
| 2587 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2588 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2589 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2590 | { |
| 2591 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2592 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2593 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2594 | } |
| 2595 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2596 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2597 | * a yield. This function consumes the data in the buffer. It returns |
| 2598 | * a string containing the data or a nil pointer if no data are available |
| 2599 | * and the channel is closed. |
| 2600 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2601 | __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] | 2602 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2603 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2604 | int ret; |
| 2605 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2606 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2607 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2608 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2609 | if (unlikely(ret == 0)) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2610 | 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] | 2611 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2612 | if (unlikely(ret == -1)) |
| 2613 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2614 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2615 | chn->buf->i -= ret; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2616 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2617 | return 1; |
| 2618 | } |
| 2619 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2620 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 2621 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2622 | { |
| 2623 | MAY_LJMP(check_args(L, 1, "get")); |
| 2624 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2625 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2626 | } |
| 2627 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2628 | /* This functions consumes and returns one line. If the channel is closed, |
| 2629 | * and the last data does not contains a final '\n', the data are returned |
| 2630 | * without the final '\n'. When no more data are avalaible, it returns nil |
| 2631 | * value. |
| 2632 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2633 | __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] | 2634 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2635 | char *blk1; |
| 2636 | char *blk2; |
| 2637 | int len1; |
| 2638 | int len2; |
| 2639 | int len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2640 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2641 | int ret; |
| 2642 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2643 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2644 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2645 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2646 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2647 | if (ret == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2648 | 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] | 2649 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2650 | if (ret == -1) { |
| 2651 | lua_pushnil(L); |
| 2652 | return 1; |
| 2653 | } |
| 2654 | |
| 2655 | luaL_buffinit(L, &b); |
| 2656 | luaL_addlstring(&b, blk1, len1); |
| 2657 | len = len1; |
| 2658 | if (unlikely(ret == 2)) { |
| 2659 | luaL_addlstring(&b, blk2, len2); |
| 2660 | len += len2; |
| 2661 | } |
| 2662 | luaL_pushresult(&b); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2663 | 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] | 2664 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2665 | return 1; |
| 2666 | } |
| 2667 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2668 | /* Check arguments for the fucntion "hlua_channel_getline_yield". */ |
| 2669 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2670 | { |
| 2671 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2672 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2673 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2674 | } |
| 2675 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2676 | /* This function takes a string as input, and append it at the |
| 2677 | * input side of channel. If the data is too big, but a space |
| 2678 | * is probably available after sending some data, the function |
| 2679 | * yield. If the data is bigger than the buffer, or if the |
| 2680 | * channel is closed, it returns -1. otherwise, it returns the |
| 2681 | * amount of data writed. |
| 2682 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2683 | __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] | 2684 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2685 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2686 | size_t len; |
| 2687 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2688 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2689 | int ret; |
| 2690 | int max; |
| 2691 | |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2692 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2693 | * the request buffer if its not required. |
| 2694 | */ |
| 2695 | if (chn->buf->size == 0) { |
| 2696 | si_applet_cant_put(chn_prod(chn)); |
| 2697 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
| 2698 | } |
| 2699 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2700 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2701 | if (max > len - l) |
| 2702 | max = len - l; |
| 2703 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2704 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2705 | if (ret == -2 || ret == -3) { |
| 2706 | lua_pushinteger(L, -1); |
| 2707 | return 1; |
| 2708 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2709 | if (ret == -1) { |
| 2710 | chn->flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2711 | 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] | 2712 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2713 | l += ret; |
| 2714 | lua_pop(L, 1); |
| 2715 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2716 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2717 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2718 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
| 2719 | if (max == 0 && chn->buf->o == 0) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2720 | /* There are no space avalaible, and the output buffer is empty. |
| 2721 | * in this case, we cannot add more data, so we cannot yield, |
| 2722 | * we return the amount of copyied data. |
| 2723 | */ |
| 2724 | return 1; |
| 2725 | } |
| 2726 | if (l < len) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2727 | 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] | 2728 | return 1; |
| 2729 | } |
| 2730 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2731 | /* just a wrapper of "hlua_channel_append_yield". It returns the length |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2732 | * of the writed string, or -1 if the channel is closed or if the |
| 2733 | * buffer size is too little for the data. |
| 2734 | */ |
| 2735 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2736 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2737 | size_t len; |
| 2738 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2739 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2740 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2741 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2742 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2743 | lua_pushinteger(L, 0); |
| 2744 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2745 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2746 | } |
| 2747 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2748 | /* just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2749 | * his process by cleaning the buffer. The result is a replacement |
| 2750 | * of the current data. It returns the length of the writed string, |
| 2751 | * or -1 if the channel is closed or if the buffer size is too |
| 2752 | * little for the data. |
| 2753 | */ |
| 2754 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2755 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2756 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2757 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2758 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2759 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2760 | lua_pushinteger(L, 0); |
| 2761 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2762 | chn->buf->i = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2763 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2764 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2765 | } |
| 2766 | |
| 2767 | /* Append data in the output side of the buffer. This data is immediatly |
| 2768 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 2769 | * cannot contains the data, the function yield. The function returns -1 |
| 2770 | * if the channel is closed. |
| 2771 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2772 | __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] | 2773 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2774 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2775 | size_t len; |
| 2776 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2777 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2778 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2779 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2780 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2781 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2782 | lua_pushinteger(L, -1); |
| 2783 | return 1; |
| 2784 | } |
| 2785 | |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2786 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2787 | * the request buffer if its not required. |
| 2788 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2789 | if (chn->buf->size == 0) { |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2790 | si_applet_cant_put(chn_prod(chn)); |
| 2791 | 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] | 2792 | } |
| 2793 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2794 | /* the writed data will be immediatly sent, so we can check |
| 2795 | * the avalaible space without taking in account the reserve. |
| 2796 | * The reserve is guaranted for the processing of incoming |
| 2797 | * data, because the buffer will be flushed. |
| 2798 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2799 | max = chn->buf->size - buffer_len(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2800 | |
| 2801 | /* If there are no space avalaible, and the output buffer is empty. |
| 2802 | * in this case, we cannot add more data, so we cannot yield, |
| 2803 | * we return the amount of copyied data. |
| 2804 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2805 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2806 | return 1; |
| 2807 | |
| 2808 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2809 | if (max > len - l) |
| 2810 | max = len - l; |
| 2811 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2812 | /* The buffer avalaible size may be not contiguous. This test |
| 2813 | * detects a non contiguous buffer and realign it. |
| 2814 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2815 | if (bi_space_for_replace(chn->buf) < max) |
| 2816 | buffer_slow_realign(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2817 | |
| 2818 | /* Copy input data in the buffer. */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2819 | 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] | 2820 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2821 | /* buffer replace considers that the input part is filled. |
| 2822 | * so, I must forward these new data in the output part. |
| 2823 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2824 | b_adv(chn->buf, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2825 | |
| 2826 | l += max; |
| 2827 | lua_pop(L, 1); |
| 2828 | lua_pushinteger(L, l); |
| 2829 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2830 | /* If there are no space avalaible, and the output buffer is empty. |
| 2831 | * in this case, we cannot add more data, so we cannot yield, |
| 2832 | * we return the amount of copyied data. |
| 2833 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2834 | max = chn->buf->size - buffer_len(chn->buf); |
| 2835 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2836 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2837 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2838 | if (l < len) { |
| 2839 | /* If we are waiting for space in the response buffer, we |
| 2840 | * must set the flag WAKERESWR. This flag required the task |
| 2841 | * wake up if any activity is detected on the response buffer. |
| 2842 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2843 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2844 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2845 | else |
| 2846 | HLUA_SET_WAKEREQWR(hlua); |
| 2847 | 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] | 2848 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2849 | |
| 2850 | return 1; |
| 2851 | } |
| 2852 | |
| 2853 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 2854 | * yield the LUA process, and resume it without checking the |
| 2855 | * input arguments. |
| 2856 | */ |
| 2857 | __LJMP static int hlua_channel_send(lua_State *L) |
| 2858 | { |
| 2859 | MAY_LJMP(check_args(L, 2, "send")); |
| 2860 | lua_pushinteger(L, 0); |
| 2861 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2862 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2863 | } |
| 2864 | |
| 2865 | /* This function forward and amount of butes. The data pass from |
| 2866 | * the input side of the buffer to the output side, and can be |
| 2867 | * forwarded. This function never fails. |
| 2868 | * |
| 2869 | * The Lua function takes an amount of bytes to be forwarded in |
| 2870 | * imput. It returns the number of bytes forwarded. |
| 2871 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2872 | __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] | 2873 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2874 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2875 | int len; |
| 2876 | int l; |
| 2877 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2878 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2879 | |
| 2880 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2881 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2882 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 2883 | |
| 2884 | max = len - l; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2885 | if (max > chn->buf->i) |
| 2886 | max = chn->buf->i; |
| 2887 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2888 | l += max; |
| 2889 | |
| 2890 | lua_pop(L, 1); |
| 2891 | lua_pushinteger(L, l); |
| 2892 | |
| 2893 | /* Check if it miss bytes to forward. */ |
| 2894 | if (l < len) { |
| 2895 | /* The the input channel or the output channel are closed, we |
| 2896 | * must return the amount of data forwarded. |
| 2897 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2898 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2899 | return 1; |
| 2900 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2901 | /* If we are waiting for space data in the response buffer, we |
| 2902 | * must set the flag WAKERESWR. This flag required the task |
| 2903 | * wake up if any activity is detected on the response buffer. |
| 2904 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2905 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2906 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2907 | else |
| 2908 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2909 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2910 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2911 | 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] | 2912 | } |
| 2913 | |
| 2914 | return 1; |
| 2915 | } |
| 2916 | |
| 2917 | /* Just check the input and prepare the stack for the previous |
| 2918 | * function "hlua_channel_forward_yield" |
| 2919 | */ |
| 2920 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 2921 | { |
| 2922 | MAY_LJMP(check_args(L, 2, "forward")); |
| 2923 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2924 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2925 | |
| 2926 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2927 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2928 | } |
| 2929 | |
| 2930 | /* Just returns the number of bytes available in the input |
| 2931 | * side of the buffer. This function never fails. |
| 2932 | */ |
| 2933 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 2934 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2935 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2936 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2937 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2938 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2939 | lua_pushinteger(L, chn->buf->i); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2940 | return 1; |
| 2941 | } |
| 2942 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 2943 | /* Returns true if the channel is full. */ |
| 2944 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 2945 | { |
| 2946 | struct channel *chn; |
| 2947 | int rem; |
| 2948 | |
| 2949 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 2950 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2951 | |
| 2952 | rem = chn->buf->size; |
| 2953 | rem -= chn->buf->o; /* Output size */ |
| 2954 | rem -= chn->buf->i; /* Input size */ |
| 2955 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 2956 | |
| 2957 | lua_pushboolean(L, rem <= 0); |
| 2958 | return 1; |
| 2959 | } |
| 2960 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2961 | /* Just returns the number of bytes available in the output |
| 2962 | * side of the buffer. This function never fails. |
| 2963 | */ |
| 2964 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 2965 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2966 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2967 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2968 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2969 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2970 | lua_pushinteger(L, chn->buf->o); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2971 | return 1; |
| 2972 | } |
| 2973 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2974 | /* |
| 2975 | * |
| 2976 | * |
| 2977 | * Class Fetches |
| 2978 | * |
| 2979 | * |
| 2980 | */ |
| 2981 | |
| 2982 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2983 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2984 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2985 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2986 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2987 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2988 | } |
| 2989 | |
| 2990 | /* This function creates and push in the stack a fetch object according |
| 2991 | * with a current TXN. |
| 2992 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 2993 | 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] | 2994 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2995 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2996 | |
| 2997 | /* Check stack size. */ |
| 2998 | if (!lua_checkstack(L, 3)) |
| 2999 | return 0; |
| 3000 | |
| 3001 | /* Create the object: obj[0] = userdata. |
| 3002 | * Note that the base of the Fetches object is the |
| 3003 | * transaction object. |
| 3004 | */ |
| 3005 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3006 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3007 | lua_rawseti(L, -2, 0); |
| 3008 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3009 | hsmp->s = txn->s; |
| 3010 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3011 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3012 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3013 | |
| 3014 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3015 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3016 | lua_setmetatable(L, -2); |
| 3017 | |
| 3018 | return 1; |
| 3019 | } |
| 3020 | |
| 3021 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3022 | * It uses closure argument to store the associated sample-fetch. It |
| 3023 | * returns only one argument or throws an error. An error is thrown |
| 3024 | * only if an error is encountered during the argument parsing. If |
| 3025 | * the "sample-fetch" function fails, nil is returned. |
| 3026 | */ |
| 3027 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3028 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3029 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3030 | struct sample_fetch *f; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3031 | struct arg args[ARGM_NBARGS + 1]; |
| 3032 | int i; |
| 3033 | struct sample smp; |
| 3034 | |
| 3035 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3036 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3037 | |
| 3038 | /* Get traditionnal arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3039 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3040 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3041 | /* Check execution authorization. */ |
| 3042 | if (f->use & SMP_USE_HTTP_ANY && |
| 3043 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3044 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3045 | "is not available in Lua services", f->kw); |
| 3046 | WILL_LJMP(lua_error(L)); |
| 3047 | } |
| 3048 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3049 | /* Get extra arguments. */ |
| 3050 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3051 | if (i >= ARGM_NBARGS) |
| 3052 | break; |
| 3053 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3054 | } |
| 3055 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 3056 | args[i].data.str.str = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3057 | |
| 3058 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3059 | 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] | 3060 | |
| 3061 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3062 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3063 | lua_pushfstring(L, "error in arguments"); |
| 3064 | WILL_LJMP(lua_error(L)); |
| 3065 | } |
| 3066 | |
| 3067 | /* Initialise the sample. */ |
| 3068 | memset(&smp, 0, sizeof(smp)); |
| 3069 | |
| 3070 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3071 | 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] | 3072 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3073 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3074 | lua_pushstring(L, ""); |
| 3075 | else |
| 3076 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3077 | return 1; |
| 3078 | } |
| 3079 | |
| 3080 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3081 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3082 | hlua_smp2lua_str(L, &smp); |
| 3083 | else |
| 3084 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3085 | return 1; |
| 3086 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3087 | |
| 3088 | /* |
| 3089 | * |
| 3090 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3091 | * Class Converters |
| 3092 | * |
| 3093 | * |
| 3094 | */ |
| 3095 | |
| 3096 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3097 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3098 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3099 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3100 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3101 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | /* This function creates and push in the stack a Converters object |
| 3105 | * according with a current TXN. |
| 3106 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3107 | 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] | 3108 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3109 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3110 | |
| 3111 | /* Check stack size. */ |
| 3112 | if (!lua_checkstack(L, 3)) |
| 3113 | return 0; |
| 3114 | |
| 3115 | /* Create the object: obj[0] = userdata. |
| 3116 | * Note that the base of the Converters object is the |
| 3117 | * same than the TXN object. |
| 3118 | */ |
| 3119 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3120 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3121 | lua_rawseti(L, -2, 0); |
| 3122 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3123 | hsmp->s = txn->s; |
| 3124 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3125 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3126 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3127 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3128 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3129 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3130 | lua_setmetatable(L, -2); |
| 3131 | |
| 3132 | return 1; |
| 3133 | } |
| 3134 | |
| 3135 | /* This function is an LUA binding. It is called with each converter. |
| 3136 | * It uses closure argument to store the associated converter. It |
| 3137 | * returns only one argument or throws an error. An error is thrown |
| 3138 | * only if an error is encountered during the argument parsing. If |
| 3139 | * the converter function function fails, nil is returned. |
| 3140 | */ |
| 3141 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3142 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3143 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3144 | struct sample_conv *conv; |
| 3145 | struct arg args[ARGM_NBARGS + 1]; |
| 3146 | int i; |
| 3147 | struct sample smp; |
| 3148 | |
| 3149 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3150 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3151 | |
| 3152 | /* Get traditionnal arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3153 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3154 | |
| 3155 | /* Get extra arguments. */ |
| 3156 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3157 | if (i >= ARGM_NBARGS) |
| 3158 | break; |
| 3159 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3160 | } |
| 3161 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 3162 | args[i].data.str.str = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3163 | |
| 3164 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3165 | 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] | 3166 | |
| 3167 | /* Run the special args checker. */ |
| 3168 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3169 | hlua_pusherror(L, "error in arguments"); |
| 3170 | WILL_LJMP(lua_error(L)); |
| 3171 | } |
| 3172 | |
| 3173 | /* Initialise the sample. */ |
| 3174 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3175 | hlua_pusherror(L, "error in the input argument"); |
| 3176 | WILL_LJMP(lua_error(L)); |
| 3177 | } |
| 3178 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3179 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3180 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3181 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3182 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3183 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3184 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3185 | WILL_LJMP(lua_error(L)); |
| 3186 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3187 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3188 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3189 | hlua_pusherror(L, "error during the input argument casting"); |
| 3190 | WILL_LJMP(lua_error(L)); |
| 3191 | } |
| 3192 | |
| 3193 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3194 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3195 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3196 | lua_pushstring(L, ""); |
| 3197 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3198 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3199 | return 1; |
| 3200 | } |
| 3201 | |
| 3202 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3203 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3204 | hlua_smp2lua_str(L, &smp); |
| 3205 | else |
| 3206 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3207 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3208 | } |
| 3209 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3210 | /* |
| 3211 | * |
| 3212 | * |
| 3213 | * Class AppletTCP |
| 3214 | * |
| 3215 | * |
| 3216 | */ |
| 3217 | |
| 3218 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3219 | * a class stream, otherwise it throws an error. |
| 3220 | */ |
| 3221 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3222 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3223 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3224 | } |
| 3225 | |
| 3226 | /* This function creates and push in the stack an Applet object |
| 3227 | * according with a current TXN. |
| 3228 | */ |
| 3229 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3230 | { |
| 3231 | struct hlua_appctx *appctx; |
| 3232 | struct stream_interface *si = ctx->owner; |
| 3233 | struct stream *s = si_strm(si); |
| 3234 | struct proxy *p = s->be; |
| 3235 | |
| 3236 | /* Check stack size. */ |
| 3237 | if (!lua_checkstack(L, 3)) |
| 3238 | return 0; |
| 3239 | |
| 3240 | /* Create the object: obj[0] = userdata. |
| 3241 | * Note that the base of the Converters object is the |
| 3242 | * same than the TXN object. |
| 3243 | */ |
| 3244 | lua_newtable(L); |
| 3245 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3246 | lua_rawseti(L, -2, 0); |
| 3247 | appctx->appctx = ctx; |
| 3248 | appctx->htxn.s = s; |
| 3249 | appctx->htxn.p = p; |
| 3250 | |
| 3251 | /* Create the "f" field that contains a list of fetches. */ |
| 3252 | lua_pushstring(L, "f"); |
| 3253 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3254 | return 0; |
| 3255 | lua_settable(L, -3); |
| 3256 | |
| 3257 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3258 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3259 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3260 | return 0; |
| 3261 | lua_settable(L, -3); |
| 3262 | |
| 3263 | /* Create the "c" field that contains a list of converters. */ |
| 3264 | lua_pushstring(L, "c"); |
| 3265 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3266 | return 0; |
| 3267 | lua_settable(L, -3); |
| 3268 | |
| 3269 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3270 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3271 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3272 | return 0; |
| 3273 | lua_settable(L, -3); |
| 3274 | |
| 3275 | /* Pop a class stream metatable and affect it to the table. */ |
| 3276 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3277 | lua_setmetatable(L, -2); |
| 3278 | |
| 3279 | return 1; |
| 3280 | } |
| 3281 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3282 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3283 | { |
| 3284 | struct hlua_appctx *appctx; |
| 3285 | struct stream *s; |
| 3286 | const char *name; |
| 3287 | size_t len; |
| 3288 | struct sample smp; |
| 3289 | |
| 3290 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3291 | |
| 3292 | /* It is useles to retrieve the stream, but this function |
| 3293 | * runs only in a stream context. |
| 3294 | */ |
| 3295 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3296 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3297 | s = appctx->htxn.s; |
| 3298 | |
| 3299 | /* Converts the third argument in a sample. */ |
| 3300 | hlua_lua2smp(L, 3, &smp); |
| 3301 | |
| 3302 | /* Store the sample in a variable. */ |
| 3303 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3304 | vars_set_by_name(name, len, &smp); |
| 3305 | return 0; |
| 3306 | } |
| 3307 | |
| 3308 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3309 | { |
| 3310 | struct hlua_appctx *appctx; |
| 3311 | struct stream *s; |
| 3312 | const char *name; |
| 3313 | size_t len; |
| 3314 | struct sample smp; |
| 3315 | |
| 3316 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3317 | |
| 3318 | /* It is useles to retrieve the stream, but this function |
| 3319 | * runs only in a stream context. |
| 3320 | */ |
| 3321 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3322 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3323 | s = appctx->htxn.s; |
| 3324 | |
| 3325 | /* Unset the variable. */ |
| 3326 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3327 | vars_unset_by_name(name, len, &smp); |
| 3328 | return 0; |
| 3329 | } |
| 3330 | |
| 3331 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3332 | { |
| 3333 | struct hlua_appctx *appctx; |
| 3334 | struct stream *s; |
| 3335 | const char *name; |
| 3336 | size_t len; |
| 3337 | struct sample smp; |
| 3338 | |
| 3339 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3340 | |
| 3341 | /* It is useles to retrieve the stream, but this function |
| 3342 | * runs only in a stream context. |
| 3343 | */ |
| 3344 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3345 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3346 | s = appctx->htxn.s; |
| 3347 | |
| 3348 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3349 | if (!vars_get_by_name(name, len, &smp)) { |
| 3350 | lua_pushnil(L); |
| 3351 | return 1; |
| 3352 | } |
| 3353 | |
| 3354 | return hlua_smp2lua(L, &smp); |
| 3355 | } |
| 3356 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3357 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3358 | { |
| 3359 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3360 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3361 | struct hlua *hlua; |
| 3362 | |
| 3363 | /* 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] | 3364 | if (!s->hlua) |
| 3365 | return 0; |
| 3366 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3367 | |
| 3368 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3369 | |
| 3370 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3371 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3372 | |
| 3373 | /* Get and store new value. */ |
| 3374 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3375 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3376 | |
| 3377 | return 0; |
| 3378 | } |
| 3379 | |
| 3380 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3381 | { |
| 3382 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3383 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3384 | struct hlua *hlua; |
| 3385 | |
| 3386 | /* 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] | 3387 | if (!s->hlua) { |
| 3388 | lua_pushnil(L); |
| 3389 | return 1; |
| 3390 | } |
| 3391 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3392 | |
| 3393 | /* Push configuration index in the stack. */ |
| 3394 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3395 | |
| 3396 | return 1; |
| 3397 | } |
| 3398 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3399 | /* If expected data not yet available, it returns a yield. This function |
| 3400 | * consumes the data in the buffer. It returns a string containing the |
| 3401 | * data. This string can be empty. |
| 3402 | */ |
| 3403 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3404 | { |
| 3405 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3406 | struct stream_interface *si = appctx->appctx->owner; |
| 3407 | int ret; |
| 3408 | char *blk1; |
| 3409 | int len1; |
| 3410 | char *blk2; |
| 3411 | int len2; |
| 3412 | |
| 3413 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3414 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3415 | |
| 3416 | /* Data not yet avalaible. return yield. */ |
| 3417 | if (ret == 0) { |
| 3418 | si_applet_cant_get(si); |
| 3419 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
| 3420 | } |
| 3421 | |
| 3422 | /* End of data: commit the total strings and return. */ |
| 3423 | if (ret < 0) { |
| 3424 | luaL_pushresult(&appctx->b); |
| 3425 | return 1; |
| 3426 | } |
| 3427 | |
| 3428 | /* Ensure that the block 2 length is usable. */ |
| 3429 | if (ret == 1) |
| 3430 | len2 = 0; |
| 3431 | |
| 3432 | /* dont check the max length read and dont check. */ |
| 3433 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3434 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3435 | |
| 3436 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3437 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3438 | luaL_pushresult(&appctx->b); |
| 3439 | return 1; |
| 3440 | } |
| 3441 | |
| 3442 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3443 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3444 | { |
| 3445 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3446 | |
| 3447 | /* Initialise the string catenation. */ |
| 3448 | luaL_buffinit(L, &appctx->b); |
| 3449 | |
| 3450 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3451 | } |
| 3452 | |
| 3453 | /* If expected data not yet available, it returns a yield. This function |
| 3454 | * consumes the data in the buffer. It returns a string containing the |
| 3455 | * data. This string can be empty. |
| 3456 | */ |
| 3457 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3458 | { |
| 3459 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3460 | struct stream_interface *si = appctx->appctx->owner; |
| 3461 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3462 | int ret; |
| 3463 | char *blk1; |
| 3464 | int len1; |
| 3465 | char *blk2; |
| 3466 | int len2; |
| 3467 | |
| 3468 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3469 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3470 | |
| 3471 | /* Data not yet avalaible. return yield. */ |
| 3472 | if (ret == 0) { |
| 3473 | si_applet_cant_get(si); |
| 3474 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3475 | } |
| 3476 | |
| 3477 | /* End of data: commit the total strings and return. */ |
| 3478 | if (ret < 0) { |
| 3479 | luaL_pushresult(&appctx->b); |
| 3480 | return 1; |
| 3481 | } |
| 3482 | |
| 3483 | /* Ensure that the block 2 length is usable. */ |
| 3484 | if (ret == 1) |
| 3485 | len2 = 0; |
| 3486 | |
| 3487 | if (len == -1) { |
| 3488 | |
| 3489 | /* If len == -1, catenate all the data avalaile and |
| 3490 | * yield because we want to get all the data until |
| 3491 | * the end of data stream. |
| 3492 | */ |
| 3493 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3494 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3495 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3496 | si_applet_cant_get(si); |
| 3497 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3498 | |
| 3499 | } else { |
| 3500 | |
| 3501 | /* Copy the fisrt block caping to the length required. */ |
| 3502 | if (len1 > len) |
| 3503 | len1 = len; |
| 3504 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3505 | len -= len1; |
| 3506 | |
| 3507 | /* Copy the second block. */ |
| 3508 | if (len2 > len) |
| 3509 | len2 = len; |
| 3510 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3511 | len -= len2; |
| 3512 | |
| 3513 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3514 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3515 | |
| 3516 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3517 | if (len > 0) { |
| 3518 | lua_pushinteger(L, len); |
| 3519 | lua_replace(L, 2); |
| 3520 | si_applet_cant_get(si); |
| 3521 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3522 | } |
| 3523 | |
| 3524 | /* return the result. */ |
| 3525 | luaL_pushresult(&appctx->b); |
| 3526 | return 1; |
| 3527 | } |
| 3528 | |
| 3529 | /* we never executes this */ |
| 3530 | hlua_pusherror(L, "Lua: internal error"); |
| 3531 | WILL_LJMP(lua_error(L)); |
| 3532 | return 0; |
| 3533 | } |
| 3534 | |
| 3535 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3536 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3537 | { |
| 3538 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3539 | int len = -1; |
| 3540 | |
| 3541 | if (lua_gettop(L) > 2) |
| 3542 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3543 | if (lua_gettop(L) >= 2) { |
| 3544 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3545 | lua_pop(L, 1); |
| 3546 | } |
| 3547 | |
| 3548 | /* Confirm or set the required length */ |
| 3549 | lua_pushinteger(L, len); |
| 3550 | |
| 3551 | /* Initialise the string catenation. */ |
| 3552 | luaL_buffinit(L, &appctx->b); |
| 3553 | |
| 3554 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3555 | } |
| 3556 | |
| 3557 | /* Append data in the output side of the buffer. This data is immediatly |
| 3558 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3559 | * cannot contains the data, the function yield. The function returns -1 |
| 3560 | * if the channel is closed. |
| 3561 | */ |
| 3562 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3563 | { |
| 3564 | size_t len; |
| 3565 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3566 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3567 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3568 | struct stream_interface *si = appctx->appctx->owner; |
| 3569 | struct channel *chn = si_ic(si); |
| 3570 | int max; |
| 3571 | |
| 3572 | /* Get the max amount of data which can write as input in the channel. */ |
| 3573 | max = channel_recv_max(chn); |
| 3574 | if (max > (len - l)) |
| 3575 | max = len - l; |
| 3576 | |
| 3577 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3578 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3579 | |
| 3580 | /* update counters. */ |
| 3581 | l += max; |
| 3582 | lua_pop(L, 1); |
| 3583 | lua_pushinteger(L, l); |
| 3584 | |
| 3585 | /* If some data is not send, declares the situation to the |
| 3586 | * applet, and returns a yield. |
| 3587 | */ |
| 3588 | if (l < len) { |
| 3589 | si_applet_cant_put(si); |
| 3590 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
| 3591 | } |
| 3592 | |
| 3593 | return 1; |
| 3594 | } |
| 3595 | |
| 3596 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3597 | * yield the LUA process, and resume it without checking the |
| 3598 | * input arguments. |
| 3599 | */ |
| 3600 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3601 | { |
| 3602 | MAY_LJMP(check_args(L, 2, "send")); |
| 3603 | lua_pushinteger(L, 0); |
| 3604 | |
| 3605 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3606 | } |
| 3607 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3608 | /* |
| 3609 | * |
| 3610 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3611 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3612 | * |
| 3613 | * |
| 3614 | */ |
| 3615 | |
| 3616 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3617 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3618 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3619 | __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] | 3620 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3621 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3622 | } |
| 3623 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3624 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3625 | * according with a current TXN. |
| 3626 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3627 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3628 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3629 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3630 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3631 | struct stream_interface *si = ctx->owner; |
| 3632 | struct stream *s = si_strm(si); |
| 3633 | struct proxy *px = s->be; |
| 3634 | struct http_txn *txn = s->txn; |
| 3635 | const char *path; |
| 3636 | const char *end; |
| 3637 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3638 | |
| 3639 | /* Check stack size. */ |
| 3640 | if (!lua_checkstack(L, 3)) |
| 3641 | return 0; |
| 3642 | |
| 3643 | /* Create the object: obj[0] = userdata. |
| 3644 | * Note that the base of the Converters object is the |
| 3645 | * same than the TXN object. |
| 3646 | */ |
| 3647 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3648 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3649 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3650 | appctx->appctx = ctx; |
| 3651 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3652 | 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] | 3653 | appctx->htxn.s = s; |
| 3654 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3655 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3656 | /* Create the "f" field that contains a list of fetches. */ |
| 3657 | lua_pushstring(L, "f"); |
| 3658 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3659 | return 0; |
| 3660 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3661 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3662 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3663 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3664 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3665 | return 0; |
| 3666 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3667 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3668 | /* Create the "c" field that contains a list of converters. */ |
| 3669 | lua_pushstring(L, "c"); |
| 3670 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3671 | return 0; |
| 3672 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3673 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3674 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3675 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3676 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3677 | return 0; |
| 3678 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3679 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3680 | /* Stores the request method. */ |
| 3681 | lua_pushstring(L, "method"); |
| 3682 | lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l); |
| 3683 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3684 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3685 | /* Stores the http version. */ |
| 3686 | lua_pushstring(L, "version"); |
| 3687 | lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
| 3688 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3689 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3690 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3691 | * the array on the top of the stack. |
| 3692 | */ |
| 3693 | lua_pushstring(L, "headers"); |
| 3694 | htxn.s = s; |
| 3695 | htxn.p = px; |
| 3696 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3697 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3698 | return 0; |
| 3699 | lua_settable(L, -3); |
| 3700 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3701 | /* Get path and qs */ |
| 3702 | path = http_get_path(txn); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3703 | if (path) { |
| 3704 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 3705 | p = path; |
| 3706 | while (p < end && *p != '?') |
| 3707 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3708 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3709 | /* Stores the request path. */ |
| 3710 | lua_pushstring(L, "path"); |
| 3711 | lua_pushlstring(L, path, p - path); |
| 3712 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3713 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3714 | /* Stores the query string. */ |
| 3715 | lua_pushstring(L, "qs"); |
| 3716 | if (*p == '?') |
| 3717 | p++; |
| 3718 | lua_pushlstring(L, p, end - p); |
| 3719 | lua_settable(L, -3); |
| 3720 | } |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3721 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3722 | /* Stores the request path. */ |
| 3723 | lua_pushstring(L, "length"); |
| 3724 | lua_pushinteger(L, txn->req.body_len); |
| 3725 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3726 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3727 | /* Create an array of HTTP request headers. */ |
| 3728 | lua_pushstring(L, "headers"); |
| 3729 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3730 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3731 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3732 | /* Create an empty array of HTTP request headers. */ |
| 3733 | lua_pushstring(L, "response"); |
| 3734 | lua_newtable(L); |
| 3735 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3736 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3737 | /* Pop a class stream metatable and affect it to the table. */ |
| 3738 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3739 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3740 | |
| 3741 | return 1; |
| 3742 | } |
| 3743 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3744 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3745 | { |
| 3746 | struct hlua_appctx *appctx; |
| 3747 | struct stream *s; |
| 3748 | const char *name; |
| 3749 | size_t len; |
| 3750 | struct sample smp; |
| 3751 | |
| 3752 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3753 | |
| 3754 | /* It is useles to retrieve the stream, but this function |
| 3755 | * runs only in a stream context. |
| 3756 | */ |
| 3757 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3758 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3759 | s = appctx->htxn.s; |
| 3760 | |
| 3761 | /* Converts the third argument in a sample. */ |
| 3762 | hlua_lua2smp(L, 3, &smp); |
| 3763 | |
| 3764 | /* Store the sample in a variable. */ |
| 3765 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3766 | vars_set_by_name(name, len, &smp); |
| 3767 | return 0; |
| 3768 | } |
| 3769 | |
| 3770 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 3771 | { |
| 3772 | struct hlua_appctx *appctx; |
| 3773 | struct stream *s; |
| 3774 | const char *name; |
| 3775 | size_t len; |
| 3776 | struct sample smp; |
| 3777 | |
| 3778 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3779 | |
| 3780 | /* It is useles to retrieve the stream, but this function |
| 3781 | * runs only in a stream context. |
| 3782 | */ |
| 3783 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3784 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3785 | s = appctx->htxn.s; |
| 3786 | |
| 3787 | /* Unset the variable. */ |
| 3788 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3789 | vars_unset_by_name(name, len, &smp); |
| 3790 | return 0; |
| 3791 | } |
| 3792 | |
| 3793 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 3794 | { |
| 3795 | struct hlua_appctx *appctx; |
| 3796 | struct stream *s; |
| 3797 | const char *name; |
| 3798 | size_t len; |
| 3799 | struct sample smp; |
| 3800 | |
| 3801 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3802 | |
| 3803 | /* It is useles to retrieve the stream, but this function |
| 3804 | * runs only in a stream context. |
| 3805 | */ |
| 3806 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3807 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3808 | s = appctx->htxn.s; |
| 3809 | |
| 3810 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3811 | if (!vars_get_by_name(name, len, &smp)) { |
| 3812 | lua_pushnil(L); |
| 3813 | return 1; |
| 3814 | } |
| 3815 | |
| 3816 | return hlua_smp2lua(L, &smp); |
| 3817 | } |
| 3818 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3819 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 3820 | { |
| 3821 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3822 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3823 | struct hlua *hlua; |
| 3824 | |
| 3825 | /* 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] | 3826 | if (!s->hlua) |
| 3827 | return 0; |
| 3828 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3829 | |
| 3830 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3831 | |
| 3832 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3833 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3834 | |
| 3835 | /* Get and store new value. */ |
| 3836 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3837 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3838 | |
| 3839 | return 0; |
| 3840 | } |
| 3841 | |
| 3842 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 3843 | { |
| 3844 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3845 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3846 | struct hlua *hlua; |
| 3847 | |
| 3848 | /* 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] | 3849 | if (!s->hlua) { |
| 3850 | lua_pushnil(L); |
| 3851 | return 1; |
| 3852 | } |
| 3853 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3854 | |
| 3855 | /* Push configuration index in the stack. */ |
| 3856 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3857 | |
| 3858 | return 1; |
| 3859 | } |
| 3860 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3861 | /* If expected data not yet available, it returns a yield. This function |
| 3862 | * consumes the data in the buffer. It returns a string containing the |
| 3863 | * data. This string can be empty. |
| 3864 | */ |
| 3865 | __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] | 3866 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3867 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3868 | struct stream_interface *si = appctx->appctx->owner; |
| 3869 | struct channel *chn = si_ic(si); |
| 3870 | int ret; |
| 3871 | char *blk1; |
| 3872 | int len1; |
| 3873 | char *blk2; |
| 3874 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3875 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3876 | /* Maybe we cant send a 100-continue ? */ |
| 3877 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3878 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3879 | /* if ret == -2 or -3 the channel closed or the message si too |
| 3880 | * big for the buffers. We cant send anything. So, we ignoring |
| 3881 | * the error, considers that the 100-continue is sent, and try |
| 3882 | * to receive. |
| 3883 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 3884 | */ |
| 3885 | if (ret == -1) { |
| 3886 | si_applet_cant_put(si); |
| 3887 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 3888 | } |
| 3889 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 3890 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3891 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3892 | /* Check for the end of the data. */ |
| 3893 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 3894 | luaL_pushresult(&appctx->b); |
| 3895 | return 1; |
| 3896 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3897 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3898 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3899 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3900 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3901 | /* Data not yet avalaible. return yield. */ |
| 3902 | if (ret == 0) { |
| 3903 | si_applet_cant_get(si); |
| 3904 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 3905 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3906 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3907 | /* End of data: commit the total strings and return. */ |
| 3908 | if (ret < 0) { |
| 3909 | luaL_pushresult(&appctx->b); |
| 3910 | return 1; |
| 3911 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3912 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3913 | /* Ensure that the block 2 length is usable. */ |
| 3914 | if (ret == 1) |
| 3915 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3916 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3917 | /* Copy the fisrt block caping to the length required. */ |
| 3918 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 3919 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 3920 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3921 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3922 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3923 | /* Copy the second block. */ |
| 3924 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 3925 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 3926 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3927 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3928 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3929 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3930 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3931 | luaL_pushresult(&appctx->b); |
| 3932 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3933 | } |
| 3934 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3935 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3936 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3937 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3938 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3939 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3940 | /* Initialise the string catenation. */ |
| 3941 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3942 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3943 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3944 | } |
| 3945 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3946 | /* If expected data not yet available, it returns a yield. This function |
| 3947 | * consumes the data in the buffer. It returns a string containing the |
| 3948 | * data. This string can be empty. |
| 3949 | */ |
| 3950 | __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] | 3951 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3952 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3953 | struct stream_interface *si = appctx->appctx->owner; |
| 3954 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3955 | struct channel *chn = si_ic(si); |
| 3956 | int ret; |
| 3957 | char *blk1; |
| 3958 | int len1; |
| 3959 | char *blk2; |
| 3960 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3961 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3962 | /* Maybe we cant send a 100-continue ? */ |
| 3963 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3964 | ret = ci_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3965 | /* if ret == -2 or -3 the channel closed or the message si too |
| 3966 | * big for the buffers. We cant send anything. So, we ignoring |
| 3967 | * the error, considers that the 100-continue is sent, and try |
| 3968 | * to receive. |
| 3969 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 3970 | */ |
| 3971 | if (ret == -1) { |
| 3972 | si_applet_cant_put(si); |
| 3973 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 3974 | } |
| 3975 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 3976 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3977 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3978 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3979 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3980 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3981 | /* Data not yet avalaible. return yield. */ |
| 3982 | if (ret == 0) { |
| 3983 | si_applet_cant_get(si); |
| 3984 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 3985 | } |
| 3986 | |
| 3987 | /* End of data: commit the total strings and return. */ |
| 3988 | if (ret < 0) { |
| 3989 | luaL_pushresult(&appctx->b); |
| 3990 | return 1; |
| 3991 | } |
| 3992 | |
| 3993 | /* Ensure that the block 2 length is usable. */ |
| 3994 | if (ret == 1) |
| 3995 | len2 = 0; |
| 3996 | |
| 3997 | /* Copy the fisrt block caping to the length required. */ |
| 3998 | if (len1 > len) |
| 3999 | len1 = len; |
| 4000 | luaL_addlstring(&appctx->b, blk1, len1); |
| 4001 | len -= len1; |
| 4002 | |
| 4003 | /* Copy the second block. */ |
| 4004 | if (len2 > len) |
| 4005 | len2 = len; |
| 4006 | luaL_addlstring(&appctx->b, blk2, len2); |
| 4007 | len -= len2; |
| 4008 | |
| 4009 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4010 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4011 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 4012 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 4013 | |
| 4014 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 4015 | if (len > 0) { |
| 4016 | lua_pushinteger(L, len); |
| 4017 | lua_replace(L, 2); |
| 4018 | si_applet_cant_get(si); |
| 4019 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 4020 | } |
| 4021 | |
| 4022 | /* return the result. */ |
| 4023 | luaL_pushresult(&appctx->b); |
| 4024 | return 1; |
| 4025 | } |
| 4026 | |
| 4027 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 4028 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4029 | { |
| 4030 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4031 | int len = -1; |
| 4032 | |
| 4033 | /* Check arguments. */ |
| 4034 | if (lua_gettop(L) > 2) |
| 4035 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4036 | if (lua_gettop(L) >= 2) { |
| 4037 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4038 | lua_pop(L, 1); |
| 4039 | } |
| 4040 | |
| 4041 | /* Check the required length */ |
| 4042 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 4043 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 4044 | lua_pushinteger(L, len); |
| 4045 | |
| 4046 | /* Initialise the string catenation. */ |
| 4047 | luaL_buffinit(L, &appctx->b); |
| 4048 | |
| 4049 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 4050 | } |
| 4051 | |
| 4052 | /* Append data in the output side of the buffer. This data is immediatly |
| 4053 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 4054 | * cannot contains the data, the function yield. The function returns -1 |
| 4055 | * if the channel is closed. |
| 4056 | */ |
| 4057 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4058 | { |
| 4059 | size_t len; |
| 4060 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4061 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4062 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4063 | struct stream_interface *si = appctx->appctx->owner; |
| 4064 | struct channel *chn = si_ic(si); |
| 4065 | int max; |
| 4066 | |
| 4067 | /* Get the max amount of data which can write as input in the channel. */ |
| 4068 | max = channel_recv_max(chn); |
| 4069 | if (max > (len - l)) |
| 4070 | max = len - l; |
| 4071 | |
| 4072 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4073 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4074 | |
| 4075 | /* update counters. */ |
| 4076 | l += max; |
| 4077 | lua_pop(L, 1); |
| 4078 | lua_pushinteger(L, l); |
| 4079 | |
| 4080 | /* If some data is not send, declares the situation to the |
| 4081 | * applet, and returns a yield. |
| 4082 | */ |
| 4083 | if (l < len) { |
| 4084 | si_applet_cant_put(si); |
| 4085 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
| 4086 | } |
| 4087 | |
| 4088 | return 1; |
| 4089 | } |
| 4090 | |
| 4091 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4092 | * yield the LUA process, and resume it without checking the |
| 4093 | * input arguments. |
| 4094 | */ |
| 4095 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4096 | { |
| 4097 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4098 | size_t len; |
| 4099 | char hex[10]; |
| 4100 | |
| 4101 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4102 | |
| 4103 | /* If transfer encoding chunked is selected, we surround the data |
| 4104 | * by chunk data. |
| 4105 | */ |
| 4106 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 4107 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 4108 | lua_pushfstring(L, "%s\r\n", hex); |
| 4109 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 4110 | lua_pushstring(L, "\r\n"); |
| 4111 | lua_concat(L, 3); |
| 4112 | } |
| 4113 | |
| 4114 | /* This interger is used for followinf the amount of data sent. */ |
| 4115 | lua_pushinteger(L, 0); |
| 4116 | |
| 4117 | /* We want to send some data. Headers must be sent. */ |
| 4118 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4119 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4120 | WILL_LJMP(lua_error(L)); |
| 4121 | } |
| 4122 | |
| 4123 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 4124 | } |
| 4125 | |
| 4126 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4127 | { |
| 4128 | const char *name; |
| 4129 | int ret; |
| 4130 | |
| 4131 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4132 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4133 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4134 | |
| 4135 | /* Push in the stack the "response" entry. */ |
| 4136 | ret = lua_getfield(L, 1, "response"); |
| 4137 | if (ret != LUA_TTABLE) { |
| 4138 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4139 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4140 | WILL_LJMP(lua_error(L)); |
| 4141 | } |
| 4142 | |
| 4143 | /* check if the header is already registered if it is not |
| 4144 | * the case, register it. |
| 4145 | */ |
| 4146 | ret = lua_getfield(L, -1, name); |
| 4147 | if (ret == LUA_TNIL) { |
| 4148 | |
| 4149 | /* Entry not found. */ |
| 4150 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4151 | |
| 4152 | /* Insert the new header name in the array in the top of the stack. |
| 4153 | * It left the new array in the top of the stack. |
| 4154 | */ |
| 4155 | lua_newtable(L); |
| 4156 | lua_pushvalue(L, 2); |
| 4157 | lua_pushvalue(L, -2); |
| 4158 | lua_settable(L, -4); |
| 4159 | |
| 4160 | } else if (ret != LUA_TTABLE) { |
| 4161 | |
| 4162 | /* corruption error. */ |
| 4163 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4164 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4165 | WILL_LJMP(lua_error(L)); |
| 4166 | } |
| 4167 | |
| 4168 | /* Now the top od thestack is an array of values. We push |
| 4169 | * the header value as new entry. |
| 4170 | */ |
| 4171 | lua_pushvalue(L, 3); |
| 4172 | ret = lua_rawlen(L, -2); |
| 4173 | lua_rawseti(L, -2, ret + 1); |
| 4174 | lua_pushboolean(L, 1); |
| 4175 | return 1; |
| 4176 | } |
| 4177 | |
| 4178 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4179 | { |
| 4180 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4181 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4182 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4183 | |
| 4184 | if (status < 100 || status > 599) { |
| 4185 | lua_pushboolean(L, 0); |
| 4186 | return 1; |
| 4187 | } |
| 4188 | |
| 4189 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4190 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4191 | lua_pushboolean(L, 1); |
| 4192 | return 1; |
| 4193 | } |
| 4194 | |
| 4195 | /* We will build the status line and the headers of the HTTP response. |
| 4196 | * We will try send at once if its not possible, we give back the hand |
| 4197 | * waiting for more room. |
| 4198 | */ |
| 4199 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 4200 | { |
| 4201 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4202 | struct stream_interface *si = appctx->appctx->owner; |
| 4203 | struct channel *chn = si_ic(si); |
| 4204 | int ret; |
| 4205 | size_t len; |
| 4206 | const char *msg; |
| 4207 | |
| 4208 | /* Get the message as the first argument on the stack. */ |
| 4209 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4210 | |
| 4211 | /* Send the message at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4212 | ret = ci_putblk(chn, msg, len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4213 | |
| 4214 | /* if ret == -2 or -3 the channel closed or the message si too |
| 4215 | * big for the buffers. |
| 4216 | */ |
| 4217 | if (ret == -2 || ret == -3) { |
| 4218 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4219 | WILL_LJMP(lua_error(L)); |
| 4220 | } |
| 4221 | |
| 4222 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 4223 | if (ret == -1) { |
| 4224 | si_applet_cant_put(si); |
| 4225 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
| 4226 | } |
| 4227 | |
| 4228 | /* Headers sent, set the flag. */ |
| 4229 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4230 | return 0; |
| 4231 | } |
| 4232 | |
| 4233 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 4234 | { |
| 4235 | struct chunk *tmp = get_trash_chunk(); |
| 4236 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4237 | const char *name; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4238 | size_t name_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4239 | const char *value; |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4240 | size_t value_len; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4241 | int id; |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4242 | long long hdr_contentlength = -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4243 | int hdr_chunked = 0; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4244 | const char *reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4245 | |
| 4246 | if (reason == NULL) |
| 4247 | reason = get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4248 | |
| 4249 | /* Use the same http version than the request. */ |
| 4250 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4251 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4252 | appctx->appctx->ctx.hlua_apphttp.status, |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4253 | reason); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4254 | |
| 4255 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4256 | lua_pushvalue(L, 0); |
| 4257 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4258 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4259 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4260 | WILL_LJMP(lua_error(L)); |
| 4261 | } |
| 4262 | |
| 4263 | /* Browse the list of headers. */ |
| 4264 | lua_pushnil(L); |
| 4265 | while(lua_next(L, -2) != 0) { |
| 4266 | |
| 4267 | /* We expect a string as -2. */ |
| 4268 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4269 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4270 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4271 | lua_typename(L, lua_type(L, -2))); |
| 4272 | WILL_LJMP(lua_error(L)); |
| 4273 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4274 | name = lua_tolstring(L, -2, &name_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4275 | |
| 4276 | /* We expect an array as -1. */ |
| 4277 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4278 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4279 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4280 | name, |
| 4281 | lua_typename(L, lua_type(L, -1))); |
| 4282 | WILL_LJMP(lua_error(L)); |
| 4283 | } |
| 4284 | |
| 4285 | /* Browse the table who is on the top of the stack. */ |
| 4286 | lua_pushnil(L); |
| 4287 | while(lua_next(L, -2) != 0) { |
| 4288 | |
| 4289 | /* We expect a number as -2. */ |
| 4290 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4291 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4292 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4293 | name, |
| 4294 | lua_typename(L, lua_type(L, -2))); |
| 4295 | WILL_LJMP(lua_error(L)); |
| 4296 | } |
| 4297 | id = lua_tointeger(L, -2); |
| 4298 | |
| 4299 | /* We expect a string as -2. */ |
| 4300 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4301 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4302 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4303 | name, id, |
| 4304 | lua_typename(L, lua_type(L, -1))); |
| 4305 | WILL_LJMP(lua_error(L)); |
| 4306 | } |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4307 | value = lua_tolstring(L, -1, &value_len); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4308 | |
| 4309 | /* Catenate a new header. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4310 | if (tmp->len + name_len + 2 + value_len + 2 < tmp->size) { |
| 4311 | memcpy(tmp->str + tmp->len, name, name_len); |
| 4312 | tmp->len += name_len; |
| 4313 | tmp->str[tmp->len++] = ':'; |
| 4314 | tmp->str[tmp->len++] = ' '; |
| 4315 | |
| 4316 | memcpy(tmp->str + tmp->len, value, value_len); |
| 4317 | tmp->len += value_len; |
| 4318 | tmp->str[tmp->len++] = '\r'; |
| 4319 | tmp->str[tmp->len++] = '\n'; |
| 4320 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4321 | |
| 4322 | /* Protocol checks. */ |
| 4323 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4324 | /* Copy the header content length. The length conversion |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4325 | * is done without control. If it contains a bad value, |
| 4326 | * the content-length remains negative so that we can |
| 4327 | * switch to either chunked encoding or close. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4328 | */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4329 | if (name_len == 14 && strcasecmp("content-length", name) == 0) |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4330 | strl2llrc(value, strlen(value), &hdr_contentlength); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4331 | |
| 4332 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
Willy Tarreau | a329463 | 2017-08-23 11:24:47 +0200 | [diff] [blame] | 4333 | if (name_len == 17 && value_len == 7 && |
| 4334 | strcasecmp("transfer-encoding", name) == 0 && |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4335 | strcasecmp("chunked", value) == 0) |
| 4336 | hdr_chunked = 1; |
| 4337 | |
| 4338 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4339 | lua_pop(L, 1); |
| 4340 | } |
| 4341 | |
| 4342 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4343 | lua_pop(L, 1); |
| 4344 | } |
| 4345 | |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4346 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4347 | * and the status code implies the presence of a message body, we must |
| 4348 | * announce a transfer encoding chunked. This is required by haproxy |
| 4349 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4350 | * chunked itslef, don't do anything. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4351 | */ |
Willy Tarreau | c9f4ea0 | 2017-08-23 09:32:06 +0200 | [diff] [blame] | 4352 | if (hdr_contentlength < 0 && hdr_chunked == 0 && |
Willy Tarreau | 06c75fe | 2017-08-23 09:10:38 +0200 | [diff] [blame] | 4353 | (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) && |
| 4354 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4355 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4356 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4357 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4358 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4359 | } |
| 4360 | |
| 4361 | /* Finalize headers. */ |
| 4362 | chunk_appendf(tmp, "\r\n"); |
| 4363 | |
| 4364 | /* Remove the last entry and the array of headers */ |
| 4365 | lua_pop(L, 2); |
| 4366 | |
| 4367 | /* Push the headers block. */ |
| 4368 | lua_pushlstring(L, tmp->str, tmp->len); |
| 4369 | |
| 4370 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4371 | } |
| 4372 | |
| 4373 | /* |
| 4374 | * |
| 4375 | * |
| 4376 | * Class HTTP |
| 4377 | * |
| 4378 | * |
| 4379 | */ |
| 4380 | |
| 4381 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4382 | * a class stream, otherwise it throws an error. |
| 4383 | */ |
| 4384 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4385 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4386 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4387 | } |
| 4388 | |
| 4389 | /* This function creates and push in the stack a HTTP object |
| 4390 | * according with a current TXN. |
| 4391 | */ |
| 4392 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4393 | { |
| 4394 | struct hlua_txn *htxn; |
| 4395 | |
| 4396 | /* Check stack size. */ |
| 4397 | if (!lua_checkstack(L, 3)) |
| 4398 | return 0; |
| 4399 | |
| 4400 | /* Create the object: obj[0] = userdata. |
| 4401 | * Note that the base of the Converters object is the |
| 4402 | * same than the TXN object. |
| 4403 | */ |
| 4404 | lua_newtable(L); |
| 4405 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4406 | lua_rawseti(L, -2, 0); |
| 4407 | |
| 4408 | htxn->s = txn->s; |
| 4409 | htxn->p = txn->p; |
| 4410 | |
| 4411 | /* Pop a class stream metatable and affect it to the table. */ |
| 4412 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4413 | lua_setmetatable(L, -2); |
| 4414 | |
| 4415 | return 1; |
| 4416 | } |
| 4417 | |
| 4418 | /* This function creates ans returns an array of HTTP headers. |
| 4419 | * This function does not fails. It is used as wrapper with the |
| 4420 | * 2 following functions. |
| 4421 | */ |
| 4422 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4423 | { |
| 4424 | const char *cur_ptr, *cur_next, *p; |
| 4425 | int old_idx, cur_idx; |
| 4426 | struct hdr_idx_elem *cur_hdr; |
| 4427 | const char *hn, *hv; |
| 4428 | int hnl, hvl; |
| 4429 | int type; |
| 4430 | const char *in; |
| 4431 | char *out; |
| 4432 | int len; |
| 4433 | |
| 4434 | /* Create the table. */ |
| 4435 | lua_newtable(L); |
| 4436 | |
| 4437 | if (!htxn->s->txn) |
| 4438 | return 1; |
| 4439 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4440 | /* Check if a valid response is parsed */ |
| 4441 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4442 | return 1; |
| 4443 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4444 | /* Build array of headers. */ |
| 4445 | old_idx = 0; |
| 4446 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 4447 | |
| 4448 | while (1) { |
| 4449 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4450 | if (!cur_idx) |
| 4451 | break; |
| 4452 | old_idx = cur_idx; |
| 4453 | |
| 4454 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4455 | cur_ptr = cur_next; |
| 4456 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4457 | |
| 4458 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4459 | * and the next header starts at cur_next. We'll check |
| 4460 | * this header in the list as well as against the default |
| 4461 | * rule. |
| 4462 | */ |
| 4463 | |
| 4464 | /* look for ': *'. */ |
| 4465 | hn = cur_ptr; |
| 4466 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4467 | if (p >= cur_ptr+cur_hdr->len) |
| 4468 | continue; |
| 4469 | hnl = p - hn; |
| 4470 | p++; |
| 4471 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4472 | p++; |
| 4473 | if (p >= cur_ptr+cur_hdr->len) |
| 4474 | continue; |
| 4475 | hv = p; |
| 4476 | hvl = cur_ptr+cur_hdr->len-p; |
| 4477 | |
| 4478 | /* Lowercase the key. Don't check the size of trash, it have |
| 4479 | * the size of one buffer and the input data contains in one |
| 4480 | * buffer. |
| 4481 | */ |
| 4482 | out = trash.str; |
| 4483 | for (in=hn; in<hn+hnl; in++, out++) |
| 4484 | *out = tolower(*in); |
| 4485 | *out = '\0'; |
| 4486 | |
| 4487 | /* Check for existing entry: |
| 4488 | * assume that the table is on the top of the stack, and |
| 4489 | * push the key in the stack, the function lua_gettable() |
| 4490 | * perform the lookup. |
| 4491 | */ |
| 4492 | lua_pushlstring(L, trash.str, hnl); |
| 4493 | lua_gettable(L, -2); |
| 4494 | type = lua_type(L, -1); |
| 4495 | |
| 4496 | switch (type) { |
| 4497 | case LUA_TNIL: |
| 4498 | /* Table not found, create it. */ |
| 4499 | lua_pop(L, 1); /* remove the nil value. */ |
| 4500 | lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */ |
| 4501 | lua_newtable(L); /* create and push empty table. */ |
| 4502 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4503 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4504 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4505 | break; |
| 4506 | |
| 4507 | case LUA_TTABLE: |
| 4508 | /* Entry found: push the value in the table. */ |
| 4509 | len = lua_rawlen(L, -1); |
| 4510 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4511 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4512 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4513 | break; |
| 4514 | |
| 4515 | default: |
| 4516 | /* Other cases are errors. */ |
| 4517 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4518 | WILL_LJMP(lua_error(L)); |
| 4519 | } |
| 4520 | } |
| 4521 | |
| 4522 | return 1; |
| 4523 | } |
| 4524 | |
| 4525 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4526 | { |
| 4527 | struct hlua_txn *htxn; |
| 4528 | |
| 4529 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4530 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4531 | |
| 4532 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4533 | } |
| 4534 | |
| 4535 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4536 | { |
| 4537 | struct hlua_txn *htxn; |
| 4538 | |
| 4539 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4540 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4541 | |
| 4542 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4543 | } |
| 4544 | |
| 4545 | /* This function replace full header, or just a value in |
| 4546 | * the request or in the response. It is a wrapper fir the |
| 4547 | * 4 following functions. |
| 4548 | */ |
| 4549 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4550 | struct http_msg *msg, int action) |
| 4551 | { |
| 4552 | size_t name_len; |
| 4553 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4554 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4555 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4556 | struct my_regex re; |
| 4557 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4558 | /* Check if a valid response is parsed */ |
| 4559 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4560 | return 0; |
| 4561 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4562 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4563 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4564 | |
| 4565 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4566 | regex_free(&re); |
| 4567 | return 0; |
| 4568 | } |
| 4569 | |
| 4570 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4571 | { |
| 4572 | struct hlua_txn *htxn; |
| 4573 | |
| 4574 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4575 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4576 | |
| 4577 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4578 | } |
| 4579 | |
| 4580 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4581 | { |
| 4582 | struct hlua_txn *htxn; |
| 4583 | |
| 4584 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4585 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4586 | |
| 4587 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4588 | } |
| 4589 | |
| 4590 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4591 | { |
| 4592 | struct hlua_txn *htxn; |
| 4593 | |
| 4594 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4595 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4596 | |
| 4597 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4598 | } |
| 4599 | |
| 4600 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4601 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4602 | struct hlua_txn *htxn; |
| 4603 | |
| 4604 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4605 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4606 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4607 | 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] | 4608 | } |
| 4609 | |
| 4610 | /* This function deletes all the occurences of an header. |
| 4611 | * It is a wrapper for the 2 following functions. |
| 4612 | */ |
| 4613 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4614 | { |
| 4615 | size_t len; |
| 4616 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4617 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4618 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4619 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4620 | /* Check if a valid response is parsed */ |
| 4621 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4622 | return 0; |
| 4623 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4624 | ctx.idx = 0; |
| 4625 | while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) |
| 4626 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4627 | return 0; |
| 4628 | } |
| 4629 | |
| 4630 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4631 | { |
| 4632 | struct hlua_txn *htxn; |
| 4633 | |
| 4634 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4635 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4636 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4637 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4638 | } |
| 4639 | |
| 4640 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4641 | { |
| 4642 | struct hlua_txn *htxn; |
| 4643 | |
| 4644 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4645 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4646 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4647 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4648 | } |
| 4649 | |
| 4650 | /* This function adds an header. It is a wrapper used by |
| 4651 | * the 2 following functions. |
| 4652 | */ |
| 4653 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4654 | { |
| 4655 | size_t name_len; |
| 4656 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4657 | size_t value_len; |
| 4658 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4659 | char *p; |
| 4660 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4661 | /* Check if a valid message is parsed */ |
| 4662 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4663 | return 0; |
| 4664 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4665 | /* Check length. */ |
| 4666 | trash.len = value_len + name_len + 2; |
| 4667 | if (trash.len > trash.size) |
| 4668 | return 0; |
| 4669 | |
| 4670 | /* Creates the header string. */ |
| 4671 | p = trash.str; |
| 4672 | memcpy(p, name, name_len); |
| 4673 | p += name_len; |
| 4674 | *p = ':'; |
| 4675 | p++; |
| 4676 | *p = ' '; |
| 4677 | p++; |
| 4678 | memcpy(p, value, value_len); |
| 4679 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4680 | 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] | 4681 | trash.str, trash.len) != 0); |
| 4682 | |
| 4683 | return 0; |
| 4684 | } |
| 4685 | |
| 4686 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4687 | { |
| 4688 | struct hlua_txn *htxn; |
| 4689 | |
| 4690 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4691 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4692 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4693 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4694 | } |
| 4695 | |
| 4696 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4697 | { |
| 4698 | struct hlua_txn *htxn; |
| 4699 | |
| 4700 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4701 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4702 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4703 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4704 | } |
| 4705 | |
| 4706 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4707 | { |
| 4708 | struct hlua_txn *htxn; |
| 4709 | |
| 4710 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4711 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4712 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4713 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4714 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4715 | } |
| 4716 | |
| 4717 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4718 | { |
| 4719 | struct hlua_txn *htxn; |
| 4720 | |
| 4721 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4722 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4723 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4724 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4725 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4726 | } |
| 4727 | |
| 4728 | /* This function set the method. */ |
| 4729 | static int hlua_http_req_set_meth(lua_State *L) |
| 4730 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4731 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4732 | size_t name_len; |
| 4733 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4734 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4735 | /* Check if a valid request is parsed */ |
| 4736 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4737 | lua_pushboolean(L, 0); |
| 4738 | return 1; |
| 4739 | } |
| 4740 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4741 | 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] | 4742 | return 1; |
| 4743 | } |
| 4744 | |
| 4745 | /* This function set the method. */ |
| 4746 | static int hlua_http_req_set_path(lua_State *L) |
| 4747 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4748 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4749 | size_t name_len; |
| 4750 | 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] | 4751 | |
| 4752 | /* Check if a valid request is parsed */ |
| 4753 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4754 | lua_pushboolean(L, 0); |
| 4755 | return 1; |
| 4756 | } |
| 4757 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4758 | 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] | 4759 | return 1; |
| 4760 | } |
| 4761 | |
| 4762 | /* This function set the query-string. */ |
| 4763 | static int hlua_http_req_set_query(lua_State *L) |
| 4764 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4765 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4766 | size_t name_len; |
| 4767 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4768 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4769 | /* Check if a valid request is parsed */ |
| 4770 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4771 | lua_pushboolean(L, 0); |
| 4772 | return 1; |
| 4773 | } |
| 4774 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4775 | /* Check length. */ |
| 4776 | if (name_len > trash.size - 1) { |
| 4777 | lua_pushboolean(L, 0); |
| 4778 | return 1; |
| 4779 | } |
| 4780 | |
| 4781 | /* Add the mark question as prefix. */ |
| 4782 | chunk_reset(&trash); |
| 4783 | trash.str[trash.len++] = '?'; |
| 4784 | memcpy(trash.str + trash.len, name, name_len); |
| 4785 | trash.len += name_len; |
| 4786 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4787 | 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] | 4788 | return 1; |
| 4789 | } |
| 4790 | |
| 4791 | /* This function set the uri. */ |
| 4792 | static int hlua_http_req_set_uri(lua_State *L) |
| 4793 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4794 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4795 | size_t name_len; |
| 4796 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4797 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4798 | /* Check if a valid request is parsed */ |
| 4799 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4800 | lua_pushboolean(L, 0); |
| 4801 | return 1; |
| 4802 | } |
| 4803 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4804 | 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] | 4805 | return 1; |
| 4806 | } |
| 4807 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4808 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4809 | static int hlua_http_res_set_status(lua_State *L) |
| 4810 | { |
| 4811 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4812 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4813 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4814 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4815 | /* Check if a valid response is parsed */ |
| 4816 | if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4817 | return 0; |
| 4818 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4819 | http_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4820 | return 0; |
| 4821 | } |
| 4822 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4823 | /* |
| 4824 | * |
| 4825 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4826 | * Class TXN |
| 4827 | * |
| 4828 | * |
| 4829 | */ |
| 4830 | |
| 4831 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4832 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4833 | */ |
| 4834 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 4835 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4836 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4837 | } |
| 4838 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4839 | __LJMP static int hlua_set_var(lua_State *L) |
| 4840 | { |
| 4841 | struct hlua_txn *htxn; |
| 4842 | const char *name; |
| 4843 | size_t len; |
| 4844 | struct sample smp; |
| 4845 | |
| 4846 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4847 | |
| 4848 | /* It is useles to retrieve the stream, but this function |
| 4849 | * runs only in a stream context. |
| 4850 | */ |
| 4851 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4852 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4853 | |
| 4854 | /* Converts the third argument in a sample. */ |
| 4855 | hlua_lua2smp(L, 3, &smp); |
| 4856 | |
| 4857 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 4858 | 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] | 4859 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4860 | return 0; |
| 4861 | } |
| 4862 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 4863 | __LJMP static int hlua_unset_var(lua_State *L) |
| 4864 | { |
| 4865 | struct hlua_txn *htxn; |
| 4866 | const char *name; |
| 4867 | size_t len; |
| 4868 | struct sample smp; |
| 4869 | |
| 4870 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4871 | |
| 4872 | /* It is useles to retrieve the stream, but this function |
| 4873 | * runs only in a stream context. |
| 4874 | */ |
| 4875 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4876 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4877 | |
| 4878 | /* Unset the variable. */ |
| 4879 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 4880 | vars_unset_by_name(name, len, &smp); |
| 4881 | return 0; |
| 4882 | } |
| 4883 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4884 | __LJMP static int hlua_get_var(lua_State *L) |
| 4885 | { |
| 4886 | struct hlua_txn *htxn; |
| 4887 | const char *name; |
| 4888 | size_t len; |
| 4889 | struct sample smp; |
| 4890 | |
| 4891 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4892 | |
| 4893 | /* It is useles to retrieve the stream, but this function |
| 4894 | * runs only in a stream context. |
| 4895 | */ |
| 4896 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4897 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4898 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 4899 | 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] | 4900 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4901 | lua_pushnil(L); |
| 4902 | return 1; |
| 4903 | } |
| 4904 | |
| 4905 | return hlua_smp2lua(L, &smp); |
| 4906 | } |
| 4907 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4908 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4909 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4910 | struct hlua *hlua; |
| 4911 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4912 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4913 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4914 | /* It is useles to retrieve the stream, but this function |
| 4915 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4916 | */ |
| 4917 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4918 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4919 | |
| 4920 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4921 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4922 | |
| 4923 | /* Get and store new value. */ |
| 4924 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4925 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4926 | |
| 4927 | return 0; |
| 4928 | } |
| 4929 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4930 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4931 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4932 | struct hlua *hlua; |
| 4933 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4934 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 4935 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4936 | /* It is useles to retrieve the stream, but this function |
| 4937 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4938 | */ |
| 4939 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4940 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4941 | |
| 4942 | /* Push configuration index in the stack. */ |
| 4943 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4944 | |
| 4945 | return 1; |
| 4946 | } |
| 4947 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4948 | /* Create stack entry containing a class TXN. This function |
| 4949 | * return 0 if the stack does not contains free slots, |
| 4950 | * otherwise it returns 1. |
| 4951 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 4952 | 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] | 4953 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4954 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4955 | |
| 4956 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4957 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4958 | return 0; |
| 4959 | |
| 4960 | /* NOTE: The allocation never fails. The failure |
| 4961 | * throw an error, and the function never returns. |
| 4962 | * if the throw is not avalaible, the process is aborted. |
| 4963 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4964 | /* Create the object: obj[0] = userdata. */ |
| 4965 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4966 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4967 | lua_rawseti(L, -2, 0); |
| 4968 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4969 | htxn->s = s; |
| 4970 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4971 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 4972 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4973 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4974 | /* Create the "f" field that contains a list of fetches. */ |
| 4975 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4976 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4977 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4978 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4979 | |
| 4980 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4981 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4982 | 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] | 4983 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4984 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4985 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4986 | /* Create the "c" field that contains a list of converters. */ |
| 4987 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4988 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4989 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4990 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4991 | |
| 4992 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4993 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4994 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4995 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4996 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4997 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4998 | /* Create the "req" field that contains the request channel object. */ |
| 4999 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5000 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5001 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5002 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5003 | |
| 5004 | /* Create the "res" field that contains the response channel object. */ |
| 5005 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5006 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5007 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5008 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5009 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5010 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5011 | lua_pushstring(L, "http"); |
| 5012 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5013 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5014 | return 0; |
| 5015 | } |
| 5016 | else |
| 5017 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5018 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5019 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5020 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5021 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5022 | lua_setmetatable(L, -2); |
| 5023 | |
| 5024 | return 1; |
| 5025 | } |
| 5026 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5027 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5028 | { |
| 5029 | const char *msg; |
| 5030 | struct hlua_txn *htxn; |
| 5031 | |
| 5032 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5033 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5034 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5035 | |
| 5036 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5037 | return 0; |
| 5038 | } |
| 5039 | |
| 5040 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5041 | { |
| 5042 | int level; |
| 5043 | const char *msg; |
| 5044 | struct hlua_txn *htxn; |
| 5045 | |
| 5046 | MAY_LJMP(check_args(L, 3, "log")); |
| 5047 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5048 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5049 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5050 | |
| 5051 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5052 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5053 | |
| 5054 | hlua_sendlog(htxn->s->be, level, msg); |
| 5055 | return 0; |
| 5056 | } |
| 5057 | |
| 5058 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5059 | { |
| 5060 | const char *msg; |
| 5061 | struct hlua_txn *htxn; |
| 5062 | |
| 5063 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5064 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5065 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5066 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5067 | return 0; |
| 5068 | } |
| 5069 | |
| 5070 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5071 | { |
| 5072 | const char *msg; |
| 5073 | struct hlua_txn *htxn; |
| 5074 | |
| 5075 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5076 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5077 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5078 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5079 | return 0; |
| 5080 | } |
| 5081 | |
| 5082 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5083 | { |
| 5084 | const char *msg; |
| 5085 | struct hlua_txn *htxn; |
| 5086 | |
| 5087 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5088 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5089 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5090 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5091 | return 0; |
| 5092 | } |
| 5093 | |
| 5094 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5095 | { |
| 5096 | const char *msg; |
| 5097 | struct hlua_txn *htxn; |
| 5098 | |
| 5099 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5100 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5101 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5102 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5103 | return 0; |
| 5104 | } |
| 5105 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5106 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5107 | { |
| 5108 | struct hlua_txn *htxn; |
| 5109 | int ll; |
| 5110 | |
| 5111 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5112 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5113 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5114 | |
| 5115 | if (ll < 0 || ll > 7) |
| 5116 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5117 | |
| 5118 | htxn->s->logs.level = ll; |
| 5119 | return 0; |
| 5120 | } |
| 5121 | |
| 5122 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5123 | { |
| 5124 | struct hlua_txn *htxn; |
| 5125 | struct connection *cli_conn; |
| 5126 | int tos; |
| 5127 | |
| 5128 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5129 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5130 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5131 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5132 | 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] | 5133 | inet_set_tos(cli_conn->handle.fd, &cli_conn->addr.from, tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5134 | |
| 5135 | return 0; |
| 5136 | } |
| 5137 | |
| 5138 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5139 | { |
| 5140 | #ifdef SO_MARK |
| 5141 | struct hlua_txn *htxn; |
| 5142 | struct connection *cli_conn; |
| 5143 | int mark; |
| 5144 | |
| 5145 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5146 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5147 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5148 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 5149 | 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] | 5150 | setsockopt(cli_conn->handle.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5151 | #endif |
| 5152 | return 0; |
| 5153 | } |
| 5154 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5155 | /* This function is an Lua binding that send pending data |
| 5156 | * to the client, and close the stream interface. |
| 5157 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5158 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5159 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5160 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5161 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5162 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5163 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5164 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5165 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5166 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5167 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5168 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5169 | * session, so we just end the execution of the current |
| 5170 | * lua code. |
| 5171 | */ |
| 5172 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5173 | WILL_LJMP(hlua_done(L)); |
| 5174 | return 0; |
| 5175 | } |
| 5176 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5177 | ic = &htxn->s->req; |
| 5178 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5179 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5180 | if (htxn->s->txn) { |
| 5181 | /* HTTP mode, let's stay in sync with the stream */ |
| 5182 | bi_fast_delete(ic->buf, htxn->s->txn->req.sov); |
| 5183 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 5184 | htxn->s->txn->req.sov = 0; |
| 5185 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 5186 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 5187 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 5188 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 5189 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 5190 | /* Note that if we want to support keep-alive, we need |
| 5191 | * to bypass the close/shutr_now calls below, but that |
| 5192 | * may only be done if the HTTP request was already |
| 5193 | * processed and the connection header is known (ie |
| 5194 | * not during TCP rules). |
| 5195 | */ |
| 5196 | } |
| 5197 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5198 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5199 | channel_abort(ic); |
| 5200 | channel_auto_close(ic); |
| 5201 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5202 | |
| 5203 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5204 | channel_auto_read(oc); |
| 5205 | channel_auto_close(oc); |
| 5206 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5207 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 5208 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5209 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5210 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5211 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5212 | return 0; |
| 5213 | } |
| 5214 | |
| 5215 | __LJMP static int hlua_log(lua_State *L) |
| 5216 | { |
| 5217 | int level; |
| 5218 | const char *msg; |
| 5219 | |
| 5220 | MAY_LJMP(check_args(L, 2, "log")); |
| 5221 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5222 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5223 | |
| 5224 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5225 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5226 | |
| 5227 | hlua_sendlog(NULL, level, msg); |
| 5228 | return 0; |
| 5229 | } |
| 5230 | |
| 5231 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5232 | { |
| 5233 | const char *msg; |
| 5234 | |
| 5235 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5236 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5237 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5238 | return 0; |
| 5239 | } |
| 5240 | |
| 5241 | __LJMP static int hlua_log_info(lua_State *L) |
| 5242 | { |
| 5243 | const char *msg; |
| 5244 | |
| 5245 | MAY_LJMP(check_args(L, 1, "info")); |
| 5246 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5247 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5248 | return 0; |
| 5249 | } |
| 5250 | |
| 5251 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5252 | { |
| 5253 | const char *msg; |
| 5254 | |
| 5255 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5256 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5257 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5258 | return 0; |
| 5259 | } |
| 5260 | |
| 5261 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5262 | { |
| 5263 | const char *msg; |
| 5264 | |
| 5265 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5266 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5267 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5268 | return 0; |
| 5269 | } |
| 5270 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5271 | __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] | 5272 | { |
| 5273 | int wakeup_ms = lua_tointeger(L, -1); |
| 5274 | if (now_ms < wakeup_ms) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5275 | 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] | 5276 | return 0; |
| 5277 | } |
| 5278 | |
| 5279 | __LJMP static int hlua_sleep(lua_State *L) |
| 5280 | { |
| 5281 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5282 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5283 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5284 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5285 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5286 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5287 | wakeup_ms = tick_add(now_ms, delay); |
| 5288 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5289 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5290 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5291 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5292 | } |
| 5293 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5294 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5295 | { |
| 5296 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5297 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5298 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5299 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5300 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5301 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5302 | wakeup_ms = tick_add(now_ms, delay); |
| 5303 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5304 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5305 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5306 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5307 | } |
| 5308 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5309 | /* This functionis an LUA binding. it permits to give back |
| 5310 | * the hand at the HAProxy scheduler. It is used when the |
| 5311 | * LUA processing consumes a lot of time. |
| 5312 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5313 | __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] | 5314 | { |
| 5315 | return 0; |
| 5316 | } |
| 5317 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5318 | __LJMP static int hlua_yield(lua_State *L) |
| 5319 | { |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5320 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 5321 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5322 | } |
| 5323 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5324 | /* This function change the nice of the currently executed |
| 5325 | * task. It is used set low or high priority at the current |
| 5326 | * task. |
| 5327 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5328 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5329 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5330 | struct hlua *hlua; |
| 5331 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5332 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5333 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5334 | hlua = hlua_gethlua(L); |
| 5335 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5336 | |
| 5337 | /* If he task is not set, I'm in a start mode. */ |
| 5338 | if (!hlua || !hlua->task) |
| 5339 | return 0; |
| 5340 | |
| 5341 | if (nice < -1024) |
| 5342 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5343 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5344 | nice = 1024; |
| 5345 | |
| 5346 | hlua->task->nice = nice; |
| 5347 | return 0; |
| 5348 | } |
| 5349 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5350 | /* This function is used as a calback of a task. It is called by the |
| 5351 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5352 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5353 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5354 | * |
| 5355 | * Task wrapper are longjmp safe because the only one Lua code |
| 5356 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5357 | */ |
| 5358 | static struct task *hlua_process_task(struct task *task) |
| 5359 | { |
| 5360 | struct hlua *hlua = task->context; |
| 5361 | enum hlua_exec status; |
| 5362 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5363 | /* If it is the first call to the task, we must initialize the |
| 5364 | * execution timeouts. |
| 5365 | */ |
| 5366 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5367 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5368 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5369 | /* Execute the Lua code. */ |
| 5370 | status = hlua_ctx_resume(hlua, 1); |
| 5371 | |
| 5372 | switch (status) { |
| 5373 | /* finished or yield */ |
| 5374 | case HLUA_E_OK: |
| 5375 | hlua_ctx_destroy(hlua); |
| 5376 | task_delete(task); |
| 5377 | task_free(task); |
| 5378 | break; |
| 5379 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5380 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
| 5381 | if (hlua->wake_time != TICK_ETERNITY) |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5382 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5383 | break; |
| 5384 | |
| 5385 | /* finished with error. */ |
| 5386 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5387 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5388 | hlua_ctx_destroy(hlua); |
| 5389 | task_delete(task); |
| 5390 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5391 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5392 | break; |
| 5393 | |
| 5394 | case HLUA_E_ERR: |
| 5395 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5396 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5397 | hlua_ctx_destroy(hlua); |
| 5398 | task_delete(task); |
| 5399 | task_free(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5400 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5401 | break; |
| 5402 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5403 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5404 | } |
| 5405 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5406 | /* This function is an LUA binding that register LUA function to be |
| 5407 | * executed after the HAProxy configuration parsing and before the |
| 5408 | * HAProxy scheduler starts. This function expect only one LUA |
| 5409 | * argument that is a function. This function returns nothing, but |
| 5410 | * throws if an error is encountered. |
| 5411 | */ |
| 5412 | __LJMP static int hlua_register_init(lua_State *L) |
| 5413 | { |
| 5414 | struct hlua_init_function *init; |
| 5415 | int ref; |
| 5416 | |
| 5417 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5418 | |
| 5419 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5420 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5421 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5422 | if (!init) |
| 5423 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5424 | |
| 5425 | init->function_ref = ref; |
| 5426 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5427 | return 0; |
| 5428 | } |
| 5429 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5430 | /* This functio is an LUA binding. It permits to register a task |
| 5431 | * executed in parallel of the main HAroxy activity. The task is |
| 5432 | * created and it is set in the HAProxy scheduler. It can be called |
| 5433 | * from the "init" section, "post init" or during the runtime. |
| 5434 | * |
| 5435 | * Lua prototype: |
| 5436 | * |
| 5437 | * <none> core.register_task(<function>) |
| 5438 | */ |
| 5439 | static int hlua_register_task(lua_State *L) |
| 5440 | { |
| 5441 | struct hlua *hlua; |
| 5442 | struct task *task; |
| 5443 | int ref; |
| 5444 | |
| 5445 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5446 | |
| 5447 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5448 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5449 | hlua = pool_alloc2(pool2_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5450 | if (!hlua) |
| 5451 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5452 | |
| 5453 | task = task_new(); |
| 5454 | task->context = hlua; |
| 5455 | task->process = hlua_process_task; |
| 5456 | |
| 5457 | if (!hlua_ctx_init(hlua, task)) |
| 5458 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5459 | |
| 5460 | /* Restore the function in the stack. */ |
| 5461 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5462 | hlua->nargs = 0; |
| 5463 | |
| 5464 | /* Schedule task. */ |
| 5465 | task_schedule(task, now_ms); |
| 5466 | |
| 5467 | return 0; |
| 5468 | } |
| 5469 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5470 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5471 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5472 | * resume converters. |
| 5473 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5474 | 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] | 5475 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5476 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5477 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5478 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5479 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5480 | if (!stream) |
| 5481 | return 0; |
| 5482 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5483 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5484 | * Lua context can be not initialized. This behavior |
| 5485 | * permits to save performances because a systematic |
| 5486 | * Lua initialization cause 5% performances loss. |
| 5487 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5488 | if (!stream->hlua) { |
| 5489 | stream->hlua = pool_alloc2(pool2_hlua); |
| 5490 | if (!stream->hlua) { |
| 5491 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5492 | return 0; |
| 5493 | } |
| 5494 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5495 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5496 | return 0; |
| 5497 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5498 | } |
| 5499 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5500 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5501 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5502 | |
| 5503 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5504 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5505 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5506 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5507 | else |
| 5508 | error = "critical error"; |
| 5509 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5510 | return 0; |
| 5511 | } |
| 5512 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5513 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5514 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5515 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5516 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5517 | return 0; |
| 5518 | } |
| 5519 | |
| 5520 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5521 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5522 | |
| 5523 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5524 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5525 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5526 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5527 | return 0; |
| 5528 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5529 | hlua_smp2lua(stream->hlua->T, smp); |
| 5530 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5531 | |
| 5532 | /* push keywords in the stack. */ |
| 5533 | if (arg_p) { |
| 5534 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5535 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5536 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5537 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5538 | return 0; |
| 5539 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5540 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5541 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5542 | } |
| 5543 | } |
| 5544 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5545 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5546 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5547 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5548 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5549 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5550 | } |
| 5551 | |
| 5552 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5553 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5554 | /* finished. */ |
| 5555 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5556 | /* If the stack is empty, the function fails. */ |
| 5557 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5558 | return 0; |
| 5559 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5560 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5561 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5562 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5563 | return 1; |
| 5564 | |
| 5565 | /* yield. */ |
| 5566 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5567 | 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] | 5568 | return 0; |
| 5569 | |
| 5570 | /* finished with error. */ |
| 5571 | case HLUA_E_ERRMSG: |
| 5572 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5573 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5574 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5575 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5576 | return 0; |
| 5577 | |
| 5578 | case HLUA_E_ERR: |
| 5579 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5580 | 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] | 5581 | |
| 5582 | default: |
| 5583 | return 0; |
| 5584 | } |
| 5585 | } |
| 5586 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5587 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5588 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5589 | * resume sample-fetches. This function will be called by the sample |
| 5590 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5591 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5592 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5593 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5594 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5595 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5596 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5597 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5598 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5599 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5600 | if (!stream) |
| 5601 | return 0; |
| 5602 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5603 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5604 | * Lua context can be not initialized. This behavior |
| 5605 | * permits to save performances because a systematic |
| 5606 | * Lua initialization cause 5% performances loss. |
| 5607 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5608 | if (!stream->hlua) { |
| 5609 | stream->hlua = pool_alloc2(pool2_hlua); |
| 5610 | if (!stream->hlua) { |
| 5611 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5612 | return 0; |
| 5613 | } |
| 5614 | if (!hlua_ctx_init(stream->hlua, stream->task)) { |
| 5615 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5616 | return 0; |
| 5617 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5618 | } |
| 5619 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5620 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5621 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5622 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5623 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5624 | |
| 5625 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5626 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5627 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5628 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5629 | else |
| 5630 | error = "critical error"; |
| 5631 | 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] | 5632 | return 0; |
| 5633 | } |
| 5634 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5635 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5636 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5637 | 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] | 5638 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5639 | return 0; |
| 5640 | } |
| 5641 | |
| 5642 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5643 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5644 | |
| 5645 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5646 | 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] | 5647 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5648 | 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] | 5649 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5650 | return 0; |
| 5651 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5652 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5653 | |
| 5654 | /* push keywords in the stack. */ |
| 5655 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5656 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5657 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5658 | 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] | 5659 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5660 | return 0; |
| 5661 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5662 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5663 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5664 | } |
| 5665 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5666 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5667 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5668 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5669 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5670 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5671 | } |
| 5672 | |
| 5673 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5674 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5675 | /* finished. */ |
| 5676 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5677 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5678 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5679 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5680 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5681 | /* If the stack is empty, the function fails. */ |
| 5682 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5683 | return 0; |
| 5684 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5685 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5686 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5687 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5688 | |
| 5689 | /* Set the end of execution flag. */ |
| 5690 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5691 | return 1; |
| 5692 | |
| 5693 | /* yield. */ |
| 5694 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5695 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5696 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5697 | 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] | 5698 | return 0; |
| 5699 | |
| 5700 | /* finished with error. */ |
| 5701 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5702 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5703 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5704 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5705 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5706 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5707 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5708 | return 0; |
| 5709 | |
| 5710 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5711 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5712 | stream_int_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5713 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5714 | 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] | 5715 | |
| 5716 | default: |
| 5717 | return 0; |
| 5718 | } |
| 5719 | } |
| 5720 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5721 | /* This function is an LUA binding used for registering |
| 5722 | * "sample-conv" functions. It expects a converter name used |
| 5723 | * in the haproxy configuration file, and an LUA function. |
| 5724 | */ |
| 5725 | __LJMP static int hlua_register_converters(lua_State *L) |
| 5726 | { |
| 5727 | struct sample_conv_kw_list *sck; |
| 5728 | const char *name; |
| 5729 | int ref; |
| 5730 | int len; |
| 5731 | struct hlua_function *fcn; |
| 5732 | |
| 5733 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 5734 | |
| 5735 | /* First argument : converter name. */ |
| 5736 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5737 | |
| 5738 | /* Second argument : lua function. */ |
| 5739 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5740 | |
| 5741 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5742 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5743 | if (!sck) |
| 5744 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5745 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5746 | if (!fcn) |
| 5747 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5748 | |
| 5749 | /* Fill fcn. */ |
| 5750 | fcn->name = strdup(name); |
| 5751 | if (!fcn->name) |
| 5752 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5753 | fcn->function_ref = ref; |
| 5754 | |
| 5755 | /* List head */ |
| 5756 | sck->list.n = sck->list.p = NULL; |
| 5757 | |
| 5758 | /* converter keyword. */ |
| 5759 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5760 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5761 | if (!sck->kw[0].kw) |
| 5762 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5763 | |
| 5764 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 5765 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5766 | 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] | 5767 | sck->kw[0].val_args = NULL; |
| 5768 | sck->kw[0].in_type = SMP_T_STR; |
| 5769 | sck->kw[0].out_type = SMP_T_STR; |
| 5770 | sck->kw[0].private = fcn; |
| 5771 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5772 | /* Register this new converter */ |
| 5773 | sample_register_convs(sck); |
| 5774 | |
| 5775 | return 0; |
| 5776 | } |
| 5777 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5778 | /* This fucntion is an LUA binding used for registering |
| 5779 | * "sample-fetch" functions. It expects a converter name used |
| 5780 | * in the haproxy configuration file, and an LUA function. |
| 5781 | */ |
| 5782 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 5783 | { |
| 5784 | const char *name; |
| 5785 | int ref; |
| 5786 | int len; |
| 5787 | struct sample_fetch_kw_list *sfk; |
| 5788 | struct hlua_function *fcn; |
| 5789 | |
| 5790 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 5791 | |
| 5792 | /* First argument : sample-fetch name. */ |
| 5793 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5794 | |
| 5795 | /* Second argument : lua function. */ |
| 5796 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5797 | |
| 5798 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5799 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5800 | if (!sfk) |
| 5801 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5802 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5803 | if (!fcn) |
| 5804 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5805 | |
| 5806 | /* Fill fcn. */ |
| 5807 | fcn->name = strdup(name); |
| 5808 | if (!fcn->name) |
| 5809 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5810 | fcn->function_ref = ref; |
| 5811 | |
| 5812 | /* List head */ |
| 5813 | sfk->list.n = sfk->list.p = NULL; |
| 5814 | |
| 5815 | /* sample-fetch keyword. */ |
| 5816 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5817 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5818 | if (!sfk->kw[0].kw) |
| 5819 | return luaL_error(L, "lua out of memory error."); |
| 5820 | |
| 5821 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 5822 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5823 | 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] | 5824 | sfk->kw[0].val_args = NULL; |
| 5825 | sfk->kw[0].out_type = SMP_T_STR; |
| 5826 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 5827 | sfk->kw[0].val = 0; |
| 5828 | sfk->kw[0].private = fcn; |
| 5829 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5830 | /* Register this new fetch. */ |
| 5831 | sample_register_fetches(sfk); |
| 5832 | |
| 5833 | return 0; |
| 5834 | } |
| 5835 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5836 | /* This function is a wrapper to execute each LUA function declared |
| 5837 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5838 | * return ACT_RET_CONT if the processing is finished (with or without |
| 5839 | * error) and return ACT_RET_YIELD if the function must be called again |
| 5840 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5841 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5842 | 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] | 5843 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5844 | { |
| 5845 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5846 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5847 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5848 | const char *error; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5849 | const struct chunk msg = { .len = 0 }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5850 | |
| 5851 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 5852 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 5853 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 5854 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 5855 | 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] | 5856 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5857 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5858 | return ACT_RET_CONT; |
| 5859 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5860 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5861 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5862 | * Lua context can be not initialized. This behavior |
| 5863 | * permits to save performances because a systematic |
| 5864 | * Lua initialization cause 5% performances loss. |
| 5865 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5866 | if (!s->hlua) { |
| 5867 | s->hlua = pool_alloc2(pool2_hlua); |
| 5868 | if (!s->hlua) { |
| 5869 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 5870 | rule->arg.hlua_rule->fcn.name); |
| 5871 | return ACT_RET_CONT; |
| 5872 | } |
| 5873 | if (!hlua_ctx_init(s->hlua, s->task)) { |
| 5874 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 5875 | rule->arg.hlua_rule->fcn.name); |
| 5876 | return ACT_RET_CONT; |
| 5877 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5878 | } |
| 5879 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5880 | consistency_set(s, dir, &s->hlua->cons); |
| 5881 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5882 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5883 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5884 | |
| 5885 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5886 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 5887 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 5888 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5889 | else |
| 5890 | error = "critical error"; |
| 5891 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 5892 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5893 | return ACT_RET_CONT; |
| 5894 | } |
| 5895 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5896 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5897 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5898 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5899 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5900 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5901 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5902 | } |
| 5903 | |
| 5904 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5905 | 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] | 5906 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5907 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5908 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5909 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5910 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5911 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5912 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5913 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5914 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5915 | |
| 5916 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5917 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5918 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5919 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5920 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5921 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5922 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5923 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5924 | lua_pushstring(s->hlua->T, *arg); |
| 5925 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5926 | } |
| 5927 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5928 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5929 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5930 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5931 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5932 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5933 | } |
| 5934 | |
| 5935 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5936 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5937 | /* finished. */ |
| 5938 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5939 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5940 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5941 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5942 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5943 | if (s->hlua->flags & HLUA_STOP) |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5944 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5945 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5946 | |
| 5947 | /* yield. */ |
| 5948 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5949 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5950 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5951 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5952 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5953 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5954 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5955 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5956 | /* Some actions can be wake up when a "write" event |
| 5957 | * is detected on a response channel. This is useful |
| 5958 | * only for actions targetted on the requests. |
| 5959 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5960 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5961 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 5962 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5963 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5964 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5965 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5966 | s->req.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5967 | /* We can quit the fcuntion without consistency check |
| 5968 | * because HAProxy is not able to manipulate data, it |
| 5969 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5970 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5971 | |
| 5972 | /* finished with error. */ |
| 5973 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5974 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5975 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5976 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5977 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5978 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5979 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5980 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 5981 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5982 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5983 | |
| 5984 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5985 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5986 | stream_int_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5987 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5988 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5989 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5990 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5991 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5992 | |
| 5993 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5994 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5995 | } |
| 5996 | } |
| 5997 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5998 | struct task *hlua_applet_wakeup(struct task *t) |
| 5999 | { |
| 6000 | struct appctx *ctx = t->context; |
| 6001 | struct stream_interface *si = ctx->owner; |
| 6002 | |
| 6003 | /* If the applet is wake up without any expected work, the sheduler |
| 6004 | * remove it from the run queue. This flag indicate that the applet |
| 6005 | * is waiting for write. If the buffer is full, the main processing |
| 6006 | * will send some data and after call the applet, otherwise it call |
| 6007 | * the applet ASAP. |
| 6008 | */ |
| 6009 | si_applet_cant_put(si); |
| 6010 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6011 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6012 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6013 | } |
| 6014 | |
| 6015 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6016 | { |
| 6017 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6018 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6019 | struct task *task; |
| 6020 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6021 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6022 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6023 | hlua = pool_alloc2(pool2_hlua); |
| 6024 | if (!hlua) { |
| 6025 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6026 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6027 | return 0; |
| 6028 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6029 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6030 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6031 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6032 | |
| 6033 | /* Create task used by signal to wakeup applets. */ |
| 6034 | task = task_new(); |
| 6035 | if (!task) { |
| 6036 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6037 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6038 | return 0; |
| 6039 | } |
| 6040 | task->nice = 0; |
| 6041 | task->context = ctx; |
| 6042 | task->process = hlua_applet_wakeup; |
| 6043 | ctx->ctx.hlua_apptcp.task = task; |
| 6044 | |
| 6045 | /* In the execution wrappers linked with a stream, the |
| 6046 | * Lua context can be not initialized. This behavior |
| 6047 | * permits to save performances because a systematic |
| 6048 | * Lua initialization cause 5% performances loss. |
| 6049 | */ |
| 6050 | if (!hlua_ctx_init(hlua, task)) { |
| 6051 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6052 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6053 | return 0; |
| 6054 | } |
| 6055 | |
| 6056 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6057 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6058 | |
| 6059 | /* The following Lua calls can fail. */ |
| 6060 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6061 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6062 | error = lua_tostring(hlua->T, -1); |
| 6063 | else |
| 6064 | error = "critical error"; |
| 6065 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6066 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6067 | RESET_SAFE_LJMP(hlua->T); |
| 6068 | return 0; |
| 6069 | } |
| 6070 | |
| 6071 | /* Check stack available size. */ |
| 6072 | if (!lua_checkstack(hlua->T, 1)) { |
| 6073 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6074 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6075 | RESET_SAFE_LJMP(hlua->T); |
| 6076 | return 0; |
| 6077 | } |
| 6078 | |
| 6079 | /* Restore the function in the stack. */ |
| 6080 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6081 | |
| 6082 | /* Create and and push object stream in the stack. */ |
| 6083 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6084 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6085 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6086 | RESET_SAFE_LJMP(hlua->T); |
| 6087 | return 0; |
| 6088 | } |
| 6089 | hlua->nargs = 1; |
| 6090 | |
| 6091 | /* push keywords in the stack. */ |
| 6092 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6093 | if (!lua_checkstack(hlua->T, 1)) { |
| 6094 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6095 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6096 | RESET_SAFE_LJMP(hlua->T); |
| 6097 | return 0; |
| 6098 | } |
| 6099 | lua_pushstring(hlua->T, *arg); |
| 6100 | hlua->nargs++; |
| 6101 | } |
| 6102 | |
| 6103 | RESET_SAFE_LJMP(hlua->T); |
| 6104 | |
| 6105 | /* Wakeup the applet ASAP. */ |
| 6106 | si_applet_cant_get(si); |
| 6107 | si_applet_cant_put(si); |
| 6108 | |
| 6109 | return 1; |
| 6110 | } |
| 6111 | |
| 6112 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6113 | { |
| 6114 | struct stream_interface *si = ctx->owner; |
| 6115 | struct stream *strm = si_strm(si); |
| 6116 | struct channel *res = si_ic(si); |
| 6117 | struct act_rule *rule = ctx->rule; |
| 6118 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6119 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6120 | |
| 6121 | /* The applet execution is already done. */ |
| 6122 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) |
| 6123 | return; |
| 6124 | |
| 6125 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6126 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6127 | return; |
| 6128 | |
| 6129 | /* Execute the function. */ |
| 6130 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6131 | /* finished. */ |
| 6132 | case HLUA_E_OK: |
| 6133 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6134 | |
| 6135 | /* log time */ |
| 6136 | strm->logs.tv_request = now; |
| 6137 | |
| 6138 | /* eat the whole request */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6139 | co_skip(si_oc(si), si_ob(si)->o); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6140 | res->flags |= CF_READ_NULL; |
| 6141 | si_shutr(si); |
| 6142 | return; |
| 6143 | |
| 6144 | /* yield. */ |
| 6145 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6146 | if (hlua->wake_time != TICK_ETERNITY) |
| 6147 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6148 | return; |
| 6149 | |
| 6150 | /* finished with error. */ |
| 6151 | case HLUA_E_ERRMSG: |
| 6152 | /* Display log. */ |
| 6153 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6154 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6155 | lua_pop(hlua->T, 1); |
| 6156 | goto error; |
| 6157 | |
| 6158 | case HLUA_E_ERR: |
| 6159 | /* Display log. */ |
| 6160 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6161 | rule->arg.hlua_rule->fcn.name); |
| 6162 | goto error; |
| 6163 | |
| 6164 | default: |
| 6165 | goto error; |
| 6166 | } |
| 6167 | |
| 6168 | error: |
| 6169 | |
| 6170 | /* For all other cases, just close the stream. */ |
| 6171 | si_shutw(si); |
| 6172 | si_shutr(si); |
| 6173 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6174 | } |
| 6175 | |
| 6176 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6177 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6178 | task_delete(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6179 | task_free(ctx->ctx.hlua_apptcp.task); |
| 6180 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6181 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6182 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6183 | } |
| 6184 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6185 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6186 | * an errors occurs and -1 if more data are required for initializing |
| 6187 | * the applet. |
| 6188 | */ |
| 6189 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6190 | { |
| 6191 | struct stream_interface *si = ctx->owner; |
| 6192 | struct channel *req = si_oc(si); |
| 6193 | struct http_msg *msg; |
| 6194 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6195 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6196 | char **arg; |
| 6197 | struct hdr_ctx hdr; |
| 6198 | struct task *task; |
| 6199 | 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] | 6200 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6201 | |
| 6202 | /* Wait for a full HTTP request. */ |
| 6203 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 6204 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 6205 | return -1; |
| 6206 | return 0; |
| 6207 | } |
| 6208 | txn = strm->txn; |
| 6209 | msg = &txn->req; |
| 6210 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6211 | /* We want two things in HTTP mode : |
| 6212 | * - enforce server-close mode if we were in keep-alive, so that the |
| 6213 | * applet is released after each response ; |
| 6214 | * - enable request body transfer to the applet in order to resync |
| 6215 | * with the response body. |
| 6216 | */ |
| 6217 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 6218 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 6219 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6220 | hlua = pool_alloc2(pool2_hlua); |
| 6221 | if (!hlua) { |
| 6222 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6223 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6224 | return 0; |
| 6225 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6226 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6227 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6228 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6229 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6230 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6231 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6232 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6233 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6234 | /* Create task used by signal to wakeup applets. */ |
| 6235 | task = task_new(); |
| 6236 | if (!task) { |
| 6237 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6238 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6239 | return 0; |
| 6240 | } |
| 6241 | task->nice = 0; |
| 6242 | task->context = ctx; |
| 6243 | task->process = hlua_applet_wakeup; |
| 6244 | ctx->ctx.hlua_apphttp.task = task; |
| 6245 | |
| 6246 | /* In the execution wrappers linked with a stream, the |
| 6247 | * Lua context can be not initialized. This behavior |
| 6248 | * permits to save performances because a systematic |
| 6249 | * Lua initialization cause 5% performances loss. |
| 6250 | */ |
| 6251 | if (!hlua_ctx_init(hlua, task)) { |
| 6252 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6253 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6254 | return 0; |
| 6255 | } |
| 6256 | |
| 6257 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6258 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6259 | |
| 6260 | /* The following Lua calls can fail. */ |
| 6261 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6262 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6263 | error = lua_tostring(hlua->T, -1); |
| 6264 | else |
| 6265 | error = "critical error"; |
| 6266 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6267 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6268 | return 0; |
| 6269 | } |
| 6270 | |
| 6271 | /* Check stack available size. */ |
| 6272 | if (!lua_checkstack(hlua->T, 1)) { |
| 6273 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6274 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6275 | RESET_SAFE_LJMP(hlua->T); |
| 6276 | return 0; |
| 6277 | } |
| 6278 | |
| 6279 | /* Restore the function in the stack. */ |
| 6280 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6281 | |
| 6282 | /* Create and and push object stream in the stack. */ |
| 6283 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6284 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6285 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6286 | RESET_SAFE_LJMP(hlua->T); |
| 6287 | return 0; |
| 6288 | } |
| 6289 | hlua->nargs = 1; |
| 6290 | |
| 6291 | /* Look for a 100-continue expected. */ |
| 6292 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 6293 | hdr.idx = 0; |
| 6294 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) && |
| 6295 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 6296 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 6297 | } |
| 6298 | |
| 6299 | /* push keywords in the stack. */ |
| 6300 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6301 | if (!lua_checkstack(hlua->T, 1)) { |
| 6302 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6303 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6304 | RESET_SAFE_LJMP(hlua->T); |
| 6305 | return 0; |
| 6306 | } |
| 6307 | lua_pushstring(hlua->T, *arg); |
| 6308 | hlua->nargs++; |
| 6309 | } |
| 6310 | |
| 6311 | RESET_SAFE_LJMP(hlua->T); |
| 6312 | |
| 6313 | /* Wakeup the applet when data is ready for read. */ |
| 6314 | si_applet_cant_get(si); |
| 6315 | |
| 6316 | return 1; |
| 6317 | } |
| 6318 | |
| 6319 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 6320 | { |
| 6321 | struct stream_interface *si = ctx->owner; |
| 6322 | struct stream *strm = si_strm(si); |
| 6323 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6324 | struct act_rule *rule = ctx->rule; |
| 6325 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6326 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6327 | char *blk1; |
| 6328 | int len1; |
| 6329 | char *blk2; |
| 6330 | int len2; |
| 6331 | int ret; |
| 6332 | |
| 6333 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6334 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6335 | return; |
| 6336 | |
| 6337 | /* Set the currently running flag. */ |
| 6338 | if (!HLUA_IS_RUNNING(hlua) && |
| 6339 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6340 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6341 | /* Wait for full HTTP analysys. */ |
| 6342 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 6343 | si_applet_cant_get(si); |
| 6344 | return; |
| 6345 | } |
| 6346 | |
| 6347 | /* Store the max amount of bytes that we can read. */ |
| 6348 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 6349 | |
| 6350 | /* We need to flush the request header. This left the body |
| 6351 | * for the Lua. |
| 6352 | */ |
| 6353 | |
| 6354 | /* Read the maximum amount of data avalaible. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6355 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6356 | if (ret == -1) |
| 6357 | return; |
| 6358 | |
| 6359 | /* No data available, ask for more data. */ |
| 6360 | if (ret == 1) |
| 6361 | len2 = 0; |
| 6362 | if (ret == 0) |
| 6363 | len1 = 0; |
| 6364 | if (len1 + len2 < strm->txn->req.eoh + 2) { |
| 6365 | si_applet_cant_get(si); |
| 6366 | return; |
| 6367 | } |
| 6368 | |
| 6369 | /* skip the requests bytes. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6370 | co_skip(si_oc(si), strm->txn->req.eoh + 2); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6371 | } |
| 6372 | |
| 6373 | /* Executes The applet if it is not done. */ |
| 6374 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6375 | |
| 6376 | /* Execute the function. */ |
| 6377 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6378 | /* finished. */ |
| 6379 | case HLUA_E_OK: |
| 6380 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6381 | break; |
| 6382 | |
| 6383 | /* yield. */ |
| 6384 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6385 | if (hlua->wake_time != TICK_ETERNITY) |
| 6386 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6387 | return; |
| 6388 | |
| 6389 | /* finished with error. */ |
| 6390 | case HLUA_E_ERRMSG: |
| 6391 | /* Display log. */ |
| 6392 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6393 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6394 | lua_pop(hlua->T, 1); |
| 6395 | goto error; |
| 6396 | |
| 6397 | case HLUA_E_ERR: |
| 6398 | /* Display log. */ |
| 6399 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6400 | rule->arg.hlua_rule->fcn.name); |
| 6401 | goto error; |
| 6402 | |
| 6403 | default: |
| 6404 | goto error; |
| 6405 | } |
| 6406 | } |
| 6407 | |
| 6408 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 6409 | |
| 6410 | /* We must send the final chunk. */ |
| 6411 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 6412 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 6413 | |
| 6414 | /* sent last chunk at once. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6415 | ret = ci_putblk(res, "0\r\n\r\n", 5); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6416 | |
| 6417 | /* critical error. */ |
| 6418 | if (ret == -2 || ret == -3) { |
| 6419 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 6420 | rule->arg.hlua_rule->fcn.name); |
| 6421 | goto error; |
| 6422 | } |
| 6423 | |
| 6424 | /* no enough space error. */ |
| 6425 | if (ret == -1) { |
| 6426 | si_applet_cant_put(si); |
| 6427 | return; |
| 6428 | } |
| 6429 | |
| 6430 | /* set the last chunk sent. */ |
| 6431 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6432 | } |
| 6433 | |
| 6434 | /* close the connection. */ |
| 6435 | |
| 6436 | /* status / log */ |
| 6437 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6438 | strm->logs.tv_request = now; |
| 6439 | |
| 6440 | /* eat the whole request */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6441 | co_skip(si_oc(si), si_ob(si)->o); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6442 | res->flags |= CF_READ_NULL; |
| 6443 | si_shutr(si); |
| 6444 | |
| 6445 | return; |
| 6446 | } |
| 6447 | |
| 6448 | error: |
| 6449 | |
| 6450 | /* If we are in HTTP mode, and we are not send any |
| 6451 | * data, return a 500 server error in best effort: |
| 6452 | * if there are no room avalaible in the buffer, |
| 6453 | * just close the connection. |
| 6454 | */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6455 | ci_putblk(res, error_500, strlen(error_500)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6456 | if (!(strm->flags & SF_ERR_MASK)) |
| 6457 | strm->flags |= SF_ERR_RESOURCE; |
| 6458 | si_shutw(si); |
| 6459 | si_shutr(si); |
| 6460 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6461 | } |
| 6462 | |
| 6463 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6464 | { |
Willy Tarreau | bd7fc95 | 2017-07-24 17:35:27 +0200 | [diff] [blame] | 6465 | task_delete(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6466 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6467 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6468 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6469 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6470 | } |
| 6471 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6472 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6473 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6474 | * |
| 6475 | * This function can fail with an abort() due to an Lua critical error. |
| 6476 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6477 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6478 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6479 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6480 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6481 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6482 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6483 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6484 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6485 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6486 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6487 | if (!rule->arg.hlua_rule) { |
| 6488 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6489 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6490 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6491 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6492 | /* Memory for arguments. */ |
| 6493 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6494 | if (!rule->arg.hlua_rule->args) { |
| 6495 | memprintf(err, "out of memory error"); |
| 6496 | return ACT_RET_PRS_ERR; |
| 6497 | } |
| 6498 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6499 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6500 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6501 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6502 | /* Expect some arguments */ |
| 6503 | for (i = 0; i < fcn->nargs; i++) { |
| 6504 | if (*args[i+1] == '\0') { |
| 6505 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6506 | return ACT_RET_PRS_ERR; |
| 6507 | } |
| 6508 | rule->arg.hlua_rule->args[i] = strdup(args[i + 1]); |
| 6509 | if (!rule->arg.hlua_rule->args[i]) { |
| 6510 | memprintf(err, "out of memory error"); |
| 6511 | return ACT_RET_PRS_ERR; |
| 6512 | } |
| 6513 | (*cur_arg)++; |
| 6514 | } |
| 6515 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6516 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6517 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6518 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6519 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6520 | } |
| 6521 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6522 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6523 | struct act_rule *rule, char **err) |
| 6524 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6525 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6526 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6527 | /* HTTP applets are forbidden in tcp-request rules. |
| 6528 | * HTTP applet request requires everything initilized by |
| 6529 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6530 | * The applet will be immediately initilized, but its before |
| 6531 | * the call of this analyzer. |
| 6532 | */ |
| 6533 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6534 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6535 | return ACT_RET_PRS_ERR; |
| 6536 | } |
| 6537 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6538 | /* Memory for the rule. */ |
| 6539 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6540 | if (!rule->arg.hlua_rule) { |
| 6541 | memprintf(err, "out of memory error"); |
| 6542 | return ACT_RET_PRS_ERR; |
| 6543 | } |
| 6544 | |
| 6545 | /* Reference the Lua function and store the reference. */ |
| 6546 | rule->arg.hlua_rule->fcn = *fcn; |
| 6547 | |
| 6548 | /* TODO: later accept arguments. */ |
| 6549 | rule->arg.hlua_rule->args = NULL; |
| 6550 | |
| 6551 | /* Add applet pointer in the rule. */ |
| 6552 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6553 | rule->applet.name = fcn->name; |
| 6554 | rule->applet.init = hlua_applet_http_init; |
| 6555 | rule->applet.fct = hlua_applet_http_fct; |
| 6556 | rule->applet.release = hlua_applet_http_release; |
| 6557 | rule->applet.timeout = hlua_timeout_applet; |
| 6558 | |
| 6559 | return ACT_RET_PRS_OK; |
| 6560 | } |
| 6561 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6562 | /* This function is an LUA binding used for registering |
| 6563 | * "sample-conv" functions. It expects a converter name used |
| 6564 | * in the haproxy configuration file, and an LUA function. |
| 6565 | */ |
| 6566 | __LJMP static int hlua_register_action(lua_State *L) |
| 6567 | { |
| 6568 | struct action_kw_list *akl; |
| 6569 | const char *name; |
| 6570 | int ref; |
| 6571 | int len; |
| 6572 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6573 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6574 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6575 | /* Initialise the number of expected arguments at 0. */ |
| 6576 | nargs = 0; |
| 6577 | |
| 6578 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6579 | 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] | 6580 | |
| 6581 | /* First argument : converter name. */ |
| 6582 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6583 | |
| 6584 | /* Second argument : environment. */ |
| 6585 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6586 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6587 | |
| 6588 | /* Third argument : lua function. */ |
| 6589 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6590 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6591 | /* Fouth argument : number of mandatories arguments expected on the configuration line. */ |
| 6592 | if (lua_gettop(L) >= 4) |
| 6593 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6594 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6595 | /* browse the second argulent as an array. */ |
| 6596 | lua_pushnil(L); |
| 6597 | while (lua_next(L, 2) != 0) { |
| 6598 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6599 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6600 | |
| 6601 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6602 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6603 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6604 | if (!akl) |
| 6605 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6606 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6607 | if (!fcn) |
| 6608 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6609 | |
| 6610 | /* Fill fcn. */ |
| 6611 | fcn->name = strdup(name); |
| 6612 | if (!fcn->name) |
| 6613 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6614 | fcn->function_ref = ref; |
| 6615 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6616 | /* Set the expected number od arguments. */ |
| 6617 | fcn->nargs = nargs; |
| 6618 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6619 | /* List head */ |
| 6620 | akl->list.n = akl->list.p = NULL; |
| 6621 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6622 | /* action keyword. */ |
| 6623 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6624 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6625 | if (!akl->kw[0].kw) |
| 6626 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6627 | |
| 6628 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6629 | |
| 6630 | akl->kw[0].match_pfx = 0; |
| 6631 | akl->kw[0].private = fcn; |
| 6632 | akl->kw[0].parse = action_register_lua; |
| 6633 | |
| 6634 | /* select the action registering point. */ |
| 6635 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6636 | tcp_req_cont_keywords_register(akl); |
| 6637 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6638 | tcp_res_cont_keywords_register(akl); |
| 6639 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6640 | http_req_keywords_register(akl); |
| 6641 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6642 | http_res_keywords_register(akl); |
| 6643 | else |
| 6644 | WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. " |
| 6645 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6646 | "are expected.", lua_tostring(L, -1))); |
| 6647 | |
| 6648 | /* pop the environment string. */ |
| 6649 | lua_pop(L, 1); |
| 6650 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6651 | return ACT_RET_PRS_OK; |
| 6652 | } |
| 6653 | |
| 6654 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6655 | struct act_rule *rule, char **err) |
| 6656 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6657 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6658 | |
| 6659 | /* Memory for the rule. */ |
| 6660 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6661 | if (!rule->arg.hlua_rule) { |
| 6662 | memprintf(err, "out of memory error"); |
| 6663 | return ACT_RET_PRS_ERR; |
| 6664 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6665 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6666 | /* Reference the Lua function and store the reference. */ |
| 6667 | rule->arg.hlua_rule->fcn = *fcn; |
| 6668 | |
| 6669 | /* TODO: later accept arguments. */ |
| 6670 | rule->arg.hlua_rule->args = NULL; |
| 6671 | |
| 6672 | /* Add applet pointer in the rule. */ |
| 6673 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6674 | rule->applet.name = fcn->name; |
| 6675 | rule->applet.init = hlua_applet_tcp_init; |
| 6676 | rule->applet.fct = hlua_applet_tcp_fct; |
| 6677 | rule->applet.release = hlua_applet_tcp_release; |
| 6678 | rule->applet.timeout = hlua_timeout_applet; |
| 6679 | |
| 6680 | return 0; |
| 6681 | } |
| 6682 | |
| 6683 | /* This function is an LUA binding used for registering |
| 6684 | * "sample-conv" functions. It expects a converter name used |
| 6685 | * in the haproxy configuration file, and an LUA function. |
| 6686 | */ |
| 6687 | __LJMP static int hlua_register_service(lua_State *L) |
| 6688 | { |
| 6689 | struct action_kw_list *akl; |
| 6690 | const char *name; |
| 6691 | const char *env; |
| 6692 | int ref; |
| 6693 | int len; |
| 6694 | struct hlua_function *fcn; |
| 6695 | |
| 6696 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 6697 | |
| 6698 | /* First argument : converter name. */ |
| 6699 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6700 | |
| 6701 | /* Second argument : environment. */ |
| 6702 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6703 | |
| 6704 | /* Third argument : lua function. */ |
| 6705 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6706 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6707 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6708 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 6709 | if (!akl) |
| 6710 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6711 | fcn = calloc(1, sizeof(*fcn)); |
| 6712 | if (!fcn) |
| 6713 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6714 | |
| 6715 | /* Fill fcn. */ |
| 6716 | len = strlen("<lua.>") + strlen(name) + 1; |
| 6717 | fcn->name = calloc(1, len); |
| 6718 | if (!fcn->name) |
| 6719 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6720 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 6721 | fcn->function_ref = ref; |
| 6722 | |
| 6723 | /* List head */ |
| 6724 | akl->list.n = akl->list.p = NULL; |
| 6725 | |
| 6726 | /* converter keyword. */ |
| 6727 | len = strlen("lua.") + strlen(name) + 1; |
| 6728 | akl->kw[0].kw = calloc(1, len); |
| 6729 | if (!akl->kw[0].kw) |
| 6730 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6731 | |
| 6732 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6733 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 6734 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6735 | if (strcmp(env, "tcp") == 0) |
| 6736 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6737 | else if (strcmp(env, "http") == 0) |
| 6738 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6739 | else |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6740 | WILL_LJMP(luaL_error(L, "lua service environment '%s' is unknown. " |
| 6741 | "'tcp' or 'http' are expected.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6742 | |
| 6743 | akl->kw[0].match_pfx = 0; |
| 6744 | akl->kw[0].private = fcn; |
| 6745 | |
| 6746 | /* End of array. */ |
| 6747 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 6748 | |
| 6749 | /* Register this new converter */ |
| 6750 | service_keywords_register(akl); |
| 6751 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6752 | return 0; |
| 6753 | } |
| 6754 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6755 | /* This function initialises Lua cli handler. It copies the |
| 6756 | * arguments in the Lua stack and create channel IO objects. |
| 6757 | */ |
| 6758 | static int hlua_cli_parse_fct(char **args, struct appctx *appctx, void *private) |
| 6759 | { |
| 6760 | struct hlua *hlua; |
| 6761 | struct hlua_function *fcn; |
| 6762 | int i; |
| 6763 | const char *error; |
| 6764 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6765 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6766 | appctx->ctx.hlua_cli.fcn = private; |
| 6767 | |
| 6768 | hlua = pool_alloc2(pool2_hlua); |
| 6769 | if (!hlua) { |
| 6770 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6771 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6772 | } |
| 6773 | HLUA_INIT(hlua); |
| 6774 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6775 | |
| 6776 | /* Create task used by signal to wakeup applets. |
| 6777 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 6778 | * applet_http. It is absolutely compatible. |
| 6779 | */ |
| 6780 | appctx->ctx.hlua_cli.task = task_new(); |
| 6781 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 6782 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6783 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6784 | } |
| 6785 | appctx->ctx.hlua_cli.task->nice = 0; |
| 6786 | appctx->ctx.hlua_cli.task->context = appctx; |
| 6787 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 6788 | |
| 6789 | /* Initialises the Lua context */ |
| 6790 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 6791 | 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] | 6792 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6793 | } |
| 6794 | |
| 6795 | /* The following Lua calls can fail. */ |
| 6796 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 6797 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6798 | error = lua_tostring(hlua->T, -1); |
| 6799 | else |
| 6800 | error = "critical error"; |
| 6801 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 6802 | goto error; |
| 6803 | } |
| 6804 | |
| 6805 | /* Check stack available size. */ |
| 6806 | if (!lua_checkstack(hlua->T, 2)) { |
| 6807 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6808 | goto error; |
| 6809 | } |
| 6810 | |
| 6811 | /* Restore the function in the stack. */ |
| 6812 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 6813 | |
| 6814 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 6815 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6816 | */ |
| 6817 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 6818 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6819 | goto error; |
| 6820 | } |
| 6821 | hlua->nargs = 1; |
| 6822 | |
| 6823 | /* push keywords in the stack. */ |
| 6824 | for (i = 0; *args[i]; i++) { |
| 6825 | /* Check stack available size. */ |
| 6826 | if (!lua_checkstack(hlua->T, 1)) { |
| 6827 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6828 | goto error; |
| 6829 | } |
| 6830 | lua_pushstring(hlua->T, args[i]); |
| 6831 | hlua->nargs++; |
| 6832 | } |
| 6833 | |
| 6834 | /* We must initialize the execution timeouts. */ |
| 6835 | hlua->max_time = hlua_timeout_session; |
| 6836 | |
| 6837 | /* At this point the execution is safe. */ |
| 6838 | RESET_SAFE_LJMP(hlua->T); |
| 6839 | |
| 6840 | /* It's ok */ |
| 6841 | return 0; |
| 6842 | |
| 6843 | /* It's not ok. */ |
| 6844 | error: |
| 6845 | RESET_SAFE_LJMP(hlua->T); |
| 6846 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6847 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6848 | return 1; |
| 6849 | } |
| 6850 | |
| 6851 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 6852 | { |
| 6853 | struct hlua *hlua; |
| 6854 | struct stream_interface *si; |
| 6855 | struct hlua_function *fcn; |
| 6856 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6857 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6858 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 6859 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6860 | |
| 6861 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6862 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6863 | return 1; |
| 6864 | |
| 6865 | /* Execute the function. */ |
| 6866 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6867 | |
| 6868 | /* finished. */ |
| 6869 | case HLUA_E_OK: |
| 6870 | return 1; |
| 6871 | |
| 6872 | /* yield. */ |
| 6873 | case HLUA_E_AGAIN: |
| 6874 | /* We want write. */ |
| 6875 | if (HLUA_IS_WAKERESWR(hlua)) |
| 6876 | si_applet_cant_put(si); |
| 6877 | /* Set the timeout. */ |
| 6878 | if (hlua->wake_time != TICK_ETERNITY) |
| 6879 | task_schedule(hlua->task, hlua->wake_time); |
| 6880 | return 0; |
| 6881 | |
| 6882 | /* finished with error. */ |
| 6883 | case HLUA_E_ERRMSG: |
| 6884 | /* Display log. */ |
| 6885 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 6886 | fcn->name, lua_tostring(hlua->T, -1)); |
| 6887 | lua_pop(hlua->T, 1); |
| 6888 | return 1; |
| 6889 | |
| 6890 | case HLUA_E_ERR: |
| 6891 | /* Display log. */ |
| 6892 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 6893 | fcn->name); |
| 6894 | return 1; |
| 6895 | |
| 6896 | default: |
| 6897 | return 1; |
| 6898 | } |
| 6899 | |
| 6900 | return 1; |
| 6901 | } |
| 6902 | |
| 6903 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 6904 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6905 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6906 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6907 | } |
| 6908 | |
| 6909 | /* This function is an LUA binding used for registering |
| 6910 | * new keywords in the cli. It expects a list of keywords |
| 6911 | * which are the "path". It is limited to 5 keywords. A |
| 6912 | * description of the command, a function to be executed |
| 6913 | * for the parsing and a function for io handlers. |
| 6914 | */ |
| 6915 | __LJMP static int hlua_register_cli(lua_State *L) |
| 6916 | { |
| 6917 | struct cli_kw_list *cli_kws; |
| 6918 | const char *message; |
| 6919 | int ref_io; |
| 6920 | int len; |
| 6921 | struct hlua_function *fcn; |
| 6922 | int index; |
| 6923 | int i; |
| 6924 | |
| 6925 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 6926 | |
| 6927 | /* First argument : an array of maximum 5 keywords. */ |
| 6928 | if (!lua_istable(L, 1)) |
| 6929 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 6930 | |
| 6931 | /* Second argument : string with contextual message. */ |
| 6932 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6933 | |
| 6934 | /* Third and fourth argument : lua function. */ |
| 6935 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6936 | |
| 6937 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6938 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 6939 | if (!cli_kws) |
| 6940 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6941 | fcn = calloc(1, sizeof(*fcn)); |
| 6942 | if (!fcn) |
| 6943 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6944 | |
| 6945 | /* Fill path. */ |
| 6946 | index = 0; |
| 6947 | lua_pushnil(L); |
| 6948 | while(lua_next(L, 1) != 0) { |
| 6949 | if (index >= 5) |
| 6950 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 6951 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6952 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 6953 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 6954 | if (!cli_kws->kw[0].str_kw[index]) |
| 6955 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6956 | index++; |
| 6957 | lua_pop(L, 1); |
| 6958 | } |
| 6959 | |
| 6960 | /* Copy help message. */ |
| 6961 | cli_kws->kw[0].usage = strdup(message); |
| 6962 | if (!cli_kws->kw[0].usage) |
| 6963 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6964 | |
| 6965 | /* Fill fcn io handler. */ |
| 6966 | len = strlen("<lua.cli>") + 1; |
| 6967 | for (i = 0; i < index; i++) |
| 6968 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 6969 | fcn->name = calloc(1, len); |
| 6970 | if (!fcn->name) |
| 6971 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6972 | strncat((char *)fcn->name, "<lua.cli", len); |
| 6973 | for (i = 0; i < index; i++) { |
| 6974 | strncat((char *)fcn->name, ".", len); |
| 6975 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 6976 | } |
| 6977 | strncat((char *)fcn->name, ">", len); |
| 6978 | fcn->function_ref = ref_io; |
| 6979 | |
| 6980 | /* Fill last entries. */ |
| 6981 | cli_kws->kw[0].private = fcn; |
| 6982 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 6983 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 6984 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 6985 | |
| 6986 | /* Register this new converter */ |
| 6987 | cli_register_kw(cli_kws); |
| 6988 | |
| 6989 | return 0; |
| 6990 | } |
| 6991 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6992 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 6993 | struct proxy *defpx, const char *file, int line, |
| 6994 | char **err, unsigned int *timeout) |
| 6995 | { |
| 6996 | const char *error; |
| 6997 | |
| 6998 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 6999 | if (error && *error != '\0') { |
| 7000 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7001 | return -1; |
| 7002 | } |
| 7003 | return 0; |
| 7004 | } |
| 7005 | |
| 7006 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7007 | struct proxy *defpx, const char *file, int line, |
| 7008 | char **err) |
| 7009 | { |
| 7010 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7011 | file, line, err, &hlua_timeout_session); |
| 7012 | } |
| 7013 | |
| 7014 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7015 | struct proxy *defpx, const char *file, int line, |
| 7016 | char **err) |
| 7017 | { |
| 7018 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7019 | file, line, err, &hlua_timeout_task); |
| 7020 | } |
| 7021 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7022 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7023 | struct proxy *defpx, const char *file, int line, |
| 7024 | char **err) |
| 7025 | { |
| 7026 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7027 | file, line, err, &hlua_timeout_applet); |
| 7028 | } |
| 7029 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7030 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7031 | struct proxy *defpx, const char *file, int line, |
| 7032 | char **err) |
| 7033 | { |
| 7034 | char *error; |
| 7035 | |
| 7036 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7037 | if (*error != '\0') { |
| 7038 | memprintf(err, "%s: invalid number", args[0]); |
| 7039 | return -1; |
| 7040 | } |
| 7041 | return 0; |
| 7042 | } |
| 7043 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7044 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7045 | struct proxy *defpx, const char *file, int line, |
| 7046 | char **err) |
| 7047 | { |
| 7048 | char *error; |
| 7049 | |
| 7050 | if (*(args[1]) == 0) { |
| 7051 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7052 | return -1; |
| 7053 | } |
| 7054 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7055 | if (*error != '\0') { |
| 7056 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7057 | return -1; |
| 7058 | } |
| 7059 | return 0; |
| 7060 | } |
| 7061 | |
| 7062 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7063 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7064 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7065 | * the main lua entry point. |
| 7066 | * |
| 7067 | * This funtion runs with the HAProxy keywords API. It returns -1 if an error is |
| 7068 | * occured, otherwise it returns 0. |
| 7069 | * |
| 7070 | * In some error case, LUA set an error message in top of the stack. This function |
| 7071 | * 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] | 7072 | * |
| 7073 | * This function can fail with an abort() due to an Lua critical error. |
| 7074 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7075 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7076 | */ |
| 7077 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7078 | struct proxy *defpx, const char *file, int line, |
| 7079 | char **err) |
| 7080 | { |
| 7081 | int error; |
| 7082 | |
| 7083 | /* Just load and compile the file. */ |
| 7084 | error = luaL_loadfile(gL.T, args[1]); |
| 7085 | if (error) { |
| 7086 | memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1)); |
| 7087 | lua_pop(gL.T, 1); |
| 7088 | return -1; |
| 7089 | } |
| 7090 | |
| 7091 | /* If no syntax error where detected, execute the code. */ |
| 7092 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7093 | switch (error) { |
| 7094 | case LUA_OK: |
| 7095 | break; |
| 7096 | case LUA_ERRRUN: |
| 7097 | memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
| 7098 | lua_pop(gL.T, 1); |
| 7099 | return -1; |
| 7100 | case LUA_ERRMEM: |
| 7101 | memprintf(err, "lua out of memory error\n"); |
| 7102 | return -1; |
| 7103 | case LUA_ERRERR: |
| 7104 | memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1)); |
| 7105 | lua_pop(gL.T, 1); |
| 7106 | return -1; |
| 7107 | case LUA_ERRGCMM: |
| 7108 | memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1)); |
| 7109 | lua_pop(gL.T, 1); |
| 7110 | return -1; |
| 7111 | default: |
| 7112 | memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
| 7113 | lua_pop(gL.T, 1); |
| 7114 | return -1; |
| 7115 | } |
| 7116 | |
| 7117 | return 0; |
| 7118 | } |
| 7119 | |
| 7120 | /* configuration keywords declaration */ |
| 7121 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7122 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7123 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7124 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7125 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7126 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7127 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7128 | { 0, NULL, NULL }, |
| 7129 | }}; |
| 7130 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7131 | /* This function can fail with an abort() due to an Lua critical error. |
| 7132 | * We are in the initialisation process of HAProxy, this abort() is |
| 7133 | * tolerated. |
| 7134 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7135 | int hlua_post_init() |
| 7136 | { |
| 7137 | struct hlua_init_function *init; |
| 7138 | const char *msg; |
| 7139 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7140 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7141 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7142 | /* Call post initialisation function in safe environement. */ |
| 7143 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7144 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7145 | error = lua_tostring(gL.T, -1); |
| 7146 | else |
| 7147 | error = "critical error"; |
| 7148 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7149 | exit(1); |
| 7150 | } |
| 7151 | hlua_fcn_post_init(gL.T); |
| 7152 | RESET_SAFE_LJMP(gL.T); |
| 7153 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7154 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7155 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7156 | ret = hlua_ctx_resume(&gL, 0); |
| 7157 | switch (ret) { |
| 7158 | case HLUA_E_OK: |
| 7159 | lua_pop(gL.T, -1); |
| 7160 | return 1; |
| 7161 | case HLUA_E_AGAIN: |
| 7162 | Alert("lua init: yield not allowed.\n"); |
| 7163 | return 0; |
| 7164 | case HLUA_E_ERRMSG: |
| 7165 | msg = lua_tostring(gL.T, -1); |
| 7166 | Alert("lua init: %s.\n", msg); |
| 7167 | return 0; |
| 7168 | case HLUA_E_ERR: |
| 7169 | default: |
| 7170 | Alert("lua init: unknown runtime error.\n"); |
| 7171 | return 0; |
| 7172 | } |
| 7173 | } |
| 7174 | return 1; |
| 7175 | } |
| 7176 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7177 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7178 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7179 | * is the previously allocated size or the kind of object in case of a new |
| 7180 | * allocation. <nsize> is the requested new size. |
| 7181 | */ |
| 7182 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7183 | { |
| 7184 | struct hlua_mem_allocator *zone = ud; |
| 7185 | |
| 7186 | if (nsize == 0) { |
| 7187 | /* it's a free */ |
| 7188 | if (ptr) |
| 7189 | zone->allocated -= osize; |
| 7190 | free(ptr); |
| 7191 | return NULL; |
| 7192 | } |
| 7193 | |
| 7194 | if (!ptr) { |
| 7195 | /* it's a new allocation */ |
| 7196 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7197 | return NULL; |
| 7198 | |
| 7199 | ptr = malloc(nsize); |
| 7200 | if (ptr) |
| 7201 | zone->allocated += nsize; |
| 7202 | return ptr; |
| 7203 | } |
| 7204 | |
| 7205 | /* it's a realloc */ |
| 7206 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7207 | return NULL; |
| 7208 | |
| 7209 | ptr = realloc(ptr, nsize); |
| 7210 | if (ptr) |
| 7211 | zone->allocated += nsize - osize; |
| 7212 | return ptr; |
| 7213 | } |
| 7214 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7215 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7216 | * We are in the initialisation process of HAProxy, this abort() is |
| 7217 | * tolerated. |
| 7218 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7219 | void hlua_init(void) |
| 7220 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7221 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7222 | int idx; |
| 7223 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7224 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7225 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7226 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7227 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7228 | struct srv_kw *kw; |
| 7229 | int tmp_error; |
| 7230 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7231 | char *args[] = { /* SSL client configuration. */ |
| 7232 | "ssl", |
| 7233 | "verify", |
| 7234 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7235 | NULL |
| 7236 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7237 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7238 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7239 | /* Initialise struct hlua and com signals pool */ |
| 7240 | pool2_hlua = create_pool("hlua", sizeof(struct hlua), MEM_F_SHARED); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7241 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7242 | /* Register configuration keywords. */ |
| 7243 | cfg_register_keywords(&cfg_kws); |
| 7244 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7245 | /* Init main lua stack. */ |
| 7246 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7247 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7248 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7249 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7250 | hlua_sethlua(&gL); |
| 7251 | gL.Tref = LUA_REFNIL; |
| 7252 | gL.task = NULL; |
| 7253 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7254 | /* From this point, until the end of the initialisation fucntion, |
| 7255 | * the Lua function can fail with an abort. We are in the initialisation |
| 7256 | * process of HAProxy, this abort() is tolerated. |
| 7257 | */ |
| 7258 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7259 | /* Initialise lua. */ |
| 7260 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7261 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7262 | /* Set safe environment for the initialisation. */ |
| 7263 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7264 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7265 | error_msg = lua_tostring(gL.T, -1); |
| 7266 | else |
| 7267 | error_msg = "critical error"; |
| 7268 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7269 | exit(1); |
| 7270 | } |
| 7271 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7272 | /* |
| 7273 | * |
| 7274 | * Create "core" object. |
| 7275 | * |
| 7276 | */ |
| 7277 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7278 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7279 | lua_newtable(gL.T); |
| 7280 | |
| 7281 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7282 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7283 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7284 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7285 | /* Register special functions. */ |
| 7286 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7287 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7288 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7289 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7290 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7291 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7292 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7293 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7294 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7295 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7296 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7297 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7298 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7299 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7300 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7301 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7302 | hlua_class_function(gL.T, "log", hlua_log); |
| 7303 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7304 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7305 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7306 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7307 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7308 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7309 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7310 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7311 | |
| 7312 | /* |
| 7313 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7314 | * Register class Map |
| 7315 | * |
| 7316 | */ |
| 7317 | |
| 7318 | /* This table entry is the object "Map" base. */ |
| 7319 | lua_newtable(gL.T); |
| 7320 | |
| 7321 | /* register pattern types. */ |
| 7322 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7323 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7324 | for (i=0; i<PAT_MATCH_NUM; i++) { |
| 7325 | snprintf(trash.str, trash.size, "_%s", pat_match_names[i]); |
| 7326 | hlua_class_const_int(gL.T, trash.str, i); |
| 7327 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7328 | |
| 7329 | /* register constructor. */ |
| 7330 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7331 | |
| 7332 | /* Create and fill the metatable. */ |
| 7333 | lua_newtable(gL.T); |
| 7334 | |
| 7335 | /* Create and fille the __index entry. */ |
| 7336 | lua_pushstring(gL.T, "__index"); |
| 7337 | lua_newtable(gL.T); |
| 7338 | |
| 7339 | /* Register . */ |
| 7340 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7341 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7342 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7343 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7344 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7345 | /* Register previous table in the registry with reference and named entry. |
| 7346 | * The function hlua_register_metatable() pops the stack, so we |
| 7347 | * previously create a copy of the table. |
| 7348 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7349 | 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] | 7350 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7351 | |
| 7352 | /* Assign the metatable to the mai Map object. */ |
| 7353 | lua_setmetatable(gL.T, -2); |
| 7354 | |
| 7355 | /* Set a name to the table. */ |
| 7356 | lua_setglobal(gL.T, "Map"); |
| 7357 | |
| 7358 | /* |
| 7359 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7360 | * Register class Channel |
| 7361 | * |
| 7362 | */ |
| 7363 | |
| 7364 | /* Create and fill the metatable. */ |
| 7365 | lua_newtable(gL.T); |
| 7366 | |
| 7367 | /* Create and fille the __index entry. */ |
| 7368 | lua_pushstring(gL.T, "__index"); |
| 7369 | lua_newtable(gL.T); |
| 7370 | |
| 7371 | /* Register . */ |
| 7372 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7373 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7374 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7375 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7376 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7377 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7378 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7379 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7380 | 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] | 7381 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7382 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7383 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7384 | |
| 7385 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7386 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7387 | |
| 7388 | /* |
| 7389 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7390 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7391 | * |
| 7392 | */ |
| 7393 | |
| 7394 | /* Create and fill the metatable. */ |
| 7395 | lua_newtable(gL.T); |
| 7396 | |
| 7397 | /* Create and fille the __index entry. */ |
| 7398 | lua_pushstring(gL.T, "__index"); |
| 7399 | lua_newtable(gL.T); |
| 7400 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7401 | /* Browse existing fetches and create the associated |
| 7402 | * object method. |
| 7403 | */ |
| 7404 | sf = NULL; |
| 7405 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7406 | |
| 7407 | /* Dont register the keywork if the arguments check function are |
| 7408 | * not safe during the runtime. |
| 7409 | */ |
| 7410 | if ((sf->val_args != NULL) && |
| 7411 | (sf->val_args != val_payload_lv) && |
| 7412 | (sf->val_args != val_hdr)) |
| 7413 | continue; |
| 7414 | |
| 7415 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7416 | * by an underscore. |
| 7417 | */ |
| 7418 | strncpy(trash.str, sf->kw, trash.size); |
| 7419 | trash.str[trash.size - 1] = '\0'; |
| 7420 | for (p = trash.str; *p; p++) |
| 7421 | if (*p == '.' || *p == '-' || *p == '+') |
| 7422 | *p = '_'; |
| 7423 | |
| 7424 | /* Register the function. */ |
| 7425 | lua_pushstring(gL.T, trash.str); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7426 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7427 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7428 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7429 | } |
| 7430 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7431 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7432 | |
| 7433 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7434 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7435 | |
| 7436 | /* |
| 7437 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7438 | * Register class Converters |
| 7439 | * |
| 7440 | */ |
| 7441 | |
| 7442 | /* Create and fill the metatable. */ |
| 7443 | lua_newtable(gL.T); |
| 7444 | |
| 7445 | /* Create and fill the __index entry. */ |
| 7446 | lua_pushstring(gL.T, "__index"); |
| 7447 | lua_newtable(gL.T); |
| 7448 | |
| 7449 | /* Browse existing converters and create the associated |
| 7450 | * object method. |
| 7451 | */ |
| 7452 | sc = NULL; |
| 7453 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7454 | /* Dont register the keywork if the arguments check function are |
| 7455 | * not safe during the runtime. |
| 7456 | */ |
| 7457 | if (sc->val_args != NULL) |
| 7458 | continue; |
| 7459 | |
| 7460 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7461 | * by an underscore. |
| 7462 | */ |
| 7463 | strncpy(trash.str, sc->kw, trash.size); |
| 7464 | trash.str[trash.size - 1] = '\0'; |
| 7465 | for (p = trash.str; *p; p++) |
| 7466 | if (*p == '.' || *p == '-' || *p == '+') |
| 7467 | *p = '_'; |
| 7468 | |
| 7469 | /* Register the function. */ |
| 7470 | lua_pushstring(gL.T, trash.str); |
| 7471 | lua_pushlightuserdata(gL.T, sc); |
| 7472 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7473 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7474 | } |
| 7475 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7476 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7477 | |
| 7478 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7479 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7480 | |
| 7481 | /* |
| 7482 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7483 | * Register class HTTP |
| 7484 | * |
| 7485 | */ |
| 7486 | |
| 7487 | /* Create and fill the metatable. */ |
| 7488 | lua_newtable(gL.T); |
| 7489 | |
| 7490 | /* Create and fille the __index entry. */ |
| 7491 | lua_pushstring(gL.T, "__index"); |
| 7492 | lua_newtable(gL.T); |
| 7493 | |
| 7494 | /* Register Lua functions. */ |
| 7495 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7496 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7497 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7498 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7499 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7500 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7501 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7502 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7503 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7504 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7505 | |
| 7506 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7507 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7508 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7509 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7510 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7511 | 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] | 7512 | 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] | 7513 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7514 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7515 | |
| 7516 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7517 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7518 | |
| 7519 | /* |
| 7520 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7521 | * Register class AppletTCP |
| 7522 | * |
| 7523 | */ |
| 7524 | |
| 7525 | /* Create and fill the metatable. */ |
| 7526 | lua_newtable(gL.T); |
| 7527 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7528 | /* Create and fille the __index entry. */ |
| 7529 | lua_pushstring(gL.T, "__index"); |
| 7530 | lua_newtable(gL.T); |
| 7531 | |
| 7532 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7533 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7534 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7535 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7536 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7537 | 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] | 7538 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7539 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7540 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7541 | |
| 7542 | lua_settable(gL.T, -3); |
| 7543 | |
| 7544 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7545 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7546 | |
| 7547 | /* |
| 7548 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7549 | * Register class AppletHTTP |
| 7550 | * |
| 7551 | */ |
| 7552 | |
| 7553 | /* Create and fill the metatable. */ |
| 7554 | lua_newtable(gL.T); |
| 7555 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7556 | /* Create and fille the __index entry. */ |
| 7557 | lua_pushstring(gL.T, "__index"); |
| 7558 | lua_newtable(gL.T); |
| 7559 | |
| 7560 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7561 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7562 | 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] | 7563 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7564 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7565 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7566 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7567 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7568 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7569 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7570 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7571 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 7572 | |
| 7573 | lua_settable(gL.T, -3); |
| 7574 | |
| 7575 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7576 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7577 | |
| 7578 | /* |
| 7579 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7580 | * Register class TXN |
| 7581 | * |
| 7582 | */ |
| 7583 | |
| 7584 | /* Create and fill the metatable. */ |
| 7585 | lua_newtable(gL.T); |
| 7586 | |
| 7587 | /* Create and fille the __index entry. */ |
| 7588 | lua_pushstring(gL.T, "__index"); |
| 7589 | lua_newtable(gL.T); |
| 7590 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7591 | /* Register Lua functions. */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7592 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 7593 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7594 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7595 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7596 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7597 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7598 | hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel); |
| 7599 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 7600 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7601 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 7602 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 7603 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 7604 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 7605 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 7606 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7607 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7608 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7609 | |
| 7610 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7611 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7612 | |
| 7613 | /* |
| 7614 | * |
| 7615 | * Register class Socket |
| 7616 | * |
| 7617 | */ |
| 7618 | |
| 7619 | /* Create and fill the metatable. */ |
| 7620 | lua_newtable(gL.T); |
| 7621 | |
| 7622 | /* Create and fille the __index entry. */ |
| 7623 | lua_pushstring(gL.T, "__index"); |
| 7624 | lua_newtable(gL.T); |
| 7625 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7626 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7627 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7628 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7629 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 7630 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 7631 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 7632 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 7633 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 7634 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 7635 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 7636 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 7637 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7638 | 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] | 7639 | |
| 7640 | /* Register the garbage collector entry. */ |
| 7641 | lua_pushstring(gL.T, "__gc"); |
| 7642 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7643 | 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] | 7644 | |
| 7645 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7646 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7647 | |
| 7648 | /* Proxy and server configuration initialisation. */ |
| 7649 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 7650 | init_new_proxy(&socket_proxy); |
| 7651 | socket_proxy.parent = NULL; |
| 7652 | socket_proxy.last_change = now.tv_sec; |
| 7653 | socket_proxy.id = "LUA-SOCKET"; |
| 7654 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 7655 | socket_proxy.maxconn = 0; |
| 7656 | socket_proxy.accept = NULL; |
| 7657 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 7658 | socket_proxy.srv = NULL; |
| 7659 | socket_proxy.conn_retries = 0; |
| 7660 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 7661 | |
| 7662 | /* Init TCP server: unchanged parameters */ |
| 7663 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 7664 | socket_tcp.next = NULL; |
| 7665 | socket_tcp.proxy = &socket_proxy; |
| 7666 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 7667 | LIST_INIT(&socket_tcp.actconns); |
| 7668 | LIST_INIT(&socket_tcp.pendconns); |
Willy Tarreau | 600802a | 2015-08-04 17:19:06 +0200 | [diff] [blame] | 7669 | LIST_INIT(&socket_tcp.priv_conns); |
Willy Tarreau | 173a1c6 | 2015-08-05 10:31:57 +0200 | [diff] [blame] | 7670 | LIST_INIT(&socket_tcp.idle_conns); |
Willy Tarreau | 7017cb0 | 2015-08-05 16:35:23 +0200 | [diff] [blame] | 7671 | LIST_INIT(&socket_tcp.safe_conns); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7672 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7673 | socket_tcp.last_change = 0; |
| 7674 | socket_tcp.id = "LUA-TCP-CONN"; |
| 7675 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7676 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7677 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 7678 | |
| 7679 | /* XXX: Copy default parameter from default server, |
| 7680 | * but the default server is not initialized. |
| 7681 | */ |
| 7682 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7683 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 7684 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 7685 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 7686 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 7687 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7688 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7689 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7690 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 7691 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 7692 | |
| 7693 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 7694 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 7695 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 7696 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 7697 | socket_tcp.check.server = &socket_tcp; |
| 7698 | |
| 7699 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 7700 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7701 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7702 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 7703 | socket_tcp.agent.server = &socket_tcp; |
| 7704 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 7705 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7706 | |
| 7707 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7708 | /* Init TCP server: unchanged parameters */ |
| 7709 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 7710 | socket_ssl.next = NULL; |
| 7711 | socket_ssl.proxy = &socket_proxy; |
| 7712 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 7713 | LIST_INIT(&socket_ssl.actconns); |
| 7714 | LIST_INIT(&socket_ssl.pendconns); |
Willy Tarreau | 600802a | 2015-08-04 17:19:06 +0200 | [diff] [blame] | 7715 | LIST_INIT(&socket_ssl.priv_conns); |
Willy Tarreau | 173a1c6 | 2015-08-05 10:31:57 +0200 | [diff] [blame] | 7716 | LIST_INIT(&socket_ssl.idle_conns); |
Willy Tarreau | 7017cb0 | 2015-08-05 16:35:23 +0200 | [diff] [blame] | 7717 | LIST_INIT(&socket_ssl.safe_conns); |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7718 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7719 | socket_ssl.last_change = 0; |
| 7720 | socket_ssl.id = "LUA-SSL-CONN"; |
| 7721 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7722 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7723 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 7724 | |
| 7725 | /* XXX: Copy default parameter from default server, |
| 7726 | * but the default server is not initialized. |
| 7727 | */ |
| 7728 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7729 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 7730 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 7731 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 7732 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 7733 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7734 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7735 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7736 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 7737 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 7738 | |
| 7739 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 7740 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 7741 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 7742 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 7743 | socket_ssl.check.server = &socket_ssl; |
| 7744 | |
| 7745 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 7746 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7747 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7748 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 7749 | socket_ssl.agent.server = &socket_ssl; |
| 7750 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7751 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 7752 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7753 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7754 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7755 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 7756 | /* |
| 7757 | * |
| 7758 | * If the keyword is not known, we can search in the registered |
| 7759 | * server keywords. This is usefull to configure special SSL |
| 7760 | * features like client certificates and ssl_verify. |
| 7761 | * |
| 7762 | */ |
| 7763 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 7764 | if (tmp_error != 0) { |
| 7765 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 7766 | abort(); /* This must be never arrives because the command line |
| 7767 | not editable by the user. */ |
| 7768 | } |
| 7769 | idx += kw->skip; |
| 7770 | } |
| 7771 | } |
| 7772 | |
| 7773 | /* Initialize SSL server. */ |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 7774 | if (socket_ssl.xprt->prepare_srv) |
| 7775 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7776 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7777 | |
| 7778 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7779 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 7780 | |
| 7781 | __attribute__((constructor)) |
| 7782 | static void __hlua_init(void) |
| 7783 | { |
| 7784 | char *ptr = NULL; |
| 7785 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 7786 | hap_register_build_opts(ptr, 1); |
| 7787 | } |