blob: 962195a6070bbd8550e1c914cddbdc33d1a4d5da [file] [log] [blame]
Thierry Fourniere726b142016-02-11 17:57:57 +01001/*
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
Bertrand Jacquinf4c12d42021-01-21 21:14:07 +000013#define _GNU_SOURCE
14
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010015#include <ctype.h>
Thierry FOURNIERbabae282015-09-17 11:36:37 +020016#include <setjmp.h>
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +010017
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010018#include <lauxlib.h>
19#include <lua.h>
20#include <lualib.h>
21
Thierry FOURNIER463119c2015-03-10 00:35:36 +010022#if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503
23#error "Requires Lua 5.3 or later."
Cyril Bontédc0306e2015-03-02 00:08:40 +010024#endif
25
Willy Tarreau8d2b7772020-05-27 10:58:19 +020026#include <import/ebpttree.h>
Thierry FOURNIER380d0932015-01-23 14:27:52 +010027
Willy Tarreaub2551052020-06-09 09:07:15 +020028#include <haproxy/api.h>
29#include <haproxy/applet.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020030#include <haproxy/arg.h>
Christopher Fauletd25d9262020-08-06 11:04:46 +020031#include <haproxy/auth.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020032#include <haproxy/cfgparse.h>
Willy Tarreauf1d32c42020-06-04 21:07:02 +020033#include <haproxy/channel.h>
Willy Tarreau83487a82020-06-04 20:19:54 +020034#include <haproxy/cli.h>
Willy Tarreau7ea393d2020-06-04 18:02:10 +020035#include <haproxy/connection.h>
Willy Tarreau5413a872020-06-02 19:33:08 +020036#include <haproxy/h1.h>
Willy Tarreau86416052020-06-04 09:20:54 +020037#include <haproxy/hlua.h>
Willy Tarreau8c794002020-06-04 10:05:25 +020038#include <haproxy/hlua_fcn.h>
Willy Tarreauc2b1ff02020-06-04 21:21:03 +020039#include <haproxy/http_ana.h>
Willy Tarreau126ba3a2020-06-04 18:26:43 +020040#include <haproxy/http_fetch.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020041#include <haproxy/http_htx.h>
Willy Tarreauc761f842020-06-04 11:40:28 +020042#include <haproxy/http_rules.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020043#include <haproxy/log.h>
Willy Tarreau2cd58092020-06-04 15:10:43 +020044#include <haproxy/map.h>
Willy Tarreau8efbdfb2020-06-04 11:29:21 +020045#include <haproxy/obj_type.h>
Willy Tarreau225a90a2020-06-04 15:06:28 +020046#include <haproxy/pattern.h>
Willy Tarreau469509b2020-06-04 15:13:30 +020047#include <haproxy/payload.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020048#include <haproxy/proxy-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020049#include <haproxy/regex.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020050#include <haproxy/sample.h>
Willy Tarreau198e92a2021-03-05 10:23:32 +010051#include <haproxy/server.h>
Willy Tarreau48d25b32020-06-04 18:58:52 +020052#include <haproxy/session.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020053#include <haproxy/stats-t.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020054#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020055#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020056#include <haproxy/task.h>
Willy Tarreau8b550af2020-06-04 17:42:48 +020057#include <haproxy/tcp_rules.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020058#include <haproxy/thread.h>
Willy Tarreau48fbcae2020-06-03 18:09:46 +020059#include <haproxy/tools.h>
Willy Tarreaua1718922020-06-04 16:25:31 +020060#include <haproxy/vars.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020061#include <haproxy/xref.h>
62
Thierry FOURNIER380d0932015-01-23 14:27:52 +010063
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010064/* Lua uses longjmp to perform yield or throwing errors. This
65 * macro is used only for identifying the function that can
66 * not return because a longjmp is executed.
67 * __LJMP marks a prototype of hlua file that can use longjmp.
68 * WILL_LJMP() marks an lua function that will use longjmp.
69 * MAY_LJMP() marks an lua function that may use longjmp.
70 */
71#define __LJMP
Willy Tarreau4e7cc332018-10-20 17:45:48 +020072#define WILL_LJMP(func) do { func; my_unreachable(); } while(0)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +010073#define MAY_LJMP(func) func
74
Thierry FOURNIERbabae282015-09-17 11:36:37 +020075/* This couple of function executes securely some Lua calls outside of
76 * the lua runtime environment. Each Lua call can return a longjmp
77 * if it encounter a memory error.
78 *
79 * Lua documentation extract:
80 *
81 * If an error happens outside any protected environment, Lua calls
82 * a panic function (see lua_atpanic) and then calls abort, thus
83 * exiting the host application. Your panic function can avoid this
84 * exit by never returning (e.g., doing a long jump to your own
85 * recovery point outside Lua).
86 *
87 * The panic function runs as if it were a message handler (see
88 * §2.3); in particular, the error message is at the top of the
89 * stack. However, there is no guarantee about stack space. To push
90 * anything on the stack, the panic function must first check the
91 * available space (see §4.2).
92 *
93 * We must check all the Lua entry point. This includes:
94 * - The include/proto/hlua.h exported functions
95 * - the task wrapper function
96 * - The action wrapper function
97 * - The converters wrapper function
98 * - The sample-fetch wrapper functions
99 *
Ilya Shipitsin46a030c2020-07-05 16:36:08 +0500100 * It is tolerated that the initialisation function returns an abort.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800101 * Before each Lua abort, an error message is written on stderr.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200102 *
103 * The macro SET_SAFE_LJMP initialise the longjmp. The Macro
104 * RESET_SAFE_LJMP reset the longjmp. These function must be macro
105 * because they must be exists in the program stack when the longjmp
106 * is called.
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200107 *
108 * Note that the Lua processing is not really thread safe. It provides
109 * heavy system which consists to add our own lock function in the Lua
110 * code and recompile the library. This system will probably not accepted
111 * by maintainers of various distribs.
112 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500113 * Our main execution point of the Lua is the function lua_resume(). A
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +0200114 * quick looking on the Lua sources displays a lua_lock() a the start
115 * of function and a lua_unlock() at the end of the function. So I
116 * conclude that the Lua thread safe mode just perform a mutex around
117 * all execution. So I prefer to do this in the HAProxy code, it will be
118 * easier for distro maintainers.
119 *
120 * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP
121 * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful
122 * to set mutex around these functions.
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200123 */
Willy Tarreau86abe442018-11-25 20:12:18 +0100124__decl_spinlock(hlua_global_lock);
Thierry FOURNIERffbad792017-07-12 11:39:04 +0200125THREAD_LOCAL jmp_buf safe_ljmp_env;
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200126static int hlua_panic_safe(lua_State *L) { return 0; }
Willy Tarreau9d6bb5a2020-02-06 15:55:41 +0100127static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); }
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200128
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100129/* This is the chained list of struct hlua_function referenced
130 * for haproxy action, sample-fetches, converters, cli and
131 * applet bindings. It is used for a post-initialisation control.
132 */
133static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions);
134
Thierry Fournierc7492592020-11-28 23:57:24 +0100135/* This variable is used only during initialization to identify the Lua state
136 * currently being initialized. 0 is the common lua state, 1 to n are the Lua
137 * states dedicated to each thread (in this case hlua_state_id==tid+1).
138 */
139static int hlua_state_id;
140
Thierry Fournier59f11be2020-11-29 00:37:41 +0100141/* This is a NULL-terminated list of lua file which are referenced to load per thread */
142static char **per_thread_load = NULL;
143
144lua_State *hlua_init_state(int thread_id);
145
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100146#define SET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200147 ({ \
148 int ret; \
Thierry Fournier021d9862020-11-28 23:42:03 +0100149 if ((__HLUA)->state_id == 0) \
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100150 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200151 if (setjmp(safe_ljmp_env) != 0) { \
152 lua_atpanic(__L, hlua_panic_safe); \
153 ret = 0; \
Thierry Fournier021d9862020-11-28 23:42:03 +0100154 if ((__HLUA)->state_id == 0) \
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100155 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200156 } else { \
157 lua_atpanic(__L, hlua_panic_ljmp); \
158 ret = 1; \
159 } \
160 ret; \
161 })
162
163/* If we are the last function catching Lua errors, we
164 * must reset the panic function.
165 */
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100166#define RESET_SAFE_LJMP_L(__L, __HLUA) \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200167 do { \
168 lua_atpanic(__L, hlua_panic_safe); \
Thierry Fournier021d9862020-11-28 23:42:03 +0100169 if ((__HLUA)->state_id == 0) \
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100170 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \
Thierry FOURNIERbabae282015-09-17 11:36:37 +0200171 } while(0)
172
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100173#define SET_SAFE_LJMP(__HLUA) \
174 SET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
175
176#define RESET_SAFE_LJMP(__HLUA) \
177 RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA)
178
179#define SET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100180 SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100181
182#define RESET_SAFE_LJMP_PARENT(__HLUA) \
Thierry Fournier021d9862020-11-28 23:42:03 +0100183 RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA)
Thierry Fournier7cbe5042020-11-28 17:02:21 +0100184
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200185/* Applet status flags */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200186#define APPLET_DONE 0x01 /* applet processing is done. */
Christopher Faulet18c2e8d2019-03-01 12:02:08 +0100187/* unused: 0x02 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200188#define APPLET_HDR_SENT 0x04 /* Response header sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +0200189/* unused: 0x08, 0x10 */
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +0100190#define APPLET_HTTP11 0x20 /* Last chunk sent. */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +0100191#define APPLET_RSP_SENT 0x40 /* The response was fully sent */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200192
Thierry Fournierafc63e22020-11-28 17:06:51 +0100193/* The main Lua execution context. The 0 index is the
194 * common state shared by all threads.
195 */
Willy Tarreau186f3762020-12-04 11:48:12 +0100196static lua_State *hlua_states[MAX_THREADS + 1];
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100197
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100198/* This is the memory pool containing struct lua for applets
199 * (including cli).
200 */
Willy Tarreau8ceae722018-11-26 11:58:30 +0100201DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua));
Thierry FOURNIERebed6e92016-12-16 11:54:07 +0100202
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100203/* Used for Socket connection. */
204static struct proxy socket_proxy;
205static struct server socket_tcp;
206#ifdef USE_OPENSSL
207static struct server socket_ssl;
208#endif
209
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100210/* List head of the function called at the initialisation time. */
Thierry Fournierc7492592020-11-28 23:57:24 +0100211struct list hlua_init_functions[MAX_THREADS + 1];
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +0100212
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100213/* The following variables contains the reference of the different
214 * Lua classes. These references are useful for identify metadata
215 * associated with an object.
216 */
Thierry FOURNIER65f34c62015-02-16 20:11:43 +0100217static int class_txn_ref;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100218static int class_socket_ref;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +0100219static int class_channel_ref;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +0100220static int class_fetches_ref;
Thierry FOURNIER594afe72015-03-10 23:58:30 +0100221static int class_converters_ref;
Thierry FOURNIER08504f42015-03-16 14:17:08 +0100222static int class_http_ref;
Thierry FOURNIER3def3932015-04-07 11:27:54 +0200223static int class_map_ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200224static int class_applet_tcp_ref;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200225static int class_applet_http_ref;
Christopher Faulet700d9e82020-01-31 12:21:52 +0100226static int class_txn_reply_ref;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +0100227
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100228/* Global Lua execution timeout. By default Lua, execution linked
Willy Tarreau87b09662015-04-03 00:22:06 +0200229 * with stream (actions, sample-fetches and converters) have a
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100230 * short timeout. Lua linked with tasks doesn't have a timeout
231 * because a task may remain alive during all the haproxy execution.
232 */
233static unsigned int hlua_timeout_session = 4000; /* session timeout. */
234static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +0200235static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */
Thierry FOURNIERbd413492015-03-03 16:52:26 +0100236
Thierry FOURNIERee9f8022015-03-03 17:37:37 +0100237/* Interrupts the Lua processing each "hlua_nb_instruction" instructions.
238 * it is used for preventing infinite loops.
239 *
240 * I test the scheer with an infinite loop containing one incrementation
241 * and one test. I run this loop between 10 seconds, I raise a ceil of
242 * 710M loops from one interrupt each 9000 instructions, so I fix the value
243 * to one interrupt each 10 000 instructions.
244 *
245 * configured | Number of
246 * instructions | loops executed
247 * between two | in milions
248 * forced yields |
249 * ---------------+---------------
250 * 10 | 160
251 * 500 | 670
252 * 1000 | 680
253 * 5000 | 700
254 * 7000 | 700
255 * 8000 | 700
256 * 9000 | 710 <- ceil
257 * 10000 | 710
258 * 100000 | 710
259 * 1000000 | 710
260 *
261 */
262static unsigned int hlua_nb_instruction = 10000;
263
Willy Tarreaucdb53462020-12-02 12:12:00 +0100264/* Descriptor for the memory allocation state. The limit is pre-initialised to
265 * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it
266 * is replaced with ~0 during post_init after everything was loaded. This way
267 * it is guaranteed that if limit is ~0 the boot is complete and that if it's
268 * zero it's not yet limited and proper accounting is required.
Willy Tarreau32f61e22015-03-18 17:54:59 +0100269 */
270struct hlua_mem_allocator {
271 size_t allocated;
272 size_t limit;
273};
274
Willy Tarreaucdb53462020-12-02 12:12:00 +0100275static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64);
Willy Tarreau32f61e22015-03-18 17:54:59 +0100276
Christopher Fauleta61789a2021-03-19 15:16:28 +0100277 /* > 0 if lua is in a non-rentrant part, thus with a non-dumpable stack */
278THREAD_LOCAL unsigned int hlua_not_dumpable = 0;
279
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100280/* These functions converts types between HAProxy internal args or
281 * sample and LUA types. Another function permits to check if the
282 * LUA stack contains arguments according with an required ARG_T
283 * format.
284 */
285static int hlua_arg2lua(lua_State *L, const struct arg *arg);
286static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg);
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100287__LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100288 uint64_t mask, struct proxy *p);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100289static int hlua_smp2lua(lua_State *L, struct sample *smp);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100290static int hlua_smp2lua_str(lua_State *L, struct sample *smp);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100291static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp);
292
Christopher Faulet9d1332b2020-02-24 16:46:16 +0100293__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200294
Thierry Fournier59f11be2020-11-29 00:37:41 +0100295struct prepend_path {
296 struct list l;
297 char *type;
298 char *path;
299};
300
301static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list);
302
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200303#define SEND_ERR(__be, __fmt, __args...) \
304 do { \
305 send_log(__be, LOG_ERR, __fmt, ## __args); \
306 if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \
Christopher Faulet767a84b2017-11-24 16:50:31 +0100307 ha_alert(__fmt, ## __args); \
Thierry FOURNIER23bc3752015-09-11 19:15:43 +0200308 } while (0)
309
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100310static inline struct hlua_function *new_hlua_function()
311{
312 struct hlua_function *fcn;
Thierry Fournierc7492592020-11-28 23:57:24 +0100313 int i;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100314
315 fcn = calloc(1, sizeof(*fcn));
316 if (!fcn)
317 return NULL;
318 LIST_ADDQ(&referenced_functions, &fcn->l);
Thierry Fournierc7492592020-11-28 23:57:24 +0100319 for (i = 0; i < MAX_THREADS + 1; i++)
320 fcn->function_ref[i] = -1;
Thierry Fournier62a22aa2020-11-28 21:06:35 +0100321 return fcn;
322}
323
Thierry Fournierc7492592020-11-28 23:57:24 +0100324/* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */
325static inline int fcn_ref_to_stack_id(struct hlua_function *fcn)
326{
327 if (fcn->function_ref[0] == -1)
328 return tid + 1;
329 return 0;
330}
331
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100332/* Used to check an Lua function type in the stack. It creates and
333 * returns a reference of the function. This function throws an
334 * error if the rgument is not a "function".
335 */
336__LJMP unsigned int hlua_checkfunction(lua_State *L, int argno)
337{
338 if (!lua_isfunction(L, argno)) {
Thierry FOURNIERfd1e9552018-02-23 18:41:18 +0100339 const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno));
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100340 WILL_LJMP(luaL_argerror(L, argno, msg));
341 }
342 lua_pushvalue(L, argno);
343 return luaL_ref(L, LUA_REGISTRYINDEX);
344}
345
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +0200346/* Return the string that is of the top of the stack. */
347const char *hlua_get_top_error_string(lua_State *L)
348{
349 if (lua_gettop(L) < 1)
350 return "unknown error";
351 if (lua_type(L, -1) != LUA_TSTRING)
352 return "unknown error";
353 return lua_tostring(L, -1);
354}
355
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200356__LJMP static const char *hlua_traceback(lua_State *L)
357{
358 lua_Debug ar;
359 int level = 0;
Willy Tarreau83061a82018-07-13 11:56:34 +0200360 struct buffer *msg = get_trash_chunk();
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200361 int filled = 0;
362
363 while (lua_getstack(L, level++, &ar)) {
364
365 /* Add separator */
366 if (filled)
367 chunk_appendf(msg, ", ");
368 filled = 1;
369
370 /* Fill fields:
371 * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what;
372 * 'l': fills in the field currentline;
373 * 'n': fills in the field name and namewhat;
374 * 't': fills in the field istailcall;
375 */
376 lua_getinfo(L, "Slnt", &ar);
377
378 /* Append code localisation */
379 if (ar.currentline > 0)
380 chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline);
381 else
382 chunk_appendf(msg, "%s ", ar.short_src);
383
384 /*
385 * Get function name
386 *
387 * if namewhat is no empty, name is defined.
388 * what contains "Lua" for Lua function, "C" for C function,
389 * or "main" for main code.
390 */
391 if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */
392 chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */
393
394 else if (*ar.what == 'm') /* "main", the code is not executed in a function */
395 chunk_appendf(msg, "main chunk");
396
397 else if (*ar.what != 'C') /* for Lua functions, use <file:line> */
398 chunk_appendf(msg, "C function line %d", ar.linedefined);
399
400 else /* nothing left... */
401 chunk_appendf(msg, "?");
402
403
404 /* Display tailed call */
405 if (ar.istailcall)
406 chunk_appendf(msg, " ...");
407 }
408
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200409 return msg->area;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +0200410}
411
412
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100413/* This function check the number of arguments available in the
414 * stack. If the number of arguments available is not the same
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500415 * then <nb> an error is thrown.
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100416 */
417__LJMP static inline void check_args(lua_State *L, int nb, char *fcn)
418{
419 if (lua_gettop(L) == nb)
420 return;
421 WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb));
422}
423
Mark Lakes22154b42018-01-29 14:38:40 -0800424/* This function pushes an error string prefixed by the file name
Thierry FOURNIERe8b9a402015-02-25 18:48:12 +0100425 * and the line number where the error is encountered.
426 */
427static int hlua_pusherror(lua_State *L, const char *fmt, ...)
428{
429 va_list argp;
430 va_start(argp, fmt);
431 luaL_where(L, 1);
432 lua_pushvfstring(L, fmt, argp);
433 va_end(argp);
434 lua_concat(L, 2);
435 return 1;
436}
437
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100438/* This functions is used with sample fetch and converters. It
439 * converts the HAProxy configuration argument in a lua stack
440 * values.
441 *
442 * It takes an array of "arg", and each entry of the array is
443 * converted and pushed in the LUA stack.
444 */
445static int hlua_arg2lua(lua_State *L, const struct arg *arg)
446{
447 switch (arg->type) {
448 case ARGT_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100449 case ARGT_TIME:
450 case ARGT_SIZE:
Thierry FOURNIERf90838b2015-03-06 13:48:32 +0100451 lua_pushinteger(L, arg->data.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100452 break;
453
454 case ARGT_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200455 lua_pushlstring(L, arg->data.str.area, arg->data.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100456 break;
457
458 case ARGT_IPV4:
459 case ARGT_IPV6:
460 case ARGT_MSK4:
461 case ARGT_MSK6:
462 case ARGT_FE:
463 case ARGT_BE:
464 case ARGT_TAB:
465 case ARGT_SRV:
466 case ARGT_USR:
467 case ARGT_MAP:
468 default:
469 lua_pushnil(L);
470 break;
471 }
472 return 1;
473}
474
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500475/* This function take one entry in an LUA stack at the index "ud",
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100476 * and try to convert it in an HAProxy argument entry. This is useful
Ilya Shipitsind4259502020-04-08 01:07:56 +0500477 * with sample fetch wrappers. The input arguments are given to the
478 * lua wrapper and converted as arg list by the function.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100479 */
480static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg)
481{
482 switch (lua_type(L, ud)) {
483
484 case LUA_TNUMBER:
485 case LUA_TBOOLEAN:
486 arg->type = ARGT_SINT;
487 arg->data.sint = lua_tointeger(L, ud);
488 break;
489
490 case LUA_TSTRING:
491 arg->type = ARGT_STR;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200492 arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200493 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200494 arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200495 arg->data.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100496 break;
497
498 case LUA_TUSERDATA:
499 case LUA_TNIL:
500 case LUA_TTABLE:
501 case LUA_TFUNCTION:
502 case LUA_TTHREAD:
503 case LUA_TLIGHTUSERDATA:
504 arg->type = ARGT_SINT;
Thierry FOURNIERbf65cd42015-07-20 17:45:02 +0200505 arg->data.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100506 break;
507 }
508 return 1;
509}
510
511/* the following functions are used to convert a struct sample
512 * in Lua type. This useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500513 * fetches or converters.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100514 */
Willy Tarreau5eadada2015-03-10 17:28:54 +0100515static int hlua_smp2lua(lua_State *L, struct sample *smp)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100516{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200517 switch (smp->data.type) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100518 case SMP_T_SINT:
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100519 case SMP_T_BOOL:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200520 lua_pushinteger(L, smp->data.u.sint);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100521 break;
522
523 case SMP_T_BIN:
524 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200525 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100526 break;
527
528 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200529 switch (smp->data.u.meth.meth) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100530 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
531 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
532 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
533 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
534 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
535 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
536 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
537 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
538 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200539 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100540 break;
541 default:
542 lua_pushnil(L);
543 break;
544 }
545 break;
546
547 case SMP_T_IPV4:
548 case SMP_T_IPV6:
549 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200550 if (sample_casts[smp->data.type][SMP_T_STR] &&
551 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200552 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Willy Tarreau5eadada2015-03-10 17:28:54 +0100553 else
554 lua_pushnil(L);
555 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100556 default:
557 lua_pushnil(L);
558 break;
559 }
560 return 1;
561}
562
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100563/* the following functions are used to convert a struct sample
564 * in Lua strings. This is useful to convert the return of the
Ilya Shipitsind4259502020-04-08 01:07:56 +0500565 * fetches or converters.
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100566 */
567static int hlua_smp2lua_str(lua_State *L, struct sample *smp)
568{
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200569 switch (smp->data.type) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100570
571 case SMP_T_BIN:
572 case SMP_T_STR:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200573 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100574 break;
575
576 case SMP_T_METH:
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200577 switch (smp->data.u.meth.meth) {
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100578 case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break;
579 case HTTP_METH_GET: lua_pushstring(L, "GET"); break;
580 case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break;
581 case HTTP_METH_POST: lua_pushstring(L, "POST"); break;
582 case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break;
583 case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break;
584 case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break;
585 case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break;
586 case HTTP_METH_OTHER:
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200587 lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100588 break;
589 default:
590 lua_pushstring(L, "");
591 break;
592 }
593 break;
594
595 case SMP_T_SINT:
596 case SMP_T_BOOL:
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100597 case SMP_T_IPV4:
598 case SMP_T_IPV6:
599 case SMP_T_ADDR: /* This type is never used to qualify a sample. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200600 if (sample_casts[smp->data.type][SMP_T_STR] &&
601 sample_casts[smp->data.type][SMP_T_STR](smp))
Willy Tarreau843b7cb2018-07-13 10:54:26 +0200602 lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +0100603 else
604 lua_pushstring(L, "");
605 break;
606 default:
607 lua_pushstring(L, "");
608 break;
609 }
610 return 1;
611}
612
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100613/* the following functions are used to convert an Lua type in a
614 * struct sample. This is useful to provide data from a converter
615 * to the LUA code.
616 */
617static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp)
618{
619 switch (lua_type(L, ud)) {
620
621 case LUA_TNUMBER:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200622 smp->data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200623 smp->data.u.sint = lua_tointeger(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100624 break;
625
626
627 case LUA_TBOOLEAN:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200628 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200629 smp->data.u.sint = lua_toboolean(L, ud);
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100630 break;
631
632 case LUA_TSTRING:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200633 smp->data.type = SMP_T_STR;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100634 smp->flags |= SMP_F_CONST;
Tim Duesterhus2e89dec2019-09-29 23:03:08 +0200635 smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data);
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200636 /* We don't know the actual size of the underlying allocation, so be conservative. */
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200637 smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */
Tim Duesterhus29d2e8a2019-09-29 23:03:07 +0200638 smp->data.u.str.head = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100639 break;
640
641 case LUA_TUSERDATA:
642 case LUA_TNIL:
643 case LUA_TTABLE:
644 case LUA_TFUNCTION:
645 case LUA_TTHREAD:
646 case LUA_TLIGHTUSERDATA:
Thierry FOURNIER93405e12015-08-26 14:19:03 +0200647 case LUA_TNONE:
648 default:
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +0200649 smp->data.type = SMP_T_BOOL;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +0200650 smp->data.u.sint = 0;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100651 break;
652 }
653 return 1;
654}
655
Ilya Shipitsind4259502020-04-08 01:07:56 +0500656/* This function check the "argp" built by another conversion function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -0800657 * is in accord with the expected argp defined by the "mask". The function
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100658 * returns true or false. It can be adjust the types if there compatibles.
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100659 *
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500660 * This function assumes that the argp argument contains ARGM_NBARGS + 1
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100661 * entries.
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100662 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100663__LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp,
David Carlier0c437f42016-04-27 16:21:56 +0100664 uint64_t mask, struct proxy *p)
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100665{
666 int min_arg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200667 int i, idx;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100668 struct proxy *px;
Christopher Fauletd25d9262020-08-06 11:04:46 +0200669 struct userlist *ul;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200670 struct my_regex *reg;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200671 const char *msg = NULL;
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200672 char *sname, *pname, *err = NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100673
674 idx = 0;
675 min_arg = ARGM(mask);
676 mask >>= ARGM_BITS;
677
678 while (1) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200679 struct buffer tmp = BUF_NULL;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100680
681 /* Check oversize. */
682 if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200683 msg = "Malformed argument mask";
684 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100685 }
686
687 /* Check for mandatory arguments. */
688 if (argp[idx].type == ARGT_STOP) {
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100689 if (idx < min_arg) {
690
691 /* If miss other argument than the first one, we return an error. */
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200692 if (idx > 0) {
693 msg = "Mandatory argument expected";
694 goto error;
695 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100696
697 /* If first argument have a certain type, some default values
698 * may be used. See the function smp_resolve_args().
699 */
700 switch (mask & ARGT_MASK) {
701
702 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200703 if (!(p->cap & PR_CAP_FE)) {
704 msg = "Mandatory argument expected";
705 goto error;
706 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100707 argp[idx].data.prx = p;
708 argp[idx].type = ARGT_FE;
709 argp[idx+1].type = ARGT_STOP;
710 break;
711
712 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200713 if (!(p->cap & PR_CAP_BE)) {
714 msg = "Mandatory argument expected";
715 goto error;
716 }
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100717 argp[idx].data.prx = p;
718 argp[idx].type = ARGT_BE;
719 argp[idx+1].type = ARGT_STOP;
720 break;
721
722 case ARGT_TAB:
723 argp[idx].data.prx = p;
724 argp[idx].type = ARGT_TAB;
725 argp[idx+1].type = ARGT_STOP;
726 break;
727
728 default:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200729 msg = "Mandatory argument expected";
730 goto error;
Thierry FOURNIER3caa0392015-03-13 13:38:17 +0100731 break;
732 }
733 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200734 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100735 }
736
Ilya Shipitsin856aabc2020-04-16 23:51:34 +0500737 /* Check for exceed the number of required argument. */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100738 if ((mask & ARGT_MASK) == ARGT_STOP &&
739 argp[idx].type != ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200740 msg = "Last argument expected";
741 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100742 }
743
744 if ((mask & ARGT_MASK) == ARGT_STOP &&
745 argp[idx].type == ARGT_STOP) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200746 break;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100747 }
748
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200749 /* Convert some argument types. All string in argp[] are for not
750 * duplicated yet.
751 */
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100752 switch (mask & ARGT_MASK) {
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100753 case ARGT_SINT:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200754 if (argp[idx].type != ARGT_SINT) {
755 msg = "integer expected";
756 goto error;
757 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100758 argp[idx].type = ARGT_SINT;
759 break;
760
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100761 case ARGT_TIME:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200762 if (argp[idx].type != ARGT_SINT) {
763 msg = "integer expected";
764 goto error;
765 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200766 argp[idx].type = ARGT_TIME;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100767 break;
768
769 case ARGT_SIZE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200770 if (argp[idx].type != ARGT_SINT) {
771 msg = "integer expected";
772 goto error;
773 }
Thierry FOURNIER29176f32015-07-07 00:41:29 +0200774 argp[idx].type = ARGT_SIZE;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100775 break;
776
777 case ARGT_FE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200778 if (argp[idx].type != ARGT_STR) {
779 msg = "string expected";
780 goto error;
781 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200782 argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200783 if (!argp[idx].data.prx) {
784 msg = "frontend doesn't exist";
785 goto error;
786 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100787 argp[idx].type = ARGT_FE;
788 break;
789
790 case ARGT_BE:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200791 if (argp[idx].type != ARGT_STR) {
792 msg = "string expected";
793 goto error;
794 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200795 argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200796 if (!argp[idx].data.prx) {
797 msg = "backend doesn't exist";
798 goto error;
799 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100800 argp[idx].type = ARGT_BE;
801 break;
802
803 case ARGT_TAB:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200804 if (argp[idx].type != ARGT_STR) {
805 msg = "string expected";
806 goto error;
807 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200808 argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200809 if (!argp[idx].data.t) {
810 msg = "table doesn't exist";
811 goto error;
812 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100813 argp[idx].type = ARGT_TAB;
814 break;
815
816 case ARGT_SRV:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200817 if (argp[idx].type != ARGT_STR) {
818 msg = "string expected";
819 goto error;
820 }
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200821 sname = strrchr(argp[idx].data.str.area, '/');
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100822 if (sname) {
823 *sname++ = '\0';
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200824 pname = argp[idx].data.str.area;
Willy Tarreau9e0bb102015-05-26 11:24:42 +0200825 px = proxy_be_by_name(pname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200826 if (!px) {
827 msg = "backend doesn't exist";
828 goto error;
829 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100830 }
831 else {
Christopher Fauletfdea1b62020-08-06 08:29:18 +0200832 sname = argp[idx].data.str.area;
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100833 px = p;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100834 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100835 argp[idx].data.srv = findserver(px, sname);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200836 if (!argp[idx].data.srv) {
837 msg = "server doesn't exist";
838 goto error;
839 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100840 argp[idx].type = ARGT_SRV;
841 break;
842
843 case ARGT_IPV4:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200844 if (argp[idx].type != ARGT_STR) {
845 msg = "string expected";
846 goto error;
847 }
848 if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) {
849 msg = "invalid IPv4 address";
850 goto error;
851 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100852 argp[idx].type = ARGT_IPV4;
853 break;
854
855 case ARGT_MSK4:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200856 if (argp[idx].type == ARGT_SINT)
857 len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4);
858 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200859 if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) {
860 msg = "invalid IPv4 mask";
861 goto error;
862 }
863 }
864 else {
865 msg = "integer or string expected";
866 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200867 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100868 argp[idx].type = ARGT_MSK4;
869 break;
870
871 case ARGT_IPV6:
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200872 if (argp[idx].type != ARGT_STR) {
873 msg = "string expected";
874 goto error;
875 }
876 if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) {
877 msg = "invalid IPv6 address";
878 goto error;
879 }
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100880 argp[idx].type = ARGT_IPV6;
881 break;
882
883 case ARGT_MSK6:
Christopher Faulete663a6e2020-08-07 09:11:22 +0200884 if (argp[idx].type == ARGT_SINT)
885 len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6);
886 else if (argp[idx].type == ARGT_STR) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200887 if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) {
888 msg = "invalid IPv6 mask";
889 goto error;
890 }
891 }
892 else {
893 msg = "integer or string expected";
894 goto error;
Christopher Faulete663a6e2020-08-07 09:11:22 +0200895 }
Tim Duesterhusb814da62018-01-25 16:24:50 +0100896 argp[idx].type = ARGT_MSK6;
897 break;
898
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200899 case ARGT_REG:
900 if (argp[idx].type != ARGT_STR) {
901 msg = "string expected";
902 goto error;
903 }
904 reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err);
905 if (!reg) {
906 msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'",
907 argp[idx].data.str.area, err);
908 free(err);
909 goto error;
910 }
911 argp[idx].type = ARGT_REG;
912 argp[idx].data.reg = reg;
913 break;
914
Thierry FOURNIER7f4942a2015-03-12 18:28:50 +0100915 case ARGT_USR:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200916 if (argp[idx].type != ARGT_STR) {
917 msg = "string expected";
918 goto error;
919 }
920 if (p->uri_auth && p->uri_auth->userlist &&
Tim Duesterhuse5ff1412021-01-02 22:31:53 +0100921 strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0)
Christopher Fauletd25d9262020-08-06 11:04:46 +0200922 ul = p->uri_auth->userlist;
923 else
924 ul = auth_find_userlist(argp[idx].data.str.area);
925
926 if (!ul) {
927 msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area);
928 goto error;
929 }
930 argp[idx].type = ARGT_USR;
931 argp[idx].data.usr = ul;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200932 break;
933
934 case ARGT_STR:
935 if (!chunk_dup(&tmp, &argp[idx].data.str)) {
936 msg = "unable to duplicate string arg";
937 goto error;
938 }
939 argp[idx].data.str = tmp;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100940 break;
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200941
Christopher Fauletd25d9262020-08-06 11:04:46 +0200942 case ARGT_MAP:
Christopher Fauletd25d9262020-08-06 11:04:46 +0200943 msg = "type not yet supported";
944 goto error;
945 break;
946
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100947 }
948
949 /* Check for type of argument. */
950 if ((mask & ARGT_MASK) != argp[idx].type) {
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200951 msg = lua_pushfstring(L, "'%s' expected, got '%s'",
952 arg_type_names[(mask & ARGT_MASK)],
953 arg_type_names[argp[idx].type & ARGT_MASK]);
954 goto error;
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100955 }
956
957 /* Next argument. */
958 mask >>= ARGT_BITS;
959 idx++;
960 }
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200961 return 0;
962
963 error:
964 for (i = 0; i < idx; i++) {
965 if (argp[i].type == ARGT_STR)
966 chunk_destroy(&argp[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +0200967 else if (argp[i].type == ARGT_REG)
968 regex_free(argp[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +0200969 }
970 WILL_LJMP(luaL_argerror(L, first + idx, msg));
971 return 0; /* Never reached */
Thierry FOURNIER55da1652015-01-23 11:36:30 +0100972}
973
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100974/*
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +0500975 * The following functions are used to make correspondence between the the
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100976 * executed lua pointer and the "struct hlua *" that contain the context.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100977 *
978 * - hlua_gethlua : return the hlua context associated with an lua_State.
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100979 * - hlua_sethlua : create the association between hlua context and lua_state.
980 */
981static inline struct hlua *hlua_gethlua(lua_State *L)
982{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100983 struct hlua **hlua = lua_getextraspace(L);
984 return *hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100985}
986static inline void hlua_sethlua(struct hlua *hlua)
987{
Thierry FOURNIER38c5fd62015-03-10 02:40:29 +0100988 struct hlua **hlua_store = lua_getextraspace(hlua->T);
989 *hlua_store = hlua;
Thierry FOURNIER380d0932015-01-23 14:27:52 +0100990}
991
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +0100992/* This function is used to send logs. It try to send on screen (stderr)
993 * and on the default syslog server.
994 */
995static inline void hlua_sendlog(struct proxy *px, int level, const char *msg)
996{
997 struct tm tm;
998 char *p;
999
1000 /* Cleanup the log message. */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001001 p = trash.area;
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001002 for (; *msg != '\0'; msg++, p++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001003 if (p >= trash.area + trash.size - 1) {
Thierry FOURNIERccf00632015-09-16 12:47:03 +02001004 /* Break the message if exceed the buffer size. */
1005 *(p-4) = ' ';
1006 *(p-3) = '.';
1007 *(p-2) = '.';
1008 *(p-1) = '.';
1009 break;
1010 }
Willy Tarreau90807112020-02-25 08:16:33 +01001011 if (isprint((unsigned char)*msg))
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001012 *p = *msg;
1013 else
1014 *p = '.';
1015 }
1016 *p = '\0';
1017
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001018 send_log(px, level, "%s\n", trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001019 if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) {
Christopher Fauletf98d8212020-10-02 18:13:52 +02001020 if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG))
1021 return;
1022
Willy Tarreaua678b432015-08-28 10:14:59 +02001023 get_localtime(date.tv_sec, &tm);
1024 fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n",
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001025 log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec,
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001026 (int)getpid(), trash.area);
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01001027 fflush(stderr);
1028 }
1029}
1030
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001031/* This function just ensure that the yield will be always
1032 * returned with a timeout and permit to set some flags
1033 */
1034__LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx,
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001035 lua_KFunction k, int timeout, unsigned int flags)
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001036{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001037 struct hlua *hlua;
1038
1039 /* Get hlua struct, or NULL if we execute from main lua state */
1040 hlua = hlua_gethlua(L);
1041 if (!hlua) {
1042 return;
1043 }
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001044
1045 /* Set the wake timeout. If timeout is required, we set
1046 * the expiration time.
1047 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001048 hlua->wake_time = timeout;
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001049
Thierry FOURNIER4abd3ae2015-03-03 17:29:06 +01001050 hlua->flags |= flags;
1051
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001052 /* Process the yield. */
Willy Tarreau9635e032018-10-16 17:52:55 +02001053 MAY_LJMP(lua_yieldk(L, nresults, ctx, k));
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001054}
1055
Willy Tarreau87b09662015-04-03 00:22:06 +02001056/* This function initialises the Lua environment stored in the stream.
1057 * It must be called at the start of the stream. This function creates
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001058 * an LUA coroutine. It can not be use to crete the main LUA context.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001059 *
1060 * This function is particular. it initialises a new Lua thread. If the
1061 * initialisation fails (example: out of memory error), the lua function
1062 * throws an error (longjmp).
1063 *
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001064 * In some case (at least one), this function can be called from safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001065 * environment, so we must not initialise it. While the support of
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001066 * threads appear, the safe environment set a lock to ensure only one
1067 * Lua execution at a time. If we initialize safe environment in another
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001068 * safe environment, we have a dead lock.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001069 *
1070 * set "already_safe" true if the context is initialized form safe
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001071 * Lua function.
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001072 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001073 * This function manipulates two Lua stacks: the main and the thread. Only
Thierry FOURNIERbabae282015-09-17 11:36:37 +02001074 * the main stack can fail. The thread is not manipulated. This function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001075 * MUST NOT manipulate the created thread stack state, because it is not
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001076 * protected against errors thrown by the thread stack.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001077 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001078int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe)
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001079{
1080 lua->Mref = LUA_REFNIL;
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001081 lua->flags = 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001082 lua->gc_count = 0;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001083 lua->wake_time = TICK_ETERNITY;
Thierry Fournier021d9862020-11-28 23:42:03 +01001084 lua->state_id = state_id;
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001085 LIST_INIT(&lua->com);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001086 if (!already_safe) {
1087 if (!SET_SAFE_LJMP_PARENT(lua)) {
1088 lua->Tref = LUA_REFNIL;
1089 return 0;
1090 }
1091 }
Thierry Fournier021d9862020-11-28 23:42:03 +01001092 lua->T = lua_newthread(hlua_states[state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001093 if (!lua->T) {
1094 lua->Tref = LUA_REFNIL;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001095 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001096 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001097 return 0;
1098 }
1099 hlua_sethlua(lua);
Thierry Fournier021d9862020-11-28 23:42:03 +01001100 lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001101 lua->task = task;
Thierry FOURNIERbf90ce12019-01-06 19:04:24 +01001102 if (!already_safe)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001103 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001104 return 1;
1105}
1106
Willy Tarreau87b09662015-04-03 00:22:06 +02001107/* Used to destroy the Lua coroutine when the attached stream or task
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001108 * is destroyed. The destroy also the memory context. The struct "lua"
1109 * is not freed.
1110 */
1111void hlua_ctx_destroy(struct hlua *lua)
1112{
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001113 if (!lua)
Thierry FOURNIERa718b292015-03-04 16:48:34 +01001114 return;
1115
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001116 if (!lua->T)
1117 goto end;
1118
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001119 /* Purge all the pending signals. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001120 notification_purge(&lua->com);
Thierry FOURNIER9ff7e6e2015-01-23 11:08:20 +01001121
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001122 if (!SET_SAFE_LJMP(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001123 return;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001124 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001125 RESET_SAFE_LJMP(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001126
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001127 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001128 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001129 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001130 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001131 /* Forces a garbage collecting process. If the Lua program is finished
1132 * without error, we run the GC on the thread pointer. Its freed all
1133 * the unused memory.
1134 * If the thread is finnish with an error or is currently yielded,
1135 * it seems that the GC applied on the thread doesn't clean anything,
1136 * so e run the GC on the main thread.
1137 * NOTE: maybe this action locks all the Lua threads untiml the en of
1138 * the garbage collection.
1139 */
Willy Tarreauf31af932020-01-14 09:59:38 +01001140 if (lua->gc_count) {
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001141 if (!SET_SAFE_LJMP_PARENT(lua))
Thierry FOURNIER75d02082017-07-12 13:41:33 +02001142 return;
Thierry Fournier021d9862020-11-28 23:42:03 +01001143 lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001144 RESET_SAFE_LJMP_PARENT(lua);
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02001145 }
Thierry FOURNIER5a50a852015-09-23 16:59:28 +02001146
Thierry FOURNIERa7b536b2015-09-21 22:50:24 +02001147 lua->T = NULL;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01001148
1149end:
Willy Tarreaubafbe012017-11-24 17:34:44 +01001150 pool_free(pool_head_hlua, lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001151}
1152
1153/* This function is used to restore the Lua context when a coroutine
1154 * fails. This function copy the common memory between old coroutine
1155 * and the new coroutine. The old coroutine is destroyed, and its
1156 * replaced by the new coroutine.
1157 * If the flag "keep_msg" is set, the last entry of the old is assumed
1158 * as string error message and it is copied in the new stack.
1159 */
1160static int hlua_ctx_renew(struct hlua *lua, int keep_msg)
1161{
1162 lua_State *T;
1163 int new_ref;
1164
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001165 /* New Lua coroutine. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001166 T = lua_newthread(hlua_states[lua->state_id]);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001167 if (!T)
1168 return 0;
1169
1170 /* Copy last error message. */
1171 if (keep_msg)
1172 lua_xmove(lua->T, T, 1);
1173
1174 /* Copy data between the coroutines. */
1175 lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1176 lua_xmove(lua->T, T, 1);
Ilya Shipitsin46a030c2020-07-05 16:36:08 +05001177 new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001178
1179 /* Destroy old data. */
1180 luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref);
1181
1182 /* The thread is garbage collected by Lua. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001183 luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001184
1185 /* Fill the struct with the new coroutine values. */
1186 lua->Mref = new_ref;
1187 lua->T = T;
Thierry Fournier021d9862020-11-28 23:42:03 +01001188 lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001189
1190 /* Set context. */
1191 hlua_sethlua(lua);
1192
1193 return 1;
1194}
1195
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001196void hlua_hook(lua_State *L, lua_Debug *ar)
1197{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001198 struct hlua *hlua;
1199
1200 /* Get hlua struct, or NULL if we execute from main lua state */
1201 hlua = hlua_gethlua(L);
1202 if (!hlua)
1203 return;
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001204
1205 /* Lua cannot yield when its returning from a function,
1206 * so, we can fix the interrupt hook to 1 instruction,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001207 * expecting that the function is finished.
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001208 */
1209 if (lua_gethookmask(L) & LUA_MASKRET) {
1210 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1);
1211 return;
1212 }
1213
1214 /* restore the interrupt condition. */
1215 lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1216
1217 /* If we interrupt the Lua processing in yieldable state, we yield.
1218 * If the state is not yieldable, trying yield causes an error.
1219 */
1220 if (lua_isyieldable(L))
Willy Tarreau9635e032018-10-16 17:52:55 +02001221 MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001222
Thierry FOURNIERa85cfb12015-03-13 14:50:06 +01001223 /* If we cannot yield, update the clock and check the timeout. */
1224 tv_update_date(0, 1);
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001225 hlua->run_time += now_ms - hlua->start_time;
1226 if (hlua->max_time && hlua->run_time >= hlua->max_time) {
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001227 lua_pushfstring(L, "execution timeout");
1228 WILL_LJMP(lua_error(L));
1229 }
1230
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001231 /* Update the start time. */
1232 hlua->start_time = now_ms;
1233
Thierry FOURNIERcae49c92015-03-06 14:05:24 +01001234 /* Try to interrupt the process at the end of the current
1235 * unyieldable function.
1236 */
1237 lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001238}
1239
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001240/* This function start or resumes the Lua stack execution. If the flag
1241 * "yield_allowed" if no set and the LUA stack execution returns a yield
1242 * The function return an error.
1243 *
1244 * The function can returns 4 values:
1245 * - HLUA_E_OK : The execution is terminated without any errors.
1246 * - HLUA_E_AGAIN : The execution must continue at the next associated
1247 * task wakeup.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001248 * - HLUA_E_ERRMSG : An error has occurred, an error message is set in
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001249 * the top of the stack.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001250 * - HLUA_E_ERR : An error has occurred without error message.
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001251 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001252 * If an error occurred, the stack is renewed and it is ready to run new
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001253 * LUA code.
1254 */
1255static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed)
1256{
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001257#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
1258 int nres;
1259#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001260 int ret;
1261 const char *msg;
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001262 const char *trace;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001263
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001264 /* Initialise run time counter. */
1265 if (!HLUA_IS_RUNNING(lua))
1266 lua->run_time = 0;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001267
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001268 /* Lock the whole Lua execution. This lock must be before the
1269 * label "resume_execution".
1270 */
Thierry Fournier021d9862020-11-28 23:42:03 +01001271 if (lua->state_id == 0)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001272 HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001273
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001274resume_execution:
1275
1276 /* This hook interrupts the Lua processing each 'hlua_nb_instruction'
1277 * instructions. it is used for preventing infinite loops.
1278 */
1279 lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction);
1280
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001281 /* Remove all flags except the running flags. */
Thierry FOURNIER2f3867f2015-09-28 01:02:01 +02001282 HLUA_SET_RUN(lua);
1283 HLUA_CLR_CTRLYIELD(lua);
1284 HLUA_CLR_WAKERESWR(lua);
1285 HLUA_CLR_WAKEREQWR(lua);
Thierry FOURNIER1bfc09b2015-03-05 17:10:14 +01001286
Christopher Fauletbc275a92020-02-26 14:55:16 +01001287 /* Update the start time and reset wake_time. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001288 lua->start_time = now_ms;
Christopher Fauletbc275a92020-02-26 14:55:16 +01001289 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001290
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001291 /* Call the function. */
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001292#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournier021d9862020-11-28 23:42:03 +01001293 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001294#else
Thierry Fournier021d9862020-11-28 23:42:03 +01001295 ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs);
Christopher Faulet08ed98f2020-07-28 10:33:25 +02001296#endif
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001297 switch (ret) {
1298
1299 case LUA_OK:
1300 ret = HLUA_E_OK;
1301 break;
1302
1303 case LUA_YIELD:
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001304 /* Check if the execution timeout is expired. It it is the case, we
1305 * break the Lua execution.
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001306 */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02001307 tv_update_date(0, 1);
1308 lua->run_time += now_ms - lua->start_time;
1309 if (lua->max_time && lua->run_time > lua->max_time) {
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001310 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001311 ret = HLUA_E_ETMOUT;
Thierry FOURNIERdc9ca962015-03-05 11:16:52 +01001312 break;
1313 }
1314 /* Process the forced yield. if the general yield is not allowed or
1315 * if no task were associated this the current Lua execution
1316 * coroutine, we resume the execution. Else we want to return in the
1317 * scheduler and we want to be waked up again, to continue the
1318 * current Lua execution. So we schedule our own task.
1319 */
1320 if (HLUA_IS_CTRLYIELDING(lua)) {
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001321 if (!yield_allowed || !lua->task)
1322 goto resume_execution;
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001323 task_wakeup(lua->task, TASK_WOKEN_MSG);
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01001324 }
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001325 if (!yield_allowed) {
1326 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001327 ret = HLUA_E_YIELD;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001328 break;
1329 }
1330 ret = HLUA_E_AGAIN;
1331 break;
1332
1333 case LUA_ERRRUN:
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001334
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001335 /* Special exit case. The traditional exit is returned as an error
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001336 * because the errors ares the only one mean to return immediately
1337 * from and lua execution.
1338 */
1339 if (lua->flags & HLUA_EXIT) {
1340 ret = HLUA_E_OK;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01001341 hlua_ctx_renew(lua, 1);
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001342 break;
1343 }
1344
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001345 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001346 if (!lua_checkstack(lua->T, 1)) {
1347 ret = HLUA_E_ERR;
1348 break;
1349 }
1350 msg = lua_tostring(lua->T, -1);
1351 lua_settop(lua->T, 0); /* Empty the stack. */
1352 lua_pop(lua->T, 1);
Thierry FOURNIERfc044c92018-06-07 14:40:48 +02001353 trace = hlua_traceback(lua->T);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001354 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001355 lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001356 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001357 lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001358 ret = HLUA_E_ERRMSG;
1359 break;
1360
1361 case LUA_ERRMEM:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001362 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001363 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001364 ret = HLUA_E_NOMEM;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001365 break;
1366
1367 case LUA_ERRERR:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001368 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001369 if (!lua_checkstack(lua->T, 1)) {
1370 ret = HLUA_E_ERR;
1371 break;
1372 }
1373 msg = lua_tostring(lua->T, -1);
1374 lua_settop(lua->T, 0); /* Empty the stack. */
1375 lua_pop(lua->T, 1);
1376 if (msg)
Thierry Fournier46278ff2020-11-29 11:48:12 +01001377 lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001378 else
Thierry Fournier46278ff2020-11-29 11:48:12 +01001379 lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001380 ret = HLUA_E_ERRMSG;
1381 break;
1382
1383 default:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01001384 lua->wake_time = TICK_ETERNITY;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001385 lua_settop(lua->T, 0); /* Empty the stack. */
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001386 ret = HLUA_E_ERR;
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001387 break;
1388 }
1389
1390 switch (ret) {
1391 case HLUA_E_AGAIN:
1392 break;
1393
1394 case HLUA_E_ERRMSG:
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001395 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001396 hlua_ctx_renew(lua, 1);
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001397 HLUA_CLR_RUN(lua);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001398 break;
1399
Thierry Fournierd5b073c2018-05-21 19:42:47 +02001400 case HLUA_E_ETMOUT:
1401 case HLUA_E_NOMEM:
1402 case HLUA_E_YIELD:
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001403 case HLUA_E_ERR:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001404 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001405 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001406 hlua_ctx_renew(lua, 0);
1407 break;
1408
1409 case HLUA_E_OK:
Thierry FOURNIERa097fdf2015-03-03 15:17:35 +01001410 HLUA_CLR_RUN(lua);
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001411 notification_purge(&lua->com);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001412 break;
1413 }
1414
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001415 /* This is the main exit point, remove the Lua lock. */
Thierry Fournier021d9862020-11-28 23:42:03 +01001416 if (lua->state_id == 0)
Thierry Fournier7cbe5042020-11-28 17:02:21 +01001417 HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock);
Thierry FOURNIER61ba0e22017-07-12 11:41:21 +02001418
Thierry FOURNIER380d0932015-01-23 14:27:52 +01001419 return ret;
1420}
1421
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001422/* This function exit the current code. */
1423__LJMP static int hlua_done(lua_State *L)
1424{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001425 struct hlua *hlua;
1426
1427 /* Get hlua struct, or NULL if we execute from main lua state */
1428 hlua = hlua_gethlua(L);
1429 if (!hlua)
1430 return 0;
Thierry FOURNIER0a99b892015-08-26 00:14:17 +02001431
1432 hlua->flags |= HLUA_EXIT;
1433 WILL_LJMP(lua_error(L));
1434
1435 return 0;
1436}
1437
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001438/* This function is an LUA binding. It provides a function
1439 * for deleting ACL from a referenced ACL file.
1440 */
1441__LJMP static int hlua_del_acl(lua_State *L)
1442{
1443 const char *name;
1444 const char *key;
1445 struct pat_ref *ref;
1446
1447 MAY_LJMP(check_args(L, 2, "del_acl"));
1448
1449 name = MAY_LJMP(luaL_checkstring(L, 1));
1450 key = MAY_LJMP(luaL_checkstring(L, 2));
1451
1452 ref = pat_ref_lookup(name);
1453 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001454 WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001455
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001456 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001457 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001458 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001459 return 0;
1460}
1461
1462/* This function is an LUA binding. It provides a function
1463 * for deleting map entry from a referenced map file.
1464 */
1465static int hlua_del_map(lua_State *L)
1466{
1467 const char *name;
1468 const char *key;
1469 struct pat_ref *ref;
1470
1471 MAY_LJMP(check_args(L, 2, "del_map"));
1472
1473 name = MAY_LJMP(luaL_checkstring(L, 1));
1474 key = MAY_LJMP(luaL_checkstring(L, 2));
1475
1476 ref = pat_ref_lookup(name);
1477 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001478 WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001479
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001480 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001481 pat_ref_delete(ref, key);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001482 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001483 return 0;
1484}
1485
1486/* This function is an LUA binding. It provides a function
1487 * for adding ACL pattern from a referenced ACL file.
1488 */
1489static int hlua_add_acl(lua_State *L)
1490{
1491 const char *name;
1492 const char *key;
1493 struct pat_ref *ref;
1494
1495 MAY_LJMP(check_args(L, 2, "add_acl"));
1496
1497 name = MAY_LJMP(luaL_checkstring(L, 1));
1498 key = MAY_LJMP(luaL_checkstring(L, 2));
1499
1500 ref = pat_ref_lookup(name);
1501 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001502 WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001503
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001504 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001505 if (pat_ref_find_elt(ref, key) == NULL)
1506 pat_ref_add(ref, key, NULL, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001507 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001508 return 0;
1509}
1510
1511/* This function is an LUA binding. It provides a function
1512 * for setting map pattern and sample from a referenced map
1513 * file.
1514 */
1515static int hlua_set_map(lua_State *L)
1516{
1517 const char *name;
1518 const char *key;
1519 const char *value;
1520 struct pat_ref *ref;
1521
1522 MAY_LJMP(check_args(L, 3, "set_map"));
1523
1524 name = MAY_LJMP(luaL_checkstring(L, 1));
1525 key = MAY_LJMP(luaL_checkstring(L, 2));
1526 value = MAY_LJMP(luaL_checkstring(L, 3));
1527
1528 ref = pat_ref_lookup(name);
1529 if (!ref)
Vincent Bernata72db182015-10-06 16:05:59 +02001530 WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name));
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001531
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001532 HA_SPIN_LOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001533 if (pat_ref_find_elt(ref, key) != NULL)
1534 pat_ref_set(ref, key, value, NULL);
1535 else
1536 pat_ref_add(ref, key, value, NULL);
Christopher Faulet5cf2dfc2020-06-03 18:39:16 +02001537 HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock);
Thierry FOURNIER83758bb2015-02-04 13:21:04 +01001538 return 0;
1539}
1540
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001541/* A class is a lot of memory that contain data. This data can be a table,
1542 * an integer or user data. This data is associated with a metatable. This
Ilya Shipitsinc02a23f2020-05-06 00:53:22 +05001543 * metatable have an original version registered in the global context with
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001544 * the name of the object (_G[<name>] = <metable> ).
1545 *
1546 * A metable is a table that modify the standard behavior of a standard
1547 * access to the associated data. The entries of this new metatable are
1548 * defined as is:
1549 *
1550 * http://lua-users.org/wiki/MetatableEvents
1551 *
1552 * __index
1553 *
1554 * we access an absent field in a table, the result is nil. This is
1555 * true, but it is not the whole truth. Actually, such access triggers
1556 * the interpreter to look for an __index metamethod: If there is no
1557 * such method, as usually happens, then the access results in nil;
1558 * otherwise, the metamethod will provide the result.
1559 *
1560 * Control 'prototype' inheritance. When accessing "myTable[key]" and
1561 * the key does not appear in the table, but the metatable has an __index
1562 * property:
1563 *
1564 * - if the value is a function, the function is called, passing in the
1565 * table and the key; the return value of that function is returned as
1566 * the result.
1567 *
1568 * - if the value is another table, the value of the key in that table is
1569 * asked for and returned (and if it doesn't exist in that table, but that
1570 * table's metatable has an __index property, then it continues on up)
1571 *
1572 * - Use "rawget(myTable,key)" to skip this metamethod.
1573 *
1574 * http://www.lua.org/pil/13.4.1.html
1575 *
1576 * __newindex
1577 *
1578 * Like __index, but control property assignment.
1579 *
1580 * __mode - Control weak references. A string value with one or both
1581 * of the characters 'k' and 'v' which specifies that the the
1582 * keys and/or values in the table are weak references.
1583 *
1584 * __call - Treat a table like a function. When a table is followed by
1585 * parenthesis such as "myTable( 'foo' )" and the metatable has
1586 * a __call key pointing to a function, that function is invoked
1587 * (passing any specified arguments) and the return value is
1588 * returned.
1589 *
1590 * __metatable - Hide the metatable. When "getmetatable( myTable )" is
1591 * called, if the metatable for myTable has a __metatable
1592 * key, the value of that key is returned instead of the
1593 * actual metatable.
1594 *
1595 * __tostring - Control string representation. When the builtin
1596 * "tostring( myTable )" function is called, if the metatable
1597 * for myTable has a __tostring property set to a function,
1598 * that function is invoked (passing myTable to it) and the
1599 * return value is used as the string representation.
1600 *
1601 * __len - Control table length. When the table length is requested using
1602 * the length operator ( '#' ), if the metatable for myTable has
1603 * a __len key pointing to a function, that function is invoked
1604 * (passing myTable to it) and the return value used as the value
1605 * of "#myTable".
1606 *
1607 * __gc - Userdata finalizer code. When userdata is set to be garbage
1608 * collected, if the metatable has a __gc field pointing to a
1609 * function, that function is first invoked, passing the userdata
1610 * to it. The __gc metamethod is not called for tables.
1611 * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html)
1612 *
1613 * Special metamethods for redefining standard operators:
1614 * http://www.lua.org/pil/13.1.html
1615 *
1616 * __add "+"
1617 * __sub "-"
1618 * __mul "*"
1619 * __div "/"
1620 * __unm "!"
1621 * __pow "^"
1622 * __concat ".."
1623 *
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05001624 * Special methods for redefining standard relations
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01001625 * http://www.lua.org/pil/13.2.html
1626 *
1627 * __eq "=="
1628 * __lt "<"
1629 * __le "<="
1630 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001631
1632/*
1633 *
1634 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001635 * Class Map
1636 *
1637 *
1638 */
1639
1640/* Returns a struct hlua_map if the stack entry "ud" is
1641 * a class session, otherwise it throws an error.
1642 */
1643__LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud)
1644{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001645 return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001646}
1647
1648/* This function is the map constructor. It don't need
1649 * the class Map object. It creates and return a new Map
1650 * object. It must be called only during "body" or "init"
1651 * context because it process some filesystem accesses.
1652 */
1653__LJMP static int hlua_map_new(struct lua_State *L)
1654{
1655 const char *fn;
1656 int match = PAT_MATCH_STR;
1657 struct sample_conv conv;
1658 const char *file = "";
1659 int line = 0;
1660 lua_Debug ar;
1661 char *err = NULL;
1662 struct arg args[2];
1663
1664 if (lua_gettop(L) < 1 || lua_gettop(L) > 2)
1665 WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument."));
1666
1667 fn = MAY_LJMP(luaL_checkstring(L, 1));
1668
1669 if (lua_gettop(L) >= 2) {
1670 match = MAY_LJMP(luaL_checkinteger(L, 2));
1671 if (match < 0 || match >= PAT_MATCH_NUM)
1672 WILL_LJMP(luaL_error(L, "'new' needs a valid match method."));
1673 }
1674
1675 /* Get Lua filename and line number. */
1676 if (lua_getstack(L, 1, &ar)) { /* check function at level */
1677 lua_getinfo(L, "Sl", &ar); /* get info about it */
1678 if (ar.currentline > 0) { /* is there info? */
1679 file = ar.short_src;
1680 line = ar.currentline;
1681 }
1682 }
1683
1684 /* fill fake sample_conv struct. */
1685 conv.kw = ""; /* unused. */
1686 conv.process = NULL; /* unused. */
1687 conv.arg_mask = 0; /* unused. */
1688 conv.val_args = NULL; /* unused. */
1689 conv.out_type = SMP_T_STR;
1690 conv.private = (void *)(long)match;
1691 switch (match) {
1692 case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break;
1693 case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break;
1694 case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break;
1695 case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break;
1696 case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break;
1697 case PAT_MATCH_END: conv.in_type = SMP_T_STR; break;
1698 case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break;
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001699 case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001700 case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break;
1701 default:
1702 WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode."));
1703 }
1704
1705 /* fill fake args. */
1706 args[0].type = ARGT_STR;
Christopher Faulet73292e92020-08-06 08:40:09 +02001707 args[0].data.str.area = strdup(fn);
1708 args[0].data.str.data = strlen(fn);
1709 args[0].data.str.size = args[0].data.str.data+1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001710 args[1].type = ARGT_STOP;
1711
1712 /* load the map. */
1713 if (!sample_load_map(args, &conv, file, line, &err)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001714 /* error case: we can't use luaL_error because we must
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001715 * free the err variable.
1716 */
1717 luaL_where(L, 1);
1718 lua_pushfstring(L, "'new': %s.", err);
1719 lua_concat(L, 2);
1720 free(err);
Christopher Faulet6ad7df42020-08-07 11:45:18 +02001721 chunk_destroy(&args[0].data.str);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001722 WILL_LJMP(lua_error(L));
1723 }
1724
1725 /* create the lua object. */
1726 lua_newtable(L);
1727 lua_pushlightuserdata(L, args[0].data.map);
1728 lua_rawseti(L, -2, 0);
1729
1730 /* Pop a class Map metatable and affect it to the userdata. */
1731 lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref);
1732 lua_setmetatable(L, -2);
1733
1734
1735 return 1;
1736}
1737
1738__LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str)
1739{
1740 struct map_descriptor *desc;
1741 struct pattern *pat;
1742 struct sample smp;
1743
1744 MAY_LJMP(check_args(L, 2, "lookup"));
1745 desc = MAY_LJMP(hlua_checkmap(L, 1));
Thierry FOURNIER07ee64e2015-07-06 23:43:03 +02001746 if (desc->pat.expect_type == SMP_T_SINT) {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001747 smp.data.type = SMP_T_SINT;
Thierry FOURNIER136f9d32015-08-19 09:07:19 +02001748 smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001749 }
1750 else {
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02001751 smp.data.type = SMP_T_STR;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001752 smp.flags = SMP_F_CONST;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001753 smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data));
Thierry Fournier91dc0c02020-11-10 20:38:20 +01001754 smp.data.u.str.size = smp.data.u.str.data + 1;
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001755 }
1756
1757 pat = pattern_exec_match(&desc->pat, &smp, 1);
Thierry FOURNIER503bb092015-08-19 08:35:43 +02001758 if (!pat || !pat->data) {
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001759 if (str)
1760 lua_pushstring(L, "");
1761 else
1762 lua_pushnil(L);
1763 return 1;
1764 }
1765
1766 /* The Lua pattern must return a string, so we can't check the returned type */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02001767 lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02001768 return 1;
1769}
1770
1771__LJMP static int hlua_map_lookup(struct lua_State *L)
1772{
1773 return _hlua_map_lookup(L, 0);
1774}
1775
1776__LJMP static int hlua_map_slookup(struct lua_State *L)
1777{
1778 return _hlua_map_lookup(L, 1);
1779}
1780
1781/*
1782 *
1783 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001784 * Class Socket
1785 *
1786 *
1787 */
1788
1789__LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud)
1790{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02001791 return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001792}
1793
1794/* This function is the handler called for each I/O on the established
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001795 * connection. It is used for notify space available to send or data
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001796 * received.
1797 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001798static void hlua_socket_handler(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001799{
Willy Tarreau00a37f02015-04-13 12:05:19 +02001800 struct stream_interface *si = appctx->owner;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001801
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001802 if (appctx->ctx.hlua_cosocket.die) {
1803 si_shutw(si);
1804 si_shutr(si);
1805 si_ic(si)->flags |= CF_READ_NULL;
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001806 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1807 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001808 stream_shutdown(si_strm(si), SF_ERR_KILLED);
1809 }
1810
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001811 /* If we can't write, wakeup the pending write signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001812 if (channel_output_closed(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001813 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001814
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05001815 /* If we can't read, wakeup the pending read signals. */
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001816 if (channel_input_closed(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001817 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER6d695e62015-09-27 19:29:38 +02001818
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001819 /* if the connection is not established, inform the stream that we want
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001820 * to be notified whenever the connection completes.
1821 */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02001822 if (si_opposite(si)->state < SI_ST_EST) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01001823 si_cant_get(si);
Willy Tarreau12c24232018-12-06 15:29:50 +01001824 si_rx_conn_blk(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01001825 si_rx_endp_more(si);
Willy Tarreaud4da1962015-04-20 01:31:23 +02001826 return;
Thierry FOURNIER316e3192015-09-04 18:25:53 +02001827 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001828
1829 /* This function is called after the connect. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01001830 appctx->ctx.hlua_cosocket.connected = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001831
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08001832 /* Wake the tasks which wants to write if the buffer have available space. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001833 if (channel_may_recv(si_ic(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001834 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001835
1836 /* Wake the tasks which wants to read if the buffer contains data. */
Thierry FOURNIEReba6f642015-09-26 22:01:07 +02001837 if (!channel_is_empty(si_oc(si)))
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001838 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001839
1840 /* Some data were injected in the buffer, notify the stream
1841 * interface.
1842 */
1843 if (!channel_is_empty(si_ic(si)))
Willy Tarreau14bfe9a2018-12-19 15:19:27 +01001844 si_update(si);
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001845
1846 /* If write notifications are registered, we considers we want
Willy Tarreau3367d412018-11-15 10:57:41 +01001847 * to write, so we clear the blocking flag.
Thierry FOURNIER101b9762018-05-27 01:27:40 +02001848 */
1849 if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write))
Willy Tarreau3367d412018-11-15 10:57:41 +01001850 si_rx_endp_more(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001851}
1852
Willy Tarreau87b09662015-04-03 00:22:06 +02001853/* This function is called when the "struct stream" is destroyed.
1854 * Remove the link from the object to this stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001855 * Wake all the pending signals.
1856 */
Willy Tarreau00a37f02015-04-13 12:05:19 +02001857static void hlua_socket_release(struct appctx *appctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001858{
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001859 struct xref *peer;
1860
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001861 /* Remove my link in the original object. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001862 peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref);
1863 if (peer)
1864 xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001865
1866 /* Wake all the task waiting for me. */
Thierry FOURNIERd6975962017-07-12 14:31:10 +02001867 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read);
1868 notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001869}
1870
1871/* If the garbage collectio of the object is launch, nobody
Willy Tarreau87b09662015-04-03 00:22:06 +02001872 * uses this object. If the stream does not exists, just quit.
1873 * Send the shutdown signal to the stream. In some cases,
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001874 * pending signal can rest in the read and write lists. destroy
1875 * it.
1876 */
1877__LJMP static int hlua_socket_gc(lua_State *L)
1878{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001879 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001880 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001881 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001882
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001883 MAY_LJMP(check_args(L, 1, "__gc"));
1884
1885 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001886 peer = xref_get_peer_and_lock(&socket->xref);
1887 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001888 return 0;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001889 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001890
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001891 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001892 appctx->ctx.hlua_cosocket.die = 1;
1893 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001894
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001895 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001896 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001897 return 0;
1898}
1899
1900/* The close function send shutdown signal and break the
Willy Tarreau87b09662015-04-03 00:22:06 +02001901 * links between the stream and the object.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001902 */
sada05ed3302018-05-11 11:48:18 -07001903__LJMP static int hlua_socket_close_helper(lua_State *L)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001904{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001905 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001906 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001907 struct xref *peer;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001908 struct hlua *hlua;
1909
1910 /* Get hlua struct, or NULL if we execute from main lua state */
1911 hlua = hlua_gethlua(L);
1912 if (!hlua)
1913 return 0;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001914
Willy Tarreau80f5fae2015-02-27 16:38:20 +01001915 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001916
1917 /* Check if we run on the same thread than the xreator thread.
1918 * We cannot access to the socket if the thread is different.
1919 */
1920 if (socket->tid != tid)
1921 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1922
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001923 peer = xref_get_peer_and_lock(&socket->xref);
1924 if (!peer)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001925 return 0;
Willy Tarreauf31af932020-01-14 09:59:38 +01001926
1927 hlua->gc_count--;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001928 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001929
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001930 /* Set the flag which destroy the session. */
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02001931 appctx->ctx.hlua_cosocket.die = 1;
1932 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001933
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001934 /* Remove all reference between the Lua stack and the coroutine stream. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001935 xref_disconnect(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001936 return 0;
1937}
1938
sada05ed3302018-05-11 11:48:18 -07001939/* The close function calls close_helper.
1940 */
1941__LJMP static int hlua_socket_close(lua_State *L)
1942{
1943 MAY_LJMP(check_args(L, 1, "close"));
1944 return hlua_socket_close_helper(L);
1945}
1946
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001947/* This Lua function assumes that the stack contain three parameters.
1948 * 1 - USERDATA containing a struct socket
1949 * 2 - INTEGER with values of the macro defined below
1950 * If the integer is -1, we must read at most one line.
1951 * If the integer is -2, we ust read all the data until the
1952 * end of the stream.
1953 * If the integer is positive value, we must read a number of
1954 * bytes corresponding to this value.
1955 */
1956#define HLSR_READ_LINE (-1)
1957#define HLSR_READ_ALL (-2)
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01001958__LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001959{
1960 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
1961 int wanted = lua_tointeger(L, 2);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001962 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001963 struct appctx *appctx;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001964 size_t len;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001965 int nblk;
Willy Tarreau206ba832018-06-14 15:27:31 +02001966 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001967 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02001968 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02001969 size_t len2;
Thierry FOURNIER00543922015-03-09 18:35:06 +01001970 int skip_at_end = 0;
Willy Tarreau81389672015-03-10 12:03:52 +01001971 struct channel *oc;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001972 struct stream_interface *si;
1973 struct stream *s;
1974 struct xref *peer;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02001975 int missing_bytes;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001976
Thierry Fournier4234dbd2020-11-28 13:18:23 +01001977 /* Get hlua struct, or NULL if we execute from main lua state */
1978 hlua = hlua_gethlua(L);
1979
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001980 /* Check if this lua stack is schedulable. */
1981 if (!hlua || !hlua->task)
1982 WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in "
1983 "'frontend', 'backend' or 'task'"));
1984
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02001985 /* Check if we run on the same thread than the xreator thread.
1986 * We cannot access to the socket if the thread is different.
1987 */
1988 if (socket->tid != tid)
1989 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
1990
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001991 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02001992 peer = xref_get_peer_and_lock(&socket->xref);
1993 if (!peer)
1994 goto no_peer;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001995 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
1996 si = appctx->owner;
1997 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01001998
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02001999 oc = &s->res;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002000 if (wanted == HLSR_READ_LINE) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002001 /* Read line. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002002 nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002003 if (nblk < 0) /* Connection close. */
2004 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002005 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002006 goto connection_empty;
Thierry FOURNIER00543922015-03-09 18:35:06 +01002007
2008 /* remove final \r\n. */
2009 if (nblk == 1) {
2010 if (blk1[len1-1] == '\n') {
2011 len1--;
2012 skip_at_end++;
2013 if (blk1[len1-1] == '\r') {
2014 len1--;
2015 skip_at_end++;
2016 }
2017 }
2018 }
2019 else {
2020 if (blk2[len2-1] == '\n') {
2021 len2--;
2022 skip_at_end++;
2023 if (blk2[len2-1] == '\r') {
2024 len2--;
2025 skip_at_end++;
2026 }
2027 }
2028 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002029 }
2030
2031 else if (wanted == HLSR_READ_ALL) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002032 /* Read all the available data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002033 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002034 if (nblk < 0) /* Connection close. */
2035 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002036 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002037 goto connection_empty;
2038 }
2039
2040 else {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002041 /* Read a block of data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002042 nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002043 if (nblk < 0) /* Connection close. */
2044 goto connection_closed;
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002045 if (nblk == 0) /* No data available. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002046 goto connection_empty;
2047
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002048 missing_bytes = wanted - socket->b.n;
2049 if (len1 > missing_bytes) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002050 nblk = 1;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002051 len1 = missing_bytes;
2052 } if (nblk == 2 && len1 + len2 > missing_bytes)
2053 len2 = missing_bytes - len1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002054 }
2055
2056 len = len1;
2057
2058 luaL_addlstring(&socket->b, blk1, len1);
2059 if (nblk == 2) {
2060 len += len2;
2061 luaL_addlstring(&socket->b, blk2, len2);
2062 }
2063
2064 /* Consume data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02002065 co_skip(oc, len + skip_at_end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002066
2067 /* Don't wait anything. */
Thierry FOURNIER7e4ee472018-05-25 15:03:50 +02002068 appctx_wakeup(appctx);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002069
2070 /* If the pattern reclaim to read all the data
2071 * in the connection, got out.
2072 */
2073 if (wanted == HLSR_READ_ALL)
2074 goto connection_empty;
Thierry FOURNIER8c126c72018-05-25 16:27:44 +02002075 else if (wanted >= 0 && socket->b.n < wanted)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002076 goto connection_empty;
2077
2078 /* Return result. */
2079 luaL_pushresult(&socket->b);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002080 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002081 return 1;
2082
2083connection_closed:
2084
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002085 xref_unlock(&socket->xref, peer);
2086
2087no_peer:
2088
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002089 /* If the buffer containds data. */
2090 if (socket->b.n > 0) {
2091 luaL_pushresult(&socket->b);
2092 return 1;
2093 }
2094 lua_pushnil(L);
2095 lua_pushstring(L, "connection closed.");
2096 return 2;
2097
2098connection_empty:
2099
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002100 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) {
2101 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002102 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002103 }
2104 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002105 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002106 return 0;
2107}
2108
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002109/* This Lua function gets two parameters. The first one can be string
2110 * or a number. If the string is "*l", the user requires one line. If
2111 * the string is "*a", the user requires all the contents of the stream.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002112 * If the value is a number, the user require a number of bytes equal
2113 * to the value. The default value is "*l" (a line).
2114 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002115 * This parameter with a variable type is converted in integer. This
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002116 * integer takes this values:
2117 * -1 : read a line
2118 * -2 : read all the stream
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002119 * >0 : amount of bytes.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002120 *
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002121 * The second parameter is optional. It contains a string that must be
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002122 * concatenated with the read data.
2123 */
2124__LJMP static int hlua_socket_receive(struct lua_State *L)
2125{
2126 int wanted = HLSR_READ_LINE;
2127 const char *pattern;
2128 int type;
2129 char *error;
2130 size_t len;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002131 struct hlua_socket *socket;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002132
2133 if (lua_gettop(L) < 1 || lua_gettop(L) > 3)
2134 WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments."));
2135
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002136 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002137
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002138 /* Check if we run on the same thread than the xreator thread.
2139 * We cannot access to the socket if the thread is different.
2140 */
2141 if (socket->tid != tid)
2142 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2143
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002144 /* check for pattern. */
2145 if (lua_gettop(L) >= 2) {
2146 type = lua_type(L, 2);
2147 if (type == LUA_TSTRING) {
2148 pattern = lua_tostring(L, 2);
2149 if (strcmp(pattern, "*a") == 0)
2150 wanted = HLSR_READ_ALL;
2151 else if (strcmp(pattern, "*l") == 0)
2152 wanted = HLSR_READ_LINE;
2153 else {
2154 wanted = strtoll(pattern, &error, 10);
2155 if (*error != '\0')
2156 WILL_LJMP(luaL_error(L, "Unsupported pattern."));
2157 }
2158 }
2159 else if (type == LUA_TNUMBER) {
2160 wanted = lua_tointeger(L, 2);
2161 if (wanted < 0)
2162 WILL_LJMP(luaL_error(L, "Unsupported size."));
2163 }
2164 }
2165
2166 /* Set pattern. */
2167 lua_pushinteger(L, wanted);
Tim Duesterhusc6e377e2018-01-04 19:32:13 +01002168
2169 /* Check if we would replace the top by itself. */
2170 if (lua_gettop(L) != 2)
2171 lua_replace(L, 2);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002172
Tim Duesterhusb33754c2018-01-04 19:32:14 +01002173 /* init buffer, and fill it with prefix. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002174 luaL_buffinit(L, &socket->b);
2175
2176 /* Check prefix. */
2177 if (lua_gettop(L) >= 3) {
2178 if (lua_type(L, 3) != LUA_TSTRING)
2179 WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix"));
2180 pattern = lua_tolstring(L, 3, &len);
2181 luaL_addlstring(&socket->b, pattern, len);
2182 }
2183
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002184 return __LJMP(hlua_socket_receive_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002185}
2186
2187/* Write the Lua input string in the output buffer.
Mark Lakes22154b42018-01-29 14:38:40 -08002188 * This function returns a yield if no space is available.
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002189 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002190static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002191{
2192 struct hlua_socket *socket;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002193 struct hlua *hlua;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002194 struct appctx *appctx;
2195 size_t buf_len;
2196 const char *buf;
2197 int len;
2198 int send_len;
2199 int sent;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002200 struct xref *peer;
2201 struct stream_interface *si;
2202 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002203
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002204 /* Get hlua struct, or NULL if we execute from main lua state */
2205 hlua = hlua_gethlua(L);
2206
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002207 /* Check if this lua stack is schedulable. */
2208 if (!hlua || !hlua->task)
2209 WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in "
2210 "'frontend', 'backend' or 'task'"));
2211
2212 /* Get object */
2213 socket = MAY_LJMP(hlua_checksocket(L, 1));
2214 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002215 sent = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002216
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002217 /* Check if we run on the same thread than the xreator thread.
2218 * We cannot access to the socket if the thread is different.
2219 */
2220 if (socket->tid != tid)
2221 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2222
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002223 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002224 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002225 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002226 lua_pushinteger(L, -1);
2227 return 1;
2228 }
2229 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2230 si = appctx->owner;
2231 s = si_strm(si);
2232
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002233 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002234 if (channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002235 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002236 lua_pushinteger(L, -1);
2237 return 1;
2238 }
2239
2240 /* Update the input buffer data. */
2241 buf += sent;
2242 send_len = buf_len - sent;
2243
2244 /* All the data are sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002245 if (sent >= buf_len) {
2246 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002247 return 1; /* Implicitly return the length sent. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002248 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002249
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002250 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002251 * the request buffer if its not required.
2252 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002253 if (s->req.buf.size == 0) {
Willy Tarreau581abd32018-10-25 10:21:41 +02002254 if (!si_alloc_ibuf(si, &appctx->buffer_wait))
Christopher Faulet33834b12016-12-19 09:29:06 +01002255 goto hlua_socket_write_yield_return;
Thierry FOURNIER486d52a2015-03-09 17:51:43 +01002256 }
2257
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002258 /* Check for available space. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02002259 len = b_room(&s->req.buf);
Christopher Faulet33834b12016-12-19 09:29:06 +01002260 if (len <= 0) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002261 goto hlua_socket_write_yield_return;
Christopher Faulet33834b12016-12-19 09:29:06 +01002262 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002263
2264 /* send data */
2265 if (len < send_len)
2266 send_len = len;
Thierry FOURNIER66b89192018-05-27 01:14:47 +02002267 len = ci_putblk(&s->req, buf, send_len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002268
2269 /* "Not enough space" (-1), "Buffer too little to contain
2270 * the data" (-2) are not expected because the available length
2271 * is tested.
2272 * Other unknown error are also not expected.
2273 */
2274 if (len <= 0) {
Willy Tarreaubc18da12015-03-13 14:00:47 +01002275 if (len == -1)
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002276 s->req.flags |= CF_WAKE_WRITE;
Willy Tarreaubc18da12015-03-13 14:00:47 +01002277
sada05ed3302018-05-11 11:48:18 -07002278 MAY_LJMP(hlua_socket_close_helper(L));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002279 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002280 lua_pushinteger(L, -1);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002281 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002282 return 1;
2283 }
2284
2285 /* update buffers. */
Thierry FOURNIER101b9762018-05-27 01:27:40 +02002286 appctx_wakeup(appctx);
Willy Tarreaude70fa12015-09-26 11:25:05 +02002287
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002288 s->req.rex = TICK_ETERNITY;
2289 s->res.wex = TICK_ETERNITY;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002290
2291 /* Update length sent. */
2292 lua_pop(L, 1);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002293 lua_pushinteger(L, sent + len);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002294
2295 /* All the data buffer is sent ? */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002296 if (sent + len >= buf_len) {
2297 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002298 return 1;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002299 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002300
2301hlua_socket_write_yield_return:
Thierry FOURNIERba42fcd2018-05-27 00:59:48 +02002302 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2303 xref_unlock(&socket->xref, peer);
2304 WILL_LJMP(luaL_error(L, "out of memory"));
2305 }
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002306 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002307 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002308 return 0;
2309}
2310
2311/* This function initiate the send of data. It just check the input
2312 * parameters and push an integer in the Lua stack that contain the
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08002313 * amount of data written to the buffer. This is used by the function
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002314 * "hlua_socket_write_yield" that can yield.
2315 *
2316 * The Lua function gets between 3 and 4 parameters. The first one is
2317 * the associated object. The second is a string buffer. The third is
2318 * a facultative integer that represents where is the buffer position
2319 * of the start of the data that can send. The first byte is the
2320 * position "1". The default value is "1". The fourth argument is a
2321 * facultative integer that represents where is the buffer position
2322 * of the end of the data that can send. The default is the last byte.
2323 */
2324static int hlua_socket_send(struct lua_State *L)
2325{
2326 int i;
2327 int j;
2328 const char *buf;
2329 size_t buf_len;
2330
2331 /* Check number of arguments. */
2332 if (lua_gettop(L) < 2 || lua_gettop(L) > 4)
2333 WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments"));
2334
2335 /* Get the string. */
2336 buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len));
2337
2338 /* Get and check j. */
2339 if (lua_gettop(L) == 4) {
2340 j = MAY_LJMP(luaL_checkinteger(L, 4));
2341 if (j < 0)
2342 j = buf_len + j + 1;
2343 if (j > buf_len)
2344 j = buf_len + 1;
2345 lua_pop(L, 1);
2346 }
2347 else
2348 j = buf_len;
2349
2350 /* Get and check i. */
2351 if (lua_gettop(L) == 3) {
2352 i = MAY_LJMP(luaL_checkinteger(L, 3));
2353 if (i < 0)
2354 i = buf_len + i + 1;
2355 if (i > buf_len)
2356 i = buf_len + 1;
2357 lua_pop(L, 1);
2358 } else
2359 i = 1;
2360
2361 /* Check bth i and j. */
2362 if (i > j) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002363 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002364 return 1;
2365 }
2366 if (i == 0 && j == 0) {
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002367 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002368 return 1;
2369 }
2370 if (i == 0)
2371 i = 1;
2372 if (j == 0)
2373 j = 1;
2374
2375 /* Pop the string. */
2376 lua_pop(L, 1);
2377
2378 /* Update the buffer length. */
2379 buf += i - 1;
2380 buf_len = j - i + 1;
2381 lua_pushlstring(L, buf, buf_len);
2382
2383 /* This unsigned is used to remember the amount of sent data. */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002384 lua_pushinteger(L, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002385
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002386 return MAY_LJMP(hlua_socket_write_yield(L, 0, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002387}
2388
Willy Tarreau22b0a682015-06-17 19:43:49 +02002389#define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345")
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002390__LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr)
2391{
2392 static char buffer[SOCKET_INFO_MAX_LEN];
2393 int ret;
2394 int len;
2395 char *p;
2396
2397 ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1);
2398 if (ret <= 0) {
2399 lua_pushnil(L);
2400 return 1;
2401 }
2402
2403 if (ret == AF_UNIX) {
2404 lua_pushstring(L, buffer+1);
2405 return 1;
2406 }
2407 else if (ret == AF_INET6) {
2408 buffer[0] = '[';
2409 len = strlen(buffer);
2410 buffer[len] = ']';
2411 len++;
2412 buffer[len] = ':';
2413 len++;
2414 p = buffer;
2415 }
2416 else if (ret == AF_INET) {
2417 p = buffer + 1;
2418 len = strlen(p);
2419 p[len] = ':';
2420 len++;
2421 }
2422 else {
2423 lua_pushnil(L);
2424 return 1;
2425 }
2426
2427 if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) {
2428 lua_pushnil(L);
2429 return 1;
2430 }
2431
2432 lua_pushstring(L, p);
2433 return 1;
2434}
2435
2436/* Returns information about the peer of the connection. */
2437__LJMP static int hlua_socket_getpeername(struct lua_State *L)
2438{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002439 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002440 struct xref *peer;
2441 struct appctx *appctx;
2442 struct stream_interface *si;
2443 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002444 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002445
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002446 MAY_LJMP(check_args(L, 1, "getpeername"));
2447
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002448 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002449
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002450 /* Check if we run on the same thread than the xreator thread.
2451 * We cannot access to the socket if the thread is different.
2452 */
2453 if (socket->tid != tid)
2454 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2455
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002456 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002457 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002458 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002459 lua_pushnil(L);
2460 return 1;
2461 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002462 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2463 si = appctx->owner;
2464 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002465
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002466 if (!s->target_addr) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002467 xref_unlock(&socket->xref, peer);
Willy Tarreaua71f6422016-11-16 17:00:14 +01002468 lua_pushnil(L);
2469 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002470 }
2471
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002472 ret = MAY_LJMP(hlua_socket_info(L, s->target_addr));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002473 xref_unlock(&socket->xref, peer);
2474 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002475}
2476
2477/* Returns information about my connection side. */
2478static int hlua_socket_getsockname(struct lua_State *L)
2479{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002480 struct hlua_socket *socket;
2481 struct connection *conn;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002482 struct appctx *appctx;
2483 struct xref *peer;
2484 struct stream_interface *si;
2485 struct stream *s;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002486 int ret;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002487
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002488 MAY_LJMP(check_args(L, 1, "getsockname"));
2489
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002490 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002491
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002492 /* Check if we run on the same thread than the xreator thread.
2493 * We cannot access to the socket if the thread is different.
2494 */
2495 if (socket->tid != tid)
2496 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2497
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002498 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002499 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002500 if (!peer) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002501 lua_pushnil(L);
2502 return 1;
2503 }
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002504 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2505 si = appctx->owner;
2506 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002507
Olivier Houchard9aaf7782017-09-13 18:30:23 +02002508 conn = cs_conn(objt_cs(s->si[1].end));
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002509 if (!conn || !conn_get_src(conn)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002510 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002511 lua_pushnil(L);
2512 return 1;
2513 }
2514
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002515 ret = hlua_socket_info(L, conn->src);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002516 xref_unlock(&socket->xref, peer);
2517 return ret;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002518}
2519
2520/* This struct define the applet. */
Willy Tarreau30576452015-04-13 13:50:30 +02002521static struct applet update_applet = {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002522 .obj_type = OBJ_TYPE_APPLET,
2523 .name = "<LUA_TCP>",
2524 .fct = hlua_socket_handler,
2525 .release = hlua_socket_release,
2526};
2527
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002528__LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002529{
2530 struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002531 struct hlua *hlua;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002532 struct xref *peer;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002533 struct appctx *appctx;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002534 struct stream_interface *si;
2535 struct stream *s;
2536
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002537 /* Get hlua struct, or NULL if we execute from main lua state */
2538 hlua = hlua_gethlua(L);
2539 if (!hlua)
2540 return 0;
2541
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002542 /* Check if we run on the same thread than the xreator thread.
2543 * We cannot access to the socket if the thread is different.
2544 */
2545 if (socket->tid != tid)
2546 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2547
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002548 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002549 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002550 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002551 lua_pushnil(L);
2552 lua_pushstring(L, "Can't connect");
2553 return 2;
2554 }
2555 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2556 si = appctx->owner;
2557 s = si_strm(si);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002558
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002559 /* Check if we run on the same thread than the xreator thread.
2560 * We cannot access to the socket if the thread is different.
2561 */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002562 if (socket->tid != tid) {
2563 xref_unlock(&socket->xref, peer);
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002564 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002565 }
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002566
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002567 /* Check for connection close. */
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002568 if (!hlua || channel_output_closed(&s->req)) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002569 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002570 lua_pushnil(L);
2571 lua_pushstring(L, "Can't connect");
2572 return 2;
2573 }
2574
Willy Tarreaue09101e2018-10-16 17:37:12 +02002575 appctx = __objt_appctx(s->si[0].end);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002576
2577 /* Check for connection established. */
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002578 if (appctx->ctx.hlua_cosocket.connected) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002579 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002580 lua_pushinteger(L, 1);
2581 return 1;
2582 }
2583
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002584 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2585 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002586 WILL_LJMP(luaL_error(L, "out of memory error"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002587 }
2588 xref_unlock(&socket->xref, peer);
Willy Tarreau9635e032018-10-16 17:52:55 +02002589 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002590 return 0;
2591}
2592
2593/* This function fail or initite the connection. */
2594__LJMP static int hlua_socket_connect(struct lua_State *L)
2595{
2596 struct hlua_socket *socket;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002597 int port = -1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002598 const char *ip;
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002599 struct hlua *hlua;
2600 struct appctx *appctx;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002601 int low, high;
2602 struct sockaddr_storage *addr;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002603 struct xref *peer;
2604 struct stream_interface *si;
2605 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002606
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002607 if (lua_gettop(L) < 2)
2608 WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments"));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002609
2610 /* Get args. */
2611 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002612
2613 /* Check if we run on the same thread than the xreator thread.
2614 * We cannot access to the socket if the thread is different.
2615 */
2616 if (socket->tid != tid)
2617 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2618
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002619 ip = MAY_LJMP(luaL_checkstring(L, 2));
Tim Duesterhus6edab862018-01-06 19:04:45 +01002620 if (lua_gettop(L) >= 3) {
2621 luaL_Buffer b;
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002622 port = MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002623
Tim Duesterhus6edab862018-01-06 19:04:45 +01002624 /* Force the ip to end with a colon, to support IPv6 addresses
2625 * that are not enclosed within square brackets.
2626 */
2627 if (port > 0) {
2628 luaL_buffinit(L, &b);
2629 luaL_addstring(&b, ip);
2630 luaL_addchar(&b, ':');
2631 luaL_pushresult(&b);
2632 ip = lua_tolstring(L, lua_gettop(L), NULL);
2633 }
2634 }
2635
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002636 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002637 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002638 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002639 lua_pushnil(L);
2640 return 1;
2641 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002642
2643 /* Parse ip address. */
Willy Tarreau5fc93282020-09-16 18:25:03 +02002644 addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, NULL, NULL, PA_O_PORT_OK | PA_O_STREAM);
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002645 if (!addr) {
2646 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002647 WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002648 }
Willy Tarreau9da9a6f2019-07-17 14:49:44 +02002649
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002650 /* Set port. */
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002651 if (low == 0) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002652 if (addr->ss_family == AF_INET) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002653 if (port == -1) {
2654 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002655 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002656 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002657 ((struct sockaddr_in *)addr)->sin_port = htons(port);
2658 } else if (addr->ss_family == AF_INET6) {
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002659 if (port == -1) {
2660 xref_unlock(&socket->xref, peer);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002661 WILL_LJMP(luaL_error(L, "connect: port missing"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002662 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002663 ((struct sockaddr_in6 *)addr)->sin6_port = htons(port);
Thierry FOURNIERc2f56532015-09-26 20:23:30 +02002664 }
2665 }
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002666
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002667 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2668 si = appctx->owner;
2669 s = si_strm(si);
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002670
Willy Tarreau9b7587a2020-10-15 07:32:10 +02002671 if (!sockaddr_alloc(&s->target_addr, addr, sizeof(*addr))) {
Willy Tarreau1c8d32b2019-07-18 15:47:45 +02002672 xref_unlock(&socket->xref, peer);
2673 WILL_LJMP(luaL_error(L, "connect: internal error"));
2674 }
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002675 s->flags |= SF_ADDR_SET;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002676
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002677 /* Get hlua struct, or NULL if we execute from main lua state */
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002678 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01002679 if (!hlua)
2680 return 0;
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002681
2682 /* inform the stream that we want to be notified whenever the
2683 * connection completes.
2684 */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01002685 si_cant_get(&s->si[0]);
Willy Tarreau3367d412018-11-15 10:57:41 +01002686 si_rx_endp_more(&s->si[0]);
Thierry FOURNIER8c8fbbe2015-09-26 17:02:35 +02002687 appctx_wakeup(appctx);
Willy Tarreaubdc97a82015-08-24 15:42:28 +02002688
Willy Tarreauf31af932020-01-14 09:59:38 +01002689 hlua->gc_count++;
Thierry FOURNIER7c39ab42015-09-27 22:53:33 +02002690
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002691 if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) {
2692 xref_unlock(&socket->xref, peer);
Thierry FOURNIER95ad96a2015-03-09 18:12:40 +01002693 WILL_LJMP(luaL_error(L, "out of memory"));
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002694 }
2695 xref_unlock(&socket->xref, peer);
PiBa-NL706d5ee2018-05-05 23:51:42 +02002696
2697 task_wakeup(s->task, TASK_WOKEN_INIT);
2698 /* Return yield waiting for connection. */
2699
Willy Tarreau9635e032018-10-16 17:52:55 +02002700 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0));
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002701
2702 return 0;
2703}
2704
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002705#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002706__LJMP static int hlua_socket_connect_ssl(struct lua_State *L)
2707{
2708 struct hlua_socket *socket;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002709 struct xref *peer;
2710 struct appctx *appctx;
2711 struct stream_interface *si;
2712 struct stream *s;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002713
2714 MAY_LJMP(check_args(L, 3, "connect_ssl"));
2715 socket = MAY_LJMP(hlua_checksocket(L, 1));
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002716
2717 /* check for connection break. If some data where read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002718 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002719 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002720 lua_pushnil(L);
2721 return 1;
2722 }
2723 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2724 si = appctx->owner;
2725 s = si_strm(si);
2726
2727 s->target = &socket_ssl.obj_type;
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002728 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002729 return MAY_LJMP(hlua_socket_connect(L));
2730}
Baptiste Assmann84bb4932015-03-02 21:40:06 +01002731#endif
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002732
2733__LJMP static int hlua_socket_setoption(struct lua_State *L)
2734{
2735 return 0;
2736}
2737
2738__LJMP static int hlua_socket_settimeout(struct lua_State *L)
2739{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002740 struct hlua_socket *socket;
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002741 int tmout;
Mark Lakes56cc1252018-03-27 09:48:06 +02002742 double dtmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002743 struct xref *peer;
2744 struct appctx *appctx;
2745 struct stream_interface *si;
2746 struct stream *s;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002747
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002748 MAY_LJMP(check_args(L, 2, "settimeout"));
2749
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002750 socket = MAY_LJMP(hlua_checksocket(L, 1));
Mark Lakes56cc1252018-03-27 09:48:06 +02002751
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002752 /* convert the timeout to millis */
2753 dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002754
Thierry Fournier17a921b2018-03-08 09:59:02 +01002755 /* Check for negative values */
Mark Lakes56cc1252018-03-27 09:48:06 +02002756 if (dtmout < 0)
Thierry Fournier17a921b2018-03-08 09:59:02 +01002757 WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values"));
2758
Mark Lakes56cc1252018-03-27 09:48:06 +02002759 if (dtmout > INT_MAX) /* overflow check */
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002760 WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX));
Mark Lakes56cc1252018-03-27 09:48:06 +02002761
2762 tmout = MS_TO_TICKS((int)dtmout);
Cyril Bonté7ee465f2018-08-19 22:08:50 +02002763 if (tmout == 0)
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002764 tmout++; /* very small timeouts are adjusted to a minimum of 1ms */
Mark Lakes56cc1252018-03-27 09:48:06 +02002765
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002766 /* Check if we run on the same thread than the xreator thread.
2767 * We cannot access to the socket if the thread is different.
2768 */
2769 if (socket->tid != tid)
2770 WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread"));
2771
Mark Lakes56cc1252018-03-27 09:48:06 +02002772 /* check for connection break. If some data were read, return it. */
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002773 peer = xref_get_peer_and_lock(&socket->xref);
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002774 if (!peer) {
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002775 hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts.");
2776 WILL_LJMP(lua_error(L));
2777 return 0;
2778 }
2779 appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref);
2780 si = appctx->owner;
2781 s = si_strm(si);
2782
Cyril Bonté7bb63452018-08-17 23:51:02 +02002783 s->sess->fe->timeout.connect = tmout;
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002784 s->req.rto = tmout;
2785 s->req.wto = tmout;
2786 s->res.rto = tmout;
2787 s->res.wto = tmout;
Cyril Bonté7bb63452018-08-17 23:51:02 +02002788 s->req.rex = tick_add_ifset(now_ms, tmout);
2789 s->req.wex = tick_add_ifset(now_ms, tmout);
2790 s->res.rex = tick_add_ifset(now_ms, tmout);
2791 s->res.wex = tick_add_ifset(now_ms, tmout);
2792
2793 s->task->expire = tick_add_ifset(now_ms, tmout);
2794 task_queue(s->task);
2795
Thierry FOURNIER952939d2017-09-01 14:17:32 +02002796 xref_unlock(&socket->xref, peer);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002797
Thierry Fourniere9636f12018-03-08 09:54:32 +01002798 lua_pushinteger(L, 1);
Tim Duesterhus119a5f12018-01-06 19:16:25 +01002799 return 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002800}
2801
2802__LJMP static int hlua_socket_new(lua_State *L)
2803{
2804 struct hlua_socket *socket;
2805 struct appctx *appctx;
Willy Tarreau15b5e142015-04-04 14:38:25 +02002806 struct session *sess;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002807 struct stream *strm;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002808
2809 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002810 if (!lua_checkstack(L, 3)) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002811 hlua_pusherror(L, "socket: full stack");
2812 goto out_fail_conf;
2813 }
2814
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002815 /* Create the object: obj[0] = userdata. */
2816 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002817 socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket)));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002818 lua_rawseti(L, -2, 0);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002819 memset(socket, 0, sizeof(*socket));
Thierry FOURNIER94a6bfc2017-07-12 12:10:44 +02002820 socket->tid = tid;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002821
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05002822 /* Check if the various memory pools are initialized. */
Willy Tarreaubafbe012017-11-24 17:34:44 +01002823 if (!pool_head_stream || !pool_head_buffer) {
Thierry FOURNIER4a6170c2015-03-09 17:07:10 +01002824 hlua_pusherror(L, "socket: uninitialized pools.");
2825 goto out_fail_conf;
2826 }
2827
Willy Tarreau87b09662015-04-03 00:22:06 +02002828 /* Pop a class stream metatable and affect it to the userdata. */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002829 lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref);
2830 lua_setmetatable(L, -2);
2831
Willy Tarreaud420a972015-04-06 00:39:18 +02002832 /* Create the applet context */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01002833 appctx = appctx_new(&update_applet, tid_bit);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002834 if (!appctx) {
2835 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaufeb76402015-04-03 14:10:06 +02002836 goto out_fail_conf;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002837 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002838
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002839 appctx->ctx.hlua_cosocket.connected = 0;
Thierry FOURNIERb13b20a2017-07-16 20:48:54 +02002840 appctx->ctx.hlua_cosocket.die = 0;
Thierry FOURNIER18d09902016-12-16 09:25:38 +01002841 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write);
2842 LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read);
Willy Tarreaub2bf8332015-04-04 15:58:58 +02002843
Willy Tarreaud420a972015-04-06 00:39:18 +02002844 /* Now create a session, task and stream for this applet */
2845 sess = session_new(&socket_proxy, NULL, &appctx->obj_type);
2846 if (!sess) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002847 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002848 goto out_fail_sess;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002849 }
2850
Christopher Faulet26256f82020-09-14 11:40:13 +02002851 strm = stream_new(sess, &appctx->obj_type, &BUF_NULL);
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002852 if (!strm) {
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002853 hlua_pusherror(L, "socket: out of memory");
Willy Tarreaud420a972015-04-06 00:39:18 +02002854 goto out_fail_stream;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002855 }
2856
Thierry FOURNIER2da788e2017-09-11 18:37:23 +02002857 /* Initialise cross reference between stream and Lua socket object. */
2858 xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002859
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002860 /* Configure "right" stream interface. this "si" is used to connect
2861 * and retrieve data from the server. The connection is initialized
2862 * with the "struct server".
2863 */
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002864 si_set_state(&strm->si[1], SI_ST_ASS);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002865
2866 /* Force destination server. */
Willy Tarreau5a0b25d2019-07-18 18:01:14 +02002867 strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED;
Willy Tarreau61cf7c82015-04-06 00:48:33 +02002868 strm->target = &socket_tcp.obj_type;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002869
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002870 return 1;
2871
Willy Tarreaud420a972015-04-06 00:39:18 +02002872 out_fail_stream:
Willy Tarreau11c36242015-04-04 15:54:03 +02002873 session_free(sess);
Willy Tarreaud420a972015-04-06 00:39:18 +02002874 out_fail_sess:
2875 appctx_free(appctx);
2876 out_fail_conf:
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01002877 WILL_LJMP(lua_error(L));
2878 return 0;
2879}
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01002880
2881/*
2882 *
2883 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002884 * Class Channel
2885 *
2886 *
2887 */
2888
2889/* Returns the struct hlua_channel join to the class channel in the
2890 * stack entry "ud" or throws an argument error.
2891 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002892__LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002893{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02002894 return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002895}
2896
Willy Tarreau47860ed2015-03-10 14:07:50 +01002897/* Pushes the channel onto the top of the stack. If the stask does not have a
2898 * free slots, the function fails and returns 0;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002899 */
Willy Tarreau2a71af42015-03-10 13:51:50 +01002900static int hlua_channel_new(lua_State *L, struct channel *channel)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002901{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002902 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002903 if (!lua_checkstack(L, 3))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002904 return 0;
2905
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002906 lua_newtable(L);
Willy Tarreau47860ed2015-03-10 14:07:50 +01002907 lua_pushlightuserdata(L, channel);
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01002908 lua_rawseti(L, -2, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002909
2910 /* Pop a class sesison metatable and affect it to the userdata. */
2911 lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref);
2912 lua_setmetatable(L, -2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002913 return 1;
2914}
2915
2916/* Duplicate all the data present in the input channel and put it
2917 * in a string LUA variables. Returns -1 and push a nil value in
2918 * the stack if the channel is closed and all the data are consumed,
2919 * returns 0 if no data are available, otherwise it returns the length
Ilya Shipitsind4259502020-04-08 01:07:56 +05002920 * of the built string.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002921 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01002922static inline int _hlua_channel_dup(struct channel *chn, lua_State *L)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002923{
2924 char *blk1;
2925 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02002926 size_t len1;
2927 size_t len2;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002928 int ret;
2929 luaL_Buffer b;
2930
Willy Tarreau06d80a92017-10-19 14:32:15 +02002931 ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002932 if (unlikely(ret == 0))
2933 return 0;
2934
2935 if (unlikely(ret < 0)) {
2936 lua_pushnil(L);
2937 return -1;
2938 }
2939
2940 luaL_buffinit(L, &b);
2941 luaL_addlstring(&b, blk1, len1);
2942 if (unlikely(ret == 2))
2943 luaL_addlstring(&b, blk2, len2);
2944 luaL_pushresult(&b);
2945
2946 if (unlikely(ret == 2))
2947 return len1 + len2;
2948 return len1;
2949}
2950
2951/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2952 * a yield. This function keep the data in the buffer.
2953 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002954__LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002955{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002956 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002957
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002958 chn = MAY_LJMP(hlua_checkchannel(L, 1));
2959
Thierry Fournier77016da2020-08-15 14:35:51 +02002960 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2961 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002962 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002963 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002964
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002965 if (_hlua_channel_dup(chn, L) == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02002966 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002967 return 1;
2968}
2969
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002970/* Check arguments for the function "hlua_channel_dup_yield". */
2971__LJMP static int hlua_channel_dup(lua_State *L)
2972{
2973 MAY_LJMP(check_args(L, 1, "dup"));
2974 MAY_LJMP(hlua_checkchannel(L, 1));
2975 return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0));
2976}
2977
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002978/* "_hlua_channel_dup" wrapper. If no data are available, it returns
2979 * a yield. This function consumes the data in the buffer. It returns
2980 * a string containing the data or a nil pointer if no data are available
2981 * and the channel is closed.
2982 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002983__LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002984{
Willy Tarreau47860ed2015-03-10 14:07:50 +01002985 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002986 int ret;
2987
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002988 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01002989
Thierry Fournier77016da2020-08-15 14:35:51 +02002990 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
2991 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01002992 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02002993 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01002994
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002995 ret = _hlua_channel_dup(chn, L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002996 if (unlikely(ret == 0))
Willy Tarreau9635e032018-10-16 17:52:55 +02002997 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01002998
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01002999 if (unlikely(ret == -1))
3000 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003001
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003002 b_sub(&chn->buf, ret);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003003 return 1;
3004}
3005
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003006/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003007__LJMP static int hlua_channel_get(lua_State *L)
3008{
3009 MAY_LJMP(check_args(L, 1, "get"));
3010 MAY_LJMP(hlua_checkchannel(L, 1));
3011 return MAY_LJMP(hlua_channel_get_yield(L, 0, 0));
3012}
3013
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003014/* This functions consumes and returns one line. If the channel is closed,
3015 * and the last data does not contains a final '\n', the data are returned
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003016 * without the final '\n'. When no more data are available, it returns nil
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003017 * value.
3018 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003019__LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003020{
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003021 char *blk1;
3022 char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003023 size_t len1;
3024 size_t len2;
3025 size_t len;
Willy Tarreau47860ed2015-03-10 14:07:50 +01003026 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003027 int ret;
3028 luaL_Buffer b;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003029
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003030 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3031
Thierry Fournier77016da2020-08-15 14:35:51 +02003032 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3033 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003034 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003035 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003036
Willy Tarreau06d80a92017-10-19 14:32:15 +02003037 ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003038 if (ret == 0)
Willy Tarreau9635e032018-10-16 17:52:55 +02003039 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003040
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003041 if (ret == -1) {
3042 lua_pushnil(L);
3043 return 1;
3044 }
3045
3046 luaL_buffinit(L, &b);
3047 luaL_addlstring(&b, blk1, len1);
3048 len = len1;
3049 if (unlikely(ret == 2)) {
3050 luaL_addlstring(&b, blk2, len2);
3051 len += len2;
3052 }
3053 luaL_pushresult(&b);
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003054 b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003055 return 1;
3056}
3057
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003058/* Check arguments for the function "hlua_channel_getline_yield". */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003059__LJMP static int hlua_channel_getline(lua_State *L)
3060{
3061 MAY_LJMP(check_args(L, 1, "getline"));
3062 MAY_LJMP(hlua_checkchannel(L, 1));
3063 return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0));
3064}
3065
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003066/* This function takes a string as input, and append it at the
3067 * input side of channel. If the data is too big, but a space
3068 * is probably available after sending some data, the function
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003069 * yields. If the data is bigger than the buffer, or if the
3070 * channel is closed, it returns -1. Otherwise, it returns the
3071 * amount of data written.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003072 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003073__LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003074{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003075 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003076 size_t len;
3077 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3078 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3079 int ret;
3080 int max;
3081
Thierry Fournier77016da2020-08-15 14:35:51 +02003082 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3083 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003084 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003085 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003086
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003087 /* Check if the buffer is available because HAProxy doesn't allocate
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003088 * the request buffer if its not required.
3089 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003090 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003091 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003092 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Christopher Fauleta73e59b2016-12-09 17:30:18 +01003093 }
3094
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003095 max = channel_recv_limit(chn) - b_data(&chn->buf);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003096 if (max > len - l)
3097 max = len - l;
3098
Willy Tarreau06d80a92017-10-19 14:32:15 +02003099 ret = ci_putblk(chn, str + l, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003100 if (ret == -2 || ret == -3) {
3101 lua_pushinteger(L, -1);
3102 return 1;
3103 }
Willy Tarreaubc18da12015-03-13 14:00:47 +01003104 if (ret == -1) {
3105 chn->flags |= CF_WAKE_WRITE;
Willy Tarreau9635e032018-10-16 17:52:55 +02003106 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Willy Tarreaubc18da12015-03-13 14:00:47 +01003107 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003108 l += ret;
3109 lua_pop(L, 1);
3110 lua_pushinteger(L, l);
3111
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003112 max = channel_recv_limit(chn) - b_data(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003113 if (max == 0 && co_data(chn) == 0) {
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003114 /* There are no space available, and the output buffer is empty.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003115 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003116 * we return the amount of copied data.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003117 */
3118 return 1;
3119 }
3120 if (l < len)
Willy Tarreau9635e032018-10-16 17:52:55 +02003121 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003122 return 1;
3123}
3124
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003125/* Just a wrapper of "hlua_channel_append_yield". It returns the length
3126 * of the written string, or -1 if the channel is closed or if the
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003127 * buffer size is too little for the data.
3128 */
3129__LJMP static int hlua_channel_append(lua_State *L)
3130{
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003131 size_t len;
3132
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003133 MAY_LJMP(check_args(L, 2, "append"));
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003134 MAY_LJMP(hlua_checkchannel(L, 1));
3135 MAY_LJMP(luaL_checklstring(L, 2, &len));
3136 MAY_LJMP(luaL_checkinteger(L, 3));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003137 lua_pushinteger(L, 0);
3138
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003139 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003140}
3141
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003142/* Just a wrapper of "hlua_channel_append_yield". This wrapper starts
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003143 * his process by cleaning the buffer. The result is a replacement
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003144 * of the current data. It returns the length of the written string,
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003145 * or -1 if the channel is closed or if the buffer size is too
3146 * little for the data.
3147 */
3148__LJMP static int hlua_channel_set(lua_State *L)
3149{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003150 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003151
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003152 MAY_LJMP(check_args(L, 2, "set"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003153 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003154 lua_pushinteger(L, 0);
3155
Thierry Fournier77016da2020-08-15 14:35:51 +02003156 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3157 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003158 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003159 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003160
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003161 b_set_data(&chn->buf, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003162
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003163 return MAY_LJMP(hlua_channel_append_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003164}
3165
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003166/* Append data in the output side of the buffer. This data is immediately
3167 * sent. The function returns the amount of data written. If the buffer
3168 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003169 * if the channel is closed.
3170 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003171__LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003172{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003173 struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003174 size_t len;
3175 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
3176 int l = MAY_LJMP(luaL_checkinteger(L, 3));
3177 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003178 struct hlua *hlua;
3179
3180 /* Get hlua struct, or NULL if we execute from main lua state */
3181 hlua = hlua_gethlua(L);
3182 if (!hlua) {
3183 lua_pushnil(L);
3184 return 1;
3185 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003186
Thierry Fournier77016da2020-08-15 14:35:51 +02003187 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3188 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003189 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003190 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003191
Willy Tarreau47860ed2015-03-10 14:07:50 +01003192 if (unlikely(channel_output_closed(chn))) {
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003193 lua_pushinteger(L, -1);
3194 return 1;
3195 }
3196
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003197 /* Check if the buffer is available because HAProxy doesn't allocate
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003198 * the request buffer if its not required.
3199 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003200 if (chn->buf.size == 0) {
Willy Tarreau4b962a42018-11-15 11:03:21 +01003201 si_rx_buff_blk(chn_prod(chn));
Willy Tarreau9635e032018-10-16 17:52:55 +02003202 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIER3e3a6082015-03-05 17:06:12 +01003203 }
3204
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003205 /* The written data will be immediately sent, so we can check
3206 * the available space without taking in account the reserve.
3207 * The reserve is guaranteed for the processing of incoming
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003208 * data, because the buffer will be flushed.
3209 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003210 max = b_room(&chn->buf);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003211
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003212 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003213 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003214 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003215 */
Willy Tarreaua79021a2018-06-15 18:07:57 +02003216 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003217 return 1;
3218
3219 /* Adjust the real required length. */
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003220 if (max > len - l)
3221 max = len - l;
3222
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003223 /* The buffer available size may be not contiguous. This test
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003224 * detects a non contiguous buffer and realign it.
3225 */
Willy Tarreau3f679992018-06-15 15:06:42 +02003226 if (ci_space_for_replace(chn) < max)
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003227 channel_slow_realign(chn, trash.area);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003228
3229 /* Copy input data in the buffer. */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003230 max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max);
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003231
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003232 /* buffer replace considers that the input part is filled.
3233 * so, I must forward these new data in the output part.
3234 */
Willy Tarreaubcbd3932018-06-06 07:13:22 +02003235 c_adv(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003236
3237 l += max;
3238 lua_pop(L, 1);
3239 lua_pushinteger(L, l);
3240
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003241 /* If there is no space available, and the output buffer is empty.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003242 * in this case, we cannot add more data, so we cannot yield,
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003243 * we return the amount of copied data.
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003244 */
Willy Tarreauc9fa0482018-07-10 17:43:27 +02003245 max = b_room(&chn->buf);
Willy Tarreaua79021a2018-06-15 18:07:57 +02003246 if (max == 0 && co_data(chn) == 0)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003247 return 1;
Thierry FOURNIERdeb5d732015-03-06 01:07:45 +01003248
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003249 if (l < len) {
3250 /* If we are waiting for space in the response buffer, we
3251 * must set the flag WAKERESWR. This flag required the task
3252 * wake up if any activity is detected on the response buffer.
3253 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003254 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003255 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003256 else
3257 HLUA_SET_WAKEREQWR(hlua);
Willy Tarreau9635e032018-10-16 17:52:55 +02003258 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003259 }
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003260
3261 return 1;
3262}
3263
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003264/* Just a wrapper of "_hlua_channel_send". This wrapper permits
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003265 * yield the LUA process, and resume it without checking the
3266 * input arguments.
3267 */
3268__LJMP static int hlua_channel_send(lua_State *L)
3269{
3270 MAY_LJMP(check_args(L, 2, "send"));
3271 lua_pushinteger(L, 0);
3272
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003273 return MAY_LJMP(hlua_channel_send_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003274}
3275
3276/* This function forward and amount of butes. The data pass from
3277 * the input side of the buffer to the output side, and can be
3278 * forwarded. This function never fails.
3279 *
3280 * The Lua function takes an amount of bytes to be forwarded in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003281 * input. It returns the number of bytes forwarded.
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003282 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003283__LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003284{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003285 struct channel *chn;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003286 int len;
3287 int l;
3288 int max;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01003289 struct hlua *hlua;
3290
3291 /* Get hlua struct, or NULL if we execute from main lua state */
3292 hlua = hlua_gethlua(L);
3293 if (!hlua)
3294 return 1;
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003295
3296 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet3f829a42018-12-13 21:56:45 +01003297
Thierry Fournier77016da2020-08-15 14:35:51 +02003298 if (chn_strm(chn)->be->mode == PR_MODE_HTTP) {
3299 lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode.");
Christopher Faulet3f829a42018-12-13 21:56:45 +01003300 WILL_LJMP(lua_error(L));
Thierry Fournier77016da2020-08-15 14:35:51 +02003301 }
Christopher Faulet3f829a42018-12-13 21:56:45 +01003302
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003303 len = MAY_LJMP(luaL_checkinteger(L, 2));
3304 l = MAY_LJMP(luaL_checkinteger(L, -1));
3305
3306 max = len - l;
Willy Tarreaua79021a2018-06-15 18:07:57 +02003307 if (max > ci_data(chn))
3308 max = ci_data(chn);
Willy Tarreau47860ed2015-03-10 14:07:50 +01003309 channel_forward(chn, max);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003310 l += max;
3311
3312 lua_pop(L, 1);
3313 lua_pushinteger(L, l);
3314
3315 /* Check if it miss bytes to forward. */
3316 if (l < len) {
3317 /* The the input channel or the output channel are closed, we
3318 * must return the amount of data forwarded.
3319 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003320 if (channel_input_closed(chn) || channel_output_closed(chn))
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003321 return 1;
3322
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003323 /* If we are waiting for space data in the response buffer, we
3324 * must set the flag WAKERESWR. This flag required the task
3325 * wake up if any activity is detected on the response buffer.
3326 */
Willy Tarreau47860ed2015-03-10 14:07:50 +01003327 if (chn->flags & CF_ISRESP)
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003328 HLUA_SET_WAKERESWR(hlua);
Thierry FOURNIER53e08ec2015-03-06 00:35:53 +01003329 else
3330 HLUA_SET_WAKEREQWR(hlua);
Thierry FOURNIERef6a2112015-03-05 17:45:34 +01003331
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003332 /* Otherwise, we can yield waiting for new data in the inpout side. */
Willy Tarreau9635e032018-10-16 17:52:55 +02003333 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003334 }
3335
3336 return 1;
3337}
3338
3339/* Just check the input and prepare the stack for the previous
3340 * function "hlua_channel_forward_yield"
3341 */
3342__LJMP static int hlua_channel_forward(lua_State *L)
3343{
3344 MAY_LJMP(check_args(L, 2, "forward"));
3345 MAY_LJMP(hlua_checkchannel(L, 1));
3346 MAY_LJMP(luaL_checkinteger(L, 2));
3347
3348 lua_pushinteger(L, 0);
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01003349 return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003350}
3351
3352/* Just returns the number of bytes available in the input
3353 * side of the buffer. This function never fails.
3354 */
3355__LJMP static int hlua_channel_get_in_len(lua_State *L)
3356{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003357 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003358
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003359 MAY_LJMP(check_args(L, 1, "get_in_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003360 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Fauleta3ceac12018-12-14 13:39:09 +01003361 if (IS_HTX_STRM(chn_strm(chn))) {
3362 struct htx *htx = htxbuf(&chn->buf);
3363 lua_pushinteger(L, htx->data - co_data(chn));
3364 }
3365 else
3366 lua_pushinteger(L, ci_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003367 return 1;
3368}
3369
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003370/* Returns true if the channel is full. */
3371__LJMP static int hlua_channel_is_full(lua_State *L)
3372{
3373 struct channel *chn;
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003374
3375 MAY_LJMP(check_args(L, 1, "is_full"));
3376 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Christopher Faulet0ec740e2020-02-26 11:59:19 +01003377 /* ignore the reserve, we are not on a producer side (ie in an
3378 * applet).
3379 */
3380 lua_pushboolean(L, channel_full(chn, 0));
Thierry FOURNIER / OZON.IO65192f32016-11-07 15:28:40 +01003381 return 1;
3382}
3383
Christopher Faulet2ac9ba22020-02-25 10:15:50 +01003384/* Returns true if the channel is the response channel. */
3385__LJMP static int hlua_channel_is_resp(lua_State *L)
3386{
3387 struct channel *chn;
3388
3389 MAY_LJMP(check_args(L, 1, "is_resp"));
3390 chn = MAY_LJMP(hlua_checkchannel(L, 1));
3391
3392 lua_pushboolean(L, !!(chn->flags & CF_ISRESP));
3393 return 1;
3394}
3395
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003396/* Just returns the number of bytes available in the output
3397 * side of the buffer. This function never fails.
3398 */
3399__LJMP static int hlua_channel_get_out_len(lua_State *L)
3400{
Willy Tarreau47860ed2015-03-10 14:07:50 +01003401 struct channel *chn;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003402
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003403 MAY_LJMP(check_args(L, 1, "get_out_len"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01003404 chn = MAY_LJMP(hlua_checkchannel(L, 1));
Willy Tarreaua79021a2018-06-15 18:07:57 +02003405 lua_pushinteger(L, co_data(chn));
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003406 return 1;
3407}
3408
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003409/*
3410 *
3411 *
3412 * Class Fetches
3413 *
3414 *
3415 */
3416
3417/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003418 * a class stream, otherwise it throws an error.
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003419 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003420__LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003421{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003422 return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003423}
3424
3425/* This function creates and push in the stack a fetch object according
3426 * with a current TXN.
3427 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003428static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003429{
Willy Tarreau7073c472015-04-06 11:15:40 +02003430 struct hlua_smp *hsmp;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003431
3432 /* Check stack size. */
3433 if (!lua_checkstack(L, 3))
3434 return 0;
3435
3436 /* Create the object: obj[0] = userdata.
3437 * Note that the base of the Fetches object is the
3438 * transaction object.
3439 */
3440 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003441 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003442 lua_rawseti(L, -2, 0);
3443
Willy Tarreau7073c472015-04-06 11:15:40 +02003444 hsmp->s = txn->s;
3445 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003446 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003447 hsmp->flags = flags;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003448
3449 /* Pop a class sesison metatable and affect it to the userdata. */
3450 lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref);
3451 lua_setmetatable(L, -2);
3452
3453 return 1;
3454}
3455
3456/* This function is an LUA binding. It is called with each sample-fetch.
3457 * It uses closure argument to store the associated sample-fetch. It
3458 * returns only one argument or throws an error. An error is thrown
3459 * only if an error is encountered during the argument parsing. If
3460 * the "sample-fetch" function fails, nil is returned.
3461 */
3462__LJMP static int hlua_run_sample_fetch(lua_State *L)
3463{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003464 struct hlua_smp *hsmp;
Willy Tarreau2ec22742015-03-10 14:27:20 +01003465 struct sample_fetch *f;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003466 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003467 int i;
3468 struct sample smp;
3469
3470 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003471 f = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003472
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003473 /* Get traditional arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003474 hsmp = MAY_LJMP(hlua_checkfetches(L, 1));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003475
Thierry FOURNIERca988662015-12-20 18:43:03 +01003476 /* Check execution authorization. */
3477 if (f->use & SMP_USE_HTTP_ANY &&
3478 !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) {
3479 lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which "
3480 "is not available in Lua services", f->kw);
3481 WILL_LJMP(lua_error(L));
3482 }
3483
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003484 /* Get extra arguments. */
3485 for (i = 0; i < lua_gettop(L) - 1; i++) {
3486 if (i >= ARGM_NBARGS)
3487 break;
3488 hlua_lua2arg(L, i + 2, &args[i]);
3489 }
3490 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003491 args[i].data.str.area = NULL;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003492
3493 /* Check arguments. */
Willy Tarreaub2ccb562015-04-06 11:11:15 +02003494 MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p));
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003495
3496 /* Run the special args checker. */
Willy Tarreau2ec22742015-03-10 14:27:20 +01003497 if (f->val_args && !f->val_args(args, NULL)) {
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003498 lua_pushfstring(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003499 goto error;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003500 }
3501
3502 /* Initialise the sample. */
3503 memset(&smp, 0, sizeof(smp));
3504
3505 /* Run the sample fetch process. */
Willy Tarreau1777ea62016-03-10 16:15:46 +01003506 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
Thierry FOURNIER0786d052015-05-11 15:42:45 +02003507 if (!f->process(args, &smp, f->kw, f->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003508 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003509 lua_pushstring(L, "");
3510 else
3511 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003512 goto end;
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003513 }
3514
3515 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003516 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003517 hlua_smp2lua_str(L, &smp);
3518 else
3519 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003520
3521 end:
3522 for (i = 0; args[i].type != ARGT_STOP; i++) {
3523 if (args[i].type == ARGT_STR)
3524 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003525 else if (args[i].type == ARGT_REG)
3526 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003527 }
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003528 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003529
3530 error:
3531 for (i = 0; args[i].type != ARGT_STOP; i++) {
3532 if (args[i].type == ARGT_STR)
3533 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003534 else if (args[i].type == ARGT_REG)
3535 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003536 }
3537 WILL_LJMP(lua_error(L));
3538 return 0; /* Never reached */
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01003539}
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01003540
3541/*
3542 *
3543 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003544 * Class Converters
3545 *
3546 *
3547 */
3548
3549/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02003550 * a class stream, otherwise it throws an error.
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003551 */
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003552__LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003553{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003554 return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003555}
3556
3557/* This function creates and push in the stack a Converters object
3558 * according with a current TXN.
3559 */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003560static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003561{
Willy Tarreau7073c472015-04-06 11:15:40 +02003562 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003563
3564 /* Check stack size. */
3565 if (!lua_checkstack(L, 3))
3566 return 0;
3567
3568 /* Create the object: obj[0] = userdata.
3569 * Note that the base of the Converters object is the
3570 * same than the TXN object.
3571 */
3572 lua_newtable(L);
Willy Tarreau7073c472015-04-06 11:15:40 +02003573 hsmp = lua_newuserdata(L, sizeof(*hsmp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003574 lua_rawseti(L, -2, 0);
3575
Willy Tarreau7073c472015-04-06 11:15:40 +02003576 hsmp->s = txn->s;
3577 hsmp->p = txn->p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01003578 hsmp->dir = txn->dir;
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003579 hsmp->flags = flags;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003580
Willy Tarreau87b09662015-04-03 00:22:06 +02003581 /* Pop a class stream metatable and affect it to the table. */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003582 lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref);
3583 lua_setmetatable(L, -2);
3584
3585 return 1;
3586}
3587
3588/* This function is an LUA binding. It is called with each converter.
3589 * It uses closure argument to store the associated converter. It
3590 * returns only one argument or throws an error. An error is thrown
3591 * only if an error is encountered during the argument parsing. If
3592 * the converter function function fails, nil is returned.
3593 */
3594__LJMP static int hlua_run_sample_conv(lua_State *L)
3595{
Willy Tarreauda5f1082015-04-06 11:17:13 +02003596 struct hlua_smp *hsmp;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003597 struct sample_conv *conv;
Frédéric Lécaillef874a832018-06-15 13:56:04 +02003598 struct arg args[ARGM_NBARGS + 1] = {{0}};
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003599 int i;
3600 struct sample smp;
3601
3602 /* Get closure arguments. */
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003603 conv = lua_touserdata(L, lua_upvalueindex(1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003604
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003605 /* Get traditional arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003606 hsmp = MAY_LJMP(hlua_checkconverters(L, 1));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003607
3608 /* Get extra arguments. */
3609 for (i = 0; i < lua_gettop(L) - 2; i++) {
3610 if (i >= ARGM_NBARGS)
3611 break;
3612 hlua_lua2arg(L, i + 3, &args[i]);
3613 }
3614 args[i].type = ARGT_STOP;
Willy Tarreau843b7cb2018-07-13 10:54:26 +02003615 args[i].data.str.area = NULL;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003616
3617 /* Check arguments. */
Willy Tarreauda5f1082015-04-06 11:17:13 +02003618 MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003619
3620 /* Run the special args checker. */
3621 if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) {
3622 hlua_pusherror(L, "error in arguments");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003623 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003624 }
3625
3626 /* Initialise the sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003627 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003628 if (!hlua_lua2smp(L, 2, &smp)) {
3629 hlua_pusherror(L, "error in the input argument");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003630 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003631 }
3632
Willy Tarreau1777ea62016-03-10 16:15:46 +01003633 smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR);
3634
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003635 /* Apply expected cast. */
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003636 if (!sample_casts[smp.data.type][conv->in_type]) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003637 hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'",
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003638 smp_to_type[smp.data.type], smp_to_type[conv->in_type]);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003639 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003640 }
Thierry FOURNIER8c542ca2015-08-19 09:00:18 +02003641 if (sample_casts[smp.data.type][conv->in_type] != c_none &&
3642 !sample_casts[smp.data.type][conv->in_type](&smp)) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003643 hlua_pusherror(L, "error during the input argument casting");
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003644 goto error;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003645 }
3646
3647 /* Run the sample conversion process. */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02003648 if (!conv->process(args, &smp, conv->private)) {
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003649 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003650 lua_pushstring(L, "");
3651 else
Willy Tarreaua678b432015-08-28 10:14:59 +02003652 lua_pushnil(L);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003653 goto end;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003654 }
3655
3656 /* Convert the returned sample in lua value. */
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003657 if (hsmp->flags & HLUA_F_AS_STRING)
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01003658 hlua_smp2lua_str(L, &smp);
3659 else
3660 hlua_smp2lua(L, &smp);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003661 end:
3662 for (i = 0; args[i].type != ARGT_STOP; i++) {
3663 if (args[i].type == ARGT_STR)
3664 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003665 else if (args[i].type == ARGT_REG)
3666 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003667 }
Willy Tarreaua678b432015-08-28 10:14:59 +02003668 return 1;
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003669
3670 error:
3671 for (i = 0; args[i].type != ARGT_STOP; i++) {
3672 if (args[i].type == ARGT_STR)
3673 chunk_destroy(&args[i].data.str);
Christopher Fauletfd2e9062020-08-06 11:10:57 +02003674 else if (args[i].type == ARGT_REG)
3675 regex_free(args[i].data.reg);
Christopher Fauletaec27ef2020-08-06 08:54:25 +02003676 }
3677 WILL_LJMP(lua_error(L));
3678 return 0; /* Never reached */
Thierry FOURNIER594afe72015-03-10 23:58:30 +01003679}
3680
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003681/*
3682 *
3683 *
3684 * Class AppletTCP
3685 *
3686 *
3687 */
3688
3689/* Returns a struct hlua_txn if the stack entry "ud" is
3690 * a class stream, otherwise it throws an error.
3691 */
3692__LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud)
3693{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02003694 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003695}
3696
3697/* This function creates and push in the stack an Applet object
3698 * according with a current TXN.
3699 */
3700static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx)
3701{
3702 struct hlua_appctx *appctx;
3703 struct stream_interface *si = ctx->owner;
3704 struct stream *s = si_strm(si);
3705 struct proxy *p = s->be;
3706
3707 /* Check stack size. */
3708 if (!lua_checkstack(L, 3))
3709 return 0;
3710
3711 /* Create the object: obj[0] = userdata.
3712 * Note that the base of the Converters object is the
3713 * same than the TXN object.
3714 */
3715 lua_newtable(L);
3716 appctx = lua_newuserdata(L, sizeof(*appctx));
3717 lua_rawseti(L, -2, 0);
3718 appctx->appctx = ctx;
3719 appctx->htxn.s = s;
3720 appctx->htxn.p = p;
3721
3722 /* Create the "f" field that contains a list of fetches. */
3723 lua_pushstring(L, "f");
3724 if (!hlua_fetches_new(L, &appctx->htxn, 0))
3725 return 0;
3726 lua_settable(L, -3);
3727
3728 /* Create the "sf" field that contains a list of stringsafe fetches. */
3729 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003730 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003731 return 0;
3732 lua_settable(L, -3);
3733
3734 /* Create the "c" field that contains a list of converters. */
3735 lua_pushstring(L, "c");
3736 if (!hlua_converters_new(L, &appctx->htxn, 0))
3737 return 0;
3738 lua_settable(L, -3);
3739
3740 /* Create the "sc" field that contains a list of stringsafe converters. */
3741 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01003742 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003743 return 0;
3744 lua_settable(L, -3);
3745
3746 /* Pop a class stream metatable and affect it to the table. */
3747 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref);
3748 lua_setmetatable(L, -2);
3749
3750 return 1;
3751}
3752
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003753__LJMP static int hlua_applet_tcp_set_var(lua_State *L)
3754{
3755 struct hlua_appctx *appctx;
3756 struct stream *s;
3757 const char *name;
3758 size_t len;
3759 struct sample smp;
3760
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003761 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
3762 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003763
3764 /* It is useles to retrieve the stream, but this function
3765 * runs only in a stream context.
3766 */
3767 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3768 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3769 s = appctx->htxn.s;
3770
3771 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01003772 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003773 hlua_lua2smp(L, 3, &smp);
3774
3775 /* Store the sample in a variable. */
3776 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02003777
3778 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
3779 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
3780 else
3781 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
3782
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003783 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003784}
3785
3786__LJMP static int hlua_applet_tcp_unset_var(lua_State *L)
3787{
3788 struct hlua_appctx *appctx;
3789 struct stream *s;
3790 const char *name;
3791 size_t len;
3792 struct sample smp;
3793
3794 MAY_LJMP(check_args(L, 2, "unset_var"));
3795
3796 /* It is useles to retrieve the stream, but this function
3797 * runs only in a stream context.
3798 */
3799 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3800 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3801 s = appctx->htxn.s;
3802
3803 /* Unset the variable. */
3804 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02003805 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
3806 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01003807}
3808
3809__LJMP static int hlua_applet_tcp_get_var(lua_State *L)
3810{
3811 struct hlua_appctx *appctx;
3812 struct stream *s;
3813 const char *name;
3814 size_t len;
3815 struct sample smp;
3816
3817 MAY_LJMP(check_args(L, 2, "get_var"));
3818
3819 /* It is useles to retrieve the stream, but this function
3820 * runs only in a stream context.
3821 */
3822 appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3823 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
3824 s = appctx->htxn.s;
3825
3826 smp_set_owner(&smp, s->be, s->sess, s, 0);
3827 if (!vars_get_by_name(name, len, &smp)) {
3828 lua_pushnil(L);
3829 return 1;
3830 }
3831
3832 return hlua_smp2lua(L, &smp);
3833}
3834
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003835__LJMP static int hlua_applet_tcp_set_priv(lua_State *L)
3836{
3837 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3838 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003839 struct hlua *hlua;
3840
3841 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003842 if (!s->hlua)
3843 return 0;
3844 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003845
3846 MAY_LJMP(check_args(L, 2, "set_priv"));
3847
3848 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02003849 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003850
3851 /* Get and store new value. */
3852 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
3853 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
3854
3855 return 0;
3856}
3857
3858__LJMP static int hlua_applet_tcp_get_priv(lua_State *L)
3859{
3860 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3861 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01003862 struct hlua *hlua;
3863
3864 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01003865 if (!s->hlua) {
3866 lua_pushnil(L);
3867 return 1;
3868 }
3869 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01003870
3871 /* Push configuration index in the stack. */
3872 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
3873
3874 return 1;
3875}
3876
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003877/* If expected data not yet available, it returns a yield. This function
3878 * consumes the data in the buffer. It returns a string containing the
3879 * data. This string can be empty.
3880 */
3881__LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx)
3882{
3883 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3884 struct stream_interface *si = appctx->appctx->owner;
3885 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003886 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003887 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003888 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003889 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003890
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003891 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003892 ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003893
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003894 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003895 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003896 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003897 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003898 }
3899
3900 /* End of data: commit the total strings and return. */
3901 if (ret < 0) {
3902 luaL_pushresult(&appctx->b);
3903 return 1;
3904 }
3905
3906 /* Ensure that the block 2 length is usable. */
3907 if (ret == 1)
3908 len2 = 0;
3909
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07003910 /* don't check the max length read and don't check. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003911 luaL_addlstring(&appctx->b, blk1, len1);
3912 luaL_addlstring(&appctx->b, blk2, len2);
3913
3914 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003915 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003916 luaL_pushresult(&appctx->b);
3917 return 1;
3918}
3919
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003920/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003921__LJMP static int hlua_applet_tcp_getline(lua_State *L)
3922{
3923 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3924
3925 /* Initialise the string catenation. */
3926 luaL_buffinit(L, &appctx->b);
3927
3928 return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0));
3929}
3930
3931/* If expected data not yet available, it returns a yield. This function
3932 * consumes the data in the buffer. It returns a string containing the
3933 * data. This string can be empty.
3934 */
3935__LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx)
3936{
3937 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
3938 struct stream_interface *si = appctx->appctx->owner;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003939 size_t len = MAY_LJMP(luaL_checkinteger(L, 2));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003940 int ret;
Willy Tarreau206ba832018-06-14 15:27:31 +02003941 const char *blk1;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003942 size_t len1;
Willy Tarreau206ba832018-06-14 15:27:31 +02003943 const char *blk2;
Willy Tarreau55f3ce12018-07-18 11:49:27 +02003944 size_t len2;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003945
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003946 /* Read the maximum amount of data available. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003947 ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003948
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003949 /* Data not yet available. return yield. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003950 if (ret == 0) {
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003951 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003952 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003953 }
3954
3955 /* End of data: commit the total strings and return. */
3956 if (ret < 0) {
3957 luaL_pushresult(&appctx->b);
3958 return 1;
3959 }
3960
3961 /* Ensure that the block 2 length is usable. */
3962 if (ret == 1)
3963 len2 = 0;
3964
3965 if (len == -1) {
3966
3967 /* If len == -1, catenate all the data avalaile and
3968 * yield because we want to get all the data until
3969 * the end of data stream.
3970 */
3971 luaL_addlstring(&appctx->b, blk1, len1);
3972 luaL_addlstring(&appctx->b, blk2, len2);
Willy Tarreau06d80a92017-10-19 14:32:15 +02003973 co_skip(si_oc(si), len1 + len2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003974 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003975 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003976
3977 } else {
3978
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05003979 /* Copy the first block caping to the length required. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003980 if (len1 > len)
3981 len1 = len;
3982 luaL_addlstring(&appctx->b, blk1, len1);
3983 len -= len1;
3984
3985 /* Copy the second block. */
3986 if (len2 > len)
3987 len2 = len;
3988 luaL_addlstring(&appctx->b, blk2, len2);
3989 len -= len2;
3990
3991 /* Consume input channel output buffer data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02003992 co_skip(si_oc(si), len1 + len2);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003993
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08003994 /* If there is no other data available, yield waiting for new data. */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02003995 if (len > 0) {
3996 lua_pushinteger(L, len);
3997 lua_replace(L, 2);
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01003998 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02003999 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004000 }
4001
4002 /* return the result. */
4003 luaL_pushresult(&appctx->b);
4004 return 1;
4005 }
4006
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004007 /* we never execute this */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004008 hlua_pusherror(L, "Lua: internal error");
4009 WILL_LJMP(lua_error(L));
4010 return 0;
4011}
4012
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004013/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004014__LJMP static int hlua_applet_tcp_recv(lua_State *L)
4015{
4016 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4017 int len = -1;
4018
4019 if (lua_gettop(L) > 2)
4020 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4021 if (lua_gettop(L) >= 2) {
4022 len = MAY_LJMP(luaL_checkinteger(L, 2));
4023 lua_pop(L, 1);
4024 }
4025
4026 /* Confirm or set the required length */
4027 lua_pushinteger(L, len);
4028
4029 /* Initialise the string catenation. */
4030 luaL_buffinit(L, &appctx->b);
4031
4032 return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0));
4033}
4034
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004035/* Append data in the output side of the buffer. This data is immediately
4036 * sent. The function returns the amount of data written. If the buffer
4037 * cannot contain the data, the function yields. The function returns -1
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004038 * if the channel is closed.
4039 */
4040__LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx)
4041{
4042 size_t len;
4043 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1));
4044 const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len));
4045 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4046 struct stream_interface *si = appctx->appctx->owner;
4047 struct channel *chn = si_ic(si);
4048 int max;
4049
4050 /* Get the max amount of data which can write as input in the channel. */
4051 max = channel_recv_max(chn);
4052 if (max > (len - l))
4053 max = len - l;
4054
4055 /* Copy data. */
Willy Tarreau06d80a92017-10-19 14:32:15 +02004056 ci_putblk(chn, str + l, max);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004057
4058 /* update counters. */
4059 l += max;
4060 lua_pop(L, 1);
4061 lua_pushinteger(L, l);
4062
4063 /* If some data is not send, declares the situation to the
4064 * applet, and returns a yield.
4065 */
4066 if (l < len) {
Willy Tarreaudb398432018-11-15 11:08:52 +01004067 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004068 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004069 }
4070
4071 return 1;
4072}
4073
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004074/* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02004075 * yield the LUA process, and resume it without checking the
4076 * input arguments.
4077 */
4078__LJMP static int hlua_applet_tcp_send(lua_State *L)
4079{
4080 MAY_LJMP(check_args(L, 2, "send"));
4081 lua_pushinteger(L, 0);
4082
4083 return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0));
4084}
4085
Thierry FOURNIER594afe72015-03-10 23:58:30 +01004086/*
4087 *
4088 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004089 * Class AppletHTTP
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004090 *
4091 *
4092 */
4093
4094/* Returns a struct hlua_txn if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02004095 * a class stream, otherwise it throws an error.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004096 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004097__LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004098{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02004099 return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004100}
4101
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004102/* This function creates and push in the stack an Applet object
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004103 * according with a current TXN.
4104 */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004105static int hlua_applet_http_new(lua_State *L, struct appctx *ctx)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004106{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004107 struct hlua_appctx *appctx;
Thierry FOURNIER841475e2015-12-11 17:10:09 +01004108 struct hlua_txn htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004109 struct stream_interface *si = ctx->owner;
4110 struct stream *s = si_strm(si);
4111 struct proxy *px = s->be;
Christopher Fauleta2097962019-07-15 16:25:33 +02004112 struct htx *htx;
4113 struct htx_blk *blk;
4114 struct htx_sl *sl;
4115 struct ist path;
4116 unsigned long long len = 0;
4117 int32_t pos;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004118
4119 /* Check stack size. */
4120 if (!lua_checkstack(L, 3))
4121 return 0;
4122
4123 /* Create the object: obj[0] = userdata.
4124 * Note that the base of the Converters object is the
4125 * same than the TXN object.
4126 */
4127 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004128 appctx = lua_newuserdata(L, sizeof(*appctx));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004129 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004130 appctx->appctx = ctx;
4131 appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004132 appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004133 appctx->htxn.s = s;
4134 appctx->htxn.p = px;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004135
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004136 /* Create the "f" field that contains a list of fetches. */
4137 lua_pushstring(L, "f");
4138 if (!hlua_fetches_new(L, &appctx->htxn, 0))
4139 return 0;
4140 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004141
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004142 /* Create the "sf" field that contains a list of stringsafe fetches. */
4143 lua_pushstring(L, "sf");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004144 if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004145 return 0;
4146 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004147
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004148 /* Create the "c" field that contains a list of converters. */
4149 lua_pushstring(L, "c");
4150 if (!hlua_converters_new(L, &appctx->htxn, 0))
4151 return 0;
4152 lua_settable(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004153
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004154 /* Create the "sc" field that contains a list of stringsafe converters. */
4155 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01004156 if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004157 return 0;
4158 lua_settable(L, -3);
Willy Tarreaueee5b512015-04-03 23:46:31 +02004159
Christopher Fauleta2097962019-07-15 16:25:33 +02004160 htx = htxbuf(&s->req.buf);
4161 blk = htx_get_first_blk(htx);
Christopher Fauletea009732019-11-18 15:50:25 +01004162 BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL);
Christopher Fauleta2097962019-07-15 16:25:33 +02004163 sl = htx_get_blk_ptr(htx, blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004164
Christopher Fauleta2097962019-07-15 16:25:33 +02004165 /* Stores the request method. */
4166 lua_pushstring(L, "method");
4167 lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl));
4168 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004169
Christopher Fauleta2097962019-07-15 16:25:33 +02004170 /* Stores the http version. */
4171 lua_pushstring(L, "version");
4172 lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl));
4173 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004174
Christopher Fauleta2097962019-07-15 16:25:33 +02004175 /* creates an array of headers. hlua_http_get_headers() crates and push
4176 * the array on the top of the stack.
4177 */
4178 lua_pushstring(L, "headers");
4179 htxn.s = s;
4180 htxn.p = px;
4181 htxn.dir = SMP_OPT_DIR_REQ;
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004182 if (!hlua_http_get_headers(L, &htxn.s->txn->req))
Christopher Fauleta2097962019-07-15 16:25:33 +02004183 return 0;
4184 lua_settable(L, -3);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004185
Christopher Fauleta2097962019-07-15 16:25:33 +02004186 path = http_get_path(htx_sl_req_uri(sl));
Tim Duesterhused526372020-03-05 17:56:33 +01004187 if (isttest(path)) {
Christopher Fauleta2097962019-07-15 16:25:33 +02004188 char *p, *q, *end;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004189
Christopher Fauleta2097962019-07-15 16:25:33 +02004190 p = path.ptr;
4191 end = path.ptr + path.len;
4192 q = p;
4193 while (q < end && *q != '?')
4194 q++;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004195
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004196 /* Stores the request path. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004197 lua_pushstring(L, "path");
4198 lua_pushlstring(L, p, q - p);
Thierry FOURNIER7d388632017-02-22 02:06:16 +01004199 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004200
Christopher Fauleta2097962019-07-15 16:25:33 +02004201 /* Stores the query string. */
4202 lua_pushstring(L, "qs");
4203 if (*q == '?')
4204 q++;
4205 lua_pushlstring(L, q, end - q);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004206 lua_settable(L, -3);
Christopher Fauleta2097962019-07-15 16:25:33 +02004207 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004208
Christopher Fauleta2097962019-07-15 16:25:33 +02004209 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4210 struct htx_blk *blk = htx_get_blk(htx, pos);
4211 enum htx_blk_type type = htx_get_blk_type(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004212
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004213 if (type == HTX_BLK_TLR || type == HTX_BLK_EOT)
Christopher Fauleta2097962019-07-15 16:25:33 +02004214 break;
4215 if (type == HTX_BLK_DATA)
4216 len += htx_get_blksz(blk);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004217 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004218 if (htx->extra != ULLONG_MAX)
4219 len += htx->extra;
4220
4221 /* Stores the request path. */
4222 lua_pushstring(L, "length");
4223 lua_pushinteger(L, len);
4224 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004225
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004226 /* Create an empty array of HTTP request headers. */
4227 lua_pushstring(L, "response");
4228 lua_newtable(L);
4229 lua_settable(L, -3);
Thierry FOURNIER04c57b32015-03-18 13:43:10 +01004230
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004231 /* Pop a class stream metatable and affect it to the table. */
4232 lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref);
4233 lua_setmetatable(L, -2);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004234
4235 return 1;
4236}
4237
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004238__LJMP static int hlua_applet_http_set_var(lua_State *L)
4239{
4240 struct hlua_appctx *appctx;
4241 struct stream *s;
4242 const char *name;
4243 size_t len;
4244 struct sample smp;
4245
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004246 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
4247 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004248
4249 /* It is useles to retrieve the stream, but this function
4250 * runs only in a stream context.
4251 */
4252 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4253 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4254 s = appctx->htxn.s;
4255
4256 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01004257 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004258 hlua_lua2smp(L, 3, &smp);
4259
4260 /* Store the sample in a variable. */
4261 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02004262
4263 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
4264 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
4265 else
4266 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
4267
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004268 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004269}
4270
4271__LJMP static int hlua_applet_http_unset_var(lua_State *L)
4272{
4273 struct hlua_appctx *appctx;
4274 struct stream *s;
4275 const char *name;
4276 size_t len;
4277 struct sample smp;
4278
4279 MAY_LJMP(check_args(L, 2, "unset_var"));
4280
4281 /* It is useles to retrieve the stream, but this function
4282 * runs only in a stream context.
4283 */
4284 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4285 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4286 s = appctx->htxn.s;
4287
4288 /* Unset the variable. */
4289 smp_set_owner(&smp, s->be, s->sess, s, 0);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02004290 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
4291 return 1;
Thierry FOURNIER / OZON.IO4394a2c2016-12-12 12:31:54 +01004292}
4293
4294__LJMP static int hlua_applet_http_get_var(lua_State *L)
4295{
4296 struct hlua_appctx *appctx;
4297 struct stream *s;
4298 const char *name;
4299 size_t len;
4300 struct sample smp;
4301
4302 MAY_LJMP(check_args(L, 2, "get_var"));
4303
4304 /* It is useles to retrieve the stream, but this function
4305 * runs only in a stream context.
4306 */
4307 appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4308 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
4309 s = appctx->htxn.s;
4310
4311 smp_set_owner(&smp, s->be, s->sess, s, 0);
4312 if (!vars_get_by_name(name, len, &smp)) {
4313 lua_pushnil(L);
4314 return 1;
4315 }
4316
4317 return hlua_smp2lua(L, &smp);
4318}
4319
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004320__LJMP static int hlua_applet_http_set_priv(lua_State *L)
4321{
4322 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4323 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004324 struct hlua *hlua;
4325
4326 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004327 if (!s->hlua)
4328 return 0;
4329 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004330
4331 MAY_LJMP(check_args(L, 2, "set_priv"));
4332
4333 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02004334 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004335
4336 /* Get and store new value. */
4337 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
4338 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
4339
4340 return 0;
4341}
4342
4343__LJMP static int hlua_applet_http_get_priv(lua_State *L)
4344{
4345 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4346 struct stream *s = appctx->htxn.s;
Thierry FOURNIER3b0a6d42016-12-16 08:48:32 +01004347 struct hlua *hlua;
4348
4349 /* Note that this hlua struct is from the session and not from the applet. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01004350 if (!s->hlua) {
4351 lua_pushnil(L);
4352 return 1;
4353 }
4354 hlua = s->hlua;
Thierry FOURNIER8db004c2015-12-25 01:33:18 +01004355
4356 /* Push configuration index in the stack. */
4357 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
4358
4359 return 1;
4360}
4361
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004362/* If expected data not yet available, it returns a yield. This function
4363 * consumes the data in the buffer. It returns a string containing the
4364 * data. This string can be empty.
4365 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004366__LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004367{
4368 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4369 struct stream_interface *si = appctx->appctx->owner;
4370 struct channel *req = si_oc(si);
4371 struct htx *htx;
4372 struct htx_blk *blk;
4373 size_t count;
4374 int stop = 0;
4375
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004376 htx = htx_from_buf(&req->buf);
4377 count = co_data(req);
Christopher Fauleta3f15502019-05-13 15:27:23 +02004378 blk = htx_get_first_blk(htx);
Christopher Fauletcc26b132018-12-18 21:20:57 +01004379
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004380 while (count && !stop && blk) {
4381 enum htx_blk_type type = htx_get_blk_type(blk);
4382 uint32_t sz = htx_get_blksz(blk);
4383 struct ist v;
4384 uint32_t vlen;
4385 char *nl;
4386
4387 vlen = sz;
4388 if (vlen > count) {
4389 if (type != HTX_BLK_DATA)
4390 break;
4391 vlen = count;
4392 }
4393
4394 switch (type) {
4395 case HTX_BLK_UNUSED:
4396 break;
4397
4398 case HTX_BLK_DATA:
4399 v = htx_get_blk_value(htx, blk);
4400 v.len = vlen;
4401 nl = istchr(v, '\n');
4402 if (nl != NULL) {
4403 stop = 1;
4404 vlen = nl - v.ptr + 1;
4405 }
4406 luaL_addlstring(&appctx->b, v.ptr, vlen);
4407 break;
4408
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004409 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004410 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004411 stop = 1;
4412 break;
4413
4414 default:
4415 break;
4416 }
4417
4418 co_set_data(req, co_data(req) - vlen);
4419 count -= vlen;
4420 if (sz == vlen)
4421 blk = htx_remove_blk(htx, blk);
4422 else {
4423 htx_cut_data_blk(htx, blk, vlen);
4424 break;
4425 }
4426 }
4427
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004428 htx_to_buf(htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004429 if (!stop) {
4430 si_cant_get(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004431 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004432 }
4433
4434 /* return the result. */
4435 luaL_pushresult(&appctx->b);
4436 return 1;
4437}
4438
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004439
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004440/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004441__LJMP static int hlua_applet_http_getline(lua_State *L)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004442{
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004443 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004444
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004445 /* Initialise the string catenation. */
4446 luaL_buffinit(L, &appctx->b);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004447
Christopher Fauleta2097962019-07-15 16:25:33 +02004448 return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01004449}
4450
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004451/* If expected data not yet available, it returns a yield. This function
4452 * consumes the data in the buffer. It returns a string containing the
4453 * data. This string can be empty.
4454 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004455__LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004456{
4457 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4458 struct stream_interface *si = appctx->appctx->owner;
4459 struct channel *req = si_oc(si);
4460 struct htx *htx;
4461 struct htx_blk *blk;
4462 size_t count;
4463 int len;
4464
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004465 htx = htx_from_buf(&req->buf);
4466 len = MAY_LJMP(luaL_checkinteger(L, 2));
4467 count = co_data(req);
Christopher Faulet29f17582019-05-23 11:03:26 +02004468 blk = htx_get_head_blk(htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004469 while (count && len && blk) {
4470 enum htx_blk_type type = htx_get_blk_type(blk);
4471 uint32_t sz = htx_get_blksz(blk);
4472 struct ist v;
4473 uint32_t vlen;
4474
4475 vlen = sz;
4476 if (len > 0 && vlen > len)
4477 vlen = len;
4478 if (vlen > count) {
4479 if (type != HTX_BLK_DATA)
4480 break;
4481 vlen = count;
4482 }
4483
4484 switch (type) {
4485 case HTX_BLK_UNUSED:
4486 break;
4487
4488 case HTX_BLK_DATA:
4489 v = htx_get_blk_value(htx, blk);
4490 luaL_addlstring(&appctx->b, v.ptr, vlen);
4491 break;
4492
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004493 case HTX_BLK_TLR:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01004494 case HTX_BLK_EOT:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004495 len = 0;
4496 break;
4497
4498 default:
4499 break;
4500 }
4501
4502 co_set_data(req, co_data(req) - vlen);
4503 count -= vlen;
4504 if (len > 0)
4505 len -= vlen;
4506 if (sz == vlen)
4507 blk = htx_remove_blk(htx, blk);
4508 else {
4509 htx_cut_data_blk(htx, blk, vlen);
4510 break;
4511 }
4512 }
4513
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004514 htx_to_buf(htx, &req->buf);
4515
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004516 /* If we are no other data available, yield waiting for new data. */
4517 if (len) {
4518 if (len > 0) {
4519 lua_pushinteger(L, len);
4520 lua_replace(L, 2);
4521 }
4522 si_cant_get(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004523 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004524 }
4525
4526 /* return the result. */
4527 luaL_pushresult(&appctx->b);
4528 return 1;
4529}
4530
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08004531/* Check arguments for the function "hlua_channel_get_yield". */
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004532__LJMP static int hlua_applet_http_recv(lua_State *L)
4533{
4534 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4535 int len = -1;
4536
4537 /* Check arguments. */
4538 if (lua_gettop(L) > 2)
4539 WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments."));
4540 if (lua_gettop(L) >= 2) {
4541 len = MAY_LJMP(luaL_checkinteger(L, 2));
4542 lua_pop(L, 1);
4543 }
4544
Christopher Fauleta2097962019-07-15 16:25:33 +02004545 lua_pushinteger(L, len);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004546
Christopher Fauleta2097962019-07-15 16:25:33 +02004547 /* Initialise the string catenation. */
4548 luaL_buffinit(L, &appctx->b);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004549
Christopher Fauleta2097962019-07-15 16:25:33 +02004550 return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004551}
4552
4553/* Append data in the output side of the buffer. This data is immediately
4554 * sent. The function returns the amount of data written. If the buffer
4555 * cannot contain the data, the function yields. The function returns -1
4556 * if the channel is closed.
4557 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004558__LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004559{
4560 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4561 struct stream_interface *si = appctx->appctx->owner;
4562 struct channel *res = si_ic(si);
4563 struct htx *htx = htx_from_buf(&res->buf);
4564 const char *data;
4565 size_t len;
4566 int l = MAY_LJMP(luaL_checkinteger(L, 3));
4567 int max;
4568
Christopher Faulet9060fc02019-07-03 11:39:30 +02004569 max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx));
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004570 if (!max)
4571 goto snd_yield;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004572
4573 data = MAY_LJMP(luaL_checklstring(L, 2, &len));
4574
4575 /* Get the max amount of data which can write as input in the channel. */
4576 if (max > (len - l))
4577 max = len - l;
4578
4579 /* Copy data. */
Willy Tarreau0a7ef022019-05-28 10:30:11 +02004580 max = htx_add_data(htx, ist2(data + l, max));
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004581 channel_add_input(res, max);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004582
4583 /* update counters. */
4584 l += max;
4585 lua_pop(L, 1);
4586 lua_pushinteger(L, l);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004587
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004588 /* If some data is not send, declares the situation to the
4589 * applet, and returns a yield.
4590 */
4591 if (l < len) {
Christopher Faulet4b0e9b22019-01-09 12:16:58 +01004592 snd_yield:
Christopher Faulet0ae79d02019-02-27 21:36:59 +01004593 htx_to_buf(htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004594 si_rx_room_blk(si);
Willy Tarreau9635e032018-10-16 17:52:55 +02004595 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004596 }
4597
Christopher Fauleta2097962019-07-15 16:25:33 +02004598 htx_to_buf(htx, &res->buf);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004599 return 1;
4600}
4601
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004602/* Just a wrapper of "hlua_applet_send_yield". This wrapper permits
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004603 * yield the LUA process, and resume it without checking the
4604 * input arguments.
4605 */
4606__LJMP static int hlua_applet_http_send(lua_State *L)
4607{
4608 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004609
4610 /* We want to send some data. Headers must be sent. */
4611 if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
4612 hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data.");
4613 WILL_LJMP(lua_error(L));
4614 }
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004615
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004616 /* This integer is used for followinf the amount of data sent. */
Christopher Fauleta2097962019-07-15 16:25:33 +02004617 lua_pushinteger(L, 0);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004618
Christopher Fauleta2097962019-07-15 16:25:33 +02004619 return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004620}
4621
4622__LJMP static int hlua_applet_http_addheader(lua_State *L)
4623{
4624 const char *name;
4625 int ret;
4626
4627 MAY_LJMP(hlua_checkapplet_http(L, 1));
4628 name = MAY_LJMP(luaL_checkstring(L, 2));
4629 MAY_LJMP(luaL_checkstring(L, 3));
4630
4631 /* Push in the stack the "response" entry. */
4632 ret = lua_getfield(L, 1, "response");
4633 if (ret != LUA_TTABLE) {
4634 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] "
4635 "is expected as an array. %s found", lua_typename(L, ret));
4636 WILL_LJMP(lua_error(L));
4637 }
4638
4639 /* check if the header is already registered if it is not
4640 * the case, register it.
4641 */
4642 ret = lua_getfield(L, -1, name);
4643 if (ret == LUA_TNIL) {
4644
4645 /* Entry not found. */
4646 lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */
4647
4648 /* Insert the new header name in the array in the top of the stack.
4649 * It left the new array in the top of the stack.
4650 */
4651 lua_newtable(L);
4652 lua_pushvalue(L, 2);
4653 lua_pushvalue(L, -2);
4654 lua_settable(L, -4);
4655
4656 } else if (ret != LUA_TTABLE) {
4657
4658 /* corruption error. */
4659 hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] "
4660 "is expected as an array. %s found", name, lua_typename(L, ret));
4661 WILL_LJMP(lua_error(L));
4662 }
4663
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004664 /* Now the top of thestack is an array of values. We push
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004665 * the header value as new entry.
4666 */
4667 lua_pushvalue(L, 3);
4668 ret = lua_rawlen(L, -2);
4669 lua_rawseti(L, -2, ret + 1);
4670 lua_pushboolean(L, 1);
4671 return 1;
4672}
4673
4674__LJMP static int hlua_applet_http_status(lua_State *L)
4675{
4676 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4677 int status = MAY_LJMP(luaL_checkinteger(L, 2));
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004678 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004679
4680 if (status < 100 || status > 599) {
4681 lua_pushboolean(L, 0);
4682 return 1;
4683 }
4684
4685 appctx->appctx->ctx.hlua_apphttp.status = status;
Robin H. Johnson52f5db22017-01-01 13:10:52 -08004686 appctx->appctx->ctx.hlua_apphttp.reason = reason;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004687 lua_pushboolean(L, 1);
4688 return 1;
4689}
4690
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004691
Christopher Fauleta2097962019-07-15 16:25:33 +02004692__LJMP static int hlua_applet_http_send_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004693{
4694 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4695 struct stream_interface *si = appctx->appctx->owner;
4696 struct channel *res = si_ic(si);
4697 struct htx *htx;
4698 struct htx_sl *sl;
4699 struct h1m h1m;
4700 const char *status, *reason;
4701 const char *name, *value;
4702 size_t nlen, vlen;
4703 unsigned int flags;
4704
4705 /* Send the message at once. */
4706 htx = htx_from_buf(&res->buf);
4707 h1m_init_res(&h1m);
4708
4709 /* Use the same http version than the request. */
4710 status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size);
4711 reason = appctx->appctx->ctx.hlua_apphttp.reason;
4712 if (reason == NULL)
4713 reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status);
4714 if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) {
4715 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
4716 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason));
4717 }
4718 else {
4719 flags = HTX_SL_F_IS_RESP;
4720 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason));
4721 }
4722 if (!sl) {
4723 hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004724 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004725 WILL_LJMP(lua_error(L));
4726 }
4727 sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status;
4728
4729 /* Get the array associated to the field "response" in the object AppletHTTP. */
4730 lua_pushvalue(L, 0);
4731 if (lua_getfield(L, 1, "response") != LUA_TTABLE) {
4732 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004733 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004734 WILL_LJMP(lua_error(L));
4735 }
4736
4737 /* Browse the list of headers. */
4738 lua_pushnil(L);
4739 while(lua_next(L, -2) != 0) {
4740 /* We expect a string as -2. */
4741 if (lua_type(L, -2) != LUA_TSTRING) {
4742 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004743 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004744 lua_typename(L, lua_type(L, -2)));
4745 WILL_LJMP(lua_error(L));
4746 }
4747 name = lua_tolstring(L, -2, &nlen);
4748
4749 /* We expect an array as -1. */
4750 if (lua_type(L, -1) != LUA_TTABLE) {
4751 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004752 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004753 name,
4754 lua_typename(L, lua_type(L, -1)));
4755 WILL_LJMP(lua_error(L));
4756 }
4757
4758 /* Browse the table who is on the top of the stack. */
4759 lua_pushnil(L);
4760 while(lua_next(L, -2) != 0) {
4761 int id;
4762
4763 /* We expect a number as -2. */
4764 if (lua_type(L, -2) != LUA_TNUMBER) {
4765 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004766 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004767 name,
4768 lua_typename(L, lua_type(L, -2)));
4769 WILL_LJMP(lua_error(L));
4770 }
4771 id = lua_tointeger(L, -2);
4772
4773 /* We expect a string as -2. */
4774 if (lua_type(L, -1) != LUA_TSTRING) {
4775 hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004776 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004777 name, id,
4778 lua_typename(L, lua_type(L, -1)));
4779 WILL_LJMP(lua_error(L));
4780 }
4781 value = lua_tolstring(L, -1, &vlen);
4782
4783 /* Simple Protocol checks. */
4784 if (isteqi(ist2(name, nlen), ist("transfer-encoding")))
Christopher Faulet700d9e82020-01-31 12:21:52 +01004785 h1_parse_xfer_enc_header(&h1m, ist2(value, vlen));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004786 else if (isteqi(ist2(name, nlen), ist("content-length"))) {
4787 struct ist v = ist2(value, vlen);
4788 int ret;
4789
4790 ret = h1_parse_cont_len_header(&h1m, &v);
4791 if (ret < 0) {
4792 hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004793 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004794 name);
4795 WILL_LJMP(lua_error(L));
4796 }
4797 else if (ret == 0)
4798 goto next; /* Skip it */
4799 }
4800
4801 /* Add a new header */
4802 if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) {
4803 hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004804 appctx->appctx->rule->arg.hlua_rule->fcn->name,
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004805 name);
4806 WILL_LJMP(lua_error(L));
4807 }
4808 next:
4809 /* Remove the array from the stack, and get next element with a remaining string. */
4810 lua_pop(L, 1);
4811 }
4812
4813 /* Remove the array from the stack, and get next element with a remaining string. */
4814 lua_pop(L, 1);
4815 }
4816
4817 if (h1m.flags & H1_MF_CHNK)
4818 h1m.flags &= ~H1_MF_CLEN;
4819 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
4820 h1m.flags |= H1_MF_XFER_LEN;
4821
4822 /* Uset HTX start-line flags */
4823 if (h1m.flags & H1_MF_XFER_ENC)
4824 flags |= HTX_SL_F_XFER_ENC;
4825 if (h1m.flags & H1_MF_XFER_LEN) {
4826 flags |= HTX_SL_F_XFER_LEN;
4827 if (h1m.flags & H1_MF_CHNK)
4828 flags |= HTX_SL_F_CHNK;
4829 else if (h1m.flags & H1_MF_CLEN)
4830 flags |= HTX_SL_F_CLEN;
4831 if (h1m.body_len == 0)
4832 flags |= HTX_SL_F_BODYLESS;
4833 }
4834 sl->flags |= flags;
4835
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07004836 /* If we don't have a content-length set, and the HTTP version is 1.1
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004837 * and the status code implies the presence of a message body, we must
4838 * announce a transfer encoding chunked. This is required by haproxy
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05004839 * for the keepalive compliance. If the applet announces a transfer-encoding
4840 * chunked itself, don't do anything.
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004841 */
4842 if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 &&
4843 appctx->appctx->ctx.hlua_apphttp.status >= 200 &&
4844 appctx->appctx->ctx.hlua_apphttp.status != 204 &&
4845 appctx->appctx->ctx.hlua_apphttp.status != 304) {
4846 /* Add a new header */
4847 sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN);
4848 if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) {
4849 hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004850 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004851 WILL_LJMP(lua_error(L));
4852 }
4853 }
4854
4855 /* Finalize headers. */
4856 if (!htx_add_endof(htx, HTX_BLK_EOH)) {
4857 hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01004858 appctx->appctx->rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004859 WILL_LJMP(lua_error(L));
4860 }
4861
4862 if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) {
4863 b_reset(&res->buf);
4864 hlua_pusherror(L, "Lua: 'start_response': response header block too big");
4865 WILL_LJMP(lua_error(L));
4866 }
4867
4868 htx_to_buf(htx, &res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01004869 channel_add_input(res, htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004870
4871 /* Headers sent, set the flag. */
4872 appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT;
4873 return 0;
4874
4875}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004876/* We will build the status line and the headers of the HTTP response.
4877 * We will try send at once if its not possible, we give back the hand
4878 * waiting for more room.
4879 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004880__LJMP static int hlua_applet_http_start_response_yield(lua_State *L, int status, lua_KContext ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004881{
4882 struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1));
4883 struct stream_interface *si = appctx->appctx->owner;
4884 struct channel *res = si_ic(si);
4885
4886 if (co_data(res)) {
4887 si_rx_room_blk(si);
Christopher Fauleta2097962019-07-15 16:25:33 +02004888 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_start_response_yield, TICK_ETERNITY, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004889 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004890 return MAY_LJMP(hlua_applet_http_send_response(L));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004891}
4892
4893
Christopher Fauleta2097962019-07-15 16:25:33 +02004894__LJMP static int hlua_applet_http_start_response(lua_State *L)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004895{
Christopher Fauleta2097962019-07-15 16:25:33 +02004896 return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004897}
4898
Christopher Fauleta2097962019-07-15 16:25:33 +02004899/*
4900 *
4901 *
4902 * Class HTTP
4903 *
4904 *
Christopher Faulet9c832fc2018-12-14 13:34:05 +01004905 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004906
4907/* Returns a struct hlua_txn if the stack entry "ud" is
4908 * a class stream, otherwise it throws an error.
4909 */
4910__LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004911{
Christopher Fauleta2097962019-07-15 16:25:33 +02004912 return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref));
4913}
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004914
Christopher Fauleta2097962019-07-15 16:25:33 +02004915/* This function creates and push in the stack a HTTP object
4916 * according with a current TXN.
4917 */
4918static int hlua_http_new(lua_State *L, struct hlua_txn *txn)
4919{
4920 struct hlua_txn *htxn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004921
Christopher Fauleta2097962019-07-15 16:25:33 +02004922 /* Check stack size. */
4923 if (!lua_checkstack(L, 3))
4924 return 0;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004925
Christopher Fauleta2097962019-07-15 16:25:33 +02004926 /* Create the object: obj[0] = userdata.
4927 * Note that the base of the Converters object is the
4928 * same than the TXN object.
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004929 */
Christopher Fauleta2097962019-07-15 16:25:33 +02004930 lua_newtable(L);
4931 htxn = lua_newuserdata(L, sizeof(*htxn));
4932 lua_rawseti(L, -2, 0);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004933
4934 htxn->s = txn->s;
4935 htxn->p = txn->p;
Christopher Faulet256b69a2019-05-23 11:14:21 +02004936 htxn->dir = txn->dir;
4937 htxn->flags = txn->flags;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004938
4939 /* Pop a class stream metatable and affect it to the table. */
4940 lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref);
4941 lua_setmetatable(L, -2);
4942
4943 return 1;
4944}
4945
4946/* This function creates ans returns an array of HTTP headers.
4947 * This function does not fails. It is used as wrapper with the
4948 * 2 following functions.
4949 */
Christopher Faulet9d1332b2020-02-24 16:46:16 +01004950__LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004951{
Christopher Fauleta2097962019-07-15 16:25:33 +02004952 struct htx *htx;
4953 int32_t pos;
4954
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004955 /* Create the table. */
4956 lua_newtable(L);
4957
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02004958
Christopher Fauleta2097962019-07-15 16:25:33 +02004959 htx = htxbuf(&msg->chn->buf);
4960 for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) {
4961 struct htx_blk *blk = htx_get_blk(htx, pos);
4962 enum htx_blk_type type = htx_get_blk_type(blk);
4963 struct ist n, v;
4964 int len;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004965
Christopher Fauleta2097962019-07-15 16:25:33 +02004966 if (type == HTX_BLK_HDR) {
4967 n = htx_get_blk_name(htx,blk);
4968 v = htx_get_blk_value(htx, blk);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004969 }
Christopher Fauleta2097962019-07-15 16:25:33 +02004970 else if (type == HTX_BLK_EOH)
4971 break;
4972 else
4973 continue;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004974
Christopher Fauleta2097962019-07-15 16:25:33 +02004975 /* Check for existing entry:
4976 * assume that the table is on the top of the stack, and
4977 * push the key in the stack, the function lua_gettable()
4978 * perform the lookup.
4979 */
4980 lua_pushlstring(L, n.ptr, n.len);
4981 lua_gettable(L, -2);
Christopher Faulet724a12c2018-12-13 22:12:15 +01004982
Christopher Fauleta2097962019-07-15 16:25:33 +02004983 switch (lua_type(L, -1)) {
4984 case LUA_TNIL:
4985 /* Table not found, create it. */
4986 lua_pop(L, 1); /* remove the nil value. */
4987 lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */
4988 lua_newtable(L); /* create and push empty table. */
4989 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4990 lua_rawseti(L, -2, 0); /* index header value (pop it). */
4991 lua_rawset(L, -3); /* index new table with header name (pop the values). */
Christopher Faulet724a12c2018-12-13 22:12:15 +01004992 break;
Christopher Faulet724a12c2018-12-13 22:12:15 +01004993
Christopher Fauleta2097962019-07-15 16:25:33 +02004994 case LUA_TTABLE:
4995 /* Entry found: push the value in the table. */
4996 len = lua_rawlen(L, -1);
4997 lua_pushlstring(L, v.ptr, v.len); /* push header value. */
4998 lua_rawseti(L, -2, len+1); /* index header value (pop it). */
4999 lua_pop(L, 1); /* remove the table (it is stored in the main table). */
5000 break;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005001
Christopher Fauleta2097962019-07-15 16:25:33 +02005002 default:
5003 /* Other cases are errors. */
5004 hlua_pusherror(L, "internal error during the parsing of headers.");
5005 WILL_LJMP(lua_error(L));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005006 }
5007 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005008 return 1;
5009}
5010
5011__LJMP static int hlua_http_req_get_headers(lua_State *L)
5012{
5013 struct hlua_txn *htxn;
5014
5015 MAY_LJMP(check_args(L, 1, "req_get_headers"));
5016 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5017
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005018 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005019 WILL_LJMP(lua_error(L));
5020
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005021 return hlua_http_get_headers(L, &htxn->s->txn->req);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005022}
5023
5024__LJMP static int hlua_http_res_get_headers(lua_State *L)
5025{
5026 struct hlua_txn *htxn;
5027
5028 MAY_LJMP(check_args(L, 1, "res_get_headers"));
5029 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5030
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005031 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005032 WILL_LJMP(lua_error(L));
5033
Christopher Faulet9d1332b2020-02-24 16:46:16 +01005034 return hlua_http_get_headers(L, &htxn->s->txn->rsp);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005035}
5036
5037/* This function replace full header, or just a value in
5038 * the request or in the response. It is a wrapper fir the
5039 * 4 following functions.
5040 */
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005041__LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full)
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005042{
5043 size_t name_len;
5044 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5045 const char *reg = MAY_LJMP(luaL_checkstring(L, 3));
5046 const char *value = MAY_LJMP(luaL_checkstring(L, 4));
Christopher Fauleta2097962019-07-15 16:25:33 +02005047 struct htx *htx;
Dragan Dosen26743032019-04-30 15:54:36 +02005048 struct my_regex *re;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005049
Dragan Dosen26743032019-04-30 15:54:36 +02005050 if (!(re = regex_comp(reg, 1, 1, NULL)))
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005051 WILL_LJMP(luaL_argerror(L, 3, "invalid regex"));
5052
Christopher Fauleta2097962019-07-15 16:25:33 +02005053 htx = htxbuf(&msg->chn->buf);
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005054 http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full);
Dragan Dosen26743032019-04-30 15:54:36 +02005055 regex_free(re);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005056 return 0;
5057}
5058
5059__LJMP static int hlua_http_req_rep_hdr(lua_State *L)
5060{
5061 struct hlua_txn *htxn;
5062
5063 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5064 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5065
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005066 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005067 WILL_LJMP(lua_error(L));
5068
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005069 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005070}
5071
5072__LJMP static int hlua_http_res_rep_hdr(lua_State *L)
5073{
5074 struct hlua_txn *htxn;
5075
5076 MAY_LJMP(check_args(L, 4, "res_rep_hdr"));
5077 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5078
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005079 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005080 WILL_LJMP(lua_error(L));
5081
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005082 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005083}
5084
5085__LJMP static int hlua_http_req_rep_val(lua_State *L)
5086{
5087 struct hlua_txn *htxn;
5088
5089 MAY_LJMP(check_args(L, 4, "req_rep_hdr"));
5090 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5091
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005092 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005093 WILL_LJMP(lua_error(L));
5094
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005095 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0));
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02005096}
5097
5098__LJMP static int hlua_http_res_rep_val(lua_State *L)
5099{
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005100 struct hlua_txn *htxn;
5101
5102 MAY_LJMP(check_args(L, 4, "res_rep_val"));
5103 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5104
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005105 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005106 WILL_LJMP(lua_error(L));
5107
Christopher Fauletd1914aa2020-02-24 16:52:46 +01005108 return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005109}
5110
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005111/* This function deletes all the occurrences of an header.
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005112 * It is a wrapper for the 2 following functions.
5113 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005114__LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005115{
5116 size_t len;
5117 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005118 struct htx *htx = htxbuf(&msg->chn->buf);
5119 struct http_hdr_ctx ctx;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005120
Christopher Fauleta2097962019-07-15 16:25:33 +02005121 ctx.blk = NULL;
5122 while (http_find_header(htx, ist2(name, len), &ctx, 1))
5123 http_remove_header(htx, &ctx);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005124 return 0;
5125}
5126
5127__LJMP static int hlua_http_req_del_hdr(lua_State *L)
5128{
5129 struct hlua_txn *htxn;
5130
5131 MAY_LJMP(check_args(L, 2, "req_del_hdr"));
5132 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5133
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005134 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005135 WILL_LJMP(lua_error(L));
5136
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005137 return hlua_http_del_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005138}
5139
5140__LJMP static int hlua_http_res_del_hdr(lua_State *L)
5141{
5142 struct hlua_txn *htxn;
5143
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005144 MAY_LJMP(check_args(L, 2, "res_del_hdr"));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005145 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5146
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005147 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005148 WILL_LJMP(lua_error(L));
5149
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005150 return hlua_http_del_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005151}
5152
5153/* This function adds an header. It is a wrapper used by
5154 * the 2 following functions.
5155 */
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005156__LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg)
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005157{
5158 size_t name_len;
5159 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
5160 size_t value_len;
5161 const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len));
Christopher Fauleta2097962019-07-15 16:25:33 +02005162 struct htx *htx = htxbuf(&msg->chn->buf);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005163
Christopher Fauleta2097962019-07-15 16:25:33 +02005164 lua_pushboolean(L, http_add_header(htx, ist2(name, name_len),
5165 ist2(value, value_len)));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005166 return 0;
5167}
5168
5169__LJMP static int hlua_http_req_add_hdr(lua_State *L)
5170{
5171 struct hlua_txn *htxn;
5172
5173 MAY_LJMP(check_args(L, 3, "req_add_hdr"));
5174 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5175
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005176 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005177 WILL_LJMP(lua_error(L));
5178
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005179 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005180}
5181
5182__LJMP static int hlua_http_res_add_hdr(lua_State *L)
5183{
5184 struct hlua_txn *htxn;
5185
5186 MAY_LJMP(check_args(L, 3, "res_add_hdr"));
5187 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5188
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005189 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005190 WILL_LJMP(lua_error(L));
5191
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005192 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005193}
5194
5195static int hlua_http_req_set_hdr(lua_State *L)
5196{
5197 struct hlua_txn *htxn;
5198
5199 MAY_LJMP(check_args(L, 3, "req_set_hdr"));
5200 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5201
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005202 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005203 WILL_LJMP(lua_error(L));
5204
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005205 hlua_http_del_hdr(L, &htxn->s->txn->req);
5206 return hlua_http_add_hdr(L, &htxn->s->txn->req);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005207}
5208
5209static int hlua_http_res_set_hdr(lua_State *L)
5210{
5211 struct hlua_txn *htxn;
5212
5213 MAY_LJMP(check_args(L, 3, "res_set_hdr"));
5214 htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5215
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005216 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005217 WILL_LJMP(lua_error(L));
5218
Christopher Fauletd31c7b32020-02-25 09:37:57 +01005219 hlua_http_del_hdr(L, &htxn->s->txn->rsp);
5220 return hlua_http_add_hdr(L, &htxn->s->txn->rsp);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005221}
5222
5223/* This function set the method. */
5224static int hlua_http_req_set_meth(lua_State *L)
5225{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005226 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005227 size_t name_len;
5228 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005229
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005230 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005231 WILL_LJMP(lua_error(L));
5232
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005233 lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005234 return 1;
5235}
5236
5237/* This function set the method. */
5238static int hlua_http_req_set_path(lua_State *L)
5239{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005240 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005241 size_t name_len;
5242 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER / OZON.IOb84ae922016-08-02 23:44:58 +02005243
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005244 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005245 WILL_LJMP(lua_error(L));
5246
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005247 lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005248 return 1;
5249}
5250
5251/* This function set the query-string. */
5252static int hlua_http_req_set_query(lua_State *L)
5253{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005254 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005255 size_t name_len;
5256 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005257
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005258 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005259 WILL_LJMP(lua_error(L));
5260
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005261 /* Check length. */
5262 if (name_len > trash.size - 1) {
5263 lua_pushboolean(L, 0);
5264 return 1;
5265 }
5266
5267 /* Add the mark question as prefix. */
5268 chunk_reset(&trash);
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005269 trash.area[trash.data++] = '?';
5270 memcpy(trash.area + trash.data, name, name_len);
5271 trash.data += name_len;
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005272
Willy Tarreau843b7cb2018-07-13 10:54:26 +02005273 lua_pushboolean(L,
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005274 http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005275 return 1;
5276}
5277
5278/* This function set the uri. */
5279static int hlua_http_req_set_uri(lua_State *L)
5280{
Willy Tarreaubcb39cc2015-04-06 11:21:44 +02005281 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005282 size_t name_len;
5283 const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len));
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005284
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005285 if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005286 WILL_LJMP(lua_error(L));
5287
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005288 lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005289 return 1;
5290}
5291
Robin H. Johnson52f5db22017-01-01 13:10:52 -08005292/* This function set the response code & optionally reason. */
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005293static int hlua_http_res_set_status(lua_State *L)
5294{
5295 struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1));
5296 unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2));
Christopher Faulet96bff762019-12-17 13:46:18 +01005297 const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
5298 const struct ist reason = ist2(str, (str ? strlen(str) : 0));
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005299
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005300 if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s))
Christopher Faulet84a6d5b2019-07-26 16:17:01 +02005301 WILL_LJMP(lua_error(L));
5302
Christopher Fauletfc9cfe42019-07-16 14:54:53 +02005303 http_res_set_status(code, reason, htxn->s);
Thierry FOURNIER35d70ef2015-08-26 16:21:56 +02005304 return 0;
5305}
5306
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005307/*
5308 *
5309 *
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005310 * Class TXN
5311 *
5312 *
5313 */
5314
5315/* Returns a struct hlua_session if the stack entry "ud" is
Willy Tarreau87b09662015-04-03 00:22:06 +02005316 * a class stream, otherwise it throws an error.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005317 */
5318__LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud)
5319{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02005320 return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref));
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005321}
5322
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005323__LJMP static int hlua_set_var(lua_State *L)
5324{
5325 struct hlua_txn *htxn;
5326 const char *name;
5327 size_t len;
5328 struct sample smp;
5329
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005330 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
5331 WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments"));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005332
5333 /* It is useles to retrieve the stream, but this function
5334 * runs only in a stream context.
5335 */
5336 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5337 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5338
5339 /* Converts the third argument in a sample. */
Amaury Denoyellebc0af6a2020-10-29 17:21:20 +01005340 memset(&smp, 0, sizeof(smp));
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005341 hlua_lua2smp(L, 3, &smp);
5342
5343 /* Store the sample in a variable. */
Willy Tarreau7560dd42016-03-10 16:28:58 +01005344 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus4e172c92020-05-19 13:49:42 +02005345
5346 if (lua_gettop(L) == 4 && lua_toboolean(L, 4))
5347 lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0);
5348 else
5349 lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0);
5350
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005351 return 1;
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005352}
5353
Christopher Faulet85d79c92016-11-09 16:54:56 +01005354__LJMP static int hlua_unset_var(lua_State *L)
5355{
5356 struct hlua_txn *htxn;
5357 const char *name;
5358 size_t len;
5359 struct sample smp;
5360
5361 MAY_LJMP(check_args(L, 2, "unset_var"));
5362
5363 /* It is useles to retrieve the stream, but this function
5364 * runs only in a stream context.
5365 */
5366 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5367 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5368
5369 /* Unset the variable. */
5370 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Tim Duesterhus84ebc132020-05-19 13:49:41 +02005371 lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0);
5372 return 1;
Christopher Faulet85d79c92016-11-09 16:54:56 +01005373}
5374
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005375__LJMP static int hlua_get_var(lua_State *L)
5376{
5377 struct hlua_txn *htxn;
5378 const char *name;
5379 size_t len;
5380 struct sample smp;
5381
5382 MAY_LJMP(check_args(L, 2, "get_var"));
5383
5384 /* It is useles to retrieve the stream, but this function
5385 * runs only in a stream context.
5386 */
5387 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5388 name = MAY_LJMP(luaL_checklstring(L, 2, &len));
5389
Willy Tarreau7560dd42016-03-10 16:28:58 +01005390 smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR);
Willy Tarreau6204cd92016-03-10 16:33:04 +01005391 if (!vars_get_by_name(name, len, &smp)) {
Thierry FOURNIER053ba8ad2015-06-08 13:05:33 +02005392 lua_pushnil(L);
5393 return 1;
5394 }
5395
5396 return hlua_smp2lua(L, &smp);
5397}
5398
Willy Tarreau59551662015-03-10 14:23:13 +01005399__LJMP static int hlua_set_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005400{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005401 struct hlua *hlua;
5402
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005403 MAY_LJMP(check_args(L, 2, "set_priv"));
5404
Willy Tarreau87b09662015-04-03 00:22:06 +02005405 /* It is useles to retrieve the stream, but this function
5406 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005407 */
5408 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005409
5410 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005411 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005412 if (!hlua)
5413 return 0;
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005414
5415 /* Remove previous value. */
Thierry FOURNIERe068b602017-04-26 13:27:05 +02005416 luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005417
5418 /* Get and store new value. */
5419 lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */
5420 hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */
5421
5422 return 0;
5423}
5424
Willy Tarreau59551662015-03-10 14:23:13 +01005425__LJMP static int hlua_get_priv(lua_State *L)
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005426{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005427 struct hlua *hlua;
5428
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005429 MAY_LJMP(check_args(L, 1, "get_priv"));
5430
Willy Tarreau87b09662015-04-03 00:22:06 +02005431 /* It is useles to retrieve the stream, but this function
5432 * runs only in a stream context.
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005433 */
5434 MAY_LJMP(hlua_checktxn(L, 1));
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005435
5436 /* Get hlua struct, or NULL if we execute from main lua state */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01005437 hlua = hlua_gethlua(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01005438 if (!hlua) {
5439 lua_pushnil(L);
5440 return 1;
5441 }
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01005442
5443 /* Push configuration index in the stack. */
5444 lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref);
5445
5446 return 1;
5447}
5448
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005449/* Create stack entry containing a class TXN. This function
5450 * return 0 if the stack does not contains free slots,
5451 * otherwise it returns 1.
5452 */
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005453static int hlua_txn_new(lua_State *L, struct stream *s, struct proxy *p, int dir, int flags)
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005454{
Willy Tarreaude491382015-04-06 11:04:28 +02005455 struct hlua_txn *htxn;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005456
5457 /* Check stack size. */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005458 if (!lua_checkstack(L, 3))
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005459 return 0;
5460
5461 /* NOTE: The allocation never fails. The failure
5462 * throw an error, and the function never returns.
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08005463 * if the throw is not available, the process is aborted.
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005464 */
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005465 /* Create the object: obj[0] = userdata. */
5466 lua_newtable(L);
Willy Tarreaude491382015-04-06 11:04:28 +02005467 htxn = lua_newuserdata(L, sizeof(*htxn));
Thierry FOURNIER2297bc22015-03-11 17:43:33 +01005468 lua_rawseti(L, -2, 0);
5469
Willy Tarreaude491382015-04-06 11:04:28 +02005470 htxn->s = s;
5471 htxn->p = p;
Thierry FOURNIERc4eebc82015-11-02 10:01:59 +01005472 htxn->dir = dir;
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005473 htxn->flags = flags;
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005474
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005475 /* Create the "f" field that contains a list of fetches. */
5476 lua_pushstring(L, "f");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005477 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005478 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005479 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005480
5481 /* Create the "sf" field that contains a list of stringsafe fetches. */
5482 lua_pushstring(L, "sf");
Thierry FOURNIERca988662015-12-20 18:43:03 +01005483 if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP | HLUA_F_AS_STRING))
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005484 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005485 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01005486
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005487 /* Create the "c" field that contains a list of converters. */
5488 lua_pushstring(L, "c");
Willy Tarreaude491382015-04-06 11:04:28 +02005489 if (!hlua_converters_new(L, htxn, 0))
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005490 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005491 lua_rawset(L, -3);
Thierry FOURNIER2694a1a2015-03-11 20:13:36 +01005492
5493 /* Create the "sc" field that contains a list of stringsafe converters. */
5494 lua_pushstring(L, "sc");
Thierry FOURNIER7fa05492015-12-20 18:42:25 +01005495 if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING))
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005496 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005497 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01005498
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005499 /* Create the "req" field that contains the request channel object. */
5500 lua_pushstring(L, "req");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005501 if (!hlua_channel_new(L, &s->req))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005502 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005503 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005504
5505 /* Create the "res" field that contains the response channel object. */
5506 lua_pushstring(L, "res");
Willy Tarreau2a71af42015-03-10 13:51:50 +01005507 if (!hlua_channel_new(L, &s->res))
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005508 return 0;
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005509 lua_rawset(L, -3);
Thierry FOURNIER397826a2015-03-11 19:39:09 +01005510
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005511 /* Creates the HTTP object is the current proxy allows http. */
5512 lua_pushstring(L, "http");
5513 if (p->mode == PR_MODE_HTTP) {
Willy Tarreaude491382015-04-06 11:04:28 +02005514 if (!hlua_http_new(L, htxn))
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005515 return 0;
5516 }
5517 else
5518 lua_pushnil(L);
Thierry FOURNIER84e73c82015-09-25 22:13:32 +02005519 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01005520
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01005521 /* Pop a class sesison metatable and affect it to the userdata. */
5522 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref);
5523 lua_setmetatable(L, -2);
5524
5525 return 1;
5526}
5527
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005528__LJMP static int hlua_txn_deflog(lua_State *L)
5529{
5530 const char *msg;
5531 struct hlua_txn *htxn;
5532
5533 MAY_LJMP(check_args(L, 2, "deflog"));
5534 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5535 msg = MAY_LJMP(luaL_checkstring(L, 2));
5536
5537 hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg);
5538 return 0;
5539}
5540
5541__LJMP static int hlua_txn_log(lua_State *L)
5542{
5543 int level;
5544 const char *msg;
5545 struct hlua_txn *htxn;
5546
5547 MAY_LJMP(check_args(L, 3, "log"));
5548 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5549 level = MAY_LJMP(luaL_checkinteger(L, 2));
5550 msg = MAY_LJMP(luaL_checkstring(L, 3));
5551
5552 if (level < 0 || level >= NB_LOG_LEVELS)
5553 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
5554
5555 hlua_sendlog(htxn->s->be, level, msg);
5556 return 0;
5557}
5558
5559__LJMP static int hlua_txn_log_debug(lua_State *L)
5560{
5561 const char *msg;
5562 struct hlua_txn *htxn;
5563
5564 MAY_LJMP(check_args(L, 2, "Debug"));
5565 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5566 msg = MAY_LJMP(luaL_checkstring(L, 2));
5567 hlua_sendlog(htxn->s->be, LOG_DEBUG, msg);
5568 return 0;
5569}
5570
5571__LJMP static int hlua_txn_log_info(lua_State *L)
5572{
5573 const char *msg;
5574 struct hlua_txn *htxn;
5575
5576 MAY_LJMP(check_args(L, 2, "Info"));
5577 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5578 msg = MAY_LJMP(luaL_checkstring(L, 2));
5579 hlua_sendlog(htxn->s->be, LOG_INFO, msg);
5580 return 0;
5581}
5582
5583__LJMP static int hlua_txn_log_warning(lua_State *L)
5584{
5585 const char *msg;
5586 struct hlua_txn *htxn;
5587
5588 MAY_LJMP(check_args(L, 2, "Warning"));
5589 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5590 msg = MAY_LJMP(luaL_checkstring(L, 2));
5591 hlua_sendlog(htxn->s->be, LOG_WARNING, msg);
5592 return 0;
5593}
5594
5595__LJMP static int hlua_txn_log_alert(lua_State *L)
5596{
5597 const char *msg;
5598 struct hlua_txn *htxn;
5599
5600 MAY_LJMP(check_args(L, 2, "Alert"));
5601 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5602 msg = MAY_LJMP(luaL_checkstring(L, 2));
5603 hlua_sendlog(htxn->s->be, LOG_ALERT, msg);
5604 return 0;
5605}
5606
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005607__LJMP static int hlua_txn_set_loglevel(lua_State *L)
5608{
5609 struct hlua_txn *htxn;
5610 int ll;
5611
5612 MAY_LJMP(check_args(L, 2, "set_loglevel"));
5613 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5614 ll = MAY_LJMP(luaL_checkinteger(L, 2));
5615
5616 if (ll < 0 || ll > 7)
5617 WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7"));
5618
5619 htxn->s->logs.level = ll;
5620 return 0;
5621}
5622
5623__LJMP static int hlua_txn_set_tos(lua_State *L)
5624{
5625 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005626 int tos;
5627
5628 MAY_LJMP(check_args(L, 2, "set_tos"));
5629 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5630 tos = MAY_LJMP(luaL_checkinteger(L, 2));
5631
Willy Tarreau1a18b542018-12-11 16:37:42 +01005632 conn_set_tos(objt_conn(htxn->s->sess->origin), tos);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005633 return 0;
5634}
5635
5636__LJMP static int hlua_txn_set_mark(lua_State *L)
5637{
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005638 struct hlua_txn *htxn;
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005639 int mark;
5640
5641 MAY_LJMP(check_args(L, 2, "set_mark"));
5642 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5643 mark = MAY_LJMP(luaL_checkinteger(L, 2));
5644
Lukas Tribus579e3e32019-08-11 18:03:45 +02005645 conn_set_mark(objt_conn(htxn->s->sess->origin), mark);
Thierry FOURNIER2cce3532015-03-16 12:04:16 +01005646 return 0;
5647}
5648
Patrick Hemmer268a7072018-05-11 12:52:31 -04005649__LJMP static int hlua_txn_set_priority_class(lua_State *L)
5650{
5651 struct hlua_txn *htxn;
5652
5653 MAY_LJMP(check_args(L, 2, "set_priority_class"));
5654 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5655 htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2)));
5656 return 0;
5657}
5658
5659__LJMP static int hlua_txn_set_priority_offset(lua_State *L)
5660{
5661 struct hlua_txn *htxn;
5662
5663 MAY_LJMP(check_args(L, 2, "set_priority_offset"));
5664 htxn = MAY_LJMP(hlua_checktxn(L, 1));
5665 htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2)));
5666 return 0;
5667}
5668
Christopher Faulet700d9e82020-01-31 12:21:52 +01005669/* Forward the Reply object to the client. This function converts the reply in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05005670 * HTX an push it to into the response channel. It is response to forward the
Christopher Faulet700d9e82020-01-31 12:21:52 +01005671 * message and terminate the transaction. It returns 1 on success and 0 on
5672 * error. The Reply must be on top of the stack.
5673 */
5674__LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s)
5675{
5676 struct htx *htx;
5677 struct htx_sl *sl;
5678 struct h1m h1m;
5679 const char *status, *reason, *body;
5680 size_t status_len, reason_len, body_len;
5681 int ret, code, flags;
5682
5683 code = 200;
5684 status = "200";
5685 status_len = 3;
5686 ret = lua_getfield(L, -1, "status");
5687 if (ret == LUA_TNUMBER) {
5688 code = lua_tointeger(L, -1);
5689 status = lua_tolstring(L, -1, &status_len);
5690 }
5691 lua_pop(L, 1);
5692
5693 reason = http_get_reason(code);
5694 reason_len = strlen(reason);
5695 ret = lua_getfield(L, -1, "reason");
5696 if (ret == LUA_TSTRING)
5697 reason = lua_tolstring(L, -1, &reason_len);
5698 lua_pop(L, 1);
5699
5700 body = NULL;
5701 body_len = 0;
5702 ret = lua_getfield(L, -1, "body");
5703 if (ret == LUA_TSTRING)
5704 body = lua_tolstring(L, -1, &body_len);
5705 lua_pop(L, 1);
5706
5707 /* Prepare the response before inserting the headers */
5708 h1m_init_res(&h1m);
5709 htx = htx_from_buf(&s->res.buf);
5710 channel_htx_truncate(&s->res, htx);
5711 if (s->txn->req.flags & HTTP_MSGF_VER_11) {
5712 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11);
5713 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"),
5714 ist2(status, status_len), ist2(reason, reason_len));
5715 }
5716 else {
5717 flags = HTX_SL_F_IS_RESP;
5718 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"),
5719 ist2(status, status_len), ist2(reason, reason_len));
5720 }
5721 if (!sl)
5722 goto fail;
5723 sl->info.res.status = code;
5724
5725 /* Push in the stack the "headers" entry. */
5726 ret = lua_getfield(L, -1, "headers");
5727 if (ret != LUA_TTABLE)
5728 goto skip_headers;
5729
5730 lua_pushnil(L);
5731 while (lua_next(L, -2) != 0) {
5732 struct ist name, value;
5733 const char *n, *v;
5734 size_t nlen, vlen;
5735
5736 if (!lua_isstring(L, -2) || !lua_istable(L, -1)) {
5737 /* Skip element if the key is not a string or if the value is not a table */
5738 goto next_hdr;
5739 }
5740
5741 n = lua_tolstring(L, -2, &nlen);
5742 name = ist2(n, nlen);
5743 if (isteqi(name, ist("content-length"))) {
5744 /* Always skip content-length header. It will be added
5745 * later with the correct len
5746 */
5747 goto next_hdr;
5748 }
5749
5750 /* Loop on header's values */
5751 lua_pushnil(L);
5752 while (lua_next(L, -2)) {
5753 if (!lua_isstring(L, -1)) {
5754 /* Skip the value if it is not a string */
5755 goto next_value;
5756 }
5757
5758 v = lua_tolstring(L, -1, &vlen);
5759 value = ist2(v, vlen);
5760
5761 if (isteqi(name, ist("transfer-encoding")))
5762 h1_parse_xfer_enc_header(&h1m, value);
5763 if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen)))
5764 goto fail;
5765
5766 next_value:
5767 lua_pop(L, 1);
5768 }
5769
5770 next_hdr:
5771 lua_pop(L, 1);
5772 }
5773 skip_headers:
5774 lua_pop(L, 1);
5775
5776 /* Update h1m flags: CLEN is set if CHNK is not present */
5777 if (!(h1m.flags & H1_MF_CHNK)) {
5778 const char *clen = ultoa(body_len);
5779
5780 h1m.flags |= H1_MF_CLEN;
5781 if (!htx_add_header(htx, ist("content-length"), ist(clen)))
5782 goto fail;
5783 }
5784 if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK))
5785 h1m.flags |= H1_MF_XFER_LEN;
5786
5787 /* Update HTX start-line flags */
5788 if (h1m.flags & H1_MF_XFER_ENC)
5789 flags |= HTX_SL_F_XFER_ENC;
5790 if (h1m.flags & H1_MF_XFER_LEN) {
5791 flags |= HTX_SL_F_XFER_LEN;
5792 if (h1m.flags & H1_MF_CHNK)
5793 flags |= HTX_SL_F_CHNK;
5794 else if (h1m.flags & H1_MF_CLEN)
5795 flags |= HTX_SL_F_CLEN;
5796 if (h1m.body_len == 0)
5797 flags |= HTX_SL_F_BODYLESS;
5798 }
5799 sl->flags |= flags;
5800
5801
5802 if (!htx_add_endof(htx, HTX_BLK_EOH) ||
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005803 (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))))
Christopher Faulet700d9e82020-01-31 12:21:52 +01005804 goto fail;
5805
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01005806 htx->flags |= HTX_FL_EOM;
5807
Christopher Faulet700d9e82020-01-31 12:21:52 +01005808 /* Now, forward the response and terminate the transaction */
5809 s->txn->status = code;
5810 htx_to_buf(htx, &s->res.buf);
5811 if (!http_forward_proxy_resp(s, 1))
5812 goto fail;
5813
5814 return 1;
5815
5816 fail:
5817 channel_htx_truncate(&s->res, htx);
5818 return 0;
5819}
5820
5821/* Terminate a transaction if called from a lua action. For TCP streams,
5822 * processing is just aborted. Nothing is returned to the client and all
5823 * arguments are ignored. For HTTP streams, if a reply is passed as argument, it
5824 * is forwarded to the client before terminating the transaction. On success,
5825 * the function exits with ACT_RET_DONE code. If an error occurred, it exits
5826 * with ACT_RET_ERR code. If this function is not called from a lua action, it
5827 * just exits without any processing.
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005828 */
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005829__LJMP static int hlua_txn_done(lua_State *L)
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005830{
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005831 struct hlua_txn *htxn;
Christopher Faulet700d9e82020-01-31 12:21:52 +01005832 struct stream *s;
5833 int finst;
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005834
Willy Tarreaub2ccb562015-04-06 11:11:15 +02005835 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005836
Christopher Faulet700d9e82020-01-31 12:21:52 +01005837 /* If the flags NOTERM is set, we cannot terminate the session, so we
5838 * just end the execution of the current lua code. */
5839 if (htxn->flags & HLUA_TXN_NOTERM)
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005840 WILL_LJMP(hlua_done(L));
Thierry FOURNIERab00df62016-07-14 11:42:37 +02005841
Christopher Faulet700d9e82020-01-31 12:21:52 +01005842 s = htxn->s;
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005843 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005844 struct channel *req = &s->req;
5845 struct channel *res = &s->res;
Willy Tarreau81389672015-03-10 12:03:52 +01005846
Christopher Faulet700d9e82020-01-31 12:21:52 +01005847 channel_auto_read(req);
5848 channel_abort(req);
5849 channel_auto_close(req);
5850 channel_erase(req);
5851
5852 res->wex = tick_add_ifset(now_ms, res->wto);
5853 channel_auto_read(res);
5854 channel_auto_close(res);
5855 channel_shutr_now(res);
5856
5857 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D);
5858 goto done;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005859 }
Thierry FOURNIER10ec2142015-08-24 17:23:45 +02005860
Christopher Faulet700d9e82020-01-31 12:21:52 +01005861 if (lua_gettop(L) == 1 || !lua_istable(L, 2)) {
5862 /* No reply or invalid reply */
5863 s->txn->status = 0;
5864 http_reply_and_close(s, 0, NULL);
5865 }
5866 else {
5867 /* Remove extra args to have the reply on top of the stack */
5868 if (lua_gettop(L) > 2)
5869 lua_pop(L, lua_gettop(L) - 2);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01005870
Christopher Faulet700d9e82020-01-31 12:21:52 +01005871 if (!hlua_txn_forward_reply(L, s)) {
5872 if (!(s->flags & SF_ERR_MASK))
5873 s->flags |= SF_ERR_PRXCOND;
5874 lua_pushinteger(L, ACT_RET_ERR);
5875 WILL_LJMP(hlua_done(L));
5876 return 0; /* Never reached */
5877 }
Christopher Faulet4d0e2632019-07-16 10:52:40 +02005878 }
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005879
Christopher Faulet700d9e82020-01-31 12:21:52 +01005880 finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H);
5881 if (htxn->dir == SMP_OPT_DIR_REQ) {
5882 /* let's log the request time */
5883 s->logs.tv_request = now;
5884 if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */
5885 _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1);
5886 }
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005887
Christopher Faulet700d9e82020-01-31 12:21:52 +01005888 done:
5889 if (!(s->flags & SF_ERR_MASK))
5890 s->flags |= SF_ERR_LOCAL;
5891 if (!(s->flags & SF_FINST_MASK))
5892 s->flags |= finst;
Christopher Fauletfe6a71b2019-07-26 16:40:24 +02005893
Christopher Faulet4ad73102020-03-05 11:07:31 +01005894 lua_pushinteger(L, ACT_RET_ABRT);
Thierry FOURNIER4bb375c2015-08-26 08:42:21 +02005895 WILL_LJMP(hlua_done(L));
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01005896 return 0;
5897}
5898
Christopher Faulet700d9e82020-01-31 12:21:52 +01005899/*
5900 *
5901 *
5902 * Class REPLY
5903 *
5904 *
5905 */
5906
5907/* Pushes the TXN reply onto the top of the stack. If the stask does not have a
5908 * free slots, the function fails and returns 0;
5909 */
5910static int hlua_txn_reply_new(lua_State *L)
5911{
5912 struct hlua_txn *htxn;
5913 const char *reason, *body = NULL;
5914 int ret, status;
5915
5916 htxn = MAY_LJMP(hlua_checktxn(L, 1));
Christopher Fauletd8f0e072020-02-25 09:45:51 +01005917 if (!IS_HTX_STRM(htxn->s)) {
Christopher Faulet700d9e82020-01-31 12:21:52 +01005918 hlua_pusherror(L, "txn object is not an HTTP transaction.");
5919 WILL_LJMP(lua_error(L));
5920 }
5921
5922 /* Default value */
5923 status = 200;
5924 reason = http_get_reason(status);
5925
5926 if (lua_istable(L, 2)) {
5927 /* load status and reason from the table argument at index 2 */
5928 ret = lua_getfield(L, 2, "status");
5929 if (ret == LUA_TNIL)
5930 goto reason;
5931 else if (ret != LUA_TNUMBER) {
5932 /* invalid status: ignore the reason */
5933 goto body;
5934 }
5935 status = lua_tointeger(L, -1);
5936
5937 reason:
5938 lua_pop(L, 1); /* restore the stack: remove status */
5939 ret = lua_getfield(L, 2, "reason");
5940 if (ret == LUA_TSTRING)
5941 reason = lua_tostring(L, -1);
5942
5943 body:
5944 lua_pop(L, 1); /* restore the stack: remove invalid status or reason */
5945 ret = lua_getfield(L, 2, "body");
5946 if (ret == LUA_TSTRING)
5947 body = lua_tostring(L, -1);
5948 lua_pop(L, 1); /* restore the stack: remove body */
5949 }
5950
5951 /* Create the Reply table */
5952 lua_newtable(L);
5953
5954 /* Add status element */
5955 lua_pushstring(L, "status");
5956 lua_pushinteger(L, status);
5957 lua_settable(L, -3);
5958
5959 /* Add reason element */
5960 reason = http_get_reason(status);
5961 lua_pushstring(L, "reason");
5962 lua_pushstring(L, reason);
5963 lua_settable(L, -3);
5964
5965 /* Add body element, nil if undefined */
5966 lua_pushstring(L, "body");
5967 if (body)
5968 lua_pushstring(L, body);
5969 else
5970 lua_pushnil(L);
5971 lua_settable(L, -3);
5972
5973 /* Add headers element */
5974 lua_pushstring(L, "headers");
5975 lua_newtable(L);
5976
5977 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
5978 if (lua_istable(L, 2)) {
5979 /* load headers from the table argument at index 2. If it is a table, copy it. */
5980 ret = lua_getfield(L, 2, "headers");
5981 if (ret == LUA_TTABLE) {
5982 /* stack: [ ... <headers:table>, <table> ] */
5983 lua_pushnil(L);
5984 while (lua_next(L, -2) != 0) {
5985 /* stack: [ ... <headers:table>, <table>, k, v] */
5986 if (!lua_isstring(L, -1) && !lua_istable(L, -1)) {
5987 /* invalid value type, skip it */
5988 lua_pop(L, 1);
5989 continue;
5990 }
5991
5992
5993 /* Duplicate the key and swap it with the value. */
5994 lua_pushvalue(L, -2);
5995 lua_insert(L, -2);
5996 /* stack: [ ... <headers:table>, <table>, k, k, v ] */
5997
5998 lua_newtable(L);
5999 lua_insert(L, -2);
6000 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */
6001
6002 if (lua_isstring(L, -1)) {
6003 /* push the value in the inner table */
6004 lua_rawseti(L, -2, 1);
6005 }
6006 else { /* table */
6007 lua_pushnil(L);
6008 while (lua_next(L, -2) != 0) {
6009 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */
6010 if (!lua_isstring(L, -1)) {
6011 /* invalid value type, skip it*/
6012 lua_pop(L, 1);
6013 continue;
6014 }
6015 /* push the value in the inner table */
6016 lua_rawseti(L, -4, lua_rawlen(L, -4) + 1);
6017 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */
6018 }
6019 lua_pop(L, 1);
6020 /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */
6021 }
6022
6023 /* push (k,v) on the stack in the headers table:
6024 * stack: [ ... <headers:table>, <table>, k, k, v ]
6025 */
6026 lua_settable(L, -5);
6027 /* stack: [ ... <headers:table>, <table>, k ] */
6028 }
6029 }
6030 lua_pop(L, 1);
6031 }
6032 /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */
6033 lua_settable(L, -3);
6034 /* stack: [ txn, <Arg:table>, <Reply:table> ] */
6035
6036 /* Pop a class sesison metatable and affect it to the userdata. */
6037 lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref);
6038 lua_setmetatable(L, -2);
6039 return 1;
6040}
6041
6042/* Set the reply status code, and optionally the reason. If no reason is
6043 * provided, the default one corresponding to the status code is used.
6044 */
6045__LJMP static int hlua_txn_reply_set_status(lua_State *L)
6046{
6047 int status = MAY_LJMP(luaL_checkinteger(L, 2));
6048 const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL));
6049
6050 /* First argument (self) must be a table */
6051 luaL_checktype(L, 1, LUA_TTABLE);
6052
6053 if (status < 100 || status > 599) {
6054 lua_pushboolean(L, 0);
6055 return 1;
6056 }
6057 if (!reason)
6058 reason = http_get_reason(status);
6059
6060 lua_pushinteger(L, status);
6061 lua_setfield(L, 1, "status");
6062
6063 lua_pushstring(L, reason);
6064 lua_setfield(L, 1, "reason");
6065
6066 lua_pushboolean(L, 1);
6067 return 1;
6068}
6069
6070/* Add a header into the reply object. Each header name is associated to an
6071 * array of values in the "headers" table. If the header name is not found, a
6072 * new entry is created.
6073 */
6074__LJMP static int hlua_txn_reply_add_header(lua_State *L)
6075{
6076 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6077 const char *value = MAY_LJMP(luaL_checkstring(L, 3));
6078 int ret;
6079
6080 /* First argument (self) must be a table */
6081 luaL_checktype(L, 1, LUA_TTABLE);
6082
6083 /* Push in the stack the "headers" entry. */
6084 ret = lua_getfield(L, 1, "headers");
6085 if (ret != LUA_TTABLE) {
6086 hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret));
6087 WILL_LJMP(lua_error(L));
6088 }
6089
6090 /* check if the header is already registered. If not, register it. */
6091 ret = lua_getfield(L, -1, name);
6092 if (ret == LUA_TNIL) {
6093 /* Entry not found. */
6094 lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */
6095
6096 /* Insert the new header name in the array in the top of the stack.
6097 * It left the new array in the top of the stack.
6098 */
6099 lua_newtable(L);
6100 lua_pushstring(L, name);
6101 lua_pushvalue(L, -2);
6102 lua_settable(L, -4);
6103 }
6104 else if (ret != LUA_TTABLE) {
6105 hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret));
6106 WILL_LJMP(lua_error(L));
6107 }
6108
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07006109 /* Now the top of thestack is an array of values. We push
Christopher Faulet700d9e82020-01-31 12:21:52 +01006110 * the header value as new entry.
6111 */
6112 lua_pushstring(L, value);
6113 ret = lua_rawlen(L, -2);
6114 lua_rawseti(L, -2, ret + 1);
6115
6116 lua_pushboolean(L, 1);
6117 return 1;
6118}
6119
6120/* Remove all occurrences of a given header name. */
6121__LJMP static int hlua_txn_reply_del_header(lua_State *L)
6122{
6123 const char *name = MAY_LJMP(luaL_checkstring(L, 2));
6124 int ret;
6125
6126 /* First argument (self) must be a table */
6127 luaL_checktype(L, 1, LUA_TTABLE);
6128
6129 /* Push in the stack the "headers" entry. */
6130 ret = lua_getfield(L, 1, "headers");
6131 if (ret != LUA_TTABLE) {
6132 hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret));
6133 WILL_LJMP(lua_error(L));
6134 }
6135
6136 lua_pushstring(L, name);
6137 lua_pushnil(L);
6138 lua_settable(L, -3);
6139
6140 lua_pushboolean(L, 1);
6141 return 1;
6142}
6143
6144/* Set the reply's body. Overwrite any existing entry. */
6145__LJMP static int hlua_txn_reply_set_body(lua_State *L)
6146{
6147 const char *payload = MAY_LJMP(luaL_checkstring(L, 2));
6148
6149 /* First argument (self) must be a table */
6150 luaL_checktype(L, 1, LUA_TTABLE);
6151
6152 lua_pushstring(L, payload);
6153 lua_setfield(L, 1, "body");
6154
6155 lua_pushboolean(L, 1);
6156 return 1;
6157}
6158
Thierry FOURNIERc798b5d2015-03-17 01:09:57 +01006159__LJMP static int hlua_log(lua_State *L)
6160{
6161 int level;
6162 const char *msg;
6163
6164 MAY_LJMP(check_args(L, 2, "log"));
6165 level = MAY_LJMP(luaL_checkinteger(L, 1));
6166 msg = MAY_LJMP(luaL_checkstring(L, 2));
6167
6168 if (level < 0 || level >= NB_LOG_LEVELS)
6169 WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel."));
6170
6171 hlua_sendlog(NULL, level, msg);
6172 return 0;
6173}
6174
6175__LJMP static int hlua_log_debug(lua_State *L)
6176{
6177 const char *msg;
6178
6179 MAY_LJMP(check_args(L, 1, "debug"));
6180 msg = MAY_LJMP(luaL_checkstring(L, 1));
6181 hlua_sendlog(NULL, LOG_DEBUG, msg);
6182 return 0;
6183}
6184
6185__LJMP static int hlua_log_info(lua_State *L)
6186{
6187 const char *msg;
6188
6189 MAY_LJMP(check_args(L, 1, "info"));
6190 msg = MAY_LJMP(luaL_checkstring(L, 1));
6191 hlua_sendlog(NULL, LOG_INFO, msg);
6192 return 0;
6193}
6194
6195__LJMP static int hlua_log_warning(lua_State *L)
6196{
6197 const char *msg;
6198
6199 MAY_LJMP(check_args(L, 1, "warning"));
6200 msg = MAY_LJMP(luaL_checkstring(L, 1));
6201 hlua_sendlog(NULL, LOG_WARNING, msg);
6202 return 0;
6203}
6204
6205__LJMP static int hlua_log_alert(lua_State *L)
6206{
6207 const char *msg;
6208
6209 MAY_LJMP(check_args(L, 1, "alert"));
6210 msg = MAY_LJMP(luaL_checkstring(L, 1));
6211 hlua_sendlog(NULL, LOG_ALERT, msg);
Thierry FOURNIER893bfa32015-02-17 18:42:34 +01006212 return 0;
6213}
6214
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006215__LJMP static int hlua_sleep_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006216{
6217 int wakeup_ms = lua_tointeger(L, -1);
6218 if (now_ms < wakeup_ms)
Willy Tarreau9635e032018-10-16 17:52:55 +02006219 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006220 return 0;
6221}
6222
6223__LJMP static int hlua_sleep(lua_State *L)
6224{
6225 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006226 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006227
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006228 MAY_LJMP(check_args(L, 1, "sleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006229
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006230 delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006231 wakeup_ms = tick_add(now_ms, delay);
6232 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006233
Willy Tarreau9635e032018-10-16 17:52:55 +02006234 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006235 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006236}
6237
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006238__LJMP static int hlua_msleep(lua_State *L)
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006239{
6240 unsigned int delay;
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006241 unsigned int wakeup_ms;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006242
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006243 MAY_LJMP(check_args(L, 1, "msleep"));
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006244
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006245 delay = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006246 wakeup_ms = tick_add(now_ms, delay);
6247 lua_pushinteger(L, wakeup_ms);
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006248
Willy Tarreau9635e032018-10-16 17:52:55 +02006249 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_sleep_yield, wakeup_ms, 0));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006250 return 0;
Thierry FOURNIER5b8608f2015-02-16 19:43:25 +01006251}
6252
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006253/* This functionis an LUA binding. it permits to give back
6254 * the hand at the HAProxy scheduler. It is used when the
6255 * LUA processing consumes a lot of time.
6256 */
Thierry FOURNIERf90838b2015-03-06 13:48:32 +01006257__LJMP static int hlua_yield_yield(lua_State *L, int status, lua_KContext ctx)
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006258{
6259 return 0;
6260}
6261
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006262__LJMP static int hlua_yield(lua_State *L)
6263{
Willy Tarreau9635e032018-10-16 17:52:55 +02006264 MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_yield_yield, TICK_ETERNITY, HLUA_CTRLYIELD));
Thierry FOURNIERd44731f2015-03-04 15:51:09 +01006265 return 0;
Thierry FOURNIER13416fe2015-02-17 15:01:59 +01006266}
6267
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006268/* This function change the nice of the currently executed
6269 * task. It is used set low or high priority at the current
6270 * task.
6271 */
Willy Tarreau59551662015-03-10 14:23:13 +01006272__LJMP static int hlua_set_nice(lua_State *L)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006273{
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006274 struct hlua *hlua;
6275 int nice;
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006276
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006277 MAY_LJMP(check_args(L, 1, "set_nice"));
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006278 nice = MAY_LJMP(luaL_checkinteger(L, 1));
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006279
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006280 /* Get hlua struct, or NULL if we execute from main lua state */
6281 hlua = hlua_gethlua(L);
6282
6283 /* If the task is not set, I'm in a start mode. */
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006284 if (!hlua || !hlua->task)
6285 return 0;
6286
6287 if (nice < -1024)
6288 nice = -1024;
Willy Tarreau80f5fae2015-02-27 16:38:20 +01006289 else if (nice > 1024)
Thierry FOURNIER37196f42015-02-16 19:34:56 +01006290 nice = 1024;
6291
6292 hlua->task->nice = nice;
6293 return 0;
6294}
6295
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006296/* This function is used as a callback of a task. It is called by the
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006297 * HAProxy task subsystem when the task is awaked. The LUA runtime can
6298 * return an E_AGAIN signal, the emmiter of this signal must set a
6299 * signal to wake the task.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006300 *
6301 * Task wrapper are longjmp safe because the only one Lua code
6302 * executed is the safe hlua_ctx_resume();
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006303 */
Willy Tarreau144f84a2021-03-02 16:09:26 +01006304struct task *hlua_process_task(struct task *task, void *context, unsigned int state)
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006305{
Olivier Houchard9f6af332018-05-25 14:04:04 +02006306 struct hlua *hlua = context;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006307 enum hlua_exec status;
6308
Christopher Faulet5bc99722018-04-25 10:34:45 +02006309 if (task->thread_mask == MAX_THREADS_MASK)
6310 task_set_affinity(task, tid_bit);
6311
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006312 /* If it is the first call to the task, we must initialize the
6313 * execution timeouts.
6314 */
6315 if (!HLUA_IS_RUNNING(hlua))
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02006316 hlua->max_time = hlua_timeout_task;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006317
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006318 /* Execute the Lua code. */
6319 status = hlua_ctx_resume(hlua, 1);
6320
6321 switch (status) {
6322 /* finished or yield */
6323 case HLUA_E_OK:
6324 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006325 task_destroy(task);
Tim Duesterhuscd235c62018-04-24 13:56:01 +02006326 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006327 break;
6328
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006329 case HLUA_E_AGAIN: /* co process or timeout wake me later. */
Thierry FOURNIERcb146882017-12-10 17:10:57 +01006330 notification_gc(&hlua->com);
PiBa-NLfe971b32018-05-02 22:27:14 +02006331 task->expire = hlua->wake_time;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006332 break;
6333
6334 /* finished with error. */
6335 case HLUA_E_ERRMSG:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006336 SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006337 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006338 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006339 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006340 break;
6341
6342 case HLUA_E_ERR:
6343 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006344 SEND_ERR(NULL, "Lua task: unknown error.\n");
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006345 hlua_ctx_destroy(hlua);
Olivier Houchard3f795f72019-04-17 22:51:06 +02006346 task_destroy(task);
Emeric Brun253e53e2017-10-17 18:58:40 +02006347 task = NULL;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006348 break;
6349 }
Emeric Brun253e53e2017-10-17 18:58:40 +02006350 return task;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006351}
6352
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006353/* This function is an LUA binding that register LUA function to be
6354 * executed after the HAProxy configuration parsing and before the
6355 * HAProxy scheduler starts. This function expect only one LUA
6356 * argument that is a function. This function returns nothing, but
6357 * throws if an error is encountered.
6358 */
6359__LJMP static int hlua_register_init(lua_State *L)
6360{
6361 struct hlua_init_function *init;
6362 int ref;
6363
6364 MAY_LJMP(check_args(L, 1, "register_init"));
6365
6366 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6367
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006368 init = calloc(1, sizeof(*init));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006369 if (!init)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006370 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006371
6372 init->function_ref = ref;
Thierry Fournierc7492592020-11-28 23:57:24 +01006373 LIST_ADDQ(&hlua_init_functions[hlua_state_id], &init->l);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01006374 return 0;
6375}
6376
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006377/* This functio is an LUA binding. It permits to register a task
6378 * executed in parallel of the main HAroxy activity. The task is
6379 * created and it is set in the HAProxy scheduler. It can be called
6380 * from the "init" section, "post init" or during the runtime.
6381 *
6382 * Lua prototype:
6383 *
6384 * <none> core.register_task(<function>)
6385 */
6386static int hlua_register_task(lua_State *L)
6387{
6388 struct hlua *hlua;
6389 struct task *task;
6390 int ref;
Thierry Fournier021d9862020-11-28 23:42:03 +01006391 int state_id;
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006392
6393 MAY_LJMP(check_args(L, 1, "register_task"));
6394
6395 ref = MAY_LJMP(hlua_checkfunction(L, 1));
6396
Thierry Fournier75fc0292020-11-28 13:18:56 +01006397 /* Get the reference state. If the reference is NULL, L is the master
6398 * state, otherwise hlua->T is.
6399 */
6400 hlua = hlua_gethlua(L);
6401 if (hlua)
Thierry Fournier021d9862020-11-28 23:42:03 +01006402 /* we are in runtime processing */
6403 state_id = hlua->state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006404 else
Thierry Fournier021d9862020-11-28 23:42:03 +01006405 /* we are in initialization mode */
Thierry Fournierc7492592020-11-28 23:57:24 +01006406 state_id = hlua_state_id;
Thierry Fournier75fc0292020-11-28 13:18:56 +01006407
Willy Tarreaubafbe012017-11-24 17:34:44 +01006408 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006409 if (!hlua)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006410 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006411
Thierry Fournier59f11be2020-11-29 00:37:41 +01006412 /* We are in the common lua state, execute the task anywhere,
6413 * otherwise, inherit the current thread identifier
6414 */
6415 if (state_id == 0)
6416 task = task_new(MAX_THREADS_MASK);
6417 else
6418 task = task_new(tid_bit);
Willy Tarreaue09101e2018-10-16 17:37:12 +02006419 if (!task)
6420 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
6421
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006422 task->context = hlua;
6423 task->process = hlua_process_task;
6424
Thierry Fournier021d9862020-11-28 23:42:03 +01006425 if (!hlua_ctx_init(hlua, state_id, task, 1))
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006426 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER24f33532015-01-23 12:13:00 +01006427
6428 /* Restore the function in the stack. */
6429 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref);
6430 hlua->nargs = 0;
6431
6432 /* Schedule task. */
6433 task_schedule(task, now_ms);
6434
6435 return 0;
6436}
6437
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006438/* Wrapper called by HAProxy to execute an LUA converter. This wrapper
6439 * doesn't allow "yield" functions because the HAProxy engine cannot
6440 * resume converters.
6441 */
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006442static int hlua_sample_conv_wrapper(const struct arg *arg_p, struct sample *smp, void *private)
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006443{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006444 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006445 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006446 const char *error;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006447
Willy Tarreaube508f12016-03-10 11:47:01 +01006448 if (!stream)
6449 return 0;
6450
Willy Tarreau87b09662015-04-03 00:22:06 +02006451 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006452 * Lua context can be not initialized. This behavior
6453 * permits to save performances because a systematic
6454 * Lua initialization cause 5% performances loss.
6455 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006456 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006457 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006458 if (!stream->hlua) {
6459 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6460 return 0;
6461 }
Thierry Fournierc7492592020-11-28 23:57:24 +01006462 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006463 SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name);
6464 return 0;
6465 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006466 }
6467
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006468 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006469 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006470
6471 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006472 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006473 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6474 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006475 else
6476 error = "critical error";
6477 SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006478 return 0;
6479 }
6480
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006481 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006482 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006483 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006484 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006485 return 0;
6486 }
6487
6488 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006489 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006490
6491 /* convert input sample and pust-it in the stack. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006492 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006493 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006494 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006495 return 0;
6496 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006497 hlua_smp2lua(stream->hlua->T, smp);
6498 stream->hlua->nargs = 1;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006499
6500 /* push keywords in the stack. */
6501 if (arg_p) {
6502 for (; arg_p->type != ARGT_STOP; arg_p++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006503 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006504 SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006505 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006506 return 0;
6507 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006508 hlua_arg2lua(stream->hlua->T, arg_p);
6509 stream->hlua->nargs++;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006510 }
6511 }
6512
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006513 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006514 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006515
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006516 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006517 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006518 }
6519
6520 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006521 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006522 /* finished. */
6523 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006524 /* If the stack is empty, the function fails. */
6525 if (lua_gettop(stream->hlua->T) <= 0)
6526 return 0;
6527
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006528 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006529 hlua_lua2smp(stream->hlua->T, -1, smp);
6530 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006531 return 1;
6532
6533 /* yield. */
6534 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006535 SEND_ERR(stream->be, "Lua converter '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006536 return 0;
6537
6538 /* finished with error. */
6539 case HLUA_E_ERRMSG:
6540 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006541 SEND_ERR(stream->be, "Lua converter '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006542 fcn->name, lua_tostring(stream->hlua->T, -1));
6543 lua_pop(stream->hlua->T, 1);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006544 return 0;
6545
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006546 case HLUA_E_ETMOUT:
6547 SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name);
6548 return 0;
6549
6550 case HLUA_E_NOMEM:
6551 SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name);
6552 return 0;
6553
6554 case HLUA_E_YIELD:
6555 SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name);
6556 return 0;
6557
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006558 case HLUA_E_ERR:
6559 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006560 SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006561 /* fall through */
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006562
6563 default:
6564 return 0;
6565 }
6566}
6567
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006568/* Wrapper called by HAProxy to execute a sample-fetch. this wrapper
6569 * doesn't allow "yield" functions because the HAProxy engine cannot
Willy Tarreaube508f12016-03-10 11:47:01 +01006570 * resume sample-fetches. This function will be called by the sample
6571 * fetch engine to call lua-based fetch operations.
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006572 */
Thierry FOURNIER0786d052015-05-11 15:42:45 +02006573static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp,
6574 const char *kw, void *private)
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006575{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02006576 struct hlua_function *fcn = private;
Thierry FOURNIER0a9a2b82015-05-11 15:20:49 +02006577 struct stream *stream = smp->strm;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006578 const char *error;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006579 unsigned int hflags = HLUA_TXN_NOTERM;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006580
Willy Tarreaube508f12016-03-10 11:47:01 +01006581 if (!stream)
6582 return 0;
Christopher Fauletafd8f102018-11-08 11:34:21 +01006583
Willy Tarreau87b09662015-04-03 00:22:06 +02006584 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006585 * Lua context can be not initialized. This behavior
6586 * permits to save performances because a systematic
6587 * Lua initialization cause 5% performances loss.
6588 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006589 if (!stream->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006590 stream->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006591 if (!stream->hlua) {
6592 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6593 return 0;
6594 }
Thierry Fournierc7492592020-11-28 23:57:24 +01006595 if (!hlua_ctx_init(stream->hlua, fcn_ref_to_stack_id(fcn), stream->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006596 SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name);
6597 return 0;
6598 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006599 }
6600
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006601 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006602 if (!HLUA_IS_RUNNING(stream->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006603
6604 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006605 if (!SET_SAFE_LJMP(stream->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006606 if (lua_type(stream->hlua->T, -1) == LUA_TSTRING)
6607 error = lua_tostring(stream->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006608 else
6609 error = "critical error";
6610 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", fcn->name, error);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006611 return 0;
6612 }
6613
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006614 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006615 if (!lua_checkstack(stream->hlua->T, 2)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006616 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006617 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006618 return 0;
6619 }
6620
6621 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006622 lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006623
6624 /* push arguments in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006625 if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006626 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006627 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006628 return 0;
6629 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006630 stream->hlua->nargs = 1;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006631
6632 /* push keywords in the stack. */
6633 for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) {
6634 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006635 if (!lua_checkstack(stream->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006636 SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006637 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006638 return 0;
6639 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006640 hlua_arg2lua(stream->hlua->T, arg_p);
6641 stream->hlua->nargs++;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006642 }
6643
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006644 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006645 stream->hlua->max_time = hlua_timeout_session;
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006646
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006647 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006648 RESET_SAFE_LJMP(stream->hlua);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006649 }
6650
6651 /* Execute the function. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006652 switch (hlua_ctx_resume(stream->hlua, 0)) {
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006653 /* finished. */
6654 case HLUA_E_OK:
Thierry FOURNIERfd80df12017-05-12 16:32:20 +02006655 /* If the stack is empty, the function fails. */
6656 if (lua_gettop(stream->hlua->T) <= 0)
6657 return 0;
6658
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006659 /* Convert the returned value in sample. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006660 hlua_lua2smp(stream->hlua->T, -1, smp);
6661 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006662
6663 /* Set the end of execution flag. */
6664 smp->flags &= ~SMP_F_MAY_CHANGE;
6665 return 1;
6666
6667 /* yield. */
6668 case HLUA_E_AGAIN:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006669 SEND_ERR(smp->px, "Lua sample-fetch '%s': cannot use yielded functions.\n", fcn->name);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006670 return 0;
6671
6672 /* finished with error. */
6673 case HLUA_E_ERRMSG:
6674 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006675 SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n",
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006676 fcn->name, lua_tostring(stream->hlua->T, -1));
6677 lua_pop(stream->hlua->T, 1);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006678 return 0;
6679
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006680 case HLUA_E_ETMOUT:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006681 SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name);
6682 return 0;
6683
6684 case HLUA_E_NOMEM:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006685 SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name);
6686 return 0;
6687
6688 case HLUA_E_YIELD:
Thierry Fournierd5b073c2018-05-21 19:42:47 +02006689 SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name);
6690 return 0;
6691
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006692 case HLUA_E_ERR:
6693 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006694 SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name);
Tim Duesterhus588b3142020-05-29 14:35:51 +02006695 /* fall through */
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006696
6697 default:
6698 return 0;
6699 }
6700}
6701
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006702/* This function is an LUA binding used for registering
6703 * "sample-conv" functions. It expects a converter name used
6704 * in the haproxy configuration file, and an LUA function.
6705 */
6706__LJMP static int hlua_register_converters(lua_State *L)
6707{
6708 struct sample_conv_kw_list *sck;
6709 const char *name;
6710 int ref;
6711 int len;
6712 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006713 struct sample_conv *sc;
6714 struct buffer *trash;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006715
6716 MAY_LJMP(check_args(L, 2, "register_converters"));
6717
6718 /* First argument : converter name. */
6719 name = MAY_LJMP(luaL_checkstring(L, 1));
6720
6721 /* Second argument : lua function. */
6722 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6723
Thierry Fournierf67442e2020-11-28 20:41:07 +01006724 /* Check if the converter is already registered */
6725 trash = get_trash_chunk();
6726 chunk_printf(trash, "lua.%s", name);
6727 sc = find_sample_conv(trash->area, trash->data);
6728 if (sc != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006729 fcn = sc->private;
6730 if (fcn->function_ref[hlua_state_id] != -1) {
6731 ha_warning("Trying to register converter 'lua.%s' more than once. "
6732 "This will become a hard error in version 2.5.\n", name);
6733 }
6734 fcn->function_ref[hlua_state_id] = ref;
6735 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006736 }
6737
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006738 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006739 sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006740 if (!sck)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006741 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006742 fcn = new_hlua_function();
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006743 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006744 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006745
6746 /* Fill fcn. */
6747 fcn->name = strdup(name);
6748 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006749 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournierc7492592020-11-28 23:57:24 +01006750 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006751
6752 /* List head */
6753 sck->list.n = sck->list.p = NULL;
6754
6755 /* converter keyword. */
6756 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006757 sck->kw[0].kw = calloc(1, len);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006758 if (!sck->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006759 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006760
6761 snprintf((char *)sck->kw[0].kw, len, "lua.%s", name);
6762 sck->kw[0].process = hlua_sample_conv_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006763 sck->kw[0].arg_mask = ARG12(0,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR);
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006764 sck->kw[0].val_args = NULL;
6765 sck->kw[0].in_type = SMP_T_STR;
6766 sck->kw[0].out_type = SMP_T_STR;
6767 sck->kw[0].private = fcn;
6768
Thierry FOURNIER9be813f2015-02-16 20:21:12 +01006769 /* Register this new converter */
6770 sample_register_convs(sck);
6771
6772 return 0;
6773}
6774
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08006775/* This function is an LUA binding used for registering
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006776 * "sample-fetch" functions. It expects a converter name used
6777 * in the haproxy configuration file, and an LUA function.
6778 */
6779__LJMP static int hlua_register_fetches(lua_State *L)
6780{
6781 const char *name;
6782 int ref;
6783 int len;
6784 struct sample_fetch_kw_list *sfk;
6785 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006786 struct sample_fetch *sf;
6787 struct buffer *trash;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006788
6789 MAY_LJMP(check_args(L, 2, "register_fetches"));
6790
6791 /* First argument : sample-fetch name. */
6792 name = MAY_LJMP(luaL_checkstring(L, 1));
6793
6794 /* Second argument : lua function. */
6795 ref = MAY_LJMP(hlua_checkfunction(L, 2));
6796
Thierry Fournierf67442e2020-11-28 20:41:07 +01006797 /* Check if the sample-fetch is already registered */
6798 trash = get_trash_chunk();
6799 chunk_printf(trash, "lua.%s", name);
6800 sf = find_sample_fetch(trash->area, trash->data);
6801 if (sf != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01006802 fcn = sf->private;
6803 if (fcn->function_ref[hlua_state_id] != -1) {
6804 ha_warning("Trying to register sample-fetch 'lua.%s' more than once. "
6805 "This will become a hard error in version 2.5.\n", name);
6806 }
6807 fcn->function_ref[hlua_state_id] = ref;
6808 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01006809 }
6810
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006811 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006812 sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006813 if (!sfk)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006814 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01006815 fcn = new_hlua_function();
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006816 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006817 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006818
6819 /* Fill fcn. */
6820 fcn->name = strdup(name);
6821 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006822 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournierc7492592020-11-28 23:57:24 +01006823 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006824
6825 /* List head */
6826 sfk->list.n = sfk->list.p = NULL;
6827
6828 /* sample-fetch keyword. */
6829 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02006830 sfk->kw[0].kw = calloc(1, len);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006831 if (!sfk->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01006832 return luaL_error(L, "Lua out of memory error.");
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006833
6834 snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name);
6835 sfk->kw[0].process = hlua_sample_fetch_wrapper;
David Carlier0c437f42016-04-27 16:21:56 +01006836 sfk->kw[0].arg_mask = ARG12(0,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR,STR);
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006837 sfk->kw[0].val_args = NULL;
6838 sfk->kw[0].out_type = SMP_T_STR;
6839 sfk->kw[0].use = SMP_USE_HTTP_ANY;
6840 sfk->kw[0].val = 0;
6841 sfk->kw[0].private = fcn;
6842
Thierry FOURNIERfa0e5dd2015-02-16 20:19:18 +01006843 /* Register this new fetch. */
6844 sample_register_fetches(sfk);
6845
6846 return 0;
6847}
6848
Christopher Faulet501465d2020-02-26 14:54:16 +01006849/* This function is a lua binding to set the wake_time.
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006850 */
Christopher Faulet501465d2020-02-26 14:54:16 +01006851__LJMP static int hlua_set_wake_time(lua_State *L)
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006852{
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006853 struct hlua *hlua;
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006854 unsigned int delay;
6855 unsigned int wakeup_ms;
6856
Thierry Fournier4234dbd2020-11-28 13:18:23 +01006857 /* Get hlua struct, or NULL if we execute from main lua state */
6858 hlua = hlua_gethlua(L);
6859 if (!hlua) {
6860 return 0;
6861 }
6862
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006863 MAY_LJMP(check_args(L, 1, "wake_time"));
6864
6865 delay = MAY_LJMP(luaL_checkinteger(L, 1));
6866 wakeup_ms = tick_add(now_ms, delay);
6867 hlua->wake_time = wakeup_ms;
6868 return 0;
6869}
6870
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006871/* This function is a wrapper to execute each LUA function declared as an action
6872 * wrapper during the initialisation period. This function may return any
6873 * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is
6874 * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the
6875 * return value is the first element on the stack.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006876 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006877static enum act_return hlua_action(struct act_rule *rule, struct proxy *px,
Willy Tarreau658b85b2015-09-27 10:00:49 +02006878 struct session *sess, struct stream *s, int flags)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006879{
6880 char **arg;
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006881 unsigned int hflags = 0;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006882 int dir, act_ret = ACT_RET_CONT;
Thierry Fournierfd107a22016-02-19 19:57:23 +01006883 const char *error;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006884
6885 switch (rule->from) {
Christopher Fauletd8f0e072020-02-25 09:45:51 +01006886 case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break;
6887 case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break;
6888 case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break;
6889 case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006890 default:
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006891 SEND_ERR(px, "Lua: internal error while execute action.\n");
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006892 goto end;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006893 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006894
Willy Tarreau87b09662015-04-03 00:22:06 +02006895 /* In the execution wrappers linked with a stream, the
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006896 * Lua context can be not initialized. This behavior
6897 * permits to save performances because a systematic
6898 * Lua initialization cause 5% performances loss.
6899 */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006900 if (!s->hlua) {
Willy Tarreaubafbe012017-11-24 17:34:44 +01006901 s->hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006902 if (!s->hlua) {
6903 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006904 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006905 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006906 }
Thierry Fournierc7492592020-11-28 23:57:24 +01006907 if (!hlua_ctx_init(s->hlua, fcn_ref_to_stack_id(rule->arg.hlua_rule->fcn), s->task, 0)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006908 SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006909 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006910 goto end;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006911 }
Thierry FOURNIER05ac4242015-02-27 18:37:27 +01006912 }
6913
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006914 /* If it is the first run, initialize the data for the call. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006915 if (!HLUA_IS_RUNNING(s->hlua)) {
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006916
6917 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006918 if (!SET_SAFE_LJMP(s->hlua)) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006919 if (lua_type(s->hlua->T, -1) == LUA_TSTRING)
6920 error = lua_tostring(s->hlua->T, -1);
Thierry Fournierfd107a22016-02-19 19:57:23 +01006921 else
6922 error = "critical error";
6923 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006924 rule->arg.hlua_rule->fcn->name, error);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006925 goto end;
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006926 }
6927
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006928 /* Check stack available size. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006929 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006930 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006931 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006932 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006933 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006934 }
6935
6936 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01006937 lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn->function_ref[s->hlua->state_id]);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006938
Willy Tarreau87b09662015-04-03 00:22:06 +02006939 /* Create and and push object stream in the stack. */
Christopher Fauletbfab2dd2019-07-26 15:09:53 +02006940 if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006941 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006942 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006943 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006944 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006945 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006946 s->hlua->nargs = 1;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006947
6948 /* push keywords in the stack. */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02006949 for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) {
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006950 if (!lua_checkstack(s->hlua->T, 1)) {
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02006951 SEND_ERR(px, "Lua function '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01006952 rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006953 RESET_SAFE_LJMP(s->hlua);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006954 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006955 }
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006956 lua_pushstring(s->hlua->T, *arg);
6957 s->hlua->nargs++;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006958 }
6959
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006960 /* Now the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01006961 RESET_SAFE_LJMP(s->hlua);
Thierry FOURNIERbabae282015-09-17 11:36:37 +02006962
Thierry FOURNIERbd413492015-03-03 16:52:26 +01006963 /* We must initialize the execution timeouts. */
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01006964 s->hlua->max_time = hlua_timeout_session;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006965 }
6966
6967 /* Execute the function. */
Christopher Faulet105ba6c2019-12-18 14:41:51 +01006968 switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) {
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006969 /* finished. */
6970 case HLUA_E_OK:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01006971 /* Catch the return value */
6972 if (lua_gettop(s->hlua->T) > 0)
6973 act_ret = lua_tointeger(s->hlua->T, -1);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006974
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006975 /* Set timeout in the required channel. */
Christopher Faulet498c4832020-07-28 11:59:58 +02006976 if (act_ret == ACT_RET_YIELD) {
6977 if (flags & ACT_OPT_FINAL)
6978 goto err_yield;
6979
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006980 if (dir == SMP_OPT_DIR_REQ)
6981 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6982 s->hlua->wake_time);
6983 else
6984 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6985 s->hlua->wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01006986 }
6987 goto end;
6988
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006989 /* yield. */
6990 case HLUA_E_AGAIN:
Thierry FOURNIERc42c1ae2015-03-03 17:17:55 +01006991 /* Set timeout in the required channel. */
Christopher Faulet8f587ea2020-07-28 12:01:55 +02006992 if (dir == SMP_OPT_DIR_REQ)
6993 s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp),
6994 s->hlua->wake_time);
6995 else
6996 s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp),
6997 s->hlua->wake_time);
6998
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01006999 /* Some actions can be wake up when a "write" event
7000 * is detected on a response channel. This is useful
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007001 * only for actions targeted on the requests.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007002 */
Christopher Faulet51fa3582019-07-26 14:54:52 +02007003 if (HLUA_IS_WAKERESWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007004 s->res.flags |= CF_WAKE_WRITE;
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007005 if (HLUA_IS_WAKEREQWR(s->hlua))
Willy Tarreau22ec1ea2014-11-27 20:45:39 +01007006 s->req.flags |= CF_WAKE_WRITE;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007007 act_ret = ACT_RET_YIELD;
7008 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007009
7010 /* finished with error. */
7011 case HLUA_E_ERRMSG:
7012 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007013 SEND_ERR(px, "Lua function '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007014 rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1));
Thierry FOURNIER2c8b54e2016-12-17 12:45:32 +01007015 lua_pop(s->hlua->T, 1);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007016 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007017
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007018 case HLUA_E_ETMOUT:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007019 SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007020 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007021
7022 case HLUA_E_NOMEM:
Thierry Fournierad5345f2020-11-29 02:05:57 +01007023 SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007024 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007025
7026 case HLUA_E_YIELD:
Christopher Faulet498c4832020-07-28 11:59:58 +02007027 err_yield:
7028 act_ret = ACT_RET_CONT;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007029 SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007030 rule->arg.hlua_rule->fcn->name);
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007031 goto end;
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007032
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007033 case HLUA_E_ERR:
7034 /* Display log. */
Thierry FOURNIER23bc3752015-09-11 19:15:43 +02007035 SEND_ERR(px, "Lua function '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007036 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007037
7038 default:
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007039 goto end;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007040 }
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007041
7042 end:
Christopher Faulet2361fd92020-07-30 10:40:58 +02007043 if (act_ret != ACT_RET_YIELD && s->hlua)
Christopher Faulet8f587ea2020-07-28 12:01:55 +02007044 s->hlua->wake_time = TICK_ETERNITY;
Christopher Faulet7716cdf2020-01-29 11:53:30 +01007045 return act_ret;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007046}
7047
Willy Tarreau144f84a2021-03-02 16:09:26 +01007048struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned int state)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007049{
Olivier Houchard9f6af332018-05-25 14:04:04 +02007050 struct appctx *ctx = context;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007051
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007052 appctx_wakeup(ctx);
Willy Tarreaud9587412017-08-23 16:07:33 +02007053 t->expire = TICK_ETERNITY;
Willy Tarreaud1aa41f2017-07-21 16:41:56 +02007054 return t;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007055}
7056
7057static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7058{
7059 struct stream_interface *si = ctx->owner;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007060 struct hlua *hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007061 struct task *task;
7062 char **arg;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007063 const char *error;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007064
Willy Tarreaubafbe012017-11-24 17:34:44 +01007065 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007066 if (!hlua) {
7067 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007068 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007069 return 0;
7070 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007071 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007072 ctx->ctx.hlua_apptcp.hlua = hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007073 ctx->ctx.hlua_apptcp.flags = 0;
7074
7075 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007076 task = task_new(tid_bit);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007077 if (!task) {
7078 SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007079 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007080 return 0;
7081 }
7082 task->nice = 0;
7083 task->context = ctx;
7084 task->process = hlua_applet_wakeup;
7085 ctx->ctx.hlua_apptcp.task = task;
7086
7087 /* In the execution wrappers linked with a stream, the
7088 * Lua context can be not initialized. This behavior
7089 * permits to save performances because a systematic
7090 * Lua initialization cause 5% performances loss.
7091 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007092 if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task, 0)) {
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007093 SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007094 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007095 return 0;
7096 }
7097
7098 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007099 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007100
7101 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007102 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007103 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7104 error = lua_tostring(hlua->T, -1);
7105 else
7106 error = "critical error";
7107 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007108 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007109 return 0;
7110 }
7111
7112 /* Check stack available size. */
7113 if (!lua_checkstack(hlua->T, 1)) {
7114 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007115 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007116 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007117 return 0;
7118 }
7119
7120 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007121 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007122
7123 /* Create and and push object stream in the stack. */
7124 if (!hlua_applet_tcp_new(hlua->T, ctx)) {
7125 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007126 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007127 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007128 return 0;
7129 }
7130 hlua->nargs = 1;
7131
7132 /* push keywords in the stack. */
7133 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7134 if (!lua_checkstack(hlua->T, 1)) {
7135 SEND_ERR(px, "Lua applet tcp '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007136 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007137 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007138 return 0;
7139 }
7140 lua_pushstring(hlua->T, *arg);
7141 hlua->nargs++;
7142 }
7143
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007144 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007145
7146 /* Wakeup the applet ASAP. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007147 si_cant_get(si);
Willy Tarreau3367d412018-11-15 10:57:41 +01007148 si_rx_endp_more(si);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007149
7150 return 1;
7151}
7152
Willy Tarreau60409db2019-08-21 14:14:50 +02007153void hlua_applet_tcp_fct(struct appctx *ctx)
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007154{
7155 struct stream_interface *si = ctx->owner;
7156 struct stream *strm = si_strm(si);
7157 struct channel *res = si_ic(si);
7158 struct act_rule *rule = ctx->rule;
7159 struct proxy *px = strm->be;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007160 struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007161
7162 /* The applet execution is already done. */
Olivier Houchard594c8c52018-08-28 14:41:31 +02007163 if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) {
7164 /* eat the whole request */
7165 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007166 return;
Olivier Houchard594c8c52018-08-28 14:41:31 +02007167 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007168
7169 /* If the stream is disconnect or closed, ldo nothing. */
7170 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7171 return;
7172
7173 /* Execute the function. */
7174 switch (hlua_ctx_resume(hlua, 1)) {
7175 /* finished. */
7176 case HLUA_E_OK:
7177 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7178
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007179 /* eat the whole request */
Willy Tarreaua79021a2018-06-15 18:07:57 +02007180 co_skip(si_oc(si), co_data(si_oc(si)));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007181 res->flags |= CF_READ_NULL;
7182 si_shutr(si);
7183 return;
7184
7185 /* yield. */
7186 case HLUA_E_AGAIN:
Thierry Fournier0164f202016-02-20 17:47:43 +01007187 if (hlua->wake_time != TICK_ETERNITY)
7188 task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007189 return;
7190
7191 /* finished with error. */
7192 case HLUA_E_ERRMSG:
7193 /* Display log. */
7194 SEND_ERR(px, "Lua applet tcp '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007195 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007196 lua_pop(hlua->T, 1);
7197 goto error;
7198
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007199 case HLUA_E_ETMOUT:
7200 SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007201 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007202 goto error;
7203
7204 case HLUA_E_NOMEM:
7205 SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007206 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007207 goto error;
7208
7209 case HLUA_E_YIELD: /* unexpected */
7210 SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007211 rule->arg.hlua_rule->fcn->name);
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007212 goto error;
7213
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007214 case HLUA_E_ERR:
7215 /* Display log. */
7216 SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007217 rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007218 goto error;
7219
7220 default:
7221 goto error;
7222 }
7223
7224error:
7225
7226 /* For all other cases, just close the stream. */
7227 si_shutw(si);
7228 si_shutr(si);
7229 ctx->ctx.hlua_apptcp.flags |= APPLET_DONE;
7230}
7231
7232static void hlua_applet_tcp_release(struct appctx *ctx)
7233{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007234 task_destroy(ctx->ctx.hlua_apptcp.task);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007235 ctx->ctx.hlua_apptcp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007236 hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007237 ctx->ctx.hlua_apptcp.hlua = NULL;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007238}
7239
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007240/* The function returns 1 if the initialisation is complete, 0 if
7241 * an errors occurs and -1 if more data are required for initializing
7242 * the applet.
7243 */
7244static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm)
7245{
7246 struct stream_interface *si = ctx->owner;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007247 struct http_txn *txn;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007248 struct hlua *hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007249 char **arg;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007250 struct task *task;
Thierry Fournierfd107a22016-02-19 19:57:23 +01007251 const char *error;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007252
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007253 txn = strm->txn;
Willy Tarreaubafbe012017-11-24 17:34:44 +01007254 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007255 if (!hlua) {
7256 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007257 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007258 return 0;
7259 }
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007260 HLUA_INIT(hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007261 ctx->ctx.hlua_apphttp.hlua = hlua;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007262 ctx->ctx.hlua_apphttp.left_bytes = -1;
7263 ctx->ctx.hlua_apphttp.flags = 0;
7264
Thierry FOURNIERd93ea2b2015-12-20 19:14:52 +01007265 if (txn->req.flags & HTTP_MSGF_VER_11)
7266 ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11;
7267
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007268 /* Create task used by signal to wakeup applets. */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007269 task = task_new(tid_bit);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007270 if (!task) {
7271 SEND_ERR(px, "Lua applet http '%s': out of memory.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007272 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007273 return 0;
7274 }
7275 task->nice = 0;
7276 task->context = ctx;
7277 task->process = hlua_applet_wakeup;
7278 ctx->ctx.hlua_apphttp.task = task;
7279
7280 /* In the execution wrappers linked with a stream, the
7281 * Lua context can be not initialized. This behavior
7282 * permits to save performances because a systematic
7283 * Lua initialization cause 5% performances loss.
7284 */
Thierry Fournierc7492592020-11-28 23:57:24 +01007285 if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(ctx->rule->arg.hlua_rule->fcn), task, 0)) {
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007286 SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007287 ctx->rule->arg.hlua_rule->fcn->name);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007288 return 0;
7289 }
7290
7291 /* Set timeout according with the applet configuration. */
Thierry FOURNIER10770fa2015-09-29 01:59:42 +02007292 hlua->max_time = ctx->applet->timeout;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007293
7294 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007295 if (!SET_SAFE_LJMP(hlua)) {
Thierry Fournierfd107a22016-02-19 19:57:23 +01007296 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7297 error = lua_tostring(hlua->T, -1);
7298 else
7299 error = "critical error";
7300 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007301 ctx->rule->arg.hlua_rule->fcn->name, error);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007302 return 0;
7303 }
7304
7305 /* Check stack available size. */
7306 if (!lua_checkstack(hlua->T, 1)) {
7307 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007308 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007309 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007310 return 0;
7311 }
7312
7313 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007314 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007315
7316 /* Create and and push object stream in the stack. */
7317 if (!hlua_applet_http_new(hlua->T, ctx)) {
7318 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007319 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007320 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007321 return 0;
7322 }
7323 hlua->nargs = 1;
7324
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007325 /* push keywords in the stack. */
7326 for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) {
7327 if (!lua_checkstack(hlua->T, 1)) {
7328 SEND_ERR(px, "Lua applet http '%s': full stack.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007329 ctx->rule->arg.hlua_rule->fcn->name);
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007330 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007331 return 0;
7332 }
7333 lua_pushstring(hlua->T, *arg);
7334 hlua->nargs++;
7335 }
7336
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007337 RESET_SAFE_LJMP(hlua);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007338
7339 /* Wakeup the applet when data is ready for read. */
Willy Tarreau0cd3bd62018-11-06 18:46:37 +01007340 si_cant_get(si);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007341
7342 return 1;
7343}
7344
Willy Tarreau60409db2019-08-21 14:14:50 +02007345void hlua_applet_http_fct(struct appctx *ctx)
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007346{
7347 struct stream_interface *si = ctx->owner;
7348 struct stream *strm = si_strm(si);
7349 struct channel *req = si_oc(si);
7350 struct channel *res = si_ic(si);
7351 struct act_rule *rule = ctx->rule;
7352 struct proxy *px = strm->be;
7353 struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua;
7354 struct htx *req_htx, *res_htx;
7355
7356 res_htx = htx_from_buf(&res->buf);
7357
7358 /* If the stream is disconnect or closed, ldo nothing. */
7359 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7360 goto out;
7361
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007362 /* Check if the input buffer is available. */
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007363 if (!b_size(&res->buf)) {
7364 si_rx_room_blk(si);
7365 goto out;
7366 }
7367 /* check that the output is not closed */
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007368 if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR))
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007369 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7370
7371 /* Set the currently running flag. */
7372 if (!HLUA_IS_RUNNING(hlua) &&
7373 !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7374 struct htx_blk *blk;
7375 size_t count = co_data(req);
7376
7377 if (!count) {
7378 si_cant_get(si);
7379 goto out;
7380 }
7381
7382 /* We need to flush the request header. This left the body for
7383 * the Lua.
7384 */
7385 req_htx = htx_from_buf(&req->buf);
Christopher Fauleta3f15502019-05-13 15:27:23 +02007386 blk = htx_get_first_blk(req_htx);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007387 while (count && blk) {
7388 enum htx_blk_type type = htx_get_blk_type(blk);
7389 uint32_t sz = htx_get_blksz(blk);
7390
7391 if (sz > count) {
7392 si_cant_get(si);
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007393 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007394 goto out;
7395 }
7396
7397 count -= sz;
7398 co_set_data(req, co_data(req) - sz);
7399 blk = htx_remove_blk(req_htx, blk);
7400
7401 if (type == HTX_BLK_EOH)
7402 break;
7403 }
Christopher Faulet0ae79d02019-02-27 21:36:59 +01007404 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007405 }
7406
7407 /* Executes The applet if it is not done. */
7408 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) {
7409
7410 /* Execute the function. */
7411 switch (hlua_ctx_resume(hlua, 1)) {
7412 /* finished. */
7413 case HLUA_E_OK:
7414 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7415 break;
7416
7417 /* yield. */
7418 case HLUA_E_AGAIN:
7419 if (hlua->wake_time != TICK_ETERNITY)
7420 task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time);
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007421 goto out;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007422
7423 /* finished with error. */
7424 case HLUA_E_ERRMSG:
7425 /* Display log. */
7426 SEND_ERR(px, "Lua applet http '%s': %s.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007427 rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1));
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007428 lua_pop(hlua->T, 1);
7429 goto error;
7430
7431 case HLUA_E_ETMOUT:
7432 SEND_ERR(px, "Lua applet http '%s': execution timeout.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007433 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007434 goto error;
7435
7436 case HLUA_E_NOMEM:
7437 SEND_ERR(px, "Lua applet http '%s': out of memory error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007438 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007439 goto error;
7440
7441 case HLUA_E_YIELD: /* unexpected */
7442 SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007443 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007444 goto error;
7445
7446 case HLUA_E_ERR:
7447 /* Display log. */
7448 SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n",
Thierry Fournierad5345f2020-11-29 02:05:57 +01007449 rule->arg.hlua_rule->fcn->name);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007450 goto error;
7451
7452 default:
7453 goto error;
7454 }
7455 }
7456
7457 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007458 if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT)
7459 goto done;
7460
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007461 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT))
7462 goto error;
7463
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01007464 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
7465 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007466 strm->txn->status = ctx->ctx.hlua_apphttp.status;
7467 ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT;
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007468 }
7469
7470 done:
7471 if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) {
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007472 if (!(res->flags & CF_SHUTR)) {
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007473 res->flags |= CF_READ_NULL;
Christopher Faulet56a3d6e2019-02-27 22:06:23 +01007474 si_shutr(si);
7475 }
7476
7477 /* eat the whole request */
7478 if (co_data(req)) {
7479 req_htx = htx_from_buf(&req->buf);
7480 co_htx_skip(req, req_htx, co_data(req));
7481 htx_to_buf(req_htx, &req->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007482 }
7483 }
7484
7485 out:
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007486 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007487 return;
7488
7489 error:
7490
7491 /* If we are in HTTP mode, and we are not send any
7492 * data, return a 500 server error in best effort:
7493 * if there is no room available in the buffer,
7494 * just close the connection.
7495 */
7496 if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) {
Christopher Fauletf7346382019-07-17 22:02:08 +02007497 struct buffer *err = &http_err_chunks[HTTP_ERR_500];
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007498
7499 channel_erase(res);
7500 res->buf.data = b_data(err);
7501 memcpy(res->buf.area, b_head(err), b_data(err));
7502 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf6cce3f2019-02-27 21:20:09 +01007503 channel_add_input(res, res_htx->data);
Christopher Faulet9c832fc2018-12-14 13:34:05 +01007504 }
7505 if (!(strm->flags & SF_ERR_MASK))
7506 strm->flags |= SF_ERR_RESOURCE;
7507 ctx->ctx.hlua_apphttp.flags |= APPLET_DONE;
7508 goto done;
7509}
7510
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007511static void hlua_applet_http_release(struct appctx *ctx)
7512{
Olivier Houchard3f795f72019-04-17 22:51:06 +02007513 task_destroy(ctx->ctx.hlua_apphttp.task);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007514 ctx->ctx.hlua_apphttp.task = NULL;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007515 hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007516 ctx->ctx.hlua_apphttp.hlua = NULL;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007517}
7518
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007519/* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007520 * success case, else return ACT_RET_PRS_ERR.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02007521 *
7522 * This function can fail with an abort() due to an Lua critical error.
7523 * We are in the configuration parsing process of HAProxy, this abort() is
7524 * tolerated.
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007525 */
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007526static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px,
7527 struct act_rule *rule, char **err)
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007528{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007529 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007530 int i;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007531
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007532 /* Memory for the rule. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007533 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007534 if (!rule->arg.hlua_rule) {
7535 memprintf(err, "out of memory error");
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007536 return ACT_RET_PRS_ERR;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007537 }
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007538
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007539 /* Memory for arguments. */
Tim Duesterhuse52b6e52020-09-12 20:26:43 +02007540 rule->arg.hlua_rule->args = calloc(fcn->nargs + 1,
7541 sizeof(*rule->arg.hlua_rule->args));
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007542 if (!rule->arg.hlua_rule->args) {
7543 memprintf(err, "out of memory error");
7544 return ACT_RET_PRS_ERR;
7545 }
7546
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007547 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007548 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007549
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007550 /* Expect some arguments */
7551 for (i = 0; i < fcn->nargs; i++) {
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007552 if (*args[*cur_arg] == '\0') {
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007553 memprintf(err, "expect %d arguments", fcn->nargs);
7554 return ACT_RET_PRS_ERR;
7555 }
Thierry FOURNIER1725c2e2019-01-06 19:38:49 +01007556 rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]);
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007557 if (!rule->arg.hlua_rule->args[i]) {
7558 memprintf(err, "out of memory error");
7559 return ACT_RET_PRS_ERR;
7560 }
7561 (*cur_arg)++;
7562 }
7563 rule->arg.hlua_rule->args[i] = NULL;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007564
Thierry FOURNIER42148732015-09-02 17:17:33 +02007565 rule->action = ACT_CUSTOM;
Thierry FOURNIER4dc15d12015-08-06 18:25:56 +02007566 rule->action_ptr = hlua_action;
Thierry FOURNIERafa80492015-08-19 09:04:15 +02007567 return ACT_RET_PRS_OK;
Thierry FOURNIER258d8aa2015-02-16 20:23:40 +01007568}
7569
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007570static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px,
7571 struct act_rule *rule, char **err)
7572{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007573 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007574
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007575 /* HTTP applets are forbidden in tcp-request rules.
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007576 * HTTP applet request requires everything initialized by
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007577 * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER).
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05007578 * The applet will be immediately initialized, but its before
Thierry FOURNIER718e2a72015-12-20 20:13:14 +01007579 * the call of this analyzer.
7580 */
7581 if (rule->from != ACT_F_HTTP_REQ) {
7582 memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets");
7583 return ACT_RET_PRS_ERR;
7584 }
7585
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007586 /* Memory for the rule. */
7587 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7588 if (!rule->arg.hlua_rule) {
7589 memprintf(err, "out of memory error");
7590 return ACT_RET_PRS_ERR;
7591 }
7592
7593 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007594 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007595
7596 /* TODO: later accept arguments. */
7597 rule->arg.hlua_rule->args = NULL;
7598
7599 /* Add applet pointer in the rule. */
7600 rule->applet.obj_type = OBJ_TYPE_APPLET;
7601 rule->applet.name = fcn->name;
7602 rule->applet.init = hlua_applet_http_init;
7603 rule->applet.fct = hlua_applet_http_fct;
7604 rule->applet.release = hlua_applet_http_release;
7605 rule->applet.timeout = hlua_timeout_applet;
7606
7607 return ACT_RET_PRS_OK;
7608}
7609
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007610/* This function is an LUA binding used for registering
7611 * "sample-conv" functions. It expects a converter name used
7612 * in the haproxy configuration file, and an LUA function.
7613 */
7614__LJMP static int hlua_register_action(lua_State *L)
7615{
7616 struct action_kw_list *akl;
7617 const char *name;
7618 int ref;
7619 int len;
7620 struct hlua_function *fcn;
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007621 int nargs;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007622 struct buffer *trash;
7623 struct action_kw *akw;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007624
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007625 /* Initialise the number of expected arguments at 0. */
7626 nargs = 0;
7627
7628 if (lua_gettop(L) < 3 || lua_gettop(L) > 4)
7629 WILL_LJMP(luaL_error(L, "'register_action' needs between 3 and 4 arguments"));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007630
7631 /* First argument : converter name. */
7632 name = MAY_LJMP(luaL_checkstring(L, 1));
7633
7634 /* Second argument : environment. */
7635 if (lua_type(L, 2) != LUA_TTABLE)
7636 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7637
7638 /* Third argument : lua function. */
7639 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7640
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007641 /* Fourth argument : number of mandatory arguments expected on the configuration line. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007642 if (lua_gettop(L) >= 4)
7643 nargs = MAY_LJMP(luaL_checkinteger(L, 4));
7644
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08007645 /* browse the second argument as an array. */
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007646 lua_pushnil(L);
7647 while (lua_next(L, 2) != 0) {
7648 if (lua_type(L, -1) != LUA_TSTRING)
7649 WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings"));
7650
Thierry Fournierf67442e2020-11-28 20:41:07 +01007651 /* Check if action exists */
7652 trash = get_trash_chunk();
7653 chunk_printf(trash, "lua.%s", name);
7654 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) {
7655 akw = tcp_req_cont_action(trash->area);
7656 } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) {
7657 akw = tcp_res_cont_action(trash->area);
7658 } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) {
7659 akw = action_http_req_custom(trash->area);
7660 } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) {
7661 akw = action_http_res_custom(trash->area);
7662 } else {
7663 akw = NULL;
7664 }
7665 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007666 fcn = akw->private;
7667 if (fcn->function_ref[hlua_state_id] != -1) {
7668 ha_warning("Trying to register action 'lua.%s' more than once. "
7669 "This will become a hard error in version 2.5.\n", name);
7670 }
7671 fcn->function_ref[hlua_state_id] = ref;
7672
7673 /* pop the environment string. */
7674 lua_pop(L, 1);
7675 continue;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007676 }
7677
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007678 /* Check required environment. Only accepted "http" or "tcp". */
7679 /* Allocate and fill the sample fetch keyword struct. */
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007680 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007681 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007682 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007683 fcn = new_hlua_function();
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007684 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007685 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007686
7687 /* Fill fcn. */
7688 fcn->name = strdup(name);
7689 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007690 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournierc7492592020-11-28 23:57:24 +01007691 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007692
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07007693 /* Set the expected number of arguments. */
Thierry FOURNIER / OZON.IO4b123be2016-12-09 18:03:31 +01007694 fcn->nargs = nargs;
7695
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007696 /* List head */
7697 akl->list.n = akl->list.p = NULL;
7698
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007699 /* action keyword. */
7700 len = strlen("lua.") + strlen(name) + 1;
Thierry FOURNIER3c7a77c2015-09-26 00:51:16 +02007701 akl->kw[0].kw = calloc(1, len);
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007702 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007703 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007704
7705 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7706
7707 akl->kw[0].match_pfx = 0;
7708 akl->kw[0].private = fcn;
7709 akl->kw[0].parse = action_register_lua;
7710
7711 /* select the action registering point. */
7712 if (strcmp(lua_tostring(L, -1), "tcp-req") == 0)
7713 tcp_req_cont_keywords_register(akl);
7714 else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0)
7715 tcp_res_cont_keywords_register(akl);
7716 else if (strcmp(lua_tostring(L, -1), "http-req") == 0)
7717 http_req_keywords_register(akl);
7718 else if (strcmp(lua_tostring(L, -1), "http-res") == 0)
7719 http_res_keywords_register(akl);
7720 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007721 WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. "
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007722 "'tcp-req', 'tcp-res', 'http-req' or 'http-res' "
7723 "are expected.", lua_tostring(L, -1)));
7724
7725 /* pop the environment string. */
7726 lua_pop(L, 1);
7727 }
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007728 return ACT_RET_PRS_OK;
7729}
7730
7731static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px,
7732 struct act_rule *rule, char **err)
7733{
Vincent Bernat3c2f2f22016-04-03 13:48:42 +02007734 struct hlua_function *fcn = rule->kw->private;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007735
Christopher Faulet280f85b2019-07-15 15:02:04 +02007736 if (px->mode == PR_MODE_HTTP) {
7737 memprintf(err, "Lua TCP services cannot be used on HTTP proxies");
Christopher Fauletafd8f102018-11-08 11:34:21 +01007738 return ACT_RET_PRS_ERR;
7739 }
7740
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007741 /* Memory for the rule. */
7742 rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule));
7743 if (!rule->arg.hlua_rule) {
7744 memprintf(err, "out of memory error");
7745 return ACT_RET_PRS_ERR;
7746 }
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007747
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007748 /* Reference the Lua function and store the reference. */
Thierry Fournierad5345f2020-11-29 02:05:57 +01007749 rule->arg.hlua_rule->fcn = fcn;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007750
7751 /* TODO: later accept arguments. */
7752 rule->arg.hlua_rule->args = NULL;
7753
7754 /* Add applet pointer in the rule. */
7755 rule->applet.obj_type = OBJ_TYPE_APPLET;
7756 rule->applet.name = fcn->name;
7757 rule->applet.init = hlua_applet_tcp_init;
7758 rule->applet.fct = hlua_applet_tcp_fct;
7759 rule->applet.release = hlua_applet_tcp_release;
7760 rule->applet.timeout = hlua_timeout_applet;
7761
7762 return 0;
7763}
7764
7765/* This function is an LUA binding used for registering
7766 * "sample-conv" functions. It expects a converter name used
7767 * in the haproxy configuration file, and an LUA function.
7768 */
7769__LJMP static int hlua_register_service(lua_State *L)
7770{
7771 struct action_kw_list *akl;
7772 const char *name;
7773 const char *env;
7774 int ref;
7775 int len;
7776 struct hlua_function *fcn;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007777 struct buffer *trash;
7778 struct action_kw *akw;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007779
7780 MAY_LJMP(check_args(L, 3, "register_service"));
7781
7782 /* First argument : converter name. */
7783 name = MAY_LJMP(luaL_checkstring(L, 1));
7784
7785 /* Second argument : environment. */
7786 env = MAY_LJMP(luaL_checkstring(L, 2));
7787
7788 /* Third argument : lua function. */
7789 ref = MAY_LJMP(hlua_checkfunction(L, 3));
7790
Thierry Fournierf67442e2020-11-28 20:41:07 +01007791 /* Check for service already registered */
7792 trash = get_trash_chunk();
7793 chunk_printf(trash, "lua.%s", name);
7794 akw = service_find(trash->area);
7795 if (akw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01007796 fcn = akw->private;
7797 if (fcn->function_ref[hlua_state_id] != -1) {
7798 ha_warning("Trying to register service 'lua.%s' more than once. "
7799 "This will become a hard error in version 2.5.\n", name);
7800 }
7801 fcn->function_ref[hlua_state_id] = ref;
7802 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01007803 }
7804
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007805 /* Allocate and fill the sample fetch keyword struct. */
7806 akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2);
7807 if (!akl)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007808 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01007809 fcn = new_hlua_function();
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007810 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007811 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007812
7813 /* Fill fcn. */
7814 len = strlen("<lua.>") + strlen(name) + 1;
7815 fcn->name = calloc(1, len);
7816 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007817 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007818 snprintf((char *)fcn->name, len, "<lua.%s>", name);
Thierry Fournierc7492592020-11-28 23:57:24 +01007819 fcn->function_ref[hlua_state_id] = ref;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007820
7821 /* List head */
7822 akl->list.n = akl->list.p = NULL;
7823
7824 /* converter keyword. */
7825 len = strlen("lua.") + strlen(name) + 1;
7826 akl->kw[0].kw = calloc(1, len);
7827 if (!akl->kw[0].kw)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007828 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007829
7830 snprintf((char *)akl->kw[0].kw, len, "lua.%s", name);
7831
Thierry FOURNIER / OZON.IO02564fd2016-11-12 11:07:05 +01007832 /* Check required environment. Only accepted "http" or "tcp". */
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007833 if (strcmp(env, "tcp") == 0)
7834 akl->kw[0].parse = action_register_service_tcp;
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02007835 else if (strcmp(env, "http") == 0)
7836 akl->kw[0].parse = action_register_service_http;
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007837 else
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01007838 WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. "
Eric Salamafe7456f2017-12-21 14:30:07 +01007839 "'tcp' or 'http' are expected.", env));
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02007840
7841 akl->kw[0].match_pfx = 0;
7842 akl->kw[0].private = fcn;
7843
7844 /* End of array. */
7845 memset(&akl->kw[1], 0, sizeof(*akl->kw));
7846
7847 /* Register this new converter */
7848 service_keywords_register(akl);
7849
Thierry FOURNIER8255a752015-09-23 21:03:35 +02007850 return 0;
7851}
7852
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007853/* This function initialises Lua cli handler. It copies the
7854 * arguments in the Lua stack and create channel IO objects.
7855 */
Aurélien Nephtaliabbf6072018-04-18 13:26:46 +02007856static int hlua_cli_parse_fct(char **args, char *payload, struct appctx *appctx, void *private)
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007857{
7858 struct hlua *hlua;
7859 struct hlua_function *fcn;
7860 int i;
7861 const char *error;
7862
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007863 fcn = private;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007864 appctx->ctx.hlua_cli.fcn = private;
7865
Willy Tarreaubafbe012017-11-24 17:34:44 +01007866 hlua = pool_alloc(pool_head_hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007867 if (!hlua) {
7868 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007869 return 1;
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007870 }
7871 HLUA_INIT(hlua);
7872 appctx->ctx.hlua_cli.hlua = hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007873
7874 /* Create task used by signal to wakeup applets.
Ilya Shipitsind4259502020-04-08 01:07:56 +05007875 * We use the same wakeup function than the Lua applet_tcp and
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007876 * applet_http. It is absolutely compatible.
7877 */
Willy Tarreau5f4a47b2017-10-31 15:59:32 +01007878 appctx->ctx.hlua_cli.task = task_new(tid_bit);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007879 if (!appctx->ctx.hlua_cli.task) {
Thierry FOURNIERffbf5692016-12-16 11:14:06 +01007880 SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007881 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007882 }
7883 appctx->ctx.hlua_cli.task->nice = 0;
7884 appctx->ctx.hlua_cli.task->context = appctx;
7885 appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup;
7886
7887 /* Initialises the Lua context */
Thierry Fournierc7492592020-11-28 23:57:24 +01007888 if (!hlua_ctx_init(hlua, fcn_ref_to_stack_id(fcn), appctx->ctx.hlua_cli.task, 0)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007889 SEND_ERR(NULL, "Lua cli '%s': can't initialize Lua context.\n", fcn->name);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007890 goto error;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007891 }
7892
7893 /* The following Lua calls can fail. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007894 if (!SET_SAFE_LJMP(hlua)) {
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007895 if (lua_type(hlua->T, -1) == LUA_TSTRING)
7896 error = lua_tostring(hlua->T, -1);
7897 else
7898 error = "critical error";
7899 SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error);
7900 goto error;
7901 }
7902
7903 /* Check stack available size. */
7904 if (!lua_checkstack(hlua->T, 2)) {
7905 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7906 goto error;
7907 }
7908
7909 /* Restore the function in the stack. */
Thierry Fournierc7492592020-11-28 23:57:24 +01007910 lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007911
7912 /* Once the arguments parsed, the CLI is like an AppletTCP,
7913 * so push AppletTCP in the stack.
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007914 */
7915 if (!hlua_applet_tcp_new(hlua->T, appctx)) {
7916 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7917 goto error;
7918 }
7919 hlua->nargs = 1;
7920
7921 /* push keywords in the stack. */
7922 for (i = 0; *args[i]; i++) {
7923 /* Check stack available size. */
7924 if (!lua_checkstack(hlua->T, 1)) {
7925 SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name);
7926 goto error;
7927 }
7928 lua_pushstring(hlua->T, args[i]);
7929 hlua->nargs++;
7930 }
7931
7932 /* We must initialize the execution timeouts. */
7933 hlua->max_time = hlua_timeout_session;
7934
7935 /* At this point the execution is safe. */
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007936 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007937
7938 /* It's ok */
7939 return 0;
7940
7941 /* It's not ok. */
7942error:
Thierry Fournier7cbe5042020-11-28 17:02:21 +01007943 RESET_SAFE_LJMP(hlua);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007944 hlua_ctx_destroy(hlua);
Thierry FOURNIER1be34152016-12-17 12:09:51 +01007945 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007946 return 1;
7947}
7948
7949static int hlua_cli_io_handler_fct(struct appctx *appctx)
7950{
7951 struct hlua *hlua;
7952 struct stream_interface *si;
7953 struct hlua_function *fcn;
7954
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01007955 hlua = appctx->ctx.hlua_cli.hlua;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007956 si = appctx->owner;
Willy Tarreau8ae4f752016-12-14 15:41:45 +01007957 fcn = appctx->ctx.hlua_cli.fcn;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007958
7959 /* If the stream is disconnect or closed, ldo nothing. */
7960 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
7961 return 1;
7962
7963 /* Execute the function. */
7964 switch (hlua_ctx_resume(hlua, 1)) {
7965
7966 /* finished. */
7967 case HLUA_E_OK:
7968 return 1;
7969
7970 /* yield. */
7971 case HLUA_E_AGAIN:
7972 /* We want write. */
7973 if (HLUA_IS_WAKERESWR(hlua))
Willy Tarreaudb398432018-11-15 11:08:52 +01007974 si_rx_room_blk(si);
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01007975 /* Set the timeout. */
7976 if (hlua->wake_time != TICK_ETERNITY)
7977 task_schedule(hlua->task, hlua->wake_time);
7978 return 0;
7979
7980 /* finished with error. */
7981 case HLUA_E_ERRMSG:
7982 /* Display log. */
7983 SEND_ERR(NULL, "Lua cli '%s': %s.\n",
7984 fcn->name, lua_tostring(hlua->T, -1));
7985 lua_pop(hlua->T, 1);
7986 return 1;
7987
Thierry Fournierd5b073c2018-05-21 19:42:47 +02007988 case HLUA_E_ETMOUT:
7989 SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n",
7990 fcn->name);
7991 return 1;
7992
7993 case HLUA_E_NOMEM:
7994 SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n",
7995 fcn->name);
7996 return 1;
7997
7998 case HLUA_E_YIELD: /* unexpected */
7999 SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n",
8000 fcn->name);
8001 return 1;
8002
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008003 case HLUA_E_ERR:
8004 /* Display log. */
8005 SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n",
8006 fcn->name);
8007 return 1;
8008
8009 default:
8010 return 1;
8011 }
8012
8013 return 1;
8014}
8015
8016static void hlua_cli_io_release_fct(struct appctx *appctx)
8017{
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008018 hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua);
Thierry FOURNIERebed6e92016-12-16 11:54:07 +01008019 appctx->ctx.hlua_cli.hlua = NULL;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008020}
8021
8022/* This function is an LUA binding used for registering
8023 * new keywords in the cli. It expects a list of keywords
8024 * which are the "path". It is limited to 5 keywords. A
8025 * description of the command, a function to be executed
8026 * for the parsing and a function for io handlers.
8027 */
8028__LJMP static int hlua_register_cli(lua_State *L)
8029{
8030 struct cli_kw_list *cli_kws;
8031 const char *message;
8032 int ref_io;
8033 int len;
8034 struct hlua_function *fcn;
8035 int index;
8036 int i;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008037 struct buffer *trash;
8038 const char *kw[5];
8039 struct cli_kw *cli_kw;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008040
8041 MAY_LJMP(check_args(L, 3, "register_cli"));
8042
8043 /* First argument : an array of maximum 5 keywords. */
8044 if (!lua_istable(L, 1))
8045 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table"));
8046
8047 /* Second argument : string with contextual message. */
8048 message = MAY_LJMP(luaL_checkstring(L, 2));
8049
8050 /* Third and fourth argument : lua function. */
8051 ref_io = MAY_LJMP(hlua_checkfunction(L, 3));
8052
Thierry Fournierf67442e2020-11-28 20:41:07 +01008053 /* Check for CLI service already registered */
8054 trash = get_trash_chunk();
8055 index = 0;
8056 lua_pushnil(L);
8057 memset(kw, 0, sizeof(kw));
8058 while (lua_next(L, 1) != 0) {
8059 if (index >= CLI_PREFIX_KW_NB)
8060 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8061 if (lua_type(L, -1) != LUA_TSTRING)
8062 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8063 kw[index] = lua_tostring(L, -1);
8064 if (index == 0)
8065 chunk_printf(trash, "%s", kw[index]);
8066 else
8067 chunk_appendf(trash, " %s", kw[index]);
8068 index++;
8069 lua_pop(L, 1);
8070 }
8071 cli_kw = cli_find_kw_exact((char **)kw);
8072 if (cli_kw != NULL) {
Thierry Fournier59f11be2020-11-29 00:37:41 +01008073 fcn = cli_kw->private;
8074 if (fcn->function_ref[hlua_state_id] != -1) {
8075 ha_warning("Trying to register CLI keyword 'lua.%s' more than once. "
8076 "This will become a hard error in version 2.5.\n", trash->area);
8077 }
8078 fcn->function_ref[hlua_state_id] = ref_io;
8079 return 0;
Thierry Fournierf67442e2020-11-28 20:41:07 +01008080 }
8081
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008082 /* Allocate and fill the sample fetch keyword struct. */
8083 cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2);
8084 if (!cli_kws)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008085 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry Fournier62a22aa2020-11-28 21:06:35 +01008086 fcn = new_hlua_function();
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008087 if (!fcn)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008088 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008089
8090 /* Fill path. */
8091 index = 0;
8092 lua_pushnil(L);
8093 while(lua_next(L, 1) != 0) {
8094 if (index >= 5)
8095 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries"));
8096 if (lua_type(L, -1) != LUA_TSTRING)
8097 WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings"));
8098 cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1));
8099 if (!cli_kws->kw[0].str_kw[index])
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008100 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008101 index++;
8102 lua_pop(L, 1);
8103 }
8104
8105 /* Copy help message. */
8106 cli_kws->kw[0].usage = strdup(message);
8107 if (!cli_kws->kw[0].usage)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008108 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008109
8110 /* Fill fcn io handler. */
8111 len = strlen("<lua.cli>") + 1;
8112 for (i = 0; i < index; i++)
8113 len += strlen(cli_kws->kw[0].str_kw[i]) + 1;
8114 fcn->name = calloc(1, len);
8115 if (!fcn->name)
Thierry FOURNIER2986c0d2018-02-25 14:32:36 +01008116 WILL_LJMP(luaL_error(L, "Lua out of memory error."));
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008117 strncat((char *)fcn->name, "<lua.cli", len);
8118 for (i = 0; i < index; i++) {
8119 strncat((char *)fcn->name, ".", len);
8120 strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len);
8121 }
8122 strncat((char *)fcn->name, ">", len);
Thierry Fournierc7492592020-11-28 23:57:24 +01008123 fcn->function_ref[hlua_state_id] = ref_io;
Thierry FOURNIER / OZON.IOa44fdd92016-11-13 13:19:20 +01008124
8125 /* Fill last entries. */
8126 cli_kws->kw[0].private = fcn;
8127 cli_kws->kw[0].parse = hlua_cli_parse_fct;
8128 cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct;
8129 cli_kws->kw[0].io_release = hlua_cli_io_release_fct;
8130
8131 /* Register this new converter */
8132 cli_register_kw(cli_kws);
8133
8134 return 0;
8135}
8136
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008137static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008138 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008139 char **err, unsigned int *timeout)
8140{
8141 const char *error;
8142
8143 error = parse_time_err(args[1], timeout, TIME_UNIT_MS);
Willy Tarreau9faebe32019-06-07 19:00:37 +02008144 if (error == PARSE_TIME_OVER) {
8145 memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)",
8146 args[1], args[0]);
8147 return -1;
8148 }
8149 else if (error == PARSE_TIME_UNDER) {
8150 memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)",
8151 args[1], args[0]);
8152 return -1;
8153 }
8154 else if (error) {
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008155 memprintf(err, "%s: invalid timeout", args[0]);
8156 return -1;
8157 }
8158 return 0;
8159}
8160
8161static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008162 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008163 char **err)
8164{
8165 return hlua_read_timeout(args, section_type, curpx, defpx,
8166 file, line, err, &hlua_timeout_session);
8167}
8168
8169static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008170 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008171 char **err)
8172{
8173 return hlua_read_timeout(args, section_type, curpx, defpx,
8174 file, line, err, &hlua_timeout_task);
8175}
8176
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008177static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008178 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008179 char **err)
8180{
8181 return hlua_read_timeout(args, section_type, curpx, defpx,
8182 file, line, err, &hlua_timeout_applet);
8183}
8184
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008185static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008186 const struct proxy *defpx, const char *file, int line,
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008187 char **err)
8188{
8189 char *error;
8190
8191 hlua_nb_instruction = strtoll(args[1], &error, 10);
8192 if (*error != '\0') {
8193 memprintf(err, "%s: invalid number", args[0]);
8194 return -1;
8195 }
8196 return 0;
8197}
8198
Willy Tarreau32f61e22015-03-18 17:54:59 +01008199static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008200 const struct proxy *defpx, const char *file, int line,
Willy Tarreau32f61e22015-03-18 17:54:59 +01008201 char **err)
8202{
8203 char *error;
8204
8205 if (*(args[1]) == 0) {
8206 memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]);
8207 return -1;
8208 }
8209 hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L;
8210 if (*error != '\0') {
8211 memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error);
8212 return -1;
8213 }
8214 return 0;
8215}
8216
8217
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008218/* This function is called by the main configuration key "lua-load". It loads and
8219 * execute an lua file during the parsing of the HAProxy configuration file. It is
8220 * the main lua entry point.
8221 *
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008222 * This function runs with the HAProxy keywords API. It returns -1 if an error
8223 * occurs, otherwise it returns 0.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008224 *
8225 * In some error case, LUA set an error message in top of the stack. This function
8226 * returns this error message in the HAProxy logs and pop it from the stack.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008227 *
8228 * This function can fail with an abort() due to an Lua critical error.
8229 * We are in the configuration parsing process of HAProxy, this abort() is
8230 * tolerated.
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008231 */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008232static int hlua_load_state(char *filename, lua_State *L, char **err)
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008233{
8234 int error;
8235
8236 /* Just load and compile the file. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008237 error = luaL_loadfile(L, filename);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008238 if (error) {
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008239 memprintf(err, "error in Lua file '%s': %s", filename, lua_tostring(L, -1));
8240 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008241 return -1;
8242 }
8243
8244 /* If no syntax error where detected, execute the code. */
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008245 error = lua_pcall(L, 0, LUA_MULTRET, 0);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008246 switch (error) {
8247 case LUA_OK:
8248 break;
8249 case LUA_ERRRUN:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008250 memprintf(err, "Lua runtime error: %s\n", lua_tostring(L, -1));
8251 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008252 return -1;
8253 case LUA_ERRMEM:
Thierry Fournierde6145f2020-11-29 00:55:53 +01008254 memprintf(err, "Lua out of memory error\n");
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008255 return -1;
8256 case LUA_ERRERR:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008257 memprintf(err, "Lua message handler error: %s\n", lua_tostring(L, -1));
8258 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008259 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008260#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008261 case LUA_ERRGCMM:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008262 memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(L, -1));
8263 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008264 return -1;
Christopher Faulet08ed98f2020-07-28 10:33:25 +02008265#endif
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008266 default:
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008267 memprintf(err, "Lua unknown error: %s\n", lua_tostring(L, -1));
8268 lua_pop(L, 1);
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008269 return -1;
8270 }
8271
8272 return 0;
8273}
8274
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008275static int hlua_load(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008276 const struct proxy *defpx, const char *file, int line,
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008277 char **err)
8278{
8279 if (*(args[1]) == 0) {
8280 memprintf(err, "'%s' expects a file name as parameter.\n", args[0]);
8281 return -1;
8282 }
8283
Thierry Fournier59f11be2020-11-29 00:37:41 +01008284 /* loading for global state */
Thierry Fournierc7492592020-11-28 23:57:24 +01008285 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008286 ha_set_tid(0);
Thierry Fournierafc63e22020-11-28 17:06:51 +01008287 return hlua_load_state(args[1], hlua_states[0], err);
Thierry Fournierc93c15c2020-11-28 15:02:13 +01008288}
8289
Thierry Fournier59f11be2020-11-29 00:37:41 +01008290static int hlua_load_per_thread(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008291 const struct proxy *defpx, const char *file, int line,
Thierry Fournier59f11be2020-11-29 00:37:41 +01008292 char **err)
8293{
8294 int len;
8295
8296 if (*(args[1]) == 0) {
8297 memprintf(err, "'%s' expects a file as parameter.\n", args[0]);
8298 return -1;
8299 }
8300
8301 if (per_thread_load == NULL) {
8302 /* allocate the first entry large enough to store the final NULL */
8303 per_thread_load = calloc(1, sizeof(*per_thread_load));
8304 if (per_thread_load == NULL) {
8305 memprintf(err, "out of memory error");
8306 return -1;
8307 }
8308 }
8309
8310 /* count used entries */
8311 for (len = 0; per_thread_load[len] != NULL; len++)
8312 ;
8313
8314 per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load));
8315 if (per_thread_load == NULL) {
8316 memprintf(err, "out of memory error");
8317 return -1;
8318 }
8319
8320 per_thread_load[len] = strdup(args[1]);
8321 per_thread_load[len + 1] = NULL;
8322
8323 if (per_thread_load[len] == NULL) {
8324 memprintf(err, "out of memory error");
8325 return -1;
8326 }
8327
8328 /* loading for thread 1 only */
8329 hlua_state_id = 1;
8330 ha_set_tid(0);
8331 return hlua_load_state(args[1], hlua_states[1], err);
8332}
8333
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008334/* Prepend the given <path> followed by a semicolon to the `package.<type>` variable
8335 * in the given <ctx>.
8336 */
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008337static int hlua_prepend_path(lua_State *L, char *type, char *path)
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008338{
Thierry Fournier3fb9e512020-11-28 10:13:12 +01008339 lua_getglobal(L, "package"); /* push package variable */
8340 lua_pushstring(L, path); /* push given path */
8341 lua_pushstring(L, ";"); /* push semicolon */
8342 lua_getfield(L, -3, type); /* push old path */
8343 lua_concat(L, 3); /* concatenate to new path */
8344 lua_setfield(L, -2, type); /* store new path */
8345 lua_pop(L, 1); /* pop package variable */
Tim Duesterhusc9fc9f22020-01-12 13:55:39 +01008346
8347 return 0;
8348}
8349
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008350static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx,
Willy Tarreau01825162021-03-09 09:53:46 +01008351 const struct proxy *defpx, const char *file, int line,
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008352 char **err)
8353{
8354 char *path;
8355 char *type = "path";
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008356 struct prepend_path *p = NULL;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008357
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008358 if (too_many_args(2, args, err, NULL)) {
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008359 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008360 }
8361
8362 if (!(*args[1])) {
8363 memprintf(err, "'%s' expects to receive a <path> as argument", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008364 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008365 }
8366 path = args[1];
8367
8368 if (*args[2]) {
8369 if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) {
8370 memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]);
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008371 goto err;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008372 }
8373 type = args[2];
8374 }
8375
Thierry Fournier59f11be2020-11-29 00:37:41 +01008376 p = calloc(1, sizeof(*p));
8377 if (p == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008378 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008379 goto err;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008380 }
8381 p->path = strdup(path);
8382 if (p->path == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008383 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008384 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008385 }
8386 p->type = strdup(type);
8387 if (p->type == NULL) {
Tim Duesterhusf89d43a2021-01-03 20:04:37 +01008388 memprintf(err, "memory allocation failed");
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008389 goto err2;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008390 }
8391 LIST_ADDQ(&prepend_path_list, &p->l);
8392
8393 hlua_prepend_path(hlua_states[0], type, path);
8394 hlua_prepend_path(hlua_states[1], type, path);
8395 return 0;
Tim Duesterhus621e74a2021-01-03 20:04:36 +01008396
8397err2:
8398 free(p->type);
8399 free(p->path);
8400err:
8401 free(p);
8402 return -1;
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008403}
8404
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008405/* configuration keywords declaration */
8406static struct cfg_kw_list cfg_kws = {{ },{
Tim Duesterhusdd74b5f2020-01-12 13:55:40 +01008407 { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008408 { CFG_GLOBAL, "lua-load", hlua_load },
Thierry Fournier59f11be2020-11-29 00:37:41 +01008409 { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread },
Thierry FOURNIERbd413492015-03-03 16:52:26 +01008410 { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout },
8411 { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout },
Thierry FOURNIER56da1012015-10-01 08:42:31 +02008412 { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout },
Thierry FOURNIERee9f8022015-03-03 17:37:37 +01008413 { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield },
Willy Tarreau32f61e22015-03-18 17:54:59 +01008414 { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem },
Thierry FOURNIER6c9b52c2015-01-23 15:57:06 +01008415 { 0, NULL, NULL },
8416}};
8417
Willy Tarreau0108d902018-11-25 19:14:37 +01008418INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws);
8419
Christopher Fauletafd8f102018-11-08 11:34:21 +01008420
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008421/* This function can fail with an abort() due to an Lua critical error.
8422 * We are in the initialisation process of HAProxy, this abort() is
8423 * tolerated.
8424 */
Thierry Fournierb8cef172020-11-28 15:37:17 +01008425int hlua_post_init_state(lua_State *L)
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008426{
8427 struct hlua_init_function *init;
8428 const char *msg;
8429 enum hlua_exec ret;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008430 const char *error;
Thierry Fournier670db242020-11-28 10:49:59 +01008431 const char *kind;
8432 const char *trace;
8433 int return_status = 1;
8434#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
8435 int nres;
8436#endif
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008437
Willy Tarreaucdb53462020-12-02 12:12:00 +01008438 /* disable memory limit checks if limit is not set */
8439 if (!hlua_global_allocator.limit)
8440 hlua_global_allocator.limit = ~hlua_global_allocator.limit;
8441
Ilya Shipitsin856aabc2020-04-16 23:51:34 +05008442 /* Call post initialisation function in safe environment. */
Thierry Fournier3c539322020-11-28 16:05:05 +01008443 if (setjmp(safe_ljmp_env) != 0) {
8444 lua_atpanic(L, hlua_panic_safe);
Thierry Fournierb8cef172020-11-28 15:37:17 +01008445 if (lua_type(L, -1) == LUA_TSTRING)
8446 error = lua_tostring(L, -1);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008447 else
8448 error = "critical error";
8449 fprintf(stderr, "Lua post-init: %s.\n", error);
8450 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008451 } else {
8452 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008453 }
Frédéric Lécaille54f2bcf2018-08-29 13:46:24 +02008454
Thierry Fournierb8cef172020-11-28 15:37:17 +01008455 hlua_fcn_post_init(L);
Thierry Fournier3d4a6752016-02-19 20:53:30 +01008456
Thierry Fournierc7492592020-11-28 23:57:24 +01008457 list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) {
Thierry Fournierb8cef172020-11-28 15:37:17 +01008458 lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref);
Thierry Fournier670db242020-11-28 10:49:59 +01008459
8460#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504
Thierry Fournierb8cef172020-11-28 15:37:17 +01008461 ret = lua_resume(L, L, 0, &nres);
Thierry Fournier670db242020-11-28 10:49:59 +01008462#else
Thierry Fournierb8cef172020-11-28 15:37:17 +01008463 ret = lua_resume(L, L, 0);
Thierry Fournier670db242020-11-28 10:49:59 +01008464#endif
8465 kind = NULL;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008466 switch (ret) {
Thierry Fournier670db242020-11-28 10:49:59 +01008467
8468 case LUA_OK:
Thierry Fournierb8cef172020-11-28 15:37:17 +01008469 lua_pop(L, -1);
Thierry Fournier13d08b72020-11-28 11:02:58 +01008470 break;
Thierry Fournier670db242020-11-28 10:49:59 +01008471
8472 case LUA_ERRERR:
8473 kind = "message handler error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008474 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008475 case LUA_ERRRUN:
8476 if (!kind)
8477 kind = "runtime error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008478 msg = lua_tostring(L, -1);
8479 lua_settop(L, 0); /* Empty the stack. */
8480 lua_pop(L, 1);
8481 trace = hlua_traceback(L);
Thierry Fournier670db242020-11-28 10:49:59 +01008482 if (msg)
8483 ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace);
8484 else
8485 ha_alert("Lua init: unknown %s from %s\n", kind, trace);
8486 return_status = 0;
8487 break;
8488
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008489 default:
Thierry Fournier670db242020-11-28 10:49:59 +01008490 /* Unknown error */
8491 kind = "Unknown error";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008492 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008493 case LUA_YIELD:
8494 /* yield is not configured at this step, this state doesn't happen */
8495 if (!kind)
8496 kind = "yield not allowed";
Thayne McCombs8f0cc5c2021-01-07 21:35:52 -07008497 /* Fall through */
Thierry Fournier670db242020-11-28 10:49:59 +01008498 case LUA_ERRMEM:
8499 if (!kind)
8500 kind = "out of memory error";
Thierry Fournierb8cef172020-11-28 15:37:17 +01008501 lua_settop(L, 0);
8502 lua_pop(L, 1);
8503 trace = hlua_traceback(L);
Thierry Fournier670db242020-11-28 10:49:59 +01008504 ha_alert("Lua init: %s: %s\n", kind, trace);
8505 return_status = 0;
8506 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008507 }
Thierry Fournier670db242020-11-28 10:49:59 +01008508 if (!return_status)
8509 break;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008510 }
Thierry Fournier3c539322020-11-28 16:05:05 +01008511
8512 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier670db242020-11-28 10:49:59 +01008513 return return_status;
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008514}
8515
Thierry Fournierb8cef172020-11-28 15:37:17 +01008516int hlua_post_init()
8517{
Thierry Fournier59f11be2020-11-29 00:37:41 +01008518 int ret;
8519 int i;
8520 int errors;
8521 char *err = NULL;
8522 struct hlua_function *fcn;
8523
Thierry Fournierb8cef172020-11-28 15:37:17 +01008524#if USE_OPENSSL
8525 /* Initialize SSL server. */
8526 if (socket_ssl.xprt->prepare_srv) {
8527 int saved_used_backed = global.ssl_used_backend;
8528 // don't affect maxconn automatic computation
8529 socket_ssl.xprt->prepare_srv(&socket_ssl);
8530 global.ssl_used_backend = saved_used_backed;
8531 }
8532#endif
8533
Thierry Fournierc7492592020-11-28 23:57:24 +01008534 /* Perform post init of common thread */
8535 hlua_state_id = 0;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008536 ha_set_tid(0);
8537 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8538 if (ret == 0)
8539 return 0;
8540
8541 /* init remaining lua states and load files */
8542 for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8543
8544 /* set thread context */
8545 ha_set_tid(hlua_state_id - 1);
8546
8547 /* Init lua state */
8548 hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id);
8549
8550 /* Load lua files */
8551 for (i = 0; per_thread_load && per_thread_load[i]; i++) {
8552 ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err);
8553 if (ret != 0) {
8554 ha_alert("Lua init: %s\n", err);
8555 return 0;
8556 }
8557 }
8558 }
8559
8560 /* Reset thread context */
8561 ha_set_tid(0);
8562
8563 /* Execute post init for all states */
8564 for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) {
8565
8566 /* set thread context */
8567 ha_set_tid(hlua_state_id - 1);
8568
8569 /* run post init */
8570 ret = hlua_post_init_state(hlua_states[hlua_state_id]);
8571 if (ret == 0)
8572 return 0;
8573 }
8574
8575 /* Reset thread context */
8576 ha_set_tid(0);
8577
8578 /* control functions registering. Each function must have:
8579 * - only the function_ref[0] set positive and all other to -1
8580 * - only the function_ref[0] set to -1 and all other positive
8581 * This ensure a same reference is not used both in shared
8582 * lua state and thread dedicated lua state. Note: is the case
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008583 * reach, the shared state is priority, but the bug will be
Thierry Fournier59f11be2020-11-29 00:37:41 +01008584 * complicated to found for the end user.
8585 */
8586 errors = 0;
8587 list_for_each_entry(fcn, &referenced_functions, l) {
8588 ret = 0;
8589 for (i = 1; i < global.nbthread + 1; i++) {
8590 if (fcn->function_ref[i] == -1)
8591 ret--;
8592 else
8593 ret++;
8594 }
8595 if (abs(ret) != global.nbthread) {
8596 ha_alert("Lua function '%s' is not referenced in all thread. "
8597 "Expect function in all thread or in none thread.\n", fcn->name);
8598 errors++;
8599 continue;
8600 }
8601
8602 if ((fcn->function_ref[0] == -1) == (ret < 0)) {
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008603 ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) "
8604 "and per-thread Lua context (through lua-load-per-thread). these two context "
Thierry Fournier59f11be2020-11-29 00:37:41 +01008605 "exclusive.\n", fcn->name);
8606 errors++;
8607 }
8608 }
8609
8610 if (errors > 0)
8611 return 0;
8612
Ilya Shipitsinb8888ab2021-01-06 21:20:16 +05008613 /* after this point, this global will no longer be used, so set to
Thierry Fournier59f11be2020-11-29 00:37:41 +01008614 * -1 in order to have probably a segfault if someone use it
8615 */
8616 hlua_state_id = -1;
8617
8618 return 1;
Thierry Fournierb8cef172020-11-28 15:37:17 +01008619}
8620
Willy Tarreau32f61e22015-03-18 17:54:59 +01008621/* The memory allocator used by the Lua stack. <ud> is a pointer to the
8622 * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize>
8623 * is the previously allocated size or the kind of object in case of a new
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008624 * allocation. <nsize> is the requested new size. A new allocation is
8625 * indicated by <ptr> being NULL. A free is indicated by <nsize> being
Willy Tarreaucdb53462020-12-02 12:12:00 +01008626 * zero. This one verifies that the limits are respected but is optimized
8627 * for the fast case where limits are not used, hence stats are not updated.
Willy Tarreau32f61e22015-03-18 17:54:59 +01008628 */
8629static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize)
8630{
8631 struct hlua_mem_allocator *zone = ud;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008632 size_t limit, old, new;
8633
Tim Duesterhus22586522021-01-08 10:35:33 +01008634 if (unlikely(!ptr && !nsize))
8635 return NULL;
8636
Willy Tarreaucdb53462020-12-02 12:12:00 +01008637 /* a limit of ~0 means unlimited and boot complete, so there's no need
8638 * for accounting anymore.
8639 */
Christopher Fauleta61789a2021-03-19 15:16:28 +01008640 if (likely(~zone->limit == 0)) {
8641 hlua_not_dumpable++;
8642 ptr = realloc(ptr, nsize);
8643 hlua_not_dumpable--;
8644 return ptr;
8645 }
Willy Tarreau32f61e22015-03-18 17:54:59 +01008646
Willy Tarreaud36c7fa2020-12-02 12:26:29 +01008647 if (!ptr)
8648 osize = 0;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008649
Willy Tarreaucdb53462020-12-02 12:12:00 +01008650 /* enforce strict limits across all threads */
8651 limit = zone->limit;
8652 old = _HA_ATOMIC_LOAD(&zone->allocated);
8653 do {
8654 new = old + nsize - osize;
8655 if (unlikely(nsize && limit && new > limit))
8656 return NULL;
8657 } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new));
Willy Tarreau32f61e22015-03-18 17:54:59 +01008658
Christopher Fauleta61789a2021-03-19 15:16:28 +01008659 hlua_not_dumpable++;
Willy Tarreau32f61e22015-03-18 17:54:59 +01008660 ptr = realloc(ptr, nsize);
Christopher Fauleta61789a2021-03-19 15:16:28 +01008661 hlua_not_dumpable--;
Willy Tarreaucdb53462020-12-02 12:12:00 +01008662
8663 if (unlikely(!ptr && nsize)) // failed
8664 _HA_ATOMIC_SUB(&zone->allocated, nsize - osize);
8665
8666 __ha_barrier_atomic_store();
Willy Tarreau32f61e22015-03-18 17:54:59 +01008667 return ptr;
8668}
8669
Thierry Fournierecb83c22020-11-28 15:49:44 +01008670/* This function can fail with an abort() due to a Lua critical error.
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008671 * We are in the initialisation process of HAProxy, this abort() is
8672 * tolerated.
8673 */
Thierry Fournierecb83c22020-11-28 15:49:44 +01008674lua_State *hlua_init_state(int thread_num)
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01008675{
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008676 int i;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008677 int idx;
8678 struct sample_fetch *sf;
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008679 struct sample_conv *sc;
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008680 char *p;
Thierry Fournierfd107a22016-02-19 19:57:23 +01008681 const char *error_msg;
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008682 void **context;
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008683 lua_State *L;
Thierry Fournier59f11be2020-11-29 00:37:41 +01008684 struct prepend_path *pp;
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008685
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008686 /* Init main lua stack. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008687 L = lua_newstate(hlua_alloc, &hlua_global_allocator);
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008688
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008689 /* Initialise Lua context to NULL */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008690 context = lua_getextraspace(L);
Thierry Fournier4234dbd2020-11-28 13:18:23 +01008691 *context = NULL;
8692
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08008693 /* From this point, until the end of the initialisation function,
Thierry FOURNIERbabae282015-09-17 11:36:37 +02008694 * the Lua function can fail with an abort. We are in the initialisation
8695 * process of HAProxy, this abort() is tolerated.
8696 */
8697
Thierry Fournier3c539322020-11-28 16:05:05 +01008698 /* Call post initialisation function in safe environment. */
8699 if (setjmp(safe_ljmp_env) != 0) {
8700 lua_atpanic(L, hlua_panic_safe);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008701 if (lua_type(L, -1) == LUA_TSTRING)
8702 error_msg = lua_tostring(L, -1);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008703 else
8704 error_msg = "critical error";
8705 fprintf(stderr, "Lua init: %s.\n", error_msg);
8706 exit(1);
Thierry Fournier3c539322020-11-28 16:05:05 +01008707 } else {
8708 lua_atpanic(L, hlua_panic_ljmp);
Thierry Fournier2f05cc62020-11-28 16:08:02 +01008709 }
8710
Thierry FOURNIER380d0932015-01-23 14:27:52 +01008711 /* Initialise lua. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008712 luaL_openlibs(L);
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008713#define HLUA_PREPEND_PATH_TOSTRING1(x) #x
8714#define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x)
8715#ifdef HLUA_PREPEND_PATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008716 hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008717#endif
8718#ifdef HLUA_PREPEND_CPATH
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008719 hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH));
Tim Duesterhus541fe1e2020-01-12 13:55:41 +01008720#endif
8721#undef HLUA_PREPEND_PATH_TOSTRING
8722#undef HLUA_PREPEND_PATH_TOSTRING1
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008723
Thierry Fournier59f11be2020-11-29 00:37:41 +01008724 /* Apply configured prepend path */
8725 list_for_each_entry(pp, &prepend_path_list, l)
8726 hlua_prepend_path(L, pp->type, pp->path);
8727
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008728 /*
8729 *
8730 * Create "core" object.
8731 *
8732 */
8733
Thierry FOURNIERa2d8c652015-03-11 17:29:39 +01008734 /* This table entry is the object "core" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008735 lua_newtable(L);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008736
Thierry Fournierecb83c22020-11-28 15:49:44 +01008737 /* set the thread id */
8738 hlua_class_const_int(L, "thread", thread_num);
8739
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008740 /* Push the loglevel constants. */
Willy Tarreau80f5fae2015-02-27 16:38:20 +01008741 for (i = 0; i < NB_LOG_LEVELS; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008742 hlua_class_const_int(L, log_levels[i], i);
Thierry FOURNIER2ba18a22015-01-23 14:07:08 +01008743
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008744 /* Register special functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008745 hlua_class_function(L, "register_init", hlua_register_init);
8746 hlua_class_function(L, "register_task", hlua_register_task);
8747 hlua_class_function(L, "register_fetches", hlua_register_fetches);
8748 hlua_class_function(L, "register_converters", hlua_register_converters);
8749 hlua_class_function(L, "register_action", hlua_register_action);
8750 hlua_class_function(L, "register_service", hlua_register_service);
8751 hlua_class_function(L, "register_cli", hlua_register_cli);
8752 hlua_class_function(L, "yield", hlua_yield);
8753 hlua_class_function(L, "set_nice", hlua_set_nice);
8754 hlua_class_function(L, "sleep", hlua_sleep);
8755 hlua_class_function(L, "msleep", hlua_msleep);
8756 hlua_class_function(L, "add_acl", hlua_add_acl);
8757 hlua_class_function(L, "del_acl", hlua_del_acl);
8758 hlua_class_function(L, "set_map", hlua_set_map);
8759 hlua_class_function(L, "del_map", hlua_del_map);
8760 hlua_class_function(L, "tcp", hlua_socket_new);
8761 hlua_class_function(L, "log", hlua_log);
8762 hlua_class_function(L, "Debug", hlua_log_debug);
8763 hlua_class_function(L, "Info", hlua_log_info);
8764 hlua_class_function(L, "Warning", hlua_log_warning);
8765 hlua_class_function(L, "Alert", hlua_log_alert);
8766 hlua_class_function(L, "done", hlua_done);
8767 hlua_fcn_reg_core_fcn(L);
Thierry FOURNIERa4a0f3d2015-01-23 12:08:30 +01008768
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008769 lua_setglobal(L, "core");
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008770
8771 /*
8772 *
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008773 * Create "act" object.
8774 *
8775 */
8776
8777 /* This table entry is the object "act" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008778 lua_newtable(L);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008779
8780 /* push action return constants */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008781 hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT);
8782 hlua_class_const_int(L, "STOP", ACT_RET_STOP);
8783 hlua_class_const_int(L, "YIELD", ACT_RET_YIELD);
8784 hlua_class_const_int(L, "ERROR", ACT_RET_ERR);
8785 hlua_class_const_int(L, "DONE", ACT_RET_DONE);
8786 hlua_class_const_int(L, "DENY", ACT_RET_DENY);
8787 hlua_class_const_int(L, "ABORT", ACT_RET_ABRT);
8788 hlua_class_const_int(L, "INVALID", ACT_RET_INV);
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008789
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008790 hlua_class_function(L, "wake_time", hlua_set_wake_time);
Christopher Faulet2c2c2e32020-01-31 19:07:52 +01008791
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008792 lua_setglobal(L, "act");
Christopher Faulet0f3c8902020-01-31 18:57:12 +01008793
8794 /*
8795 *
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008796 * Register class Map
8797 *
8798 */
8799
8800 /* This table entry is the object "Map" base. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008801 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008802
8803 /* register pattern types. */
8804 for (i=0; i<PAT_MATCH_NUM; i++)
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008805 hlua_class_const_int(L, pat_match_names[i], i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008806 for (i=0; i<PAT_MATCH_NUM; i++) {
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008807 snprintf(trash.area, trash.size, "_%s", pat_match_names[i]);
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008808 hlua_class_const_int(L, trash.area, i);
Thierry FOURNIER4dc71972017-01-28 08:33:08 +01008809 }
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008810
8811 /* register constructor. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008812 hlua_class_function(L, "new", hlua_map_new);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008813
8814 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008815 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008816
Ilya Shipitsind4259502020-04-08 01:07:56 +05008817 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008818 lua_pushstring(L, "__index");
8819 lua_newtable(L);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008820
8821 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008822 hlua_class_function(L, "lookup", hlua_map_lookup);
8823 hlua_class_function(L, "slookup", hlua_map_slookup);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008824
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008825 lua_rawset(L, -3);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008826
Thierry Fournier45e78d72016-02-19 18:34:46 +01008827 /* Register previous table in the registry with reference and named entry.
8828 * The function hlua_register_metatable() pops the stack, so we
8829 * previously create a copy of the table.
8830 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008831 lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */
8832 class_map_ref = hlua_register_metatable(L, CLASS_MAP);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008833
8834 /* Assign the metatable to the mai Map object. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008835 lua_setmetatable(L, -2);
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008836
8837 /* Set a name to the table. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008838 lua_setglobal(L, "Map");
Thierry FOURNIER3def3932015-04-07 11:27:54 +02008839
8840 /*
8841 *
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008842 * Register class Channel
8843 *
8844 */
8845
8846 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008847 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008848
Ilya Shipitsind4259502020-04-08 01:07:56 +05008849 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008850 lua_pushstring(L, "__index");
8851 lua_newtable(L);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008852
8853 /* Register . */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008854 hlua_class_function(L, "get", hlua_channel_get);
8855 hlua_class_function(L, "dup", hlua_channel_dup);
8856 hlua_class_function(L, "getline", hlua_channel_getline);
8857 hlua_class_function(L, "set", hlua_channel_set);
8858 hlua_class_function(L, "append", hlua_channel_append);
8859 hlua_class_function(L, "send", hlua_channel_send);
8860 hlua_class_function(L, "forward", hlua_channel_forward);
8861 hlua_class_function(L, "get_in_len", hlua_channel_get_in_len);
8862 hlua_class_function(L, "get_out_len", hlua_channel_get_out_len);
8863 hlua_class_function(L, "is_full", hlua_channel_is_full);
8864 hlua_class_function(L, "is_resp", hlua_channel_is_resp);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008865
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008866 lua_rawset(L, -3);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008867
8868 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008869 class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL);
Thierry FOURNIER5a6d3fd2015-02-09 16:38:34 +01008870
8871 /*
8872 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008873 * Register class Fetches
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008874 *
8875 */
8876
8877 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008878 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008879
Ilya Shipitsind4259502020-04-08 01:07:56 +05008880 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008881 lua_pushstring(L, "__index");
8882 lua_newtable(L);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01008883
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008884 /* Browse existing fetches and create the associated
8885 * object method.
8886 */
8887 sf = NULL;
8888 while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) {
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008889 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8890 * by an underscore.
8891 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008892 strncpy(trash.area, sf->kw, trash.size);
8893 trash.area[trash.size - 1] = '\0';
8894 for (p = trash.area; *p; p++)
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008895 if (*p == '.' || *p == '-' || *p == '+')
8896 *p = '_';
8897
8898 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008899 lua_pushstring(L, trash.area);
8900 lua_pushlightuserdata(L, sf);
8901 lua_pushcclosure(L, hlua_run_sample_fetch, 1);
8902 lua_rawset(L, -3);
Thierry FOURNIERd0fa5382015-02-16 20:14:51 +01008903 }
8904
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008905 lua_rawset(L, -3);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008906
8907 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008908 class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01008909
8910 /*
8911 *
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008912 * Register class Converters
8913 *
8914 */
8915
8916 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008917 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008918
8919 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008920 lua_pushstring(L, "__index");
8921 lua_newtable(L);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008922
8923 /* Browse existing converters and create the associated
8924 * object method.
8925 */
8926 sc = NULL;
8927 while ((sc = sample_conv_getnext(sc, &idx)) != NULL) {
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008928 /* gL.Tua doesn't support '.' and '-' in the function names, replace it
8929 * by an underscore.
8930 */
Willy Tarreau843b7cb2018-07-13 10:54:26 +02008931 strncpy(trash.area, sc->kw, trash.size);
8932 trash.area[trash.size - 1] = '\0';
8933 for (p = trash.area; *p; p++)
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008934 if (*p == '.' || *p == '-' || *p == '+')
8935 *p = '_';
8936
8937 /* Register the function. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008938 lua_pushstring(L, trash.area);
8939 lua_pushlightuserdata(L, sc);
8940 lua_pushcclosure(L, hlua_run_sample_conv, 1);
8941 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008942 }
8943
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008944 lua_rawset(L, -3);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008945
8946 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008947 class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS);
Thierry FOURNIER594afe72015-03-10 23:58:30 +01008948
8949 /*
8950 *
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008951 * Register class HTTP
8952 *
8953 */
8954
8955 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008956 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008957
Ilya Shipitsind4259502020-04-08 01:07:56 +05008958 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008959 lua_pushstring(L, "__index");
8960 lua_newtable(L);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008961
8962 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008963 hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers);
8964 hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr);
8965 hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr);
8966 hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val);
8967 hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr);
8968 hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr);
8969 hlua_class_function(L, "req_set_method", hlua_http_req_set_meth);
8970 hlua_class_function(L, "req_set_path", hlua_http_req_set_path);
8971 hlua_class_function(L, "req_set_query", hlua_http_req_set_query);
8972 hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008973
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008974 hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers);
8975 hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr);
8976 hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr);
8977 hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val);
8978 hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr);
8979 hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr);
8980 hlua_class_function(L, "res_set_status", hlua_http_res_set_status);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008981
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008982 lua_rawset(L, -3);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008983
8984 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008985 class_http_ref = hlua_register_metatable(L, CLASS_HTTP);
Thierry FOURNIER08504f42015-03-16 14:17:08 +01008986
8987 /*
8988 *
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008989 * Register class AppletTCP
8990 *
8991 */
8992
8993 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008994 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008995
Ilya Shipitsind4259502020-04-08 01:07:56 +05008996 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01008997 lua_pushstring(L, "__index");
8998 lua_newtable(L);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02008999
9000 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009001 hlua_class_function(L, "getline", hlua_applet_tcp_getline);
9002 hlua_class_function(L, "receive", hlua_applet_tcp_recv);
9003 hlua_class_function(L, "send", hlua_applet_tcp_send);
9004 hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv);
9005 hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv);
9006 hlua_class_function(L, "set_var", hlua_applet_tcp_set_var);
9007 hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var);
9008 hlua_class_function(L, "get_var", hlua_applet_tcp_get_var);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009009
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009010 lua_settable(L, -3);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009011
9012 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009013 class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP);
Thierry FOURNIERf0a64b62015-09-19 12:36:17 +02009014
9015 /*
9016 *
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009017 * Register class AppletHTTP
9018 *
9019 */
9020
9021 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009022 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009023
Ilya Shipitsind4259502020-04-08 01:07:56 +05009024 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009025 lua_pushstring(L, "__index");
9026 lua_newtable(L);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009027
9028 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009029 hlua_class_function(L, "set_priv", hlua_applet_http_set_priv);
9030 hlua_class_function(L, "get_priv", hlua_applet_http_get_priv);
9031 hlua_class_function(L, "set_var", hlua_applet_http_set_var);
9032 hlua_class_function(L, "unset_var", hlua_applet_http_unset_var);
9033 hlua_class_function(L, "get_var", hlua_applet_http_get_var);
9034 hlua_class_function(L, "getline", hlua_applet_http_getline);
9035 hlua_class_function(L, "receive", hlua_applet_http_recv);
9036 hlua_class_function(L, "send", hlua_applet_http_send);
9037 hlua_class_function(L, "add_header", hlua_applet_http_addheader);
9038 hlua_class_function(L, "set_status", hlua_applet_http_status);
9039 hlua_class_function(L, "start_response", hlua_applet_http_start_response);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009040
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009041 lua_settable(L, -3);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009042
9043 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009044 class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP);
Thierry FOURNIERa30b5db2015-09-18 09:04:27 +02009045
9046 /*
9047 *
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009048 * Register class TXN
9049 *
9050 */
9051
9052 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009053 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009054
Ilya Shipitsind4259502020-04-08 01:07:56 +05009055 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009056 lua_pushstring(L, "__index");
9057 lua_newtable(L);
Thierry FOURNIERbb53c7b2015-03-11 18:28:02 +01009058
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009059 /* Register Lua functions. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009060 hlua_class_function(L, "set_priv", hlua_set_priv);
9061 hlua_class_function(L, "get_priv", hlua_get_priv);
9062 hlua_class_function(L, "set_var", hlua_set_var);
9063 hlua_class_function(L, "unset_var", hlua_unset_var);
9064 hlua_class_function(L, "get_var", hlua_get_var);
9065 hlua_class_function(L, "done", hlua_txn_done);
9066 hlua_class_function(L, "reply", hlua_txn_reply_new);
9067 hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel);
9068 hlua_class_function(L, "set_tos", hlua_txn_set_tos);
9069 hlua_class_function(L, "set_mark", hlua_txn_set_mark);
9070 hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class);
9071 hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset);
9072 hlua_class_function(L, "deflog", hlua_txn_deflog);
9073 hlua_class_function(L, "log", hlua_txn_log);
9074 hlua_class_function(L, "Debug", hlua_txn_log_debug);
9075 hlua_class_function(L, "Info", hlua_txn_log_info);
9076 hlua_class_function(L, "Warning", hlua_txn_log_warning);
9077 hlua_class_function(L, "Alert", hlua_txn_log_alert);
Thierry FOURNIER05c0b8a2015-02-25 11:43:21 +01009078
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009079 lua_rawset(L, -3);
Thierry FOURNIER65f34c62015-02-16 20:11:43 +01009080
9081 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009082 class_txn_ref = hlua_register_metatable(L, CLASS_TXN);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009083
9084 /*
9085 *
Christopher Faulet700d9e82020-01-31 12:21:52 +01009086 * Register class reply
9087 *
9088 */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009089 lua_newtable(L);
9090 lua_pushstring(L, "__index");
9091 lua_newtable(L);
9092 hlua_class_function(L, "set_status", hlua_txn_reply_set_status);
9093 hlua_class_function(L, "add_header", hlua_txn_reply_add_header);
9094 hlua_class_function(L, "del_header", hlua_txn_reply_del_header);
9095 hlua_class_function(L, "set_body", hlua_txn_reply_set_body);
9096 lua_settable(L, -3); /* Sets the __index entry. */
9097 class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX);
Christopher Faulet700d9e82020-01-31 12:21:52 +01009098
9099
9100 /*
9101 *
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009102 * Register class Socket
9103 *
9104 */
9105
9106 /* Create and fill the metatable. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009107 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009108
Ilya Shipitsind4259502020-04-08 01:07:56 +05009109 /* Create and fill the __index entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009110 lua_pushstring(L, "__index");
9111 lua_newtable(L);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009112
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009113#ifdef USE_OPENSSL
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009114 hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl);
Baptiste Assmann84bb4932015-03-02 21:40:06 +01009115#endif
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009116 hlua_class_function(L, "connect", hlua_socket_connect);
9117 hlua_class_function(L, "send", hlua_socket_send);
9118 hlua_class_function(L, "receive", hlua_socket_receive);
9119 hlua_class_function(L, "close", hlua_socket_close);
9120 hlua_class_function(L, "getpeername", hlua_socket_getpeername);
9121 hlua_class_function(L, "getsockname", hlua_socket_getsockname);
9122 hlua_class_function(L, "setoption", hlua_socket_setoption);
9123 hlua_class_function(L, "settimeout", hlua_socket_settimeout);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009124
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009125 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009126
9127 /* Register the garbage collector entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009128 lua_pushstring(L, "__gc");
9129 lua_pushcclosure(L, hlua_socket_gc, 0);
9130 lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009131
9132 /* Register previous table in the registry with reference and named entry. */
Thierry Fournier1eac28f2020-11-28 12:26:24 +01009133 class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009134
Thierry Fournieraafc7772020-12-04 11:47:47 +01009135 lua_atpanic(L, hlua_panic_safe);
9136
9137 return L;
9138}
9139
9140void hlua_init(void) {
9141 int i;
Thierry Fournieraafc7772020-12-04 11:47:47 +01009142#ifdef USE_OPENSSL
9143 struct srv_kw *kw;
9144 int tmp_error;
9145 char *error;
9146 char *args[] = { /* SSL client configuration. */
9147 "ssl",
9148 "verify",
9149 "none",
9150 NULL
9151 };
9152#endif
9153
9154 /* Init post init function list head */
9155 for (i = 0; i < MAX_THREADS + 1; i++)
9156 LIST_INIT(&hlua_init_functions[i]);
9157
9158 /* Init state for common/shared lua parts */
9159 hlua_state_id = 0;
9160 ha_set_tid(0);
9161 hlua_states[0] = hlua_init_state(0);
9162
9163 /* Init state 1 for thread 0. We have at least one thread. */
9164 hlua_state_id = 1;
9165 ha_set_tid(0);
9166 hlua_states[1] = hlua_init_state(1);
9167
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009168 /* Proxy and server configuration initialisation. */
9169 memset(&socket_proxy, 0, sizeof(socket_proxy));
9170 init_new_proxy(&socket_proxy);
9171 socket_proxy.parent = NULL;
9172 socket_proxy.last_change = now.tv_sec;
9173 socket_proxy.id = "LUA-SOCKET";
9174 socket_proxy.cap = PR_CAP_FE | PR_CAP_BE;
9175 socket_proxy.maxconn = 0;
9176 socket_proxy.accept = NULL;
9177 socket_proxy.options2 |= PR_O2_INDEPSTR;
9178 socket_proxy.srv = NULL;
9179 socket_proxy.conn_retries = 0;
9180 socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */
9181
9182 /* Init TCP server: unchanged parameters */
9183 memset(&socket_tcp, 0, sizeof(socket_tcp));
9184 socket_tcp.next = NULL;
9185 socket_tcp.proxy = &socket_proxy;
9186 socket_tcp.obj_type = OBJ_TYPE_SERVER;
Patrick Hemmer0355dab2018-05-11 12:52:31 -04009187 socket_tcp.pendconns = EB_ROOT;
Willy Tarreau198e92a2021-03-05 10:23:32 +01009188 LIST_ADD(&servers_list, &socket_tcp.global_list);
Emeric Brun52a91d32017-08-31 14:41:55 +02009189 socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009190 socket_tcp.last_change = 0;
Willy Tarreau0f143af2021-03-05 10:41:48 +01009191 socket_tcp.conf.file = strdup("HLUA_INTERNAL");
9192 socket_tcp.conf.line = 1;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009193 socket_tcp.id = "LUA-TCP-CONN";
9194 socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
9195 socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
9196 socket_tcp.pp_opts = 0; /* Remove proxy protocol. */
9197
9198 /* XXX: Copy default parameter from default server,
9199 * but the default server is not initialized.
9200 */
9201 socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue;
9202 socket_tcp.minconn = socket_proxy.defsrv.minconn;
9203 socket_tcp.maxconn = socket_proxy.defsrv.maxconn;
9204 socket_tcp.slowstart = socket_proxy.defsrv.slowstart;
9205 socket_tcp.onerror = socket_proxy.defsrv.onerror;
9206 socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
9207 socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup;
9208 socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
9209 socket_tcp.uweight = socket_proxy.defsrv.iweight;
9210 socket_tcp.iweight = socket_proxy.defsrv.iweight;
9211
9212 socket_tcp.check.status = HCHK_STATUS_INI;
9213 socket_tcp.check.rise = socket_proxy.defsrv.check.rise;
9214 socket_tcp.check.fall = socket_proxy.defsrv.check.fall;
9215 socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */
9216 socket_tcp.check.server = &socket_tcp;
9217
9218 socket_tcp.agent.status = HCHK_STATUS_INI;
9219 socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise;
9220 socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall;
9221 socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */
9222 socket_tcp.agent.server = &socket_tcp;
9223
Willy Tarreaua261e9b2016-12-22 20:44:00 +01009224 socket_tcp.xprt = xprt_get(XPRT_RAW);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009225
9226#ifdef USE_OPENSSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009227 /* Init TCP server: unchanged parameters */
9228 memset(&socket_ssl, 0, sizeof(socket_ssl));
9229 socket_ssl.next = NULL;
9230 socket_ssl.proxy = &socket_proxy;
9231 socket_ssl.obj_type = OBJ_TYPE_SERVER;
Patrick Hemmer0355dab2018-05-11 12:52:31 -04009232 socket_ssl.pendconns = EB_ROOT;
Willy Tarreau198e92a2021-03-05 10:23:32 +01009233 LIST_ADD(&servers_list, &socket_ssl.global_list);
Emeric Brun52a91d32017-08-31 14:41:55 +02009234 socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009235 socket_ssl.last_change = 0;
Willy Tarreau0f143af2021-03-05 10:41:48 +01009236 socket_ssl.conf.file = strdup("HLUA_INTERNAL");
9237 socket_ssl.conf.line = 2;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009238 socket_ssl.id = "LUA-SSL-CONN";
9239 socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
9240 socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */
9241 socket_ssl.pp_opts = 0; /* Remove proxy protocol. */
9242
9243 /* XXX: Copy default parameter from default server,
9244 * but the default server is not initialized.
9245 */
9246 socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue;
9247 socket_ssl.minconn = socket_proxy.defsrv.minconn;
9248 socket_ssl.maxconn = socket_proxy.defsrv.maxconn;
9249 socket_ssl.slowstart = socket_proxy.defsrv.slowstart;
9250 socket_ssl.onerror = socket_proxy.defsrv.onerror;
9251 socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown;
9252 socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup;
9253 socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit;
9254 socket_ssl.uweight = socket_proxy.defsrv.iweight;
9255 socket_ssl.iweight = socket_proxy.defsrv.iweight;
9256
9257 socket_ssl.check.status = HCHK_STATUS_INI;
9258 socket_ssl.check.rise = socket_proxy.defsrv.check.rise;
9259 socket_ssl.check.fall = socket_proxy.defsrv.check.fall;
9260 socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */
9261 socket_ssl.check.server = &socket_ssl;
9262
9263 socket_ssl.agent.status = HCHK_STATUS_INI;
9264 socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise;
9265 socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall;
9266 socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */
9267 socket_ssl.agent.server = &socket_ssl;
9268
Thierry FOURNIER36d13742015-03-17 16:48:53 +01009269 socket_ssl.use_ssl = 1;
Willy Tarreaua261e9b2016-12-22 20:44:00 +01009270 socket_ssl.xprt = xprt_get(XPRT_SSL);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009271
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009272 for (i = 0; args[i] != NULL; i++) {
9273 if ((kw = srv_find_kw(args[i])) != NULL) { /* Maybe it's registered server keyword */
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009274 /*
9275 *
9276 * If the keyword is not known, we can search in the registered
Joseph Herlantb8f9c5e2018-11-15 10:06:08 -08009277 * server keywords. This is useful to configure special SSL
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009278 * features like client certificates and ssl_verify.
9279 *
9280 */
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009281 tmp_error = kw->parse(args, &i, &socket_proxy, &socket_ssl, &error);
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009282 if (tmp_error != 0) {
9283 fprintf(stderr, "INTERNAL ERROR: %s\n", error);
9284 abort(); /* This must be never arrives because the command line
9285 not editable by the user. */
9286 }
Bertrand Jacquin80839ff2021-01-21 19:14:46 +00009287 i += kw->skip;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009288 }
9289 }
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +01009290#endif
Thierry Fournier75933d42016-01-21 09:30:18 +01009291
Thierry FOURNIER6f1fd482015-01-23 14:06:13 +01009292}
Willy Tarreaubb57d942016-12-21 19:04:56 +01009293
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009294static void hlua_deinit()
9295{
Willy Tarreau186f3762020-12-04 11:48:12 +01009296 int thr;
9297
9298 for (thr = 0; thr < MAX_THREADS+1; thr++) {
9299 if (hlua_states[thr])
9300 lua_close(hlua_states[thr]);
9301 }
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009302
9303 ha_free(&socket_tcp.per_thr);
Willy Tarreau0f143af2021-03-05 10:41:48 +01009304 ha_free((char**)&socket_tcp.conf.file);
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009305
Willy Tarreau0f143af2021-03-05 10:41:48 +01009306#ifdef USE_OPENSSL
Willy Tarreau430bf4a2021-03-04 09:45:32 +01009307 ha_free(&socket_ssl.per_thr);
Willy Tarreau0f143af2021-03-05 10:41:48 +01009308 ha_free((char**)&socket_ssl.conf.file);
9309#endif
Tim Duesterhusd0c0ca22020-07-04 11:53:26 +02009310}
9311
9312REGISTER_POST_DEINIT(hlua_deinit);
9313
Willy Tarreau80713382018-11-26 10:19:54 +01009314static void hlua_register_build_options(void)
9315{
Willy Tarreaubb57d942016-12-21 19:04:56 +01009316 char *ptr = NULL;
Willy Tarreau80713382018-11-26 10:19:54 +01009317
Willy Tarreaubb57d942016-12-21 19:04:56 +01009318 memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE);
9319 hap_register_build_opts(ptr, 1);
9320}
Willy Tarreau80713382018-11-26 10:19:54 +01009321
9322INITCALL0(STG_REGISTER, hlua_register_build_options);