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> |
| 27 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 28 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 29 | #include <types/hlua.h> |
| 30 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 31 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 32 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 33 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 34 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 35 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 36 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 37 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 38 | #include <proto/stats.h> |
Thierry FOURNIER | 9a819e7 | 2015-02-16 20:22:55 +0100 | [diff] [blame] | 39 | #include <proto/hdr_idx.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 40 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 41 | #include <proto/hlua_fcn.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 42 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 43 | #include <proto/obj_type.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 44 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 45 | #include <proto/payload.h> |
| 46 | #include <proto/proto_http.h> |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 47 | #include <proto/proto_tcp.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 48 | #include <proto/raw_sock.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 49 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 50 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 51 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 52 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 53 | #include <proto/ssl_sock.h> |
| 54 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 55 | #include <proto/task.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 56 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 57 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 58 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 59 | * macro is used only for identifying the function that can |
| 60 | * not return because a longjmp is executed. |
| 61 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 62 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 63 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 64 | */ |
| 65 | #define __LJMP |
| 66 | #define WILL_LJMP(func) func |
| 67 | #define MAY_LJMP(func) func |
| 68 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 69 | /* This couple of function executes securely some Lua calls outside of |
| 70 | * the lua runtime environment. Each Lua call can return a longjmp |
| 71 | * if it encounter a memory error. |
| 72 | * |
| 73 | * Lua documentation extract: |
| 74 | * |
| 75 | * If an error happens outside any protected environment, Lua calls |
| 76 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 77 | * exiting the host application. Your panic function can avoid this |
| 78 | * exit by never returning (e.g., doing a long jump to your own |
| 79 | * recovery point outside Lua). |
| 80 | * |
| 81 | * The panic function runs as if it were a message handler (see |
| 82 | * §2.3); in particular, the error message is at the top of the |
| 83 | * stack. However, there is no guarantee about stack space. To push |
| 84 | * anything on the stack, the panic function must first check the |
| 85 | * available space (see §4.2). |
| 86 | * |
| 87 | * We must check all the Lua entry point. This includes: |
| 88 | * - The include/proto/hlua.h exported functions |
| 89 | * - the task wrapper function |
| 90 | * - The action wrapper function |
| 91 | * - The converters wrapper function |
| 92 | * - The sample-fetch wrapper functions |
| 93 | * |
| 94 | * It is tolerated that the initilisation function returns an abort. |
| 95 | * Before each Lua abort, an error message is writed on stderr. |
| 96 | * |
| 97 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 98 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 99 | * because they must be exists in the program stack when the longjmp |
| 100 | * is called. |
| 101 | */ |
| 102 | jmp_buf safe_ljmp_env; |
| 103 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 104 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 105 | |
| 106 | #define SET_SAFE_LJMP(__L) \ |
| 107 | ({ \ |
| 108 | int ret; \ |
| 109 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 110 | lua_atpanic(__L, hlua_panic_safe); \ |
| 111 | ret = 0; \ |
| 112 | } else { \ |
| 113 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 114 | ret = 1; \ |
| 115 | } \ |
| 116 | ret; \ |
| 117 | }) |
| 118 | |
| 119 | /* If we are the last function catching Lua errors, we |
| 120 | * must reset the panic function. |
| 121 | */ |
| 122 | #define RESET_SAFE_LJMP(__L) \ |
| 123 | do { \ |
| 124 | lua_atpanic(__L, hlua_panic_safe); \ |
| 125 | } while(0) |
| 126 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 127 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 128 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
| 129 | #define APPLET_100C 0x02 /* 100 continue expected. */ |
| 130 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
| 131 | #define APPLET_CHUNKED 0x08 /* Use transfer encoding chunked. */ |
| 132 | #define APPLET_LAST_CHK 0x10 /* Last chunk sent. */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 133 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 134 | |
| 135 | #define HTTP_100C "HTTP/1.1 100 Continue\r\n\r\n" |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 136 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 137 | /* The main Lua execution context. */ |
| 138 | struct hlua gL; |
| 139 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 140 | /* This is the memory pool containing all the signal structs. These |
| 141 | * struct are used to store each requiered signal between two tasks. |
| 142 | */ |
| 143 | struct pool_head *pool2_hlua_com; |
| 144 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 145 | /* Used for Socket connection. */ |
| 146 | static struct proxy socket_proxy; |
| 147 | static struct server socket_tcp; |
| 148 | #ifdef USE_OPENSSL |
| 149 | static struct server socket_ssl; |
| 150 | #endif |
| 151 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 152 | /* List head of the function called at the initialisation time. */ |
| 153 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 154 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 155 | /* The following variables contains the reference of the different |
| 156 | * Lua classes. These references are useful for identify metadata |
| 157 | * associated with an object. |
| 158 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 159 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 160 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 161 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 162 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 163 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 164 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 165 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 166 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 167 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 168 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 169 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 170 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 171 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 172 | * because a task may remain alive during all the haproxy execution. |
| 173 | */ |
| 174 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 175 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 176 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 177 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 178 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 179 | * it is used for preventing infinite loops. |
| 180 | * |
| 181 | * I test the scheer with an infinite loop containing one incrementation |
| 182 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 183 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 184 | * to one interrupt each 10 000 instructions. |
| 185 | * |
| 186 | * configured | Number of |
| 187 | * instructions | loops executed |
| 188 | * between two | in milions |
| 189 | * forced yields | |
| 190 | * ---------------+--------------- |
| 191 | * 10 | 160 |
| 192 | * 500 | 670 |
| 193 | * 1000 | 680 |
| 194 | * 5000 | 700 |
| 195 | * 7000 | 700 |
| 196 | * 8000 | 700 |
| 197 | * 9000 | 710 <- ceil |
| 198 | * 10000 | 710 |
| 199 | * 100000 | 710 |
| 200 | * 1000000 | 710 |
| 201 | * |
| 202 | */ |
| 203 | static unsigned int hlua_nb_instruction = 10000; |
| 204 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 205 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 206 | * be enforced on any memory allocation. |
| 207 | */ |
| 208 | struct hlua_mem_allocator { |
| 209 | size_t allocated; |
| 210 | size_t limit; |
| 211 | }; |
| 212 | |
| 213 | static struct hlua_mem_allocator hlua_global_allocator; |
| 214 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 215 | static const char error_500[] = |
| 216 | "HTTP/1.0 500 Server Error\r\n" |
| 217 | "Cache-Control: no-cache\r\n" |
| 218 | "Connection: close\r\n" |
| 219 | "Content-Type: text/html\r\n" |
| 220 | "\r\n" |
| 221 | "<html><body><h1>500 Server Error</h1>\nAn internal server error occured.\n</body></html>\n"; |
| 222 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 223 | /* These functions converts types between HAProxy internal args or |
| 224 | * sample and LUA types. Another function permits to check if the |
| 225 | * LUA stack contains arguments according with an required ARG_T |
| 226 | * format. |
| 227 | */ |
| 228 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 229 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 230 | __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] | 231 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 232 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 233 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 234 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 235 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 236 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 237 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 238 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 239 | do { \ |
| 240 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 241 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
| 242 | Alert(__fmt, ## __args); \ |
| 243 | } while (0) |
| 244 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 245 | /* Used to check an Lua function type in the stack. It creates and |
| 246 | * returns a reference of the function. This function throws an |
| 247 | * error if the rgument is not a "function". |
| 248 | */ |
| 249 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 250 | { |
| 251 | if (!lua_isfunction(L, argno)) { |
| 252 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, -1)); |
| 253 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 254 | } |
| 255 | lua_pushvalue(L, argno); |
| 256 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 257 | } |
| 258 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 259 | /* Return the string that is of the top of the stack. */ |
| 260 | const char *hlua_get_top_error_string(lua_State *L) |
| 261 | { |
| 262 | if (lua_gettop(L) < 1) |
| 263 | return "unknown error"; |
| 264 | if (lua_type(L, -1) != LUA_TSTRING) |
| 265 | return "unknown error"; |
| 266 | return lua_tostring(L, -1); |
| 267 | } |
| 268 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 269 | /* This function check the number of arguments available in the |
| 270 | * stack. If the number of arguments available is not the same |
| 271 | * then <nb> an error is throwed. |
| 272 | */ |
| 273 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 274 | { |
| 275 | if (lua_gettop(L) == nb) |
| 276 | return; |
| 277 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 278 | } |
| 279 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 280 | /* This fucntion push an error string prefixed by the file name |
| 281 | * and the line number where the error is encountered. |
| 282 | */ |
| 283 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 284 | { |
| 285 | va_list argp; |
| 286 | va_start(argp, fmt); |
| 287 | luaL_where(L, 1); |
| 288 | lua_pushvfstring(L, fmt, argp); |
| 289 | va_end(argp); |
| 290 | lua_concat(L, 2); |
| 291 | return 1; |
| 292 | } |
| 293 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 294 | /* This function register a new signal. "lua" is the current lua |
| 295 | * execution context. It contains a pointer to the associated task. |
| 296 | * "link" is a list head attached to an other task that must be wake |
| 297 | * the lua task if an event occurs. This is useful with external |
| 298 | * events like TCP I/O or sleep functions. This funcion allocate |
| 299 | * memory for the signal. |
| 300 | */ |
| 301 | static int hlua_com_new(struct hlua *lua, struct list *link) |
| 302 | { |
| 303 | struct hlua_com *com = pool_alloc2(pool2_hlua_com); |
| 304 | if (!com) |
| 305 | return 0; |
| 306 | LIST_ADDQ(&lua->com, &com->purge_me); |
| 307 | LIST_ADDQ(link, &com->wake_me); |
| 308 | com->task = lua->task; |
| 309 | return 1; |
| 310 | } |
| 311 | |
| 312 | /* This function purge all the pending signals when the LUA execution |
| 313 | * is finished. This prevent than a coprocess try to wake a deleted |
| 314 | * task. This function remove the memory associated to the signal. |
| 315 | */ |
| 316 | static void hlua_com_purge(struct hlua *lua) |
| 317 | { |
| 318 | struct hlua_com *com, *back; |
| 319 | |
| 320 | /* Delete all pending communication signals. */ |
| 321 | list_for_each_entry_safe(com, back, &lua->com, purge_me) { |
| 322 | LIST_DEL(&com->purge_me); |
| 323 | LIST_DEL(&com->wake_me); |
| 324 | pool_free2(pool2_hlua_com, com); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | /* This function sends signals. It wakes all the tasks attached |
| 329 | * to a list head, and remove the signal, and free the used |
| 330 | * memory. |
| 331 | */ |
| 332 | static void hlua_com_wake(struct list *wake) |
| 333 | { |
| 334 | struct hlua_com *com, *back; |
| 335 | |
| 336 | /* Wake task and delete all pending communication signals. */ |
| 337 | list_for_each_entry_safe(com, back, wake, wake_me) { |
| 338 | LIST_DEL(&com->purge_me); |
| 339 | LIST_DEL(&com->wake_me); |
| 340 | task_wakeup(com->task, TASK_WOKEN_MSG); |
| 341 | pool_free2(pool2_hlua_com, com); |
| 342 | } |
| 343 | } |
| 344 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 345 | /* This functions is used with sample fetch and converters. It |
| 346 | * converts the HAProxy configuration argument in a lua stack |
| 347 | * values. |
| 348 | * |
| 349 | * It takes an array of "arg", and each entry of the array is |
| 350 | * converted and pushed in the LUA stack. |
| 351 | */ |
| 352 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 353 | { |
| 354 | switch (arg->type) { |
| 355 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 356 | case ARGT_TIME: |
| 357 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 358 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 359 | break; |
| 360 | |
| 361 | case ARGT_STR: |
| 362 | lua_pushlstring(L, arg->data.str.str, arg->data.str.len); |
| 363 | break; |
| 364 | |
| 365 | case ARGT_IPV4: |
| 366 | case ARGT_IPV6: |
| 367 | case ARGT_MSK4: |
| 368 | case ARGT_MSK6: |
| 369 | case ARGT_FE: |
| 370 | case ARGT_BE: |
| 371 | case ARGT_TAB: |
| 372 | case ARGT_SRV: |
| 373 | case ARGT_USR: |
| 374 | case ARGT_MAP: |
| 375 | default: |
| 376 | lua_pushnil(L); |
| 377 | break; |
| 378 | } |
| 379 | return 1; |
| 380 | } |
| 381 | |
| 382 | /* This function take one entrie in an LUA stack at the index "ud", |
| 383 | * and try to convert it in an HAProxy argument entry. This is useful |
| 384 | * with sample fetch wrappers. The input arguments are gived to the |
| 385 | * lua wrapper and converted as arg list by thi function. |
| 386 | */ |
| 387 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 388 | { |
| 389 | switch (lua_type(L, ud)) { |
| 390 | |
| 391 | case LUA_TNUMBER: |
| 392 | case LUA_TBOOLEAN: |
| 393 | arg->type = ARGT_SINT; |
| 394 | arg->data.sint = lua_tointeger(L, ud); |
| 395 | break; |
| 396 | |
| 397 | case LUA_TSTRING: |
| 398 | arg->type = ARGT_STR; |
| 399 | arg->data.str.str = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.len); |
| 400 | break; |
| 401 | |
| 402 | case LUA_TUSERDATA: |
| 403 | case LUA_TNIL: |
| 404 | case LUA_TTABLE: |
| 405 | case LUA_TFUNCTION: |
| 406 | case LUA_TTHREAD: |
| 407 | case LUA_TLIGHTUSERDATA: |
| 408 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 409 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 410 | break; |
| 411 | } |
| 412 | return 1; |
| 413 | } |
| 414 | |
| 415 | /* the following functions are used to convert a struct sample |
| 416 | * in Lua type. This useful to convert the return of the |
| 417 | * fetchs or converters. |
| 418 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 419 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 420 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 421 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 422 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 423 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 424 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 425 | break; |
| 426 | |
| 427 | case SMP_T_BIN: |
| 428 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 429 | 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] | 430 | break; |
| 431 | |
| 432 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 433 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 434 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 435 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 436 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 437 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 438 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 439 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 440 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 441 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 442 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 443 | 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] | 444 | break; |
| 445 | default: |
| 446 | lua_pushnil(L); |
| 447 | break; |
| 448 | } |
| 449 | break; |
| 450 | |
| 451 | case SMP_T_IPV4: |
| 452 | case SMP_T_IPV6: |
| 453 | 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] | 454 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 455 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 456 | 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] | 457 | else |
| 458 | lua_pushnil(L); |
| 459 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 460 | default: |
| 461 | lua_pushnil(L); |
| 462 | break; |
| 463 | } |
| 464 | return 1; |
| 465 | } |
| 466 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 467 | /* the following functions are used to convert a struct sample |
| 468 | * in Lua strings. This is useful to convert the return of the |
| 469 | * fetchs or converters. |
| 470 | */ |
| 471 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 472 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 473 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 474 | |
| 475 | case SMP_T_BIN: |
| 476 | case SMP_T_STR: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 477 | lua_pushlstring(L, smp->data.u.str.str, smp->data.u.str.len); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 478 | break; |
| 479 | |
| 480 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 481 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 482 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 483 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 484 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 485 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 486 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 487 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 488 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 489 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 490 | case HTTP_METH_OTHER: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 491 | 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] | 492 | break; |
| 493 | default: |
| 494 | lua_pushstring(L, ""); |
| 495 | break; |
| 496 | } |
| 497 | break; |
| 498 | |
| 499 | case SMP_T_SINT: |
| 500 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 501 | case SMP_T_IPV4: |
| 502 | case SMP_T_IPV6: |
| 503 | 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] | 504 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 505 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 506 | 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] | 507 | else |
| 508 | lua_pushstring(L, ""); |
| 509 | break; |
| 510 | default: |
| 511 | lua_pushstring(L, ""); |
| 512 | break; |
| 513 | } |
| 514 | return 1; |
| 515 | } |
| 516 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 517 | /* the following functions are used to convert an Lua type in a |
| 518 | * struct sample. This is useful to provide data from a converter |
| 519 | * to the LUA code. |
| 520 | */ |
| 521 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 522 | { |
| 523 | switch (lua_type(L, ud)) { |
| 524 | |
| 525 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 526 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 527 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 528 | break; |
| 529 | |
| 530 | |
| 531 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 532 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 533 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 534 | break; |
| 535 | |
| 536 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 537 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 538 | smp->flags |= SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 539 | 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] | 540 | break; |
| 541 | |
| 542 | case LUA_TUSERDATA: |
| 543 | case LUA_TNIL: |
| 544 | case LUA_TTABLE: |
| 545 | case LUA_TFUNCTION: |
| 546 | case LUA_TTHREAD: |
| 547 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 548 | case LUA_TNONE: |
| 549 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 550 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 551 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 552 | break; |
| 553 | } |
| 554 | return 1; |
| 555 | } |
| 556 | |
| 557 | /* This function check the "argp" builded by another conversion function |
| 558 | * is in accord with the expected argp defined by the "mask". The fucntion |
| 559 | * 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] | 560 | * |
| 561 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 562 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 563 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 564 | __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] | 565 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 566 | { |
| 567 | int min_arg; |
| 568 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 569 | struct proxy *px; |
| 570 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 571 | |
| 572 | idx = 0; |
| 573 | min_arg = ARGM(mask); |
| 574 | mask >>= ARGM_BITS; |
| 575 | |
| 576 | while (1) { |
| 577 | |
| 578 | /* Check oversize. */ |
| 579 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 580 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /* Check for mandatory arguments. */ |
| 584 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 585 | if (idx < min_arg) { |
| 586 | |
| 587 | /* If miss other argument than the first one, we return an error. */ |
| 588 | if (idx > 0) |
| 589 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 590 | |
| 591 | /* If first argument have a certain type, some default values |
| 592 | * may be used. See the function smp_resolve_args(). |
| 593 | */ |
| 594 | switch (mask & ARGT_MASK) { |
| 595 | |
| 596 | case ARGT_FE: |
| 597 | if (!(p->cap & PR_CAP_FE)) |
| 598 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 599 | argp[idx].data.prx = p; |
| 600 | argp[idx].type = ARGT_FE; |
| 601 | argp[idx+1].type = ARGT_STOP; |
| 602 | break; |
| 603 | |
| 604 | case ARGT_BE: |
| 605 | if (!(p->cap & PR_CAP_BE)) |
| 606 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 607 | argp[idx].data.prx = p; |
| 608 | argp[idx].type = ARGT_BE; |
| 609 | argp[idx+1].type = ARGT_STOP; |
| 610 | break; |
| 611 | |
| 612 | case ARGT_TAB: |
| 613 | argp[idx].data.prx = p; |
| 614 | argp[idx].type = ARGT_TAB; |
| 615 | argp[idx+1].type = ARGT_STOP; |
| 616 | break; |
| 617 | |
| 618 | default: |
| 619 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 620 | break; |
| 621 | } |
| 622 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | /* Check for exceed the number of requiered argument. */ |
| 627 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 628 | argp[idx].type != ARGT_STOP) { |
| 629 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 630 | } |
| 631 | |
| 632 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 633 | argp[idx].type == ARGT_STOP) { |
| 634 | return 0; |
| 635 | } |
| 636 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 637 | /* Convert some argument types. */ |
| 638 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 639 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 640 | if (argp[idx].type != ARGT_SINT) |
| 641 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 642 | argp[idx].type = ARGT_SINT; |
| 643 | break; |
| 644 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 645 | case ARGT_TIME: |
| 646 | if (argp[idx].type != ARGT_SINT) |
| 647 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 648 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 649 | break; |
| 650 | |
| 651 | case ARGT_SIZE: |
| 652 | if (argp[idx].type != ARGT_SINT) |
| 653 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 654 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 655 | break; |
| 656 | |
| 657 | case ARGT_FE: |
| 658 | if (argp[idx].type != ARGT_STR) |
| 659 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 660 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 661 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 662 | argp[idx].data.prx = proxy_fe_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 663 | if (!argp[idx].data.prx) |
| 664 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 665 | argp[idx].type = ARGT_FE; |
| 666 | break; |
| 667 | |
| 668 | case ARGT_BE: |
| 669 | if (argp[idx].type != ARGT_STR) |
| 670 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 671 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 672 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 673 | argp[idx].data.prx = proxy_be_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 674 | if (!argp[idx].data.prx) |
| 675 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 676 | argp[idx].type = ARGT_BE; |
| 677 | break; |
| 678 | |
| 679 | case ARGT_TAB: |
| 680 | if (argp[idx].type != ARGT_STR) |
| 681 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 682 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 683 | trash.str[argp[idx].data.str.len] = 0; |
Willy Tarreau | e2dc1fa | 2015-05-26 12:08:07 +0200 | [diff] [blame] | 684 | argp[idx].data.prx = proxy_tbl_by_name(trash.str); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 685 | if (!argp[idx].data.prx) |
| 686 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 687 | argp[idx].type = ARGT_TAB; |
| 688 | break; |
| 689 | |
| 690 | case ARGT_SRV: |
| 691 | if (argp[idx].type != ARGT_STR) |
| 692 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
| 693 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 694 | trash.str[argp[idx].data.str.len] = 0; |
| 695 | sname = strrchr(trash.str, '/'); |
| 696 | if (sname) { |
| 697 | *sname++ = '\0'; |
| 698 | pname = trash.str; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 699 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 700 | if (!px) |
| 701 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 702 | } |
| 703 | else { |
| 704 | sname = trash.str; |
| 705 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 706 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 707 | argp[idx].data.srv = findserver(px, sname); |
| 708 | if (!argp[idx].data.srv) |
| 709 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 710 | argp[idx].type = ARGT_SRV; |
| 711 | break; |
| 712 | |
| 713 | case ARGT_IPV4: |
| 714 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 715 | trash.str[argp[idx].data.str.len] = 0; |
| 716 | if (inet_pton(AF_INET, trash.str, &argp[idx].data.ipv4)) |
| 717 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 718 | argp[idx].type = ARGT_IPV4; |
| 719 | break; |
| 720 | |
| 721 | case ARGT_MSK4: |
| 722 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 723 | trash.str[argp[idx].data.str.len] = 0; |
| 724 | if (!str2mask(trash.str, &argp[idx].data.ipv4)) |
| 725 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 726 | argp[idx].type = ARGT_MSK4; |
| 727 | break; |
| 728 | |
| 729 | case ARGT_IPV6: |
| 730 | memcpy(trash.str, argp[idx].data.str.str, argp[idx].data.str.len); |
| 731 | trash.str[argp[idx].data.str.len] = 0; |
| 732 | if (inet_pton(AF_INET6, trash.str, &argp[idx].data.ipv6)) |
| 733 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 734 | argp[idx].type = ARGT_IPV6; |
| 735 | break; |
| 736 | |
| 737 | case ARGT_MSK6: |
| 738 | case ARGT_MAP: |
| 739 | case ARGT_REG: |
| 740 | case ARGT_USR: |
| 741 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 742 | break; |
| 743 | } |
| 744 | |
| 745 | /* Check for type of argument. */ |
| 746 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 747 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 748 | arg_type_names[(mask & ARGT_MASK)], |
| 749 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 750 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 751 | } |
| 752 | |
| 753 | /* Next argument. */ |
| 754 | mask >>= ARGT_BITS; |
| 755 | idx++; |
| 756 | } |
| 757 | } |
| 758 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 759 | /* |
| 760 | * The following functions are used to make correspondance between the the |
| 761 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 762 | * |
| 763 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 764 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 765 | */ |
| 766 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 767 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 768 | struct hlua **hlua = lua_getextraspace(L); |
| 769 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 770 | } |
| 771 | static inline void hlua_sethlua(struct hlua *hlua) |
| 772 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 773 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 774 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 775 | } |
| 776 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 777 | /* This function is used to send logs. It try to send on screen (stderr) |
| 778 | * and on the default syslog server. |
| 779 | */ |
| 780 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 781 | { |
| 782 | struct tm tm; |
| 783 | char *p; |
| 784 | |
| 785 | /* Cleanup the log message. */ |
| 786 | p = trash.str; |
| 787 | for (; *msg != '\0'; msg++, p++) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 788 | if (p >= trash.str + trash.size - 1) { |
| 789 | /* Break the message if exceed the buffer size. */ |
| 790 | *(p-4) = ' '; |
| 791 | *(p-3) = '.'; |
| 792 | *(p-2) = '.'; |
| 793 | *(p-1) = '.'; |
| 794 | break; |
| 795 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 796 | if (isprint(*msg)) |
| 797 | *p = *msg; |
| 798 | else |
| 799 | *p = '.'; |
| 800 | } |
| 801 | *p = '\0'; |
| 802 | |
Thierry FOURNIER | 5554e29 | 2015-09-09 11:21:37 +0200 | [diff] [blame] | 803 | send_log(px, level, "%s\n", trash.str); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 804 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 805 | get_localtime(date.tv_sec, &tm); |
| 806 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 807 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
| 808 | (int)getpid(), trash.str); |
| 809 | fflush(stderr); |
| 810 | } |
| 811 | } |
| 812 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 813 | /* This function just ensure that the yield will be always |
| 814 | * returned with a timeout and permit to set some flags |
| 815 | */ |
| 816 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 817 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 818 | { |
| 819 | struct hlua *hlua = hlua_gethlua(L); |
| 820 | |
| 821 | /* Set the wake timeout. If timeout is required, we set |
| 822 | * the expiration time. |
| 823 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 824 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 825 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 826 | hlua->flags |= flags; |
| 827 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 828 | /* Process the yield. */ |
| 829 | WILL_LJMP(lua_yieldk(L, nresults, ctx, k)); |
| 830 | } |
| 831 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 832 | /* This function initialises the Lua environment stored in the stream. |
| 833 | * 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] | 834 | * 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] | 835 | * |
| 836 | * This function is particular. it initialises a new Lua thread. If the |
| 837 | * initialisation fails (example: out of memory error), the lua function |
| 838 | * throws an error (longjmp). |
| 839 | * |
| 840 | * This function manipulates two Lua stack: the main and the thread. Only |
| 841 | * the main stack can fail. The thread is not manipulated. This function |
| 842 | * MUST NOT manipulate the created thread stack state, because is not |
| 843 | * proctected agains error throwed by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 844 | */ |
| 845 | int hlua_ctx_init(struct hlua *lua, struct task *task) |
| 846 | { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 847 | if (!SET_SAFE_LJMP(gL.T)) { |
| 848 | lua->Tref = LUA_REFNIL; |
| 849 | return 0; |
| 850 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 851 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 852 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 853 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 854 | lua->T = lua_newthread(gL.T); |
| 855 | if (!lua->T) { |
| 856 | lua->Tref = LUA_REFNIL; |
| 857 | return 0; |
| 858 | } |
| 859 | hlua_sethlua(lua); |
| 860 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 861 | lua->task = task; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 862 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 863 | return 1; |
| 864 | } |
| 865 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 866 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 867 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 868 | * is not freed. |
| 869 | */ |
| 870 | void hlua_ctx_destroy(struct hlua *lua) |
| 871 | { |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 872 | if (!lua->T) |
| 873 | return; |
| 874 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 875 | /* Purge all the pending signals. */ |
| 876 | hlua_com_purge(lua); |
| 877 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 878 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 879 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 880 | |
| 881 | /* Forces a garbage collecting process. If the Lua program is finished |
| 882 | * without error, we run the GC on the thread pointer. Its freed all |
| 883 | * the unused memory. |
| 884 | * If the thread is finnish with an error or is currently yielded, |
| 885 | * it seems that the GC applied on the thread doesn't clean anything, |
| 886 | * so e run the GC on the main thread. |
| 887 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 888 | * the garbage collection. |
| 889 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 890 | if (lua->flags & HLUA_MUST_GC) { |
| 891 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 892 | if (lua_status(lua->T) != LUA_OK) |
| 893 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 894 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 895 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 896 | lua->T = NULL; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | /* This function is used to restore the Lua context when a coroutine |
| 900 | * fails. This function copy the common memory between old coroutine |
| 901 | * and the new coroutine. The old coroutine is destroyed, and its |
| 902 | * replaced by the new coroutine. |
| 903 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 904 | * as string error message and it is copied in the new stack. |
| 905 | */ |
| 906 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 907 | { |
| 908 | lua_State *T; |
| 909 | int new_ref; |
| 910 | |
| 911 | /* Renew the main LUA stack doesn't have sense. */ |
| 912 | if (lua == &gL) |
| 913 | return 0; |
| 914 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 915 | /* New Lua coroutine. */ |
| 916 | T = lua_newthread(gL.T); |
| 917 | if (!T) |
| 918 | return 0; |
| 919 | |
| 920 | /* Copy last error message. */ |
| 921 | if (keep_msg) |
| 922 | lua_xmove(lua->T, T, 1); |
| 923 | |
| 924 | /* Copy data between the coroutines. */ |
| 925 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 926 | lua_xmove(lua->T, T, 1); |
| 927 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 928 | |
| 929 | /* Destroy old data. */ |
| 930 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 931 | |
| 932 | /* The thread is garbage collected by Lua. */ |
| 933 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 934 | |
| 935 | /* Fill the struct with the new coroutine values. */ |
| 936 | lua->Mref = new_ref; |
| 937 | lua->T = T; |
| 938 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 939 | |
| 940 | /* Set context. */ |
| 941 | hlua_sethlua(lua); |
| 942 | |
| 943 | return 1; |
| 944 | } |
| 945 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 946 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 947 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 948 | struct hlua *hlua = hlua_gethlua(L); |
| 949 | |
| 950 | /* Lua cannot yield when its returning from a function, |
| 951 | * so, we can fix the interrupt hook to 1 instruction, |
| 952 | * expecting that the function is finnished. |
| 953 | */ |
| 954 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 955 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 956 | return; |
| 957 | } |
| 958 | |
| 959 | /* restore the interrupt condition. */ |
| 960 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 961 | |
| 962 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 963 | * If the state is not yieldable, trying yield causes an error. |
| 964 | */ |
| 965 | if (lua_isyieldable(L)) |
| 966 | WILL_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 967 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 968 | /* If we cannot yield, update the clock and check the timeout. */ |
| 969 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 970 | hlua->run_time += now_ms - hlua->start_time; |
| 971 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 972 | lua_pushfstring(L, "execution timeout"); |
| 973 | WILL_LJMP(lua_error(L)); |
| 974 | } |
| 975 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 976 | /* Update the start time. */ |
| 977 | hlua->start_time = now_ms; |
| 978 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 979 | /* Try to interrupt the process at the end of the current |
| 980 | * unyieldable function. |
| 981 | */ |
| 982 | 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] | 983 | } |
| 984 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 985 | /* This function start or resumes the Lua stack execution. If the flag |
| 986 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 987 | * The function return an error. |
| 988 | * |
| 989 | * The function can returns 4 values: |
| 990 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 991 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 992 | * task wakeup. |
| 993 | * - HLUA_E_ERRMSG : An error has occured, an error message is set in |
| 994 | * the top of the stack. |
| 995 | * - HLUA_E_ERR : An error has occured without error message. |
| 996 | * |
| 997 | * If an error occured, the stack is renewed and it is ready to run new |
| 998 | * LUA code. |
| 999 | */ |
| 1000 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1001 | { |
| 1002 | int ret; |
| 1003 | const char *msg; |
| 1004 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1005 | /* Initialise run time counter. */ |
| 1006 | if (!HLUA_IS_RUNNING(lua)) |
| 1007 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1008 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1009 | resume_execution: |
| 1010 | |
| 1011 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1012 | * instructions. it is used for preventing infinite loops. |
| 1013 | */ |
| 1014 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1015 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1016 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1017 | HLUA_SET_RUN(lua); |
| 1018 | HLUA_CLR_CTRLYIELD(lua); |
| 1019 | HLUA_CLR_WAKERESWR(lua); |
| 1020 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1021 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1022 | /* Update the start time. */ |
| 1023 | lua->start_time = now_ms; |
| 1024 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1025 | /* Call the function. */ |
| 1026 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1027 | switch (ret) { |
| 1028 | |
| 1029 | case LUA_OK: |
| 1030 | ret = HLUA_E_OK; |
| 1031 | break; |
| 1032 | |
| 1033 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1034 | /* Check if the execution timeout is expired. It it is the case, we |
| 1035 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1036 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1037 | tv_update_date(0, 1); |
| 1038 | lua->run_time += now_ms - lua->start_time; |
| 1039 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1040 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1041 | if (!lua_checkstack(lua->T, 1)) { |
| 1042 | ret = HLUA_E_ERR; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1043 | break; |
| 1044 | } |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1045 | lua_pushfstring(lua->T, "execution timeout"); |
| 1046 | ret = HLUA_E_ERRMSG; |
| 1047 | break; |
| 1048 | } |
| 1049 | /* Process the forced yield. if the general yield is not allowed or |
| 1050 | * if no task were associated this the current Lua execution |
| 1051 | * coroutine, we resume the execution. Else we want to return in the |
| 1052 | * scheduler and we want to be waked up again, to continue the |
| 1053 | * current Lua execution. So we schedule our own task. |
| 1054 | */ |
| 1055 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1056 | if (!yield_allowed || !lua->task) |
| 1057 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1058 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1059 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1060 | if (!yield_allowed) { |
| 1061 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1062 | if (!lua_checkstack(lua->T, 1)) { |
| 1063 | ret = HLUA_E_ERR; |
| 1064 | break; |
| 1065 | } |
| 1066 | lua_pushfstring(lua->T, "yield not allowed"); |
| 1067 | ret = HLUA_E_ERRMSG; |
| 1068 | break; |
| 1069 | } |
| 1070 | ret = HLUA_E_AGAIN; |
| 1071 | break; |
| 1072 | |
| 1073 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1074 | |
| 1075 | /* Special exit case. The traditionnal exit is returned as an error |
| 1076 | * because the errors ares the only one mean to return immediately |
| 1077 | * from and lua execution. |
| 1078 | */ |
| 1079 | if (lua->flags & HLUA_EXIT) { |
| 1080 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1081 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1082 | break; |
| 1083 | } |
| 1084 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1085 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1086 | if (!lua_checkstack(lua->T, 1)) { |
| 1087 | ret = HLUA_E_ERR; |
| 1088 | break; |
| 1089 | } |
| 1090 | msg = lua_tostring(lua->T, -1); |
| 1091 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1092 | lua_pop(lua->T, 1); |
| 1093 | if (msg) |
| 1094 | lua_pushfstring(lua->T, "runtime error: %s", msg); |
| 1095 | else |
| 1096 | lua_pushfstring(lua->T, "unknown runtime error"); |
| 1097 | ret = HLUA_E_ERRMSG; |
| 1098 | break; |
| 1099 | |
| 1100 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1101 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1102 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1103 | if (!lua_checkstack(lua->T, 1)) { |
| 1104 | ret = HLUA_E_ERR; |
| 1105 | break; |
| 1106 | } |
| 1107 | lua_pushfstring(lua->T, "out of memory error"); |
| 1108 | ret = HLUA_E_ERRMSG; |
| 1109 | break; |
| 1110 | |
| 1111 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1112 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1113 | if (!lua_checkstack(lua->T, 1)) { |
| 1114 | ret = HLUA_E_ERR; |
| 1115 | break; |
| 1116 | } |
| 1117 | msg = lua_tostring(lua->T, -1); |
| 1118 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1119 | lua_pop(lua->T, 1); |
| 1120 | if (msg) |
| 1121 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1122 | else |
| 1123 | lua_pushfstring(lua->T, "message handler error"); |
| 1124 | ret = HLUA_E_ERRMSG; |
| 1125 | break; |
| 1126 | |
| 1127 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1128 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1129 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1130 | if (!lua_checkstack(lua->T, 1)) { |
| 1131 | ret = HLUA_E_ERR; |
| 1132 | break; |
| 1133 | } |
| 1134 | lua_pushfstring(lua->T, "unknonwn error"); |
| 1135 | ret = HLUA_E_ERRMSG; |
| 1136 | break; |
| 1137 | } |
| 1138 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1139 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1140 | if (lua->flags & HLUA_MUST_GC && |
| 1141 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1142 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1143 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1144 | switch (ret) { |
| 1145 | case HLUA_E_AGAIN: |
| 1146 | break; |
| 1147 | |
| 1148 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1149 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1150 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1151 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1152 | break; |
| 1153 | |
| 1154 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1155 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1156 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1157 | hlua_ctx_renew(lua, 0); |
| 1158 | break; |
| 1159 | |
| 1160 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1161 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1162 | hlua_com_purge(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1163 | break; |
| 1164 | } |
| 1165 | |
| 1166 | return ret; |
| 1167 | } |
| 1168 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1169 | /* This function exit the current code. */ |
| 1170 | __LJMP static int hlua_done(lua_State *L) |
| 1171 | { |
| 1172 | struct hlua *hlua = hlua_gethlua(L); |
| 1173 | |
| 1174 | hlua->flags |= HLUA_EXIT; |
| 1175 | WILL_LJMP(lua_error(L)); |
| 1176 | |
| 1177 | return 0; |
| 1178 | } |
| 1179 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1180 | /* This function is an LUA binding. It provides a function |
| 1181 | * for deleting ACL from a referenced ACL file. |
| 1182 | */ |
| 1183 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1184 | { |
| 1185 | const char *name; |
| 1186 | const char *key; |
| 1187 | struct pat_ref *ref; |
| 1188 | |
| 1189 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1190 | |
| 1191 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1192 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1193 | |
| 1194 | ref = pat_ref_lookup(name); |
| 1195 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1196 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1197 | |
| 1198 | pat_ref_delete(ref, key); |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | /* This function is an LUA binding. It provides a function |
| 1203 | * for deleting map entry from a referenced map file. |
| 1204 | */ |
| 1205 | static int hlua_del_map(lua_State *L) |
| 1206 | { |
| 1207 | const char *name; |
| 1208 | const char *key; |
| 1209 | struct pat_ref *ref; |
| 1210 | |
| 1211 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1212 | |
| 1213 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1214 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1215 | |
| 1216 | ref = pat_ref_lookup(name); |
| 1217 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1218 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1219 | |
| 1220 | pat_ref_delete(ref, key); |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
| 1224 | /* This function is an LUA binding. It provides a function |
| 1225 | * for adding ACL pattern from a referenced ACL file. |
| 1226 | */ |
| 1227 | static int hlua_add_acl(lua_State *L) |
| 1228 | { |
| 1229 | const char *name; |
| 1230 | const char *key; |
| 1231 | struct pat_ref *ref; |
| 1232 | |
| 1233 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1234 | |
| 1235 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1236 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1237 | |
| 1238 | ref = pat_ref_lookup(name); |
| 1239 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1240 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1241 | |
| 1242 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1243 | pat_ref_add(ref, key, NULL, NULL); |
| 1244 | return 0; |
| 1245 | } |
| 1246 | |
| 1247 | /* This function is an LUA binding. It provides a function |
| 1248 | * for setting map pattern and sample from a referenced map |
| 1249 | * file. |
| 1250 | */ |
| 1251 | static int hlua_set_map(lua_State *L) |
| 1252 | { |
| 1253 | const char *name; |
| 1254 | const char *key; |
| 1255 | const char *value; |
| 1256 | struct pat_ref *ref; |
| 1257 | |
| 1258 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1259 | |
| 1260 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1261 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1262 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1263 | |
| 1264 | ref = pat_ref_lookup(name); |
| 1265 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1266 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1267 | |
| 1268 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1269 | pat_ref_set(ref, key, value, NULL); |
| 1270 | else |
| 1271 | pat_ref_add(ref, key, value, NULL); |
| 1272 | return 0; |
| 1273 | } |
| 1274 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1275 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1276 | * an integer or user data. This data is associated with a metatable. This |
| 1277 | * metatable have an original version registred in the global context with |
| 1278 | * the name of the object (_G[<name>] = <metable> ). |
| 1279 | * |
| 1280 | * A metable is a table that modify the standard behavior of a standard |
| 1281 | * access to the associated data. The entries of this new metatable are |
| 1282 | * defined as is: |
| 1283 | * |
| 1284 | * http://lua-users.org/wiki/MetatableEvents |
| 1285 | * |
| 1286 | * __index |
| 1287 | * |
| 1288 | * we access an absent field in a table, the result is nil. This is |
| 1289 | * true, but it is not the whole truth. Actually, such access triggers |
| 1290 | * the interpreter to look for an __index metamethod: If there is no |
| 1291 | * such method, as usually happens, then the access results in nil; |
| 1292 | * otherwise, the metamethod will provide the result. |
| 1293 | * |
| 1294 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1295 | * the key does not appear in the table, but the metatable has an __index |
| 1296 | * property: |
| 1297 | * |
| 1298 | * - if the value is a function, the function is called, passing in the |
| 1299 | * table and the key; the return value of that function is returned as |
| 1300 | * the result. |
| 1301 | * |
| 1302 | * - if the value is another table, the value of the key in that table is |
| 1303 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1304 | * table's metatable has an __index property, then it continues on up) |
| 1305 | * |
| 1306 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1307 | * |
| 1308 | * http://www.lua.org/pil/13.4.1.html |
| 1309 | * |
| 1310 | * __newindex |
| 1311 | * |
| 1312 | * Like __index, but control property assignment. |
| 1313 | * |
| 1314 | * __mode - Control weak references. A string value with one or both |
| 1315 | * of the characters 'k' and 'v' which specifies that the the |
| 1316 | * keys and/or values in the table are weak references. |
| 1317 | * |
| 1318 | * __call - Treat a table like a function. When a table is followed by |
| 1319 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1320 | * a __call key pointing to a function, that function is invoked |
| 1321 | * (passing any specified arguments) and the return value is |
| 1322 | * returned. |
| 1323 | * |
| 1324 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1325 | * called, if the metatable for myTable has a __metatable |
| 1326 | * key, the value of that key is returned instead of the |
| 1327 | * actual metatable. |
| 1328 | * |
| 1329 | * __tostring - Control string representation. When the builtin |
| 1330 | * "tostring( myTable )" function is called, if the metatable |
| 1331 | * for myTable has a __tostring property set to a function, |
| 1332 | * that function is invoked (passing myTable to it) and the |
| 1333 | * return value is used as the string representation. |
| 1334 | * |
| 1335 | * __len - Control table length. When the table length is requested using |
| 1336 | * the length operator ( '#' ), if the metatable for myTable has |
| 1337 | * a __len key pointing to a function, that function is invoked |
| 1338 | * (passing myTable to it) and the return value used as the value |
| 1339 | * of "#myTable". |
| 1340 | * |
| 1341 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1342 | * collected, if the metatable has a __gc field pointing to a |
| 1343 | * function, that function is first invoked, passing the userdata |
| 1344 | * to it. The __gc metamethod is not called for tables. |
| 1345 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1346 | * |
| 1347 | * Special metamethods for redefining standard operators: |
| 1348 | * http://www.lua.org/pil/13.1.html |
| 1349 | * |
| 1350 | * __add "+" |
| 1351 | * __sub "-" |
| 1352 | * __mul "*" |
| 1353 | * __div "/" |
| 1354 | * __unm "!" |
| 1355 | * __pow "^" |
| 1356 | * __concat ".." |
| 1357 | * |
| 1358 | * Special methods for redfining standar relations |
| 1359 | * http://www.lua.org/pil/13.2.html |
| 1360 | * |
| 1361 | * __eq "==" |
| 1362 | * __lt "<" |
| 1363 | * __le "<=" |
| 1364 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1365 | |
| 1366 | /* |
| 1367 | * |
| 1368 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1369 | * Class Map |
| 1370 | * |
| 1371 | * |
| 1372 | */ |
| 1373 | |
| 1374 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1375 | * a class session, otherwise it throws an error. |
| 1376 | */ |
| 1377 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1378 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1379 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1380 | } |
| 1381 | |
| 1382 | /* This function is the map constructor. It don't need |
| 1383 | * the class Map object. It creates and return a new Map |
| 1384 | * object. It must be called only during "body" or "init" |
| 1385 | * context because it process some filesystem accesses. |
| 1386 | */ |
| 1387 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1388 | { |
| 1389 | const char *fn; |
| 1390 | int match = PAT_MATCH_STR; |
| 1391 | struct sample_conv conv; |
| 1392 | const char *file = ""; |
| 1393 | int line = 0; |
| 1394 | lua_Debug ar; |
| 1395 | char *err = NULL; |
| 1396 | struct arg args[2]; |
| 1397 | |
| 1398 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1399 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1400 | |
| 1401 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1402 | |
| 1403 | if (lua_gettop(L) >= 2) { |
| 1404 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1405 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1406 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1407 | } |
| 1408 | |
| 1409 | /* Get Lua filename and line number. */ |
| 1410 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1411 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1412 | if (ar.currentline > 0) { /* is there info? */ |
| 1413 | file = ar.short_src; |
| 1414 | line = ar.currentline; |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | /* fill fake sample_conv struct. */ |
| 1419 | conv.kw = ""; /* unused. */ |
| 1420 | conv.process = NULL; /* unused. */ |
| 1421 | conv.arg_mask = 0; /* unused. */ |
| 1422 | conv.val_args = NULL; /* unused. */ |
| 1423 | conv.out_type = SMP_T_STR; |
| 1424 | conv.private = (void *)(long)match; |
| 1425 | switch (match) { |
| 1426 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1427 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1428 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1429 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1430 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1431 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1432 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1433 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1434 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1435 | default: |
| 1436 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1437 | } |
| 1438 | |
| 1439 | /* fill fake args. */ |
| 1440 | args[0].type = ARGT_STR; |
| 1441 | args[0].data.str.str = (char *)fn; |
| 1442 | args[1].type = ARGT_STOP; |
| 1443 | |
| 1444 | /* load the map. */ |
| 1445 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1446 | /* error case: we cant use luaL_error because we must |
| 1447 | * free the err variable. |
| 1448 | */ |
| 1449 | luaL_where(L, 1); |
| 1450 | lua_pushfstring(L, "'new': %s.", err); |
| 1451 | lua_concat(L, 2); |
| 1452 | free(err); |
| 1453 | WILL_LJMP(lua_error(L)); |
| 1454 | } |
| 1455 | |
| 1456 | /* create the lua object. */ |
| 1457 | lua_newtable(L); |
| 1458 | lua_pushlightuserdata(L, args[0].data.map); |
| 1459 | lua_rawseti(L, -2, 0); |
| 1460 | |
| 1461 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1462 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1463 | lua_setmetatable(L, -2); |
| 1464 | |
| 1465 | |
| 1466 | return 1; |
| 1467 | } |
| 1468 | |
| 1469 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1470 | { |
| 1471 | struct map_descriptor *desc; |
| 1472 | struct pattern *pat; |
| 1473 | struct sample smp; |
| 1474 | |
| 1475 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1476 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1477 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1478 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1479 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1480 | } |
| 1481 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1482 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1483 | smp.flags = SMP_F_CONST; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1484 | 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] | 1485 | } |
| 1486 | |
| 1487 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1488 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1489 | if (str) |
| 1490 | lua_pushstring(L, ""); |
| 1491 | else |
| 1492 | lua_pushnil(L); |
| 1493 | return 1; |
| 1494 | } |
| 1495 | |
| 1496 | /* 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] | 1497 | 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] | 1498 | return 1; |
| 1499 | } |
| 1500 | |
| 1501 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1502 | { |
| 1503 | return _hlua_map_lookup(L, 0); |
| 1504 | } |
| 1505 | |
| 1506 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1507 | { |
| 1508 | return _hlua_map_lookup(L, 1); |
| 1509 | } |
| 1510 | |
| 1511 | /* |
| 1512 | * |
| 1513 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1514 | * Class Socket |
| 1515 | * |
| 1516 | * |
| 1517 | */ |
| 1518 | |
| 1519 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1520 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1521 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1522 | } |
| 1523 | |
| 1524 | /* This function is the handler called for each I/O on the established |
| 1525 | * connection. It is used for notify space avalaible to send or data |
| 1526 | * received. |
| 1527 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1528 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1529 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1530 | struct stream_interface *si = appctx->owner; |
Willy Tarreau | 50fe03b | 2014-11-28 13:59:31 +0100 | [diff] [blame] | 1531 | struct connection *c = objt_conn(si_opposite(si)->end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1532 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1533 | /* If the connection object is not avalaible, close all the |
| 1534 | * streams and wakeup everithing waiting for. |
| 1535 | */ |
| 1536 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1537 | si_shutw(si); |
| 1538 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1539 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1540 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1541 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1542 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1543 | } |
| 1544 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1545 | /* If we cant write, wakeup the pending write signals. */ |
| 1546 | if (channel_output_closed(si_ic(si))) |
| 1547 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
| 1548 | |
| 1549 | /* If we cant read, wakeup the pending read signals. */ |
| 1550 | if (channel_input_closed(si_oc(si))) |
| 1551 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1552 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1553 | /* if the connection is not estabkished, inform the stream that we want |
| 1554 | * to be notified whenever the connection completes. |
| 1555 | */ |
| 1556 | if (!(c->flags & CO_FL_CONNECTED)) { |
| 1557 | si_applet_cant_get(si); |
| 1558 | si_applet_cant_put(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1559 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1560 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1561 | |
| 1562 | /* This function is called after the connect. */ |
| 1563 | appctx->ctx.hlua.connected = 1; |
| 1564 | |
| 1565 | /* 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] | 1566 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1567 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
| 1568 | |
| 1569 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1570 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1571 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1572 | } |
| 1573 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1574 | /* This function is called when the "struct stream" is destroyed. |
| 1575 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1576 | * Wake all the pending signals. |
| 1577 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1578 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1579 | { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1580 | /* Remove my link in the original object. */ |
| 1581 | if (appctx->ctx.hlua.socket) |
| 1582 | appctx->ctx.hlua.socket->s = NULL; |
| 1583 | |
| 1584 | /* Wake all the task waiting for me. */ |
| 1585 | hlua_com_wake(&appctx->ctx.hlua.wake_on_read); |
| 1586 | hlua_com_wake(&appctx->ctx.hlua.wake_on_write); |
| 1587 | } |
| 1588 | |
| 1589 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1590 | * uses this object. If the stream does not exists, just quit. |
| 1591 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1592 | * pending signal can rest in the read and write lists. destroy |
| 1593 | * it. |
| 1594 | */ |
| 1595 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1596 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1597 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1598 | struct appctx *appctx; |
| 1599 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1600 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1601 | |
| 1602 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1603 | if (!socket->s) |
| 1604 | return 0; |
| 1605 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1606 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1607 | appctx = objt_appctx(socket->s->si[0].end); |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1608 | stream_shutdown(socket->s, SF_ERR_KILLED); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1609 | socket->s = NULL; |
| 1610 | appctx->ctx.hlua.socket = NULL; |
| 1611 | |
| 1612 | return 0; |
| 1613 | } |
| 1614 | |
| 1615 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1616 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1617 | */ |
| 1618 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1619 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1620 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1621 | struct appctx *appctx; |
| 1622 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1623 | MAY_LJMP(check_args(L, 1, "close")); |
| 1624 | |
| 1625 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1626 | if (!socket->s) |
| 1627 | return 0; |
| 1628 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1629 | /* Close the stream and remove the associated stop task. */ |
Willy Tarreau | e7dff02 | 2015-04-03 01:14:29 +0200 | [diff] [blame] | 1630 | stream_shutdown(socket->s, SF_ERR_KILLED); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1631 | appctx = objt_appctx(socket->s->si[0].end); |
| 1632 | appctx->ctx.hlua.socket = NULL; |
| 1633 | socket->s = NULL; |
| 1634 | |
| 1635 | return 0; |
| 1636 | } |
| 1637 | |
| 1638 | /* This Lua function assumes that the stack contain three parameters. |
| 1639 | * 1 - USERDATA containing a struct socket |
| 1640 | * 2 - INTEGER with values of the macro defined below |
| 1641 | * If the integer is -1, we must read at most one line. |
| 1642 | * If the integer is -2, we ust read all the data until the |
| 1643 | * end of the stream. |
| 1644 | * If the integer is positive value, we must read a number of |
| 1645 | * bytes corresponding to this value. |
| 1646 | */ |
| 1647 | #define HLSR_READ_LINE (-1) |
| 1648 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1649 | __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] | 1650 | { |
| 1651 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1652 | int wanted = lua_tointeger(L, 2); |
| 1653 | struct hlua *hlua = hlua_gethlua(L); |
| 1654 | struct appctx *appctx; |
| 1655 | int len; |
| 1656 | int nblk; |
| 1657 | char *blk1; |
| 1658 | int len1; |
| 1659 | char *blk2; |
| 1660 | int len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1661 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1662 | struct channel *oc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1663 | |
| 1664 | /* Check if this lua stack is schedulable. */ |
| 1665 | if (!hlua || !hlua->task) |
| 1666 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1667 | "'frontend', 'backend' or 'task'")); |
| 1668 | |
| 1669 | /* check for connection closed. If some data where read, return it. */ |
| 1670 | if (!socket->s) |
| 1671 | goto connection_closed; |
| 1672 | |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1673 | oc = &socket->s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1674 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1675 | /* Read line. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1676 | nblk = bo_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1677 | if (nblk < 0) /* Connection close. */ |
| 1678 | goto connection_closed; |
| 1679 | if (nblk == 0) /* No data avalaible. */ |
| 1680 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1681 | |
| 1682 | /* remove final \r\n. */ |
| 1683 | if (nblk == 1) { |
| 1684 | if (blk1[len1-1] == '\n') { |
| 1685 | len1--; |
| 1686 | skip_at_end++; |
| 1687 | if (blk1[len1-1] == '\r') { |
| 1688 | len1--; |
| 1689 | skip_at_end++; |
| 1690 | } |
| 1691 | } |
| 1692 | } |
| 1693 | else { |
| 1694 | if (blk2[len2-1] == '\n') { |
| 1695 | len2--; |
| 1696 | skip_at_end++; |
| 1697 | if (blk2[len2-1] == '\r') { |
| 1698 | len2--; |
| 1699 | skip_at_end++; |
| 1700 | } |
| 1701 | } |
| 1702 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1703 | } |
| 1704 | |
| 1705 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1706 | /* Read all the available data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1707 | nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1708 | if (nblk < 0) /* Connection close. */ |
| 1709 | goto connection_closed; |
| 1710 | if (nblk == 0) /* No data avalaible. */ |
| 1711 | goto connection_empty; |
| 1712 | } |
| 1713 | |
| 1714 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1715 | /* Read a block of data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1716 | nblk = bo_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1717 | if (nblk < 0) /* Connection close. */ |
| 1718 | goto connection_closed; |
| 1719 | if (nblk == 0) /* No data avalaible. */ |
| 1720 | goto connection_empty; |
| 1721 | |
| 1722 | if (len1 > wanted) { |
| 1723 | nblk = 1; |
| 1724 | len1 = wanted; |
| 1725 | } if (nblk == 2 && len1 + len2 > wanted) |
| 1726 | len2 = wanted - len1; |
| 1727 | } |
| 1728 | |
| 1729 | len = len1; |
| 1730 | |
| 1731 | luaL_addlstring(&socket->b, blk1, len1); |
| 1732 | if (nblk == 2) { |
| 1733 | len += len2; |
| 1734 | luaL_addlstring(&socket->b, blk2, len2); |
| 1735 | } |
| 1736 | |
| 1737 | /* Consume data. */ |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1738 | bo_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1739 | |
| 1740 | /* Don't wait anything. */ |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 1741 | stream_int_notify(&socket->s->si[0]); |
| 1742 | stream_int_update_applet(&socket->s->si[0]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1743 | |
| 1744 | /* If the pattern reclaim to read all the data |
| 1745 | * in the connection, got out. |
| 1746 | */ |
| 1747 | if (wanted == HLSR_READ_ALL) |
| 1748 | goto connection_empty; |
| 1749 | else if (wanted >= 0 && len < wanted) |
| 1750 | goto connection_empty; |
| 1751 | |
| 1752 | /* Return result. */ |
| 1753 | luaL_pushresult(&socket->b); |
| 1754 | return 1; |
| 1755 | |
| 1756 | connection_closed: |
| 1757 | |
| 1758 | /* If the buffer containds data. */ |
| 1759 | if (socket->b.n > 0) { |
| 1760 | luaL_pushresult(&socket->b); |
| 1761 | return 1; |
| 1762 | } |
| 1763 | lua_pushnil(L); |
| 1764 | lua_pushstring(L, "connection closed."); |
| 1765 | return 2; |
| 1766 | |
| 1767 | connection_empty: |
| 1768 | |
| 1769 | appctx = objt_appctx(socket->s->si[0].end); |
| 1770 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_read)) |
| 1771 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1772 | 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] | 1773 | return 0; |
| 1774 | } |
| 1775 | |
| 1776 | /* This Lus function gets two parameters. The first one can be string |
| 1777 | * or a number. If the string is "*l", the user require one line. If |
| 1778 | * the string is "*a", the user require all the content of the stream. |
| 1779 | * If the value is a number, the user require a number of bytes equal |
| 1780 | * to the value. The default value is "*l" (a line). |
| 1781 | * |
| 1782 | * This paraeter with a variable type is converted in integer. This |
| 1783 | * integer takes this values: |
| 1784 | * -1 : read a line |
| 1785 | * -2 : read all the stream |
| 1786 | * >0 : amount if bytes. |
| 1787 | * |
| 1788 | * The second parameter is optinal. It contains a string that must be |
| 1789 | * concatenated with the read data. |
| 1790 | */ |
| 1791 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1792 | { |
| 1793 | int wanted = HLSR_READ_LINE; |
| 1794 | const char *pattern; |
| 1795 | int type; |
| 1796 | char *error; |
| 1797 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1798 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1799 | |
| 1800 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1801 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1802 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1803 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1804 | |
| 1805 | /* check for pattern. */ |
| 1806 | if (lua_gettop(L) >= 2) { |
| 1807 | type = lua_type(L, 2); |
| 1808 | if (type == LUA_TSTRING) { |
| 1809 | pattern = lua_tostring(L, 2); |
| 1810 | if (strcmp(pattern, "*a") == 0) |
| 1811 | wanted = HLSR_READ_ALL; |
| 1812 | else if (strcmp(pattern, "*l") == 0) |
| 1813 | wanted = HLSR_READ_LINE; |
| 1814 | else { |
| 1815 | wanted = strtoll(pattern, &error, 10); |
| 1816 | if (*error != '\0') |
| 1817 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1818 | } |
| 1819 | } |
| 1820 | else if (type == LUA_TNUMBER) { |
| 1821 | wanted = lua_tointeger(L, 2); |
| 1822 | if (wanted < 0) |
| 1823 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1824 | } |
| 1825 | } |
| 1826 | |
| 1827 | /* Set pattern. */ |
| 1828 | lua_pushinteger(L, wanted); |
| 1829 | lua_replace(L, 2); |
| 1830 | |
| 1831 | /* init bufffer, and fiil it wih prefix. */ |
| 1832 | luaL_buffinit(L, &socket->b); |
| 1833 | |
| 1834 | /* Check prefix. */ |
| 1835 | if (lua_gettop(L) >= 3) { |
| 1836 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1837 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1838 | pattern = lua_tolstring(L, 3, &len); |
| 1839 | luaL_addlstring(&socket->b, pattern, len); |
| 1840 | } |
| 1841 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1842 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1843 | } |
| 1844 | |
| 1845 | /* Write the Lua input string in the output buffer. |
| 1846 | * This fucntion returns a yield if no space are available. |
| 1847 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1848 | 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] | 1849 | { |
| 1850 | struct hlua_socket *socket; |
| 1851 | struct hlua *hlua = hlua_gethlua(L); |
| 1852 | struct appctx *appctx; |
| 1853 | size_t buf_len; |
| 1854 | const char *buf; |
| 1855 | int len; |
| 1856 | int send_len; |
| 1857 | int sent; |
| 1858 | |
| 1859 | /* Check if this lua stack is schedulable. */ |
| 1860 | if (!hlua || !hlua->task) |
| 1861 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1862 | "'frontend', 'backend' or 'task'")); |
| 1863 | |
| 1864 | /* Get object */ |
| 1865 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1866 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1867 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1868 | |
| 1869 | /* Check for connection close. */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1870 | if (!socket->s || channel_output_closed(&socket->s->req)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1871 | lua_pushinteger(L, -1); |
| 1872 | return 1; |
| 1873 | } |
| 1874 | |
| 1875 | /* Update the input buffer data. */ |
| 1876 | buf += sent; |
| 1877 | send_len = buf_len - sent; |
| 1878 | |
| 1879 | /* All the data are sent. */ |
| 1880 | if (sent >= buf_len) |
| 1881 | return 1; /* Implicitly return the length sent. */ |
| 1882 | |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1883 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 1884 | * the request buffer if its not required. |
| 1885 | */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 1886 | if (socket->s->req.buf->size == 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1887 | if (!stream_alloc_recv_buffer(&socket->s->req)) { |
Willy Tarreau | 350f487 | 2014-11-28 14:42:25 +0100 | [diff] [blame] | 1888 | socket->s->si[0].flags |= SI_FL_WAIT_ROOM; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 1889 | goto hlua_socket_write_yield_return; |
| 1890 | } |
| 1891 | } |
| 1892 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1893 | /* Check for avalaible space. */ |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1894 | len = buffer_total_space(socket->s->req.buf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1895 | if (len <= 0) |
| 1896 | goto hlua_socket_write_yield_return; |
| 1897 | |
| 1898 | /* send data */ |
| 1899 | if (len < send_len) |
| 1900 | send_len = len; |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1901 | len = bi_putblk(&socket->s->req, buf+sent, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1902 | |
| 1903 | /* "Not enough space" (-1), "Buffer too little to contain |
| 1904 | * the data" (-2) are not expected because the available length |
| 1905 | * is tested. |
| 1906 | * Other unknown error are also not expected. |
| 1907 | */ |
| 1908 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1909 | if (len == -1) |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1910 | socket->s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 1911 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1912 | MAY_LJMP(hlua_socket_close(L)); |
| 1913 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1914 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1915 | return 1; |
| 1916 | } |
| 1917 | |
| 1918 | /* update buffers. */ |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 1919 | stream_int_notify(&socket->s->si[0]); |
| 1920 | stream_int_update_applet(&socket->s->si[0]); |
| 1921 | |
Willy Tarreau | 94aa617 | 2015-03-13 14:19:06 +0100 | [diff] [blame] | 1922 | socket->s->req.rex = TICK_ETERNITY; |
| 1923 | socket->s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1924 | |
| 1925 | /* Update length sent. */ |
| 1926 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1927 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1928 | |
| 1929 | /* All the data buffer is sent ? */ |
| 1930 | if (sent + len >= buf_len) |
| 1931 | return 1; |
| 1932 | |
| 1933 | hlua_socket_write_yield_return: |
| 1934 | appctx = objt_appctx(socket->s->si[0].end); |
| 1935 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write)) |
| 1936 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1937 | 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] | 1938 | return 0; |
| 1939 | } |
| 1940 | |
| 1941 | /* This function initiate the send of data. It just check the input |
| 1942 | * parameters and push an integer in the Lua stack that contain the |
| 1943 | * amount of data writed in the buffer. This is used by the function |
| 1944 | * "hlua_socket_write_yield" that can yield. |
| 1945 | * |
| 1946 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 1947 | * the associated object. The second is a string buffer. The third is |
| 1948 | * a facultative integer that represents where is the buffer position |
| 1949 | * of the start of the data that can send. The first byte is the |
| 1950 | * position "1". The default value is "1". The fourth argument is a |
| 1951 | * facultative integer that represents where is the buffer position |
| 1952 | * of the end of the data that can send. The default is the last byte. |
| 1953 | */ |
| 1954 | static int hlua_socket_send(struct lua_State *L) |
| 1955 | { |
| 1956 | int i; |
| 1957 | int j; |
| 1958 | const char *buf; |
| 1959 | size_t buf_len; |
| 1960 | |
| 1961 | /* Check number of arguments. */ |
| 1962 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 1963 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 1964 | |
| 1965 | /* Get the string. */ |
| 1966 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 1967 | |
| 1968 | /* Get and check j. */ |
| 1969 | if (lua_gettop(L) == 4) { |
| 1970 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 1971 | if (j < 0) |
| 1972 | j = buf_len + j + 1; |
| 1973 | if (j > buf_len) |
| 1974 | j = buf_len + 1; |
| 1975 | lua_pop(L, 1); |
| 1976 | } |
| 1977 | else |
| 1978 | j = buf_len; |
| 1979 | |
| 1980 | /* Get and check i. */ |
| 1981 | if (lua_gettop(L) == 3) { |
| 1982 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 1983 | if (i < 0) |
| 1984 | i = buf_len + i + 1; |
| 1985 | if (i > buf_len) |
| 1986 | i = buf_len + 1; |
| 1987 | lua_pop(L, 1); |
| 1988 | } else |
| 1989 | i = 1; |
| 1990 | |
| 1991 | /* Check bth i and j. */ |
| 1992 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1993 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1994 | return 1; |
| 1995 | } |
| 1996 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1997 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1998 | return 1; |
| 1999 | } |
| 2000 | if (i == 0) |
| 2001 | i = 1; |
| 2002 | if (j == 0) |
| 2003 | j = 1; |
| 2004 | |
| 2005 | /* Pop the string. */ |
| 2006 | lua_pop(L, 1); |
| 2007 | |
| 2008 | /* Update the buffer length. */ |
| 2009 | buf += i - 1; |
| 2010 | buf_len = j - i + 1; |
| 2011 | lua_pushlstring(L, buf, buf_len); |
| 2012 | |
| 2013 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2014 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2016 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2017 | } |
| 2018 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2019 | #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] | 2020 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2021 | { |
| 2022 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2023 | int ret; |
| 2024 | int len; |
| 2025 | char *p; |
| 2026 | |
| 2027 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2028 | if (ret <= 0) { |
| 2029 | lua_pushnil(L); |
| 2030 | return 1; |
| 2031 | } |
| 2032 | |
| 2033 | if (ret == AF_UNIX) { |
| 2034 | lua_pushstring(L, buffer+1); |
| 2035 | return 1; |
| 2036 | } |
| 2037 | else if (ret == AF_INET6) { |
| 2038 | buffer[0] = '['; |
| 2039 | len = strlen(buffer); |
| 2040 | buffer[len] = ']'; |
| 2041 | len++; |
| 2042 | buffer[len] = ':'; |
| 2043 | len++; |
| 2044 | p = buffer; |
| 2045 | } |
| 2046 | else if (ret == AF_INET) { |
| 2047 | p = buffer + 1; |
| 2048 | len = strlen(p); |
| 2049 | p[len] = ':'; |
| 2050 | len++; |
| 2051 | } |
| 2052 | else { |
| 2053 | lua_pushnil(L); |
| 2054 | return 1; |
| 2055 | } |
| 2056 | |
| 2057 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2058 | lua_pushnil(L); |
| 2059 | return 1; |
| 2060 | } |
| 2061 | |
| 2062 | lua_pushstring(L, p); |
| 2063 | return 1; |
| 2064 | } |
| 2065 | |
| 2066 | /* Returns information about the peer of the connection. */ |
| 2067 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2068 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2069 | struct hlua_socket *socket; |
| 2070 | struct connection *conn; |
| 2071 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2072 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2073 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2074 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2075 | |
| 2076 | /* Check if the tcp object is avalaible. */ |
| 2077 | if (!socket->s) { |
| 2078 | lua_pushnil(L); |
| 2079 | return 1; |
| 2080 | } |
| 2081 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2082 | conn = objt_conn(socket->s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2083 | if (!conn) { |
| 2084 | lua_pushnil(L); |
| 2085 | return 1; |
| 2086 | } |
| 2087 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2088 | conn_get_to_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2089 | if (!(conn->flags & CO_FL_ADDR_TO_SET)) { |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2090 | lua_pushnil(L); |
| 2091 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2092 | } |
| 2093 | |
| 2094 | return MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2095 | } |
| 2096 | |
| 2097 | /* Returns information about my connection side. */ |
| 2098 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2099 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2100 | struct hlua_socket *socket; |
| 2101 | struct connection *conn; |
| 2102 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2103 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2104 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2105 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2106 | |
| 2107 | /* Check if the tcp object is avalaible. */ |
| 2108 | if (!socket->s) { |
| 2109 | lua_pushnil(L); |
| 2110 | return 1; |
| 2111 | } |
| 2112 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2113 | conn = objt_conn(socket->s->si[1].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2114 | if (!conn) { |
| 2115 | lua_pushnil(L); |
| 2116 | return 1; |
| 2117 | } |
| 2118 | |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2119 | conn_get_from_addr(conn); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2120 | if (!(conn->flags & CO_FL_ADDR_FROM_SET)) { |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2121 | lua_pushnil(L); |
| 2122 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | return hlua_socket_info(L, &conn->addr.from); |
| 2126 | } |
| 2127 | |
| 2128 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2129 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2130 | .obj_type = OBJ_TYPE_APPLET, |
| 2131 | .name = "<LUA_TCP>", |
| 2132 | .fct = hlua_socket_handler, |
| 2133 | .release = hlua_socket_release, |
| 2134 | }; |
| 2135 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2136 | __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] | 2137 | { |
| 2138 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2139 | struct hlua *hlua = hlua_gethlua(L); |
| 2140 | struct appctx *appctx; |
| 2141 | |
| 2142 | /* Check for connection close. */ |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2143 | if (!hlua || !socket->s || channel_output_closed(&socket->s->req)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2144 | lua_pushnil(L); |
| 2145 | lua_pushstring(L, "Can't connect"); |
| 2146 | return 2; |
| 2147 | } |
| 2148 | |
| 2149 | appctx = objt_appctx(socket->s->si[0].end); |
| 2150 | |
| 2151 | /* Check for connection established. */ |
| 2152 | if (appctx->ctx.hlua.connected) { |
| 2153 | lua_pushinteger(L, 1); |
| 2154 | return 1; |
| 2155 | } |
| 2156 | |
| 2157 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write)) |
| 2158 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2159 | 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] | 2160 | return 0; |
| 2161 | } |
| 2162 | |
| 2163 | /* This function fail or initite the connection. */ |
| 2164 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2165 | { |
| 2166 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2167 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2168 | const char *ip; |
| 2169 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2170 | struct hlua *hlua; |
| 2171 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2172 | int low, high; |
| 2173 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2174 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2175 | if (lua_gettop(L) < 2) |
| 2176 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2177 | |
| 2178 | /* Get args. */ |
| 2179 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2180 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2181 | if (lua_gettop(L) >= 3) |
| 2182 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2183 | |
Willy Tarreau | 973a542 | 2015-08-05 21:47:23 +0200 | [diff] [blame] | 2184 | conn = si_alloc_conn(&socket->s->si[1]); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2185 | if (!conn) |
| 2186 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2187 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2188 | /* needed for the connection not to be closed */ |
| 2189 | conn->target = socket->s->target; |
| 2190 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2191 | /* Parse ip address. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2192 | addr = str2sa_range(ip, &low, &high, NULL, NULL, NULL, 0); |
| 2193 | if (!addr) |
| 2194 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
| 2195 | if (low != high) |
| 2196 | WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip)); |
| 2197 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2198 | |
| 2199 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2200 | if (low == 0) { |
| 2201 | if (conn->addr.to.ss_family == AF_INET) { |
| 2202 | if (port == -1) |
| 2203 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
| 2204 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2205 | } else if (conn->addr.to.ss_family == AF_INET6) { |
| 2206 | if (port == -1) |
| 2207 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
| 2208 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2209 | } |
| 2210 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2211 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2212 | hlua = hlua_gethlua(L); |
| 2213 | appctx = objt_appctx(socket->s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2214 | |
| 2215 | /* inform the stream that we want to be notified whenever the |
| 2216 | * connection completes. |
| 2217 | */ |
| 2218 | si_applet_cant_get(&socket->s->si[0]); |
| 2219 | si_applet_cant_put(&socket->s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2220 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2221 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2222 | hlua->flags |= HLUA_MUST_GC; |
| 2223 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2224 | if (!hlua_com_new(hlua, &appctx->ctx.hlua.wake_on_write)) |
| 2225 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2226 | 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] | 2227 | |
| 2228 | return 0; |
| 2229 | } |
| 2230 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2231 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2232 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2233 | { |
| 2234 | struct hlua_socket *socket; |
| 2235 | |
| 2236 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2237 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2238 | socket->s->target = &socket_ssl.obj_type; |
| 2239 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2240 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2241 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2242 | |
| 2243 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2244 | { |
| 2245 | return 0; |
| 2246 | } |
| 2247 | |
| 2248 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2249 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2250 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2251 | int tmout; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2252 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2253 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2254 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2255 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2256 | tmout = MAY_LJMP(luaL_checkinteger(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2257 | |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 2258 | socket->s->req.rto = tmout; |
| 2259 | socket->s->req.wto = tmout; |
| 2260 | socket->s->res.rto = tmout; |
| 2261 | socket->s->res.wto = tmout; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2262 | |
| 2263 | return 0; |
| 2264 | } |
| 2265 | |
| 2266 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2267 | { |
| 2268 | struct hlua_socket *socket; |
| 2269 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2270 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2271 | struct stream *strm; |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2272 | struct task *task; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2273 | |
| 2274 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2275 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2276 | hlua_pusherror(L, "socket: full stack"); |
| 2277 | goto out_fail_conf; |
| 2278 | } |
| 2279 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2280 | /* Create the object: obj[0] = userdata. */ |
| 2281 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2282 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2283 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2284 | memset(socket, 0, sizeof(*socket)); |
| 2285 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2286 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2287 | if (!pool2_stream || !pool2_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2288 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2289 | goto out_fail_conf; |
| 2290 | } |
| 2291 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2292 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2294 | lua_setmetatable(L, -2); |
| 2295 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2296 | /* Create the applet context */ |
| 2297 | appctx = appctx_new(&update_applet); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2298 | if (!appctx) { |
| 2299 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2300 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2301 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2302 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2303 | appctx->ctx.hlua.socket = socket; |
| 2304 | appctx->ctx.hlua.connected = 0; |
| 2305 | LIST_INIT(&appctx->ctx.hlua.wake_on_write); |
| 2306 | LIST_INIT(&appctx->ctx.hlua.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2307 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2308 | /* Now create a session, task and stream for this applet */ |
| 2309 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2310 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2311 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2312 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2313 | } |
| 2314 | |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2315 | task = task_new(); |
| 2316 | if (!task) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2317 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2318 | goto out_fail_task; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2319 | } |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2320 | task->nice = 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | |
Willy Tarreau | 73b65ac | 2015-04-08 18:26:29 +0200 | [diff] [blame] | 2322 | strm = stream_new(sess, task, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2323 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2324 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2325 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2326 | } |
| 2327 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2328 | /* Configure an empty Lua for the stream. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2329 | socket->s = strm; |
| 2330 | strm->hlua.T = NULL; |
| 2331 | strm->hlua.Tref = LUA_REFNIL; |
| 2332 | strm->hlua.Mref = LUA_REFNIL; |
| 2333 | strm->hlua.nargs = 0; |
| 2334 | strm->hlua.flags = 0; |
| 2335 | LIST_INIT(&strm->hlua.com); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2336 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2337 | /* Configure "right" stream interface. this "si" is used to connect |
| 2338 | * and retrieve data from the server. The connection is initialized |
| 2339 | * with the "struct server". |
| 2340 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2341 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2342 | |
| 2343 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2344 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2345 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2346 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2347 | /* Update statistics counters. */ |
| 2348 | socket_proxy.feconn++; /* beconn will be increased later */ |
| 2349 | jobs++; |
| 2350 | totalconn++; |
| 2351 | |
| 2352 | /* Return yield waiting for connection. */ |
| 2353 | return 1; |
| 2354 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2355 | out_fail_stream: |
| 2356 | task_free(task); |
| 2357 | out_fail_task: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2358 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2359 | out_fail_sess: |
| 2360 | appctx_free(appctx); |
| 2361 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2362 | WILL_LJMP(lua_error(L)); |
| 2363 | return 0; |
| 2364 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2365 | |
| 2366 | /* |
| 2367 | * |
| 2368 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2369 | * Class Channel |
| 2370 | * |
| 2371 | * |
| 2372 | */ |
| 2373 | |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2374 | /* The state between the channel data and the HTTP parser state can be |
| 2375 | * unconsistent, so reset the parser and call it again. Warning, this |
| 2376 | * action not revalidate the request and not send a 400 if the modified |
| 2377 | * resuest is not valid. |
| 2378 | * |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2379 | * This function never fails. The direction is set using dir, which equals |
| 2380 | * either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2381 | */ |
| 2382 | static void hlua_resynchonize_proto(struct stream *stream, int dir) |
| 2383 | { |
| 2384 | /* Protocol HTTP. */ |
| 2385 | if (stream->be->mode == PR_MODE_HTTP) { |
| 2386 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2387 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2388 | http_txn_reset_req(stream->txn); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2389 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2390 | http_txn_reset_res(stream->txn); |
| 2391 | |
| 2392 | if (stream->txn->hdr_idx.v) |
| 2393 | hdr_idx_init(&stream->txn->hdr_idx); |
| 2394 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2395 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2396 | http_msg_analyzer(&stream->txn->req, &stream->txn->hdr_idx); |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2397 | else if (dir == SMP_OPT_DIR_RES) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2398 | http_msg_analyzer(&stream->txn->rsp, &stream->txn->hdr_idx); |
| 2399 | } |
| 2400 | } |
| 2401 | |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2402 | /* Check the protocole integrity after the Lua manipulations. Close the stream |
| 2403 | * and returns 0 if fails, otherwise returns 1. The direction is set using dir, |
| 2404 | * which equals either SMP_OPT_DIR_REQ or SMP_OPT_DIR_RES. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2405 | */ |
| 2406 | static int hlua_check_proto(struct stream *stream, int dir) |
| 2407 | { |
| 2408 | const struct chunk msg = { .len = 0 }; |
| 2409 | |
Willy Tarreau | 9af89f7 | 2015-09-26 11:50:08 +0200 | [diff] [blame] | 2410 | /* Protocol HTTP. The message parsing state must match the request or |
| 2411 | * response state. The problem that may happen is that Lua modifies |
| 2412 | * the request or response message *after* it was parsed, and corrupted |
| 2413 | * it so that it could not be processed anymore. We just need to verify |
| 2414 | * if the parser is still expected to run or not. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2415 | */ |
| 2416 | if (stream->be->mode == PR_MODE_HTTP) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2417 | if (dir == SMP_OPT_DIR_REQ && |
Willy Tarreau | 9af89f7 | 2015-09-26 11:50:08 +0200 | [diff] [blame] | 2418 | !(stream->req.analysers & AN_REQ_WAIT_HTTP) && |
Thierry FOURNIER / OZON.IO | 8dc7316 | 2016-11-18 19:06:21 +0100 | [diff] [blame] | 2419 | stream->txn->req.msg_state < HTTP_MSG_ERROR) { |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2420 | stream_int_retnclose(&stream->si[0], &msg); |
| 2421 | return 0; |
| 2422 | } |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 2423 | else if (dir == SMP_OPT_DIR_RES && |
Willy Tarreau | 9af89f7 | 2015-09-26 11:50:08 +0200 | [diff] [blame] | 2424 | !(stream->res.analysers & AN_RES_WAIT_HTTP) && |
Thierry FOURNIER / OZON.IO | 8dc7316 | 2016-11-18 19:06:21 +0100 | [diff] [blame] | 2425 | stream->txn->rsp.msg_state < HTTP_MSG_ERROR) { |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2426 | stream_int_retnclose(&stream->si[0], &msg); |
| 2427 | return 0; |
| 2428 | } |
| 2429 | } |
| 2430 | return 1; |
| 2431 | } |
| 2432 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2433 | /* Returns the struct hlua_channel join to the class channel in the |
| 2434 | * stack entry "ud" or throws an argument error. |
| 2435 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2436 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2437 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2438 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2439 | } |
| 2440 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2441 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2442 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2443 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2444 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2445 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2446 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2447 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2448 | return 0; |
| 2449 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2450 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2451 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2452 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2453 | |
| 2454 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2455 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2456 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2457 | return 1; |
| 2458 | } |
| 2459 | |
| 2460 | /* Duplicate all the data present in the input channel and put it |
| 2461 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2462 | * the stack if the channel is closed and all the data are consumed, |
| 2463 | * returns 0 if no data are available, otherwise it returns the length |
| 2464 | * of the builded string. |
| 2465 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2466 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2467 | { |
| 2468 | char *blk1; |
| 2469 | char *blk2; |
| 2470 | int len1; |
| 2471 | int len2; |
| 2472 | int ret; |
| 2473 | luaL_Buffer b; |
| 2474 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2475 | ret = bi_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2476 | if (unlikely(ret == 0)) |
| 2477 | return 0; |
| 2478 | |
| 2479 | if (unlikely(ret < 0)) { |
| 2480 | lua_pushnil(L); |
| 2481 | return -1; |
| 2482 | } |
| 2483 | |
| 2484 | luaL_buffinit(L, &b); |
| 2485 | luaL_addlstring(&b, blk1, len1); |
| 2486 | if (unlikely(ret == 2)) |
| 2487 | luaL_addlstring(&b, blk2, len2); |
| 2488 | luaL_pushresult(&b); |
| 2489 | |
| 2490 | if (unlikely(ret == 2)) |
| 2491 | return len1 + len2; |
| 2492 | return len1; |
| 2493 | } |
| 2494 | |
| 2495 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2496 | * a yield. This function keep the data in the buffer. |
| 2497 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2498 | __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] | 2499 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2500 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2501 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2502 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2503 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2504 | if (_hlua_channel_dup(chn, L) == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2505 | 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] | 2506 | return 1; |
| 2507 | } |
| 2508 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2509 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2510 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2511 | { |
| 2512 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2513 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2514 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2515 | } |
| 2516 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2517 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2518 | * a yield. This function consumes the data in the buffer. It returns |
| 2519 | * a string containing the data or a nil pointer if no data are available |
| 2520 | * and the channel is closed. |
| 2521 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2522 | __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] | 2523 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2524 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2525 | int ret; |
| 2526 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2527 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2528 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2529 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2530 | if (unlikely(ret == 0)) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2531 | 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] | 2532 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2533 | if (unlikely(ret == -1)) |
| 2534 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2535 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2536 | chn->buf->i -= ret; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2537 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2538 | return 1; |
| 2539 | } |
| 2540 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2541 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 2542 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2543 | { |
| 2544 | MAY_LJMP(check_args(L, 1, "get")); |
| 2545 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2546 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2547 | } |
| 2548 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2549 | /* This functions consumes and returns one line. If the channel is closed, |
| 2550 | * and the last data does not contains a final '\n', the data are returned |
| 2551 | * without the final '\n'. When no more data are avalaible, it returns nil |
| 2552 | * value. |
| 2553 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2554 | __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] | 2555 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2556 | char *blk1; |
| 2557 | char *blk2; |
| 2558 | int len1; |
| 2559 | int len2; |
| 2560 | int len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2561 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2562 | int ret; |
| 2563 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2564 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2565 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2566 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2567 | ret = bi_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2568 | if (ret == 0) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2569 | 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] | 2570 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2571 | if (ret == -1) { |
| 2572 | lua_pushnil(L); |
| 2573 | return 1; |
| 2574 | } |
| 2575 | |
| 2576 | luaL_buffinit(L, &b); |
| 2577 | luaL_addlstring(&b, blk1, len1); |
| 2578 | len = len1; |
| 2579 | if (unlikely(ret == 2)) { |
| 2580 | luaL_addlstring(&b, blk2, len2); |
| 2581 | len += len2; |
| 2582 | } |
| 2583 | luaL_pushresult(&b); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2584 | 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] | 2585 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2586 | return 1; |
| 2587 | } |
| 2588 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2589 | /* Check arguments for the fucntion "hlua_channel_getline_yield". */ |
| 2590 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2591 | { |
| 2592 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2593 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2594 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2595 | } |
| 2596 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2597 | /* This function takes a string as input, and append it at the |
| 2598 | * input side of channel. If the data is too big, but a space |
| 2599 | * is probably available after sending some data, the function |
| 2600 | * yield. If the data is bigger than the buffer, or if the |
| 2601 | * channel is closed, it returns -1. otherwise, it returns the |
| 2602 | * amount of data writed. |
| 2603 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2604 | __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] | 2605 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2606 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2607 | size_t len; |
| 2608 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2609 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2610 | int ret; |
| 2611 | int max; |
| 2612 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2613 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2614 | if (max > len - l) |
| 2615 | max = len - l; |
| 2616 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2617 | ret = bi_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2618 | if (ret == -2 || ret == -3) { |
| 2619 | lua_pushinteger(L, -1); |
| 2620 | return 1; |
| 2621 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2622 | if (ret == -1) { |
| 2623 | chn->flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2624 | 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] | 2625 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2626 | l += ret; |
| 2627 | lua_pop(L, 1); |
| 2628 | lua_pushinteger(L, l); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2629 | hlua_resynchonize_proto(chn_strm(chn), !!(chn->flags & CF_ISRESP)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2630 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2631 | max = channel_recv_limit(chn) - buffer_len(chn->buf); |
| 2632 | if (max == 0 && chn->buf->o == 0) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2633 | /* There are no space avalaible, and the output buffer is empty. |
| 2634 | * in this case, we cannot add more data, so we cannot yield, |
| 2635 | * we return the amount of copyied data. |
| 2636 | */ |
| 2637 | return 1; |
| 2638 | } |
| 2639 | if (l < len) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2640 | 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] | 2641 | return 1; |
| 2642 | } |
| 2643 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2644 | /* just a wrapper of "hlua_channel_append_yield". It returns the length |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2645 | * of the writed string, or -1 if the channel is closed or if the |
| 2646 | * buffer size is too little for the data. |
| 2647 | */ |
| 2648 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2649 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2650 | size_t len; |
| 2651 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2652 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2653 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2654 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2655 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2656 | lua_pushinteger(L, 0); |
| 2657 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2658 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2659 | } |
| 2660 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2661 | /* just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2662 | * his process by cleaning the buffer. The result is a replacement |
| 2663 | * of the current data. It returns the length of the writed string, |
| 2664 | * or -1 if the channel is closed or if the buffer size is too |
| 2665 | * little for the data. |
| 2666 | */ |
| 2667 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2668 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2669 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2670 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2671 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2672 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2673 | lua_pushinteger(L, 0); |
| 2674 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2675 | chn->buf->i = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2676 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2677 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2678 | } |
| 2679 | |
| 2680 | /* Append data in the output side of the buffer. This data is immediatly |
| 2681 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 2682 | * cannot contains the data, the function yield. The function returns -1 |
| 2683 | * if the channel is closed. |
| 2684 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2685 | __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] | 2686 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2687 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2688 | size_t len; |
| 2689 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2690 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2691 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2692 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2693 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2694 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2695 | lua_pushinteger(L, -1); |
| 2696 | return 1; |
| 2697 | } |
| 2698 | |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2699 | /* Check if the buffer is avalaible because HAProxy doesn't allocate |
| 2700 | * the request buffer if its not required. |
| 2701 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2702 | if (chn->buf->size == 0) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2703 | if (!stream_alloc_recv_buffer(chn)) { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2704 | chn_prod(chn)->flags |= SI_FL_WAIT_ROOM; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2705 | 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] | 2706 | } |
| 2707 | } |
| 2708 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2709 | /* the writed data will be immediatly sent, so we can check |
| 2710 | * the avalaible space without taking in account the reserve. |
| 2711 | * The reserve is guaranted for the processing of incoming |
| 2712 | * data, because the buffer will be flushed. |
| 2713 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2714 | max = chn->buf->size - buffer_len(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2715 | |
| 2716 | /* If there are no space avalaible, and the output buffer is empty. |
| 2717 | * in this case, we cannot add more data, so we cannot yield, |
| 2718 | * we return the amount of copyied data. |
| 2719 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2720 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2721 | return 1; |
| 2722 | |
| 2723 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2724 | if (max > len - l) |
| 2725 | max = len - l; |
| 2726 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2727 | /* The buffer avalaible size may be not contiguous. This test |
| 2728 | * detects a non contiguous buffer and realign it. |
| 2729 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2730 | if (bi_space_for_replace(chn->buf) < max) |
| 2731 | buffer_slow_realign(chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2732 | |
| 2733 | /* Copy input data in the buffer. */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2734 | 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] | 2735 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2736 | /* buffer replace considers that the input part is filled. |
| 2737 | * so, I must forward these new data in the output part. |
| 2738 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2739 | b_adv(chn->buf, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2740 | |
| 2741 | l += max; |
| 2742 | lua_pop(L, 1); |
| 2743 | lua_pushinteger(L, l); |
| 2744 | |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2745 | /* If there are no space avalaible, and the output buffer is empty. |
| 2746 | * in this case, we cannot add more data, so we cannot yield, |
| 2747 | * we return the amount of copyied data. |
| 2748 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2749 | max = chn->buf->size - buffer_len(chn->buf); |
| 2750 | if (max == 0 && chn->buf->o == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2751 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2752 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2753 | if (l < len) { |
| 2754 | /* If we are waiting for space in the response buffer, we |
| 2755 | * must set the flag WAKERESWR. This flag required the task |
| 2756 | * wake up if any activity is detected on the response buffer. |
| 2757 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2758 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2759 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2760 | else |
| 2761 | HLUA_SET_WAKEREQWR(hlua); |
| 2762 | 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] | 2763 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2764 | |
| 2765 | return 1; |
| 2766 | } |
| 2767 | |
| 2768 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 2769 | * yield the LUA process, and resume it without checking the |
| 2770 | * input arguments. |
| 2771 | */ |
| 2772 | __LJMP static int hlua_channel_send(lua_State *L) |
| 2773 | { |
| 2774 | MAY_LJMP(check_args(L, 2, "send")); |
| 2775 | lua_pushinteger(L, 0); |
| 2776 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2777 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2778 | } |
| 2779 | |
| 2780 | /* This function forward and amount of butes. The data pass from |
| 2781 | * the input side of the buffer to the output side, and can be |
| 2782 | * forwarded. This function never fails. |
| 2783 | * |
| 2784 | * The Lua function takes an amount of bytes to be forwarded in |
| 2785 | * imput. It returns the number of bytes forwarded. |
| 2786 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2787 | __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] | 2788 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2789 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2790 | int len; |
| 2791 | int l; |
| 2792 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2793 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2794 | |
| 2795 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2796 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2797 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 2798 | |
| 2799 | max = len - l; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2800 | if (max > chn->buf->i) |
| 2801 | max = chn->buf->i; |
| 2802 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2803 | l += max; |
| 2804 | |
| 2805 | lua_pop(L, 1); |
| 2806 | lua_pushinteger(L, l); |
| 2807 | |
| 2808 | /* Check if it miss bytes to forward. */ |
| 2809 | if (l < len) { |
| 2810 | /* The the input channel or the output channel are closed, we |
| 2811 | * must return the amount of data forwarded. |
| 2812 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2813 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2814 | return 1; |
| 2815 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2816 | /* If we are waiting for space data in the response buffer, we |
| 2817 | * must set the flag WAKERESWR. This flag required the task |
| 2818 | * wake up if any activity is detected on the response buffer. |
| 2819 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2820 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2821 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 2822 | else |
| 2823 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2824 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2825 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 2826 | 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] | 2827 | } |
| 2828 | |
| 2829 | return 1; |
| 2830 | } |
| 2831 | |
| 2832 | /* Just check the input and prepare the stack for the previous |
| 2833 | * function "hlua_channel_forward_yield" |
| 2834 | */ |
| 2835 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 2836 | { |
| 2837 | MAY_LJMP(check_args(L, 2, "forward")); |
| 2838 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2839 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 2840 | |
| 2841 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2842 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2843 | } |
| 2844 | |
| 2845 | /* Just returns the number of bytes available in the input |
| 2846 | * side of the buffer. This function never fails. |
| 2847 | */ |
| 2848 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 2849 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2850 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2851 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2852 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2853 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2854 | lua_pushinteger(L, chn->buf->i); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2855 | return 1; |
| 2856 | } |
| 2857 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 2858 | /* Returns true if the channel is full. */ |
| 2859 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 2860 | { |
| 2861 | struct channel *chn; |
| 2862 | int rem; |
| 2863 | |
| 2864 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 2865 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2866 | |
| 2867 | rem = chn->buf->size; |
| 2868 | rem -= chn->buf->o; /* Output size */ |
| 2869 | rem -= chn->buf->i; /* Input size */ |
| 2870 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 2871 | |
| 2872 | lua_pushboolean(L, rem <= 0); |
| 2873 | return 1; |
| 2874 | } |
| 2875 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2876 | /* Just returns the number of bytes available in the output |
| 2877 | * side of the buffer. This function never fails. |
| 2878 | */ |
| 2879 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 2880 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2881 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2882 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2883 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2884 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2885 | lua_pushinteger(L, chn->buf->o); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2886 | return 1; |
| 2887 | } |
| 2888 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2889 | /* |
| 2890 | * |
| 2891 | * |
| 2892 | * Class Fetches |
| 2893 | * |
| 2894 | * |
| 2895 | */ |
| 2896 | |
| 2897 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2898 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2899 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2900 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2901 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2902 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | /* This function creates and push in the stack a fetch object according |
| 2906 | * with a current TXN. |
| 2907 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 2908 | 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] | 2909 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2910 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2911 | |
| 2912 | /* Check stack size. */ |
| 2913 | if (!lua_checkstack(L, 3)) |
| 2914 | return 0; |
| 2915 | |
| 2916 | /* Create the object: obj[0] = userdata. |
| 2917 | * Note that the base of the Fetches object is the |
| 2918 | * transaction object. |
| 2919 | */ |
| 2920 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2921 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2922 | lua_rawseti(L, -2, 0); |
| 2923 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 2924 | hsmp->s = txn->s; |
| 2925 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 2926 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 2927 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2928 | |
| 2929 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2930 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 2931 | lua_setmetatable(L, -2); |
| 2932 | |
| 2933 | return 1; |
| 2934 | } |
| 2935 | |
| 2936 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 2937 | * It uses closure argument to store the associated sample-fetch. It |
| 2938 | * returns only one argument or throws an error. An error is thrown |
| 2939 | * only if an error is encountered during the argument parsing. If |
| 2940 | * the "sample-fetch" function fails, nil is returned. |
| 2941 | */ |
| 2942 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 2943 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2944 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 2945 | struct sample_fetch *f; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2946 | struct arg args[ARGM_NBARGS + 1]; |
| 2947 | int i; |
| 2948 | struct sample smp; |
| 2949 | |
| 2950 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2951 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2952 | |
| 2953 | /* Get traditionnal arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2954 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2955 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 2956 | /* Check execution authorization. */ |
| 2957 | if (f->use & SMP_USE_HTTP_ANY && |
| 2958 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 2959 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 2960 | "is not available in Lua services", f->kw); |
| 2961 | WILL_LJMP(lua_error(L)); |
| 2962 | } |
| 2963 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2964 | /* Get extra arguments. */ |
| 2965 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 2966 | if (i >= ARGM_NBARGS) |
| 2967 | break; |
| 2968 | hlua_lua2arg(L, i + 2, &args[i]); |
| 2969 | } |
| 2970 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 2971 | args[i].data.str.str = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2972 | |
| 2973 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 2974 | 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] | 2975 | |
| 2976 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 2977 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2978 | lua_pushfstring(L, "error in arguments"); |
| 2979 | WILL_LJMP(lua_error(L)); |
| 2980 | } |
| 2981 | |
| 2982 | /* Initialise the sample. */ |
| 2983 | memset(&smp, 0, sizeof(smp)); |
| 2984 | |
| 2985 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 2986 | 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] | 2987 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 2988 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2989 | lua_pushstring(L, ""); |
| 2990 | else |
| 2991 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 2992 | return 1; |
| 2993 | } |
| 2994 | |
| 2995 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 2996 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 2997 | hlua_smp2lua_str(L, &smp); |
| 2998 | else |
| 2999 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3000 | return 1; |
| 3001 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3002 | |
| 3003 | /* |
| 3004 | * |
| 3005 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3006 | * Class Converters |
| 3007 | * |
| 3008 | * |
| 3009 | */ |
| 3010 | |
| 3011 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3012 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3013 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3014 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3015 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3016 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3017 | } |
| 3018 | |
| 3019 | /* This function creates and push in the stack a Converters object |
| 3020 | * according with a current TXN. |
| 3021 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3022 | 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] | 3023 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3024 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3025 | |
| 3026 | /* Check stack size. */ |
| 3027 | if (!lua_checkstack(L, 3)) |
| 3028 | return 0; |
| 3029 | |
| 3030 | /* Create the object: obj[0] = userdata. |
| 3031 | * Note that the base of the Converters object is the |
| 3032 | * same than the TXN object. |
| 3033 | */ |
| 3034 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3035 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3036 | lua_rawseti(L, -2, 0); |
| 3037 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3038 | hsmp->s = txn->s; |
| 3039 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3040 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3041 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3042 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3043 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3044 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3045 | lua_setmetatable(L, -2); |
| 3046 | |
| 3047 | return 1; |
| 3048 | } |
| 3049 | |
| 3050 | /* This function is an LUA binding. It is called with each converter. |
| 3051 | * It uses closure argument to store the associated converter. It |
| 3052 | * returns only one argument or throws an error. An error is thrown |
| 3053 | * only if an error is encountered during the argument parsing. If |
| 3054 | * the converter function function fails, nil is returned. |
| 3055 | */ |
| 3056 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3057 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3058 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3059 | struct sample_conv *conv; |
| 3060 | struct arg args[ARGM_NBARGS + 1]; |
| 3061 | int i; |
| 3062 | struct sample smp; |
| 3063 | |
| 3064 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3065 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3066 | |
| 3067 | /* Get traditionnal arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3068 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3069 | |
| 3070 | /* Get extra arguments. */ |
| 3071 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3072 | if (i >= ARGM_NBARGS) |
| 3073 | break; |
| 3074 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3075 | } |
| 3076 | args[i].type = ARGT_STOP; |
David Carlier | abdb00f | 2016-04-27 16:14:50 +0100 | [diff] [blame] | 3077 | args[i].data.str.str = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3078 | |
| 3079 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3080 | 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] | 3081 | |
| 3082 | /* Run the special args checker. */ |
| 3083 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3084 | hlua_pusherror(L, "error in arguments"); |
| 3085 | WILL_LJMP(lua_error(L)); |
| 3086 | } |
| 3087 | |
| 3088 | /* Initialise the sample. */ |
| 3089 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3090 | hlua_pusherror(L, "error in the input argument"); |
| 3091 | WILL_LJMP(lua_error(L)); |
| 3092 | } |
| 3093 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3094 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3095 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3096 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3097 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3098 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3099 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3100 | WILL_LJMP(lua_error(L)); |
| 3101 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3102 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3103 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3104 | hlua_pusherror(L, "error during the input argument casting"); |
| 3105 | WILL_LJMP(lua_error(L)); |
| 3106 | } |
| 3107 | |
| 3108 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3109 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3110 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3111 | lua_pushstring(L, ""); |
| 3112 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3113 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3114 | return 1; |
| 3115 | } |
| 3116 | |
| 3117 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3118 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3119 | hlua_smp2lua_str(L, &smp); |
| 3120 | else |
| 3121 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3122 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3123 | } |
| 3124 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3125 | /* |
| 3126 | * |
| 3127 | * |
| 3128 | * Class AppletTCP |
| 3129 | * |
| 3130 | * |
| 3131 | */ |
| 3132 | |
| 3133 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3134 | * a class stream, otherwise it throws an error. |
| 3135 | */ |
| 3136 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3137 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3138 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3139 | } |
| 3140 | |
| 3141 | /* This function creates and push in the stack an Applet object |
| 3142 | * according with a current TXN. |
| 3143 | */ |
| 3144 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3145 | { |
| 3146 | struct hlua_appctx *appctx; |
| 3147 | struct stream_interface *si = ctx->owner; |
| 3148 | struct stream *s = si_strm(si); |
| 3149 | struct proxy *p = s->be; |
| 3150 | |
| 3151 | /* Check stack size. */ |
| 3152 | if (!lua_checkstack(L, 3)) |
| 3153 | return 0; |
| 3154 | |
| 3155 | /* Create the object: obj[0] = userdata. |
| 3156 | * Note that the base of the Converters object is the |
| 3157 | * same than the TXN object. |
| 3158 | */ |
| 3159 | lua_newtable(L); |
| 3160 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3161 | lua_rawseti(L, -2, 0); |
| 3162 | appctx->appctx = ctx; |
| 3163 | appctx->htxn.s = s; |
| 3164 | appctx->htxn.p = p; |
| 3165 | |
| 3166 | /* Create the "f" field that contains a list of fetches. */ |
| 3167 | lua_pushstring(L, "f"); |
| 3168 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3169 | return 0; |
| 3170 | lua_settable(L, -3); |
| 3171 | |
| 3172 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3173 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3174 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3175 | return 0; |
| 3176 | lua_settable(L, -3); |
| 3177 | |
| 3178 | /* Create the "c" field that contains a list of converters. */ |
| 3179 | lua_pushstring(L, "c"); |
| 3180 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3181 | return 0; |
| 3182 | lua_settable(L, -3); |
| 3183 | |
| 3184 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3185 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3186 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3187 | return 0; |
| 3188 | lua_settable(L, -3); |
| 3189 | |
| 3190 | /* Pop a class stream metatable and affect it to the table. */ |
| 3191 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3192 | lua_setmetatable(L, -2); |
| 3193 | |
| 3194 | return 1; |
| 3195 | } |
| 3196 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3197 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3198 | { |
| 3199 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3200 | struct stream *s = appctx->htxn.s; |
| 3201 | struct hlua *hlua = &s->hlua; |
| 3202 | |
| 3203 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3204 | |
| 3205 | /* Remove previous value. */ |
| 3206 | if (hlua->Mref != -1) |
| 3207 | luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX); |
| 3208 | |
| 3209 | /* Get and store new value. */ |
| 3210 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3211 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3212 | |
| 3213 | return 0; |
| 3214 | } |
| 3215 | |
| 3216 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3217 | { |
| 3218 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3219 | struct stream *s = appctx->htxn.s; |
| 3220 | struct hlua *hlua = &s->hlua; |
| 3221 | |
| 3222 | /* Push configuration index in the stack. */ |
| 3223 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3224 | |
| 3225 | return 1; |
| 3226 | } |
| 3227 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3228 | /* If expected data not yet available, it returns a yield. This function |
| 3229 | * consumes the data in the buffer. It returns a string containing the |
| 3230 | * data. This string can be empty. |
| 3231 | */ |
| 3232 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3233 | { |
| 3234 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3235 | struct stream_interface *si = appctx->appctx->owner; |
| 3236 | int ret; |
| 3237 | char *blk1; |
| 3238 | int len1; |
| 3239 | char *blk2; |
| 3240 | int len2; |
| 3241 | |
| 3242 | /* Read the maximum amount of data avalaible. */ |
| 3243 | ret = bo_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
| 3244 | |
| 3245 | /* Data not yet avalaible. return yield. */ |
| 3246 | if (ret == 0) { |
| 3247 | si_applet_cant_get(si); |
| 3248 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
| 3249 | } |
| 3250 | |
| 3251 | /* End of data: commit the total strings and return. */ |
| 3252 | if (ret < 0) { |
| 3253 | luaL_pushresult(&appctx->b); |
| 3254 | return 1; |
| 3255 | } |
| 3256 | |
| 3257 | /* Ensure that the block 2 length is usable. */ |
| 3258 | if (ret == 1) |
| 3259 | len2 = 0; |
| 3260 | |
| 3261 | /* dont check the max length read and dont check. */ |
| 3262 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3263 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3264 | |
| 3265 | /* Consume input channel output buffer data. */ |
| 3266 | bo_skip(si_oc(si), len1 + len2); |
| 3267 | luaL_pushresult(&appctx->b); |
| 3268 | return 1; |
| 3269 | } |
| 3270 | |
| 3271 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3272 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3273 | { |
| 3274 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3275 | |
| 3276 | /* Initialise the string catenation. */ |
| 3277 | luaL_buffinit(L, &appctx->b); |
| 3278 | |
| 3279 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3280 | } |
| 3281 | |
| 3282 | /* If expected data not yet available, it returns a yield. This function |
| 3283 | * consumes the data in the buffer. It returns a string containing the |
| 3284 | * data. This string can be empty. |
| 3285 | */ |
| 3286 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3287 | { |
| 3288 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3289 | struct stream_interface *si = appctx->appctx->owner; |
| 3290 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3291 | int ret; |
| 3292 | char *blk1; |
| 3293 | int len1; |
| 3294 | char *blk2; |
| 3295 | int len2; |
| 3296 | |
| 3297 | /* Read the maximum amount of data avalaible. */ |
| 3298 | ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
| 3299 | |
| 3300 | /* Data not yet avalaible. return yield. */ |
| 3301 | if (ret == 0) { |
| 3302 | si_applet_cant_get(si); |
| 3303 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3304 | } |
| 3305 | |
| 3306 | /* End of data: commit the total strings and return. */ |
| 3307 | if (ret < 0) { |
| 3308 | luaL_pushresult(&appctx->b); |
| 3309 | return 1; |
| 3310 | } |
| 3311 | |
| 3312 | /* Ensure that the block 2 length is usable. */ |
| 3313 | if (ret == 1) |
| 3314 | len2 = 0; |
| 3315 | |
| 3316 | if (len == -1) { |
| 3317 | |
| 3318 | /* If len == -1, catenate all the data avalaile and |
| 3319 | * yield because we want to get all the data until |
| 3320 | * the end of data stream. |
| 3321 | */ |
| 3322 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3323 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3324 | bo_skip(si_oc(si), len1 + len2); |
| 3325 | si_applet_cant_get(si); |
| 3326 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3327 | |
| 3328 | } else { |
| 3329 | |
| 3330 | /* Copy the fisrt block caping to the length required. */ |
| 3331 | if (len1 > len) |
| 3332 | len1 = len; |
| 3333 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3334 | len -= len1; |
| 3335 | |
| 3336 | /* Copy the second block. */ |
| 3337 | if (len2 > len) |
| 3338 | len2 = len; |
| 3339 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3340 | len -= len2; |
| 3341 | |
| 3342 | /* Consume input channel output buffer data. */ |
| 3343 | bo_skip(si_oc(si), len1 + len2); |
| 3344 | |
| 3345 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3346 | if (len > 0) { |
| 3347 | lua_pushinteger(L, len); |
| 3348 | lua_replace(L, 2); |
| 3349 | si_applet_cant_get(si); |
| 3350 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
| 3351 | } |
| 3352 | |
| 3353 | /* return the result. */ |
| 3354 | luaL_pushresult(&appctx->b); |
| 3355 | return 1; |
| 3356 | } |
| 3357 | |
| 3358 | /* we never executes this */ |
| 3359 | hlua_pusherror(L, "Lua: internal error"); |
| 3360 | WILL_LJMP(lua_error(L)); |
| 3361 | return 0; |
| 3362 | } |
| 3363 | |
| 3364 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3365 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3366 | { |
| 3367 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3368 | int len = -1; |
| 3369 | |
| 3370 | if (lua_gettop(L) > 2) |
| 3371 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3372 | if (lua_gettop(L) >= 2) { |
| 3373 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3374 | lua_pop(L, 1); |
| 3375 | } |
| 3376 | |
| 3377 | /* Confirm or set the required length */ |
| 3378 | lua_pushinteger(L, len); |
| 3379 | |
| 3380 | /* Initialise the string catenation. */ |
| 3381 | luaL_buffinit(L, &appctx->b); |
| 3382 | |
| 3383 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3384 | } |
| 3385 | |
| 3386 | /* Append data in the output side of the buffer. This data is immediatly |
| 3387 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3388 | * cannot contains the data, the function yield. The function returns -1 |
| 3389 | * if the channel is closed. |
| 3390 | */ |
| 3391 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3392 | { |
| 3393 | size_t len; |
| 3394 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3395 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3396 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3397 | struct stream_interface *si = appctx->appctx->owner; |
| 3398 | struct channel *chn = si_ic(si); |
| 3399 | int max; |
| 3400 | |
| 3401 | /* Get the max amount of data which can write as input in the channel. */ |
| 3402 | max = channel_recv_max(chn); |
| 3403 | if (max > (len - l)) |
| 3404 | max = len - l; |
| 3405 | |
| 3406 | /* Copy data. */ |
| 3407 | bi_putblk(chn, str + l, max); |
| 3408 | |
| 3409 | /* update counters. */ |
| 3410 | l += max; |
| 3411 | lua_pop(L, 1); |
| 3412 | lua_pushinteger(L, l); |
| 3413 | |
| 3414 | /* If some data is not send, declares the situation to the |
| 3415 | * applet, and returns a yield. |
| 3416 | */ |
| 3417 | if (l < len) { |
| 3418 | si_applet_cant_put(si); |
| 3419 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
| 3420 | } |
| 3421 | |
| 3422 | return 1; |
| 3423 | } |
| 3424 | |
| 3425 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3426 | * yield the LUA process, and resume it without checking the |
| 3427 | * input arguments. |
| 3428 | */ |
| 3429 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3430 | { |
| 3431 | MAY_LJMP(check_args(L, 2, "send")); |
| 3432 | lua_pushinteger(L, 0); |
| 3433 | |
| 3434 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3435 | } |
| 3436 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3437 | /* |
| 3438 | * |
| 3439 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3440 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3441 | * |
| 3442 | * |
| 3443 | */ |
| 3444 | |
| 3445 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3446 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3447 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3448 | __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] | 3449 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3450 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3451 | } |
| 3452 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3453 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3454 | * according with a current TXN. |
| 3455 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3456 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3457 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3458 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3459 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3460 | struct stream_interface *si = ctx->owner; |
| 3461 | struct stream *s = si_strm(si); |
| 3462 | struct proxy *px = s->be; |
| 3463 | struct http_txn *txn = s->txn; |
| 3464 | const char *path; |
| 3465 | const char *end; |
| 3466 | const char *p; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3467 | |
| 3468 | /* Check stack size. */ |
| 3469 | if (!lua_checkstack(L, 3)) |
| 3470 | return 0; |
| 3471 | |
| 3472 | /* Create the object: obj[0] = userdata. |
| 3473 | * Note that the base of the Converters object is the |
| 3474 | * same than the TXN object. |
| 3475 | */ |
| 3476 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3477 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3478 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3479 | appctx->appctx = ctx; |
| 3480 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
| 3481 | appctx->htxn.s = s; |
| 3482 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3483 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3484 | /* Create the "f" field that contains a list of fetches. */ |
| 3485 | lua_pushstring(L, "f"); |
| 3486 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3487 | return 0; |
| 3488 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3489 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3490 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3491 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3492 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3493 | return 0; |
| 3494 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3495 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3496 | /* Create the "c" field that contains a list of converters. */ |
| 3497 | lua_pushstring(L, "c"); |
| 3498 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3499 | return 0; |
| 3500 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3501 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3502 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3503 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3504 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3505 | return 0; |
| 3506 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3507 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3508 | /* Stores the request method. */ |
| 3509 | lua_pushstring(L, "method"); |
| 3510 | lua_pushlstring(L, txn->req.chn->buf->p, txn->req.sl.rq.m_l); |
| 3511 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3512 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3513 | /* Stores the http version. */ |
| 3514 | lua_pushstring(L, "version"); |
| 3515 | lua_pushlstring(L, txn->req.chn->buf->p + txn->req.sl.rq.v, txn->req.sl.rq.v_l); |
| 3516 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3517 | |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3518 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3519 | * the array on the top of the stack. |
| 3520 | */ |
| 3521 | lua_pushstring(L, "headers"); |
| 3522 | htxn.s = s; |
| 3523 | htxn.p = px; |
| 3524 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3525 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3526 | return 0; |
| 3527 | lua_settable(L, -3); |
| 3528 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3529 | /* Get path and qs */ |
| 3530 | path = http_get_path(txn); |
| 3531 | end = txn->req.chn->buf->p + txn->req.sl.rq.u + txn->req.sl.rq.u_l; |
| 3532 | p = path; |
| 3533 | while (p < end && *p != '?') |
| 3534 | p++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3535 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3536 | /* Stores the request path. */ |
| 3537 | lua_pushstring(L, "path"); |
| 3538 | lua_pushlstring(L, path, p - path); |
| 3539 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3540 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3541 | /* Stores the query string. */ |
| 3542 | lua_pushstring(L, "qs"); |
| 3543 | if (*p == '?') |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3544 | p++; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3545 | lua_pushlstring(L, p, end - p); |
| 3546 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3547 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3548 | /* Stores the request path. */ |
| 3549 | lua_pushstring(L, "length"); |
| 3550 | lua_pushinteger(L, txn->req.body_len); |
| 3551 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3552 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3553 | /* Create an array of HTTP request headers. */ |
| 3554 | lua_pushstring(L, "headers"); |
| 3555 | MAY_LJMP(hlua_http_get_headers(L, &appctx->htxn, &appctx->htxn.s->txn->req)); |
| 3556 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3557 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3558 | /* Create an empty array of HTTP request headers. */ |
| 3559 | lua_pushstring(L, "response"); |
| 3560 | lua_newtable(L); |
| 3561 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3562 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3563 | /* Pop a class stream metatable and affect it to the table. */ |
| 3564 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3565 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3566 | |
| 3567 | return 1; |
| 3568 | } |
| 3569 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3570 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 3571 | { |
| 3572 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3573 | struct stream *s = appctx->htxn.s; |
| 3574 | struct hlua *hlua = &s->hlua; |
| 3575 | |
| 3576 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3577 | |
| 3578 | /* Remove previous value. */ |
| 3579 | if (hlua->Mref != -1) |
| 3580 | luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX); |
| 3581 | |
| 3582 | /* Get and store new value. */ |
| 3583 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3584 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3585 | |
| 3586 | return 0; |
| 3587 | } |
| 3588 | |
| 3589 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 3590 | { |
| 3591 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3592 | struct stream *s = appctx->htxn.s; |
| 3593 | struct hlua *hlua = &s->hlua; |
| 3594 | |
| 3595 | /* Push configuration index in the stack. */ |
| 3596 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3597 | |
| 3598 | return 1; |
| 3599 | } |
| 3600 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3601 | /* If expected data not yet available, it returns a yield. This function |
| 3602 | * consumes the data in the buffer. It returns a string containing the |
| 3603 | * data. This string can be empty. |
| 3604 | */ |
| 3605 | __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] | 3606 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3607 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3608 | struct stream_interface *si = appctx->appctx->owner; |
| 3609 | struct channel *chn = si_ic(si); |
| 3610 | int ret; |
| 3611 | char *blk1; |
| 3612 | int len1; |
| 3613 | char *blk2; |
| 3614 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3615 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3616 | /* Maybe we cant send a 100-continue ? */ |
| 3617 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
| 3618 | ret = bi_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
| 3619 | /* if ret == -2 or -3 the channel closed or the message si too |
| 3620 | * big for the buffers. We cant send anything. So, we ignoring |
| 3621 | * the error, considers that the 100-continue is sent, and try |
| 3622 | * to receive. |
| 3623 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 3624 | */ |
| 3625 | if (ret == -1) { |
| 3626 | si_applet_cant_put(si); |
| 3627 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 3628 | } |
| 3629 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 3630 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3631 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3632 | /* Check for the end of the data. */ |
| 3633 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes <= 0) { |
| 3634 | luaL_pushresult(&appctx->b); |
| 3635 | return 1; |
| 3636 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3637 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3638 | /* Read the maximum amount of data avalaible. */ |
| 3639 | ret = bo_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3640 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3641 | /* Data not yet avalaible. return yield. */ |
| 3642 | if (ret == 0) { |
| 3643 | si_applet_cant_get(si); |
| 3644 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
| 3645 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3646 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3647 | /* End of data: commit the total strings and return. */ |
| 3648 | if (ret < 0) { |
| 3649 | luaL_pushresult(&appctx->b); |
| 3650 | return 1; |
| 3651 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3652 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3653 | /* Ensure that the block 2 length is usable. */ |
| 3654 | if (ret == 1) |
| 3655 | len2 = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3656 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3657 | /* Copy the fisrt block caping to the length required. */ |
| 3658 | if (len1 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 3659 | len1 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 3660 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3661 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3662 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3663 | /* Copy the second block. */ |
| 3664 | if (len2 > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 3665 | len2 = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 3666 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3667 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3668 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3669 | /* Consume input channel output buffer data. */ |
| 3670 | bo_skip(si_oc(si), len1 + len2); |
| 3671 | luaL_pushresult(&appctx->b); |
| 3672 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3673 | } |
| 3674 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3675 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3676 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3677 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3678 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3679 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3680 | /* Initialise the string catenation. */ |
| 3681 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3682 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3683 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3684 | } |
| 3685 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3686 | /* If expected data not yet available, it returns a yield. This function |
| 3687 | * consumes the data in the buffer. It returns a string containing the |
| 3688 | * data. This string can be empty. |
| 3689 | */ |
| 3690 | __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] | 3691 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3692 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3693 | struct stream_interface *si = appctx->appctx->owner; |
| 3694 | int len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3695 | struct channel *chn = si_ic(si); |
| 3696 | int ret; |
| 3697 | char *blk1; |
| 3698 | int len1; |
| 3699 | char *blk2; |
| 3700 | int len2; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3701 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3702 | /* Maybe we cant send a 100-continue ? */ |
| 3703 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_100C) { |
| 3704 | ret = bi_putblk(chn, HTTP_100C, strlen(HTTP_100C)); |
| 3705 | /* if ret == -2 or -3 the channel closed or the message si too |
| 3706 | * big for the buffers. We cant send anything. So, we ignoring |
| 3707 | * the error, considers that the 100-continue is sent, and try |
| 3708 | * to receive. |
| 3709 | * If ret is -1, we dont have room in the buffer, so we yield. |
| 3710 | */ |
| 3711 | if (ret == -1) { |
| 3712 | si_applet_cant_put(si); |
| 3713 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 3714 | } |
| 3715 | appctx->appctx->ctx.hlua_apphttp.flags &= ~APPLET_100C; |
| 3716 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3717 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3718 | /* Read the maximum amount of data avalaible. */ |
| 3719 | ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3720 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3721 | /* Data not yet avalaible. return yield. */ |
| 3722 | if (ret == 0) { |
| 3723 | si_applet_cant_get(si); |
| 3724 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 3725 | } |
| 3726 | |
| 3727 | /* End of data: commit the total strings and return. */ |
| 3728 | if (ret < 0) { |
| 3729 | luaL_pushresult(&appctx->b); |
| 3730 | return 1; |
| 3731 | } |
| 3732 | |
| 3733 | /* Ensure that the block 2 length is usable. */ |
| 3734 | if (ret == 1) |
| 3735 | len2 = 0; |
| 3736 | |
| 3737 | /* Copy the fisrt block caping to the length required. */ |
| 3738 | if (len1 > len) |
| 3739 | len1 = len; |
| 3740 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3741 | len -= len1; |
| 3742 | |
| 3743 | /* Copy the second block. */ |
| 3744 | if (len2 > len) |
| 3745 | len2 = len; |
| 3746 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3747 | len -= len2; |
| 3748 | |
| 3749 | /* Consume input channel output buffer data. */ |
| 3750 | bo_skip(si_oc(si), len1 + len2); |
| 3751 | if (appctx->appctx->ctx.hlua_apphttp.left_bytes != -1) |
| 3752 | appctx->appctx->ctx.hlua_apphttp.left_bytes -= len; |
| 3753 | |
| 3754 | /* If we are no other data avalaible, yield waiting for new data. */ |
| 3755 | if (len > 0) { |
| 3756 | lua_pushinteger(L, len); |
| 3757 | lua_replace(L, 2); |
| 3758 | si_applet_cant_get(si); |
| 3759 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
| 3760 | } |
| 3761 | |
| 3762 | /* return the result. */ |
| 3763 | luaL_pushresult(&appctx->b); |
| 3764 | return 1; |
| 3765 | } |
| 3766 | |
| 3767 | /* Check arguments for the fucntion "hlua_channel_get_yield". */ |
| 3768 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 3769 | { |
| 3770 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3771 | int len = -1; |
| 3772 | |
| 3773 | /* Check arguments. */ |
| 3774 | if (lua_gettop(L) > 2) |
| 3775 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3776 | if (lua_gettop(L) >= 2) { |
| 3777 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3778 | lua_pop(L, 1); |
| 3779 | } |
| 3780 | |
| 3781 | /* Check the required length */ |
| 3782 | if (len == -1 || len > appctx->appctx->ctx.hlua_apphttp.left_bytes) |
| 3783 | len = appctx->appctx->ctx.hlua_apphttp.left_bytes; |
| 3784 | lua_pushinteger(L, len); |
| 3785 | |
| 3786 | /* Initialise the string catenation. */ |
| 3787 | luaL_buffinit(L, &appctx->b); |
| 3788 | |
| 3789 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
| 3790 | } |
| 3791 | |
| 3792 | /* Append data in the output side of the buffer. This data is immediatly |
| 3793 | * sent. The fcuntion returns the ammount of data writed. If the buffer |
| 3794 | * cannot contains the data, the function yield. The function returns -1 |
| 3795 | * if the channel is closed. |
| 3796 | */ |
| 3797 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3798 | { |
| 3799 | size_t len; |
| 3800 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3801 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3802 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3803 | struct stream_interface *si = appctx->appctx->owner; |
| 3804 | struct channel *chn = si_ic(si); |
| 3805 | int max; |
| 3806 | |
| 3807 | /* Get the max amount of data which can write as input in the channel. */ |
| 3808 | max = channel_recv_max(chn); |
| 3809 | if (max > (len - l)) |
| 3810 | max = len - l; |
| 3811 | |
| 3812 | /* Copy data. */ |
| 3813 | bi_putblk(chn, str + l, max); |
| 3814 | |
| 3815 | /* update counters. */ |
| 3816 | l += max; |
| 3817 | lua_pop(L, 1); |
| 3818 | lua_pushinteger(L, l); |
| 3819 | |
| 3820 | /* If some data is not send, declares the situation to the |
| 3821 | * applet, and returns a yield. |
| 3822 | */ |
| 3823 | if (l < len) { |
| 3824 | si_applet_cant_put(si); |
| 3825 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
| 3826 | } |
| 3827 | |
| 3828 | return 1; |
| 3829 | } |
| 3830 | |
| 3831 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 3832 | * yield the LUA process, and resume it without checking the |
| 3833 | * input arguments. |
| 3834 | */ |
| 3835 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 3836 | { |
| 3837 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3838 | size_t len; |
| 3839 | char hex[10]; |
| 3840 | |
| 3841 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3842 | |
| 3843 | /* If transfer encoding chunked is selected, we surround the data |
| 3844 | * by chunk data. |
| 3845 | */ |
| 3846 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED) { |
| 3847 | snprintf(hex, 9, "%x", (unsigned int)len); |
| 3848 | lua_pushfstring(L, "%s\r\n", hex); |
| 3849 | lua_insert(L, 2); /* swap the last 2 entries. */ |
| 3850 | lua_pushstring(L, "\r\n"); |
| 3851 | lua_concat(L, 3); |
| 3852 | } |
| 3853 | |
| 3854 | /* This interger is used for followinf the amount of data sent. */ |
| 3855 | lua_pushinteger(L, 0); |
| 3856 | |
| 3857 | /* We want to send some data. Headers must be sent. */ |
| 3858 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 3859 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 3860 | WILL_LJMP(lua_error(L)); |
| 3861 | } |
| 3862 | |
| 3863 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
| 3864 | } |
| 3865 | |
| 3866 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 3867 | { |
| 3868 | const char *name; |
| 3869 | int ret; |
| 3870 | |
| 3871 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3872 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 3873 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 3874 | |
| 3875 | /* Push in the stack the "response" entry. */ |
| 3876 | ret = lua_getfield(L, 1, "response"); |
| 3877 | if (ret != LUA_TTABLE) { |
| 3878 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 3879 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 3880 | WILL_LJMP(lua_error(L)); |
| 3881 | } |
| 3882 | |
| 3883 | /* check if the header is already registered if it is not |
| 3884 | * the case, register it. |
| 3885 | */ |
| 3886 | ret = lua_getfield(L, -1, name); |
| 3887 | if (ret == LUA_TNIL) { |
| 3888 | |
| 3889 | /* Entry not found. */ |
| 3890 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 3891 | |
| 3892 | /* Insert the new header name in the array in the top of the stack. |
| 3893 | * It left the new array in the top of the stack. |
| 3894 | */ |
| 3895 | lua_newtable(L); |
| 3896 | lua_pushvalue(L, 2); |
| 3897 | lua_pushvalue(L, -2); |
| 3898 | lua_settable(L, -4); |
| 3899 | |
| 3900 | } else if (ret != LUA_TTABLE) { |
| 3901 | |
| 3902 | /* corruption error. */ |
| 3903 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 3904 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 3905 | WILL_LJMP(lua_error(L)); |
| 3906 | } |
| 3907 | |
| 3908 | /* Now the top od thestack is an array of values. We push |
| 3909 | * the header value as new entry. |
| 3910 | */ |
| 3911 | lua_pushvalue(L, 3); |
| 3912 | ret = lua_rawlen(L, -2); |
| 3913 | lua_rawseti(L, -2, ret + 1); |
| 3914 | lua_pushboolean(L, 1); |
| 3915 | return 1; |
| 3916 | } |
| 3917 | |
| 3918 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 3919 | { |
| 3920 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3921 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3922 | |
| 3923 | if (status < 100 || status > 599) { |
| 3924 | lua_pushboolean(L, 0); |
| 3925 | return 1; |
| 3926 | } |
| 3927 | |
| 3928 | appctx->appctx->ctx.hlua_apphttp.status = status; |
| 3929 | lua_pushboolean(L, 1); |
| 3930 | return 1; |
| 3931 | } |
| 3932 | |
| 3933 | /* We will build the status line and the headers of the HTTP response. |
| 3934 | * We will try send at once if its not possible, we give back the hand |
| 3935 | * waiting for more room. |
| 3936 | */ |
| 3937 | __LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx) |
| 3938 | { |
| 3939 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3940 | struct stream_interface *si = appctx->appctx->owner; |
| 3941 | struct channel *chn = si_ic(si); |
| 3942 | int ret; |
| 3943 | size_t len; |
| 3944 | const char *msg; |
| 3945 | |
| 3946 | /* Get the message as the first argument on the stack. */ |
| 3947 | msg = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3948 | |
| 3949 | /* Send the message at once. */ |
| 3950 | ret = bi_putblk(chn, msg, len); |
| 3951 | |
| 3952 | /* if ret == -2 or -3 the channel closed or the message si too |
| 3953 | * big for the buffers. |
| 3954 | */ |
| 3955 | if (ret == -2 || ret == -3) { |
| 3956 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 3957 | WILL_LJMP(lua_error(L)); |
| 3958 | } |
| 3959 | |
| 3960 | /* If ret is -1, we dont have room in the buffer, so we yield. */ |
| 3961 | if (ret == -1) { |
| 3962 | si_applet_cant_put(si); |
| 3963 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0)); |
| 3964 | } |
| 3965 | |
| 3966 | /* Headers sent, set the flag. */ |
| 3967 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 3968 | return 0; |
| 3969 | } |
| 3970 | |
| 3971 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
| 3972 | { |
| 3973 | struct chunk *tmp = get_trash_chunk(); |
| 3974 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3975 | const char *name; |
| 3976 | const char *value; |
| 3977 | int id; |
| 3978 | int hdr_connection = 0; |
| 3979 | int hdr_contentlength = -1; |
| 3980 | int hdr_chunked = 0; |
| 3981 | |
| 3982 | /* Use the same http version than the request. */ |
| 3983 | chunk_appendf(tmp, "HTTP/1.%c %d %s\r\n", |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 3984 | appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 ? '1' : '0', |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3985 | appctx->appctx->ctx.hlua_apphttp.status, |
| 3986 | get_reason(appctx->appctx->ctx.hlua_apphttp.status)); |
| 3987 | |
| 3988 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 3989 | lua_pushvalue(L, 0); |
| 3990 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 3991 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 3992 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 3993 | WILL_LJMP(lua_error(L)); |
| 3994 | } |
| 3995 | |
| 3996 | /* Browse the list of headers. */ |
| 3997 | lua_pushnil(L); |
| 3998 | while(lua_next(L, -2) != 0) { |
| 3999 | |
| 4000 | /* We expect a string as -2. */ |
| 4001 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4002 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4003 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4004 | lua_typename(L, lua_type(L, -2))); |
| 4005 | WILL_LJMP(lua_error(L)); |
| 4006 | } |
| 4007 | name = lua_tostring(L, -2); |
| 4008 | |
| 4009 | /* We expect an array as -1. */ |
| 4010 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4011 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4012 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4013 | name, |
| 4014 | lua_typename(L, lua_type(L, -1))); |
| 4015 | WILL_LJMP(lua_error(L)); |
| 4016 | } |
| 4017 | |
| 4018 | /* Browse the table who is on the top of the stack. */ |
| 4019 | lua_pushnil(L); |
| 4020 | while(lua_next(L, -2) != 0) { |
| 4021 | |
| 4022 | /* We expect a number as -2. */ |
| 4023 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4024 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4025 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4026 | name, |
| 4027 | lua_typename(L, lua_type(L, -2))); |
| 4028 | WILL_LJMP(lua_error(L)); |
| 4029 | } |
| 4030 | id = lua_tointeger(L, -2); |
| 4031 | |
| 4032 | /* We expect a string as -2. */ |
| 4033 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4034 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4035 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4036 | name, id, |
| 4037 | lua_typename(L, lua_type(L, -1))); |
| 4038 | WILL_LJMP(lua_error(L)); |
| 4039 | } |
| 4040 | value = lua_tostring(L, -1); |
| 4041 | |
| 4042 | /* Catenate a new header. */ |
| 4043 | chunk_appendf(tmp, "%s: %s\r\n", name, value); |
| 4044 | |
| 4045 | /* Protocol checks. */ |
| 4046 | |
| 4047 | /* Check if the header conneciton is present. */ |
| 4048 | if (strcasecmp("connection", name) == 0) |
| 4049 | hdr_connection = 1; |
| 4050 | |
| 4051 | /* Copy the header content length. The length conversion |
| 4052 | * is done without control. If it contains a ad value, this |
| 4053 | * is not our problem. |
| 4054 | */ |
| 4055 | if (strcasecmp("content-length", name) == 0) |
| 4056 | hdr_contentlength = atoi(value); |
| 4057 | |
| 4058 | /* Check if the client annouces a transfer-encoding chunked it self. */ |
| 4059 | if (strcasecmp("transfer-encoding", name) == 0 && |
| 4060 | strcasecmp("chunked", value) == 0) |
| 4061 | hdr_chunked = 1; |
| 4062 | |
| 4063 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4064 | lua_pop(L, 1); |
| 4065 | } |
| 4066 | |
| 4067 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4068 | lua_pop(L, 1); |
| 4069 | } |
| 4070 | |
| 4071 | /* If the http protocol version is 1.1, we expect an header "connection" set |
| 4072 | * to "close" to be HAProxy/keeplive compliant. Otherwise, we expect nothing. |
| 4073 | * If the header conneciton is present, don't change it, if it is not present, |
| 4074 | * we must set. |
| 4075 | * |
| 4076 | * we set a "connection: close" header for ensuring that the keepalive will be |
| 4077 | * respected by haproxy. HAProcy considers that the application cloe the connection |
| 4078 | * and it keep the connection from the client open. |
| 4079 | */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 4080 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11 && !hdr_connection) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4081 | chunk_appendf(tmp, "Connection: close\r\n"); |
| 4082 | |
| 4083 | /* If we dont have a content-length set, we must announce a transfer enconding |
| 4084 | * chunked. This is required by haproxy for the keepalive compliance. |
| 4085 | * If the applet annouce a transfer-encoding chunked itslef, don't |
| 4086 | * do anything. |
| 4087 | */ |
| 4088 | if (hdr_contentlength == -1 && hdr_chunked == 0) { |
| 4089 | chunk_appendf(tmp, "Transfer-encoding: chunked\r\n"); |
| 4090 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_CHUNKED; |
| 4091 | } |
| 4092 | |
| 4093 | /* Finalize headers. */ |
| 4094 | chunk_appendf(tmp, "\r\n"); |
| 4095 | |
| 4096 | /* Remove the last entry and the array of headers */ |
| 4097 | lua_pop(L, 2); |
| 4098 | |
| 4099 | /* Push the headers block. */ |
| 4100 | lua_pushlstring(L, tmp->str, tmp->len); |
| 4101 | |
| 4102 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
| 4103 | } |
| 4104 | |
| 4105 | /* |
| 4106 | * |
| 4107 | * |
| 4108 | * Class HTTP |
| 4109 | * |
| 4110 | * |
| 4111 | */ |
| 4112 | |
| 4113 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4114 | * a class stream, otherwise it throws an error. |
| 4115 | */ |
| 4116 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
| 4117 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4118 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | /* This function creates and push in the stack a HTTP object |
| 4122 | * according with a current TXN. |
| 4123 | */ |
| 4124 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4125 | { |
| 4126 | struct hlua_txn *htxn; |
| 4127 | |
| 4128 | /* Check stack size. */ |
| 4129 | if (!lua_checkstack(L, 3)) |
| 4130 | return 0; |
| 4131 | |
| 4132 | /* Create the object: obj[0] = userdata. |
| 4133 | * Note that the base of the Converters object is the |
| 4134 | * same than the TXN object. |
| 4135 | */ |
| 4136 | lua_newtable(L); |
| 4137 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4138 | lua_rawseti(L, -2, 0); |
| 4139 | |
| 4140 | htxn->s = txn->s; |
| 4141 | htxn->p = txn->p; |
| 4142 | |
| 4143 | /* Pop a class stream metatable and affect it to the table. */ |
| 4144 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4145 | lua_setmetatable(L, -2); |
| 4146 | |
| 4147 | return 1; |
| 4148 | } |
| 4149 | |
| 4150 | /* This function creates ans returns an array of HTTP headers. |
| 4151 | * This function does not fails. It is used as wrapper with the |
| 4152 | * 2 following functions. |
| 4153 | */ |
| 4154 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4155 | { |
| 4156 | const char *cur_ptr, *cur_next, *p; |
| 4157 | int old_idx, cur_idx; |
| 4158 | struct hdr_idx_elem *cur_hdr; |
| 4159 | const char *hn, *hv; |
| 4160 | int hnl, hvl; |
| 4161 | int type; |
| 4162 | const char *in; |
| 4163 | char *out; |
| 4164 | int len; |
| 4165 | |
| 4166 | /* Create the table. */ |
| 4167 | lua_newtable(L); |
| 4168 | |
| 4169 | if (!htxn->s->txn) |
| 4170 | return 1; |
| 4171 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4172 | /* Check if a valid response is parsed */ |
| 4173 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4174 | return 1; |
| 4175 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4176 | /* Build array of headers. */ |
| 4177 | old_idx = 0; |
| 4178 | cur_next = msg->chn->buf->p + hdr_idx_first_pos(&htxn->s->txn->hdr_idx); |
| 4179 | |
| 4180 | while (1) { |
| 4181 | cur_idx = htxn->s->txn->hdr_idx.v[old_idx].next; |
| 4182 | if (!cur_idx) |
| 4183 | break; |
| 4184 | old_idx = cur_idx; |
| 4185 | |
| 4186 | cur_hdr = &htxn->s->txn->hdr_idx.v[cur_idx]; |
| 4187 | cur_ptr = cur_next; |
| 4188 | cur_next = cur_ptr + cur_hdr->len + cur_hdr->cr + 1; |
| 4189 | |
| 4190 | /* Now we have one full header at cur_ptr of len cur_hdr->len, |
| 4191 | * and the next header starts at cur_next. We'll check |
| 4192 | * this header in the list as well as against the default |
| 4193 | * rule. |
| 4194 | */ |
| 4195 | |
| 4196 | /* look for ': *'. */ |
| 4197 | hn = cur_ptr; |
| 4198 | for (p = cur_ptr; p < cur_ptr + cur_hdr->len && *p != ':'; p++); |
| 4199 | if (p >= cur_ptr+cur_hdr->len) |
| 4200 | continue; |
| 4201 | hnl = p - hn; |
| 4202 | p++; |
| 4203 | while (p < cur_ptr+cur_hdr->len && ( *p == ' ' || *p == '\t' )) |
| 4204 | p++; |
| 4205 | if (p >= cur_ptr+cur_hdr->len) |
| 4206 | continue; |
| 4207 | hv = p; |
| 4208 | hvl = cur_ptr+cur_hdr->len-p; |
| 4209 | |
| 4210 | /* Lowercase the key. Don't check the size of trash, it have |
| 4211 | * the size of one buffer and the input data contains in one |
| 4212 | * buffer. |
| 4213 | */ |
| 4214 | out = trash.str; |
| 4215 | for (in=hn; in<hn+hnl; in++, out++) |
| 4216 | *out = tolower(*in); |
| 4217 | *out = '\0'; |
| 4218 | |
| 4219 | /* Check for existing entry: |
| 4220 | * assume that the table is on the top of the stack, and |
| 4221 | * push the key in the stack, the function lua_gettable() |
| 4222 | * perform the lookup. |
| 4223 | */ |
| 4224 | lua_pushlstring(L, trash.str, hnl); |
| 4225 | lua_gettable(L, -2); |
| 4226 | type = lua_type(L, -1); |
| 4227 | |
| 4228 | switch (type) { |
| 4229 | case LUA_TNIL: |
| 4230 | /* Table not found, create it. */ |
| 4231 | lua_pop(L, 1); /* remove the nil value. */ |
| 4232 | lua_pushlstring(L, trash.str, hnl); /* push the header name as key. */ |
| 4233 | lua_newtable(L); /* create and push empty table. */ |
| 4234 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4235 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4236 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 4237 | break; |
| 4238 | |
| 4239 | case LUA_TTABLE: |
| 4240 | /* Entry found: push the value in the table. */ |
| 4241 | len = lua_rawlen(L, -1); |
| 4242 | lua_pushlstring(L, hv, hvl); /* push header value. */ |
| 4243 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4244 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4245 | break; |
| 4246 | |
| 4247 | default: |
| 4248 | /* Other cases are errors. */ |
| 4249 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4250 | WILL_LJMP(lua_error(L)); |
| 4251 | } |
| 4252 | } |
| 4253 | |
| 4254 | return 1; |
| 4255 | } |
| 4256 | |
| 4257 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4258 | { |
| 4259 | struct hlua_txn *htxn; |
| 4260 | |
| 4261 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4262 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4263 | |
| 4264 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4265 | } |
| 4266 | |
| 4267 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4268 | { |
| 4269 | struct hlua_txn *htxn; |
| 4270 | |
| 4271 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4272 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4273 | |
| 4274 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4275 | } |
| 4276 | |
| 4277 | /* This function replace full header, or just a value in |
| 4278 | * the request or in the response. It is a wrapper fir the |
| 4279 | * 4 following functions. |
| 4280 | */ |
| 4281 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4282 | struct http_msg *msg, int action) |
| 4283 | { |
| 4284 | size_t name_len; |
| 4285 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4286 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4287 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
| 4288 | struct my_regex re; |
| 4289 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4290 | /* Check if a valid response is parsed */ |
| 4291 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4292 | return 0; |
| 4293 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4294 | if (!regex_comp(reg, &re, 1, 1, NULL)) |
| 4295 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4296 | |
| 4297 | http_transform_header_str(htxn->s, msg, name, name_len, value, &re, action); |
| 4298 | regex_free(&re); |
| 4299 | return 0; |
| 4300 | } |
| 4301 | |
| 4302 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4303 | { |
| 4304 | struct hlua_txn *htxn; |
| 4305 | |
| 4306 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4307 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4308 | |
| 4309 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4310 | } |
| 4311 | |
| 4312 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4313 | { |
| 4314 | struct hlua_txn *htxn; |
| 4315 | |
| 4316 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4317 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4318 | |
| 4319 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4320 | } |
| 4321 | |
| 4322 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4323 | { |
| 4324 | struct hlua_txn *htxn; |
| 4325 | |
| 4326 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4327 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4328 | |
| 4329 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4330 | } |
| 4331 | |
| 4332 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4333 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4334 | struct hlua_txn *htxn; |
| 4335 | |
| 4336 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4337 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4338 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4339 | 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] | 4340 | } |
| 4341 | |
| 4342 | /* This function deletes all the occurences of an header. |
| 4343 | * It is a wrapper for the 2 following functions. |
| 4344 | */ |
| 4345 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4346 | { |
| 4347 | size_t len; |
| 4348 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4349 | struct hdr_ctx ctx; |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4350 | struct http_txn *txn = htxn->s->txn; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4351 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4352 | /* Check if a valid response is parsed */ |
| 4353 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4354 | return 0; |
| 4355 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4356 | ctx.idx = 0; |
| 4357 | while (http_find_header2(name, len, msg->chn->buf->p, &txn->hdr_idx, &ctx)) |
| 4358 | http_remove_header2(msg, &txn->hdr_idx, &ctx); |
| 4359 | return 0; |
| 4360 | } |
| 4361 | |
| 4362 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4363 | { |
| 4364 | struct hlua_txn *htxn; |
| 4365 | |
| 4366 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4367 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4368 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4369 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4373 | { |
| 4374 | struct hlua_txn *htxn; |
| 4375 | |
| 4376 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4377 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4378 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4379 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4380 | } |
| 4381 | |
| 4382 | /* This function adds an header. It is a wrapper used by |
| 4383 | * the 2 following functions. |
| 4384 | */ |
| 4385 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4386 | { |
| 4387 | size_t name_len; |
| 4388 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4389 | size_t value_len; |
| 4390 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
| 4391 | char *p; |
| 4392 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4393 | /* Check if a valid message is parsed */ |
| 4394 | if (unlikely(msg->msg_state < HTTP_MSG_BODY)) |
| 4395 | return 0; |
| 4396 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4397 | /* Check length. */ |
| 4398 | trash.len = value_len + name_len + 2; |
| 4399 | if (trash.len > trash.size) |
| 4400 | return 0; |
| 4401 | |
| 4402 | /* Creates the header string. */ |
| 4403 | p = trash.str; |
| 4404 | memcpy(p, name, name_len); |
| 4405 | p += name_len; |
| 4406 | *p = ':'; |
| 4407 | p++; |
| 4408 | *p = ' '; |
| 4409 | p++; |
| 4410 | memcpy(p, value, value_len); |
| 4411 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4412 | 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] | 4413 | trash.str, trash.len) != 0); |
| 4414 | |
| 4415 | return 0; |
| 4416 | } |
| 4417 | |
| 4418 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4419 | { |
| 4420 | struct hlua_txn *htxn; |
| 4421 | |
| 4422 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4423 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4424 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4425 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4426 | } |
| 4427 | |
| 4428 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4429 | { |
| 4430 | struct hlua_txn *htxn; |
| 4431 | |
| 4432 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4433 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4434 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4435 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4436 | } |
| 4437 | |
| 4438 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4439 | { |
| 4440 | struct hlua_txn *htxn; |
| 4441 | |
| 4442 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4443 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4444 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4445 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4446 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4447 | } |
| 4448 | |
| 4449 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4450 | { |
| 4451 | struct hlua_txn *htxn; |
| 4452 | |
| 4453 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4454 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4455 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4456 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4457 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4458 | } |
| 4459 | |
| 4460 | /* This function set the method. */ |
| 4461 | static int hlua_http_req_set_meth(lua_State *L) |
| 4462 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4463 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4464 | size_t name_len; |
| 4465 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4466 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4467 | /* Check if a valid request is parsed */ |
| 4468 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4469 | lua_pushboolean(L, 0); |
| 4470 | return 1; |
| 4471 | } |
| 4472 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4473 | 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] | 4474 | return 1; |
| 4475 | } |
| 4476 | |
| 4477 | /* This function set the method. */ |
| 4478 | static int hlua_http_req_set_path(lua_State *L) |
| 4479 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4480 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4481 | size_t name_len; |
| 4482 | 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] | 4483 | |
| 4484 | /* Check if a valid request is parsed */ |
| 4485 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4486 | lua_pushboolean(L, 0); |
| 4487 | return 1; |
| 4488 | } |
| 4489 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4490 | 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] | 4491 | return 1; |
| 4492 | } |
| 4493 | |
| 4494 | /* This function set the query-string. */ |
| 4495 | static int hlua_http_req_set_query(lua_State *L) |
| 4496 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4497 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4498 | size_t name_len; |
| 4499 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4500 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4501 | /* Check if a valid request is parsed */ |
| 4502 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4503 | lua_pushboolean(L, 0); |
| 4504 | return 1; |
| 4505 | } |
| 4506 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4507 | /* Check length. */ |
| 4508 | if (name_len > trash.size - 1) { |
| 4509 | lua_pushboolean(L, 0); |
| 4510 | return 1; |
| 4511 | } |
| 4512 | |
| 4513 | /* Add the mark question as prefix. */ |
| 4514 | chunk_reset(&trash); |
| 4515 | trash.str[trash.len++] = '?'; |
| 4516 | memcpy(trash.str + trash.len, name, name_len); |
| 4517 | trash.len += name_len; |
| 4518 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4519 | 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] | 4520 | return 1; |
| 4521 | } |
| 4522 | |
| 4523 | /* This function set the uri. */ |
| 4524 | static int hlua_http_req_set_uri(lua_State *L) |
| 4525 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4526 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4527 | size_t name_len; |
| 4528 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4529 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4530 | /* Check if a valid request is parsed */ |
| 4531 | if (unlikely(htxn->s->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 4532 | lua_pushboolean(L, 0); |
| 4533 | return 1; |
| 4534 | } |
| 4535 | |
Willy Tarreau | 987e3fb | 2015-04-04 01:09:08 +0200 | [diff] [blame] | 4536 | 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] | 4537 | return 1; |
| 4538 | } |
| 4539 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4540 | /* This function set the response code. */ |
| 4541 | static int hlua_http_res_set_status(lua_State *L) |
| 4542 | { |
| 4543 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4544 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4545 | |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4546 | /* Check if a valid response is parsed */ |
| 4547 | if (unlikely(htxn->s->txn->rsp.msg_state < HTTP_MSG_BODY)) |
| 4548 | return 0; |
| 4549 | |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 4550 | http_set_status(code, htxn->s); |
| 4551 | return 0; |
| 4552 | } |
| 4553 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4554 | /* |
| 4555 | * |
| 4556 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4557 | * Class TXN |
| 4558 | * |
| 4559 | * |
| 4560 | */ |
| 4561 | |
| 4562 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4563 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4564 | */ |
| 4565 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 4566 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4567 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4568 | } |
| 4569 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4570 | __LJMP static int hlua_set_var(lua_State *L) |
| 4571 | { |
| 4572 | struct hlua_txn *htxn; |
| 4573 | const char *name; |
| 4574 | size_t len; |
| 4575 | struct sample smp; |
| 4576 | |
| 4577 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4578 | |
| 4579 | /* It is useles to retrieve the stream, but this function |
| 4580 | * runs only in a stream context. |
| 4581 | */ |
| 4582 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4583 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4584 | |
| 4585 | /* Converts the third argument in a sample. */ |
| 4586 | hlua_lua2smp(L, 3, &smp); |
| 4587 | |
| 4588 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 4589 | 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] | 4590 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4591 | return 0; |
| 4592 | } |
| 4593 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 4594 | __LJMP static int hlua_unset_var(lua_State *L) |
| 4595 | { |
| 4596 | struct hlua_txn *htxn; |
| 4597 | const char *name; |
| 4598 | size_t len; |
| 4599 | struct sample smp; |
| 4600 | |
| 4601 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4602 | |
| 4603 | /* It is useles to retrieve the stream, but this function |
| 4604 | * runs only in a stream context. |
| 4605 | */ |
| 4606 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4607 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4608 | |
| 4609 | /* Unset the variable. */ |
| 4610 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 4611 | vars_unset_by_name(name, len, &smp); |
| 4612 | return 0; |
| 4613 | } |
| 4614 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4615 | __LJMP static int hlua_get_var(lua_State *L) |
| 4616 | { |
| 4617 | struct hlua_txn *htxn; |
| 4618 | const char *name; |
| 4619 | size_t len; |
| 4620 | struct sample smp; |
| 4621 | |
| 4622 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4623 | |
| 4624 | /* It is useles to retrieve the stream, but this function |
| 4625 | * runs only in a stream context. |
| 4626 | */ |
| 4627 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4628 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4629 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 4630 | 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] | 4631 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 4632 | lua_pushnil(L); |
| 4633 | return 1; |
| 4634 | } |
| 4635 | |
| 4636 | return hlua_smp2lua(L, &smp); |
| 4637 | } |
| 4638 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4639 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4640 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4641 | struct hlua *hlua; |
| 4642 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4643 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4644 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4645 | /* It is useles to retrieve the stream, but this function |
| 4646 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4647 | */ |
| 4648 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4649 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4650 | |
| 4651 | /* Remove previous value. */ |
| 4652 | if (hlua->Mref != -1) |
| 4653 | luaL_unref(L, hlua->Mref, LUA_REGISTRYINDEX); |
| 4654 | |
| 4655 | /* Get and store new value. */ |
| 4656 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4657 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4658 | |
| 4659 | return 0; |
| 4660 | } |
| 4661 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 4662 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4663 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4664 | struct hlua *hlua; |
| 4665 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4666 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 4667 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4668 | /* It is useles to retrieve the stream, but this function |
| 4669 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4670 | */ |
| 4671 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4672 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 4673 | |
| 4674 | /* Push configuration index in the stack. */ |
| 4675 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4676 | |
| 4677 | return 1; |
| 4678 | } |
| 4679 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4680 | /* Create stack entry containing a class TXN. This function |
| 4681 | * return 0 if the stack does not contains free slots, |
| 4682 | * otherwise it returns 1. |
| 4683 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 4684 | 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] | 4685 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4686 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4687 | |
| 4688 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4689 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4690 | return 0; |
| 4691 | |
| 4692 | /* NOTE: The allocation never fails. The failure |
| 4693 | * throw an error, and the function never returns. |
| 4694 | * if the throw is not avalaible, the process is aborted. |
| 4695 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4696 | /* Create the object: obj[0] = userdata. */ |
| 4697 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4698 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 4699 | lua_rawseti(L, -2, 0); |
| 4700 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4701 | htxn->s = s; |
| 4702 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4703 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 4704 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4705 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4706 | /* Create the "f" field that contains a list of fetches. */ |
| 4707 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4708 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4709 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4710 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4711 | |
| 4712 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4713 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4714 | 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] | 4715 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4716 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4717 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4718 | /* Create the "c" field that contains a list of converters. */ |
| 4719 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4720 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4721 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4722 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4723 | |
| 4724 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4725 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4726 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4727 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4728 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4729 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4730 | /* Create the "req" field that contains the request channel object. */ |
| 4731 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 4732 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4733 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4734 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4735 | |
| 4736 | /* Create the "res" field that contains the response channel object. */ |
| 4737 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 4738 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4739 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4740 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 4741 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4742 | /* Creates the HTTP object is the current proxy allows http. */ |
| 4743 | lua_pushstring(L, "http"); |
| 4744 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 4745 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4746 | return 0; |
| 4747 | } |
| 4748 | else |
| 4749 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 4750 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4751 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 4752 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4753 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 4754 | lua_setmetatable(L, -2); |
| 4755 | |
| 4756 | return 1; |
| 4757 | } |
| 4758 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 4759 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 4760 | { |
| 4761 | const char *msg; |
| 4762 | struct hlua_txn *htxn; |
| 4763 | |
| 4764 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 4765 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4766 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4767 | |
| 4768 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 4769 | return 0; |
| 4770 | } |
| 4771 | |
| 4772 | __LJMP static int hlua_txn_log(lua_State *L) |
| 4773 | { |
| 4774 | int level; |
| 4775 | const char *msg; |
| 4776 | struct hlua_txn *htxn; |
| 4777 | |
| 4778 | MAY_LJMP(check_args(L, 3, "log")); |
| 4779 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4780 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4781 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4782 | |
| 4783 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 4784 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 4785 | |
| 4786 | hlua_sendlog(htxn->s->be, level, msg); |
| 4787 | return 0; |
| 4788 | } |
| 4789 | |
| 4790 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 4791 | { |
| 4792 | const char *msg; |
| 4793 | struct hlua_txn *htxn; |
| 4794 | |
| 4795 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 4796 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4797 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4798 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 4799 | return 0; |
| 4800 | } |
| 4801 | |
| 4802 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 4803 | { |
| 4804 | const char *msg; |
| 4805 | struct hlua_txn *htxn; |
| 4806 | |
| 4807 | MAY_LJMP(check_args(L, 2, "Info")); |
| 4808 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4809 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4810 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 4811 | return 0; |
| 4812 | } |
| 4813 | |
| 4814 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 4815 | { |
| 4816 | const char *msg; |
| 4817 | struct hlua_txn *htxn; |
| 4818 | |
| 4819 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 4820 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4821 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4822 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 4823 | return 0; |
| 4824 | } |
| 4825 | |
| 4826 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 4827 | { |
| 4828 | const char *msg; |
| 4829 | struct hlua_txn *htxn; |
| 4830 | |
| 4831 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 4832 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4833 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4834 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 4835 | return 0; |
| 4836 | } |
| 4837 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 4838 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 4839 | { |
| 4840 | struct hlua_txn *htxn; |
| 4841 | int ll; |
| 4842 | |
| 4843 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 4844 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4845 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4846 | |
| 4847 | if (ll < 0 || ll > 7) |
| 4848 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 4849 | |
| 4850 | htxn->s->logs.level = ll; |
| 4851 | return 0; |
| 4852 | } |
| 4853 | |
| 4854 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 4855 | { |
| 4856 | struct hlua_txn *htxn; |
| 4857 | struct connection *cli_conn; |
| 4858 | int tos; |
| 4859 | |
| 4860 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 4861 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4862 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4863 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 4864 | if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn)) |
Vincent Bernat | 6e61589 | 2016-05-18 16:17:44 +0200 | [diff] [blame] | 4865 | inet_set_tos(cli_conn->t.sock.fd, &cli_conn->addr.from, tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 4866 | |
| 4867 | return 0; |
| 4868 | } |
| 4869 | |
| 4870 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 4871 | { |
| 4872 | #ifdef SO_MARK |
| 4873 | struct hlua_txn *htxn; |
| 4874 | struct connection *cli_conn; |
| 4875 | int mark; |
| 4876 | |
| 4877 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 4878 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 4879 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4880 | |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 4881 | if ((cli_conn = objt_conn(htxn->s->sess->origin)) && conn_ctrl_ready(cli_conn)) |
Willy Tarreau | 07081fe | 2015-04-06 10:59:20 +0200 | [diff] [blame] | 4882 | setsockopt(cli_conn->t.sock.fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 4883 | #endif |
| 4884 | return 0; |
| 4885 | } |
| 4886 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4887 | /* This function is an Lua binding that send pending data |
| 4888 | * to the client, and close the stream interface. |
| 4889 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 4890 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4891 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4892 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 4893 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 4894 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4895 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4896 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4897 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 4898 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4899 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 4900 | /* If the flags NOTERM is set, we cannot terminate the http |
| 4901 | * session, so we just end the execution of the current |
| 4902 | * lua code. |
| 4903 | */ |
| 4904 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 4905 | WILL_LJMP(hlua_done(L)); |
| 4906 | return 0; |
| 4907 | } |
| 4908 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4909 | ic = &htxn->s->req; |
| 4910 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 4911 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 4912 | if (htxn->s->txn) { |
| 4913 | /* HTTP mode, let's stay in sync with the stream */ |
| 4914 | bi_fast_delete(ic->buf, htxn->s->txn->req.sov); |
| 4915 | htxn->s->txn->req.next -= htxn->s->txn->req.sov; |
| 4916 | htxn->s->txn->req.sov = 0; |
| 4917 | ic->analysers &= AN_REQ_HTTP_XFER_BODY; |
| 4918 | oc->analysers = AN_RES_HTTP_XFER_BODY; |
| 4919 | htxn->s->txn->req.msg_state = HTTP_MSG_CLOSED; |
| 4920 | htxn->s->txn->rsp.msg_state = HTTP_MSG_DONE; |
| 4921 | |
Willy Tarreau | 630ef45 | 2015-08-28 10:06:15 +0200 | [diff] [blame] | 4922 | /* Note that if we want to support keep-alive, we need |
| 4923 | * to bypass the close/shutr_now calls below, but that |
| 4924 | * may only be done if the HTTP request was already |
| 4925 | * processed and the connection header is known (ie |
| 4926 | * not during TCP rules). |
| 4927 | */ |
| 4928 | } |
| 4929 | |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 4930 | channel_auto_read(ic); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 4931 | channel_abort(ic); |
| 4932 | channel_auto_close(ic); |
| 4933 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 4934 | |
| 4935 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 4936 | channel_auto_read(oc); |
| 4937 | channel_auto_close(oc); |
| 4938 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 4939 | |
Willy Tarreau | 0458b08 | 2015-08-28 09:40:04 +0200 | [diff] [blame] | 4940 | ic->analysers = 0; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 4941 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 4942 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 4943 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 4944 | return 0; |
| 4945 | } |
| 4946 | |
| 4947 | __LJMP static int hlua_log(lua_State *L) |
| 4948 | { |
| 4949 | int level; |
| 4950 | const char *msg; |
| 4951 | |
| 4952 | MAY_LJMP(check_args(L, 2, "log")); |
| 4953 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 4954 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4955 | |
| 4956 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 4957 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 4958 | |
| 4959 | hlua_sendlog(NULL, level, msg); |
| 4960 | return 0; |
| 4961 | } |
| 4962 | |
| 4963 | __LJMP static int hlua_log_debug(lua_State *L) |
| 4964 | { |
| 4965 | const char *msg; |
| 4966 | |
| 4967 | MAY_LJMP(check_args(L, 1, "debug")); |
| 4968 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 4969 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 4970 | return 0; |
| 4971 | } |
| 4972 | |
| 4973 | __LJMP static int hlua_log_info(lua_State *L) |
| 4974 | { |
| 4975 | const char *msg; |
| 4976 | |
| 4977 | MAY_LJMP(check_args(L, 1, "info")); |
| 4978 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 4979 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 4980 | return 0; |
| 4981 | } |
| 4982 | |
| 4983 | __LJMP static int hlua_log_warning(lua_State *L) |
| 4984 | { |
| 4985 | const char *msg; |
| 4986 | |
| 4987 | MAY_LJMP(check_args(L, 1, "warning")); |
| 4988 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 4989 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 4990 | return 0; |
| 4991 | } |
| 4992 | |
| 4993 | __LJMP static int hlua_log_alert(lua_State *L) |
| 4994 | { |
| 4995 | const char *msg; |
| 4996 | |
| 4997 | MAY_LJMP(check_args(L, 1, "alert")); |
| 4998 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 4999 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5000 | return 0; |
| 5001 | } |
| 5002 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5003 | __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] | 5004 | { |
| 5005 | int wakeup_ms = lua_tointeger(L, -1); |
| 5006 | if (now_ms < wakeup_ms) |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5007 | 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] | 5008 | return 0; |
| 5009 | } |
| 5010 | |
| 5011 | __LJMP static int hlua_sleep(lua_State *L) |
| 5012 | { |
| 5013 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5014 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5015 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5016 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5017 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5018 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5019 | wakeup_ms = tick_add(now_ms, delay); |
| 5020 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5021 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5022 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5023 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5024 | } |
| 5025 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5026 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5027 | { |
| 5028 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5029 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5030 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5031 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5032 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5033 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5034 | wakeup_ms = tick_add(now_ms, delay); |
| 5035 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5036 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5037 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0)); |
| 5038 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5039 | } |
| 5040 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5041 | /* This functionis an LUA binding. it permits to give back |
| 5042 | * the hand at the HAProxy scheduler. It is used when the |
| 5043 | * LUA processing consumes a lot of time. |
| 5044 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5045 | __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] | 5046 | { |
| 5047 | return 0; |
| 5048 | } |
| 5049 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5050 | __LJMP static int hlua_yield(lua_State *L) |
| 5051 | { |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5052 | WILL_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD)); |
| 5053 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5054 | } |
| 5055 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5056 | /* This function change the nice of the currently executed |
| 5057 | * task. It is used set low or high priority at the current |
| 5058 | * task. |
| 5059 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5060 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5061 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5062 | struct hlua *hlua; |
| 5063 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5064 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5065 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5066 | hlua = hlua_gethlua(L); |
| 5067 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5068 | |
| 5069 | /* If he task is not set, I'm in a start mode. */ |
| 5070 | if (!hlua || !hlua->task) |
| 5071 | return 0; |
| 5072 | |
| 5073 | if (nice < -1024) |
| 5074 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5075 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5076 | nice = 1024; |
| 5077 | |
| 5078 | hlua->task->nice = nice; |
| 5079 | return 0; |
| 5080 | } |
| 5081 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5082 | /* This function is used as a calback of a task. It is called by the |
| 5083 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5084 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5085 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5086 | * |
| 5087 | * Task wrapper are longjmp safe because the only one Lua code |
| 5088 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5089 | */ |
| 5090 | static struct task *hlua_process_task(struct task *task) |
| 5091 | { |
| 5092 | struct hlua *hlua = task->context; |
| 5093 | enum hlua_exec status; |
| 5094 | |
| 5095 | /* We need to remove the task from the wait queue before executing |
| 5096 | * the Lua code because we don't know if it needs to wait for |
| 5097 | * another timer or not in the case of E_AGAIN. |
| 5098 | */ |
| 5099 | task_delete(task); |
| 5100 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5101 | /* If it is the first call to the task, we must initialize the |
| 5102 | * execution timeouts. |
| 5103 | */ |
| 5104 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5105 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5106 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5107 | /* Execute the Lua code. */ |
| 5108 | status = hlua_ctx_resume(hlua, 1); |
| 5109 | |
| 5110 | switch (status) { |
| 5111 | /* finished or yield */ |
| 5112 | case HLUA_E_OK: |
| 5113 | hlua_ctx_destroy(hlua); |
| 5114 | task_delete(task); |
| 5115 | task_free(task); |
| 5116 | break; |
| 5117 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5118 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
| 5119 | if (hlua->wake_time != TICK_ETERNITY) |
| 5120 | task_schedule(task, hlua->wake_time); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5121 | break; |
| 5122 | |
| 5123 | /* finished with error. */ |
| 5124 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5125 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5126 | hlua_ctx_destroy(hlua); |
| 5127 | task_delete(task); |
| 5128 | task_free(task); |
| 5129 | break; |
| 5130 | |
| 5131 | case HLUA_E_ERR: |
| 5132 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5133 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5134 | hlua_ctx_destroy(hlua); |
| 5135 | task_delete(task); |
| 5136 | task_free(task); |
| 5137 | break; |
| 5138 | } |
| 5139 | return NULL; |
| 5140 | } |
| 5141 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5142 | /* This function is an LUA binding that register LUA function to be |
| 5143 | * executed after the HAProxy configuration parsing and before the |
| 5144 | * HAProxy scheduler starts. This function expect only one LUA |
| 5145 | * argument that is a function. This function returns nothing, but |
| 5146 | * throws if an error is encountered. |
| 5147 | */ |
| 5148 | __LJMP static int hlua_register_init(lua_State *L) |
| 5149 | { |
| 5150 | struct hlua_init_function *init; |
| 5151 | int ref; |
| 5152 | |
| 5153 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5154 | |
| 5155 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5156 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5157 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5158 | if (!init) |
| 5159 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5160 | |
| 5161 | init->function_ref = ref; |
| 5162 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5163 | return 0; |
| 5164 | } |
| 5165 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5166 | /* This functio is an LUA binding. It permits to register a task |
| 5167 | * executed in parallel of the main HAroxy activity. The task is |
| 5168 | * created and it is set in the HAProxy scheduler. It can be called |
| 5169 | * from the "init" section, "post init" or during the runtime. |
| 5170 | * |
| 5171 | * Lua prototype: |
| 5172 | * |
| 5173 | * <none> core.register_task(<function>) |
| 5174 | */ |
| 5175 | static int hlua_register_task(lua_State *L) |
| 5176 | { |
| 5177 | struct hlua *hlua; |
| 5178 | struct task *task; |
| 5179 | int ref; |
| 5180 | |
| 5181 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5182 | |
| 5183 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5184 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5185 | hlua = calloc(1, sizeof(*hlua)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5186 | if (!hlua) |
| 5187 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5188 | |
| 5189 | task = task_new(); |
| 5190 | task->context = hlua; |
| 5191 | task->process = hlua_process_task; |
| 5192 | |
| 5193 | if (!hlua_ctx_init(hlua, task)) |
| 5194 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5195 | |
| 5196 | /* Restore the function in the stack. */ |
| 5197 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5198 | hlua->nargs = 0; |
| 5199 | |
| 5200 | /* Schedule task. */ |
| 5201 | task_schedule(task, now_ms); |
| 5202 | |
| 5203 | return 0; |
| 5204 | } |
| 5205 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5206 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5207 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5208 | * resume converters. |
| 5209 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5210 | 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] | 5211 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5212 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5213 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5214 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5215 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5216 | if (!stream) |
| 5217 | return 0; |
| 5218 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5219 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5220 | * Lua context can be not initialized. This behavior |
| 5221 | * permits to save performances because a systematic |
| 5222 | * Lua initialization cause 5% performances loss. |
| 5223 | */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5224 | if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5225 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5226 | return 0; |
| 5227 | } |
| 5228 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5229 | /* If it is the first run, initialize the data for the call. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5230 | if (!HLUA_IS_RUNNING(&stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5231 | |
| 5232 | /* The following Lua calls can fail. */ |
| 5233 | if (!SET_SAFE_LJMP(stream->hlua.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5234 | if (lua_type(stream->hlua.T, -1) == LUA_TSTRING) |
| 5235 | error = lua_tostring(stream->hlua.T, -1); |
| 5236 | else |
| 5237 | error = "critical error"; |
| 5238 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5239 | return 0; |
| 5240 | } |
| 5241 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5242 | /* Check stack available size. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5243 | if (!lua_checkstack(stream->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5244 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5245 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5246 | return 0; |
| 5247 | } |
| 5248 | |
| 5249 | /* Restore the function in the stack. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5250 | lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5251 | |
| 5252 | /* convert input sample and pust-it in the stack. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5253 | if (!lua_checkstack(stream->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5254 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5255 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5256 | return 0; |
| 5257 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5258 | hlua_smp2lua(stream->hlua.T, smp); |
Thierry Fournier | 4a53bfd | 2016-05-27 16:35:01 +0200 | [diff] [blame] | 5259 | stream->hlua.nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5260 | |
| 5261 | /* push keywords in the stack. */ |
| 5262 | if (arg_p) { |
| 5263 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5264 | if (!lua_checkstack(stream->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5265 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5266 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5267 | return 0; |
| 5268 | } |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5269 | hlua_arg2lua(stream->hlua.T, arg_p); |
| 5270 | stream->hlua.nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5271 | } |
| 5272 | } |
| 5273 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5274 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5275 | stream->hlua.max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5276 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5277 | /* At this point the execution is safe. */ |
| 5278 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5279 | } |
| 5280 | |
| 5281 | /* Execute the function. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5282 | switch (hlua_ctx_resume(&stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5283 | /* finished. */ |
| 5284 | case HLUA_E_OK: |
| 5285 | /* Convert the returned value in sample. */ |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5286 | hlua_lua2smp(stream->hlua.T, -1, smp); |
| 5287 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5288 | return 1; |
| 5289 | |
| 5290 | /* yield. */ |
| 5291 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5292 | 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] | 5293 | return 0; |
| 5294 | |
| 5295 | /* finished with error. */ |
| 5296 | case HLUA_E_ERRMSG: |
| 5297 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5298 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
| 5299 | fcn->name, lua_tostring(stream->hlua.T, -1)); |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5300 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5301 | return 0; |
| 5302 | |
| 5303 | case HLUA_E_ERR: |
| 5304 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5305 | 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] | 5306 | |
| 5307 | default: |
| 5308 | return 0; |
| 5309 | } |
| 5310 | } |
| 5311 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5312 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5313 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5314 | * resume sample-fetches. This function will be called by the sample |
| 5315 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5316 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5317 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5318 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5319 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5320 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5321 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5322 | const char *error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5323 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5324 | if (!stream) |
| 5325 | return 0; |
| 5326 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5327 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5328 | * Lua context can be not initialized. This behavior |
| 5329 | * permits to save performances because a systematic |
| 5330 | * Lua initialization cause 5% performances loss. |
| 5331 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5332 | if (!stream->hlua.T && !hlua_ctx_init(&stream->hlua, stream->task)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5333 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5334 | return 0; |
| 5335 | } |
| 5336 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5337 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5338 | if (!HLUA_IS_RUNNING(&stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5339 | |
| 5340 | /* The following Lua calls can fail. */ |
| 5341 | if (!SET_SAFE_LJMP(stream->hlua.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5342 | if (lua_type(stream->hlua.T, -1) == LUA_TSTRING) |
| 5343 | error = lua_tostring(stream->hlua.T, -1); |
| 5344 | else |
| 5345 | error = "critical error"; |
| 5346 | 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] | 5347 | return 0; |
| 5348 | } |
| 5349 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5350 | /* Check stack available size. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5351 | if (!lua_checkstack(stream->hlua.T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5352 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5353 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5354 | return 0; |
| 5355 | } |
| 5356 | |
| 5357 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5358 | lua_rawgeti(stream->hlua.T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5359 | |
| 5360 | /* push arguments in the stack. */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5361 | if (!hlua_txn_new(stream->hlua.T, stream, smp->px, smp->opt & SMP_OPT_DIR, |
| 5362 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5363 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5364 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5365 | return 0; |
| 5366 | } |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5367 | stream->hlua.nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5368 | |
| 5369 | /* push keywords in the stack. */ |
| 5370 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5371 | /* Check stack available size. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5372 | if (!lua_checkstack(stream->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5373 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5374 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5375 | return 0; |
| 5376 | } |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5377 | hlua_arg2lua(stream->hlua.T, arg_p); |
| 5378 | stream->hlua.nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5379 | } |
| 5380 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5381 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5382 | stream->hlua.max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5383 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5384 | /* At this point the execution is safe. */ |
| 5385 | RESET_SAFE_LJMP(stream->hlua.T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5386 | } |
| 5387 | |
| 5388 | /* Execute the function. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5389 | switch (hlua_ctx_resume(&stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5390 | /* finished. */ |
| 5391 | case HLUA_E_OK: |
Thierry FOURNIER | 26a7aac | 2015-10-13 14:25:11 +0200 | [diff] [blame] | 5392 | if (!hlua_check_proto(stream, (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5393 | return 0; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5394 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5395 | hlua_lua2smp(stream->hlua.T, -1, smp); |
| 5396 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5397 | |
| 5398 | /* Set the end of execution flag. */ |
| 5399 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5400 | return 1; |
| 5401 | |
| 5402 | /* yield. */ |
| 5403 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 26a7aac | 2015-10-13 14:25:11 +0200 | [diff] [blame] | 5404 | hlua_check_proto(stream, (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5405 | 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] | 5406 | return 0; |
| 5407 | |
| 5408 | /* finished with error. */ |
| 5409 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 26a7aac | 2015-10-13 14:25:11 +0200 | [diff] [blame] | 5410 | hlua_check_proto(stream, (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5411 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5412 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
| 5413 | fcn->name, lua_tostring(stream->hlua.T, -1)); |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5414 | lua_pop(stream->hlua.T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5415 | return 0; |
| 5416 | |
| 5417 | case HLUA_E_ERR: |
Thierry FOURNIER | 26a7aac | 2015-10-13 14:25:11 +0200 | [diff] [blame] | 5418 | hlua_check_proto(stream, (smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_RES); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5419 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5420 | 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] | 5421 | |
| 5422 | default: |
| 5423 | return 0; |
| 5424 | } |
| 5425 | } |
| 5426 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5427 | /* This function is an LUA binding used for registering |
| 5428 | * "sample-conv" functions. It expects a converter name used |
| 5429 | * in the haproxy configuration file, and an LUA function. |
| 5430 | */ |
| 5431 | __LJMP static int hlua_register_converters(lua_State *L) |
| 5432 | { |
| 5433 | struct sample_conv_kw_list *sck; |
| 5434 | const char *name; |
| 5435 | int ref; |
| 5436 | int len; |
| 5437 | struct hlua_function *fcn; |
| 5438 | |
| 5439 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 5440 | |
| 5441 | /* First argument : converter name. */ |
| 5442 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5443 | |
| 5444 | /* Second argument : lua function. */ |
| 5445 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5446 | |
| 5447 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5448 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5449 | if (!sck) |
| 5450 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5451 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5452 | if (!fcn) |
| 5453 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5454 | |
| 5455 | /* Fill fcn. */ |
| 5456 | fcn->name = strdup(name); |
| 5457 | if (!fcn->name) |
| 5458 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5459 | fcn->function_ref = ref; |
| 5460 | |
| 5461 | /* List head */ |
| 5462 | sck->list.n = sck->list.p = NULL; |
| 5463 | |
| 5464 | /* converter keyword. */ |
| 5465 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5466 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5467 | if (!sck->kw[0].kw) |
| 5468 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5469 | |
| 5470 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 5471 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5472 | 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] | 5473 | sck->kw[0].val_args = NULL; |
| 5474 | sck->kw[0].in_type = SMP_T_STR; |
| 5475 | sck->kw[0].out_type = SMP_T_STR; |
| 5476 | sck->kw[0].private = fcn; |
| 5477 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5478 | /* Register this new converter */ |
| 5479 | sample_register_convs(sck); |
| 5480 | |
| 5481 | return 0; |
| 5482 | } |
| 5483 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5484 | /* This fucntion is an LUA binding used for registering |
| 5485 | * "sample-fetch" functions. It expects a converter name used |
| 5486 | * in the haproxy configuration file, and an LUA function. |
| 5487 | */ |
| 5488 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 5489 | { |
| 5490 | const char *name; |
| 5491 | int ref; |
| 5492 | int len; |
| 5493 | struct sample_fetch_kw_list *sfk; |
| 5494 | struct hlua_function *fcn; |
| 5495 | |
| 5496 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 5497 | |
| 5498 | /* First argument : sample-fetch name. */ |
| 5499 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5500 | |
| 5501 | /* Second argument : lua function. */ |
| 5502 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5503 | |
| 5504 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5505 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5506 | if (!sfk) |
| 5507 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5508 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5509 | if (!fcn) |
| 5510 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5511 | |
| 5512 | /* Fill fcn. */ |
| 5513 | fcn->name = strdup(name); |
| 5514 | if (!fcn->name) |
| 5515 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 5516 | fcn->function_ref = ref; |
| 5517 | |
| 5518 | /* List head */ |
| 5519 | sfk->list.n = sfk->list.p = NULL; |
| 5520 | |
| 5521 | /* sample-fetch keyword. */ |
| 5522 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5523 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5524 | if (!sfk->kw[0].kw) |
| 5525 | return luaL_error(L, "lua out of memory error."); |
| 5526 | |
| 5527 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 5528 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5529 | 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] | 5530 | sfk->kw[0].val_args = NULL; |
| 5531 | sfk->kw[0].out_type = SMP_T_STR; |
| 5532 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 5533 | sfk->kw[0].val = 0; |
| 5534 | sfk->kw[0].private = fcn; |
| 5535 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5536 | /* Register this new fetch. */ |
| 5537 | sample_register_fetches(sfk); |
| 5538 | |
| 5539 | return 0; |
| 5540 | } |
| 5541 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5542 | /* This function is a wrapper to execute each LUA function declared |
| 5543 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5544 | * return ACT_RET_CONT if the processing is finished (with or without |
| 5545 | * error) and return ACT_RET_YIELD if the function must be called again |
| 5546 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5547 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5548 | 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] | 5549 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5550 | { |
| 5551 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5552 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5553 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5554 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5555 | |
| 5556 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 5557 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 5558 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 5559 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 5560 | 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] | 5561 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5562 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5563 | return ACT_RET_CONT; |
| 5564 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5565 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5566 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5567 | * Lua context can be not initialized. This behavior |
| 5568 | * permits to save performances because a systematic |
| 5569 | * Lua initialization cause 5% performances loss. |
| 5570 | */ |
| 5571 | if (!s->hlua.T && !hlua_ctx_init(&s->hlua, s->task)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5572 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5573 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5574 | return ACT_RET_CONT; |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5575 | } |
| 5576 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5577 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 5578 | if (!HLUA_IS_RUNNING(&s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5579 | |
| 5580 | /* The following Lua calls can fail. */ |
| 5581 | if (!SET_SAFE_LJMP(s->hlua.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5582 | if (lua_type(s->hlua.T, -1) == LUA_TSTRING) |
| 5583 | error = lua_tostring(s->hlua.T, -1); |
| 5584 | else |
| 5585 | error = "critical error"; |
| 5586 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 5587 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5588 | return ACT_RET_CONT; |
| 5589 | } |
| 5590 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5591 | /* Check stack available size. */ |
| 5592 | if (!lua_checkstack(s->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5593 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5594 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5595 | RESET_SAFE_LJMP(s->hlua.T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5596 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5597 | } |
| 5598 | |
| 5599 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5600 | 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] | 5601 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5602 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5603 | if (!hlua_txn_new(s->hlua.T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5604 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5605 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5606 | RESET_SAFE_LJMP(s->hlua.T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5607 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5608 | } |
| 5609 | s->hlua.nargs = 1; |
| 5610 | |
| 5611 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5612 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5613 | if (!lua_checkstack(s->hlua.T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5614 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5615 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 10e5bc7 | 2015-09-25 23:51:34 +0200 | [diff] [blame] | 5616 | RESET_SAFE_LJMP(s->hlua.T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5617 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5618 | } |
| 5619 | lua_pushstring(s->hlua.T, *arg); |
| 5620 | s->hlua.nargs++; |
| 5621 | } |
| 5622 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5623 | /* Now the execution is safe. */ |
| 5624 | RESET_SAFE_LJMP(s->hlua.T); |
| 5625 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5626 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5627 | s->hlua.max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5628 | } |
| 5629 | |
| 5630 | /* Execute the function. */ |
Willy Tarreau | 528192d | 2015-09-27 10:48:01 +0200 | [diff] [blame] | 5631 | switch (hlua_ctx_resume(&s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5632 | /* finished. */ |
| 5633 | case HLUA_E_OK: |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5634 | if (!hlua_check_proto(s, dir)) |
| 5635 | return ACT_RET_ERR; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5636 | if (s->hlua.flags & HLUA_STOP) |
| 5637 | return ACT_RET_STOP; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5638 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5639 | |
| 5640 | /* yield. */ |
| 5641 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5642 | /* Set timeout in the required channel. */ |
| 5643 | if (s->hlua.wake_time != TICK_ETERNITY) { |
| 5644 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5645 | s->req.analyse_exp = s->hlua.wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5646 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5647 | s->res.analyse_exp = s->hlua.wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5648 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5649 | /* Some actions can be wake up when a "write" event |
| 5650 | * is detected on a response channel. This is useful |
| 5651 | * only for actions targetted on the requests. |
| 5652 | */ |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 5653 | if (HLUA_IS_WAKERESWR(&s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5654 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 5655 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5656 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5657 | } |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 5658 | if (HLUA_IS_WAKEREQWR(&s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 5659 | s->req.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5660 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5661 | |
| 5662 | /* finished with error. */ |
| 5663 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5664 | if (!hlua_check_proto(s, dir)) |
| 5665 | return ACT_RET_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5666 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5667 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5668 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua.T, -1)); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5669 | lua_pop(s->hlua.T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5670 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5671 | |
| 5672 | case HLUA_E_ERR: |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5673 | if (!hlua_check_proto(s, dir)) |
| 5674 | return ACT_RET_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5675 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5676 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 5677 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5678 | |
| 5679 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 5680 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 5681 | } |
| 5682 | } |
| 5683 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5684 | struct task *hlua_applet_wakeup(struct task *t) |
| 5685 | { |
| 5686 | struct appctx *ctx = t->context; |
| 5687 | struct stream_interface *si = ctx->owner; |
| 5688 | |
| 5689 | /* If the applet is wake up without any expected work, the sheduler |
| 5690 | * remove it from the run queue. This flag indicate that the applet |
| 5691 | * is waiting for write. If the buffer is full, the main processing |
| 5692 | * will send some data and after call the applet, otherwise it call |
| 5693 | * the applet ASAP. |
| 5694 | */ |
| 5695 | si_applet_cant_put(si); |
| 5696 | appctx_wakeup(ctx); |
| 5697 | return NULL; |
| 5698 | } |
| 5699 | |
| 5700 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 5701 | { |
| 5702 | struct stream_interface *si = ctx->owner; |
| 5703 | struct hlua *hlua = &ctx->ctx.hlua_apptcp.hlua; |
| 5704 | struct task *task; |
| 5705 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5706 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5707 | |
| 5708 | HLUA_INIT(hlua); |
| 5709 | ctx->ctx.hlua_apptcp.flags = 0; |
| 5710 | |
| 5711 | /* Create task used by signal to wakeup applets. */ |
| 5712 | task = task_new(); |
| 5713 | if (!task) { |
| 5714 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 5715 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5716 | return 0; |
| 5717 | } |
| 5718 | task->nice = 0; |
| 5719 | task->context = ctx; |
| 5720 | task->process = hlua_applet_wakeup; |
| 5721 | ctx->ctx.hlua_apptcp.task = task; |
| 5722 | |
| 5723 | /* In the execution wrappers linked with a stream, the |
| 5724 | * Lua context can be not initialized. This behavior |
| 5725 | * permits to save performances because a systematic |
| 5726 | * Lua initialization cause 5% performances loss. |
| 5727 | */ |
| 5728 | if (!hlua_ctx_init(hlua, task)) { |
| 5729 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 5730 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5731 | return 0; |
| 5732 | } |
| 5733 | |
| 5734 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5735 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5736 | |
| 5737 | /* The following Lua calls can fail. */ |
| 5738 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5739 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 5740 | error = lua_tostring(hlua->T, -1); |
| 5741 | else |
| 5742 | error = "critical error"; |
| 5743 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 5744 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5745 | RESET_SAFE_LJMP(hlua->T); |
| 5746 | return 0; |
| 5747 | } |
| 5748 | |
| 5749 | /* Check stack available size. */ |
| 5750 | if (!lua_checkstack(hlua->T, 1)) { |
| 5751 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 5752 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5753 | RESET_SAFE_LJMP(hlua->T); |
| 5754 | return 0; |
| 5755 | } |
| 5756 | |
| 5757 | /* Restore the function in the stack. */ |
| 5758 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 5759 | |
| 5760 | /* Create and and push object stream in the stack. */ |
| 5761 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 5762 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 5763 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5764 | RESET_SAFE_LJMP(hlua->T); |
| 5765 | return 0; |
| 5766 | } |
| 5767 | hlua->nargs = 1; |
| 5768 | |
| 5769 | /* push keywords in the stack. */ |
| 5770 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 5771 | if (!lua_checkstack(hlua->T, 1)) { |
| 5772 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 5773 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5774 | RESET_SAFE_LJMP(hlua->T); |
| 5775 | return 0; |
| 5776 | } |
| 5777 | lua_pushstring(hlua->T, *arg); |
| 5778 | hlua->nargs++; |
| 5779 | } |
| 5780 | |
| 5781 | RESET_SAFE_LJMP(hlua->T); |
| 5782 | |
| 5783 | /* Wakeup the applet ASAP. */ |
| 5784 | si_applet_cant_get(si); |
| 5785 | si_applet_cant_put(si); |
| 5786 | |
| 5787 | return 1; |
| 5788 | } |
| 5789 | |
| 5790 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 5791 | { |
| 5792 | struct stream_interface *si = ctx->owner; |
| 5793 | struct stream *strm = si_strm(si); |
| 5794 | struct channel *res = si_ic(si); |
| 5795 | struct act_rule *rule = ctx->rule; |
| 5796 | struct proxy *px = strm->be; |
| 5797 | struct hlua *hlua = &ctx->ctx.hlua_apptcp.hlua; |
| 5798 | |
| 5799 | /* The applet execution is already done. */ |
| 5800 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) |
| 5801 | return; |
| 5802 | |
| 5803 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 5804 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 5805 | return; |
| 5806 | |
| 5807 | /* Execute the function. */ |
| 5808 | switch (hlua_ctx_resume(hlua, 1)) { |
| 5809 | /* finished. */ |
| 5810 | case HLUA_E_OK: |
| 5811 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 5812 | |
| 5813 | /* log time */ |
| 5814 | strm->logs.tv_request = now; |
| 5815 | |
| 5816 | /* eat the whole request */ |
| 5817 | bo_skip(si_oc(si), si_ob(si)->o); |
| 5818 | res->flags |= CF_READ_NULL; |
| 5819 | si_shutr(si); |
| 5820 | return; |
| 5821 | |
| 5822 | /* yield. */ |
| 5823 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 5824 | if (hlua->wake_time != TICK_ETERNITY) |
| 5825 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 5826 | return; |
| 5827 | |
| 5828 | /* finished with error. */ |
| 5829 | case HLUA_E_ERRMSG: |
| 5830 | /* Display log. */ |
| 5831 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 5832 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 5833 | lua_pop(hlua->T, 1); |
| 5834 | goto error; |
| 5835 | |
| 5836 | case HLUA_E_ERR: |
| 5837 | /* Display log. */ |
| 5838 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 5839 | rule->arg.hlua_rule->fcn.name); |
| 5840 | goto error; |
| 5841 | |
| 5842 | default: |
| 5843 | goto error; |
| 5844 | } |
| 5845 | |
| 5846 | error: |
| 5847 | |
| 5848 | /* For all other cases, just close the stream. */ |
| 5849 | si_shutw(si); |
| 5850 | si_shutr(si); |
| 5851 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 5852 | } |
| 5853 | |
| 5854 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 5855 | { |
| 5856 | task_free(ctx->ctx.hlua_apptcp.task); |
| 5857 | ctx->ctx.hlua_apptcp.task = NULL; |
| 5858 | hlua_ctx_destroy(&ctx->ctx.hlua_apptcp.hlua); |
| 5859 | } |
| 5860 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5861 | /* The function returns 1 if the initialisation is complete, 0 if |
| 5862 | * an errors occurs and -1 if more data are required for initializing |
| 5863 | * the applet. |
| 5864 | */ |
| 5865 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 5866 | { |
| 5867 | struct stream_interface *si = ctx->owner; |
| 5868 | struct channel *req = si_oc(si); |
| 5869 | struct http_msg *msg; |
| 5870 | struct http_txn *txn; |
| 5871 | struct hlua *hlua = &ctx->ctx.hlua_apphttp.hlua; |
| 5872 | char **arg; |
| 5873 | struct hdr_ctx hdr; |
| 5874 | struct task *task; |
| 5875 | 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] | 5876 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5877 | |
| 5878 | /* Wait for a full HTTP request. */ |
| 5879 | if (!smp_prefetch_http(px, strm, 0, NULL, &smp, 0)) { |
| 5880 | if (smp.flags & SMP_F_MAY_CHANGE) |
| 5881 | return -1; |
| 5882 | return 0; |
| 5883 | } |
| 5884 | txn = strm->txn; |
| 5885 | msg = &txn->req; |
| 5886 | |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 5887 | /* We want two things in HTTP mode : |
| 5888 | * - enforce server-close mode if we were in keep-alive, so that the |
| 5889 | * applet is released after each response ; |
| 5890 | * - enable request body transfer to the applet in order to resync |
| 5891 | * with the response body. |
| 5892 | */ |
| 5893 | if ((txn->flags & TX_CON_WANT_MSK) == TX_CON_WANT_KAL) |
| 5894 | txn->flags = (txn->flags & ~TX_CON_WANT_MSK) | TX_CON_WANT_SCL; |
Willy Tarreau | 0078bfc | 2015-10-07 20:20:28 +0200 | [diff] [blame] | 5895 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5896 | HLUA_INIT(hlua); |
| 5897 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 5898 | ctx->ctx.hlua_apphttp.flags = 0; |
| 5899 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 5900 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 5901 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 5902 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5903 | /* Create task used by signal to wakeup applets. */ |
| 5904 | task = task_new(); |
| 5905 | if (!task) { |
| 5906 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 5907 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5908 | return 0; |
| 5909 | } |
| 5910 | task->nice = 0; |
| 5911 | task->context = ctx; |
| 5912 | task->process = hlua_applet_wakeup; |
| 5913 | ctx->ctx.hlua_apphttp.task = task; |
| 5914 | |
| 5915 | /* In the execution wrappers linked with a stream, the |
| 5916 | * Lua context can be not initialized. This behavior |
| 5917 | * permits to save performances because a systematic |
| 5918 | * Lua initialization cause 5% performances loss. |
| 5919 | */ |
| 5920 | if (!hlua_ctx_init(hlua, task)) { |
| 5921 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 5922 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5923 | return 0; |
| 5924 | } |
| 5925 | |
| 5926 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5927 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5928 | |
| 5929 | /* The following Lua calls can fail. */ |
| 5930 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5931 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 5932 | error = lua_tostring(hlua->T, -1); |
| 5933 | else |
| 5934 | error = "critical error"; |
| 5935 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 5936 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5937 | return 0; |
| 5938 | } |
| 5939 | |
| 5940 | /* Check stack available size. */ |
| 5941 | if (!lua_checkstack(hlua->T, 1)) { |
| 5942 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 5943 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5944 | RESET_SAFE_LJMP(hlua->T); |
| 5945 | return 0; |
| 5946 | } |
| 5947 | |
| 5948 | /* Restore the function in the stack. */ |
| 5949 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 5950 | |
| 5951 | /* Create and and push object stream in the stack. */ |
| 5952 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 5953 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 5954 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5955 | RESET_SAFE_LJMP(hlua->T); |
| 5956 | return 0; |
| 5957 | } |
| 5958 | hlua->nargs = 1; |
| 5959 | |
| 5960 | /* Look for a 100-continue expected. */ |
| 5961 | if (msg->flags & HTTP_MSGF_VER_11) { |
| 5962 | hdr.idx = 0; |
| 5963 | if (http_find_header2("Expect", 6, req->buf->p, &txn->hdr_idx, &hdr) && |
| 5964 | unlikely(hdr.vlen == 12 && strncasecmp(hdr.line+hdr.val, "100-continue", 12) == 0)) |
| 5965 | ctx->ctx.hlua_apphttp.flags |= APPLET_100C; |
| 5966 | } |
| 5967 | |
| 5968 | /* push keywords in the stack. */ |
| 5969 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 5970 | if (!lua_checkstack(hlua->T, 1)) { |
| 5971 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 5972 | ctx->rule->arg.hlua_rule->fcn.name); |
| 5973 | RESET_SAFE_LJMP(hlua->T); |
| 5974 | return 0; |
| 5975 | } |
| 5976 | lua_pushstring(hlua->T, *arg); |
| 5977 | hlua->nargs++; |
| 5978 | } |
| 5979 | |
| 5980 | RESET_SAFE_LJMP(hlua->T); |
| 5981 | |
| 5982 | /* Wakeup the applet when data is ready for read. */ |
| 5983 | si_applet_cant_get(si); |
| 5984 | |
| 5985 | return 1; |
| 5986 | } |
| 5987 | |
| 5988 | static void hlua_applet_http_fct(struct appctx *ctx) |
| 5989 | { |
| 5990 | struct stream_interface *si = ctx->owner; |
| 5991 | struct stream *strm = si_strm(si); |
| 5992 | struct channel *res = si_ic(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5993 | struct act_rule *rule = ctx->rule; |
| 5994 | struct proxy *px = strm->be; |
| 5995 | struct hlua *hlua = &ctx->ctx.hlua_apphttp.hlua; |
| 5996 | char *blk1; |
| 5997 | int len1; |
| 5998 | char *blk2; |
| 5999 | int len2; |
| 6000 | int ret; |
| 6001 | |
| 6002 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6003 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6004 | return; |
| 6005 | |
| 6006 | /* Set the currently running flag. */ |
| 6007 | if (!HLUA_IS_RUNNING(hlua) && |
| 6008 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6009 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6010 | /* Wait for full HTTP analysys. */ |
| 6011 | if (unlikely(strm->txn->req.msg_state < HTTP_MSG_BODY)) { |
| 6012 | si_applet_cant_get(si); |
| 6013 | return; |
| 6014 | } |
| 6015 | |
| 6016 | /* Store the max amount of bytes that we can read. */ |
| 6017 | ctx->ctx.hlua_apphttp.left_bytes = strm->txn->req.body_len; |
| 6018 | |
| 6019 | /* We need to flush the request header. This left the body |
| 6020 | * for the Lua. |
| 6021 | */ |
| 6022 | |
| 6023 | /* Read the maximum amount of data avalaible. */ |
| 6024 | ret = bo_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
| 6025 | if (ret == -1) |
| 6026 | return; |
| 6027 | |
| 6028 | /* No data available, ask for more data. */ |
| 6029 | if (ret == 1) |
| 6030 | len2 = 0; |
| 6031 | if (ret == 0) |
| 6032 | len1 = 0; |
| 6033 | if (len1 + len2 < strm->txn->req.eoh + 2) { |
| 6034 | si_applet_cant_get(si); |
| 6035 | return; |
| 6036 | } |
| 6037 | |
| 6038 | /* skip the requests bytes. */ |
| 6039 | bo_skip(si_oc(si), strm->txn->req.eoh + 2); |
| 6040 | } |
| 6041 | |
| 6042 | /* Executes The applet if it is not done. */ |
| 6043 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6044 | |
| 6045 | /* Execute the function. */ |
| 6046 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6047 | /* finished. */ |
| 6048 | case HLUA_E_OK: |
| 6049 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6050 | break; |
| 6051 | |
| 6052 | /* yield. */ |
| 6053 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6054 | if (hlua->wake_time != TICK_ETERNITY) |
| 6055 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6056 | return; |
| 6057 | |
| 6058 | /* finished with error. */ |
| 6059 | case HLUA_E_ERRMSG: |
| 6060 | /* Display log. */ |
| 6061 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6062 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6063 | lua_pop(hlua->T, 1); |
| 6064 | goto error; |
| 6065 | |
| 6066 | case HLUA_E_ERR: |
| 6067 | /* Display log. */ |
| 6068 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6069 | rule->arg.hlua_rule->fcn.name); |
| 6070 | goto error; |
| 6071 | |
| 6072 | default: |
| 6073 | goto error; |
| 6074 | } |
| 6075 | } |
| 6076 | |
| 6077 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
| 6078 | |
| 6079 | /* We must send the final chunk. */ |
| 6080 | if (ctx->ctx.hlua_apphttp.flags & APPLET_CHUNKED && |
| 6081 | !(ctx->ctx.hlua_apphttp.flags & APPLET_LAST_CHK)) { |
| 6082 | |
| 6083 | /* sent last chunk at once. */ |
| 6084 | ret = bi_putblk(res, "0\r\n\r\n", 5); |
| 6085 | |
| 6086 | /* critical error. */ |
| 6087 | if (ret == -2 || ret == -3) { |
| 6088 | SEND_ERR(px, "Lua applet http '%s'cannont send last chunk.\n", |
| 6089 | rule->arg.hlua_rule->fcn.name); |
| 6090 | goto error; |
| 6091 | } |
| 6092 | |
| 6093 | /* no enough space error. */ |
| 6094 | if (ret == -1) { |
| 6095 | si_applet_cant_put(si); |
| 6096 | return; |
| 6097 | } |
| 6098 | |
| 6099 | /* set the last chunk sent. */ |
| 6100 | ctx->ctx.hlua_apphttp.flags |= APPLET_LAST_CHK; |
| 6101 | } |
| 6102 | |
| 6103 | /* close the connection. */ |
| 6104 | |
| 6105 | /* status / log */ |
| 6106 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6107 | strm->logs.tv_request = now; |
| 6108 | |
| 6109 | /* eat the whole request */ |
| 6110 | bo_skip(si_oc(si), si_ob(si)->o); |
| 6111 | res->flags |= CF_READ_NULL; |
| 6112 | si_shutr(si); |
| 6113 | |
| 6114 | return; |
| 6115 | } |
| 6116 | |
| 6117 | error: |
| 6118 | |
| 6119 | /* If we are in HTTP mode, and we are not send any |
| 6120 | * data, return a 500 server error in best effort: |
| 6121 | * if there are no room avalaible in the buffer, |
| 6122 | * just close the connection. |
| 6123 | */ |
| 6124 | bi_putblk(res, error_500, strlen(error_500)); |
| 6125 | if (!(strm->flags & SF_ERR_MASK)) |
| 6126 | strm->flags |= SF_ERR_RESOURCE; |
| 6127 | si_shutw(si); |
| 6128 | si_shutr(si); |
| 6129 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6130 | } |
| 6131 | |
| 6132 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6133 | { |
| 6134 | task_free(ctx->ctx.hlua_apphttp.task); |
| 6135 | ctx->ctx.hlua_apphttp.task = NULL; |
| 6136 | hlua_ctx_destroy(&ctx->ctx.hlua_apphttp.hlua); |
| 6137 | } |
| 6138 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6139 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6140 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6141 | * |
| 6142 | * This function can fail with an abort() due to an Lua critical error. |
| 6143 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6144 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6145 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6146 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6147 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6148 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6149 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6150 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6151 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6152 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6153 | if (!rule->arg.hlua_rule) { |
| 6154 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6155 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6156 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6157 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6158 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6159 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6160 | |
| 6161 | /* TODO: later accept arguments. */ |
| 6162 | rule->arg.hlua_rule->args = NULL; |
| 6163 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6164 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6165 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6166 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6167 | } |
| 6168 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6169 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6170 | struct act_rule *rule, char **err) |
| 6171 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6172 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6173 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6174 | /* HTTP applets are forbidden in tcp-request rules. |
| 6175 | * HTTP applet request requires everything initilized by |
| 6176 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6177 | * The applet will be immediately initilized, but its before |
| 6178 | * the call of this analyzer. |
| 6179 | */ |
| 6180 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6181 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6182 | return ACT_RET_PRS_ERR; |
| 6183 | } |
| 6184 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6185 | /* Memory for the rule. */ |
| 6186 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6187 | if (!rule->arg.hlua_rule) { |
| 6188 | memprintf(err, "out of memory error"); |
| 6189 | return ACT_RET_PRS_ERR; |
| 6190 | } |
| 6191 | |
| 6192 | /* Reference the Lua function and store the reference. */ |
| 6193 | rule->arg.hlua_rule->fcn = *fcn; |
| 6194 | |
| 6195 | /* TODO: later accept arguments. */ |
| 6196 | rule->arg.hlua_rule->args = NULL; |
| 6197 | |
| 6198 | /* Add applet pointer in the rule. */ |
| 6199 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6200 | rule->applet.name = fcn->name; |
| 6201 | rule->applet.init = hlua_applet_http_init; |
| 6202 | rule->applet.fct = hlua_applet_http_fct; |
| 6203 | rule->applet.release = hlua_applet_http_release; |
| 6204 | rule->applet.timeout = hlua_timeout_applet; |
| 6205 | |
| 6206 | return ACT_RET_PRS_OK; |
| 6207 | } |
| 6208 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6209 | /* This function is an LUA binding used for registering |
| 6210 | * "sample-conv" functions. It expects a converter name used |
| 6211 | * in the haproxy configuration file, and an LUA function. |
| 6212 | */ |
| 6213 | __LJMP static int hlua_register_action(lua_State *L) |
| 6214 | { |
| 6215 | struct action_kw_list *akl; |
| 6216 | const char *name; |
| 6217 | int ref; |
| 6218 | int len; |
| 6219 | struct hlua_function *fcn; |
| 6220 | |
Thierry FOURNIER | ed0bdaa | 2015-12-20 19:51:06 +0100 | [diff] [blame] | 6221 | MAY_LJMP(check_args(L, 3, "register_action")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6222 | |
| 6223 | /* First argument : converter name. */ |
| 6224 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6225 | |
| 6226 | /* Second argument : environment. */ |
| 6227 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6228 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6229 | |
| 6230 | /* Third argument : lua function. */ |
| 6231 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6232 | |
| 6233 | /* browse the second argulent as an array. */ |
| 6234 | lua_pushnil(L); |
| 6235 | while (lua_next(L, 2) != 0) { |
| 6236 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6237 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6238 | |
| 6239 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6240 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6241 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6242 | if (!akl) |
| 6243 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6244 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6245 | if (!fcn) |
| 6246 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6247 | |
| 6248 | /* Fill fcn. */ |
| 6249 | fcn->name = strdup(name); |
| 6250 | if (!fcn->name) |
| 6251 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6252 | fcn->function_ref = ref; |
| 6253 | |
| 6254 | /* List head */ |
| 6255 | akl->list.n = akl->list.p = NULL; |
| 6256 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6257 | /* action keyword. */ |
| 6258 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6259 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6260 | if (!akl->kw[0].kw) |
| 6261 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6262 | |
| 6263 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6264 | |
| 6265 | akl->kw[0].match_pfx = 0; |
| 6266 | akl->kw[0].private = fcn; |
| 6267 | akl->kw[0].parse = action_register_lua; |
| 6268 | |
| 6269 | /* select the action registering point. */ |
| 6270 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6271 | tcp_req_cont_keywords_register(akl); |
| 6272 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6273 | tcp_res_cont_keywords_register(akl); |
| 6274 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6275 | http_req_keywords_register(akl); |
| 6276 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6277 | http_res_keywords_register(akl); |
| 6278 | else |
| 6279 | WILL_LJMP(luaL_error(L, "lua action environment '%s' is unknown. " |
| 6280 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6281 | "are expected.", lua_tostring(L, -1))); |
| 6282 | |
| 6283 | /* pop the environment string. */ |
| 6284 | lua_pop(L, 1); |
| 6285 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6286 | return ACT_RET_PRS_OK; |
| 6287 | } |
| 6288 | |
| 6289 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6290 | struct act_rule *rule, char **err) |
| 6291 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6292 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6293 | |
| 6294 | /* Memory for the rule. */ |
| 6295 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6296 | if (!rule->arg.hlua_rule) { |
| 6297 | memprintf(err, "out of memory error"); |
| 6298 | return ACT_RET_PRS_ERR; |
| 6299 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6300 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6301 | /* Reference the Lua function and store the reference. */ |
| 6302 | rule->arg.hlua_rule->fcn = *fcn; |
| 6303 | |
| 6304 | /* TODO: later accept arguments. */ |
| 6305 | rule->arg.hlua_rule->args = NULL; |
| 6306 | |
| 6307 | /* Add applet pointer in the rule. */ |
| 6308 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6309 | rule->applet.name = fcn->name; |
| 6310 | rule->applet.init = hlua_applet_tcp_init; |
| 6311 | rule->applet.fct = hlua_applet_tcp_fct; |
| 6312 | rule->applet.release = hlua_applet_tcp_release; |
| 6313 | rule->applet.timeout = hlua_timeout_applet; |
| 6314 | |
| 6315 | return 0; |
| 6316 | } |
| 6317 | |
| 6318 | /* This function is an LUA binding used for registering |
| 6319 | * "sample-conv" functions. It expects a converter name used |
| 6320 | * in the haproxy configuration file, and an LUA function. |
| 6321 | */ |
| 6322 | __LJMP static int hlua_register_service(lua_State *L) |
| 6323 | { |
| 6324 | struct action_kw_list *akl; |
| 6325 | const char *name; |
| 6326 | const char *env; |
| 6327 | int ref; |
| 6328 | int len; |
| 6329 | struct hlua_function *fcn; |
| 6330 | |
| 6331 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 6332 | |
| 6333 | /* First argument : converter name. */ |
| 6334 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6335 | |
| 6336 | /* Second argument : environment. */ |
| 6337 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6338 | |
| 6339 | /* Third argument : lua function. */ |
| 6340 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6341 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6342 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6343 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 6344 | if (!akl) |
| 6345 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6346 | fcn = calloc(1, sizeof(*fcn)); |
| 6347 | if (!fcn) |
| 6348 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6349 | |
| 6350 | /* Fill fcn. */ |
| 6351 | len = strlen("<lua.>") + strlen(name) + 1; |
| 6352 | fcn->name = calloc(1, len); |
| 6353 | if (!fcn->name) |
| 6354 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6355 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 6356 | fcn->function_ref = ref; |
| 6357 | |
| 6358 | /* List head */ |
| 6359 | akl->list.n = akl->list.p = NULL; |
| 6360 | |
| 6361 | /* converter keyword. */ |
| 6362 | len = strlen("lua.") + strlen(name) + 1; |
| 6363 | akl->kw[0].kw = calloc(1, len); |
| 6364 | if (!akl->kw[0].kw) |
| 6365 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6366 | |
| 6367 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6368 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 6369 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6370 | if (strcmp(env, "tcp") == 0) |
| 6371 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6372 | else if (strcmp(env, "http") == 0) |
| 6373 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6374 | else |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6375 | WILL_LJMP(luaL_error(L, "lua service environment '%s' is unknown. " |
| 6376 | "'tcp' or 'http' are expected.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6377 | |
| 6378 | akl->kw[0].match_pfx = 0; |
| 6379 | akl->kw[0].private = fcn; |
| 6380 | |
| 6381 | /* End of array. */ |
| 6382 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 6383 | |
| 6384 | /* Register this new converter */ |
| 6385 | service_keywords_register(akl); |
| 6386 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6387 | return 0; |
| 6388 | } |
| 6389 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6390 | /* This function initialises Lua cli handler. It copies the |
| 6391 | * arguments in the Lua stack and create channel IO objects. |
| 6392 | */ |
| 6393 | static int hlua_cli_parse_fct(char **args, struct appctx *appctx, void *private) |
| 6394 | { |
| 6395 | struct hlua *hlua; |
| 6396 | struct hlua_function *fcn; |
| 6397 | int i; |
| 6398 | const char *error; |
| 6399 | |
| 6400 | hlua = &appctx->ctx.hlua_cli.hlua; |
| 6401 | fcn = private; |
| 6402 | appctx->private = private; |
| 6403 | |
| 6404 | /* Create task used by signal to wakeup applets. |
| 6405 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 6406 | * applet_http. It is absolutely compatible. |
| 6407 | */ |
| 6408 | appctx->ctx.hlua_cli.task = task_new(); |
| 6409 | if (!appctx->ctx.hlua_cli.task) { |
| 6410 | SEND_ERR(NULL, "Lua applet tcp '%s': out of memory.\n", fcn->name); |
| 6411 | return 0; |
| 6412 | } |
| 6413 | appctx->ctx.hlua_cli.task->nice = 0; |
| 6414 | appctx->ctx.hlua_cli.task->context = appctx; |
| 6415 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 6416 | |
| 6417 | /* Initialises the Lua context */ |
| 6418 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task)) { |
| 6419 | SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name); |
| 6420 | return 1; |
| 6421 | } |
| 6422 | |
| 6423 | /* The following Lua calls can fail. */ |
| 6424 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 6425 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6426 | error = lua_tostring(hlua->T, -1); |
| 6427 | else |
| 6428 | error = "critical error"; |
| 6429 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 6430 | goto error; |
| 6431 | } |
| 6432 | |
| 6433 | /* Check stack available size. */ |
| 6434 | if (!lua_checkstack(hlua->T, 2)) { |
| 6435 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6436 | goto error; |
| 6437 | } |
| 6438 | |
| 6439 | /* Restore the function in the stack. */ |
| 6440 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 6441 | |
| 6442 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 6443 | * so push AppletTCP in the stack. |
| 6444 | * TODO: get_priv() and set_priv() are useless. Maybe we will |
| 6445 | * create a new object without these two functions. |
| 6446 | */ |
| 6447 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 6448 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6449 | goto error; |
| 6450 | } |
| 6451 | hlua->nargs = 1; |
| 6452 | |
| 6453 | /* push keywords in the stack. */ |
| 6454 | for (i = 0; *args[i]; i++) { |
| 6455 | /* Check stack available size. */ |
| 6456 | if (!lua_checkstack(hlua->T, 1)) { |
| 6457 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 6458 | goto error; |
| 6459 | } |
| 6460 | lua_pushstring(hlua->T, args[i]); |
| 6461 | hlua->nargs++; |
| 6462 | } |
| 6463 | |
| 6464 | /* We must initialize the execution timeouts. */ |
| 6465 | hlua->max_time = hlua_timeout_session; |
| 6466 | |
| 6467 | /* At this point the execution is safe. */ |
| 6468 | RESET_SAFE_LJMP(hlua->T); |
| 6469 | |
| 6470 | /* It's ok */ |
| 6471 | return 0; |
| 6472 | |
| 6473 | /* It's not ok. */ |
| 6474 | error: |
| 6475 | RESET_SAFE_LJMP(hlua->T); |
| 6476 | hlua_ctx_destroy(hlua); |
| 6477 | return 1; |
| 6478 | } |
| 6479 | |
| 6480 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 6481 | { |
| 6482 | struct hlua *hlua; |
| 6483 | struct stream_interface *si; |
| 6484 | struct hlua_function *fcn; |
| 6485 | |
| 6486 | hlua = &appctx->ctx.hlua_cli.hlua; |
| 6487 | si = appctx->owner; |
| 6488 | fcn = appctx->private; |
| 6489 | |
| 6490 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6491 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6492 | return 1; |
| 6493 | |
| 6494 | /* Execute the function. */ |
| 6495 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6496 | |
| 6497 | /* finished. */ |
| 6498 | case HLUA_E_OK: |
| 6499 | return 1; |
| 6500 | |
| 6501 | /* yield. */ |
| 6502 | case HLUA_E_AGAIN: |
| 6503 | /* We want write. */ |
| 6504 | if (HLUA_IS_WAKERESWR(hlua)) |
| 6505 | si_applet_cant_put(si); |
| 6506 | /* Set the timeout. */ |
| 6507 | if (hlua->wake_time != TICK_ETERNITY) |
| 6508 | task_schedule(hlua->task, hlua->wake_time); |
| 6509 | return 0; |
| 6510 | |
| 6511 | /* finished with error. */ |
| 6512 | case HLUA_E_ERRMSG: |
| 6513 | /* Display log. */ |
| 6514 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 6515 | fcn->name, lua_tostring(hlua->T, -1)); |
| 6516 | lua_pop(hlua->T, 1); |
| 6517 | return 1; |
| 6518 | |
| 6519 | case HLUA_E_ERR: |
| 6520 | /* Display log. */ |
| 6521 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 6522 | fcn->name); |
| 6523 | return 1; |
| 6524 | |
| 6525 | default: |
| 6526 | return 1; |
| 6527 | } |
| 6528 | |
| 6529 | return 1; |
| 6530 | } |
| 6531 | |
| 6532 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 6533 | { |
| 6534 | struct hlua *hlua; |
| 6535 | |
| 6536 | hlua = &appctx->ctx.hlua_cli.hlua; |
| 6537 | hlua_ctx_destroy(hlua); |
| 6538 | } |
| 6539 | |
| 6540 | /* This function is an LUA binding used for registering |
| 6541 | * new keywords in the cli. It expects a list of keywords |
| 6542 | * which are the "path". It is limited to 5 keywords. A |
| 6543 | * description of the command, a function to be executed |
| 6544 | * for the parsing and a function for io handlers. |
| 6545 | */ |
| 6546 | __LJMP static int hlua_register_cli(lua_State *L) |
| 6547 | { |
| 6548 | struct cli_kw_list *cli_kws; |
| 6549 | const char *message; |
| 6550 | int ref_io; |
| 6551 | int len; |
| 6552 | struct hlua_function *fcn; |
| 6553 | int index; |
| 6554 | int i; |
| 6555 | |
| 6556 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 6557 | |
| 6558 | /* First argument : an array of maximum 5 keywords. */ |
| 6559 | if (!lua_istable(L, 1)) |
| 6560 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 6561 | |
| 6562 | /* Second argument : string with contextual message. */ |
| 6563 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6564 | |
| 6565 | /* Third and fourth argument : lua function. */ |
| 6566 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6567 | |
| 6568 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6569 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 6570 | if (!cli_kws) |
| 6571 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6572 | fcn = calloc(1, sizeof(*fcn)); |
| 6573 | if (!fcn) |
| 6574 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6575 | |
| 6576 | /* Fill path. */ |
| 6577 | index = 0; |
| 6578 | lua_pushnil(L); |
| 6579 | while(lua_next(L, 1) != 0) { |
| 6580 | if (index >= 5) |
| 6581 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 6582 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6583 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 6584 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 6585 | if (!cli_kws->kw[0].str_kw[index]) |
| 6586 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6587 | index++; |
| 6588 | lua_pop(L, 1); |
| 6589 | } |
| 6590 | |
| 6591 | /* Copy help message. */ |
| 6592 | cli_kws->kw[0].usage = strdup(message); |
| 6593 | if (!cli_kws->kw[0].usage) |
| 6594 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6595 | |
| 6596 | /* Fill fcn io handler. */ |
| 6597 | len = strlen("<lua.cli>") + 1; |
| 6598 | for (i = 0; i < index; i++) |
| 6599 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 6600 | fcn->name = calloc(1, len); |
| 6601 | if (!fcn->name) |
| 6602 | WILL_LJMP(luaL_error(L, "lua out of memory error.")); |
| 6603 | strncat((char *)fcn->name, "<lua.cli", len); |
| 6604 | for (i = 0; i < index; i++) { |
| 6605 | strncat((char *)fcn->name, ".", len); |
| 6606 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 6607 | } |
| 6608 | strncat((char *)fcn->name, ">", len); |
| 6609 | fcn->function_ref = ref_io; |
| 6610 | |
| 6611 | /* Fill last entries. */ |
| 6612 | cli_kws->kw[0].private = fcn; |
| 6613 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 6614 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 6615 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 6616 | |
| 6617 | /* Register this new converter */ |
| 6618 | cli_register_kw(cli_kws); |
| 6619 | |
| 6620 | return 0; |
| 6621 | } |
| 6622 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6623 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 6624 | struct proxy *defpx, const char *file, int line, |
| 6625 | char **err, unsigned int *timeout) |
| 6626 | { |
| 6627 | const char *error; |
| 6628 | |
| 6629 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
| 6630 | if (error && *error != '\0') { |
| 6631 | memprintf(err, "%s: invalid timeout", args[0]); |
| 6632 | return -1; |
| 6633 | } |
| 6634 | return 0; |
| 6635 | } |
| 6636 | |
| 6637 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 6638 | struct proxy *defpx, const char *file, int line, |
| 6639 | char **err) |
| 6640 | { |
| 6641 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 6642 | file, line, err, &hlua_timeout_session); |
| 6643 | } |
| 6644 | |
| 6645 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 6646 | struct proxy *defpx, const char *file, int line, |
| 6647 | char **err) |
| 6648 | { |
| 6649 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 6650 | file, line, err, &hlua_timeout_task); |
| 6651 | } |
| 6652 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6653 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 6654 | struct proxy *defpx, const char *file, int line, |
| 6655 | char **err) |
| 6656 | { |
| 6657 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 6658 | file, line, err, &hlua_timeout_applet); |
| 6659 | } |
| 6660 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 6661 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 6662 | struct proxy *defpx, const char *file, int line, |
| 6663 | char **err) |
| 6664 | { |
| 6665 | char *error; |
| 6666 | |
| 6667 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 6668 | if (*error != '\0') { |
| 6669 | memprintf(err, "%s: invalid number", args[0]); |
| 6670 | return -1; |
| 6671 | } |
| 6672 | return 0; |
| 6673 | } |
| 6674 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 6675 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 6676 | struct proxy *defpx, const char *file, int line, |
| 6677 | char **err) |
| 6678 | { |
| 6679 | char *error; |
| 6680 | |
| 6681 | if (*(args[1]) == 0) { |
| 6682 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 6683 | return -1; |
| 6684 | } |
| 6685 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 6686 | if (*error != '\0') { |
| 6687 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 6688 | return -1; |
| 6689 | } |
| 6690 | return 0; |
| 6691 | } |
| 6692 | |
| 6693 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 6694 | /* This function is called by the main configuration key "lua-load". It loads and |
| 6695 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 6696 | * the main lua entry point. |
| 6697 | * |
| 6698 | * This funtion runs with the HAProxy keywords API. It returns -1 if an error is |
| 6699 | * occured, otherwise it returns 0. |
| 6700 | * |
| 6701 | * In some error case, LUA set an error message in top of the stack. This function |
| 6702 | * 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] | 6703 | * |
| 6704 | * This function can fail with an abort() due to an Lua critical error. |
| 6705 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6706 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 6707 | */ |
| 6708 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 6709 | struct proxy *defpx, const char *file, int line, |
| 6710 | char **err) |
| 6711 | { |
| 6712 | int error; |
| 6713 | |
| 6714 | /* Just load and compile the file. */ |
| 6715 | error = luaL_loadfile(gL.T, args[1]); |
| 6716 | if (error) { |
| 6717 | memprintf(err, "error in lua file '%s': %s", args[1], lua_tostring(gL.T, -1)); |
| 6718 | lua_pop(gL.T, 1); |
| 6719 | return -1; |
| 6720 | } |
| 6721 | |
| 6722 | /* If no syntax error where detected, execute the code. */ |
| 6723 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 6724 | switch (error) { |
| 6725 | case LUA_OK: |
| 6726 | break; |
| 6727 | case LUA_ERRRUN: |
| 6728 | memprintf(err, "lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
| 6729 | lua_pop(gL.T, 1); |
| 6730 | return -1; |
| 6731 | case LUA_ERRMEM: |
| 6732 | memprintf(err, "lua out of memory error\n"); |
| 6733 | return -1; |
| 6734 | case LUA_ERRERR: |
| 6735 | memprintf(err, "lua message handler error: %s\n", lua_tostring(gL.T, -1)); |
| 6736 | lua_pop(gL.T, 1); |
| 6737 | return -1; |
| 6738 | case LUA_ERRGCMM: |
| 6739 | memprintf(err, "lua garbage collector error: %s\n", lua_tostring(gL.T, -1)); |
| 6740 | lua_pop(gL.T, 1); |
| 6741 | return -1; |
| 6742 | default: |
| 6743 | memprintf(err, "lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
| 6744 | lua_pop(gL.T, 1); |
| 6745 | return -1; |
| 6746 | } |
| 6747 | |
| 6748 | return 0; |
| 6749 | } |
| 6750 | |
| 6751 | /* configuration keywords declaration */ |
| 6752 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6753 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 6754 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 6755 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 6756 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 6757 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 6758 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 6759 | { 0, NULL, NULL }, |
| 6760 | }}; |
| 6761 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6762 | /* This function can fail with an abort() due to an Lua critical error. |
| 6763 | * We are in the initialisation process of HAProxy, this abort() is |
| 6764 | * tolerated. |
| 6765 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6766 | int hlua_post_init() |
| 6767 | { |
| 6768 | struct hlua_init_function *init; |
| 6769 | const char *msg; |
| 6770 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6771 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6772 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 6773 | /* Call post initialisation function in safe environement. */ |
| 6774 | if (!SET_SAFE_LJMP(gL.T)) { |
| 6775 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 6776 | error = lua_tostring(gL.T, -1); |
| 6777 | else |
| 6778 | error = "critical error"; |
| 6779 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 6780 | exit(1); |
| 6781 | } |
| 6782 | hlua_fcn_post_init(gL.T); |
| 6783 | RESET_SAFE_LJMP(gL.T); |
| 6784 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6785 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 6786 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 6787 | ret = hlua_ctx_resume(&gL, 0); |
| 6788 | switch (ret) { |
| 6789 | case HLUA_E_OK: |
| 6790 | lua_pop(gL.T, -1); |
| 6791 | return 1; |
| 6792 | case HLUA_E_AGAIN: |
| 6793 | Alert("lua init: yield not allowed.\n"); |
| 6794 | return 0; |
| 6795 | case HLUA_E_ERRMSG: |
| 6796 | msg = lua_tostring(gL.T, -1); |
| 6797 | Alert("lua init: %s.\n", msg); |
| 6798 | return 0; |
| 6799 | case HLUA_E_ERR: |
| 6800 | default: |
| 6801 | Alert("lua init: unknown runtime error.\n"); |
| 6802 | return 0; |
| 6803 | } |
| 6804 | } |
| 6805 | return 1; |
| 6806 | } |
| 6807 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 6808 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 6809 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 6810 | * is the previously allocated size or the kind of object in case of a new |
| 6811 | * allocation. <nsize> is the requested new size. |
| 6812 | */ |
| 6813 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 6814 | { |
| 6815 | struct hlua_mem_allocator *zone = ud; |
| 6816 | |
| 6817 | if (nsize == 0) { |
| 6818 | /* it's a free */ |
| 6819 | if (ptr) |
| 6820 | zone->allocated -= osize; |
| 6821 | free(ptr); |
| 6822 | return NULL; |
| 6823 | } |
| 6824 | |
| 6825 | if (!ptr) { |
| 6826 | /* it's a new allocation */ |
| 6827 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 6828 | return NULL; |
| 6829 | |
| 6830 | ptr = malloc(nsize); |
| 6831 | if (ptr) |
| 6832 | zone->allocated += nsize; |
| 6833 | return ptr; |
| 6834 | } |
| 6835 | |
| 6836 | /* it's a realloc */ |
| 6837 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 6838 | return NULL; |
| 6839 | |
| 6840 | ptr = realloc(ptr, nsize); |
| 6841 | if (ptr) |
| 6842 | zone->allocated += nsize - osize; |
| 6843 | return ptr; |
| 6844 | } |
| 6845 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6846 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 6847 | * We are in the initialisation process of HAProxy, this abort() is |
| 6848 | * tolerated. |
| 6849 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 6850 | void hlua_init(void) |
| 6851 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6852 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 6853 | int idx; |
| 6854 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 6855 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 6856 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6857 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6858 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6859 | struct srv_kw *kw; |
| 6860 | int tmp_error; |
| 6861 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 6862 | char *args[] = { /* SSL client configuration. */ |
| 6863 | "ssl", |
| 6864 | "verify", |
| 6865 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 6866 | NULL |
| 6867 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6868 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6869 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6870 | /* Initialise com signals pool */ |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 6871 | pool2_hlua_com = create_pool("hlua_com", sizeof(struct hlua_com), MEM_F_SHARED); |
| 6872 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 6873 | /* Register configuration keywords. */ |
| 6874 | cfg_register_keywords(&cfg_kws); |
| 6875 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 6876 | /* Init main lua stack. */ |
| 6877 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 6878 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 6879 | LIST_INIT(&gL.com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 6880 | gL.T = luaL_newstate(); |
| 6881 | hlua_sethlua(&gL); |
| 6882 | gL.Tref = LUA_REFNIL; |
| 6883 | gL.task = NULL; |
| 6884 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6885 | /* From this point, until the end of the initialisation fucntion, |
| 6886 | * the Lua function can fail with an abort. We are in the initialisation |
| 6887 | * process of HAProxy, this abort() is tolerated. |
| 6888 | */ |
| 6889 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 6890 | /* change the memory allocators to track memory usage */ |
| 6891 | lua_setallocf(gL.T, hlua_alloc, &hlua_global_allocator); |
| 6892 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 6893 | /* Initialise lua. */ |
| 6894 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6895 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 6896 | /* Set safe environment for the initialisation. */ |
| 6897 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6898 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 6899 | error_msg = lua_tostring(gL.T, -1); |
| 6900 | else |
| 6901 | error_msg = "critical error"; |
| 6902 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 6903 | exit(1); |
| 6904 | } |
| 6905 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6906 | /* |
| 6907 | * |
| 6908 | * Create "core" object. |
| 6909 | * |
| 6910 | */ |
| 6911 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 6912 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6913 | lua_newtable(gL.T); |
| 6914 | |
| 6915 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6916 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6917 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 6918 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6919 | /* Register special functions. */ |
| 6920 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6921 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6922 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6923 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6924 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6925 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6926 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 6927 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 6928 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 6929 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 6930 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 6931 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 6932 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 6933 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 6934 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 6935 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 6936 | hlua_class_function(gL.T, "log", hlua_log); |
| 6937 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 6938 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 6939 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 6940 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 6941 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 6942 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6943 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 6944 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 6945 | |
| 6946 | /* |
| 6947 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 6948 | * Register class Map |
| 6949 | * |
| 6950 | */ |
| 6951 | |
| 6952 | /* This table entry is the object "Map" base. */ |
| 6953 | lua_newtable(gL.T); |
| 6954 | |
| 6955 | /* register pattern types. */ |
| 6956 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 6957 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
| 6958 | |
| 6959 | /* register constructor. */ |
| 6960 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 6961 | |
| 6962 | /* Create and fill the metatable. */ |
| 6963 | lua_newtable(gL.T); |
| 6964 | |
| 6965 | /* Create and fille the __index entry. */ |
| 6966 | lua_pushstring(gL.T, "__index"); |
| 6967 | lua_newtable(gL.T); |
| 6968 | |
| 6969 | /* Register . */ |
| 6970 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 6971 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 6972 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 6973 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 6974 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 6975 | /* Register previous table in the registry with reference and named entry. |
| 6976 | * The function hlua_register_metatable() pops the stack, so we |
| 6977 | * previously create a copy of the table. |
| 6978 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 6979 | 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] | 6980 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 6981 | |
| 6982 | /* Assign the metatable to the mai Map object. */ |
| 6983 | lua_setmetatable(gL.T, -2); |
| 6984 | |
| 6985 | /* Set a name to the table. */ |
| 6986 | lua_setglobal(gL.T, "Map"); |
| 6987 | |
| 6988 | /* |
| 6989 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 6990 | * Register class Channel |
| 6991 | * |
| 6992 | */ |
| 6993 | |
| 6994 | /* Create and fill the metatable. */ |
| 6995 | lua_newtable(gL.T); |
| 6996 | |
| 6997 | /* Create and fille the __index entry. */ |
| 6998 | lua_pushstring(gL.T, "__index"); |
| 6999 | lua_newtable(gL.T); |
| 7000 | |
| 7001 | /* Register . */ |
| 7002 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7003 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7004 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7005 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7006 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7007 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7008 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7009 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7010 | 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] | 7011 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7012 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7013 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7014 | |
| 7015 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7016 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7017 | |
| 7018 | /* |
| 7019 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7020 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7021 | * |
| 7022 | */ |
| 7023 | |
| 7024 | /* Create and fill the metatable. */ |
| 7025 | lua_newtable(gL.T); |
| 7026 | |
| 7027 | /* Create and fille the __index entry. */ |
| 7028 | lua_pushstring(gL.T, "__index"); |
| 7029 | lua_newtable(gL.T); |
| 7030 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7031 | /* Browse existing fetches and create the associated |
| 7032 | * object method. |
| 7033 | */ |
| 7034 | sf = NULL; |
| 7035 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7036 | |
| 7037 | /* Dont register the keywork if the arguments check function are |
| 7038 | * not safe during the runtime. |
| 7039 | */ |
| 7040 | if ((sf->val_args != NULL) && |
| 7041 | (sf->val_args != val_payload_lv) && |
| 7042 | (sf->val_args != val_hdr)) |
| 7043 | continue; |
| 7044 | |
| 7045 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7046 | * by an underscore. |
| 7047 | */ |
| 7048 | strncpy(trash.str, sf->kw, trash.size); |
| 7049 | trash.str[trash.size - 1] = '\0'; |
| 7050 | for (p = trash.str; *p; p++) |
| 7051 | if (*p == '.' || *p == '-' || *p == '+') |
| 7052 | *p = '_'; |
| 7053 | |
| 7054 | /* Register the function. */ |
| 7055 | lua_pushstring(gL.T, trash.str); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7056 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7057 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7058 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7059 | } |
| 7060 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7061 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7062 | |
| 7063 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7064 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7065 | |
| 7066 | /* |
| 7067 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7068 | * Register class Converters |
| 7069 | * |
| 7070 | */ |
| 7071 | |
| 7072 | /* Create and fill the metatable. */ |
| 7073 | lua_newtable(gL.T); |
| 7074 | |
| 7075 | /* Create and fill the __index entry. */ |
| 7076 | lua_pushstring(gL.T, "__index"); |
| 7077 | lua_newtable(gL.T); |
| 7078 | |
| 7079 | /* Browse existing converters and create the associated |
| 7080 | * object method. |
| 7081 | */ |
| 7082 | sc = NULL; |
| 7083 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7084 | /* Dont register the keywork if the arguments check function are |
| 7085 | * not safe during the runtime. |
| 7086 | */ |
| 7087 | if (sc->val_args != NULL) |
| 7088 | continue; |
| 7089 | |
| 7090 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7091 | * by an underscore. |
| 7092 | */ |
| 7093 | strncpy(trash.str, sc->kw, trash.size); |
| 7094 | trash.str[trash.size - 1] = '\0'; |
| 7095 | for (p = trash.str; *p; p++) |
| 7096 | if (*p == '.' || *p == '-' || *p == '+') |
| 7097 | *p = '_'; |
| 7098 | |
| 7099 | /* Register the function. */ |
| 7100 | lua_pushstring(gL.T, trash.str); |
| 7101 | lua_pushlightuserdata(gL.T, sc); |
| 7102 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7103 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7104 | } |
| 7105 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7106 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7107 | |
| 7108 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7109 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7110 | |
| 7111 | /* |
| 7112 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7113 | * Register class HTTP |
| 7114 | * |
| 7115 | */ |
| 7116 | |
| 7117 | /* Create and fill the metatable. */ |
| 7118 | lua_newtable(gL.T); |
| 7119 | |
| 7120 | /* Create and fille the __index entry. */ |
| 7121 | lua_pushstring(gL.T, "__index"); |
| 7122 | lua_newtable(gL.T); |
| 7123 | |
| 7124 | /* Register Lua functions. */ |
| 7125 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7126 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7127 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7128 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7129 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7130 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7131 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7132 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7133 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7134 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7135 | |
| 7136 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7137 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7138 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7139 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7140 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7141 | 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] | 7142 | 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] | 7143 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7144 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7145 | |
| 7146 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7147 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7148 | |
| 7149 | /* |
| 7150 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7151 | * Register class AppletTCP |
| 7152 | * |
| 7153 | */ |
| 7154 | |
| 7155 | /* Create and fill the metatable. */ |
| 7156 | lua_newtable(gL.T); |
| 7157 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7158 | /* Create and fille the __index entry. */ |
| 7159 | lua_pushstring(gL.T, "__index"); |
| 7160 | lua_newtable(gL.T); |
| 7161 | |
| 7162 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7163 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7164 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7165 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7166 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7167 | hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7168 | |
| 7169 | lua_settable(gL.T, -3); |
| 7170 | |
| 7171 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7172 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7173 | |
| 7174 | /* |
| 7175 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7176 | * Register class AppletHTTP |
| 7177 | * |
| 7178 | */ |
| 7179 | |
| 7180 | /* Create and fill the metatable. */ |
| 7181 | lua_newtable(gL.T); |
| 7182 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7183 | /* Create and fille the __index entry. */ |
| 7184 | lua_pushstring(gL.T, "__index"); |
| 7185 | lua_newtable(gL.T); |
| 7186 | |
| 7187 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7188 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7189 | hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7190 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7191 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7192 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7193 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7194 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7195 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 7196 | |
| 7197 | lua_settable(gL.T, -3); |
| 7198 | |
| 7199 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7200 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7201 | |
| 7202 | /* |
| 7203 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7204 | * Register class TXN |
| 7205 | * |
| 7206 | */ |
| 7207 | |
| 7208 | /* Create and fill the metatable. */ |
| 7209 | lua_newtable(gL.T); |
| 7210 | |
| 7211 | /* Create and fille the __index entry. */ |
| 7212 | lua_pushstring(gL.T, "__index"); |
| 7213 | lua_newtable(gL.T); |
| 7214 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7215 | /* Register Lua functions. */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7216 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 7217 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7218 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7219 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7220 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 7221 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7222 | hlua_class_function(gL.T, "set_loglevel",hlua_txn_set_loglevel); |
| 7223 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 7224 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7225 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 7226 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 7227 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 7228 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 7229 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 7230 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7231 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7232 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7233 | |
| 7234 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7235 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7236 | |
| 7237 | /* |
| 7238 | * |
| 7239 | * Register class Socket |
| 7240 | * |
| 7241 | */ |
| 7242 | |
| 7243 | /* Create and fill the metatable. */ |
| 7244 | lua_newtable(gL.T); |
| 7245 | |
| 7246 | /* Create and fille the __index entry. */ |
| 7247 | lua_pushstring(gL.T, "__index"); |
| 7248 | lua_newtable(gL.T); |
| 7249 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7250 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7251 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7252 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7253 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 7254 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 7255 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 7256 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 7257 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 7258 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 7259 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 7260 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 7261 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7262 | 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] | 7263 | |
| 7264 | /* Register the garbage collector entry. */ |
| 7265 | lua_pushstring(gL.T, "__gc"); |
| 7266 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7267 | 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] | 7268 | |
| 7269 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7270 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7271 | |
| 7272 | /* Proxy and server configuration initialisation. */ |
| 7273 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 7274 | init_new_proxy(&socket_proxy); |
| 7275 | socket_proxy.parent = NULL; |
| 7276 | socket_proxy.last_change = now.tv_sec; |
| 7277 | socket_proxy.id = "LUA-SOCKET"; |
| 7278 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 7279 | socket_proxy.maxconn = 0; |
| 7280 | socket_proxy.accept = NULL; |
| 7281 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 7282 | socket_proxy.srv = NULL; |
| 7283 | socket_proxy.conn_retries = 0; |
| 7284 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 7285 | |
| 7286 | /* Init TCP server: unchanged parameters */ |
| 7287 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 7288 | socket_tcp.next = NULL; |
| 7289 | socket_tcp.proxy = &socket_proxy; |
| 7290 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 7291 | LIST_INIT(&socket_tcp.actconns); |
| 7292 | LIST_INIT(&socket_tcp.pendconns); |
Willy Tarreau | 600802a | 2015-08-04 17:19:06 +0200 | [diff] [blame] | 7293 | LIST_INIT(&socket_tcp.priv_conns); |
Willy Tarreau | 173a1c6 | 2015-08-05 10:31:57 +0200 | [diff] [blame] | 7294 | LIST_INIT(&socket_tcp.idle_conns); |
Willy Tarreau | 7017cb0 | 2015-08-05 16:35:23 +0200 | [diff] [blame] | 7295 | LIST_INIT(&socket_tcp.safe_conns); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7296 | socket_tcp.state = SRV_ST_RUNNING; /* early server setup */ |
| 7297 | socket_tcp.last_change = 0; |
| 7298 | socket_tcp.id = "LUA-TCP-CONN"; |
| 7299 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7300 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7301 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 7302 | |
| 7303 | /* XXX: Copy default parameter from default server, |
| 7304 | * but the default server is not initialized. |
| 7305 | */ |
| 7306 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7307 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 7308 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 7309 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 7310 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 7311 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7312 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7313 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7314 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 7315 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 7316 | |
| 7317 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 7318 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 7319 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 7320 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 7321 | socket_tcp.check.server = &socket_tcp; |
| 7322 | |
| 7323 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 7324 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7325 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7326 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 7327 | socket_tcp.agent.server = &socket_tcp; |
| 7328 | |
| 7329 | socket_tcp.xprt = &raw_sock; |
| 7330 | |
| 7331 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7332 | /* Init TCP server: unchanged parameters */ |
| 7333 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 7334 | socket_ssl.next = NULL; |
| 7335 | socket_ssl.proxy = &socket_proxy; |
| 7336 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 7337 | LIST_INIT(&socket_ssl.actconns); |
| 7338 | LIST_INIT(&socket_ssl.pendconns); |
Willy Tarreau | 600802a | 2015-08-04 17:19:06 +0200 | [diff] [blame] | 7339 | LIST_INIT(&socket_ssl.priv_conns); |
Willy Tarreau | 173a1c6 | 2015-08-05 10:31:57 +0200 | [diff] [blame] | 7340 | LIST_INIT(&socket_ssl.idle_conns); |
Willy Tarreau | 7017cb0 | 2015-08-05 16:35:23 +0200 | [diff] [blame] | 7341 | LIST_INIT(&socket_ssl.safe_conns); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7342 | socket_ssl.state = SRV_ST_RUNNING; /* early server setup */ |
| 7343 | socket_ssl.last_change = 0; |
| 7344 | socket_ssl.id = "LUA-SSL-CONN"; |
| 7345 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7346 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7347 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 7348 | |
| 7349 | /* XXX: Copy default parameter from default server, |
| 7350 | * but the default server is not initialized. |
| 7351 | */ |
| 7352 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7353 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 7354 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 7355 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 7356 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 7357 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7358 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7359 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7360 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 7361 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 7362 | |
| 7363 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 7364 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 7365 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 7366 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 7367 | socket_ssl.check.server = &socket_ssl; |
| 7368 | |
| 7369 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 7370 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 7371 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 7372 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 7373 | socket_ssl.agent.server = &socket_ssl; |
| 7374 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7375 | socket_ssl.use_ssl = 1; |
| 7376 | socket_ssl.xprt = &ssl_sock; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7377 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7378 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7379 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 7380 | /* |
| 7381 | * |
| 7382 | * If the keyword is not known, we can search in the registered |
| 7383 | * server keywords. This is usefull to configure special SSL |
| 7384 | * features like client certificates and ssl_verify. |
| 7385 | * |
| 7386 | */ |
| 7387 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 7388 | if (tmp_error != 0) { |
| 7389 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 7390 | abort(); /* This must be never arrives because the command line |
| 7391 | not editable by the user. */ |
| 7392 | } |
| 7393 | idx += kw->skip; |
| 7394 | } |
| 7395 | } |
| 7396 | |
| 7397 | /* Initialize SSL server. */ |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7398 | ssl_sock_prepare_srv_ctx(&socket_ssl, &socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7399 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7400 | |
| 7401 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7402 | } |